Commit | Line | Data |
---|---|---|
2b188cc1 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Shared application/kernel submission and completion ring pairs, for | |
4 | * supporting fast/efficient IO. | |
5 | * | |
6 | * A note on the read/write ordering memory barriers that are matched between | |
1e84b97b SB |
7 | * the application and kernel side. |
8 | * | |
9 | * After the application reads the CQ ring tail, it must use an | |
10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses | |
11 | * before writing the tail (using smp_load_acquire to read the tail will | |
12 | * do). It also needs a smp_mb() before updating CQ head (ordering the | |
13 | * entry load(s) with the head store), pairing with an implicit barrier | |
d068b506 | 14 | * through a control-dependency in io_get_cqe (smp_store_release to |
1e84b97b SB |
15 | * store head will do). Failure to do so could lead to reading invalid |
16 | * CQ entries. | |
17 | * | |
18 | * Likewise, the application must use an appropriate smp_wmb() before | |
19 | * writing the SQ tail (ordering SQ entry stores with the tail store), | |
20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release | |
21 | * to store the tail will do). And it needs a barrier ordering the SQ | |
22 | * head load before writing new SQ entries (smp_load_acquire to read | |
23 | * head will do). | |
24 | * | |
25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application | |
26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* | |
27 | * updating the SQ tail; a full memory barrier smp_mb() is needed | |
28 | * between. | |
2b188cc1 JA |
29 | * |
30 | * Also see the examples in the liburing library: | |
31 | * | |
32 | * git://git.kernel.dk/liburing | |
33 | * | |
34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens | |
35 | * from data shared between the kernel and application. This is done both | |
36 | * for ordering purposes, but also to ensure that once a value is loaded from | |
37 | * data that the application could potentially modify, it remains stable. | |
38 | * | |
39 | * Copyright (C) 2018-2019 Jens Axboe | |
c992fe29 | 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
2b188cc1 JA |
41 | */ |
42 | #include <linux/kernel.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/errno.h> | |
45 | #include <linux/syscalls.h> | |
46 | #include <linux/compat.h> | |
52de1fe1 | 47 | #include <net/compat.h> |
2b188cc1 JA |
48 | #include <linux/refcount.h> |
49 | #include <linux/uio.h> | |
6b47ee6e | 50 | #include <linux/bits.h> |
2b188cc1 JA |
51 | |
52 | #include <linux/sched/signal.h> | |
53 | #include <linux/fs.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/fdtable.h> | |
56 | #include <linux/mm.h> | |
57 | #include <linux/mman.h> | |
2b188cc1 JA |
58 | #include <linux/percpu.h> |
59 | #include <linux/slab.h> | |
edce22e1 | 60 | #include <linux/blk-mq.h> |
edafccee | 61 | #include <linux/bvec.h> |
2b188cc1 JA |
62 | #include <linux/net.h> |
63 | #include <net/sock.h> | |
64 | #include <net/af_unix.h> | |
6b06314c | 65 | #include <net/scm.h> |
adc8682e | 66 | #include <net/busy_poll.h> |
2b188cc1 JA |
67 | #include <linux/anon_inodes.h> |
68 | #include <linux/sched/mm.h> | |
69 | #include <linux/uaccess.h> | |
70 | #include <linux/nospec.h> | |
edafccee JA |
71 | #include <linux/sizes.h> |
72 | #include <linux/hugetlb.h> | |
aa4c3967 | 73 | #include <linux/highmem.h> |
15b71abe JA |
74 | #include <linux/namei.h> |
75 | #include <linux/fsnotify.h> | |
4840e418 | 76 | #include <linux/fadvise.h> |
3e4827b0 | 77 | #include <linux/eventpoll.h> |
7d67af2c | 78 | #include <linux/splice.h> |
b41e9852 | 79 | #include <linux/task_work.h> |
bcf5a063 | 80 | #include <linux/pagemap.h> |
0f212204 | 81 | #include <linux/io_uring.h> |
5bd2182d | 82 | #include <linux/audit.h> |
cdc1404a | 83 | #include <linux/security.h> |
2b188cc1 | 84 | |
c826bd7a DD |
85 | #define CREATE_TRACE_POINTS |
86 | #include <trace/events/io_uring.h> | |
87 | ||
2b188cc1 JA |
88 | #include <uapi/linux/io_uring.h> |
89 | ||
90 | #include "internal.h" | |
561fb04a | 91 | #include "io-wq.h" |
2b188cc1 | 92 | |
5277deaa | 93 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 94 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
4ce8ad95 | 95 | #define IORING_SQPOLL_CAP_ENTRIES_VALUE 8 |
65e19f54 | 96 | |
187f08c1 | 97 | /* only define max */ |
042b0d85 | 98 | #define IORING_MAX_FIXED_FILES (1U << 15) |
21b55dbc SG |
99 | #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
100 | IORING_REGISTER_LAST + IORING_OP_LAST) | |
2b188cc1 | 101 | |
187f08c1 | 102 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
2d091d62 PB |
103 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) |
104 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
105 | ||
489809e2 PB |
106 | #define IORING_MAX_REG_BUFFERS (1U << 14) |
107 | ||
68fe256a PB |
108 | #define SQE_COMMON_FLAGS (IOSQE_FIXED_FILE | IOSQE_IO_LINK | \ |
109 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) | |
110 | ||
5562a8d7 PB |
111 | #define SQE_VALID_FLAGS (SQE_COMMON_FLAGS | IOSQE_BUFFER_SELECT | \ |
112 | IOSQE_IO_DRAIN | IOSQE_CQE_SKIP_SUCCESS) | |
68fe256a | 113 | |
c854357b | 114 | #define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ |
d886e185 PB |
115 | REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ |
116 | REQ_F_ASYNC_DATA) | |
b16fed66 | 117 | |
09899b19 PB |
118 | #define IO_TCTX_REFS_CACHE_NR (1U << 10) |
119 | ||
2b188cc1 JA |
120 | struct io_uring { |
121 | u32 head ____cacheline_aligned_in_smp; | |
122 | u32 tail ____cacheline_aligned_in_smp; | |
123 | }; | |
124 | ||
1e84b97b | 125 | /* |
75b28aff HV |
126 | * This data is shared with the application through the mmap at offsets |
127 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
128 | * |
129 | * The offsets to the member fields are published through struct | |
130 | * io_sqring_offsets when calling io_uring_setup. | |
131 | */ | |
75b28aff | 132 | struct io_rings { |
1e84b97b SB |
133 | /* |
134 | * Head and tail offsets into the ring; the offsets need to be | |
135 | * masked to get valid indices. | |
136 | * | |
75b28aff HV |
137 | * The kernel controls head of the sq ring and the tail of the cq ring, |
138 | * and the application controls tail of the sq ring and the head of the | |
139 | * cq ring. | |
1e84b97b | 140 | */ |
75b28aff | 141 | struct io_uring sq, cq; |
1e84b97b | 142 | /* |
75b28aff | 143 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
144 | * ring_entries - 1) |
145 | */ | |
75b28aff HV |
146 | u32 sq_ring_mask, cq_ring_mask; |
147 | /* Ring sizes (constant, power of 2) */ | |
148 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
149 | /* |
150 | * Number of invalid entries dropped by the kernel due to | |
151 | * invalid index stored in array | |
152 | * | |
153 | * Written by the kernel, shouldn't be modified by the | |
154 | * application (i.e. get number of "new events" by comparing to | |
155 | * cached value). | |
156 | * | |
157 | * After a new SQ head value was read by the application this | |
158 | * counter includes all submissions that were dropped reaching | |
159 | * the new SQ head (and possibly more). | |
160 | */ | |
75b28aff | 161 | u32 sq_dropped; |
1e84b97b | 162 | /* |
0d9b5b3a | 163 | * Runtime SQ flags |
1e84b97b SB |
164 | * |
165 | * Written by the kernel, shouldn't be modified by the | |
166 | * application. | |
167 | * | |
168 | * The application needs a full memory barrier before checking | |
169 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
170 | */ | |
75b28aff | 171 | u32 sq_flags; |
0d9b5b3a SG |
172 | /* |
173 | * Runtime CQ flags | |
174 | * | |
175 | * Written by the application, shouldn't be modified by the | |
176 | * kernel. | |
177 | */ | |
fe7e3257 | 178 | u32 cq_flags; |
1e84b97b SB |
179 | /* |
180 | * Number of completion events lost because the queue was full; | |
181 | * this should be avoided by the application by making sure | |
0b4295b5 | 182 | * there are not more requests pending than there is space in |
1e84b97b SB |
183 | * the completion queue. |
184 | * | |
185 | * Written by the kernel, shouldn't be modified by the | |
186 | * application (i.e. get number of "new events" by comparing to | |
187 | * cached value). | |
188 | * | |
189 | * As completion events come in out of order this counter is not | |
190 | * ordered with any other data. | |
191 | */ | |
75b28aff | 192 | u32 cq_overflow; |
1e84b97b SB |
193 | /* |
194 | * Ring buffer of completion events. | |
195 | * | |
196 | * The kernel writes completion events fresh every time they are | |
197 | * produced, so the application is allowed to modify pending | |
198 | * entries. | |
199 | */ | |
75b28aff | 200 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
201 | }; |
202 | ||
45d189c6 | 203 | enum io_uring_cmd_flags { |
51aac424 | 204 | IO_URING_F_COMPLETE_DEFER = 1, |
3b44b371 | 205 | IO_URING_F_UNLOCKED = 2, |
51aac424 PB |
206 | /* int's last bit, sign checks are usually faster than a bit test */ |
207 | IO_URING_F_NONBLOCK = INT_MIN, | |
45d189c6 PB |
208 | }; |
209 | ||
edafccee JA |
210 | struct io_mapped_ubuf { |
211 | u64 ubuf; | |
4751f53d | 212 | u64 ubuf_end; |
edafccee | 213 | unsigned int nr_bvecs; |
de293938 | 214 | unsigned long acct_pages; |
41edf1a5 | 215 | struct bio_vec bvec[]; |
edafccee JA |
216 | }; |
217 | ||
50238531 BM |
218 | struct io_ring_ctx; |
219 | ||
6c2450ae PB |
220 | struct io_overflow_cqe { |
221 | struct io_uring_cqe cqe; | |
222 | struct list_head list; | |
223 | }; | |
224 | ||
a04b0ac0 PB |
225 | struct io_fixed_file { |
226 | /* file * with additional FFS_* flags */ | |
227 | unsigned long file_ptr; | |
228 | }; | |
229 | ||
269bbe5f BM |
230 | struct io_rsrc_put { |
231 | struct list_head list; | |
b60c8dce | 232 | u64 tag; |
50238531 BM |
233 | union { |
234 | void *rsrc; | |
235 | struct file *file; | |
bd54b6fe | 236 | struct io_mapped_ubuf *buf; |
50238531 | 237 | }; |
269bbe5f BM |
238 | }; |
239 | ||
aeca241b | 240 | struct io_file_table { |
042b0d85 | 241 | struct io_fixed_file *files; |
31b51510 JA |
242 | }; |
243 | ||
b895c9a6 | 244 | struct io_rsrc_node { |
05589553 XW |
245 | struct percpu_ref refs; |
246 | struct list_head node; | |
269bbe5f | 247 | struct list_head rsrc_list; |
b895c9a6 | 248 | struct io_rsrc_data *rsrc_data; |
4a38aed2 | 249 | struct llist_node llist; |
e297822b | 250 | bool done; |
05589553 XW |
251 | }; |
252 | ||
40ae0ff7 PB |
253 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); |
254 | ||
b895c9a6 | 255 | struct io_rsrc_data { |
05f3fb3c JA |
256 | struct io_ring_ctx *ctx; |
257 | ||
2d091d62 PB |
258 | u64 **tags; |
259 | unsigned int nr; | |
40ae0ff7 | 260 | rsrc_put_fn *do_put; |
3e942498 | 261 | atomic_t refs; |
05f3fb3c | 262 | struct completion done; |
8bad28d8 | 263 | bool quiesce; |
05f3fb3c JA |
264 | }; |
265 | ||
dbc7d452 JA |
266 | struct io_buffer_list { |
267 | struct list_head list; | |
268 | struct list_head buf_list; | |
269 | __u16 bgid; | |
270 | }; | |
271 | ||
5a2e745d JA |
272 | struct io_buffer { |
273 | struct list_head list; | |
274 | __u64 addr; | |
d1f82808 | 275 | __u32 len; |
5a2e745d | 276 | __u16 bid; |
b1c62645 | 277 | __u16 bgid; |
5a2e745d JA |
278 | }; |
279 | ||
21b55dbc SG |
280 | struct io_restriction { |
281 | DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); | |
282 | DECLARE_BITMAP(sqe_op, IORING_OP_LAST); | |
283 | u8 sqe_flags_allowed; | |
284 | u8 sqe_flags_required; | |
7e84e1c7 | 285 | bool registered; |
21b55dbc SG |
286 | }; |
287 | ||
37d1e2e3 JA |
288 | enum { |
289 | IO_SQ_THREAD_SHOULD_STOP = 0, | |
290 | IO_SQ_THREAD_SHOULD_PARK, | |
291 | }; | |
292 | ||
534ca6d6 JA |
293 | struct io_sq_data { |
294 | refcount_t refs; | |
9e138a48 | 295 | atomic_t park_pending; |
09a6f4ef | 296 | struct mutex lock; |
69fb2131 JA |
297 | |
298 | /* ctx's that are using this sqd */ | |
299 | struct list_head ctx_list; | |
69fb2131 | 300 | |
534ca6d6 JA |
301 | struct task_struct *thread; |
302 | struct wait_queue_head wait; | |
08369246 XW |
303 | |
304 | unsigned sq_thread_idle; | |
37d1e2e3 JA |
305 | int sq_cpu; |
306 | pid_t task_pid; | |
5c2469e0 | 307 | pid_t task_tgid; |
37d1e2e3 JA |
308 | |
309 | unsigned long state; | |
37d1e2e3 | 310 | struct completion exited; |
534ca6d6 JA |
311 | }; |
312 | ||
6dd0be1e | 313 | #define IO_COMPL_BATCH 32 |
6ff119a6 | 314 | #define IO_REQ_CACHE_SIZE 32 |
bf019da7 | 315 | #define IO_REQ_ALLOC_BATCH 8 |
258b29a9 | 316 | |
a1ab7b35 PB |
317 | struct io_submit_link { |
318 | struct io_kiocb *head; | |
319 | struct io_kiocb *last; | |
320 | }; | |
321 | ||
258b29a9 | 322 | struct io_submit_state { |
5a158c6b PB |
323 | /* inline/task_work completion list, under ->uring_lock */ |
324 | struct io_wq_work_node free_list; | |
325 | /* batch completion logic */ | |
326 | struct io_wq_work_list compl_reqs; | |
a1ab7b35 | 327 | struct io_submit_link link; |
258b29a9 | 328 | |
258b29a9 | 329 | bool plug_started; |
4b628aeb | 330 | bool need_plug; |
3d4aeb9f | 331 | bool flush_cqes; |
5ca7a8b3 | 332 | unsigned short submit_nr; |
5a158c6b | 333 | struct blk_plug plug; |
258b29a9 PB |
334 | }; |
335 | ||
77bc59b4 UA |
336 | struct io_ev_fd { |
337 | struct eventfd_ctx *cq_ev_fd; | |
c75312dd | 338 | unsigned int eventfd_async: 1; |
77bc59b4 UA |
339 | struct rcu_head rcu; |
340 | }; | |
341 | ||
dbc7d452 JA |
342 | #define IO_BUFFERS_HASH_BITS 5 |
343 | ||
2b188cc1 | 344 | struct io_ring_ctx { |
b52ecf8c | 345 | /* const or read-mostly hot data */ |
2b188cc1 JA |
346 | struct { |
347 | struct percpu_ref refs; | |
2b188cc1 | 348 | |
b52ecf8c | 349 | struct io_rings *rings; |
2b188cc1 | 350 | unsigned int flags; |
e1d85334 | 351 | unsigned int compat: 1; |
e1d85334 | 352 | unsigned int drain_next: 1; |
21b55dbc | 353 | unsigned int restricted: 1; |
f18ee4cf | 354 | unsigned int off_timeout_used: 1; |
10c66904 | 355 | unsigned int drain_active: 1; |
5562a8d7 | 356 | unsigned int drain_disabled: 1; |
9aa8dfde | 357 | unsigned int has_evfd: 1; |
b52ecf8c | 358 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 359 | |
7f1129d2 | 360 | /* submission data */ |
b52ecf8c | 361 | struct { |
0499e582 PB |
362 | struct mutex uring_lock; |
363 | ||
75b28aff HV |
364 | /* |
365 | * Ring buffer of indices into array of io_uring_sqe, which is | |
366 | * mmapped by the application using the IORING_OFF_SQES offset. | |
367 | * | |
368 | * This indirection could e.g. be used to assign fixed | |
369 | * io_uring_sqe entries to operations and only submit them to | |
370 | * the queue when needed. | |
371 | * | |
372 | * The kernel modifies neither the indices array nor the entries | |
373 | * array. | |
374 | */ | |
375 | u32 *sq_array; | |
c7af47cf | 376 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
377 | unsigned cached_sq_head; |
378 | unsigned sq_entries; | |
de0617e4 | 379 | struct list_head defer_list; |
7f1129d2 PB |
380 | |
381 | /* | |
382 | * Fixed resources fast path, should be accessed only under | |
383 | * uring_lock, and updated through io_uring_register(2) | |
384 | */ | |
385 | struct io_rsrc_node *rsrc_node; | |
ab409402 | 386 | int rsrc_cached_refs; |
7f1129d2 PB |
387 | struct io_file_table file_table; |
388 | unsigned nr_user_files; | |
389 | unsigned nr_user_bufs; | |
390 | struct io_mapped_ubuf **user_bufs; | |
391 | ||
392 | struct io_submit_state submit_state; | |
5262f567 | 393 | struct list_head timeout_list; |
ef9dd637 | 394 | struct list_head ltimeout_list; |
1d7bb1d5 | 395 | struct list_head cq_overflow_list; |
dbc7d452 | 396 | struct list_head *io_buffers; |
cc3cec83 | 397 | struct list_head io_buffers_cache; |
4d9237e3 | 398 | struct list_head apoll_cache; |
7f1129d2 PB |
399 | struct xarray personalities; |
400 | u32 pers_next; | |
401 | unsigned sq_thread_idle; | |
2b188cc1 JA |
402 | } ____cacheline_aligned_in_smp; |
403 | ||
d0acdee2 | 404 | /* IRQ completion list, under ->completion_lock */ |
c2b6c6bc | 405 | struct io_wq_work_list locked_free_list; |
d0acdee2 | 406 | unsigned int locked_free_nr; |
3c1a2ead | 407 | |
7c30f36a | 408 | const struct cred *sq_creds; /* cred used for __io_sq_thread() */ |
534ca6d6 JA |
409 | struct io_sq_data *sq_data; /* if using sq thread polling */ |
410 | ||
90554200 | 411 | struct wait_queue_head sqo_sq_wait; |
69fb2131 | 412 | struct list_head sqd_list; |
75b28aff | 413 | |
5ed7a37d | 414 | unsigned long check_cq_overflow; |
adc8682e OL |
415 | #ifdef CONFIG_NET_RX_BUSY_POLL |
416 | /* used to track busy poll napi_id */ | |
417 | struct list_head napi_list; | |
418 | spinlock_t napi_lock; /* napi_list lock */ | |
419 | #endif | |
5ed7a37d | 420 | |
206aefde JA |
421 | struct { |
422 | unsigned cached_cq_tail; | |
423 | unsigned cq_entries; | |
77bc59b4 | 424 | struct io_ev_fd __rcu *io_ev_fd; |
0499e582 PB |
425 | struct wait_queue_head cq_wait; |
426 | unsigned cq_extra; | |
427 | atomic_t cq_timeouts; | |
0499e582 | 428 | unsigned cq_last_tm_flush; |
206aefde | 429 | } ____cacheline_aligned_in_smp; |
2b188cc1 | 430 | |
2b188cc1 JA |
431 | struct { |
432 | spinlock_t completion_lock; | |
e94f141b | 433 | |
89850fce JA |
434 | spinlock_t timeout_lock; |
435 | ||
def596e9 | 436 | /* |
540e32a0 | 437 | * ->iopoll_list is protected by the ctx->uring_lock for |
def596e9 JA |
438 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
439 | * For SQPOLL, only the single threaded io_sq_thread() will | |
440 | * manipulate the list, hence no extra locking is needed there. | |
441 | */ | |
5eef4e87 | 442 | struct io_wq_work_list iopoll_list; |
78076bb6 JA |
443 | struct hlist_head *cancel_hash; |
444 | unsigned cancel_hash_bits; | |
915b3dde | 445 | bool poll_multi_queue; |
cc3cec83 JA |
446 | |
447 | struct list_head io_buffers_comp; | |
2b188cc1 | 448 | } ____cacheline_aligned_in_smp; |
85faa7b8 | 449 | |
21b55dbc | 450 | struct io_restriction restrictions; |
3c1a2ead | 451 | |
b13a8918 PB |
452 | /* slow path rsrc auxilary data, used by update/register */ |
453 | struct { | |
454 | struct io_rsrc_node *rsrc_backup_node; | |
455 | struct io_mapped_ubuf *dummy_ubuf; | |
456 | struct io_rsrc_data *file_data; | |
457 | struct io_rsrc_data *buf_data; | |
458 | ||
459 | struct delayed_work rsrc_put_work; | |
460 | struct llist_head rsrc_put_llist; | |
461 | struct list_head rsrc_ref_list; | |
462 | spinlock_t rsrc_ref_lock; | |
cc3cec83 JA |
463 | |
464 | struct list_head io_buffers_pages; | |
b13a8918 PB |
465 | }; |
466 | ||
3c1a2ead | 467 | /* Keep this last, we don't need it for the fast path */ |
b986af7e PB |
468 | struct { |
469 | #if defined(CONFIG_UNIX) | |
470 | struct socket *ring_sock; | |
471 | #endif | |
472 | /* hashed buffered write serialization */ | |
473 | struct io_wq_hash *hash_map; | |
474 | ||
475 | /* Only used for accounting purposes */ | |
476 | struct user_struct *user; | |
477 | struct mm_struct *mm_account; | |
478 | ||
479 | /* ctx exit and cancelation */ | |
9011bf9a PB |
480 | struct llist_head fallback_llist; |
481 | struct delayed_work fallback_work; | |
b986af7e PB |
482 | struct work_struct exit_work; |
483 | struct list_head tctx_list; | |
484 | struct completion ref_comp; | |
e139a1ec PB |
485 | u32 iowq_limits[2]; |
486 | bool iowq_limits_set; | |
b986af7e | 487 | }; |
2b188cc1 JA |
488 | }; |
489 | ||
e7a6c00d JA |
490 | /* |
491 | * Arbitrary limit, can be raised if need be | |
492 | */ | |
493 | #define IO_RINGFD_REG_MAX 16 | |
494 | ||
53e043b2 SM |
495 | struct io_uring_task { |
496 | /* submission side */ | |
09899b19 | 497 | int cached_refs; |
53e043b2 SM |
498 | struct xarray xa; |
499 | struct wait_queue_head wait; | |
ee53fb2b SM |
500 | const struct io_ring_ctx *last; |
501 | struct io_wq *io_wq; | |
53e043b2 | 502 | struct percpu_counter inflight; |
b303fe2e | 503 | atomic_t inflight_tracked; |
53e043b2 | 504 | atomic_t in_idle; |
53e043b2 SM |
505 | |
506 | spinlock_t task_lock; | |
507 | struct io_wq_work_list task_list; | |
4813c377 | 508 | struct io_wq_work_list prior_task_list; |
53e043b2 | 509 | struct callback_head task_work; |
e7a6c00d | 510 | struct file **registered_rings; |
6294f368 | 511 | bool task_running; |
53e043b2 SM |
512 | }; |
513 | ||
09bb8394 JA |
514 | /* |
515 | * First field must be the file pointer in all the | |
516 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
517 | */ | |
221c5eb2 JA |
518 | struct io_poll_iocb { |
519 | struct file *file; | |
018043be | 520 | struct wait_queue_head *head; |
221c5eb2 | 521 | __poll_t events; |
392edb45 | 522 | struct wait_queue_entry wait; |
221c5eb2 JA |
523 | }; |
524 | ||
9d805892 | 525 | struct io_poll_update { |
018043be | 526 | struct file *file; |
9d805892 PB |
527 | u64 old_user_data; |
528 | u64 new_user_data; | |
529 | __poll_t events; | |
b69de288 JA |
530 | bool update_events; |
531 | bool update_user_data; | |
018043be PB |
532 | }; |
533 | ||
b5dba59e JA |
534 | struct io_close { |
535 | struct file *file; | |
b5dba59e | 536 | int fd; |
7df778be | 537 | u32 file_slot; |
b5dba59e JA |
538 | }; |
539 | ||
ad8a48ac JA |
540 | struct io_timeout_data { |
541 | struct io_kiocb *req; | |
542 | struct hrtimer timer; | |
543 | struct timespec64 ts; | |
544 | enum hrtimer_mode mode; | |
50c1df2b | 545 | u32 flags; |
ad8a48ac JA |
546 | }; |
547 | ||
8ed8d3c3 JA |
548 | struct io_accept { |
549 | struct file *file; | |
550 | struct sockaddr __user *addr; | |
551 | int __user *addr_len; | |
552 | int flags; | |
aaa4db12 | 553 | u32 file_slot; |
09952e3e | 554 | unsigned long nofile; |
8ed8d3c3 JA |
555 | }; |
556 | ||
557 | struct io_sync { | |
558 | struct file *file; | |
559 | loff_t len; | |
560 | loff_t off; | |
561 | int flags; | |
d63d1b5e | 562 | int mode; |
8ed8d3c3 JA |
563 | }; |
564 | ||
fbf23849 JA |
565 | struct io_cancel { |
566 | struct file *file; | |
567 | u64 addr; | |
568 | }; | |
569 | ||
b29472ee JA |
570 | struct io_timeout { |
571 | struct file *file; | |
bfe68a22 PB |
572 | u32 off; |
573 | u32 target_seq; | |
135fcde8 | 574 | struct list_head list; |
90cd7e42 PB |
575 | /* head of the link, used by linked timeouts only */ |
576 | struct io_kiocb *head; | |
89b263f6 JA |
577 | /* for linked completions */ |
578 | struct io_kiocb *prev; | |
b29472ee JA |
579 | }; |
580 | ||
0bdf7a2d PB |
581 | struct io_timeout_rem { |
582 | struct file *file; | |
583 | u64 addr; | |
9c8e11b3 PB |
584 | |
585 | /* timeout update */ | |
586 | struct timespec64 ts; | |
587 | u32 flags; | |
f1042b6c | 588 | bool ltimeout; |
0bdf7a2d PB |
589 | }; |
590 | ||
9adbd45d JA |
591 | struct io_rw { |
592 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
593 | struct kiocb kiocb; | |
594 | u64 addr; | |
595 | u64 len; | |
596 | }; | |
597 | ||
3fbb51c1 JA |
598 | struct io_connect { |
599 | struct file *file; | |
600 | struct sockaddr __user *addr; | |
601 | int addr_len; | |
602 | }; | |
603 | ||
e47293fd JA |
604 | struct io_sr_msg { |
605 | struct file *file; | |
fddaface | 606 | union { |
4af3417a PB |
607 | struct compat_msghdr __user *umsg_compat; |
608 | struct user_msghdr __user *umsg; | |
609 | void __user *buf; | |
fddaface | 610 | }; |
e47293fd | 611 | int msg_flags; |
bcda7baa | 612 | int bgid; |
fddaface | 613 | size_t len; |
7ba89d2a | 614 | size_t done_io; |
e47293fd JA |
615 | }; |
616 | ||
15b71abe JA |
617 | struct io_open { |
618 | struct file *file; | |
619 | int dfd; | |
b9445598 | 620 | u32 file_slot; |
15b71abe | 621 | struct filename *filename; |
c12cedf2 | 622 | struct open_how how; |
4022e7af | 623 | unsigned long nofile; |
15b71abe JA |
624 | }; |
625 | ||
269bbe5f | 626 | struct io_rsrc_update { |
05f3fb3c JA |
627 | struct file *file; |
628 | u64 arg; | |
629 | u32 nr_args; | |
630 | u32 offset; | |
631 | }; | |
632 | ||
4840e418 JA |
633 | struct io_fadvise { |
634 | struct file *file; | |
635 | u64 offset; | |
636 | u32 len; | |
637 | u32 advice; | |
638 | }; | |
639 | ||
c1ca757b JA |
640 | struct io_madvise { |
641 | struct file *file; | |
642 | u64 addr; | |
643 | u32 len; | |
644 | u32 advice; | |
645 | }; | |
646 | ||
3e4827b0 JA |
647 | struct io_epoll { |
648 | struct file *file; | |
649 | int epfd; | |
650 | int op; | |
651 | int fd; | |
652 | struct epoll_event event; | |
e47293fd JA |
653 | }; |
654 | ||
7d67af2c PB |
655 | struct io_splice { |
656 | struct file *file_out; | |
657 | struct file *file_in; | |
658 | loff_t off_out; | |
659 | loff_t off_in; | |
660 | u64 len; | |
661 | unsigned int flags; | |
662 | }; | |
663 | ||
ddf0322d JA |
664 | struct io_provide_buf { |
665 | struct file *file; | |
666 | __u64 addr; | |
38134ada | 667 | __u32 len; |
ddf0322d JA |
668 | __u32 bgid; |
669 | __u16 nbufs; | |
670 | __u16 bid; | |
671 | }; | |
672 | ||
1d9e1288 BM |
673 | struct io_statx { |
674 | struct file *file; | |
675 | int dfd; | |
676 | unsigned int mask; | |
677 | unsigned int flags; | |
1b6fe6e0 | 678 | struct filename *filename; |
1d9e1288 BM |
679 | struct statx __user *buffer; |
680 | }; | |
681 | ||
36f4fa68 JA |
682 | struct io_shutdown { |
683 | struct file *file; | |
684 | int how; | |
685 | }; | |
686 | ||
80a261fd JA |
687 | struct io_rename { |
688 | struct file *file; | |
689 | int old_dfd; | |
690 | int new_dfd; | |
691 | struct filename *oldpath; | |
692 | struct filename *newpath; | |
693 | int flags; | |
694 | }; | |
695 | ||
14a1143b JA |
696 | struct io_unlink { |
697 | struct file *file; | |
698 | int dfd; | |
699 | int flags; | |
700 | struct filename *filename; | |
701 | }; | |
702 | ||
e34a02dc DK |
703 | struct io_mkdir { |
704 | struct file *file; | |
705 | int dfd; | |
706 | umode_t mode; | |
707 | struct filename *filename; | |
708 | }; | |
709 | ||
7a8721f8 DK |
710 | struct io_symlink { |
711 | struct file *file; | |
712 | int new_dfd; | |
713 | struct filename *oldpath; | |
714 | struct filename *newpath; | |
715 | }; | |
716 | ||
cf30da90 DK |
717 | struct io_hardlink { |
718 | struct file *file; | |
719 | int old_dfd; | |
720 | int new_dfd; | |
721 | struct filename *oldpath; | |
722 | struct filename *newpath; | |
723 | int flags; | |
724 | }; | |
725 | ||
4f57f06c JA |
726 | struct io_msg { |
727 | struct file *file; | |
728 | u64 user_data; | |
729 | u32 len; | |
730 | }; | |
731 | ||
f499a021 JA |
732 | struct io_async_connect { |
733 | struct sockaddr_storage address; | |
734 | }; | |
735 | ||
03b1230c JA |
736 | struct io_async_msghdr { |
737 | struct iovec fast_iov[UIO_FASTIOV]; | |
257e84a5 PB |
738 | /* points to an allocated iov, if NULL we use fast_iov instead */ |
739 | struct iovec *free_iov; | |
03b1230c JA |
740 | struct sockaddr __user *uaddr; |
741 | struct msghdr msg; | |
b537916c | 742 | struct sockaddr_storage addr; |
03b1230c JA |
743 | }; |
744 | ||
538941e2 | 745 | struct io_rw_state { |
ff6165b2 | 746 | struct iov_iter iter; |
cd658695 | 747 | struct iov_iter_state iter_state; |
c88598a9 | 748 | struct iovec fast_iov[UIO_FASTIOV]; |
538941e2 PB |
749 | }; |
750 | ||
751 | struct io_async_rw { | |
752 | struct io_rw_state s; | |
753 | const struct iovec *free_iovec; | |
227c0c96 | 754 | size_t bytes_done; |
bcf5a063 | 755 | struct wait_page_queue wpq; |
f67676d1 JA |
756 | }; |
757 | ||
6b47ee6e PB |
758 | enum { |
759 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
760 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
761 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
762 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
763 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
bcda7baa | 764 | REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
04c76b41 | 765 | REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT, |
6b47ee6e | 766 | |
dddca226 | 767 | /* first byte is taken by user flags, shift it to not overlap */ |
93d2bcd2 | 768 | REQ_F_FAIL_BIT = 8, |
6b47ee6e PB |
769 | REQ_F_INFLIGHT_BIT, |
770 | REQ_F_CUR_POS_BIT, | |
771 | REQ_F_NOWAIT_BIT, | |
6b47ee6e | 772 | REQ_F_LINK_TIMEOUT_BIT, |
99bc4c38 | 773 | REQ_F_NEED_CLEANUP_BIT, |
d7718a9d | 774 | REQ_F_POLLED_BIT, |
bcda7baa | 775 | REQ_F_BUFFER_SELECTED_BIT, |
e342c807 | 776 | REQ_F_COMPLETE_INLINE_BIT, |
230d50d4 | 777 | REQ_F_REISSUE_BIT, |
b8e64b53 | 778 | REQ_F_CREDS_BIT, |
20e60a38 | 779 | REQ_F_REFCOUNT_BIT, |
4d13d1a4 | 780 | REQ_F_ARM_LTIMEOUT_BIT, |
d886e185 | 781 | REQ_F_ASYNC_DATA_BIT, |
04c76b41 | 782 | REQ_F_SKIP_LINK_CQES_BIT, |
91eac1c6 JA |
783 | REQ_F_SINGLE_POLL_BIT, |
784 | REQ_F_DOUBLE_POLL_BIT, | |
8a3e8ee5 | 785 | REQ_F_PARTIAL_IO_BIT, |
7b29f92d | 786 | /* keep async read/write and isreg together and in order */ |
35645ac3 | 787 | REQ_F_SUPPORT_NOWAIT_BIT, |
7b29f92d | 788 | REQ_F_ISREG_BIT, |
84557871 JA |
789 | |
790 | /* not a real bit, just to check we're not overflowing the space */ | |
791 | __REQ_F_LAST_BIT, | |
6b47ee6e PB |
792 | }; |
793 | ||
794 | enum { | |
795 | /* ctx owns file */ | |
796 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
797 | /* drain existing IO first */ | |
798 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
799 | /* linked sqes */ | |
800 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
801 | /* doesn't sever on completion < 0 */ | |
802 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
803 | /* IOSQE_ASYNC */ | |
804 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
bcda7baa JA |
805 | /* IOSQE_BUFFER_SELECT */ |
806 | REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), | |
04c76b41 PB |
807 | /* IOSQE_CQE_SKIP_SUCCESS */ |
808 | REQ_F_CQE_SKIP = BIT(REQ_F_CQE_SKIP_BIT), | |
6b47ee6e | 809 | |
6b47ee6e | 810 | /* fail rest of links */ |
93d2bcd2 | 811 | REQ_F_FAIL = BIT(REQ_F_FAIL_BIT), |
b05a1bcd | 812 | /* on inflight list, should be cancelled and waited on exit reliably */ |
6b47ee6e PB |
813 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
814 | /* read/write uses file position */ | |
815 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
816 | /* must not punt to workers */ | |
817 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
900fad45 | 818 | /* has or had linked timeout */ |
6b47ee6e | 819 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
99bc4c38 PB |
820 | /* needs cleanup */ |
821 | REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), | |
d7718a9d JA |
822 | /* already went through poll handler */ |
823 | REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), | |
bcda7baa JA |
824 | /* buffer already selected */ |
825 | REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), | |
e342c807 PB |
826 | /* completion is deferred through io_comp_state */ |
827 | REQ_F_COMPLETE_INLINE = BIT(REQ_F_COMPLETE_INLINE_BIT), | |
230d50d4 JA |
828 | /* caller should reissue async */ |
829 | REQ_F_REISSUE = BIT(REQ_F_REISSUE_BIT), | |
35645ac3 PB |
830 | /* supports async reads/writes */ |
831 | REQ_F_SUPPORT_NOWAIT = BIT(REQ_F_SUPPORT_NOWAIT_BIT), | |
7b29f92d JA |
832 | /* regular file */ |
833 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
b8e64b53 PB |
834 | /* has creds assigned */ |
835 | REQ_F_CREDS = BIT(REQ_F_CREDS_BIT), | |
20e60a38 PB |
836 | /* skip refcounting if not set */ |
837 | REQ_F_REFCOUNT = BIT(REQ_F_REFCOUNT_BIT), | |
4d13d1a4 PB |
838 | /* there is a linked timeout that has to be armed */ |
839 | REQ_F_ARM_LTIMEOUT = BIT(REQ_F_ARM_LTIMEOUT_BIT), | |
d886e185 PB |
840 | /* ->async_data allocated */ |
841 | REQ_F_ASYNC_DATA = BIT(REQ_F_ASYNC_DATA_BIT), | |
04c76b41 PB |
842 | /* don't post CQEs while failing linked requests */ |
843 | REQ_F_SKIP_LINK_CQES = BIT(REQ_F_SKIP_LINK_CQES_BIT), | |
91eac1c6 JA |
844 | /* single poll may be active */ |
845 | REQ_F_SINGLE_POLL = BIT(REQ_F_SINGLE_POLL_BIT), | |
846 | /* double poll may active */ | |
847 | REQ_F_DOUBLE_POLL = BIT(REQ_F_DOUBLE_POLL_BIT), | |
8a3e8ee5 JA |
848 | /* request has already done partial IO */ |
849 | REQ_F_PARTIAL_IO = BIT(REQ_F_PARTIAL_IO_BIT), | |
d7718a9d JA |
850 | }; |
851 | ||
852 | struct async_poll { | |
853 | struct io_poll_iocb poll; | |
807abcb0 | 854 | struct io_poll_iocb *double_poll; |
6b47ee6e PB |
855 | }; |
856 | ||
f237c30a | 857 | typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked); |
5b0a6acc | 858 | |
7cbf1722 | 859 | struct io_task_work { |
5b0a6acc PB |
860 | union { |
861 | struct io_wq_work_node node; | |
862 | struct llist_node fallback_node; | |
863 | }; | |
864 | io_req_tw_func_t func; | |
7cbf1722 JA |
865 | }; |
866 | ||
992da01a PB |
867 | enum { |
868 | IORING_RSRC_FILE = 0, | |
869 | IORING_RSRC_BUFFER = 1, | |
870 | }; | |
871 | ||
09bb8394 JA |
872 | /* |
873 | * NOTE! Each of the iocb union members has the file pointer | |
874 | * as the first entry in their struct definition. So you can | |
875 | * access the file pointer through any of the sub-structs, | |
63c36549 | 876 | * or directly as just 'file' in this struct. |
09bb8394 | 877 | */ |
2b188cc1 | 878 | struct io_kiocb { |
221c5eb2 | 879 | union { |
09bb8394 | 880 | struct file *file; |
9adbd45d | 881 | struct io_rw rw; |
221c5eb2 | 882 | struct io_poll_iocb poll; |
9d805892 | 883 | struct io_poll_update poll_update; |
8ed8d3c3 JA |
884 | struct io_accept accept; |
885 | struct io_sync sync; | |
fbf23849 | 886 | struct io_cancel cancel; |
b29472ee | 887 | struct io_timeout timeout; |
0bdf7a2d | 888 | struct io_timeout_rem timeout_rem; |
3fbb51c1 | 889 | struct io_connect connect; |
e47293fd | 890 | struct io_sr_msg sr_msg; |
15b71abe | 891 | struct io_open open; |
b5dba59e | 892 | struct io_close close; |
269bbe5f | 893 | struct io_rsrc_update rsrc_update; |
4840e418 | 894 | struct io_fadvise fadvise; |
c1ca757b | 895 | struct io_madvise madvise; |
3e4827b0 | 896 | struct io_epoll epoll; |
7d67af2c | 897 | struct io_splice splice; |
ddf0322d | 898 | struct io_provide_buf pbuf; |
1d9e1288 | 899 | struct io_statx statx; |
36f4fa68 | 900 | struct io_shutdown shutdown; |
80a261fd | 901 | struct io_rename rename; |
14a1143b | 902 | struct io_unlink unlink; |
e34a02dc | 903 | struct io_mkdir mkdir; |
7a8721f8 | 904 | struct io_symlink symlink; |
cf30da90 | 905 | struct io_hardlink hardlink; |
4f57f06c | 906 | struct io_msg msg; |
221c5eb2 | 907 | }; |
2b188cc1 | 908 | |
d625c6ee | 909 | u8 opcode; |
65a6543d XW |
910 | /* polled IO has completed */ |
911 | u8 iopoll_completed; | |
4f4eeba8 | 912 | u16 buf_index; |
d17e56eb PB |
913 | unsigned int flags; |
914 | ||
915 | u64 user_data; | |
9cf7c104 | 916 | u32 result; |
d17e56eb | 917 | u32 cflags; |
4f4eeba8 | 918 | |
010e8e6b | 919 | struct io_ring_ctx *ctx; |
010e8e6b | 920 | struct task_struct *task; |
d7718a9d | 921 | |
269bbe5f | 922 | struct percpu_ref *fixed_rsrc_refs; |
d886e185 PB |
923 | /* store used ubuf, so we can prevent reloading */ |
924 | struct io_mapped_ubuf *imu; | |
fcb323cc | 925 | |
7e3709d5 | 926 | /* used by request caches, completion batching and iopoll */ |
ef05d9eb | 927 | struct io_wq_work_node comp_list; |
d17e56eb | 928 | atomic_t refs; |
521d61fc | 929 | atomic_t poll_refs; |
5b0a6acc | 930 | struct io_task_work io_task_work; |
010e8e6b PB |
931 | /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
932 | struct hlist_node hash_node; | |
7e3709d5 | 933 | /* internal polling, see IORING_FEAT_FAST_POLL */ |
010e8e6b | 934 | struct async_poll *apoll; |
d886e185 PB |
935 | /* opcode allocated if it needs to store data for async defer */ |
936 | void *async_data; | |
7e3709d5 | 937 | /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */ |
30d51dd4 | 938 | struct io_buffer *kbuf; |
41cdcc22 | 939 | /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ |
34d2bfe7 | 940 | struct io_kiocb *link; |
41cdcc22 | 941 | /* custom credentials, valid IFF REQ_F_CREDS is set */ |
521d61fc JA |
942 | const struct cred *creds; |
943 | struct io_wq_work work; | |
2b188cc1 | 944 | }; |
05589553 | 945 | |
13bf43f5 PB |
946 | struct io_tctx_node { |
947 | struct list_head ctx_node; | |
948 | struct task_struct *task; | |
13bf43f5 PB |
949 | struct io_ring_ctx *ctx; |
950 | }; | |
951 | ||
27dc8338 PB |
952 | struct io_defer_entry { |
953 | struct list_head list; | |
954 | struct io_kiocb *req; | |
9cf7c104 | 955 | u32 seq; |
2b188cc1 JA |
956 | }; |
957 | ||
d3656344 | 958 | struct io_op_def { |
d3656344 JA |
959 | /* needs req->file assigned */ |
960 | unsigned needs_file : 1; | |
6d63416d PB |
961 | /* should block plug */ |
962 | unsigned plug : 1; | |
d3656344 JA |
963 | /* hash wq insertion if file is a regular file */ |
964 | unsigned hash_reg_file : 1; | |
965 | /* unbound wq insertion if file is a non-regular file */ | |
966 | unsigned unbound_nonreg_file : 1; | |
8a72758c JA |
967 | /* set if opcode supports polled "wait" */ |
968 | unsigned pollin : 1; | |
969 | unsigned pollout : 1; | |
52dd8640 | 970 | unsigned poll_exclusive : 1; |
bcda7baa JA |
971 | /* op supports buffer selection */ |
972 | unsigned buffer_select : 1; | |
26f0505a PB |
973 | /* do prep async if is going to be punted */ |
974 | unsigned needs_async_setup : 1; | |
6d63416d PB |
975 | /* opcode is not supported by this kernel */ |
976 | unsigned not_supported : 1; | |
5bd2182d PM |
977 | /* skip auditing */ |
978 | unsigned audit_skip : 1; | |
e8c2bc1f JA |
979 | /* size of async data needed, if any */ |
980 | unsigned short async_size; | |
d3656344 JA |
981 | }; |
982 | ||
0918682b | 983 | static const struct io_op_def io_op_defs[] = { |
0463b6c5 PB |
984 | [IORING_OP_NOP] = {}, |
985 | [IORING_OP_READV] = { | |
d3656344 JA |
986 | .needs_file = 1, |
987 | .unbound_nonreg_file = 1, | |
8a72758c | 988 | .pollin = 1, |
4d954c25 | 989 | .buffer_select = 1, |
26f0505a | 990 | .needs_async_setup = 1, |
27926b68 | 991 | .plug = 1, |
5bd2182d | 992 | .audit_skip = 1, |
e8c2bc1f | 993 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 994 | }, |
0463b6c5 | 995 | [IORING_OP_WRITEV] = { |
d3656344 JA |
996 | .needs_file = 1, |
997 | .hash_reg_file = 1, | |
998 | .unbound_nonreg_file = 1, | |
8a72758c | 999 | .pollout = 1, |
26f0505a | 1000 | .needs_async_setup = 1, |
27926b68 | 1001 | .plug = 1, |
5bd2182d | 1002 | .audit_skip = 1, |
e8c2bc1f | 1003 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1004 | }, |
0463b6c5 | 1005 | [IORING_OP_FSYNC] = { |
d3656344 | 1006 | .needs_file = 1, |
5bd2182d | 1007 | .audit_skip = 1, |
d3656344 | 1008 | }, |
0463b6c5 | 1009 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
1010 | .needs_file = 1, |
1011 | .unbound_nonreg_file = 1, | |
8a72758c | 1012 | .pollin = 1, |
27926b68 | 1013 | .plug = 1, |
5bd2182d | 1014 | .audit_skip = 1, |
e8c2bc1f | 1015 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1016 | }, |
0463b6c5 | 1017 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
1018 | .needs_file = 1, |
1019 | .hash_reg_file = 1, | |
1020 | .unbound_nonreg_file = 1, | |
8a72758c | 1021 | .pollout = 1, |
27926b68 | 1022 | .plug = 1, |
5bd2182d | 1023 | .audit_skip = 1, |
e8c2bc1f | 1024 | .async_size = sizeof(struct io_async_rw), |
d3656344 | 1025 | }, |
0463b6c5 | 1026 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
1027 | .needs_file = 1, |
1028 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1029 | .audit_skip = 1, |
1030 | }, | |
1031 | [IORING_OP_POLL_REMOVE] = { | |
1032 | .audit_skip = 1, | |
d3656344 | 1033 | }, |
0463b6c5 | 1034 | [IORING_OP_SYNC_FILE_RANGE] = { |
d3656344 | 1035 | .needs_file = 1, |
5bd2182d | 1036 | .audit_skip = 1, |
d3656344 | 1037 | }, |
0463b6c5 | 1038 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
1039 | .needs_file = 1, |
1040 | .unbound_nonreg_file = 1, | |
8a72758c | 1041 | .pollout = 1, |
26f0505a | 1042 | .needs_async_setup = 1, |
e8c2bc1f | 1043 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1044 | }, |
0463b6c5 | 1045 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
1046 | .needs_file = 1, |
1047 | .unbound_nonreg_file = 1, | |
8a72758c | 1048 | .pollin = 1, |
52de1fe1 | 1049 | .buffer_select = 1, |
26f0505a | 1050 | .needs_async_setup = 1, |
e8c2bc1f | 1051 | .async_size = sizeof(struct io_async_msghdr), |
d3656344 | 1052 | }, |
0463b6c5 | 1053 | [IORING_OP_TIMEOUT] = { |
5bd2182d | 1054 | .audit_skip = 1, |
e8c2bc1f | 1055 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1056 | }, |
9c8e11b3 PB |
1057 | [IORING_OP_TIMEOUT_REMOVE] = { |
1058 | /* used by timeout updates' prep() */ | |
5bd2182d | 1059 | .audit_skip = 1, |
9c8e11b3 | 1060 | }, |
0463b6c5 | 1061 | [IORING_OP_ACCEPT] = { |
d3656344 JA |
1062 | .needs_file = 1, |
1063 | .unbound_nonreg_file = 1, | |
8a72758c | 1064 | .pollin = 1, |
52dd8640 | 1065 | .poll_exclusive = 1, |
d3656344 | 1066 | }, |
5bd2182d PM |
1067 | [IORING_OP_ASYNC_CANCEL] = { |
1068 | .audit_skip = 1, | |
1069 | }, | |
0463b6c5 | 1070 | [IORING_OP_LINK_TIMEOUT] = { |
5bd2182d | 1071 | .audit_skip = 1, |
e8c2bc1f | 1072 | .async_size = sizeof(struct io_timeout_data), |
d3656344 | 1073 | }, |
0463b6c5 | 1074 | [IORING_OP_CONNECT] = { |
d3656344 JA |
1075 | .needs_file = 1, |
1076 | .unbound_nonreg_file = 1, | |
8a72758c | 1077 | .pollout = 1, |
26f0505a | 1078 | .needs_async_setup = 1, |
e8c2bc1f | 1079 | .async_size = sizeof(struct io_async_connect), |
d3656344 | 1080 | }, |
0463b6c5 | 1081 | [IORING_OP_FALLOCATE] = { |
d3656344 | 1082 | .needs_file = 1, |
d3656344 | 1083 | }, |
44526bed JA |
1084 | [IORING_OP_OPENAT] = {}, |
1085 | [IORING_OP_CLOSE] = {}, | |
5bd2182d PM |
1086 | [IORING_OP_FILES_UPDATE] = { |
1087 | .audit_skip = 1, | |
1088 | }, | |
1089 | [IORING_OP_STATX] = { | |
1090 | .audit_skip = 1, | |
1091 | }, | |
0463b6c5 | 1092 | [IORING_OP_READ] = { |
3a6820f2 JA |
1093 | .needs_file = 1, |
1094 | .unbound_nonreg_file = 1, | |
8a72758c | 1095 | .pollin = 1, |
bcda7baa | 1096 | .buffer_select = 1, |
27926b68 | 1097 | .plug = 1, |
5bd2182d | 1098 | .audit_skip = 1, |
e8c2bc1f | 1099 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1100 | }, |
0463b6c5 | 1101 | [IORING_OP_WRITE] = { |
3a6820f2 | 1102 | .needs_file = 1, |
7b3188e7 | 1103 | .hash_reg_file = 1, |
3a6820f2 | 1104 | .unbound_nonreg_file = 1, |
8a72758c | 1105 | .pollout = 1, |
27926b68 | 1106 | .plug = 1, |
5bd2182d | 1107 | .audit_skip = 1, |
e8c2bc1f | 1108 | .async_size = sizeof(struct io_async_rw), |
3a6820f2 | 1109 | }, |
0463b6c5 | 1110 | [IORING_OP_FADVISE] = { |
4840e418 | 1111 | .needs_file = 1, |
5bd2182d | 1112 | .audit_skip = 1, |
c1ca757b | 1113 | }, |
44526bed | 1114 | [IORING_OP_MADVISE] = {}, |
0463b6c5 | 1115 | [IORING_OP_SEND] = { |
fddaface JA |
1116 | .needs_file = 1, |
1117 | .unbound_nonreg_file = 1, | |
8a72758c | 1118 | .pollout = 1, |
5bd2182d | 1119 | .audit_skip = 1, |
fddaface | 1120 | }, |
0463b6c5 | 1121 | [IORING_OP_RECV] = { |
fddaface JA |
1122 | .needs_file = 1, |
1123 | .unbound_nonreg_file = 1, | |
8a72758c | 1124 | .pollin = 1, |
bcda7baa | 1125 | .buffer_select = 1, |
5bd2182d | 1126 | .audit_skip = 1, |
fddaface | 1127 | }, |
0463b6c5 | 1128 | [IORING_OP_OPENAT2] = { |
cebdb986 | 1129 | }, |
3e4827b0 JA |
1130 | [IORING_OP_EPOLL_CTL] = { |
1131 | .unbound_nonreg_file = 1, | |
5bd2182d | 1132 | .audit_skip = 1, |
3e4827b0 | 1133 | }, |
7d67af2c PB |
1134 | [IORING_OP_SPLICE] = { |
1135 | .needs_file = 1, | |
1136 | .hash_reg_file = 1, | |
1137 | .unbound_nonreg_file = 1, | |
5bd2182d PM |
1138 | .audit_skip = 1, |
1139 | }, | |
1140 | [IORING_OP_PROVIDE_BUFFERS] = { | |
1141 | .audit_skip = 1, | |
1142 | }, | |
1143 | [IORING_OP_REMOVE_BUFFERS] = { | |
1144 | .audit_skip = 1, | |
ddf0322d | 1145 | }, |
f2a8d5c7 PB |
1146 | [IORING_OP_TEE] = { |
1147 | .needs_file = 1, | |
1148 | .hash_reg_file = 1, | |
1149 | .unbound_nonreg_file = 1, | |
5bd2182d | 1150 | .audit_skip = 1, |
f2a8d5c7 | 1151 | }, |
36f4fa68 JA |
1152 | [IORING_OP_SHUTDOWN] = { |
1153 | .needs_file = 1, | |
1154 | }, | |
44526bed JA |
1155 | [IORING_OP_RENAMEAT] = {}, |
1156 | [IORING_OP_UNLINKAT] = {}, | |
e34a02dc | 1157 | [IORING_OP_MKDIRAT] = {}, |
7a8721f8 | 1158 | [IORING_OP_SYMLINKAT] = {}, |
cf30da90 | 1159 | [IORING_OP_LINKAT] = {}, |
4f57f06c JA |
1160 | [IORING_OP_MSG_RING] = { |
1161 | .needs_file = 1, | |
1162 | }, | |
d3656344 JA |
1163 | }; |
1164 | ||
0756a869 PB |
1165 | /* requests with any of those set should undergo io_disarm_next() */ |
1166 | #define IO_DISARM_MASK (REQ_F_ARM_LTIMEOUT | REQ_F_LINK_TIMEOUT | REQ_F_FAIL) | |
1167 | ||
7a612350 | 1168 | static bool io_disarm_next(struct io_kiocb *req); |
eef51daa | 1169 | static void io_uring_del_tctx_node(unsigned long index); |
9936c7c2 PB |
1170 | static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
1171 | struct task_struct *task, | |
3dd0c97a | 1172 | bool cancel_all); |
78cc687b | 1173 | static void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); |
1ffc5422 | 1174 | |
913a571a PB |
1175 | static void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags); |
1176 | ||
ec9c02ad | 1177 | static void io_put_req(struct io_kiocb *req); |
91c2f697 | 1178 | static void io_put_req_deferred(struct io_kiocb *req); |
c7dae4ba | 1179 | static void io_dismantle_req(struct io_kiocb *req); |
94ae5e77 | 1180 | static void io_queue_linked_timeout(struct io_kiocb *req); |
fdecb662 | 1181 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 1182 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 | 1183 | unsigned nr_args); |
68fb8979 | 1184 | static void io_clean_op(struct io_kiocb *req); |
ac177053 | 1185 | static struct file *io_file_get(struct io_ring_ctx *ctx, |
8371adf5 | 1186 | struct io_kiocb *req, int fd, bool fixed); |
c5eef2b9 | 1187 | static void __io_queue_sqe(struct io_kiocb *req); |
269bbe5f | 1188 | static void io_rsrc_put_work(struct work_struct *work); |
de0617e4 | 1189 | |
907d1df3 | 1190 | static void io_req_task_queue(struct io_kiocb *req); |
c450178d | 1191 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx); |
179ae0d1 | 1192 | static int io_req_prep_async(struct io_kiocb *req); |
de0617e4 | 1193 | |
b9445598 PB |
1194 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
1195 | unsigned int issue_flags, u32 slot_index); | |
7df778be PB |
1196 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags); |
1197 | ||
f1042b6c | 1198 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer); |
9aa8dfde | 1199 | static void io_eventfd_signal(struct io_ring_ctx *ctx); |
b9445598 | 1200 | |
2b188cc1 JA |
1201 | static struct kmem_cache *req_cachep; |
1202 | ||
0918682b | 1203 | static const struct file_operations io_uring_fops; |
2b188cc1 JA |
1204 | |
1205 | struct sock *io_uring_get_socket(struct file *file) | |
1206 | { | |
1207 | #if defined(CONFIG_UNIX) | |
1208 | if (file->f_op == &io_uring_fops) { | |
1209 | struct io_ring_ctx *ctx = file->private_data; | |
1210 | ||
1211 | return ctx->ring_sock->sk; | |
1212 | } | |
1213 | #endif | |
1214 | return NULL; | |
1215 | } | |
1216 | EXPORT_SYMBOL(io_uring_get_socket); | |
1217 | ||
f237c30a PB |
1218 | static inline void io_tw_lock(struct io_ring_ctx *ctx, bool *locked) |
1219 | { | |
1220 | if (!*locked) { | |
1221 | mutex_lock(&ctx->uring_lock); | |
1222 | *locked = true; | |
1223 | } | |
1224 | } | |
1225 | ||
f2f87370 PB |
1226 | #define io_for_each_link(pos, head) \ |
1227 | for (pos = (head); pos; pos = pos->link) | |
1228 | ||
21c843d5 PB |
1229 | /* |
1230 | * Shamelessly stolen from the mm implementation of page reference checking, | |
1231 | * see commit f958d7b528b1 for details. | |
1232 | */ | |
1233 | #define req_ref_zero_or_close_to_overflow(req) \ | |
1234 | ((unsigned int) atomic_read(&(req->refs)) + 127u <= 127u) | |
1235 | ||
1236 | static inline bool req_ref_inc_not_zero(struct io_kiocb *req) | |
1237 | { | |
20e60a38 | 1238 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1239 | return atomic_inc_not_zero(&req->refs); |
1240 | } | |
1241 | ||
21c843d5 PB |
1242 | static inline bool req_ref_put_and_test(struct io_kiocb *req) |
1243 | { | |
20e60a38 PB |
1244 | if (likely(!(req->flags & REQ_F_REFCOUNT))) |
1245 | return true; | |
1246 | ||
21c843d5 PB |
1247 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1248 | return atomic_dec_and_test(&req->refs); | |
1249 | } | |
1250 | ||
21c843d5 PB |
1251 | static inline void req_ref_get(struct io_kiocb *req) |
1252 | { | |
20e60a38 | 1253 | WARN_ON_ONCE(!(req->flags & REQ_F_REFCOUNT)); |
21c843d5 PB |
1254 | WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req)); |
1255 | atomic_inc(&req->refs); | |
1256 | } | |
1257 | ||
c450178d PB |
1258 | static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) |
1259 | { | |
6f33b0bc | 1260 | if (!wq_list_empty(&ctx->submit_state.compl_reqs)) |
c450178d PB |
1261 | __io_submit_flush_completions(ctx); |
1262 | } | |
1263 | ||
48dcd38d | 1264 | static inline void __io_req_set_refcount(struct io_kiocb *req, int nr) |
20e60a38 PB |
1265 | { |
1266 | if (!(req->flags & REQ_F_REFCOUNT)) { | |
1267 | req->flags |= REQ_F_REFCOUNT; | |
48dcd38d | 1268 | atomic_set(&req->refs, nr); |
20e60a38 PB |
1269 | } |
1270 | } | |
1271 | ||
48dcd38d PB |
1272 | static inline void io_req_set_refcount(struct io_kiocb *req) |
1273 | { | |
1274 | __io_req_set_refcount(req, 1); | |
1275 | } | |
1276 | ||
ab409402 PB |
1277 | #define IO_RSRC_REF_BATCH 100 |
1278 | ||
1279 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
1280 | struct io_ring_ctx *ctx) | |
1281 | __must_hold(&ctx->uring_lock) | |
36f72fe2 | 1282 | { |
ab409402 PB |
1283 | struct percpu_ref *ref = req->fixed_rsrc_refs; |
1284 | ||
1285 | if (ref) { | |
1286 | if (ref == &ctx->rsrc_node->refs) | |
1287 | ctx->rsrc_cached_refs++; | |
1288 | else | |
1289 | percpu_ref_put(ref); | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | static inline void io_req_put_rsrc(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
1294 | { | |
1295 | if (req->fixed_rsrc_refs) | |
1296 | percpu_ref_put(req->fixed_rsrc_refs); | |
1297 | } | |
1298 | ||
1299 | static __cold void io_rsrc_refs_drop(struct io_ring_ctx *ctx) | |
1300 | __must_hold(&ctx->uring_lock) | |
1301 | { | |
1302 | if (ctx->rsrc_cached_refs) { | |
1303 | percpu_ref_put_many(&ctx->rsrc_node->refs, ctx->rsrc_cached_refs); | |
1304 | ctx->rsrc_cached_refs = 0; | |
1305 | } | |
1306 | } | |
1307 | ||
1308 | static void io_rsrc_refs_refill(struct io_ring_ctx *ctx) | |
1309 | __must_hold(&ctx->uring_lock) | |
1310 | { | |
1311 | ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH; | |
1312 | percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH); | |
1313 | } | |
36f72fe2 | 1314 | |
a46be971 PB |
1315 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
1316 | struct io_ring_ctx *ctx) | |
36f72fe2 | 1317 | { |
269bbe5f | 1318 | if (!req->fixed_rsrc_refs) { |
a7f0ed5a | 1319 | req->fixed_rsrc_refs = &ctx->rsrc_node->refs; |
ab409402 PB |
1320 | ctx->rsrc_cached_refs--; |
1321 | if (unlikely(ctx->rsrc_cached_refs < 0)) | |
1322 | io_rsrc_refs_refill(ctx); | |
36f72fe2 PB |
1323 | } |
1324 | } | |
1325 | ||
cc3cec83 | 1326 | static unsigned int __io_put_kbuf(struct io_kiocb *req, struct list_head *list) |
3648e526 | 1327 | { |
d1fd1c20 | 1328 | struct io_buffer *kbuf = req->kbuf; |
3648e526 HX |
1329 | unsigned int cflags; |
1330 | ||
cc3cec83 | 1331 | cflags = IORING_CQE_F_BUFFER | (kbuf->bid << IORING_CQE_BUFFER_SHIFT); |
3648e526 | 1332 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
cc3cec83 | 1333 | list_add(&kbuf->list, list); |
d1fd1c20 | 1334 | req->kbuf = NULL; |
3648e526 HX |
1335 | return cflags; |
1336 | } | |
1337 | ||
cc3cec83 | 1338 | static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req) |
3648e526 | 1339 | { |
8197b053 PB |
1340 | lockdep_assert_held(&req->ctx->completion_lock); |
1341 | ||
3648e526 HX |
1342 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) |
1343 | return 0; | |
cc3cec83 JA |
1344 | return __io_put_kbuf(req, &req->ctx->io_buffers_comp); |
1345 | } | |
1346 | ||
1347 | static inline unsigned int io_put_kbuf(struct io_kiocb *req, | |
1348 | unsigned issue_flags) | |
1349 | { | |
1350 | unsigned int cflags; | |
1351 | ||
1352 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1353 | return 0; | |
1354 | ||
1355 | /* | |
1356 | * We can add this buffer back to two lists: | |
1357 | * | |
1358 | * 1) The io_buffers_cache list. This one is protected by the | |
1359 | * ctx->uring_lock. If we already hold this lock, add back to this | |
1360 | * list as we can grab it from issue as well. | |
1361 | * 2) The io_buffers_comp list. This one is protected by the | |
1362 | * ctx->completion_lock. | |
1363 | * | |
1364 | * We migrate buffers from the comp_list to the issue cache list | |
1365 | * when we need one. | |
1366 | */ | |
1367 | if (issue_flags & IO_URING_F_UNLOCKED) { | |
1368 | struct io_ring_ctx *ctx = req->ctx; | |
1369 | ||
1370 | spin_lock(&ctx->completion_lock); | |
1371 | cflags = __io_put_kbuf(req, &ctx->io_buffers_comp); | |
1372 | spin_unlock(&ctx->completion_lock); | |
1373 | } else { | |
ab0ac095 PB |
1374 | lockdep_assert_held(&req->ctx->uring_lock); |
1375 | ||
cc3cec83 JA |
1376 | cflags = __io_put_kbuf(req, &req->ctx->io_buffers_cache); |
1377 | } | |
1378 | ||
1379 | return cflags; | |
3648e526 HX |
1380 | } |
1381 | ||
dbc7d452 JA |
1382 | static struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx, |
1383 | unsigned int bgid) | |
1384 | { | |
1385 | struct list_head *hash_list; | |
1386 | struct io_buffer_list *bl; | |
1387 | ||
1388 | hash_list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
1389 | list_for_each_entry(bl, hash_list, list) | |
1390 | if (bl->bgid == bgid || bgid == -1U) | |
1391 | return bl; | |
1392 | ||
1393 | return NULL; | |
1394 | } | |
1395 | ||
4d55f238 | 1396 | static void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags) |
b1c62645 JA |
1397 | { |
1398 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 JA |
1399 | struct io_buffer_list *bl; |
1400 | struct io_buffer *buf; | |
b1c62645 JA |
1401 | |
1402 | if (likely(!(req->flags & REQ_F_BUFFER_SELECTED))) | |
1403 | return; | |
8a3e8ee5 JA |
1404 | /* don't recycle if we already did IO to this buffer */ |
1405 | if (req->flags & REQ_F_PARTIAL_IO) | |
1406 | return; | |
b1c62645 | 1407 | |
4d55f238 JA |
1408 | if (issue_flags & IO_URING_F_UNLOCKED) |
1409 | mutex_lock(&ctx->uring_lock); | |
b1c62645 JA |
1410 | |
1411 | lockdep_assert_held(&ctx->uring_lock); | |
1412 | ||
1413 | buf = req->kbuf; | |
dbc7d452 JA |
1414 | bl = io_buffer_get_list(ctx, buf->bgid); |
1415 | list_add(&buf->list, &bl->buf_list); | |
b1c62645 JA |
1416 | req->flags &= ~REQ_F_BUFFER_SELECTED; |
1417 | req->kbuf = NULL; | |
4d55f238 JA |
1418 | |
1419 | if (issue_flags & IO_URING_F_UNLOCKED) | |
1420 | mutex_unlock(&ctx->uring_lock); | |
b1c62645 JA |
1421 | } |
1422 | ||
3dd0c97a PB |
1423 | static bool io_match_task(struct io_kiocb *head, struct task_struct *task, |
1424 | bool cancel_all) | |
6af3f48b | 1425 | __must_hold(&req->ctx->timeout_lock) |
08d23634 PB |
1426 | { |
1427 | struct io_kiocb *req; | |
1428 | ||
68207680 | 1429 | if (task && head->task != task) |
08d23634 | 1430 | return false; |
3dd0c97a | 1431 | if (cancel_all) |
08d23634 PB |
1432 | return true; |
1433 | ||
1434 | io_for_each_link(req, head) { | |
b05a1bcd | 1435 | if (req->flags & REQ_F_INFLIGHT) |
02a13674 | 1436 | return true; |
08d23634 PB |
1437 | } |
1438 | return false; | |
1439 | } | |
1440 | ||
6af3f48b PB |
1441 | static bool io_match_linked(struct io_kiocb *head) |
1442 | { | |
1443 | struct io_kiocb *req; | |
1444 | ||
1445 | io_for_each_link(req, head) { | |
1446 | if (req->flags & REQ_F_INFLIGHT) | |
1447 | return true; | |
1448 | } | |
1449 | return false; | |
1450 | } | |
1451 | ||
1452 | /* | |
1453 | * As io_match_task() but protected against racing with linked timeouts. | |
1454 | * User must not hold timeout_lock. | |
1455 | */ | |
1456 | static bool io_match_task_safe(struct io_kiocb *head, struct task_struct *task, | |
1457 | bool cancel_all) | |
1458 | { | |
1459 | bool matched; | |
1460 | ||
1461 | if (task && head->task != task) | |
1462 | return false; | |
1463 | if (cancel_all) | |
1464 | return true; | |
1465 | ||
1466 | if (head->flags & REQ_F_LINK_TIMEOUT) { | |
1467 | struct io_ring_ctx *ctx = head->ctx; | |
1468 | ||
1469 | /* protect against races with linked timeouts */ | |
1470 | spin_lock_irq(&ctx->timeout_lock); | |
1471 | matched = io_match_linked(head); | |
1472 | spin_unlock_irq(&ctx->timeout_lock); | |
1473 | } else { | |
1474 | matched = io_match_linked(head); | |
1475 | } | |
1476 | return matched; | |
1477 | } | |
1478 | ||
d886e185 PB |
1479 | static inline bool req_has_async_data(struct io_kiocb *req) |
1480 | { | |
1481 | return req->flags & REQ_F_ASYNC_DATA; | |
1482 | } | |
1483 | ||
93d2bcd2 | 1484 | static inline void req_set_fail(struct io_kiocb *req) |
c40f6379 | 1485 | { |
93d2bcd2 | 1486 | req->flags |= REQ_F_FAIL; |
04c76b41 PB |
1487 | if (req->flags & REQ_F_CQE_SKIP) { |
1488 | req->flags &= ~REQ_F_CQE_SKIP; | |
1489 | req->flags |= REQ_F_SKIP_LINK_CQES; | |
1490 | } | |
c40f6379 | 1491 | } |
4a38aed2 | 1492 | |
a8295b98 HX |
1493 | static inline void req_fail_link_node(struct io_kiocb *req, int res) |
1494 | { | |
1495 | req_set_fail(req); | |
1496 | req->result = res; | |
1497 | } | |
1498 | ||
c072481d | 1499 | static __cold void io_ring_ctx_ref_free(struct percpu_ref *ref) |
2b188cc1 JA |
1500 | { |
1501 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
1502 | ||
0f158b4c | 1503 | complete(&ctx->ref_comp); |
2b188cc1 JA |
1504 | } |
1505 | ||
8eb7e2d0 PB |
1506 | static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
1507 | { | |
1508 | return !req->timeout.off; | |
1509 | } | |
1510 | ||
c072481d | 1511 | static __cold void io_fallback_req_func(struct work_struct *work) |
f56165e6 PB |
1512 | { |
1513 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, | |
1514 | fallback_work.work); | |
1515 | struct llist_node *node = llist_del_all(&ctx->fallback_llist); | |
1516 | struct io_kiocb *req, *tmp; | |
f237c30a | 1517 | bool locked = false; |
f56165e6 PB |
1518 | |
1519 | percpu_ref_get(&ctx->refs); | |
1520 | llist_for_each_entry_safe(req, tmp, node, io_task_work.fallback_node) | |
f237c30a | 1521 | req->io_task_work.func(req, &locked); |
5636c00d | 1522 | |
f237c30a | 1523 | if (locked) { |
c450178d | 1524 | io_submit_flush_completions(ctx); |
f237c30a PB |
1525 | mutex_unlock(&ctx->uring_lock); |
1526 | } | |
f56165e6 PB |
1527 | percpu_ref_put(&ctx->refs); |
1528 | } | |
1529 | ||
c072481d | 1530 | static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
2b188cc1 JA |
1531 | { |
1532 | struct io_ring_ctx *ctx; | |
dbc7d452 | 1533 | int i, hash_bits; |
2b188cc1 JA |
1534 | |
1535 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1536 | if (!ctx) | |
1537 | return NULL; | |
1538 | ||
78076bb6 JA |
1539 | /* |
1540 | * Use 5 bits less than the max cq entries, that should give us around | |
1541 | * 32 entries per hash list if totally full and uniformly spread. | |
1542 | */ | |
1543 | hash_bits = ilog2(p->cq_entries); | |
1544 | hash_bits -= 5; | |
1545 | if (hash_bits <= 0) | |
1546 | hash_bits = 1; | |
1547 | ctx->cancel_hash_bits = hash_bits; | |
1548 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
1549 | GFP_KERNEL); | |
1550 | if (!ctx->cancel_hash) | |
1551 | goto err; | |
1552 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
1553 | ||
6224843d PB |
1554 | ctx->dummy_ubuf = kzalloc(sizeof(*ctx->dummy_ubuf), GFP_KERNEL); |
1555 | if (!ctx->dummy_ubuf) | |
1556 | goto err; | |
1557 | /* set invalid range, so io_import_fixed() fails meeting it */ | |
1558 | ctx->dummy_ubuf->ubuf = -1UL; | |
1559 | ||
dbc7d452 JA |
1560 | ctx->io_buffers = kcalloc(1U << IO_BUFFERS_HASH_BITS, |
1561 | sizeof(struct list_head), GFP_KERNEL); | |
1562 | if (!ctx->io_buffers) | |
1563 | goto err; | |
1564 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) | |
1565 | INIT_LIST_HEAD(&ctx->io_buffers[i]); | |
1566 | ||
21482896 | 1567 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
1568 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
1569 | goto err; | |
2b188cc1 JA |
1570 | |
1571 | ctx->flags = p->flags; | |
90554200 | 1572 | init_waitqueue_head(&ctx->sqo_sq_wait); |
69fb2131 | 1573 | INIT_LIST_HEAD(&ctx->sqd_list); |
1d7bb1d5 | 1574 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
cc3cec83 | 1575 | INIT_LIST_HEAD(&ctx->io_buffers_cache); |
4d9237e3 | 1576 | INIT_LIST_HEAD(&ctx->apoll_cache); |
0f158b4c | 1577 | init_completion(&ctx->ref_comp); |
61cf9370 | 1578 | xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
2b188cc1 | 1579 | mutex_init(&ctx->uring_lock); |
311997b3 | 1580 | init_waitqueue_head(&ctx->cq_wait); |
2b188cc1 | 1581 | spin_lock_init(&ctx->completion_lock); |
89850fce | 1582 | spin_lock_init(&ctx->timeout_lock); |
5eef4e87 | 1583 | INIT_WQ_LIST(&ctx->iopoll_list); |
cc3cec83 JA |
1584 | INIT_LIST_HEAD(&ctx->io_buffers_pages); |
1585 | INIT_LIST_HEAD(&ctx->io_buffers_comp); | |
de0617e4 | 1586 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 1587 | INIT_LIST_HEAD(&ctx->timeout_list); |
ef9dd637 | 1588 | INIT_LIST_HEAD(&ctx->ltimeout_list); |
d67d2263 BM |
1589 | spin_lock_init(&ctx->rsrc_ref_lock); |
1590 | INIT_LIST_HEAD(&ctx->rsrc_ref_list); | |
269bbe5f BM |
1591 | INIT_DELAYED_WORK(&ctx->rsrc_put_work, io_rsrc_put_work); |
1592 | init_llist_head(&ctx->rsrc_put_llist); | |
13bf43f5 | 1593 | INIT_LIST_HEAD(&ctx->tctx_list); |
c2b6c6bc PB |
1594 | ctx->submit_state.free_list.next = NULL; |
1595 | INIT_WQ_LIST(&ctx->locked_free_list); | |
9011bf9a | 1596 | INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); |
6f33b0bc | 1597 | INIT_WQ_LIST(&ctx->submit_state.compl_reqs); |
adc8682e OL |
1598 | #ifdef CONFIG_NET_RX_BUSY_POLL |
1599 | INIT_LIST_HEAD(&ctx->napi_list); | |
1600 | spin_lock_init(&ctx->napi_lock); | |
1601 | #endif | |
2b188cc1 | 1602 | return ctx; |
206aefde | 1603 | err: |
6224843d | 1604 | kfree(ctx->dummy_ubuf); |
78076bb6 | 1605 | kfree(ctx->cancel_hash); |
dbc7d452 | 1606 | kfree(ctx->io_buffers); |
206aefde JA |
1607 | kfree(ctx); |
1608 | return NULL; | |
2b188cc1 JA |
1609 | } |
1610 | ||
8f6ed49a PB |
1611 | static void io_account_cq_overflow(struct io_ring_ctx *ctx) |
1612 | { | |
1613 | struct io_rings *r = ctx->rings; | |
1614 | ||
1615 | WRITE_ONCE(r->cq_overflow, READ_ONCE(r->cq_overflow) + 1); | |
1616 | ctx->cq_extra--; | |
1617 | } | |
1618 | ||
9cf7c104 | 1619 | static bool req_need_defer(struct io_kiocb *req, u32 seq) |
7adf4eaf | 1620 | { |
2bc9930e JA |
1621 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
1622 | struct io_ring_ctx *ctx = req->ctx; | |
a197f664 | 1623 | |
8f6ed49a | 1624 | return seq + READ_ONCE(ctx->cq_extra) != ctx->cached_cq_tail; |
2bc9930e | 1625 | } |
de0617e4 | 1626 | |
9d858b21 | 1627 | return false; |
de0617e4 JA |
1628 | } |
1629 | ||
35645ac3 PB |
1630 | #define FFS_NOWAIT 0x1UL |
1631 | #define FFS_ISREG 0x2UL | |
1632 | #define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG) | |
c97d8a0f PB |
1633 | |
1634 | static inline bool io_req_ffs_set(struct io_kiocb *req) | |
1635 | { | |
35645ac3 | 1636 | return req->flags & REQ_F_FIXED_FILE; |
c97d8a0f PB |
1637 | } |
1638 | ||
c072481d | 1639 | static inline void io_req_track_inflight(struct io_kiocb *req) |
ce3d5aae | 1640 | { |
ce3d5aae | 1641 | if (!(req->flags & REQ_F_INFLIGHT)) { |
ce3d5aae | 1642 | req->flags |= REQ_F_INFLIGHT; |
b303fe2e | 1643 | atomic_inc(¤t->io_uring->inflight_tracked); |
ce3d5aae PB |
1644 | } |
1645 | } | |
1646 | ||
fd08e530 PB |
1647 | static struct io_kiocb *__io_prep_linked_timeout(struct io_kiocb *req) |
1648 | { | |
906c6caa PB |
1649 | if (WARN_ON_ONCE(!req->link)) |
1650 | return NULL; | |
1651 | ||
4d13d1a4 PB |
1652 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
1653 | req->flags |= REQ_F_LINK_TIMEOUT; | |
fd08e530 PB |
1654 | |
1655 | /* linked timeouts should have two refs once prep'ed */ | |
48dcd38d | 1656 | io_req_set_refcount(req); |
4d13d1a4 PB |
1657 | __io_req_set_refcount(req->link, 2); |
1658 | return req->link; | |
fd08e530 PB |
1659 | } |
1660 | ||
1661 | static inline struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) | |
1662 | { | |
4d13d1a4 | 1663 | if (likely(!(req->flags & REQ_F_ARM_LTIMEOUT))) |
fd08e530 PB |
1664 | return NULL; |
1665 | return __io_prep_linked_timeout(req); | |
1666 | } | |
1667 | ||
1e6fa521 JA |
1668 | static void io_prep_async_work(struct io_kiocb *req) |
1669 | { | |
1670 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
1e6fa521 JA |
1671 | struct io_ring_ctx *ctx = req->ctx; |
1672 | ||
b8e64b53 PB |
1673 | if (!(req->flags & REQ_F_CREDS)) { |
1674 | req->flags |= REQ_F_CREDS; | |
c10d1f98 | 1675 | req->creds = get_current_cred(); |
b8e64b53 | 1676 | } |
003e8dcc | 1677 | |
e1d675df PB |
1678 | req->work.list.next = NULL; |
1679 | req->work.flags = 0; | |
feaadc4f PB |
1680 | if (req->flags & REQ_F_FORCE_ASYNC) |
1681 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
1682 | ||
1e6fa521 JA |
1683 | if (req->flags & REQ_F_ISREG) { |
1684 | if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) | |
1685 | io_wq_hash_work(&req->work, file_inode(req->file)); | |
4b982bd0 | 1686 | } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
1e6fa521 JA |
1687 | if (def->unbound_nonreg_file) |
1688 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1689 | } | |
e1d675df PB |
1690 | |
1691 | switch (req->opcode) { | |
1692 | case IORING_OP_SPLICE: | |
1693 | case IORING_OP_TEE: | |
e1d675df PB |
1694 | if (!S_ISREG(file_inode(req->splice.file_in)->i_mode)) |
1695 | req->work.flags |= IO_WQ_WORK_UNBOUND; | |
1696 | break; | |
1697 | } | |
561fb04a | 1698 | } |
cccf0ee8 | 1699 | |
cbdcb435 | 1700 | static void io_prep_async_link(struct io_kiocb *req) |
561fb04a | 1701 | { |
cbdcb435 | 1702 | struct io_kiocb *cur; |
54a91f3b | 1703 | |
44eff40a PB |
1704 | if (req->flags & REQ_F_LINK_TIMEOUT) { |
1705 | struct io_ring_ctx *ctx = req->ctx; | |
1706 | ||
674ee8e1 | 1707 | spin_lock_irq(&ctx->timeout_lock); |
44eff40a PB |
1708 | io_for_each_link(cur, req) |
1709 | io_prep_async_work(cur); | |
674ee8e1 | 1710 | spin_unlock_irq(&ctx->timeout_lock); |
44eff40a PB |
1711 | } else { |
1712 | io_for_each_link(cur, req) | |
1713 | io_prep_async_work(cur); | |
1714 | } | |
561fb04a JA |
1715 | } |
1716 | ||
fff4e40e PB |
1717 | static inline void io_req_add_compl_list(struct io_kiocb *req) |
1718 | { | |
3d4aeb9f | 1719 | struct io_ring_ctx *ctx = req->ctx; |
33ce2aff | 1720 | struct io_submit_state *state = &ctx->submit_state; |
fff4e40e | 1721 | |
3d4aeb9f PB |
1722 | if (!(req->flags & REQ_F_CQE_SKIP)) |
1723 | ctx->submit_state.flush_cqes = true; | |
fff4e40e PB |
1724 | wq_list_add_tail(&req->comp_list, &state->compl_reqs); |
1725 | } | |
1726 | ||
00169246 | 1727 | static void io_queue_async_work(struct io_kiocb *req, bool *dont_use) |
561fb04a | 1728 | { |
a197f664 | 1729 | struct io_ring_ctx *ctx = req->ctx; |
cbdcb435 | 1730 | struct io_kiocb *link = io_prep_linked_timeout(req); |
5aa75ed5 | 1731 | struct io_uring_task *tctx = req->task->io_uring; |
561fb04a | 1732 | |
3bfe6106 JA |
1733 | BUG_ON(!tctx); |
1734 | BUG_ON(!tctx->io_wq); | |
561fb04a | 1735 | |
cbdcb435 PB |
1736 | /* init ->work of the whole link before punting */ |
1737 | io_prep_async_link(req); | |
991468dc JA |
1738 | |
1739 | /* | |
1740 | * Not expected to happen, but if we do have a bug where this _can_ | |
1741 | * happen, catch it here and ensure the request is marked as | |
1742 | * canceled. That will make io-wq go through the usual work cancel | |
1743 | * procedure rather than attempt to run this request (or create a new | |
1744 | * worker for it). | |
1745 | */ | |
1746 | if (WARN_ON_ONCE(!same_thread_group(req->task, current))) | |
1747 | req->work.flags |= IO_WQ_WORK_CANCEL; | |
1748 | ||
502c87d6 SR |
1749 | trace_io_uring_queue_async_work(ctx, req, req->user_data, req->opcode, req->flags, |
1750 | &req->work, io_wq_is_hashed(&req->work)); | |
ebf93667 | 1751 | io_wq_enqueue(tctx->io_wq, &req->work); |
7271ef3a JA |
1752 | if (link) |
1753 | io_queue_linked_timeout(link); | |
cbdcb435 PB |
1754 | } |
1755 | ||
1ee4160c | 1756 | static void io_kill_timeout(struct io_kiocb *req, int status) |
8c855885 | 1757 | __must_hold(&req->ctx->completion_lock) |
89850fce | 1758 | __must_hold(&req->ctx->timeout_lock) |
5262f567 | 1759 | { |
e8c2bc1f | 1760 | struct io_timeout_data *io = req->async_data; |
5262f567 | 1761 | |
fd9c7bc5 | 1762 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
2ae2eb9d PB |
1763 | if (status) |
1764 | req_set_fail(req); | |
01cec8c1 PB |
1765 | atomic_set(&req->ctx->cq_timeouts, |
1766 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
135fcde8 | 1767 | list_del_init(&req->timeout.list); |
913a571a | 1768 | io_fill_cqe_req(req, status, 0); |
91c2f697 | 1769 | io_put_req_deferred(req); |
5262f567 JA |
1770 | } |
1771 | } | |
1772 | ||
c072481d | 1773 | static __cold void io_queue_deferred(struct io_ring_ctx *ctx) |
de0617e4 | 1774 | { |
441b8a78 | 1775 | while (!list_empty(&ctx->defer_list)) { |
27dc8338 PB |
1776 | struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
1777 | struct io_defer_entry, list); | |
de0617e4 | 1778 | |
9cf7c104 | 1779 | if (req_need_defer(de->req, de->seq)) |
04518945 | 1780 | break; |
27dc8338 | 1781 | list_del_init(&de->list); |
907d1df3 | 1782 | io_req_task_queue(de->req); |
27dc8338 | 1783 | kfree(de); |
441b8a78 | 1784 | } |
04518945 PB |
1785 | } |
1786 | ||
c072481d | 1787 | static __cold void io_flush_timeouts(struct io_ring_ctx *ctx) |
89850fce | 1788 | __must_hold(&ctx->completion_lock) |
de0617e4 | 1789 | { |
441b8a78 | 1790 | u32 seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
f010505b | 1791 | |
79ebeaee | 1792 | spin_lock_irq(&ctx->timeout_lock); |
f18ee4cf | 1793 | while (!list_empty(&ctx->timeout_list)) { |
f010505b | 1794 | u32 events_needed, events_got; |
360428f8 | 1795 | struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
135fcde8 | 1796 | struct io_kiocb, timeout.list); |
de0617e4 | 1797 | |
8eb7e2d0 | 1798 | if (io_is_timeout_noseq(req)) |
360428f8 | 1799 | break; |
f010505b MDG |
1800 | |
1801 | /* | |
1802 | * Since seq can easily wrap around over time, subtract | |
1803 | * the last seq at which timeouts were flushed before comparing. | |
1804 | * Assuming not more than 2^31-1 events have happened since, | |
1805 | * these subtractions won't have wrapped, so we can check if | |
1806 | * target is in [last_seq, current_seq] by comparing the two. | |
1807 | */ | |
1808 | events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; | |
1809 | events_got = seq - ctx->cq_last_tm_flush; | |
1810 | if (events_got < events_needed) | |
360428f8 | 1811 | break; |
bfe68a22 | 1812 | |
135fcde8 | 1813 | list_del_init(&req->timeout.list); |
1ee4160c | 1814 | io_kill_timeout(req, 0); |
f18ee4cf | 1815 | } |
f010505b | 1816 | ctx->cq_last_tm_flush = seq; |
79ebeaee | 1817 | spin_unlock_irq(&ctx->timeout_lock); |
360428f8 | 1818 | } |
5262f567 | 1819 | |
9333f6b4 PB |
1820 | static inline void io_commit_cqring(struct io_ring_ctx *ctx) |
1821 | { | |
1822 | /* order cqe stores with ring update */ | |
1823 | smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); | |
1824 | } | |
1825 | ||
9aa8dfde | 1826 | static void __io_commit_cqring_flush(struct io_ring_ctx *ctx) |
360428f8 | 1827 | { |
9aa8dfde PB |
1828 | if (ctx->off_timeout_used || ctx->drain_active) { |
1829 | spin_lock(&ctx->completion_lock); | |
1830 | if (ctx->off_timeout_used) | |
1831 | io_flush_timeouts(ctx); | |
1832 | if (ctx->drain_active) | |
1833 | io_queue_deferred(ctx); | |
1834 | io_commit_cqring(ctx); | |
1835 | spin_unlock(&ctx->completion_lock); | |
1836 | } | |
1837 | if (ctx->has_evfd) | |
1838 | io_eventfd_signal(ctx); | |
de0617e4 JA |
1839 | } |
1840 | ||
90554200 JA |
1841 | static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
1842 | { | |
1843 | struct io_rings *r = ctx->rings; | |
1844 | ||
a566c556 | 1845 | return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == ctx->sq_entries; |
90554200 JA |
1846 | } |
1847 | ||
888aae2e PB |
1848 | static inline unsigned int __io_cqring_events(struct io_ring_ctx *ctx) |
1849 | { | |
1850 | return ctx->cached_cq_tail - READ_ONCE(ctx->rings->cq.head); | |
1851 | } | |
1852 | ||
d068b506 | 1853 | static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) |
2b188cc1 | 1854 | { |
75b28aff | 1855 | struct io_rings *rings = ctx->rings; |
ea5ab3b5 | 1856 | unsigned tail, mask = ctx->cq_entries - 1; |
2b188cc1 | 1857 | |
115e12e5 SB |
1858 | /* |
1859 | * writes to the cq entry need to come after reading head; the | |
1860 | * control dependency is enough as we're using WRITE_ONCE to | |
1861 | * fill the cq entry | |
1862 | */ | |
a566c556 | 1863 | if (__io_cqring_events(ctx) == ctx->cq_entries) |
2b188cc1 JA |
1864 | return NULL; |
1865 | ||
888aae2e | 1866 | tail = ctx->cached_cq_tail++; |
ea5ab3b5 | 1867 | return &rings->cqes[tail & mask]; |
2b188cc1 JA |
1868 | } |
1869 | ||
77bc59b4 | 1870 | static void io_eventfd_signal(struct io_ring_ctx *ctx) |
f2842ab5 | 1871 | { |
77bc59b4 UA |
1872 | struct io_ev_fd *ev_fd; |
1873 | ||
77bc59b4 UA |
1874 | rcu_read_lock(); |
1875 | /* | |
1876 | * rcu_dereference ctx->io_ev_fd once and use it for both for checking | |
1877 | * and eventfd_signal | |
1878 | */ | |
1879 | ev_fd = rcu_dereference(ctx->io_ev_fd); | |
1880 | ||
1881 | /* | |
1882 | * Check again if ev_fd exists incase an io_eventfd_unregister call | |
1883 | * completed between the NULL check of ctx->io_ev_fd at the start of | |
1884 | * the function and rcu_read_lock. | |
1885 | */ | |
1886 | if (unlikely(!ev_fd)) | |
1887 | goto out; | |
7e55a19c | 1888 | if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
77bc59b4 UA |
1889 | goto out; |
1890 | ||
c75312dd | 1891 | if (!ev_fd->eventfd_async || io_wq_current_is_worker()) |
77bc59b4 | 1892 | eventfd_signal(ev_fd->cq_ev_fd, 1); |
77bc59b4 UA |
1893 | out: |
1894 | rcu_read_unlock(); | |
f2842ab5 JA |
1895 | } |
1896 | ||
9aa8dfde PB |
1897 | static inline void io_cqring_wake(struct io_ring_ctx *ctx) |
1898 | { | |
1899 | /* | |
1900 | * wake_up_all() may seem excessive, but io_wake_function() and | |
1901 | * io_should_wake() handle the termination of the loop and only | |
1902 | * wake as many waiters as we need to. | |
1903 | */ | |
1904 | if (wq_has_sleeper(&ctx->cq_wait)) | |
1905 | wake_up_all(&ctx->cq_wait); | |
1906 | } | |
1907 | ||
2c5d763c JA |
1908 | /* |
1909 | * This should only get called when at least one event has been posted. | |
1910 | * Some applications rely on the eventfd notification count only changing | |
1911 | * IFF a new CQE has been added to the CQ ring. There's no depedency on | |
1912 | * 1:1 relationship between how many times this function is called (and | |
1913 | * hence the eventfd count) and number of CQEs posted to the CQ ring. | |
1914 | */ | |
66fc25ca | 1915 | static inline void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1d7bb1d5 | 1916 | { |
9aa8dfde PB |
1917 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1918 | ctx->has_evfd)) | |
9333f6b4 PB |
1919 | __io_commit_cqring_flush(ctx); |
1920 | ||
9aa8dfde | 1921 | io_cqring_wake(ctx); |
1d7bb1d5 JA |
1922 | } |
1923 | ||
80c18e4a PB |
1924 | static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) |
1925 | { | |
9aa8dfde PB |
1926 | if (unlikely(ctx->off_timeout_used || ctx->drain_active || |
1927 | ctx->has_evfd)) | |
9333f6b4 PB |
1928 | __io_commit_cqring_flush(ctx); |
1929 | ||
9aa8dfde PB |
1930 | if (ctx->flags & IORING_SETUP_SQPOLL) |
1931 | io_cqring_wake(ctx); | |
80c18e4a PB |
1932 | } |
1933 | ||
c4a2ed72 | 1934 | /* Returns true if there are no backlogged entries after the flush */ |
6c2450ae | 1935 | static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) |
1d7bb1d5 | 1936 | { |
b18032bb | 1937 | bool all_flushed, posted; |
1d7bb1d5 | 1938 | |
a566c556 | 1939 | if (!force && __io_cqring_events(ctx) == ctx->cq_entries) |
e23de15f | 1940 | return false; |
1d7bb1d5 | 1941 | |
b18032bb | 1942 | posted = false; |
79ebeaee | 1943 | spin_lock(&ctx->completion_lock); |
6c2450ae | 1944 | while (!list_empty(&ctx->cq_overflow_list)) { |
d068b506 | 1945 | struct io_uring_cqe *cqe = io_get_cqe(ctx); |
6c2450ae | 1946 | struct io_overflow_cqe *ocqe; |
e6c8aa9a | 1947 | |
1d7bb1d5 JA |
1948 | if (!cqe && !force) |
1949 | break; | |
6c2450ae PB |
1950 | ocqe = list_first_entry(&ctx->cq_overflow_list, |
1951 | struct io_overflow_cqe, list); | |
1952 | if (cqe) | |
1953 | memcpy(cqe, &ocqe->cqe, sizeof(*cqe)); | |
1954 | else | |
8f6ed49a PB |
1955 | io_account_cq_overflow(ctx); |
1956 | ||
b18032bb | 1957 | posted = true; |
6c2450ae PB |
1958 | list_del(&ocqe->list); |
1959 | kfree(ocqe); | |
1d7bb1d5 JA |
1960 | } |
1961 | ||
09e88404 PB |
1962 | all_flushed = list_empty(&ctx->cq_overflow_list); |
1963 | if (all_flushed) { | |
5ed7a37d | 1964 | clear_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
1965 | WRITE_ONCE(ctx->rings->sq_flags, |
1966 | ctx->rings->sq_flags & ~IORING_SQ_CQ_OVERFLOW); | |
09e88404 | 1967 | } |
46930143 | 1968 | |
b18032bb JA |
1969 | if (posted) |
1970 | io_commit_cqring(ctx); | |
79ebeaee | 1971 | spin_unlock(&ctx->completion_lock); |
b18032bb JA |
1972 | if (posted) |
1973 | io_cqring_ev_posted(ctx); | |
09e88404 | 1974 | return all_flushed; |
1d7bb1d5 JA |
1975 | } |
1976 | ||
90f67366 | 1977 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx) |
6c503150 | 1978 | { |
ca0a2651 JA |
1979 | bool ret = true; |
1980 | ||
5ed7a37d | 1981 | if (test_bit(0, &ctx->check_cq_overflow)) { |
6c503150 PB |
1982 | /* iopoll syncs against uring_lock, not completion_lock */ |
1983 | if (ctx->flags & IORING_SETUP_IOPOLL) | |
1984 | mutex_lock(&ctx->uring_lock); | |
90f67366 | 1985 | ret = __io_cqring_overflow_flush(ctx, false); |
6c503150 PB |
1986 | if (ctx->flags & IORING_SETUP_IOPOLL) |
1987 | mutex_unlock(&ctx->uring_lock); | |
1988 | } | |
ca0a2651 JA |
1989 | |
1990 | return ret; | |
6c503150 PB |
1991 | } |
1992 | ||
6a290a14 PB |
1993 | /* must to be called somewhat shortly after putting a request */ |
1994 | static inline void io_put_task(struct task_struct *task, int nr) | |
1995 | { | |
1996 | struct io_uring_task *tctx = task->io_uring; | |
1997 | ||
e98e49b2 PB |
1998 | if (likely(task == current)) { |
1999 | tctx->cached_refs += nr; | |
2000 | } else { | |
2001 | percpu_counter_sub(&tctx->inflight, nr); | |
2002 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2003 | wake_up(&tctx->wait); | |
2004 | put_task_struct_many(task, nr); | |
2005 | } | |
6a290a14 PB |
2006 | } |
2007 | ||
9a10867a PB |
2008 | static void io_task_refs_refill(struct io_uring_task *tctx) |
2009 | { | |
2010 | unsigned int refill = -tctx->cached_refs + IO_TCTX_REFS_CACHE_NR; | |
2011 | ||
2012 | percpu_counter_add(&tctx->inflight, refill); | |
2013 | refcount_add(refill, ¤t->usage); | |
2014 | tctx->cached_refs += refill; | |
2015 | } | |
2016 | ||
2017 | static inline void io_get_task_refs(int nr) | |
2018 | { | |
2019 | struct io_uring_task *tctx = current->io_uring; | |
2020 | ||
2021 | tctx->cached_refs -= nr; | |
2022 | if (unlikely(tctx->cached_refs < 0)) | |
2023 | io_task_refs_refill(tctx); | |
2024 | } | |
2025 | ||
3cc7fdb9 PB |
2026 | static __cold void io_uring_drop_tctx_refs(struct task_struct *task) |
2027 | { | |
2028 | struct io_uring_task *tctx = task->io_uring; | |
2029 | unsigned int refs = tctx->cached_refs; | |
2030 | ||
2031 | if (refs) { | |
2032 | tctx->cached_refs = 0; | |
2033 | percpu_counter_sub(&tctx->inflight, refs); | |
2034 | put_task_struct_many(task, refs); | |
2035 | } | |
2036 | } | |
2037 | ||
d4d19c19 | 2038 | static bool io_cqring_event_overflow(struct io_ring_ctx *ctx, u64 user_data, |
54daa9b2 | 2039 | s32 res, u32 cflags) |
2b188cc1 | 2040 | { |
cce4b8b0 | 2041 | struct io_overflow_cqe *ocqe; |
2b188cc1 | 2042 | |
cce4b8b0 PB |
2043 | ocqe = kmalloc(sizeof(*ocqe), GFP_ATOMIC | __GFP_ACCOUNT); |
2044 | if (!ocqe) { | |
2045 | /* | |
2046 | * If we're in ring overflow flush mode, or in task cancel mode, | |
2047 | * or cannot allocate an overflow entry, then we need to drop it | |
2048 | * on the floor. | |
2049 | */ | |
8f6ed49a | 2050 | io_account_cq_overflow(ctx); |
cce4b8b0 | 2051 | return false; |
2b188cc1 | 2052 | } |
cce4b8b0 | 2053 | if (list_empty(&ctx->cq_overflow_list)) { |
5ed7a37d | 2054 | set_bit(0, &ctx->check_cq_overflow); |
20c0b380 NA |
2055 | WRITE_ONCE(ctx->rings->sq_flags, |
2056 | ctx->rings->sq_flags | IORING_SQ_CQ_OVERFLOW); | |
2057 | ||
cce4b8b0 | 2058 | } |
d4d19c19 | 2059 | ocqe->cqe.user_data = user_data; |
cce4b8b0 PB |
2060 | ocqe->cqe.res = res; |
2061 | ocqe->cqe.flags = cflags; | |
2062 | list_add_tail(&ocqe->list, &ctx->cq_overflow_list); | |
2063 | return true; | |
2b188cc1 JA |
2064 | } |
2065 | ||
ae4da189 | 2066 | static inline bool __io_fill_cqe(struct io_ring_ctx *ctx, u64 user_data, |
913a571a | 2067 | s32 res, u32 cflags) |
2b188cc1 JA |
2068 | { |
2069 | struct io_uring_cqe *cqe; | |
2070 | ||
2071 | /* | |
2072 | * If we can't get a cq entry, userspace overflowed the | |
2073 | * submission (by quite a lot). Increment the overflow count in | |
2074 | * the ring. | |
2075 | */ | |
d068b506 | 2076 | cqe = io_get_cqe(ctx); |
1d7bb1d5 | 2077 | if (likely(cqe)) { |
d4d19c19 | 2078 | WRITE_ONCE(cqe->user_data, user_data); |
2b188cc1 | 2079 | WRITE_ONCE(cqe->res, res); |
bcda7baa | 2080 | WRITE_ONCE(cqe->flags, cflags); |
8d13326e | 2081 | return true; |
2b188cc1 | 2082 | } |
d4d19c19 | 2083 | return io_cqring_event_overflow(ctx, user_data, res, cflags); |
2b188cc1 JA |
2084 | } |
2085 | ||
ae4da189 | 2086 | static inline bool __io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
d5ec1dfa | 2087 | { |
502c87d6 | 2088 | trace_io_uring_complete(req->ctx, req, req->user_data, res, cflags); |
ae4da189 | 2089 | return __io_fill_cqe(req->ctx, req->user_data, res, cflags); |
d5ec1dfa SR |
2090 | } |
2091 | ||
913a571a | 2092 | static noinline void io_fill_cqe_req(struct io_kiocb *req, s32 res, u32 cflags) |
bcda7baa | 2093 | { |
04c76b41 | 2094 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2095 | __io_fill_cqe_req(req, res, cflags); |
bcda7baa JA |
2096 | } |
2097 | ||
913a571a PB |
2098 | static noinline bool io_fill_cqe_aux(struct io_ring_ctx *ctx, u64 user_data, |
2099 | s32 res, u32 cflags) | |
bcda7baa | 2100 | { |
913a571a | 2101 | ctx->cq_extra++; |
502c87d6 | 2102 | trace_io_uring_complete(ctx, NULL, user_data, res, cflags); |
ae4da189 | 2103 | return __io_fill_cqe(ctx, user_data, res, cflags); |
bcda7baa JA |
2104 | } |
2105 | ||
a37fae8a HX |
2106 | static void __io_req_complete_post(struct io_kiocb *req, s32 res, |
2107 | u32 cflags) | |
2b188cc1 | 2108 | { |
78e19bbe | 2109 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 | 2110 | |
04c76b41 | 2111 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2112 | __io_fill_cqe_req(req, res, cflags); |
c7dae4ba JA |
2113 | /* |
2114 | * If we're the last reference to this request, add to our locked | |
2115 | * free_list cache. | |
2116 | */ | |
de9b4cca | 2117 | if (req_ref_put_and_test(req)) { |
7a612350 | 2118 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { |
0756a869 | 2119 | if (req->flags & IO_DISARM_MASK) |
7a612350 PB |
2120 | io_disarm_next(req); |
2121 | if (req->link) { | |
2122 | io_req_task_queue(req->link); | |
2123 | req->link = NULL; | |
2124 | } | |
2125 | } | |
ab409402 | 2126 | io_req_put_rsrc(req, ctx); |
8197b053 PB |
2127 | /* |
2128 | * Selected buffer deallocation in io_clean_op() assumes that | |
2129 | * we don't hold ->completion_lock. Clean them here to avoid | |
2130 | * deadlocks. | |
2131 | */ | |
2132 | io_put_kbuf_comp(req); | |
c7dae4ba JA |
2133 | io_dismantle_req(req); |
2134 | io_put_task(req->task, 1); | |
c2b6c6bc | 2135 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
d0acdee2 | 2136 | ctx->locked_free_nr++; |
180f829f | 2137 | } |
a37fae8a HX |
2138 | } |
2139 | ||
2140 | static void io_req_complete_post(struct io_kiocb *req, s32 res, | |
2141 | u32 cflags) | |
2142 | { | |
2143 | struct io_ring_ctx *ctx = req->ctx; | |
2144 | ||
2145 | spin_lock(&ctx->completion_lock); | |
2146 | __io_req_complete_post(req, res, cflags); | |
7a612350 | 2147 | io_commit_cqring(ctx); |
79ebeaee | 2148 | spin_unlock(&ctx->completion_lock); |
a3f34907 | 2149 | io_cqring_ev_posted(ctx); |
4e3d9ff9 JA |
2150 | } |
2151 | ||
54daa9b2 PB |
2152 | static inline void io_req_complete_state(struct io_kiocb *req, s32 res, |
2153 | u32 cflags) | |
229a7b63 | 2154 | { |
a38d68db | 2155 | req->result = res; |
d17e56eb | 2156 | req->cflags = cflags; |
e342c807 | 2157 | req->flags |= REQ_F_COMPLETE_INLINE; |
e1e16097 JA |
2158 | } |
2159 | ||
889fca73 | 2160 | static inline void __io_req_complete(struct io_kiocb *req, unsigned issue_flags, |
54daa9b2 | 2161 | s32 res, u32 cflags) |
bcda7baa | 2162 | { |
889fca73 PB |
2163 | if (issue_flags & IO_URING_F_COMPLETE_DEFER) |
2164 | io_req_complete_state(req, res, cflags); | |
a38d68db | 2165 | else |
c7dae4ba | 2166 | io_req_complete_post(req, res, cflags); |
bcda7baa JA |
2167 | } |
2168 | ||
54daa9b2 | 2169 | static inline void io_req_complete(struct io_kiocb *req, s32 res) |
0ddf92e8 | 2170 | { |
889fca73 | 2171 | __io_req_complete(req, 0, res, 0); |
0ddf92e8 JA |
2172 | } |
2173 | ||
54daa9b2 | 2174 | static void io_req_complete_failed(struct io_kiocb *req, s32 res) |
f41db273 | 2175 | { |
93d2bcd2 | 2176 | req_set_fail(req); |
ab0ac095 | 2177 | io_req_complete_post(req, res, io_put_kbuf(req, IO_URING_F_UNLOCKED)); |
f41db273 PB |
2178 | } |
2179 | ||
c6d3d9cb PB |
2180 | static void io_req_complete_fail_submit(struct io_kiocb *req) |
2181 | { | |
2182 | /* | |
2183 | * We don't submit, fail them all, for that replace hardlinks with | |
2184 | * normal links. Extra REQ_F_LINK is tolerated. | |
2185 | */ | |
2186 | req->flags &= ~REQ_F_HARDLINK; | |
2187 | req->flags |= REQ_F_LINK; | |
2188 | io_req_complete_failed(req, req->result); | |
2189 | } | |
2190 | ||
864ea921 PB |
2191 | /* |
2192 | * Don't initialise the fields below on every allocation, but do that in | |
2193 | * advance and keep them valid across allocations. | |
2194 | */ | |
2195 | static void io_preinit_req(struct io_kiocb *req, struct io_ring_ctx *ctx) | |
2196 | { | |
2197 | req->ctx = ctx; | |
2198 | req->link = NULL; | |
2199 | req->async_data = NULL; | |
2200 | /* not necessary, but safer to zero */ | |
2201 | req->result = 0; | |
2202 | } | |
2203 | ||
dac7a098 | 2204 | static void io_flush_cached_locked_reqs(struct io_ring_ctx *ctx, |
cd0ca2e0 | 2205 | struct io_submit_state *state) |
dac7a098 | 2206 | { |
79ebeaee | 2207 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2208 | wq_list_splice(&ctx->locked_free_list, &state->free_list); |
d0acdee2 | 2209 | ctx->locked_free_nr = 0; |
79ebeaee | 2210 | spin_unlock(&ctx->completion_lock); |
dac7a098 PB |
2211 | } |
2212 | ||
dd78f492 | 2213 | /* Returns true IFF there are requests in the cache */ |
c7dae4ba | 2214 | static bool io_flush_cached_reqs(struct io_ring_ctx *ctx) |
0ddf92e8 | 2215 | { |
c7dae4ba | 2216 | struct io_submit_state *state = &ctx->submit_state; |
0ddf92e8 | 2217 | |
c7dae4ba JA |
2218 | /* |
2219 | * If we have more than a batch's worth of requests in our IRQ side | |
2220 | * locked cache, grab the lock and move them over to our submission | |
2221 | * side cache. | |
2222 | */ | |
d0acdee2 | 2223 | if (READ_ONCE(ctx->locked_free_nr) > IO_COMPL_BATCH) |
cd0ca2e0 | 2224 | io_flush_cached_locked_reqs(ctx, state); |
c2b6c6bc | 2225 | return !!state->free_list.next; |
0ddf92e8 JA |
2226 | } |
2227 | ||
5d5901a3 PB |
2228 | /* |
2229 | * A request might get retired back into the request caches even before opcode | |
2230 | * handlers and io_issue_sqe() are done with it, e.g. inline completion path. | |
2231 | * Because of that, io_alloc_req() should be called only under ->uring_lock | |
2232 | * and with extra caution to not get a request that is still worked on. | |
2233 | */ | |
c072481d | 2234 | static __cold bool __io_alloc_req_refill(struct io_ring_ctx *ctx) |
5d5901a3 | 2235 | __must_hold(&ctx->uring_lock) |
2b188cc1 | 2236 | { |
e5d1bc0a | 2237 | struct io_submit_state *state = &ctx->submit_state; |
864ea921 | 2238 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
3ab665b7 PB |
2239 | void *reqs[IO_REQ_ALLOC_BATCH]; |
2240 | struct io_kiocb *req; | |
864ea921 | 2241 | int ret, i; |
e5d1bc0a | 2242 | |
c2b6c6bc | 2243 | if (likely(state->free_list.next || io_flush_cached_reqs(ctx))) |
a33ae9ce | 2244 | return true; |
e5d1bc0a | 2245 | |
3ab665b7 | 2246 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs); |
fd6fab2c | 2247 | |
864ea921 PB |
2248 | /* |
2249 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
2250 | * retry single alloc to be on the safe side. | |
2251 | */ | |
2252 | if (unlikely(ret <= 0)) { | |
3ab665b7 PB |
2253 | reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
2254 | if (!reqs[0]) | |
a33ae9ce | 2255 | return false; |
864ea921 | 2256 | ret = 1; |
2b188cc1 | 2257 | } |
864ea921 | 2258 | |
37f0e767 | 2259 | percpu_ref_get_many(&ctx->refs, ret); |
3ab665b7 PB |
2260 | for (i = 0; i < ret; i++) { |
2261 | req = reqs[i]; | |
2262 | ||
2263 | io_preinit_req(req, ctx); | |
c2b6c6bc | 2264 | wq_stack_add_head(&req->comp_list, &state->free_list); |
3ab665b7 | 2265 | } |
a33ae9ce PB |
2266 | return true; |
2267 | } | |
2268 | ||
2269 | static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx) | |
2270 | { | |
2271 | if (unlikely(!ctx->submit_state.free_list.next)) | |
2272 | return __io_alloc_req_refill(ctx); | |
2273 | return true; | |
2274 | } | |
2275 | ||
2276 | static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx) | |
2277 | { | |
2278 | struct io_wq_work_node *node; | |
2279 | ||
2280 | node = wq_stack_extract(&ctx->submit_state.free_list); | |
c2b6c6bc | 2281 | return container_of(node, struct io_kiocb, comp_list); |
2b188cc1 JA |
2282 | } |
2283 | ||
e1d767f0 | 2284 | static inline void io_put_file(struct file *file) |
8da11c19 | 2285 | { |
e1d767f0 | 2286 | if (file) |
8da11c19 PB |
2287 | fput(file); |
2288 | } | |
2289 | ||
6b639522 | 2290 | static inline void io_dismantle_req(struct io_kiocb *req) |
2b188cc1 | 2291 | { |
094bae49 | 2292 | unsigned int flags = req->flags; |
929a3af9 | 2293 | |
867f8fa5 | 2294 | if (unlikely(flags & IO_REQ_CLEAN_FLAGS)) |
3a0a6902 | 2295 | io_clean_op(req); |
e1d767f0 PB |
2296 | if (!(flags & REQ_F_FIXED_FILE)) |
2297 | io_put_file(req->file); | |
e65ef56d JA |
2298 | } |
2299 | ||
c072481d | 2300 | static __cold void __io_free_req(struct io_kiocb *req) |
c6ca97b3 | 2301 | { |
51a4cc11 | 2302 | struct io_ring_ctx *ctx = req->ctx; |
c6ca97b3 | 2303 | |
ab409402 | 2304 | io_req_put_rsrc(req, ctx); |
216578e5 | 2305 | io_dismantle_req(req); |
7c660731 | 2306 | io_put_task(req->task, 1); |
c6ca97b3 | 2307 | |
79ebeaee | 2308 | spin_lock(&ctx->completion_lock); |
c2b6c6bc | 2309 | wq_list_add_head(&req->comp_list, &ctx->locked_free_list); |
c34b025f | 2310 | ctx->locked_free_nr++; |
79ebeaee | 2311 | spin_unlock(&ctx->completion_lock); |
e65ef56d JA |
2312 | } |
2313 | ||
f2f87370 PB |
2314 | static inline void io_remove_next_linked(struct io_kiocb *req) |
2315 | { | |
2316 | struct io_kiocb *nxt = req->link; | |
2317 | ||
2318 | req->link = nxt->link; | |
2319 | nxt->link = NULL; | |
2320 | } | |
2321 | ||
33cc89a9 PB |
2322 | static bool io_kill_linked_timeout(struct io_kiocb *req) |
2323 | __must_hold(&req->ctx->completion_lock) | |
89b263f6 | 2324 | __must_hold(&req->ctx->timeout_lock) |
2665abfd | 2325 | { |
33cc89a9 | 2326 | struct io_kiocb *link = req->link; |
f2f87370 | 2327 | |
b97e736a | 2328 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
c9abd7ad | 2329 | struct io_timeout_data *io = link->async_data; |
7c86ffee | 2330 | |
f2f87370 | 2331 | io_remove_next_linked(req); |
90cd7e42 | 2332 | link->timeout.head = NULL; |
fd9c7bc5 | 2333 | if (hrtimer_try_to_cancel(&io->timer) != -1) { |
ef9dd637 | 2334 | list_del(&link->timeout.list); |
04c76b41 | 2335 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2336 | io_fill_cqe_req(link, -ECANCELED, 0); |
91c2f697 | 2337 | io_put_req_deferred(link); |
d4729fbd | 2338 | return true; |
c9abd7ad PB |
2339 | } |
2340 | } | |
d4729fbd | 2341 | return false; |
7c86ffee PB |
2342 | } |
2343 | ||
d148ca4b | 2344 | static void io_fail_links(struct io_kiocb *req) |
33cc89a9 | 2345 | __must_hold(&req->ctx->completion_lock) |
9e645e11 | 2346 | { |
33cc89a9 | 2347 | struct io_kiocb *nxt, *link = req->link; |
04c76b41 | 2348 | bool ignore_cqes = req->flags & REQ_F_SKIP_LINK_CQES; |
9e645e11 | 2349 | |
f2f87370 | 2350 | req->link = NULL; |
f2f87370 | 2351 | while (link) { |
a8295b98 HX |
2352 | long res = -ECANCELED; |
2353 | ||
2354 | if (link->flags & REQ_F_FAIL) | |
2355 | res = link->result; | |
2356 | ||
f2f87370 PB |
2357 | nxt = link->link; |
2358 | link->link = NULL; | |
2665abfd | 2359 | |
502c87d6 SR |
2360 | trace_io_uring_fail_link(req->ctx, req, req->user_data, |
2361 | req->opcode, link); | |
2362 | ||
04c76b41 PB |
2363 | if (!ignore_cqes) { |
2364 | link->flags &= ~REQ_F_CQE_SKIP; | |
2365 | io_fill_cqe_req(link, res, 0); | |
2366 | } | |
91c2f697 | 2367 | io_put_req_deferred(link); |
f2f87370 | 2368 | link = nxt; |
9e645e11 | 2369 | } |
33cc89a9 | 2370 | } |
9e645e11 | 2371 | |
33cc89a9 PB |
2372 | static bool io_disarm_next(struct io_kiocb *req) |
2373 | __must_hold(&req->ctx->completion_lock) | |
2374 | { | |
2375 | bool posted = false; | |
2376 | ||
0756a869 PB |
2377 | if (req->flags & REQ_F_ARM_LTIMEOUT) { |
2378 | struct io_kiocb *link = req->link; | |
2379 | ||
906c6caa | 2380 | req->flags &= ~REQ_F_ARM_LTIMEOUT; |
0756a869 PB |
2381 | if (link && link->opcode == IORING_OP_LINK_TIMEOUT) { |
2382 | io_remove_next_linked(req); | |
04c76b41 | 2383 | /* leave REQ_F_CQE_SKIP to io_fill_cqe_req */ |
913a571a | 2384 | io_fill_cqe_req(link, -ECANCELED, 0); |
0756a869 PB |
2385 | io_put_req_deferred(link); |
2386 | posted = true; | |
2387 | } | |
2388 | } else if (req->flags & REQ_F_LINK_TIMEOUT) { | |
89b263f6 JA |
2389 | struct io_ring_ctx *ctx = req->ctx; |
2390 | ||
2391 | spin_lock_irq(&ctx->timeout_lock); | |
33cc89a9 | 2392 | posted = io_kill_linked_timeout(req); |
89b263f6 JA |
2393 | spin_unlock_irq(&ctx->timeout_lock); |
2394 | } | |
93d2bcd2 | 2395 | if (unlikely((req->flags & REQ_F_FAIL) && |
e4335ed3 | 2396 | !(req->flags & REQ_F_HARDLINK))) { |
33cc89a9 PB |
2397 | posted |= (req->link != NULL); |
2398 | io_fail_links(req); | |
2399 | } | |
2400 | return posted; | |
9e645e11 JA |
2401 | } |
2402 | ||
d81499bf PB |
2403 | static void __io_req_find_next_prep(struct io_kiocb *req) |
2404 | { | |
2405 | struct io_ring_ctx *ctx = req->ctx; | |
2406 | bool posted; | |
2407 | ||
2408 | spin_lock(&ctx->completion_lock); | |
2409 | posted = io_disarm_next(req); | |
2410 | if (posted) | |
33ce2aff | 2411 | io_commit_cqring(ctx); |
d81499bf PB |
2412 | spin_unlock(&ctx->completion_lock); |
2413 | if (posted) | |
2414 | io_cqring_ev_posted(ctx); | |
2415 | } | |
2416 | ||
2417 | static inline struct io_kiocb *io_req_find_next(struct io_kiocb *req) | |
c69f8dbe | 2418 | { |
33cc89a9 | 2419 | struct io_kiocb *nxt; |
944e58bf | 2420 | |
d81499bf PB |
2421 | if (likely(!(req->flags & (REQ_F_LINK|REQ_F_HARDLINK)))) |
2422 | return NULL; | |
9e645e11 JA |
2423 | /* |
2424 | * If LINK is set, we have dependent requests in this chain. If we | |
2425 | * didn't fail this request, queue the first one up, moving any other | |
2426 | * dependencies to the next request. In case of failure, fail the rest | |
2427 | * of the chain. | |
2428 | */ | |
d81499bf PB |
2429 | if (unlikely(req->flags & IO_DISARM_MASK)) |
2430 | __io_req_find_next_prep(req); | |
33cc89a9 PB |
2431 | nxt = req->link; |
2432 | req->link = NULL; | |
2433 | return nxt; | |
4d7dd462 | 2434 | } |
9e645e11 | 2435 | |
f237c30a | 2436 | static void ctx_flush_and_put(struct io_ring_ctx *ctx, bool *locked) |
2c32395d PB |
2437 | { |
2438 | if (!ctx) | |
2439 | return; | |
f237c30a | 2440 | if (*locked) { |
c450178d | 2441 | io_submit_flush_completions(ctx); |
2c32395d | 2442 | mutex_unlock(&ctx->uring_lock); |
f237c30a | 2443 | *locked = false; |
2c32395d PB |
2444 | } |
2445 | percpu_ref_put(&ctx->refs); | |
2446 | } | |
2447 | ||
f28c240e HX |
2448 | static inline void ctx_commit_and_unlock(struct io_ring_ctx *ctx) |
2449 | { | |
2450 | io_commit_cqring(ctx); | |
2451 | spin_unlock(&ctx->completion_lock); | |
2452 | io_cqring_ev_posted(ctx); | |
2453 | } | |
2454 | ||
2455 | static void handle_prev_tw_list(struct io_wq_work_node *node, | |
2456 | struct io_ring_ctx **ctx, bool *uring_locked) | |
2457 | { | |
2458 | if (*ctx && !*uring_locked) | |
2459 | spin_lock(&(*ctx)->completion_lock); | |
2460 | ||
2461 | do { | |
2462 | struct io_wq_work_node *next = node->next; | |
2463 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2464 | io_task_work.node); | |
2465 | ||
34d2bfe7 JA |
2466 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2467 | ||
f28c240e HX |
2468 | if (req->ctx != *ctx) { |
2469 | if (unlikely(!*uring_locked && *ctx)) | |
2470 | ctx_commit_and_unlock(*ctx); | |
2471 | ||
2472 | ctx_flush_and_put(*ctx, uring_locked); | |
2473 | *ctx = req->ctx; | |
2474 | /* if not contended, grab and improve batching */ | |
2475 | *uring_locked = mutex_trylock(&(*ctx)->uring_lock); | |
2476 | percpu_ref_get(&(*ctx)->refs); | |
2477 | if (unlikely(!*uring_locked)) | |
2478 | spin_lock(&(*ctx)->completion_lock); | |
2479 | } | |
2480 | if (likely(*uring_locked)) | |
2481 | req->io_task_work.func(req, uring_locked); | |
2482 | else | |
cc3cec83 JA |
2483 | __io_req_complete_post(req, req->result, |
2484 | io_put_kbuf_comp(req)); | |
f28c240e HX |
2485 | node = next; |
2486 | } while (node); | |
2487 | ||
2488 | if (unlikely(!*uring_locked)) | |
2489 | ctx_commit_and_unlock(*ctx); | |
2490 | } | |
2491 | ||
2492 | static void handle_tw_list(struct io_wq_work_node *node, | |
2493 | struct io_ring_ctx **ctx, bool *locked) | |
9f8d032a HX |
2494 | { |
2495 | do { | |
2496 | struct io_wq_work_node *next = node->next; | |
2497 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2498 | io_task_work.node); | |
2499 | ||
34d2bfe7 JA |
2500 | prefetch(container_of(next, struct io_kiocb, io_task_work.node)); |
2501 | ||
9f8d032a HX |
2502 | if (req->ctx != *ctx) { |
2503 | ctx_flush_and_put(*ctx, locked); | |
2504 | *ctx = req->ctx; | |
2505 | /* if not contended, grab and improve batching */ | |
2506 | *locked = mutex_trylock(&(*ctx)->uring_lock); | |
2507 | percpu_ref_get(&(*ctx)->refs); | |
2508 | } | |
2509 | req->io_task_work.func(req, locked); | |
2510 | node = next; | |
2511 | } while (node); | |
2512 | } | |
2513 | ||
7cbf1722 | 2514 | static void tctx_task_work(struct callback_head *cb) |
c40f6379 | 2515 | { |
f28c240e | 2516 | bool uring_locked = false; |
ebd0df2e | 2517 | struct io_ring_ctx *ctx = NULL; |
3f18407d PB |
2518 | struct io_uring_task *tctx = container_of(cb, struct io_uring_task, |
2519 | task_work); | |
c40f6379 | 2520 | |
16f72070 | 2521 | while (1) { |
f28c240e | 2522 | struct io_wq_work_node *node1, *node2; |
3f18407d | 2523 | |
f28c240e HX |
2524 | if (!tctx->task_list.first && |
2525 | !tctx->prior_task_list.first && uring_locked) | |
8d4ad41e PB |
2526 | io_submit_flush_completions(ctx); |
2527 | ||
3f18407d | 2528 | spin_lock_irq(&tctx->task_lock); |
f28c240e HX |
2529 | node1 = tctx->prior_task_list.first; |
2530 | node2 = tctx->task_list.first; | |
3f18407d | 2531 | INIT_WQ_LIST(&tctx->task_list); |
f28c240e HX |
2532 | INIT_WQ_LIST(&tctx->prior_task_list); |
2533 | if (!node2 && !node1) | |
6294f368 | 2534 | tctx->task_running = false; |
3f18407d | 2535 | spin_unlock_irq(&tctx->task_lock); |
f28c240e | 2536 | if (!node2 && !node1) |
6294f368 | 2537 | break; |
3f18407d | 2538 | |
f28c240e HX |
2539 | if (node1) |
2540 | handle_prev_tw_list(node1, &ctx, &uring_locked); | |
6294f368 | 2541 | |
f28c240e HX |
2542 | if (node2) |
2543 | handle_tw_list(node2, &ctx, &uring_locked); | |
7cbf1722 | 2544 | cond_resched(); |
3f18407d | 2545 | } |
ebd0df2e | 2546 | |
f28c240e | 2547 | ctx_flush_and_put(ctx, &uring_locked); |
3cc7fdb9 PB |
2548 | |
2549 | /* relaxed read is enough as only the task itself sets ->in_idle */ | |
2550 | if (unlikely(atomic_read(&tctx->in_idle))) | |
2551 | io_uring_drop_tctx_refs(current); | |
7cbf1722 JA |
2552 | } |
2553 | ||
4813c377 | 2554 | static void io_req_task_work_add(struct io_kiocb *req, bool priority) |
7cbf1722 | 2555 | { |
c15b79de | 2556 | struct task_struct *tsk = req->task; |
7cbf1722 | 2557 | struct io_uring_task *tctx = tsk->io_uring; |
c15b79de | 2558 | enum task_work_notify_mode notify; |
e09ee510 | 2559 | struct io_wq_work_node *node; |
0b81e80c | 2560 | unsigned long flags; |
6294f368 | 2561 | bool running; |
7cbf1722 JA |
2562 | |
2563 | WARN_ON_ONCE(!tctx); | |
2564 | ||
0b81e80c | 2565 | spin_lock_irqsave(&tctx->task_lock, flags); |
4813c377 HX |
2566 | if (priority) |
2567 | wq_list_add_tail(&req->io_task_work.node, &tctx->prior_task_list); | |
2568 | else | |
2569 | wq_list_add_tail(&req->io_task_work.node, &tctx->task_list); | |
6294f368 PB |
2570 | running = tctx->task_running; |
2571 | if (!running) | |
2572 | tctx->task_running = true; | |
0b81e80c | 2573 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 JA |
2574 | |
2575 | /* task_work already pending, we're done */ | |
6294f368 | 2576 | if (running) |
e09ee510 | 2577 | return; |
7cbf1722 | 2578 | |
c15b79de PB |
2579 | /* |
2580 | * SQPOLL kernel thread doesn't need notification, just a wakeup. For | |
2581 | * all other cases, use TWA_SIGNAL unconditionally to ensure we're | |
2582 | * processing task_work. There's no reliable way to tell if TWA_RESUME | |
2583 | * will do the job. | |
2584 | */ | |
2585 | notify = (req->ctx->flags & IORING_SETUP_SQPOLL) ? TWA_NONE : TWA_SIGNAL; | |
d97ec623 PB |
2586 | if (likely(!task_work_add(tsk, &tctx->task_work, notify))) { |
2587 | if (notify == TWA_NONE) | |
2588 | wake_up_process(tsk); | |
e09ee510 | 2589 | return; |
c15b79de | 2590 | } |
2215bed9 | 2591 | |
0b81e80c | 2592 | spin_lock_irqsave(&tctx->task_lock, flags); |
6294f368 | 2593 | tctx->task_running = false; |
4813c377 | 2594 | node = wq_list_merge(&tctx->prior_task_list, &tctx->task_list); |
0b81e80c | 2595 | spin_unlock_irqrestore(&tctx->task_lock, flags); |
7cbf1722 | 2596 | |
e09ee510 PB |
2597 | while (node) { |
2598 | req = container_of(node, struct io_kiocb, io_task_work.node); | |
2599 | node = node->next; | |
2600 | if (llist_add(&req->io_task_work.fallback_node, | |
2601 | &req->ctx->fallback_llist)) | |
2602 | schedule_delayed_work(&req->ctx->fallback_work, 1); | |
2603 | } | |
eab30c4d PB |
2604 | } |
2605 | ||
f237c30a | 2606 | static void io_req_task_cancel(struct io_kiocb *req, bool *locked) |
c40f6379 | 2607 | { |
87ceb6a6 | 2608 | struct io_ring_ctx *ctx = req->ctx; |
c40f6379 | 2609 | |
b18a1a45 | 2610 | /* not needed for normal modes, but SQPOLL depends on it */ |
f237c30a | 2611 | io_tw_lock(ctx, locked); |
2593553a | 2612 | io_req_complete_failed(req, req->result); |
c40f6379 JA |
2613 | } |
2614 | ||
f237c30a | 2615 | static void io_req_task_submit(struct io_kiocb *req, bool *locked) |
c40f6379 JA |
2616 | { |
2617 | struct io_ring_ctx *ctx = req->ctx; | |
2618 | ||
f237c30a | 2619 | io_tw_lock(ctx, locked); |
316319e8 | 2620 | /* req->task == current here, checking PF_EXITING is safe */ |
af066f31 | 2621 | if (likely(!(req->task->flags & PF_EXITING))) |
c5eef2b9 | 2622 | __io_queue_sqe(req); |
81b6d05c | 2623 | else |
2593553a | 2624 | io_req_complete_failed(req, -EFAULT); |
c40f6379 JA |
2625 | } |
2626 | ||
2c4b8eb6 | 2627 | static void io_req_task_queue_fail(struct io_kiocb *req, int ret) |
c40f6379 | 2628 | { |
2c4b8eb6 | 2629 | req->result = ret; |
5b0a6acc | 2630 | req->io_task_work.func = io_req_task_cancel; |
4813c377 | 2631 | io_req_task_work_add(req, false); |
c40f6379 JA |
2632 | } |
2633 | ||
2c4b8eb6 | 2634 | static void io_req_task_queue(struct io_kiocb *req) |
a3df7698 | 2635 | { |
5b0a6acc | 2636 | req->io_task_work.func = io_req_task_submit; |
4813c377 | 2637 | io_req_task_work_add(req, false); |
a3df7698 PB |
2638 | } |
2639 | ||
773af691 JA |
2640 | static void io_req_task_queue_reissue(struct io_kiocb *req) |
2641 | { | |
2642 | req->io_task_work.func = io_queue_async_work; | |
4813c377 | 2643 | io_req_task_work_add(req, false); |
773af691 JA |
2644 | } |
2645 | ||
f2f87370 | 2646 | static inline void io_queue_next(struct io_kiocb *req) |
c69f8dbe | 2647 | { |
9b5f7bd9 | 2648 | struct io_kiocb *nxt = io_req_find_next(req); |
944e58bf PB |
2649 | |
2650 | if (nxt) | |
906a8c3f | 2651 | io_req_task_queue(nxt); |
c69f8dbe JL |
2652 | } |
2653 | ||
c3524383 | 2654 | static void io_free_req(struct io_kiocb *req) |
7a743e22 | 2655 | { |
c3524383 PB |
2656 | io_queue_next(req); |
2657 | __io_free_req(req); | |
2658 | } | |
8766dd51 | 2659 | |
f237c30a PB |
2660 | static void io_free_req_work(struct io_kiocb *req, bool *locked) |
2661 | { | |
2662 | io_free_req(req); | |
2663 | } | |
2664 | ||
3aa83bfb | 2665 | static void io_free_batch_list(struct io_ring_ctx *ctx, |
1cce17ac | 2666 | struct io_wq_work_node *node) |
3aa83bfb | 2667 | __must_hold(&ctx->uring_lock) |
5af1d13e | 2668 | { |
d4b7a5ef | 2669 | struct task_struct *task = NULL; |
37f0e767 | 2670 | int task_refs = 0; |
5af1d13e | 2671 | |
3aa83bfb PB |
2672 | do { |
2673 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
2674 | comp_list); | |
2d6500d4 | 2675 | |
def77acf | 2676 | if (unlikely(req->flags & REQ_F_REFCOUNT)) { |
c1e53a69 | 2677 | node = req->comp_list.next; |
def77acf PB |
2678 | if (!req_ref_put_and_test(req)) |
2679 | continue; | |
c1e53a69 | 2680 | } |
2d6500d4 | 2681 | |
ab409402 | 2682 | io_req_put_rsrc_locked(req, ctx); |
d4b7a5ef PB |
2683 | io_queue_next(req); |
2684 | io_dismantle_req(req); | |
5af1d13e | 2685 | |
d4b7a5ef PB |
2686 | if (req->task != task) { |
2687 | if (task) | |
2688 | io_put_task(task, task_refs); | |
2689 | task = req->task; | |
2690 | task_refs = 0; | |
2691 | } | |
2692 | task_refs++; | |
c1e53a69 | 2693 | node = req->comp_list.next; |
d4b7a5ef | 2694 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
3aa83bfb | 2695 | } while (node); |
d4b7a5ef | 2696 | |
d4b7a5ef PB |
2697 | if (task) |
2698 | io_put_task(task, task_refs); | |
7a743e22 PB |
2699 | } |
2700 | ||
c450178d | 2701 | static void __io_submit_flush_completions(struct io_ring_ctx *ctx) |
a141dd89 | 2702 | __must_hold(&ctx->uring_lock) |
905c172f | 2703 | { |
6f33b0bc | 2704 | struct io_wq_work_node *node, *prev; |
cd0ca2e0 | 2705 | struct io_submit_state *state = &ctx->submit_state; |
905c172f | 2706 | |
3d4aeb9f PB |
2707 | if (state->flush_cqes) { |
2708 | spin_lock(&ctx->completion_lock); | |
2709 | wq_list_for_each(node, prev, &state->compl_reqs) { | |
2710 | struct io_kiocb *req = container_of(node, struct io_kiocb, | |
6f33b0bc | 2711 | comp_list); |
5182ed2e | 2712 | |
3d4aeb9f | 2713 | if (!(req->flags & REQ_F_CQE_SKIP)) |
ae4da189 | 2714 | __io_fill_cqe_req(req, req->result, req->cflags); |
4d9237e3 JA |
2715 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
2716 | struct async_poll *apoll = req->apoll; | |
2717 | ||
2718 | if (apoll->double_poll) | |
2719 | kfree(apoll->double_poll); | |
2720 | list_add(&apoll->poll.wait.entry, | |
2721 | &ctx->apoll_cache); | |
2722 | req->flags &= ~REQ_F_POLLED; | |
2723 | } | |
3d4aeb9f PB |
2724 | } |
2725 | ||
2726 | io_commit_cqring(ctx); | |
2727 | spin_unlock(&ctx->completion_lock); | |
2728 | io_cqring_ev_posted(ctx); | |
2729 | state->flush_cqes = false; | |
905c172f | 2730 | } |
5182ed2e | 2731 | |
1cce17ac | 2732 | io_free_batch_list(ctx, state->compl_reqs.first); |
6f33b0bc | 2733 | INIT_WQ_LIST(&state->compl_reqs); |
7a743e22 PB |
2734 | } |
2735 | ||
ba816ad6 JA |
2736 | /* |
2737 | * Drop reference to request, return next in chain (if there is one) if this | |
2738 | * was the last reference to this request. | |
2739 | */ | |
0d85035a | 2740 | static inline struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
e65ef56d | 2741 | { |
9b5f7bd9 PB |
2742 | struct io_kiocb *nxt = NULL; |
2743 | ||
de9b4cca | 2744 | if (req_ref_put_and_test(req)) { |
9b5f7bd9 | 2745 | nxt = io_req_find_next(req); |
4d7dd462 | 2746 | __io_free_req(req); |
2a44f467 | 2747 | } |
9b5f7bd9 | 2748 | return nxt; |
2b188cc1 JA |
2749 | } |
2750 | ||
0d85035a | 2751 | static inline void io_put_req(struct io_kiocb *req) |
e65ef56d | 2752 | { |
de9b4cca | 2753 | if (req_ref_put_and_test(req)) |
e65ef56d | 2754 | io_free_req(req); |
2b188cc1 JA |
2755 | } |
2756 | ||
91c2f697 | 2757 | static inline void io_put_req_deferred(struct io_kiocb *req) |
216578e5 | 2758 | { |
91c2f697 | 2759 | if (req_ref_put_and_test(req)) { |
f237c30a | 2760 | req->io_task_work.func = io_free_req_work; |
4813c377 | 2761 | io_req_task_work_add(req, false); |
543af3a1 | 2762 | } |
216578e5 PB |
2763 | } |
2764 | ||
6c503150 | 2765 | static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
a3a0e43f JA |
2766 | { |
2767 | /* See comment at the top of this file */ | |
2768 | smp_rmb(); | |
e23de15f | 2769 | return __io_cqring_events(ctx); |
a3a0e43f JA |
2770 | } |
2771 | ||
fb5ccc98 PB |
2772 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
2773 | { | |
2774 | struct io_rings *rings = ctx->rings; | |
2775 | ||
2776 | /* make sure SQ entry isn't read before tail */ | |
2777 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
2778 | } | |
2779 | ||
4c6e277c JA |
2780 | static inline bool io_run_task_work(void) |
2781 | { | |
7f62d40d | 2782 | if (test_thread_flag(TIF_NOTIFY_SIGNAL) || task_work_pending(current)) { |
4c6e277c | 2783 | __set_current_state(TASK_RUNNING); |
7c5d8fa6 EB |
2784 | clear_notify_signal(); |
2785 | if (task_work_pending(current)) | |
2786 | task_work_run(); | |
4c6e277c JA |
2787 | return true; |
2788 | } | |
2789 | ||
2790 | return false; | |
bcda7baa JA |
2791 | } |
2792 | ||
5ba3c874 | 2793 | static int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin) |
def596e9 | 2794 | { |
5eef4e87 | 2795 | struct io_wq_work_node *pos, *start, *prev; |
d729cf9a | 2796 | unsigned int poll_flags = BLK_POLL_NOSLEEP; |
b688f11e | 2797 | DEFINE_IO_COMP_BATCH(iob); |
5ba3c874 | 2798 | int nr_events = 0; |
def596e9 JA |
2799 | |
2800 | /* | |
2801 | * Only spin for completions if we don't have multiple devices hanging | |
87a115fb | 2802 | * off our complete list. |
def596e9 | 2803 | */ |
87a115fb | 2804 | if (ctx->poll_multi_queue || force_nonspin) |
ef99b2d3 | 2805 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2806 | |
5eef4e87 PB |
2807 | wq_list_for_each(pos, start, &ctx->iopoll_list) { |
2808 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
9adbd45d | 2809 | struct kiocb *kiocb = &req->rw.kiocb; |
a2416e1e | 2810 | int ret; |
def596e9 JA |
2811 | |
2812 | /* | |
581f9810 BM |
2813 | * Move completed and retryable entries to our local lists. |
2814 | * If we find a request that requires polling, break out | |
2815 | * and complete those lists first, if we have entries there. | |
def596e9 | 2816 | */ |
e3f721e6 | 2817 | if (READ_ONCE(req->iopoll_completed)) |
def596e9 JA |
2818 | break; |
2819 | ||
b688f11e | 2820 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, &iob, poll_flags); |
a2416e1e PB |
2821 | if (unlikely(ret < 0)) |
2822 | return ret; | |
2823 | else if (ret) | |
ef99b2d3 | 2824 | poll_flags |= BLK_POLL_ONESHOT; |
def596e9 | 2825 | |
3aadc23e | 2826 | /* iopoll may have completed current req */ |
b688f11e JA |
2827 | if (!rq_list_empty(iob.req_list) || |
2828 | READ_ONCE(req->iopoll_completed)) | |
e3f721e6 | 2829 | break; |
def596e9 JA |
2830 | } |
2831 | ||
b688f11e JA |
2832 | if (!rq_list_empty(iob.req_list)) |
2833 | iob.complete(&iob); | |
5eef4e87 PB |
2834 | else if (!pos) |
2835 | return 0; | |
def596e9 | 2836 | |
5eef4e87 PB |
2837 | prev = start; |
2838 | wq_list_for_each_resume(pos, prev) { | |
2839 | struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list); | |
2840 | ||
b3fa03fd PB |
2841 | /* order with io_complete_rw_iopoll(), e.g. ->result updates */ |
2842 | if (!smp_load_acquire(&req->iopoll_completed)) | |
e3f721e6 | 2843 | break; |
83a13a41 PB |
2844 | if (unlikely(req->flags & REQ_F_CQE_SKIP)) |
2845 | continue; | |
d1fd1c20 | 2846 | |
ae4da189 | 2847 | __io_fill_cqe_req(req, req->result, io_put_kbuf(req, 0)); |
e3f721e6 PB |
2848 | nr_events++; |
2849 | } | |
def596e9 | 2850 | |
f5ed3bcd PB |
2851 | if (unlikely(!nr_events)) |
2852 | return 0; | |
2853 | ||
2854 | io_commit_cqring(ctx); | |
2855 | io_cqring_ev_posted_iopoll(ctx); | |
1cce17ac | 2856 | pos = start ? start->next : ctx->iopoll_list.first; |
5eef4e87 | 2857 | wq_list_cut(&ctx->iopoll_list, prev, start); |
1cce17ac | 2858 | io_free_batch_list(ctx, pos); |
5ba3c874 | 2859 | return nr_events; |
def596e9 JA |
2860 | } |
2861 | ||
def596e9 JA |
2862 | /* |
2863 | * We can't just wait for polled events to come to us, we have to actively | |
2864 | * find and complete them. | |
2865 | */ | |
c072481d | 2866 | static __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
def596e9 JA |
2867 | { |
2868 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
2869 | return; | |
2870 | ||
2871 | mutex_lock(&ctx->uring_lock); | |
5eef4e87 | 2872 | while (!wq_list_empty(&ctx->iopoll_list)) { |
b2edc0a7 | 2873 | /* let it sleep and repeat later if can't complete a request */ |
5ba3c874 | 2874 | if (io_do_iopoll(ctx, true) == 0) |
b2edc0a7 | 2875 | break; |
08f5439f JA |
2876 | /* |
2877 | * Ensure we allow local-to-the-cpu processing to take place, | |
2878 | * in this case we need to ensure that we reap all events. | |
3fcee5a6 | 2879 | * Also let task_work, etc. to progress by releasing the mutex |
08f5439f | 2880 | */ |
3fcee5a6 PB |
2881 | if (need_resched()) { |
2882 | mutex_unlock(&ctx->uring_lock); | |
2883 | cond_resched(); | |
2884 | mutex_lock(&ctx->uring_lock); | |
2885 | } | |
def596e9 JA |
2886 | } |
2887 | mutex_unlock(&ctx->uring_lock); | |
2888 | } | |
2889 | ||
7668b92a | 2890 | static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
def596e9 | 2891 | { |
7668b92a | 2892 | unsigned int nr_events = 0; |
e9979b36 | 2893 | int ret = 0; |
500f9fba | 2894 | |
c7849be9 XW |
2895 | /* |
2896 | * We disallow the app entering submit/complete with polling, but we | |
2897 | * still need to lock the ring to prevent racing with polled issue | |
2898 | * that got punted to a workqueue. | |
2899 | */ | |
2900 | mutex_lock(&ctx->uring_lock); | |
f39c8a5b PB |
2901 | /* |
2902 | * Don't enter poll loop if we already have events pending. | |
2903 | * If we do, we can potentially be spinning for commands that | |
2904 | * already triggered a CQE (eg in error). | |
2905 | */ | |
5ed7a37d | 2906 | if (test_bit(0, &ctx->check_cq_overflow)) |
f39c8a5b PB |
2907 | __io_cqring_overflow_flush(ctx, false); |
2908 | if (io_cqring_events(ctx)) | |
2909 | goto out; | |
def596e9 | 2910 | do { |
500f9fba JA |
2911 | /* |
2912 | * If a submit got punted to a workqueue, we can have the | |
2913 | * application entering polling for a command before it gets | |
2914 | * issued. That app will hold the uring_lock for the duration | |
2915 | * of the poll right here, so we need to take a breather every | |
2916 | * now and then to ensure that the issue has a chance to add | |
2917 | * the poll to the issued list. Otherwise we can spin here | |
2918 | * forever, while the workqueue is stuck trying to acquire the | |
2919 | * very same mutex. | |
2920 | */ | |
5eef4e87 | 2921 | if (wq_list_empty(&ctx->iopoll_list)) { |
8f487ef2 PB |
2922 | u32 tail = ctx->cached_cq_tail; |
2923 | ||
500f9fba | 2924 | mutex_unlock(&ctx->uring_lock); |
4c6e277c | 2925 | io_run_task_work(); |
500f9fba | 2926 | mutex_lock(&ctx->uring_lock); |
def596e9 | 2927 | |
8f487ef2 PB |
2928 | /* some requests don't go through iopoll_list */ |
2929 | if (tail != ctx->cached_cq_tail || | |
5eef4e87 | 2930 | wq_list_empty(&ctx->iopoll_list)) |
e9979b36 | 2931 | break; |
500f9fba | 2932 | } |
5ba3c874 PB |
2933 | ret = io_do_iopoll(ctx, !min); |
2934 | if (ret < 0) | |
2935 | break; | |
2936 | nr_events += ret; | |
2937 | ret = 0; | |
2938 | } while (nr_events < min && !need_resched()); | |
f39c8a5b | 2939 | out: |
500f9fba | 2940 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
2941 | return ret; |
2942 | } | |
2943 | ||
491381ce | 2944 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 2945 | { |
491381ce JA |
2946 | /* |
2947 | * Tell lockdep we inherited freeze protection from submission | |
2948 | * thread. | |
2949 | */ | |
2950 | if (req->flags & REQ_F_ISREG) { | |
1c98679d | 2951 | struct super_block *sb = file_inode(req->file)->i_sb; |
2b188cc1 | 2952 | |
1c98679d PB |
2953 | __sb_writers_acquired(sb, SB_FREEZE_WRITE); |
2954 | sb_end_write(sb); | |
2b188cc1 JA |
2955 | } |
2956 | } | |
2957 | ||
b63534c4 | 2958 | #ifdef CONFIG_BLOCK |
dc2a6e9a | 2959 | static bool io_resubmit_prep(struct io_kiocb *req) |
b63534c4 | 2960 | { |
ab454438 | 2961 | struct io_async_rw *rw = req->async_data; |
b63534c4 | 2962 | |
d886e185 | 2963 | if (!req_has_async_data(req)) |
ab454438 | 2964 | return !io_req_prep_async(req); |
538941e2 | 2965 | iov_iter_restore(&rw->s.iter, &rw->s.iter_state); |
ab454438 | 2966 | return true; |
b63534c4 | 2967 | } |
b63534c4 | 2968 | |
3e6a0d3c | 2969 | static bool io_rw_should_reissue(struct io_kiocb *req) |
b63534c4 | 2970 | { |
355afaeb | 2971 | umode_t mode = file_inode(req->file)->i_mode; |
3e6a0d3c | 2972 | struct io_ring_ctx *ctx = req->ctx; |
b63534c4 | 2973 | |
355afaeb JA |
2974 | if (!S_ISBLK(mode) && !S_ISREG(mode)) |
2975 | return false; | |
3e6a0d3c JA |
2976 | if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() && |
2977 | !(ctx->flags & IORING_SETUP_IOPOLL))) | |
b63534c4 | 2978 | return false; |
7c977a58 JA |
2979 | /* |
2980 | * If ref is dying, we might be running poll reap from the exit work. | |
2981 | * Don't attempt to reissue from that path, just let it fail with | |
2982 | * -EAGAIN. | |
2983 | */ | |
3e6a0d3c JA |
2984 | if (percpu_ref_is_dying(&ctx->refs)) |
2985 | return false; | |
ef046888 JA |
2986 | /* |
2987 | * Play it safe and assume not safe to re-import and reissue if we're | |
2988 | * not in the original thread group (or in task context). | |
2989 | */ | |
2990 | if (!same_thread_group(req->task, current) || !in_task()) | |
2991 | return false; | |
3e6a0d3c JA |
2992 | return true; |
2993 | } | |
e82ad485 | 2994 | #else |
a1ff1e3f | 2995 | static bool io_resubmit_prep(struct io_kiocb *req) |
e82ad485 JA |
2996 | { |
2997 | return false; | |
2998 | } | |
e82ad485 | 2999 | static bool io_rw_should_reissue(struct io_kiocb *req) |
3e6a0d3c | 3000 | { |
b63534c4 JA |
3001 | return false; |
3002 | } | |
3e6a0d3c | 3003 | #endif |
b63534c4 | 3004 | |
8ef12efe | 3005 | static bool __io_complete_rw_common(struct io_kiocb *req, long res) |
a1d7c393 | 3006 | { |
f63cf519 | 3007 | if (req->rw.kiocb.ki_flags & IOCB_WRITE) { |
b65c128f | 3008 | kiocb_end_write(req); |
f63cf519 JA |
3009 | fsnotify_modify(req->file); |
3010 | } else { | |
3011 | fsnotify_access(req->file); | |
3012 | } | |
258f3a7f | 3013 | if (unlikely(res != req->result)) { |
9532b99b PB |
3014 | if ((res == -EAGAIN || res == -EOPNOTSUPP) && |
3015 | io_rw_should_reissue(req)) { | |
3016 | req->flags |= REQ_F_REISSUE; | |
8ef12efe | 3017 | return true; |
9532b99b | 3018 | } |
93d2bcd2 | 3019 | req_set_fail(req); |
8ef12efe | 3020 | req->result = res; |
9532b99b | 3021 | } |
8ef12efe JA |
3022 | return false; |
3023 | } | |
3024 | ||
cc8e9ba7 | 3025 | static inline void io_req_task_complete(struct io_kiocb *req, bool *locked) |
8ef12efe | 3026 | { |
54daa9b2 | 3027 | int res = req->result; |
126180b9 PB |
3028 | |
3029 | if (*locked) { | |
cc3cec83 | 3030 | io_req_complete_state(req, res, io_put_kbuf(req, 0)); |
fff4e40e | 3031 | io_req_add_compl_list(req); |
126180b9 | 3032 | } else { |
cc3cec83 JA |
3033 | io_req_complete_post(req, res, |
3034 | io_put_kbuf(req, IO_URING_F_UNLOCKED)); | |
126180b9 | 3035 | } |
8ef12efe JA |
3036 | } |
3037 | ||
00f6e68b | 3038 | static void __io_complete_rw(struct io_kiocb *req, long res, |
8ef12efe JA |
3039 | unsigned int issue_flags) |
3040 | { | |
3041 | if (__io_complete_rw_common(req, res)) | |
3042 | return; | |
cc3cec83 JA |
3043 | __io_req_complete(req, issue_flags, req->result, |
3044 | io_put_kbuf(req, issue_flags)); | |
ba816ad6 JA |
3045 | } |
3046 | ||
6b19b766 | 3047 | static void io_complete_rw(struct kiocb *kiocb, long res) |
ba816ad6 | 3048 | { |
9adbd45d | 3049 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 | 3050 | |
8ef12efe JA |
3051 | if (__io_complete_rw_common(req, res)) |
3052 | return; | |
3053 | req->result = res; | |
3054 | req->io_task_work.func = io_req_task_complete; | |
f28c240e | 3055 | io_req_task_work_add(req, !!(req->ctx->flags & IORING_SETUP_SQPOLL)); |
2b188cc1 JA |
3056 | } |
3057 | ||
6b19b766 | 3058 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res) |
def596e9 | 3059 | { |
9adbd45d | 3060 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 3061 | |
491381ce JA |
3062 | if (kiocb->ki_flags & IOCB_WRITE) |
3063 | kiocb_end_write(req); | |
9532b99b | 3064 | if (unlikely(res != req->result)) { |
b66ceaf3 PB |
3065 | if (res == -EAGAIN && io_rw_should_reissue(req)) { |
3066 | req->flags |= REQ_F_REISSUE; | |
3067 | return; | |
9532b99b | 3068 | } |
258f3a7f | 3069 | req->result = res; |
8c130827 | 3070 | } |
bbde017a | 3071 | |
b3fa03fd PB |
3072 | /* order with io_iopoll_complete() checking ->iopoll_completed */ |
3073 | smp_store_release(&req->iopoll_completed, 1); | |
def596e9 JA |
3074 | } |
3075 | ||
3076 | /* | |
3077 | * After the iocb has been issued, it's safe to be found on the poll list. | |
3078 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
f39c8a5b | 3079 | * find it from a io_do_iopoll() thread before the issuer is done |
def596e9 JA |
3080 | * accessing the kiocb cookie. |
3081 | */ | |
9882131c | 3082 | static void io_iopoll_req_issued(struct io_kiocb *req, unsigned int issue_flags) |
def596e9 JA |
3083 | { |
3084 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 3085 | const bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
cb3d8972 PB |
3086 | |
3087 | /* workqueue context doesn't hold uring_lock, grab it now */ | |
3b44b371 | 3088 | if (unlikely(needs_lock)) |
cb3d8972 | 3089 | mutex_lock(&ctx->uring_lock); |
def596e9 JA |
3090 | |
3091 | /* | |
3092 | * Track whether we have multiple files in our lists. This will impact | |
3093 | * how we do polling eventually, not spinning if we're on potentially | |
3094 | * different devices. | |
3095 | */ | |
5eef4e87 | 3096 | if (wq_list_empty(&ctx->iopoll_list)) { |
915b3dde HX |
3097 | ctx->poll_multi_queue = false; |
3098 | } else if (!ctx->poll_multi_queue) { | |
def596e9 JA |
3099 | struct io_kiocb *list_req; |
3100 | ||
5eef4e87 PB |
3101 | list_req = container_of(ctx->iopoll_list.first, struct io_kiocb, |
3102 | comp_list); | |
30da1b45 | 3103 | if (list_req->file != req->file) |
915b3dde | 3104 | ctx->poll_multi_queue = true; |
def596e9 JA |
3105 | } |
3106 | ||
3107 | /* | |
3108 | * For fast devices, IO may have already completed. If it has, add | |
3109 | * it to the front so we find it first. | |
3110 | */ | |
65a6543d | 3111 | if (READ_ONCE(req->iopoll_completed)) |
5eef4e87 | 3112 | wq_list_add_head(&req->comp_list, &ctx->iopoll_list); |
def596e9 | 3113 | else |
5eef4e87 | 3114 | wq_list_add_tail(&req->comp_list, &ctx->iopoll_list); |
bdcd3eab | 3115 | |
3b44b371 | 3116 | if (unlikely(needs_lock)) { |
cb3d8972 PB |
3117 | /* |
3118 | * If IORING_SETUP_SQPOLL is enabled, sqes are either handle | |
3119 | * in sq thread task context or in io worker task context. If | |
3120 | * current task context is sq thread, we don't need to check | |
3121 | * whether should wake up sq thread. | |
3122 | */ | |
3123 | if ((ctx->flags & IORING_SETUP_SQPOLL) && | |
3124 | wq_has_sleeper(&ctx->sq_data->wait)) | |
3125 | wake_up(&ctx->sq_data->wait); | |
3126 | ||
3127 | mutex_unlock(&ctx->uring_lock); | |
3128 | } | |
def596e9 JA |
3129 | } |
3130 | ||
4503b767 JA |
3131 | static bool io_bdev_nowait(struct block_device *bdev) |
3132 | { | |
9ba0d0c8 | 3133 | return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
4503b767 JA |
3134 | } |
3135 | ||
2b188cc1 JA |
3136 | /* |
3137 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3138 | * any file. For now, just ensure that anything potentially problematic is done | |
3139 | * inline. | |
3140 | */ | |
88459b50 | 3141 | static bool __io_file_supports_nowait(struct file *file, umode_t mode) |
2b188cc1 | 3142 | { |
4503b767 | 3143 | if (S_ISBLK(mode)) { |
4e7b5671 CH |
3144 | if (IS_ENABLED(CONFIG_BLOCK) && |
3145 | io_bdev_nowait(I_BDEV(file->f_mapping->host))) | |
4503b767 JA |
3146 | return true; |
3147 | return false; | |
3148 | } | |
976517f1 | 3149 | if (S_ISSOCK(mode)) |
2b188cc1 | 3150 | return true; |
4503b767 | 3151 | if (S_ISREG(mode)) { |
4e7b5671 CH |
3152 | if (IS_ENABLED(CONFIG_BLOCK) && |
3153 | io_bdev_nowait(file->f_inode->i_sb->s_bdev) && | |
4503b767 JA |
3154 | file->f_op != &io_uring_fops) |
3155 | return true; | |
3156 | return false; | |
3157 | } | |
2b188cc1 | 3158 | |
c5b85625 JA |
3159 | /* any ->read/write should understand O_NONBLOCK */ |
3160 | if (file->f_flags & O_NONBLOCK) | |
3161 | return true; | |
35645ac3 | 3162 | return file->f_mode & FMODE_NOWAIT; |
2b188cc1 | 3163 | } |
c5b85625 | 3164 | |
88459b50 PB |
3165 | /* |
3166 | * If we tracked the file through the SCM inflight mechanism, we could support | |
3167 | * any file. For now, just ensure that anything potentially problematic is done | |
3168 | * inline. | |
3169 | */ | |
3170 | static unsigned int io_file_get_flags(struct file *file) | |
3171 | { | |
3172 | umode_t mode = file_inode(file)->i_mode; | |
3173 | unsigned int res = 0; | |
af197f50 | 3174 | |
88459b50 PB |
3175 | if (S_ISREG(mode)) |
3176 | res |= FFS_ISREG; | |
3177 | if (__io_file_supports_nowait(file, mode)) | |
3178 | res |= FFS_NOWAIT; | |
3179 | return res; | |
2b188cc1 JA |
3180 | } |
3181 | ||
35645ac3 | 3182 | static inline bool io_file_supports_nowait(struct io_kiocb *req) |
7b29f92d | 3183 | { |
88459b50 | 3184 | return req->flags & REQ_F_SUPPORT_NOWAIT; |
7b29f92d JA |
3185 | } |
3186 | ||
b9a6b8f9 | 3187 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 3188 | { |
def596e9 | 3189 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 3190 | struct kiocb *kiocb = &req->rw.kiocb; |
75c668cd | 3191 | struct file *file = req->file; |
09bb8394 JA |
3192 | unsigned ioprio; |
3193 | int ret; | |
2b188cc1 | 3194 | |
88459b50 PB |
3195 | if (!io_req_ffs_set(req)) |
3196 | req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT; | |
491381ce | 3197 | |
2b188cc1 | 3198 | kiocb->ki_pos = READ_ONCE(sqe->off); |
5cb03d63 | 3199 | kiocb->ki_flags = iocb_flags(file); |
3e577dcd PB |
3200 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
3201 | if (unlikely(ret)) | |
3202 | return ret; | |
2b188cc1 | 3203 | |
5d329e12 JA |
3204 | /* |
3205 | * If the file is marked O_NONBLOCK, still allow retry for it if it | |
3206 | * supports async. Otherwise it's impossible to use O_NONBLOCK files | |
3207 | * reliably. If not, or it IOCB_NOWAIT is set, don't retry. | |
3208 | */ | |
3209 | if ((kiocb->ki_flags & IOCB_NOWAIT) || | |
35645ac3 | 3210 | ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req))) |
75c668cd PB |
3211 | req->flags |= REQ_F_NOWAIT; |
3212 | ||
def596e9 | 3213 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
5cb03d63 | 3214 | if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll) |
09bb8394 | 3215 | return -EOPNOTSUPP; |
2b188cc1 | 3216 | |
394918eb | 3217 | kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE; |
def596e9 | 3218 | kiocb->ki_complete = io_complete_rw_iopoll; |
65a6543d | 3219 | req->iopoll_completed = 0; |
def596e9 | 3220 | } else { |
09bb8394 JA |
3221 | if (kiocb->ki_flags & IOCB_HIPRI) |
3222 | return -EINVAL; | |
def596e9 JA |
3223 | kiocb->ki_complete = io_complete_rw; |
3224 | } | |
9adbd45d | 3225 | |
fb27274a PB |
3226 | ioprio = READ_ONCE(sqe->ioprio); |
3227 | if (ioprio) { | |
3228 | ret = ioprio_check_cap(ioprio); | |
3229 | if (ret) | |
3230 | return ret; | |
3231 | ||
3232 | kiocb->ki_ioprio = ioprio; | |
3233 | } else { | |
3234 | kiocb->ki_ioprio = get_current_ioprio(); | |
eae071c9 PB |
3235 | } |
3236 | ||
578c0ee2 | 3237 | req->imu = NULL; |
3529d8c2 JA |
3238 | req->rw.addr = READ_ONCE(sqe->addr); |
3239 | req->rw.len = READ_ONCE(sqe->len); | |
4f4eeba8 | 3240 | req->buf_index = READ_ONCE(sqe->buf_index); |
2b188cc1 | 3241 | return 0; |
2b188cc1 JA |
3242 | } |
3243 | ||
3244 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
3245 | { | |
3246 | switch (ret) { | |
3247 | case -EIOCBQUEUED: | |
3248 | break; | |
3249 | case -ERESTARTSYS: | |
3250 | case -ERESTARTNOINTR: | |
3251 | case -ERESTARTNOHAND: | |
3252 | case -ERESTART_RESTARTBLOCK: | |
3253 | /* | |
3254 | * We can't just restart the syscall, since previously | |
3255 | * submitted sqes may already be in progress. Just fail this | |
3256 | * IO with EINTR. | |
3257 | */ | |
3258 | ret = -EINTR; | |
df561f66 | 3259 | fallthrough; |
2b188cc1 | 3260 | default: |
6b19b766 | 3261 | kiocb->ki_complete(kiocb, ret); |
2b188cc1 JA |
3262 | } |
3263 | } | |
3264 | ||
b4aec400 | 3265 | static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req) |
d34e1e5b DY |
3266 | { |
3267 | struct kiocb *kiocb = &req->rw.kiocb; | |
b4aec400 | 3268 | bool is_stream = req->file->f_mode & FMODE_STREAM; |
d34e1e5b DY |
3269 | |
3270 | if (kiocb->ki_pos == -1) { | |
b4aec400 | 3271 | if (!is_stream) { |
d34e1e5b DY |
3272 | req->flags |= REQ_F_CUR_POS; |
3273 | kiocb->ki_pos = req->file->f_pos; | |
b4aec400 | 3274 | return &kiocb->ki_pos; |
d34e1e5b DY |
3275 | } else { |
3276 | kiocb->ki_pos = 0; | |
b4aec400 | 3277 | return NULL; |
d34e1e5b DY |
3278 | } |
3279 | } | |
b4aec400 | 3280 | return is_stream ? NULL : &kiocb->ki_pos; |
d34e1e5b DY |
3281 | } |
3282 | ||
2ea537ca | 3283 | static void kiocb_done(struct io_kiocb *req, ssize_t ret, |
889fca73 | 3284 | unsigned int issue_flags) |
ba816ad6 | 3285 | { |
e8c2bc1f | 3286 | struct io_async_rw *io = req->async_data; |
ba04291e | 3287 | |
227c0c96 | 3288 | /* add previously done IO, if any */ |
d886e185 | 3289 | if (req_has_async_data(req) && io->bytes_done > 0) { |
227c0c96 | 3290 | if (ret < 0) |
e8c2bc1f | 3291 | ret = io->bytes_done; |
227c0c96 | 3292 | else |
e8c2bc1f | 3293 | ret += io->bytes_done; |
227c0c96 JA |
3294 | } |
3295 | ||
ba04291e | 3296 | if (req->flags & REQ_F_CUR_POS) |
2ea537ca PB |
3297 | req->file->f_pos = req->rw.kiocb.ki_pos; |
3298 | if (ret >= 0 && (req->rw.kiocb.ki_complete == io_complete_rw)) | |
00f6e68b | 3299 | __io_complete_rw(req, ret, issue_flags); |
ba816ad6 | 3300 | else |
2ea537ca | 3301 | io_rw_done(&req->rw.kiocb, ret); |
97284637 | 3302 | |
b66ceaf3 | 3303 | if (req->flags & REQ_F_REISSUE) { |
97284637 | 3304 | req->flags &= ~REQ_F_REISSUE; |
b91ef187 | 3305 | if (io_resubmit_prep(req)) |
773af691 | 3306 | io_req_task_queue_reissue(req); |
b91ef187 PB |
3307 | else |
3308 | io_req_task_queue_fail(req, ret); | |
97284637 | 3309 | } |
ba816ad6 JA |
3310 | } |
3311 | ||
eae071c9 PB |
3312 | static int __io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter, |
3313 | struct io_mapped_ubuf *imu) | |
edafccee | 3314 | { |
9adbd45d | 3315 | size_t len = req->rw.len; |
75769e3f | 3316 | u64 buf_end, buf_addr = req->rw.addr; |
edafccee | 3317 | size_t offset; |
edafccee | 3318 | |
75769e3f | 3319 | if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end))) |
edafccee JA |
3320 | return -EFAULT; |
3321 | /* not inside the mapped region */ | |
4751f53d | 3322 | if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end)) |
edafccee JA |
3323 | return -EFAULT; |
3324 | ||
3325 | /* | |
3326 | * May not be a start of buffer, set size appropriately | |
3327 | * and advance us to the beginning. | |
3328 | */ | |
3329 | offset = buf_addr - imu->ubuf; | |
3330 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
3331 | |
3332 | if (offset) { | |
3333 | /* | |
3334 | * Don't use iov_iter_advance() here, as it's really slow for | |
3335 | * using the latter parts of a big fixed buffer - it iterates | |
3336 | * over each segment manually. We can cheat a bit here, because | |
3337 | * we know that: | |
3338 | * | |
3339 | * 1) it's a BVEC iter, we set it up | |
3340 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
3341 | * first and last bvec | |
3342 | * | |
3343 | * So just find our index, and adjust the iterator afterwards. | |
3344 | * If the offset is within the first bvec (or the whole first | |
3345 | * bvec, just use iov_iter_advance(). This makes it easier | |
3346 | * since we can just skip the first segment, which may not | |
3347 | * be PAGE_SIZE aligned. | |
3348 | */ | |
3349 | const struct bio_vec *bvec = imu->bvec; | |
3350 | ||
3351 | if (offset <= bvec->bv_len) { | |
3352 | iov_iter_advance(iter, offset); | |
3353 | } else { | |
3354 | unsigned long seg_skip; | |
3355 | ||
3356 | /* skip first vec */ | |
3357 | offset -= bvec->bv_len; | |
3358 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
3359 | ||
3360 | iter->bvec = bvec + seg_skip; | |
3361 | iter->nr_segs -= seg_skip; | |
99c79f66 | 3362 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 3363 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
3364 | } |
3365 | } | |
3366 | ||
847595de | 3367 | return 0; |
edafccee JA |
3368 | } |
3369 | ||
eae071c9 PB |
3370 | static int io_import_fixed(struct io_kiocb *req, int rw, struct iov_iter *iter) |
3371 | { | |
eae071c9 PB |
3372 | struct io_mapped_ubuf *imu = req->imu; |
3373 | u16 index, buf_index = req->buf_index; | |
3374 | ||
3375 | if (likely(!imu)) { | |
578c0ee2 PB |
3376 | struct io_ring_ctx *ctx = req->ctx; |
3377 | ||
eae071c9 PB |
3378 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
3379 | return -EFAULT; | |
578c0ee2 | 3380 | io_req_set_rsrc_node(req, ctx); |
eae071c9 PB |
3381 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
3382 | imu = READ_ONCE(ctx->user_bufs[index]); | |
3383 | req->imu = imu; | |
3384 | } | |
3385 | return __io_import_fixed(req, rw, iter, imu); | |
3386 | } | |
3387 | ||
bcda7baa JA |
3388 | static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
3389 | { | |
3390 | if (needs_lock) | |
3391 | mutex_unlock(&ctx->uring_lock); | |
3392 | } | |
3393 | ||
3394 | static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) | |
3395 | { | |
3396 | /* | |
3397 | * "Normal" inline submissions always hold the uring_lock, since we | |
3398 | * grab it from the system call. Same is true for the SQPOLL offload. | |
3399 | * The only exception is when we've detached the request and issue it | |
3400 | * from an async worker thread, grab the lock for that case. | |
3401 | */ | |
3402 | if (needs_lock) | |
3403 | mutex_lock(&ctx->uring_lock); | |
3404 | } | |
3405 | ||
dbc7d452 JA |
3406 | static void io_buffer_add_list(struct io_ring_ctx *ctx, |
3407 | struct io_buffer_list *bl, unsigned int bgid) | |
3408 | { | |
3409 | struct list_head *list; | |
3410 | ||
3411 | list = &ctx->io_buffers[hash_32(bgid, IO_BUFFERS_HASH_BITS)]; | |
3412 | INIT_LIST_HEAD(&bl->buf_list); | |
3413 | bl->bgid = bgid; | |
3414 | list_add(&bl->list, list); | |
3415 | } | |
3416 | ||
bcda7baa | 3417 | static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3418 | int bgid, unsigned int issue_flags) |
bcda7baa | 3419 | { |
30d51dd4 | 3420 | struct io_buffer *kbuf = req->kbuf; |
3b44b371 | 3421 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
dbc7d452 JA |
3422 | struct io_ring_ctx *ctx = req->ctx; |
3423 | struct io_buffer_list *bl; | |
bcda7baa JA |
3424 | |
3425 | if (req->flags & REQ_F_BUFFER_SELECTED) | |
3426 | return kbuf; | |
3427 | ||
dbc7d452 | 3428 | io_ring_submit_lock(ctx, needs_lock); |
bcda7baa | 3429 | |
dbc7d452 | 3430 | lockdep_assert_held(&ctx->uring_lock); |
bcda7baa | 3431 | |
dbc7d452 JA |
3432 | bl = io_buffer_get_list(ctx, bgid); |
3433 | if (bl && !list_empty(&bl->buf_list)) { | |
3434 | kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list); | |
3435 | list_del(&kbuf->list); | |
bcda7baa JA |
3436 | if (*len > kbuf->len) |
3437 | *len = kbuf->len; | |
30d51dd4 PB |
3438 | req->flags |= REQ_F_BUFFER_SELECTED; |
3439 | req->kbuf = kbuf; | |
bcda7baa JA |
3440 | } else { |
3441 | kbuf = ERR_PTR(-ENOBUFS); | |
3442 | } | |
3443 | ||
3444 | io_ring_submit_unlock(req->ctx, needs_lock); | |
bcda7baa JA |
3445 | return kbuf; |
3446 | } | |
3447 | ||
4d954c25 | 3448 | static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
51aac424 | 3449 | unsigned int issue_flags) |
4d954c25 JA |
3450 | { |
3451 | struct io_buffer *kbuf; | |
4f4eeba8 | 3452 | u16 bgid; |
4d954c25 | 3453 | |
4f4eeba8 | 3454 | bgid = req->buf_index; |
51aac424 | 3455 | kbuf = io_buffer_select(req, len, bgid, issue_flags); |
4d954c25 JA |
3456 | if (IS_ERR(kbuf)) |
3457 | return kbuf; | |
4d954c25 JA |
3458 | return u64_to_user_ptr(kbuf->addr); |
3459 | } | |
3460 | ||
3461 | #ifdef CONFIG_COMPAT | |
3462 | static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3463 | unsigned int issue_flags) |
4d954c25 JA |
3464 | { |
3465 | struct compat_iovec __user *uiov; | |
3466 | compat_ssize_t clen; | |
3467 | void __user *buf; | |
3468 | ssize_t len; | |
3469 | ||
3470 | uiov = u64_to_user_ptr(req->rw.addr); | |
3471 | if (!access_ok(uiov, sizeof(*uiov))) | |
3472 | return -EFAULT; | |
3473 | if (__get_user(clen, &uiov->iov_len)) | |
3474 | return -EFAULT; | |
3475 | if (clen < 0) | |
3476 | return -EINVAL; | |
3477 | ||
3478 | len = clen; | |
51aac424 | 3479 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3480 | if (IS_ERR(buf)) |
3481 | return PTR_ERR(buf); | |
3482 | iov[0].iov_base = buf; | |
3483 | iov[0].iov_len = (compat_size_t) len; | |
3484 | return 0; | |
3485 | } | |
3486 | #endif | |
3487 | ||
3488 | static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3489 | unsigned int issue_flags) |
4d954c25 JA |
3490 | { |
3491 | struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); | |
3492 | void __user *buf; | |
3493 | ssize_t len; | |
3494 | ||
3495 | if (copy_from_user(iov, uiov, sizeof(*uiov))) | |
3496 | return -EFAULT; | |
3497 | ||
3498 | len = iov[0].iov_len; | |
3499 | if (len < 0) | |
3500 | return -EINVAL; | |
51aac424 | 3501 | buf = io_rw_buffer_select(req, &len, issue_flags); |
4d954c25 JA |
3502 | if (IS_ERR(buf)) |
3503 | return PTR_ERR(buf); | |
3504 | iov[0].iov_base = buf; | |
3505 | iov[0].iov_len = len; | |
3506 | return 0; | |
3507 | } | |
3508 | ||
3509 | static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, | |
51aac424 | 3510 | unsigned int issue_flags) |
4d954c25 | 3511 | { |
dddb3e26 | 3512 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
30d51dd4 | 3513 | struct io_buffer *kbuf = req->kbuf; |
dddb3e26 | 3514 | |
dddb3e26 JA |
3515 | iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
3516 | iov[0].iov_len = kbuf->len; | |
4d954c25 | 3517 | return 0; |
dddb3e26 | 3518 | } |
dd201662 | 3519 | if (req->rw.len != 1) |
4d954c25 JA |
3520 | return -EINVAL; |
3521 | ||
3522 | #ifdef CONFIG_COMPAT | |
3523 | if (req->ctx->compat) | |
51aac424 | 3524 | return io_compat_import(req, iov, issue_flags); |
4d954c25 JA |
3525 | #endif |
3526 | ||
51aac424 | 3527 | return __io_iov_buffer_select(req, iov, issue_flags); |
4d954c25 JA |
3528 | } |
3529 | ||
caa8fe6e PB |
3530 | static struct iovec *__io_import_iovec(int rw, struct io_kiocb *req, |
3531 | struct io_rw_state *s, | |
3532 | unsigned int issue_flags) | |
2b188cc1 | 3533 | { |
5e49c973 | 3534 | struct iov_iter *iter = &s->iter; |
847595de | 3535 | u8 opcode = req->opcode; |
caa8fe6e | 3536 | struct iovec *iovec; |
d1d681b0 PB |
3537 | void __user *buf; |
3538 | size_t sqe_len; | |
4d954c25 | 3539 | ssize_t ret; |
edafccee | 3540 | |
f3251183 PB |
3541 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
3542 | ret = io_import_fixed(req, rw, iter); | |
3543 | if (ret) | |
3544 | return ERR_PTR(ret); | |
3545 | return NULL; | |
3546 | } | |
2b188cc1 | 3547 | |
bcda7baa | 3548 | /* buffer index only valid with fixed read/write, or buffer select */ |
d1d681b0 | 3549 | if (unlikely(req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT))) |
caa8fe6e | 3550 | return ERR_PTR(-EINVAL); |
9adbd45d | 3551 | |
d1d681b0 PB |
3552 | buf = u64_to_user_ptr(req->rw.addr); |
3553 | sqe_len = req->rw.len; | |
9adbd45d | 3554 | |
3a6820f2 | 3555 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
bcda7baa | 3556 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 3557 | buf = io_rw_buffer_select(req, &sqe_len, issue_flags); |
867a23ea | 3558 | if (IS_ERR(buf)) |
898df244 | 3559 | return ERR_CAST(buf); |
3f9d6441 | 3560 | req->rw.len = sqe_len; |
bcda7baa JA |
3561 | } |
3562 | ||
5e49c973 | 3563 | ret = import_single_range(rw, buf, sqe_len, s->fast_iov, iter); |
f3251183 PB |
3564 | if (ret) |
3565 | return ERR_PTR(ret); | |
3566 | return NULL; | |
3a6820f2 JA |
3567 | } |
3568 | ||
caa8fe6e | 3569 | iovec = s->fast_iov; |
4d954c25 | 3570 | if (req->flags & REQ_F_BUFFER_SELECT) { |
caa8fe6e | 3571 | ret = io_iov_buffer_select(req, iovec, issue_flags); |
f3251183 PB |
3572 | if (ret) |
3573 | return ERR_PTR(ret); | |
3574 | iov_iter_init(iter, rw, iovec, 1, iovec->iov_len); | |
3575 | return NULL; | |
4d954c25 JA |
3576 | } |
3577 | ||
caa8fe6e | 3578 | ret = __import_iovec(rw, buf, sqe_len, UIO_FASTIOV, &iovec, iter, |
89cd35c5 | 3579 | req->ctx->compat); |
caa8fe6e PB |
3580 | if (unlikely(ret < 0)) |
3581 | return ERR_PTR(ret); | |
3582 | return iovec; | |
2b188cc1 JA |
3583 | } |
3584 | ||
5e49c973 PB |
3585 | static inline int io_import_iovec(int rw, struct io_kiocb *req, |
3586 | struct iovec **iovec, struct io_rw_state *s, | |
3587 | unsigned int issue_flags) | |
3588 | { | |
caa8fe6e PB |
3589 | *iovec = __io_import_iovec(rw, req, s, issue_flags); |
3590 | if (unlikely(IS_ERR(*iovec))) | |
3591 | return PTR_ERR(*iovec); | |
5e49c973 | 3592 | |
5e49c973 | 3593 | iov_iter_save_state(&s->iter, &s->iter_state); |
caa8fe6e | 3594 | return 0; |
2b188cc1 JA |
3595 | } |
3596 | ||
0fef9483 JA |
3597 | static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb) |
3598 | { | |
5b09e37e | 3599 | return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos; |
0fef9483 JA |
3600 | } |
3601 | ||
31b51510 | 3602 | /* |
32960613 JA |
3603 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
3604 | * by looping over ->read() or ->write() manually. | |
31b51510 | 3605 | */ |
4017eb91 | 3606 | static ssize_t loop_rw_iter(int rw, struct io_kiocb *req, struct iov_iter *iter) |
32960613 | 3607 | { |
4017eb91 JA |
3608 | struct kiocb *kiocb = &req->rw.kiocb; |
3609 | struct file *file = req->file; | |
32960613 | 3610 | ssize_t ret = 0; |
af9c45ec | 3611 | loff_t *ppos; |
32960613 JA |
3612 | |
3613 | /* | |
3614 | * Don't support polled IO through this interface, and we can't | |
3615 | * support non-blocking either. For the latter, this just causes | |
3616 | * the kiocb to be handled from an async context. | |
3617 | */ | |
3618 | if (kiocb->ki_flags & IOCB_HIPRI) | |
3619 | return -EOPNOTSUPP; | |
35645ac3 PB |
3620 | if ((kiocb->ki_flags & IOCB_NOWAIT) && |
3621 | !(kiocb->ki_filp->f_flags & O_NONBLOCK)) | |
32960613 JA |
3622 | return -EAGAIN; |
3623 | ||
af9c45ec DY |
3624 | ppos = io_kiocb_ppos(kiocb); |
3625 | ||
32960613 | 3626 | while (iov_iter_count(iter)) { |
311ae9e1 | 3627 | struct iovec iovec; |
32960613 JA |
3628 | ssize_t nr; |
3629 | ||
311ae9e1 PB |
3630 | if (!iov_iter_is_bvec(iter)) { |
3631 | iovec = iov_iter_iovec(iter); | |
3632 | } else { | |
4017eb91 JA |
3633 | iovec.iov_base = u64_to_user_ptr(req->rw.addr); |
3634 | iovec.iov_len = req->rw.len; | |
311ae9e1 PB |
3635 | } |
3636 | ||
32960613 JA |
3637 | if (rw == READ) { |
3638 | nr = file->f_op->read(file, iovec.iov_base, | |
af9c45ec | 3639 | iovec.iov_len, ppos); |
32960613 JA |
3640 | } else { |
3641 | nr = file->f_op->write(file, iovec.iov_base, | |
af9c45ec | 3642 | iovec.iov_len, ppos); |
32960613 JA |
3643 | } |
3644 | ||
3645 | if (nr < 0) { | |
3646 | if (!ret) | |
3647 | ret = nr; | |
3648 | break; | |
3649 | } | |
5e929367 | 3650 | ret += nr; |
16c8d2df JA |
3651 | if (!iov_iter_is_bvec(iter)) { |
3652 | iov_iter_advance(iter, nr); | |
3653 | } else { | |
16c8d2df | 3654 | req->rw.addr += nr; |
5e929367 JA |
3655 | req->rw.len -= nr; |
3656 | if (!req->rw.len) | |
3657 | break; | |
16c8d2df | 3658 | } |
32960613 JA |
3659 | if (nr != iovec.iov_len) |
3660 | break; | |
32960613 JA |
3661 | } |
3662 | ||
3663 | return ret; | |
3664 | } | |
3665 | ||
ff6165b2 JA |
3666 | static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec, |
3667 | const struct iovec *fast_iov, struct iov_iter *iter) | |
f67676d1 | 3668 | { |
e8c2bc1f | 3669 | struct io_async_rw *rw = req->async_data; |
b64e3444 | 3670 | |
538941e2 | 3671 | memcpy(&rw->s.iter, iter, sizeof(*iter)); |
afb87658 | 3672 | rw->free_iovec = iovec; |
227c0c96 | 3673 | rw->bytes_done = 0; |
ff6165b2 | 3674 | /* can only be fixed buffers, no need to do anything */ |
9c3a205c | 3675 | if (iov_iter_is_bvec(iter)) |
ff6165b2 | 3676 | return; |
b64e3444 | 3677 | if (!iovec) { |
ff6165b2 JA |
3678 | unsigned iov_off = 0; |
3679 | ||
538941e2 | 3680 | rw->s.iter.iov = rw->s.fast_iov; |
ff6165b2 JA |
3681 | if (iter->iov != fast_iov) { |
3682 | iov_off = iter->iov - fast_iov; | |
538941e2 | 3683 | rw->s.iter.iov += iov_off; |
ff6165b2 | 3684 | } |
538941e2 PB |
3685 | if (rw->s.fast_iov != fast_iov) |
3686 | memcpy(rw->s.fast_iov + iov_off, fast_iov + iov_off, | |
45097dae | 3687 | sizeof(struct iovec) * iter->nr_segs); |
99bc4c38 PB |
3688 | } else { |
3689 | req->flags |= REQ_F_NEED_CLEANUP; | |
f67676d1 JA |
3690 | } |
3691 | } | |
3692 | ||
8d4af685 | 3693 | static inline bool io_alloc_async_data(struct io_kiocb *req) |
3d9932a8 | 3694 | { |
e8c2bc1f JA |
3695 | WARN_ON_ONCE(!io_op_defs[req->opcode].async_size); |
3696 | req->async_data = kmalloc(io_op_defs[req->opcode].async_size, GFP_KERNEL); | |
d886e185 PB |
3697 | if (req->async_data) { |
3698 | req->flags |= REQ_F_ASYNC_DATA; | |
3699 | return false; | |
3700 | } | |
3701 | return true; | |
3d9932a8 XW |
3702 | } |
3703 | ||
ff6165b2 | 3704 | static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
c88598a9 | 3705 | struct io_rw_state *s, bool force) |
b7bb4f7d | 3706 | { |
26f0505a | 3707 | if (!force && !io_op_defs[req->opcode].needs_async_setup) |
74566df3 | 3708 | return 0; |
d886e185 | 3709 | if (!req_has_async_data(req)) { |
cd658695 JA |
3710 | struct io_async_rw *iorw; |
3711 | ||
6cb78689 | 3712 | if (io_alloc_async_data(req)) { |
6bf985dc | 3713 | kfree(iovec); |
5d204bcf | 3714 | return -ENOMEM; |
6bf985dc | 3715 | } |
b7bb4f7d | 3716 | |
c88598a9 | 3717 | io_req_map_rw(req, iovec, s->fast_iov, &s->iter); |
cd658695 JA |
3718 | iorw = req->async_data; |
3719 | /* we've copied and mapped the iter, ensure state is saved */ | |
538941e2 | 3720 | iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state); |
5d204bcf | 3721 | } |
b7bb4f7d | 3722 | return 0; |
f67676d1 JA |
3723 | } |
3724 | ||
73debe68 | 3725 | static inline int io_rw_prep_async(struct io_kiocb *req, int rw) |
c3e330a4 | 3726 | { |
e8c2bc1f | 3727 | struct io_async_rw *iorw = req->async_data; |
5e49c973 | 3728 | struct iovec *iov; |
847595de | 3729 | int ret; |
c3e330a4 | 3730 | |
51aac424 | 3731 | /* submission path, ->uring_lock should already be taken */ |
3b44b371 | 3732 | ret = io_import_iovec(rw, req, &iov, &iorw->s, 0); |
c3e330a4 PB |
3733 | if (unlikely(ret < 0)) |
3734 | return ret; | |
3735 | ||
ab0b196c PB |
3736 | iorw->bytes_done = 0; |
3737 | iorw->free_iovec = iov; | |
3738 | if (iov) | |
3739 | req->flags |= REQ_F_NEED_CLEANUP; | |
c3e330a4 PB |
3740 | return 0; |
3741 | } | |
3742 | ||
73debe68 | 3743 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3744 | { |
3529d8c2 JA |
3745 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
3746 | return -EBADF; | |
b9a6b8f9 | 3747 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3748 | } |
3749 | ||
c1dd91d1 | 3750 | /* |
ffdc8dab | 3751 | * This is our waitqueue callback handler, registered through __folio_lock_async() |
c1dd91d1 JA |
3752 | * when we initially tried to do the IO with the iocb armed our waitqueue. |
3753 | * This gets called when the page is unlocked, and we generally expect that to | |
3754 | * happen when the page IO is completed and the page is now uptodate. This will | |
3755 | * queue a task_work based retry of the operation, attempting to copy the data | |
3756 | * again. If the latter fails because the page was NOT uptodate, then we will | |
3757 | * do a thread based blocking retry of the operation. That's the unexpected | |
3758 | * slow path. | |
3759 | */ | |
bcf5a063 JA |
3760 | static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode, |
3761 | int sync, void *arg) | |
3762 | { | |
3763 | struct wait_page_queue *wpq; | |
3764 | struct io_kiocb *req = wait->private; | |
bcf5a063 | 3765 | struct wait_page_key *key = arg; |
bcf5a063 JA |
3766 | |
3767 | wpq = container_of(wait, struct wait_page_queue, wait); | |
3768 | ||
cdc8fcb4 LT |
3769 | if (!wake_page_match(wpq, key)) |
3770 | return 0; | |
3771 | ||
c8d317aa | 3772 | req->rw.kiocb.ki_flags &= ~IOCB_WAITQ; |
bcf5a063 | 3773 | list_del_init(&wait->entry); |
921b9054 | 3774 | io_req_task_queue(req); |
bcf5a063 JA |
3775 | return 1; |
3776 | } | |
3777 | ||
c1dd91d1 JA |
3778 | /* |
3779 | * This controls whether a given IO request should be armed for async page | |
3780 | * based retry. If we return false here, the request is handed to the async | |
3781 | * worker threads for retry. If we're doing buffered reads on a regular file, | |
3782 | * we prepare a private wait_page_queue entry and retry the operation. This | |
3783 | * will either succeed because the page is now uptodate and unlocked, or it | |
3784 | * will register a callback when the page is unlocked at IO completion. Through | |
3785 | * that callback, io_uring uses task_work to setup a retry of the operation. | |
3786 | * That retry will attempt the buffered read again. The retry will generally | |
3787 | * succeed, or in rare cases where it fails, we then fall back to using the | |
3788 | * async worker threads for a blocking retry. | |
3789 | */ | |
227c0c96 | 3790 | static bool io_rw_should_retry(struct io_kiocb *req) |
f67676d1 | 3791 | { |
e8c2bc1f JA |
3792 | struct io_async_rw *rw = req->async_data; |
3793 | struct wait_page_queue *wait = &rw->wpq; | |
bcf5a063 | 3794 | struct kiocb *kiocb = &req->rw.kiocb; |
f67676d1 | 3795 | |
bcf5a063 JA |
3796 | /* never retry for NOWAIT, we just complete with -EAGAIN */ |
3797 | if (req->flags & REQ_F_NOWAIT) | |
3798 | return false; | |
f67676d1 | 3799 | |
227c0c96 | 3800 | /* Only for buffered IO */ |
3b2a4439 | 3801 | if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI)) |
bcf5a063 | 3802 | return false; |
3b2a4439 | 3803 | |
bcf5a063 JA |
3804 | /* |
3805 | * just use poll if we can, and don't attempt if the fs doesn't | |
3806 | * support callback based unlocks | |
3807 | */ | |
3808 | if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC)) | |
3809 | return false; | |
f67676d1 | 3810 | |
3b2a4439 JA |
3811 | wait->wait.func = io_async_buf_func; |
3812 | wait->wait.private = req; | |
3813 | wait->wait.flags = 0; | |
3814 | INIT_LIST_HEAD(&wait->wait.entry); | |
3815 | kiocb->ki_flags |= IOCB_WAITQ; | |
c8d317aa | 3816 | kiocb->ki_flags &= ~IOCB_NOWAIT; |
3b2a4439 | 3817 | kiocb->ki_waitq = wait; |
3b2a4439 | 3818 | return true; |
bcf5a063 JA |
3819 | } |
3820 | ||
aeab9506 | 3821 | static inline int io_iter_do_read(struct io_kiocb *req, struct iov_iter *iter) |
bcf5a063 | 3822 | { |
607b6fb8 | 3823 | if (likely(req->file->f_op->read_iter)) |
bcf5a063 | 3824 | return call_read_iter(req->file, &req->rw.kiocb, iter); |
2dd2111d | 3825 | else if (req->file->f_op->read) |
4017eb91 | 3826 | return loop_rw_iter(READ, req, iter); |
2dd2111d GH |
3827 | else |
3828 | return -EINVAL; | |
f67676d1 JA |
3829 | } |
3830 | ||
7db30437 ML |
3831 | static bool need_read_all(struct io_kiocb *req) |
3832 | { | |
3833 | return req->flags & REQ_F_ISREG || | |
3834 | S_ISBLK(file_inode(req->file)->i_mode); | |
3835 | } | |
3836 | ||
889fca73 | 3837 | static int io_read(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3838 | { |
607b6fb8 | 3839 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3840 | struct iovec *iovec; |
9adbd45d | 3841 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3842 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
d886e185 | 3843 | struct io_async_rw *rw; |
cd658695 | 3844 | ssize_t ret, ret2; |
b4aec400 | 3845 | loff_t *ppos; |
ff6165b2 | 3846 | |
607b6fb8 PB |
3847 | if (!req_has_async_data(req)) { |
3848 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3849 | if (unlikely(ret < 0)) | |
3850 | return ret; | |
3851 | } else { | |
2be2eb02 JA |
3852 | /* |
3853 | * Safe and required to re-import if we're using provided | |
3854 | * buffers, as we dropped the selected one before retry. | |
3855 | */ | |
3856 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
3857 | ret = io_import_iovec(READ, req, &iovec, s, issue_flags); | |
3858 | if (unlikely(ret < 0)) | |
3859 | return ret; | |
3860 | } | |
3861 | ||
d886e185 | 3862 | rw = req->async_data; |
c88598a9 | 3863 | s = &rw->s; |
cd658695 JA |
3864 | /* |
3865 | * We come here from an earlier attempt, restore our state to | |
3866 | * match in case it doesn't. It's cheap enough that we don't | |
3867 | * need to make this conditional. | |
3868 | */ | |
c88598a9 | 3869 | iov_iter_restore(&s->iter, &s->iter_state); |
2846c481 | 3870 | iovec = NULL; |
2846c481 | 3871 | } |
c88598a9 | 3872 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 3873 | |
607b6fb8 PB |
3874 | if (force_nonblock) { |
3875 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 3876 | if (unlikely(!io_file_supports_nowait(req))) { |
607b6fb8 PB |
3877 | ret = io_setup_async_rw(req, iovec, s, true); |
3878 | return ret ?: -EAGAIN; | |
3879 | } | |
a88fc400 | 3880 | kiocb->ki_flags |= IOCB_NOWAIT; |
607b6fb8 PB |
3881 | } else { |
3882 | /* Ensure we clear previously set non-block flag */ | |
3883 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
6713e7a6 | 3884 | } |
9e645e11 | 3885 | |
b4aec400 | 3886 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 3887 | |
b4aec400 | 3888 | ret = rw_verify_area(READ, req->file, ppos, req->result); |
5ea5dd45 PB |
3889 | if (unlikely(ret)) { |
3890 | kfree(iovec); | |
3891 | return ret; | |
3892 | } | |
2b188cc1 | 3893 | |
c88598a9 | 3894 | ret = io_iter_do_read(req, &s->iter); |
32960613 | 3895 | |
230d50d4 | 3896 | if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) { |
6ad7f233 | 3897 | req->flags &= ~REQ_F_REISSUE; |
9af177ee JA |
3898 | /* if we can poll, just do that */ |
3899 | if (req->opcode == IORING_OP_READ && file_can_poll(req->file)) | |
3900 | return -EAGAIN; | |
eefdf30f JA |
3901 | /* IOPOLL retry should happen for io-wq threads */ |
3902 | if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
f91daf56 | 3903 | goto done; |
75c668cd PB |
3904 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
3905 | if (req->flags & REQ_F_NOWAIT) | |
355afaeb | 3906 | goto done; |
f38c7e3a | 3907 | ret = 0; |
230d50d4 JA |
3908 | } else if (ret == -EIOCBQUEUED) { |
3909 | goto out_free; | |
f80a50a6 | 3910 | } else if (ret == req->result || ret <= 0 || !force_nonblock || |
7db30437 | 3911 | (req->flags & REQ_F_NOWAIT) || !need_read_all(req)) { |
7335e3bf | 3912 | /* read all, failed, already did sync or don't want to retry */ |
00d23d51 | 3913 | goto done; |
227c0c96 JA |
3914 | } |
3915 | ||
cd658695 JA |
3916 | /* |
3917 | * Don't depend on the iter state matching what was consumed, or being | |
3918 | * untouched in case of error. Restore it and we'll advance it | |
3919 | * manually if we need to. | |
3920 | */ | |
c88598a9 | 3921 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3922 | |
c88598a9 | 3923 | ret2 = io_setup_async_rw(req, iovec, s, true); |
6bf985dc PB |
3924 | if (ret2) |
3925 | return ret2; | |
3926 | ||
fe1cdd55 | 3927 | iovec = NULL; |
e8c2bc1f | 3928 | rw = req->async_data; |
c88598a9 | 3929 | s = &rw->s; |
cd658695 JA |
3930 | /* |
3931 | * Now use our persistent iterator and state, if we aren't already. | |
3932 | * We've restored and mapped the iter to match. | |
3933 | */ | |
227c0c96 | 3934 | |
b23df91b | 3935 | do { |
cd658695 JA |
3936 | /* |
3937 | * We end up here because of a partial read, either from | |
3938 | * above or inside this loop. Advance the iter by the bytes | |
3939 | * that were consumed. | |
3940 | */ | |
c88598a9 PB |
3941 | iov_iter_advance(&s->iter, ret); |
3942 | if (!iov_iter_count(&s->iter)) | |
cd658695 | 3943 | break; |
b23df91b | 3944 | rw->bytes_done += ret; |
c88598a9 | 3945 | iov_iter_save_state(&s->iter, &s->iter_state); |
cd658695 | 3946 | |
b23df91b PB |
3947 | /* if we can retry, do so with the callbacks armed */ |
3948 | if (!io_rw_should_retry(req)) { | |
3949 | kiocb->ki_flags &= ~IOCB_WAITQ; | |
3950 | return -EAGAIN; | |
3951 | } | |
3952 | ||
3953 | /* | |
3954 | * Now retry read with the IOCB_WAITQ parts set in the iocb. If | |
3955 | * we get -EIOCBQUEUED, then we'll get a notification when the | |
3956 | * desired page gets unlocked. We can also get a partial read | |
3957 | * here, and if we do, then just retry at the new offset. | |
3958 | */ | |
c88598a9 | 3959 | ret = io_iter_do_read(req, &s->iter); |
b23df91b PB |
3960 | if (ret == -EIOCBQUEUED) |
3961 | return 0; | |
227c0c96 | 3962 | /* we got some bytes, but not all. retry. */ |
b5b0ecb7 | 3963 | kiocb->ki_flags &= ~IOCB_WAITQ; |
c88598a9 | 3964 | iov_iter_restore(&s->iter, &s->iter_state); |
cd658695 | 3965 | } while (ret > 0); |
227c0c96 | 3966 | done: |
2ea537ca | 3967 | kiocb_done(req, ret, issue_flags); |
fe1cdd55 PB |
3968 | out_free: |
3969 | /* it's faster to check here then delegate to kfree */ | |
3970 | if (iovec) | |
3971 | kfree(iovec); | |
5ea5dd45 | 3972 | return 0; |
2b188cc1 JA |
3973 | } |
3974 | ||
73debe68 | 3975 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 3976 | { |
3529d8c2 JA |
3977 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
3978 | return -EBADF; | |
b9a6b8f9 | 3979 | return io_prep_rw(req, sqe); |
f67676d1 JA |
3980 | } |
3981 | ||
889fca73 | 3982 | static int io_write(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 3983 | { |
607b6fb8 | 3984 | struct io_rw_state __s, *s = &__s; |
c88598a9 | 3985 | struct iovec *iovec; |
9adbd45d | 3986 | struct kiocb *kiocb = &req->rw.kiocb; |
45d189c6 | 3987 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
cd658695 | 3988 | ssize_t ret, ret2; |
b4aec400 | 3989 | loff_t *ppos; |
2b188cc1 | 3990 | |
607b6fb8 | 3991 | if (!req_has_async_data(req)) { |
5e49c973 PB |
3992 | ret = io_import_iovec(WRITE, req, &iovec, s, issue_flags); |
3993 | if (unlikely(ret < 0)) | |
2846c481 | 3994 | return ret; |
607b6fb8 PB |
3995 | } else { |
3996 | struct io_async_rw *rw = req->async_data; | |
3997 | ||
3998 | s = &rw->s; | |
3999 | iov_iter_restore(&s->iter, &s->iter_state); | |
2846c481 | 4000 | iovec = NULL; |
2846c481 | 4001 | } |
c88598a9 | 4002 | req->result = iov_iter_count(&s->iter); |
2b188cc1 | 4003 | |
607b6fb8 PB |
4004 | if (force_nonblock) { |
4005 | /* If the file doesn't support async, just async punt */ | |
35645ac3 | 4006 | if (unlikely(!io_file_supports_nowait(req))) |
607b6fb8 | 4007 | goto copy_iov; |
fd6c2e4c | 4008 | |
607b6fb8 PB |
4009 | /* file path doesn't support NOWAIT for non-direct_IO */ |
4010 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
4011 | (req->flags & REQ_F_ISREG)) | |
4012 | goto copy_iov; | |
31b51510 | 4013 | |
607b6fb8 PB |
4014 | kiocb->ki_flags |= IOCB_NOWAIT; |
4015 | } else { | |
4016 | /* Ensure we clear previously set non-block flag */ | |
4017 | kiocb->ki_flags &= ~IOCB_NOWAIT; | |
4018 | } | |
31b51510 | 4019 | |
b4aec400 | 4020 | ppos = io_kiocb_update_pos(req); |
d34e1e5b | 4021 | |
b4aec400 | 4022 | ret = rw_verify_area(WRITE, req->file, ppos, req->result); |
fa15bafb PB |
4023 | if (unlikely(ret)) |
4024 | goto out_free; | |
4ed734b0 | 4025 | |
fa15bafb PB |
4026 | /* |
4027 | * Open-code file_start_write here to grab freeze protection, | |
4028 | * which will be released by another thread in | |
4029 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
4030 | * released so that it doesn't complain about the held lock when | |
4031 | * we return to userspace. | |
4032 | */ | |
4033 | if (req->flags & REQ_F_ISREG) { | |
8a3c84b6 | 4034 | sb_start_write(file_inode(req->file)->i_sb); |
fa15bafb PB |
4035 | __sb_writers_release(file_inode(req->file)->i_sb, |
4036 | SB_FREEZE_WRITE); | |
4037 | } | |
4038 | kiocb->ki_flags |= IOCB_WRITE; | |
4ed734b0 | 4039 | |
35645ac3 | 4040 | if (likely(req->file->f_op->write_iter)) |
c88598a9 | 4041 | ret2 = call_write_iter(req->file, kiocb, &s->iter); |
2dd2111d | 4042 | else if (req->file->f_op->write) |
c88598a9 | 4043 | ret2 = loop_rw_iter(WRITE, req, &s->iter); |
2dd2111d GH |
4044 | else |
4045 | ret2 = -EINVAL; | |
4ed734b0 | 4046 | |
6ad7f233 PB |
4047 | if (req->flags & REQ_F_REISSUE) { |
4048 | req->flags &= ~REQ_F_REISSUE; | |
230d50d4 | 4049 | ret2 = -EAGAIN; |
6ad7f233 | 4050 | } |
230d50d4 | 4051 | |
fa15bafb PB |
4052 | /* |
4053 | * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just | |
4054 | * retry them without IOCB_NOWAIT. | |
4055 | */ | |
4056 | if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) | |
4057 | ret2 = -EAGAIN; | |
75c668cd PB |
4058 | /* no retry on NONBLOCK nor RWF_NOWAIT */ |
4059 | if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) | |
355afaeb | 4060 | goto done; |
fa15bafb | 4061 | if (!force_nonblock || ret2 != -EAGAIN) { |
eefdf30f | 4062 | /* IOPOLL retry should happen for io-wq threads */ |
b10841c9 | 4063 | if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL)) |
eefdf30f | 4064 | goto copy_iov; |
355afaeb | 4065 | done: |
2ea537ca | 4066 | kiocb_done(req, ret2, issue_flags); |
fa15bafb | 4067 | } else { |
f67676d1 | 4068 | copy_iov: |
c88598a9 PB |
4069 | iov_iter_restore(&s->iter, &s->iter_state); |
4070 | ret = io_setup_async_rw(req, iovec, s, false); | |
6bf985dc | 4071 | return ret ?: -EAGAIN; |
2b188cc1 | 4072 | } |
31b51510 | 4073 | out_free: |
f261c168 | 4074 | /* it's reportedly faster than delegating the null check to kfree() */ |
252917c3 | 4075 | if (iovec) |
6f2cc166 | 4076 | kfree(iovec); |
2b188cc1 JA |
4077 | return ret; |
4078 | } | |
4079 | ||
80a261fd JA |
4080 | static int io_renameat_prep(struct io_kiocb *req, |
4081 | const struct io_uring_sqe *sqe) | |
4082 | { | |
4083 | struct io_rename *ren = &req->rename; | |
4084 | const char __user *oldf, *newf; | |
4085 | ||
ed7eb259 JA |
4086 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4087 | return -EINVAL; | |
26578cda | 4088 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
ed7eb259 | 4089 | return -EINVAL; |
80a261fd JA |
4090 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4091 | return -EBADF; | |
4092 | ||
4093 | ren->old_dfd = READ_ONCE(sqe->fd); | |
4094 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4095 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4096 | ren->new_dfd = READ_ONCE(sqe->len); | |
4097 | ren->flags = READ_ONCE(sqe->rename_flags); | |
4098 | ||
4099 | ren->oldpath = getname(oldf); | |
4100 | if (IS_ERR(ren->oldpath)) | |
4101 | return PTR_ERR(ren->oldpath); | |
4102 | ||
4103 | ren->newpath = getname(newf); | |
4104 | if (IS_ERR(ren->newpath)) { | |
4105 | putname(ren->oldpath); | |
4106 | return PTR_ERR(ren->newpath); | |
4107 | } | |
4108 | ||
4109 | req->flags |= REQ_F_NEED_CLEANUP; | |
4110 | return 0; | |
4111 | } | |
4112 | ||
45d189c6 | 4113 | static int io_renameat(struct io_kiocb *req, unsigned int issue_flags) |
80a261fd JA |
4114 | { |
4115 | struct io_rename *ren = &req->rename; | |
4116 | int ret; | |
4117 | ||
45d189c6 | 4118 | if (issue_flags & IO_URING_F_NONBLOCK) |
80a261fd JA |
4119 | return -EAGAIN; |
4120 | ||
4121 | ret = do_renameat2(ren->old_dfd, ren->oldpath, ren->new_dfd, | |
4122 | ren->newpath, ren->flags); | |
4123 | ||
4124 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4125 | if (ret < 0) | |
93d2bcd2 | 4126 | req_set_fail(req); |
80a261fd JA |
4127 | io_req_complete(req, ret); |
4128 | return 0; | |
4129 | } | |
4130 | ||
14a1143b JA |
4131 | static int io_unlinkat_prep(struct io_kiocb *req, |
4132 | const struct io_uring_sqe *sqe) | |
4133 | { | |
4134 | struct io_unlink *un = &req->unlink; | |
4135 | const char __user *fname; | |
4136 | ||
22634bc5 JA |
4137 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4138 | return -EINVAL; | |
26578cda PB |
4139 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
4140 | sqe->splice_fd_in) | |
22634bc5 | 4141 | return -EINVAL; |
14a1143b JA |
4142 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
4143 | return -EBADF; | |
4144 | ||
4145 | un->dfd = READ_ONCE(sqe->fd); | |
4146 | ||
4147 | un->flags = READ_ONCE(sqe->unlink_flags); | |
4148 | if (un->flags & ~AT_REMOVEDIR) | |
4149 | return -EINVAL; | |
4150 | ||
4151 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4152 | un->filename = getname(fname); | |
4153 | if (IS_ERR(un->filename)) | |
4154 | return PTR_ERR(un->filename); | |
4155 | ||
4156 | req->flags |= REQ_F_NEED_CLEANUP; | |
4157 | return 0; | |
4158 | } | |
4159 | ||
45d189c6 | 4160 | static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags) |
14a1143b JA |
4161 | { |
4162 | struct io_unlink *un = &req->unlink; | |
4163 | int ret; | |
4164 | ||
45d189c6 | 4165 | if (issue_flags & IO_URING_F_NONBLOCK) |
14a1143b JA |
4166 | return -EAGAIN; |
4167 | ||
4168 | if (un->flags & AT_REMOVEDIR) | |
4169 | ret = do_rmdir(un->dfd, un->filename); | |
4170 | else | |
4171 | ret = do_unlinkat(un->dfd, un->filename); | |
4172 | ||
4173 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4174 | if (ret < 0) | |
93d2bcd2 | 4175 | req_set_fail(req); |
14a1143b JA |
4176 | io_req_complete(req, ret); |
4177 | return 0; | |
4178 | } | |
4179 | ||
e34a02dc DK |
4180 | static int io_mkdirat_prep(struct io_kiocb *req, |
4181 | const struct io_uring_sqe *sqe) | |
4182 | { | |
4183 | struct io_mkdir *mkd = &req->mkdir; | |
4184 | const char __user *fname; | |
4185 | ||
4186 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4187 | return -EINVAL; | |
4188 | if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index || | |
4189 | sqe->splice_fd_in) | |
4190 | return -EINVAL; | |
4191 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4192 | return -EBADF; | |
4193 | ||
4194 | mkd->dfd = READ_ONCE(sqe->fd); | |
4195 | mkd->mode = READ_ONCE(sqe->len); | |
4196 | ||
4197 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4198 | mkd->filename = getname(fname); | |
4199 | if (IS_ERR(mkd->filename)) | |
4200 | return PTR_ERR(mkd->filename); | |
4201 | ||
4202 | req->flags |= REQ_F_NEED_CLEANUP; | |
4203 | return 0; | |
4204 | } | |
4205 | ||
04f34081 | 4206 | static int io_mkdirat(struct io_kiocb *req, unsigned int issue_flags) |
e34a02dc DK |
4207 | { |
4208 | struct io_mkdir *mkd = &req->mkdir; | |
4209 | int ret; | |
4210 | ||
4211 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4212 | return -EAGAIN; | |
4213 | ||
4214 | ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode); | |
4215 | ||
4216 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4217 | if (ret < 0) | |
4218 | req_set_fail(req); | |
4219 | io_req_complete(req, ret); | |
4220 | return 0; | |
4221 | } | |
4222 | ||
7a8721f8 DK |
4223 | static int io_symlinkat_prep(struct io_kiocb *req, |
4224 | const struct io_uring_sqe *sqe) | |
4225 | { | |
4226 | struct io_symlink *sl = &req->symlink; | |
4227 | const char __user *oldpath, *newpath; | |
4228 | ||
4229 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4230 | return -EINVAL; | |
4231 | if (sqe->ioprio || sqe->len || sqe->rw_flags || sqe->buf_index || | |
4232 | sqe->splice_fd_in) | |
4233 | return -EINVAL; | |
4234 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4235 | return -EBADF; | |
4236 | ||
4237 | sl->new_dfd = READ_ONCE(sqe->fd); | |
4238 | oldpath = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4239 | newpath = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4240 | ||
4241 | sl->oldpath = getname(oldpath); | |
4242 | if (IS_ERR(sl->oldpath)) | |
4243 | return PTR_ERR(sl->oldpath); | |
4244 | ||
4245 | sl->newpath = getname(newpath); | |
4246 | if (IS_ERR(sl->newpath)) { | |
4247 | putname(sl->oldpath); | |
4248 | return PTR_ERR(sl->newpath); | |
4249 | } | |
4250 | ||
4251 | req->flags |= REQ_F_NEED_CLEANUP; | |
4252 | return 0; | |
4253 | } | |
4254 | ||
04f34081 | 4255 | static int io_symlinkat(struct io_kiocb *req, unsigned int issue_flags) |
7a8721f8 DK |
4256 | { |
4257 | struct io_symlink *sl = &req->symlink; | |
4258 | int ret; | |
4259 | ||
4260 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4261 | return -EAGAIN; | |
4262 | ||
4263 | ret = do_symlinkat(sl->oldpath, sl->new_dfd, sl->newpath); | |
4264 | ||
4265 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4266 | if (ret < 0) | |
4267 | req_set_fail(req); | |
4268 | io_req_complete(req, ret); | |
4269 | return 0; | |
4270 | } | |
4271 | ||
cf30da90 DK |
4272 | static int io_linkat_prep(struct io_kiocb *req, |
4273 | const struct io_uring_sqe *sqe) | |
4274 | { | |
4275 | struct io_hardlink *lnk = &req->hardlink; | |
4276 | const char __user *oldf, *newf; | |
4277 | ||
4278 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4279 | return -EINVAL; | |
4280 | if (sqe->ioprio || sqe->rw_flags || sqe->buf_index || sqe->splice_fd_in) | |
4281 | return -EINVAL; | |
4282 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) | |
4283 | return -EBADF; | |
4284 | ||
4285 | lnk->old_dfd = READ_ONCE(sqe->fd); | |
4286 | lnk->new_dfd = READ_ONCE(sqe->len); | |
4287 | oldf = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4288 | newf = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
4289 | lnk->flags = READ_ONCE(sqe->hardlink_flags); | |
4290 | ||
4291 | lnk->oldpath = getname(oldf); | |
4292 | if (IS_ERR(lnk->oldpath)) | |
4293 | return PTR_ERR(lnk->oldpath); | |
4294 | ||
4295 | lnk->newpath = getname(newf); | |
4296 | if (IS_ERR(lnk->newpath)) { | |
4297 | putname(lnk->oldpath); | |
4298 | return PTR_ERR(lnk->newpath); | |
4299 | } | |
4300 | ||
4301 | req->flags |= REQ_F_NEED_CLEANUP; | |
4302 | return 0; | |
4303 | } | |
4304 | ||
04f34081 | 4305 | static int io_linkat(struct io_kiocb *req, unsigned int issue_flags) |
cf30da90 DK |
4306 | { |
4307 | struct io_hardlink *lnk = &req->hardlink; | |
4308 | int ret; | |
4309 | ||
4310 | if (issue_flags & IO_URING_F_NONBLOCK) | |
4311 | return -EAGAIN; | |
4312 | ||
4313 | ret = do_linkat(lnk->old_dfd, lnk->oldpath, lnk->new_dfd, | |
4314 | lnk->newpath, lnk->flags); | |
4315 | ||
4316 | req->flags &= ~REQ_F_NEED_CLEANUP; | |
4317 | if (ret < 0) | |
4318 | req_set_fail(req); | |
4319 | io_req_complete(req, ret); | |
4320 | return 0; | |
4321 | } | |
4322 | ||
36f4fa68 JA |
4323 | static int io_shutdown_prep(struct io_kiocb *req, |
4324 | const struct io_uring_sqe *sqe) | |
4325 | { | |
4326 | #if defined(CONFIG_NET) | |
4327 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
4328 | return -EINVAL; | |
26578cda PB |
4329 | if (unlikely(sqe->ioprio || sqe->off || sqe->addr || sqe->rw_flags || |
4330 | sqe->buf_index || sqe->splice_fd_in)) | |
36f4fa68 JA |
4331 | return -EINVAL; |
4332 | ||
4333 | req->shutdown.how = READ_ONCE(sqe->len); | |
4334 | return 0; | |
4335 | #else | |
4336 | return -EOPNOTSUPP; | |
4337 | #endif | |
4338 | } | |
4339 | ||
45d189c6 | 4340 | static int io_shutdown(struct io_kiocb *req, unsigned int issue_flags) |
36f4fa68 JA |
4341 | { |
4342 | #if defined(CONFIG_NET) | |
4343 | struct socket *sock; | |
4344 | int ret; | |
4345 | ||
45d189c6 | 4346 | if (issue_flags & IO_URING_F_NONBLOCK) |
36f4fa68 JA |
4347 | return -EAGAIN; |
4348 | ||
48aba79b | 4349 | sock = sock_from_file(req->file); |
36f4fa68 | 4350 | if (unlikely(!sock)) |
48aba79b | 4351 | return -ENOTSOCK; |
36f4fa68 JA |
4352 | |
4353 | ret = __sys_shutdown_sock(sock, req->shutdown.how); | |
a146468d | 4354 | if (ret < 0) |
93d2bcd2 | 4355 | req_set_fail(req); |
36f4fa68 JA |
4356 | io_req_complete(req, ret); |
4357 | return 0; | |
4358 | #else | |
4359 | return -EOPNOTSUPP; | |
4360 | #endif | |
4361 | } | |
4362 | ||
f2a8d5c7 PB |
4363 | static int __io_splice_prep(struct io_kiocb *req, |
4364 | const struct io_uring_sqe *sqe) | |
7d67af2c | 4365 | { |
fe7e3257 | 4366 | struct io_splice *sp = &req->splice; |
7d67af2c | 4367 | unsigned int valid_flags = SPLICE_F_FD_IN_FIXED | SPLICE_F_ALL; |
7d67af2c | 4368 | |
3232dd02 PB |
4369 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4370 | return -EINVAL; | |
7d67af2c PB |
4371 | |
4372 | sp->file_in = NULL; | |
7d67af2c PB |
4373 | sp->len = READ_ONCE(sqe->len); |
4374 | sp->flags = READ_ONCE(sqe->splice_flags); | |
4375 | ||
4376 | if (unlikely(sp->flags & ~valid_flags)) | |
4377 | return -EINVAL; | |
4378 | ||
62906e89 | 4379 | sp->file_in = io_file_get(req->ctx, req, READ_ONCE(sqe->splice_fd_in), |
8371adf5 PB |
4380 | (sp->flags & SPLICE_F_FD_IN_FIXED)); |
4381 | if (!sp->file_in) | |
4382 | return -EBADF; | |
7d67af2c | 4383 | req->flags |= REQ_F_NEED_CLEANUP; |
7d67af2c PB |
4384 | return 0; |
4385 | } | |
4386 | ||
f2a8d5c7 PB |
4387 | static int io_tee_prep(struct io_kiocb *req, |
4388 | const struct io_uring_sqe *sqe) | |
4389 | { | |
4390 | if (READ_ONCE(sqe->splice_off_in) || READ_ONCE(sqe->off)) | |
4391 | return -EINVAL; | |
4392 | return __io_splice_prep(req, sqe); | |
4393 | } | |
4394 | ||
45d189c6 | 4395 | static int io_tee(struct io_kiocb *req, unsigned int issue_flags) |
f2a8d5c7 PB |
4396 | { |
4397 | struct io_splice *sp = &req->splice; | |
4398 | struct file *in = sp->file_in; | |
4399 | struct file *out = sp->file_out; | |
4400 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4401 | long ret = 0; | |
4402 | ||
45d189c6 | 4403 | if (issue_flags & IO_URING_F_NONBLOCK) |
f2a8d5c7 PB |
4404 | return -EAGAIN; |
4405 | if (sp->len) | |
4406 | ret = do_tee(in, out, sp->len, flags); | |
4407 | ||
e1d767f0 PB |
4408 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4409 | io_put_file(in); | |
f2a8d5c7 PB |
4410 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4411 | ||
f2a8d5c7 | 4412 | if (ret != sp->len) |
93d2bcd2 | 4413 | req_set_fail(req); |
e1e16097 | 4414 | io_req_complete(req, ret); |
f2a8d5c7 PB |
4415 | return 0; |
4416 | } | |
4417 | ||
4418 | static int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
4419 | { | |
fe7e3257 | 4420 | struct io_splice *sp = &req->splice; |
f2a8d5c7 PB |
4421 | |
4422 | sp->off_in = READ_ONCE(sqe->splice_off_in); | |
4423 | sp->off_out = READ_ONCE(sqe->off); | |
4424 | return __io_splice_prep(req, sqe); | |
4425 | } | |
4426 | ||
45d189c6 | 4427 | static int io_splice(struct io_kiocb *req, unsigned int issue_flags) |
7d67af2c PB |
4428 | { |
4429 | struct io_splice *sp = &req->splice; | |
4430 | struct file *in = sp->file_in; | |
4431 | struct file *out = sp->file_out; | |
4432 | unsigned int flags = sp->flags & ~SPLICE_F_FD_IN_FIXED; | |
4433 | loff_t *poff_in, *poff_out; | |
c9687426 | 4434 | long ret = 0; |
7d67af2c | 4435 | |
45d189c6 | 4436 | if (issue_flags & IO_URING_F_NONBLOCK) |
2fb3e822 | 4437 | return -EAGAIN; |
7d67af2c PB |
4438 | |
4439 | poff_in = (sp->off_in == -1) ? NULL : &sp->off_in; | |
4440 | poff_out = (sp->off_out == -1) ? NULL : &sp->off_out; | |
c9687426 | 4441 | |
948a7749 | 4442 | if (sp->len) |
c9687426 | 4443 | ret = do_splice(in, poff_in, out, poff_out, sp->len, flags); |
7d67af2c | 4444 | |
e1d767f0 PB |
4445 | if (!(sp->flags & SPLICE_F_FD_IN_FIXED)) |
4446 | io_put_file(in); | |
7d67af2c PB |
4447 | req->flags &= ~REQ_F_NEED_CLEANUP; |
4448 | ||
7d67af2c | 4449 | if (ret != sp->len) |
93d2bcd2 | 4450 | req_set_fail(req); |
e1e16097 | 4451 | io_req_complete(req, ret); |
7d67af2c PB |
4452 | return 0; |
4453 | } | |
4454 | ||
2b188cc1 JA |
4455 | /* |
4456 | * IORING_OP_NOP just posts a completion event, nothing else. | |
4457 | */ | |
889fca73 | 4458 | static int io_nop(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 JA |
4459 | { |
4460 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 4461 | |
def596e9 JA |
4462 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
4463 | return -EINVAL; | |
4464 | ||
889fca73 | 4465 | __io_req_complete(req, issue_flags, 0, 0); |
2b188cc1 JA |
4466 | return 0; |
4467 | } | |
4468 | ||
4f57f06c JA |
4469 | static int io_msg_ring_prep(struct io_kiocb *req, |
4470 | const struct io_uring_sqe *sqe) | |
4471 | { | |
f3b6a41e JA |
4472 | if (unlikely(sqe->addr || sqe->ioprio || sqe->rw_flags || |
4473 | sqe->splice_fd_in || sqe->buf_index || sqe->personality)) | |
4f57f06c JA |
4474 | return -EINVAL; |
4475 | ||
4f57f06c JA |
4476 | req->msg.user_data = READ_ONCE(sqe->off); |
4477 | req->msg.len = READ_ONCE(sqe->len); | |
4478 | return 0; | |
4479 | } | |
4480 | ||
4481 | static int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags) | |
4482 | { | |
4483 | struct io_ring_ctx *target_ctx; | |
4484 | struct io_msg *msg = &req->msg; | |
4f57f06c | 4485 | bool filled; |
3f1d52ab | 4486 | int ret; |
4f57f06c | 4487 | |
3f1d52ab JA |
4488 | ret = -EBADFD; |
4489 | if (req->file->f_op != &io_uring_fops) | |
4490 | goto done; | |
4f57f06c | 4491 | |
3f1d52ab | 4492 | ret = -EOVERFLOW; |
4f57f06c JA |
4493 | target_ctx = req->file->private_data; |
4494 | ||
4495 | spin_lock(&target_ctx->completion_lock); | |
7ef66d18 | 4496 | filled = io_fill_cqe_aux(target_ctx, msg->user_data, msg->len, 0); |
4f57f06c JA |
4497 | io_commit_cqring(target_ctx); |
4498 | spin_unlock(&target_ctx->completion_lock); | |
4499 | ||
4500 | if (filled) { | |
4501 | io_cqring_ev_posted(target_ctx); | |
4502 | ret = 0; | |
4503 | } | |
4504 | ||
3f1d52ab | 4505 | done: |
9666d420 JA |
4506 | if (ret < 0) |
4507 | req_set_fail(req); | |
4f57f06c JA |
4508 | __io_req_complete(req, issue_flags, ret, 0); |
4509 | return 0; | |
4510 | } | |
4511 | ||
1155c76a | 4512 | static int io_fsync_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 4513 | { |
6b06314c | 4514 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 4515 | |
6b06314c | 4516 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 4517 | return -EINVAL; |
26578cda PB |
4518 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
4519 | sqe->splice_fd_in)) | |
c992fe29 CH |
4520 | return -EINVAL; |
4521 | ||
8ed8d3c3 JA |
4522 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
4523 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
4524 | return -EINVAL; | |
4525 | ||
4526 | req->sync.off = READ_ONCE(sqe->off); | |
4527 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
4528 | return 0; |
4529 | } | |
4530 | ||
45d189c6 | 4531 | static int io_fsync(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 4532 | { |
8ed8d3c3 | 4533 | loff_t end = req->sync.off + req->sync.len; |
8ed8d3c3 JA |
4534 | int ret; |
4535 | ||
ac45abc0 | 4536 | /* fsync always requires a blocking context */ |
45d189c6 | 4537 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
4538 | return -EAGAIN; |
4539 | ||
9adbd45d | 4540 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
4541 | end > 0 ? end : LLONG_MAX, |
4542 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
4543 | if (ret < 0) | |
93d2bcd2 | 4544 | req_set_fail(req); |
e1e16097 | 4545 | io_req_complete(req, ret); |
c992fe29 CH |
4546 | return 0; |
4547 | } | |
4548 | ||
d63d1b5e JA |
4549 | static int io_fallocate_prep(struct io_kiocb *req, |
4550 | const struct io_uring_sqe *sqe) | |
4551 | { | |
26578cda PB |
4552 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags || |
4553 | sqe->splice_fd_in) | |
d63d1b5e | 4554 | return -EINVAL; |
3232dd02 PB |
4555 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4556 | return -EINVAL; | |
d63d1b5e JA |
4557 | |
4558 | req->sync.off = READ_ONCE(sqe->off); | |
4559 | req->sync.len = READ_ONCE(sqe->addr); | |
4560 | req->sync.mode = READ_ONCE(sqe->len); | |
4561 | return 0; | |
4562 | } | |
4563 | ||
45d189c6 | 4564 | static int io_fallocate(struct io_kiocb *req, unsigned int issue_flags) |
5d17b4a4 | 4565 | { |
ac45abc0 PB |
4566 | int ret; |
4567 | ||
d63d1b5e | 4568 | /* fallocate always requiring blocking context */ |
45d189c6 | 4569 | if (issue_flags & IO_URING_F_NONBLOCK) |
5d17b4a4 | 4570 | return -EAGAIN; |
ac45abc0 PB |
4571 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
4572 | req->sync.len); | |
ac45abc0 | 4573 | if (ret < 0) |
93d2bcd2 | 4574 | req_set_fail(req); |
f63cf519 JA |
4575 | else |
4576 | fsnotify_modify(req->file); | |
e1e16097 | 4577 | io_req_complete(req, ret); |
5d17b4a4 JA |
4578 | return 0; |
4579 | } | |
4580 | ||
ec65fea5 | 4581 | static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 4582 | { |
f8748881 | 4583 | const char __user *fname; |
15b71abe | 4584 | int ret; |
b7bb4f7d | 4585 | |
d3fddf6d PB |
4586 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4587 | return -EINVAL; | |
b9445598 | 4588 | if (unlikely(sqe->ioprio || sqe->buf_index)) |
15b71abe | 4589 | return -EINVAL; |
ec65fea5 | 4590 | if (unlikely(req->flags & REQ_F_FIXED_FILE)) |
cf3040ca | 4591 | return -EBADF; |
03b1230c | 4592 | |
ec65fea5 PB |
4593 | /* open.how should be already initialised */ |
4594 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) | |
08a1d26e | 4595 | req->open.how.flags |= O_LARGEFILE; |
3529d8c2 | 4596 | |
25e72d10 PB |
4597 | req->open.dfd = READ_ONCE(sqe->fd); |
4598 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
f8748881 | 4599 | req->open.filename = getname(fname); |
15b71abe JA |
4600 | if (IS_ERR(req->open.filename)) { |
4601 | ret = PTR_ERR(req->open.filename); | |
4602 | req->open.filename = NULL; | |
4603 | return ret; | |
4604 | } | |
b9445598 PB |
4605 | |
4606 | req->open.file_slot = READ_ONCE(sqe->file_index); | |
4607 | if (req->open.file_slot && (req->open.how.flags & O_CLOEXEC)) | |
4608 | return -EINVAL; | |
4609 | ||
4022e7af | 4610 | req->open.nofile = rlimit(RLIMIT_NOFILE); |
8fef80bf | 4611 | req->flags |= REQ_F_NEED_CLEANUP; |
15b71abe | 4612 | return 0; |
03b1230c JA |
4613 | } |
4614 | ||
ec65fea5 PB |
4615 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4616 | { | |
d3fddf6d PB |
4617 | u64 mode = READ_ONCE(sqe->len); |
4618 | u64 flags = READ_ONCE(sqe->open_flags); | |
ec65fea5 | 4619 | |
ec65fea5 PB |
4620 | req->open.how = build_open_how(flags, mode); |
4621 | return __io_openat_prep(req, sqe); | |
4622 | } | |
4623 | ||
cebdb986 | 4624 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 4625 | { |
cebdb986 | 4626 | struct open_how __user *how; |
cebdb986 | 4627 | size_t len; |
0fa03c62 JA |
4628 | int ret; |
4629 | ||
cebdb986 JA |
4630 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
4631 | len = READ_ONCE(sqe->len); | |
cebdb986 JA |
4632 | if (len < OPEN_HOW_SIZE_VER0) |
4633 | return -EINVAL; | |
3529d8c2 | 4634 | |
cebdb986 JA |
4635 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
4636 | len); | |
4637 | if (ret) | |
4638 | return ret; | |
3529d8c2 | 4639 | |
ec65fea5 | 4640 | return __io_openat_prep(req, sqe); |
cebdb986 JA |
4641 | } |
4642 | ||
45d189c6 | 4643 | static int io_openat2(struct io_kiocb *req, unsigned int issue_flags) |
15b71abe JA |
4644 | { |
4645 | struct open_flags op; | |
15b71abe | 4646 | struct file *file; |
b9445598 PB |
4647 | bool resolve_nonblock, nonblock_set; |
4648 | bool fixed = !!req->open.file_slot; | |
15b71abe JA |
4649 | int ret; |
4650 | ||
cebdb986 | 4651 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
4652 | if (ret) |
4653 | goto err; | |
3a81fd02 JA |
4654 | nonblock_set = op.open_flag & O_NONBLOCK; |
4655 | resolve_nonblock = req->open.how.resolve & RESOLVE_CACHED; | |
45d189c6 | 4656 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3a81fd02 JA |
4657 | /* |
4658 | * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open, | |
4659 | * it'll always -EAGAIN | |
4660 | */ | |
4661 | if (req->open.how.flags & (O_TRUNC | O_CREAT | O_TMPFILE)) | |
4662 | return -EAGAIN; | |
4663 | op.lookup_flags |= LOOKUP_CACHED; | |
4664 | op.open_flag |= O_NONBLOCK; | |
4665 | } | |
15b71abe | 4666 | |
b9445598 PB |
4667 | if (!fixed) { |
4668 | ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile); | |
4669 | if (ret < 0) | |
4670 | goto err; | |
4671 | } | |
15b71abe JA |
4672 | |
4673 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
12dcb58a | 4674 | if (IS_ERR(file)) { |
944d1444 | 4675 | /* |
12dcb58a PB |
4676 | * We could hang on to this 'fd' on retrying, but seems like |
4677 | * marginal gain for something that is now known to be a slower | |
4678 | * path. So just put it, and we'll get a new one when we retry. | |
944d1444 | 4679 | */ |
b9445598 PB |
4680 | if (!fixed) |
4681 | put_unused_fd(ret); | |
3a81fd02 | 4682 | |
15b71abe | 4683 | ret = PTR_ERR(file); |
12dcb58a PB |
4684 | /* only retry if RESOLVE_CACHED wasn't already set by application */ |
4685 | if (ret == -EAGAIN && | |
4686 | (!resolve_nonblock && (issue_flags & IO_URING_F_NONBLOCK))) | |
4687 | return -EAGAIN; | |
4688 | goto err; | |
15b71abe | 4689 | } |
12dcb58a PB |
4690 | |
4691 | if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set) | |
4692 | file->f_flags &= ~O_NONBLOCK; | |
4693 | fsnotify_open(file); | |
b9445598 PB |
4694 | |
4695 | if (!fixed) | |
4696 | fd_install(ret, file); | |
4697 | else | |
4698 | ret = io_install_fixed_file(req, file, issue_flags, | |
4699 | req->open.file_slot - 1); | |
15b71abe JA |
4700 | err: |
4701 | putname(req->open.filename); | |
8fef80bf | 4702 | req->flags &= ~REQ_F_NEED_CLEANUP; |
15b71abe | 4703 | if (ret < 0) |
93d2bcd2 | 4704 | req_set_fail(req); |
0bdf3398 | 4705 | __io_req_complete(req, issue_flags, ret, 0); |
15b71abe JA |
4706 | return 0; |
4707 | } | |
4708 | ||
45d189c6 | 4709 | static int io_openat(struct io_kiocb *req, unsigned int issue_flags) |
cebdb986 | 4710 | { |
e45cff58 | 4711 | return io_openat2(req, issue_flags); |
cebdb986 JA |
4712 | } |
4713 | ||
067524e9 JA |
4714 | static int io_remove_buffers_prep(struct io_kiocb *req, |
4715 | const struct io_uring_sqe *sqe) | |
4716 | { | |
4717 | struct io_provide_buf *p = &req->pbuf; | |
4718 | u64 tmp; | |
4719 | ||
26578cda PB |
4720 | if (sqe->ioprio || sqe->rw_flags || sqe->addr || sqe->len || sqe->off || |
4721 | sqe->splice_fd_in) | |
067524e9 JA |
4722 | return -EINVAL; |
4723 | ||
4724 | tmp = READ_ONCE(sqe->fd); | |
4725 | if (!tmp || tmp > USHRT_MAX) | |
4726 | return -EINVAL; | |
4727 | ||
4728 | memset(p, 0, sizeof(*p)); | |
4729 | p->nbufs = tmp; | |
4730 | p->bgid = READ_ONCE(sqe->buf_group); | |
4731 | return 0; | |
4732 | } | |
4733 | ||
dbc7d452 JA |
4734 | static int __io_remove_buffers(struct io_ring_ctx *ctx, |
4735 | struct io_buffer_list *bl, unsigned nbufs) | |
067524e9 JA |
4736 | { |
4737 | unsigned i = 0; | |
4738 | ||
4739 | /* shouldn't happen */ | |
4740 | if (!nbufs) | |
4741 | return 0; | |
4742 | ||
4743 | /* the head kbuf is the list itself */ | |
dbc7d452 | 4744 | while (!list_empty(&bl->buf_list)) { |
067524e9 JA |
4745 | struct io_buffer *nxt; |
4746 | ||
dbc7d452 | 4747 | nxt = list_first_entry(&bl->buf_list, struct io_buffer, list); |
067524e9 | 4748 | list_del(&nxt->list); |
067524e9 JA |
4749 | if (++i == nbufs) |
4750 | return i; | |
1d0254e6 | 4751 | cond_resched(); |
067524e9 JA |
4752 | } |
4753 | i++; | |
067524e9 JA |
4754 | |
4755 | return i; | |
4756 | } | |
4757 | ||
889fca73 | 4758 | static int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags) |
067524e9 JA |
4759 | { |
4760 | struct io_provide_buf *p = &req->pbuf; | |
4761 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4762 | struct io_buffer_list *bl; |
067524e9 | 4763 | int ret = 0; |
3b44b371 | 4764 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
067524e9 | 4765 | |
3b44b371 | 4766 | io_ring_submit_lock(ctx, needs_lock); |
067524e9 JA |
4767 | |
4768 | lockdep_assert_held(&ctx->uring_lock); | |
4769 | ||
4770 | ret = -ENOENT; | |
dbc7d452 JA |
4771 | bl = io_buffer_get_list(ctx, p->bgid); |
4772 | if (bl) | |
4773 | ret = __io_remove_buffers(ctx, bl, p->nbufs); | |
067524e9 | 4774 | if (ret < 0) |
93d2bcd2 | 4775 | req_set_fail(req); |
067524e9 | 4776 | |
9fb8cb49 PB |
4777 | /* complete before unlock, IOPOLL may need the lock */ |
4778 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4779 | io_ring_submit_unlock(ctx, needs_lock); |
067524e9 JA |
4780 | return 0; |
4781 | } | |
4782 | ||
ddf0322d JA |
4783 | static int io_provide_buffers_prep(struct io_kiocb *req, |
4784 | const struct io_uring_sqe *sqe) | |
4785 | { | |
38134ada | 4786 | unsigned long size, tmp_check; |
ddf0322d JA |
4787 | struct io_provide_buf *p = &req->pbuf; |
4788 | u64 tmp; | |
4789 | ||
26578cda | 4790 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
ddf0322d JA |
4791 | return -EINVAL; |
4792 | ||
4793 | tmp = READ_ONCE(sqe->fd); | |
4794 | if (!tmp || tmp > USHRT_MAX) | |
4795 | return -E2BIG; | |
4796 | p->nbufs = tmp; | |
4797 | p->addr = READ_ONCE(sqe->addr); | |
4798 | p->len = READ_ONCE(sqe->len); | |
4799 | ||
38134ada PB |
4800 | if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs, |
4801 | &size)) | |
4802 | return -EOVERFLOW; | |
4803 | if (check_add_overflow((unsigned long)p->addr, size, &tmp_check)) | |
4804 | return -EOVERFLOW; | |
4805 | ||
d81269fe PB |
4806 | size = (unsigned long)p->len * p->nbufs; |
4807 | if (!access_ok(u64_to_user_ptr(p->addr), size)) | |
ddf0322d JA |
4808 | return -EFAULT; |
4809 | ||
4810 | p->bgid = READ_ONCE(sqe->buf_group); | |
4811 | tmp = READ_ONCE(sqe->off); | |
4812 | if (tmp > USHRT_MAX) | |
4813 | return -E2BIG; | |
4814 | p->bid = tmp; | |
4815 | return 0; | |
4816 | } | |
4817 | ||
cc3cec83 JA |
4818 | static int io_refill_buffer_cache(struct io_ring_ctx *ctx) |
4819 | { | |
4820 | struct io_buffer *buf; | |
4821 | struct page *page; | |
4822 | int bufs_in_page; | |
4823 | ||
4824 | /* | |
4825 | * Completions that don't happen inline (eg not under uring_lock) will | |
4826 | * add to ->io_buffers_comp. If we don't have any free buffers, check | |
4827 | * the completion list and splice those entries first. | |
4828 | */ | |
4829 | if (!list_empty_careful(&ctx->io_buffers_comp)) { | |
4830 | spin_lock(&ctx->completion_lock); | |
4831 | if (!list_empty(&ctx->io_buffers_comp)) { | |
4832 | list_splice_init(&ctx->io_buffers_comp, | |
4833 | &ctx->io_buffers_cache); | |
4834 | spin_unlock(&ctx->completion_lock); | |
4835 | return 0; | |
4836 | } | |
4837 | spin_unlock(&ctx->completion_lock); | |
4838 | } | |
4839 | ||
4840 | /* | |
4841 | * No free buffers and no completion entries either. Allocate a new | |
4842 | * page worth of buffer entries and add those to our freelist. | |
4843 | */ | |
4844 | page = alloc_page(GFP_KERNEL_ACCOUNT); | |
4845 | if (!page) | |
4846 | return -ENOMEM; | |
4847 | ||
4848 | list_add(&page->lru, &ctx->io_buffers_pages); | |
4849 | ||
4850 | buf = page_address(page); | |
4851 | bufs_in_page = PAGE_SIZE / sizeof(*buf); | |
4852 | while (bufs_in_page) { | |
4853 | list_add_tail(&buf->list, &ctx->io_buffers_cache); | |
4854 | buf++; | |
4855 | bufs_in_page--; | |
4856 | } | |
4857 | ||
4858 | return 0; | |
4859 | } | |
4860 | ||
4861 | static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf, | |
dbc7d452 | 4862 | struct io_buffer_list *bl) |
ddf0322d JA |
4863 | { |
4864 | struct io_buffer *buf; | |
4865 | u64 addr = pbuf->addr; | |
4866 | int i, bid = pbuf->bid; | |
4867 | ||
4868 | for (i = 0; i < pbuf->nbufs; i++) { | |
cc3cec83 JA |
4869 | if (list_empty(&ctx->io_buffers_cache) && |
4870 | io_refill_buffer_cache(ctx)) | |
ddf0322d | 4871 | break; |
cc3cec83 JA |
4872 | buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer, |
4873 | list); | |
dbc7d452 | 4874 | list_move_tail(&buf->list, &bl->buf_list); |
ddf0322d | 4875 | buf->addr = addr; |
d1f82808 | 4876 | buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT); |
ddf0322d | 4877 | buf->bid = bid; |
b1c62645 | 4878 | buf->bgid = pbuf->bgid; |
ddf0322d JA |
4879 | addr += pbuf->len; |
4880 | bid++; | |
f240762f | 4881 | cond_resched(); |
ddf0322d JA |
4882 | } |
4883 | ||
dbc7d452 | 4884 | return i ? 0 : -ENOMEM; |
ddf0322d JA |
4885 | } |
4886 | ||
889fca73 | 4887 | static int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags) |
ddf0322d JA |
4888 | { |
4889 | struct io_provide_buf *p = &req->pbuf; | |
4890 | struct io_ring_ctx *ctx = req->ctx; | |
dbc7d452 | 4891 | struct io_buffer_list *bl; |
ddf0322d | 4892 | int ret = 0; |
3b44b371 | 4893 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
ddf0322d | 4894 | |
3b44b371 | 4895 | io_ring_submit_lock(ctx, needs_lock); |
ddf0322d JA |
4896 | |
4897 | lockdep_assert_held(&ctx->uring_lock); | |
4898 | ||
dbc7d452 JA |
4899 | bl = io_buffer_get_list(ctx, p->bgid); |
4900 | if (unlikely(!bl)) { | |
4901 | bl = kmalloc(sizeof(*bl), GFP_KERNEL); | |
4902 | if (!bl) { | |
4903 | ret = -ENOMEM; | |
4904 | goto err; | |
4905 | } | |
4906 | io_buffer_add_list(ctx, bl, p->bgid); | |
ddf0322d | 4907 | } |
dbc7d452 JA |
4908 | |
4909 | ret = io_add_buffers(ctx, p, bl); | |
4910 | err: | |
ddf0322d | 4911 | if (ret < 0) |
93d2bcd2 | 4912 | req_set_fail(req); |
9fb8cb49 PB |
4913 | /* complete before unlock, IOPOLL may need the lock */ |
4914 | __io_req_complete(req, issue_flags, ret, 0); | |
3b44b371 | 4915 | io_ring_submit_unlock(ctx, needs_lock); |
ddf0322d | 4916 | return 0; |
cebdb986 JA |
4917 | } |
4918 | ||
3e4827b0 JA |
4919 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
4920 | const struct io_uring_sqe *sqe) | |
4921 | { | |
4922 | #if defined(CONFIG_EPOLL) | |
26578cda | 4923 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
3e4827b0 | 4924 | return -EINVAL; |
2d74d042 | 4925 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 4926 | return -EINVAL; |
3e4827b0 JA |
4927 | |
4928 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
4929 | req->epoll.op = READ_ONCE(sqe->len); | |
4930 | req->epoll.fd = READ_ONCE(sqe->off); | |
4931 | ||
4932 | if (ep_op_has_event(req->epoll.op)) { | |
4933 | struct epoll_event __user *ev; | |
4934 | ||
4935 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
4936 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
4937 | return -EFAULT; | |
4938 | } | |
4939 | ||
4940 | return 0; | |
4941 | #else | |
4942 | return -EOPNOTSUPP; | |
4943 | #endif | |
4944 | } | |
4945 | ||
889fca73 | 4946 | static int io_epoll_ctl(struct io_kiocb *req, unsigned int issue_flags) |
3e4827b0 JA |
4947 | { |
4948 | #if defined(CONFIG_EPOLL) | |
4949 | struct io_epoll *ie = &req->epoll; | |
4950 | int ret; | |
45d189c6 | 4951 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
3e4827b0 JA |
4952 | |
4953 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
4954 | if (force_nonblock && ret == -EAGAIN) | |
4955 | return -EAGAIN; | |
4956 | ||
4957 | if (ret < 0) | |
93d2bcd2 | 4958 | req_set_fail(req); |
889fca73 | 4959 | __io_req_complete(req, issue_flags, ret, 0); |
3e4827b0 JA |
4960 | return 0; |
4961 | #else | |
4962 | return -EOPNOTSUPP; | |
4963 | #endif | |
4964 | } | |
4965 | ||
c1ca757b JA |
4966 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4967 | { | |
4968 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
26578cda | 4969 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->splice_fd_in) |
c1ca757b | 4970 | return -EINVAL; |
3232dd02 PB |
4971 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
4972 | return -EINVAL; | |
c1ca757b JA |
4973 | |
4974 | req->madvise.addr = READ_ONCE(sqe->addr); | |
4975 | req->madvise.len = READ_ONCE(sqe->len); | |
4976 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
4977 | return 0; | |
4978 | #else | |
4979 | return -EOPNOTSUPP; | |
4980 | #endif | |
4981 | } | |
4982 | ||
45d189c6 | 4983 | static int io_madvise(struct io_kiocb *req, unsigned int issue_flags) |
c1ca757b JA |
4984 | { |
4985 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
4986 | struct io_madvise *ma = &req->madvise; | |
4987 | int ret; | |
4988 | ||
45d189c6 | 4989 | if (issue_flags & IO_URING_F_NONBLOCK) |
c1ca757b JA |
4990 | return -EAGAIN; |
4991 | ||
0726b01e | 4992 | ret = do_madvise(current->mm, ma->addr, ma->len, ma->advice); |
c1ca757b | 4993 | if (ret < 0) |
93d2bcd2 | 4994 | req_set_fail(req); |
e1e16097 | 4995 | io_req_complete(req, ret); |
c1ca757b JA |
4996 | return 0; |
4997 | #else | |
4998 | return -EOPNOTSUPP; | |
4999 | #endif | |
5000 | } | |
5001 | ||
4840e418 JA |
5002 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5003 | { | |
26578cda | 5004 | if (sqe->ioprio || sqe->buf_index || sqe->addr || sqe->splice_fd_in) |
4840e418 | 5005 | return -EINVAL; |
3232dd02 PB |
5006 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5007 | return -EINVAL; | |
4840e418 JA |
5008 | |
5009 | req->fadvise.offset = READ_ONCE(sqe->off); | |
5010 | req->fadvise.len = READ_ONCE(sqe->len); | |
5011 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
5012 | return 0; | |
5013 | } | |
5014 | ||
45d189c6 | 5015 | static int io_fadvise(struct io_kiocb *req, unsigned int issue_flags) |
4840e418 JA |
5016 | { |
5017 | struct io_fadvise *fa = &req->fadvise; | |
5018 | int ret; | |
5019 | ||
45d189c6 | 5020 | if (issue_flags & IO_URING_F_NONBLOCK) { |
3e69426d JA |
5021 | switch (fa->advice) { |
5022 | case POSIX_FADV_NORMAL: | |
5023 | case POSIX_FADV_RANDOM: | |
5024 | case POSIX_FADV_SEQUENTIAL: | |
5025 | break; | |
5026 | default: | |
5027 | return -EAGAIN; | |
5028 | } | |
5029 | } | |
4840e418 JA |
5030 | |
5031 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
5032 | if (ret < 0) | |
93d2bcd2 | 5033 | req_set_fail(req); |
0bdf3398 | 5034 | __io_req_complete(req, issue_flags, ret, 0); |
4840e418 JA |
5035 | return 0; |
5036 | } | |
5037 | ||
eddc7ef5 JA |
5038 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5039 | { | |
1b6fe6e0 SR |
5040 | const char __user *path; |
5041 | ||
2d74d042 | 5042 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5043 | return -EINVAL; |
26578cda | 5044 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
eddc7ef5 | 5045 | return -EINVAL; |
9c280f90 | 5046 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5047 | return -EBADF; |
eddc7ef5 | 5048 | |
1d9e1288 BM |
5049 | req->statx.dfd = READ_ONCE(sqe->fd); |
5050 | req->statx.mask = READ_ONCE(sqe->len); | |
1b6fe6e0 | 5051 | path = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
1d9e1288 BM |
5052 | req->statx.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
5053 | req->statx.flags = READ_ONCE(sqe->statx_flags); | |
eddc7ef5 | 5054 | |
1b6fe6e0 SR |
5055 | req->statx.filename = getname_flags(path, |
5056 | getname_statx_lookup_flags(req->statx.flags), | |
5057 | NULL); | |
5058 | ||
5059 | if (IS_ERR(req->statx.filename)) { | |
5060 | int ret = PTR_ERR(req->statx.filename); | |
5061 | ||
5062 | req->statx.filename = NULL; | |
5063 | return ret; | |
5064 | } | |
5065 | ||
5066 | req->flags |= REQ_F_NEED_CLEANUP; | |
eddc7ef5 JA |
5067 | return 0; |
5068 | } | |
5069 | ||
45d189c6 | 5070 | static int io_statx(struct io_kiocb *req, unsigned int issue_flags) |
eddc7ef5 | 5071 | { |
1d9e1288 | 5072 | struct io_statx *ctx = &req->statx; |
eddc7ef5 JA |
5073 | int ret; |
5074 | ||
59d70013 | 5075 | if (issue_flags & IO_URING_F_NONBLOCK) |
eddc7ef5 JA |
5076 | return -EAGAIN; |
5077 | ||
e62753e4 BM |
5078 | ret = do_statx(ctx->dfd, ctx->filename, ctx->flags, ctx->mask, |
5079 | ctx->buffer); | |
eddc7ef5 | 5080 | |
eddc7ef5 | 5081 | if (ret < 0) |
93d2bcd2 | 5082 | req_set_fail(req); |
e1e16097 | 5083 | io_req_complete(req, ret); |
eddc7ef5 JA |
5084 | return 0; |
5085 | } | |
5086 | ||
b5dba59e JA |
5087 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5088 | { | |
14587a46 | 5089 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3232dd02 | 5090 | return -EINVAL; |
b5dba59e | 5091 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || |
7df778be | 5092 | sqe->rw_flags || sqe->buf_index) |
b5dba59e | 5093 | return -EINVAL; |
9c280f90 | 5094 | if (req->flags & REQ_F_FIXED_FILE) |
cf3040ca | 5095 | return -EBADF; |
b5dba59e JA |
5096 | |
5097 | req->close.fd = READ_ONCE(sqe->fd); | |
7df778be PB |
5098 | req->close.file_slot = READ_ONCE(sqe->file_index); |
5099 | if (req->close.file_slot && req->close.fd) | |
5100 | return -EINVAL; | |
5101 | ||
b5dba59e | 5102 | return 0; |
b5dba59e JA |
5103 | } |
5104 | ||
889fca73 | 5105 | static int io_close(struct io_kiocb *req, unsigned int issue_flags) |
b5dba59e | 5106 | { |
9eac1904 | 5107 | struct files_struct *files = current->files; |
3af73b28 | 5108 | struct io_close *close = &req->close; |
9eac1904 | 5109 | struct fdtable *fdt; |
a1fde923 PB |
5110 | struct file *file = NULL; |
5111 | int ret = -EBADF; | |
b5dba59e | 5112 | |
7df778be PB |
5113 | if (req->close.file_slot) { |
5114 | ret = io_close_fixed(req, issue_flags); | |
5115 | goto err; | |
5116 | } | |
5117 | ||
9eac1904 JA |
5118 | spin_lock(&files->file_lock); |
5119 | fdt = files_fdtable(files); | |
5120 | if (close->fd >= fdt->max_fds) { | |
5121 | spin_unlock(&files->file_lock); | |
5122 | goto err; | |
5123 | } | |
5124 | file = fdt->fd[close->fd]; | |
a1fde923 | 5125 | if (!file || file->f_op == &io_uring_fops) { |
9eac1904 JA |
5126 | spin_unlock(&files->file_lock); |
5127 | file = NULL; | |
5128 | goto err; | |
3af73b28 | 5129 | } |
b5dba59e JA |
5130 | |
5131 | /* if the file has a flush method, be safe and punt to async */ | |
45d189c6 | 5132 | if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) { |
9eac1904 | 5133 | spin_unlock(&files->file_lock); |
0bf0eefd | 5134 | return -EAGAIN; |
a2100672 | 5135 | } |
b5dba59e | 5136 | |
9eac1904 JA |
5137 | ret = __close_fd_get_file(close->fd, &file); |
5138 | spin_unlock(&files->file_lock); | |
5139 | if (ret < 0) { | |
5140 | if (ret == -ENOENT) | |
5141 | ret = -EBADF; | |
5142 | goto err; | |
5143 | } | |
5144 | ||
3af73b28 | 5145 | /* No ->flush() or already async, safely close from here */ |
9eac1904 JA |
5146 | ret = filp_close(file, current->files); |
5147 | err: | |
3af73b28 | 5148 | if (ret < 0) |
93d2bcd2 | 5149 | req_set_fail(req); |
9eac1904 JA |
5150 | if (file) |
5151 | fput(file); | |
889fca73 | 5152 | __io_req_complete(req, issue_flags, ret, 0); |
1a417f4e | 5153 | return 0; |
b5dba59e JA |
5154 | } |
5155 | ||
1155c76a | 5156 | static int io_sfr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
5157 | { |
5158 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 | 5159 | |
5d17b4a4 JA |
5160 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
5161 | return -EINVAL; | |
26578cda PB |
5162 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index || |
5163 | sqe->splice_fd_in)) | |
5d17b4a4 JA |
5164 | return -EINVAL; |
5165 | ||
8ed8d3c3 JA |
5166 | req->sync.off = READ_ONCE(sqe->off); |
5167 | req->sync.len = READ_ONCE(sqe->len); | |
5168 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
5169 | return 0; |
5170 | } | |
5171 | ||
45d189c6 | 5172 | static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 | 5173 | { |
8ed8d3c3 JA |
5174 | int ret; |
5175 | ||
ac45abc0 | 5176 | /* sync_file_range always requires a blocking context */ |
45d189c6 | 5177 | if (issue_flags & IO_URING_F_NONBLOCK) |
ac45abc0 PB |
5178 | return -EAGAIN; |
5179 | ||
9adbd45d | 5180 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
5181 | req->sync.flags); |
5182 | if (ret < 0) | |
93d2bcd2 | 5183 | req_set_fail(req); |
e1e16097 | 5184 | io_req_complete(req, ret); |
5d17b4a4 JA |
5185 | return 0; |
5186 | } | |
5187 | ||
469956e8 | 5188 | #if defined(CONFIG_NET) |
02d27d89 PB |
5189 | static int io_setup_async_msg(struct io_kiocb *req, |
5190 | struct io_async_msghdr *kmsg) | |
5191 | { | |
e8c2bc1f JA |
5192 | struct io_async_msghdr *async_msg = req->async_data; |
5193 | ||
5194 | if (async_msg) | |
02d27d89 | 5195 | return -EAGAIN; |
e8c2bc1f | 5196 | if (io_alloc_async_data(req)) { |
257e84a5 | 5197 | kfree(kmsg->free_iov); |
02d27d89 PB |
5198 | return -ENOMEM; |
5199 | } | |
e8c2bc1f | 5200 | async_msg = req->async_data; |
02d27d89 | 5201 | req->flags |= REQ_F_NEED_CLEANUP; |
e8c2bc1f | 5202 | memcpy(async_msg, kmsg, sizeof(*kmsg)); |
2a780802 | 5203 | async_msg->msg.msg_name = &async_msg->addr; |
257e84a5 PB |
5204 | /* if were using fast_iov, set it to the new one */ |
5205 | if (!async_msg->free_iov) | |
5206 | async_msg->msg.msg_iter.iov = async_msg->fast_iov; | |
5207 | ||
02d27d89 PB |
5208 | return -EAGAIN; |
5209 | } | |
5210 | ||
2ae523ed PB |
5211 | static int io_sendmsg_copy_hdr(struct io_kiocb *req, |
5212 | struct io_async_msghdr *iomsg) | |
5213 | { | |
2ae523ed | 5214 | iomsg->msg.msg_name = &iomsg->addr; |
257e84a5 | 5215 | iomsg->free_iov = iomsg->fast_iov; |
2ae523ed | 5216 | return sendmsg_copy_msghdr(&iomsg->msg, req->sr_msg.umsg, |
257e84a5 | 5217 | req->sr_msg.msg_flags, &iomsg->free_iov); |
2ae523ed PB |
5218 | } |
5219 | ||
93642ef8 PB |
5220 | static int io_sendmsg_prep_async(struct io_kiocb *req) |
5221 | { | |
5222 | int ret; | |
5223 | ||
93642ef8 PB |
5224 | ret = io_sendmsg_copy_hdr(req, req->async_data); |
5225 | if (!ret) | |
5226 | req->flags |= REQ_F_NEED_CLEANUP; | |
5227 | return ret; | |
5228 | } | |
5229 | ||
3529d8c2 | 5230 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 5231 | { |
e47293fd | 5232 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5233 | |
d2b6f48b PB |
5234 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5235 | return -EINVAL; | |
5236 | ||
270a5940 | 5237 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
fddaface | 5238 | sr->len = READ_ONCE(sqe->len); |
04411806 PB |
5239 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5240 | if (sr->msg_flags & MSG_DONTWAIT) | |
5241 | req->flags |= REQ_F_NOWAIT; | |
3529d8c2 | 5242 | |
d8768362 JA |
5243 | #ifdef CONFIG_COMPAT |
5244 | if (req->ctx->compat) | |
5245 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5246 | #endif | |
93642ef8 | 5247 | return 0; |
03b1230c JA |
5248 | } |
5249 | ||
889fca73 | 5250 | static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5251 | { |
6b754c8b | 5252 | struct io_async_msghdr iomsg, *kmsg; |
0fa03c62 | 5253 | struct socket *sock; |
7a7cacba | 5254 | unsigned flags; |
0031275d | 5255 | int min_ret = 0; |
0fa03c62 JA |
5256 | int ret; |
5257 | ||
dba4a925 | 5258 | sock = sock_from_file(req->file); |
7a7cacba | 5259 | if (unlikely(!sock)) |
dba4a925 | 5260 | return -ENOTSOCK; |
3529d8c2 | 5261 | |
d886e185 PB |
5262 | if (req_has_async_data(req)) { |
5263 | kmsg = req->async_data; | |
5264 | } else { | |
7a7cacba PB |
5265 | ret = io_sendmsg_copy_hdr(req, &iomsg); |
5266 | if (ret) | |
5267 | return ret; | |
5268 | kmsg = &iomsg; | |
0fa03c62 | 5269 | } |
0fa03c62 | 5270 | |
04411806 PB |
5271 | flags = req->sr_msg.msg_flags; |
5272 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5273 | flags |= MSG_DONTWAIT; |
0031275d SM |
5274 | if (flags & MSG_WAITALL) |
5275 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5276 | ||
7a7cacba | 5277 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
0fa03c62 | 5278 | |
7297ce3d PB |
5279 | if (ret < min_ret) { |
5280 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5281 | return io_setup_async_msg(req, kmsg); | |
5282 | if (ret == -ERESTARTSYS) | |
5283 | ret = -EINTR; | |
5284 | req_set_fail(req); | |
5285 | } | |
257e84a5 PB |
5286 | /* fast path, check for non-NULL to avoid function call */ |
5287 | if (kmsg->free_iov) | |
5288 | kfree(kmsg->free_iov); | |
99bc4c38 | 5289 | req->flags &= ~REQ_F_NEED_CLEANUP; |
889fca73 | 5290 | __io_req_complete(req, issue_flags, ret, 0); |
5d17b4a4 | 5291 | return 0; |
03b1230c | 5292 | } |
aa1fa28f | 5293 | |
889fca73 | 5294 | static int io_send(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5295 | { |
7a7cacba PB |
5296 | struct io_sr_msg *sr = &req->sr_msg; |
5297 | struct msghdr msg; | |
5298 | struct iovec iov; | |
fddaface | 5299 | struct socket *sock; |
7a7cacba | 5300 | unsigned flags; |
0031275d | 5301 | int min_ret = 0; |
fddaface JA |
5302 | int ret; |
5303 | ||
dba4a925 | 5304 | sock = sock_from_file(req->file); |
7a7cacba | 5305 | if (unlikely(!sock)) |
dba4a925 | 5306 | return -ENOTSOCK; |
fddaface | 5307 | |
7a7cacba PB |
5308 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter); |
5309 | if (unlikely(ret)) | |
14db8411 | 5310 | return ret; |
fddaface | 5311 | |
7a7cacba PB |
5312 | msg.msg_name = NULL; |
5313 | msg.msg_control = NULL; | |
5314 | msg.msg_controllen = 0; | |
5315 | msg.msg_namelen = 0; | |
fddaface | 5316 | |
04411806 PB |
5317 | flags = req->sr_msg.msg_flags; |
5318 | if (issue_flags & IO_URING_F_NONBLOCK) | |
7a7cacba | 5319 | flags |= MSG_DONTWAIT; |
0031275d SM |
5320 | if (flags & MSG_WAITALL) |
5321 | min_ret = iov_iter_count(&msg.msg_iter); | |
5322 | ||
7a7cacba PB |
5323 | msg.msg_flags = flags; |
5324 | ret = sock_sendmsg(sock, &msg); | |
7297ce3d PB |
5325 | if (ret < min_ret) { |
5326 | if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK)) | |
5327 | return -EAGAIN; | |
5328 | if (ret == -ERESTARTSYS) | |
5329 | ret = -EINTR; | |
93d2bcd2 | 5330 | req_set_fail(req); |
7297ce3d | 5331 | } |
889fca73 | 5332 | __io_req_complete(req, issue_flags, ret, 0); |
fddaface | 5333 | return 0; |
fddaface JA |
5334 | } |
5335 | ||
1400e697 PB |
5336 | static int __io_recvmsg_copy_hdr(struct io_kiocb *req, |
5337 | struct io_async_msghdr *iomsg) | |
52de1fe1 JA |
5338 | { |
5339 | struct io_sr_msg *sr = &req->sr_msg; | |
5340 | struct iovec __user *uiov; | |
5341 | size_t iov_len; | |
5342 | int ret; | |
5343 | ||
1400e697 PB |
5344 | ret = __copy_msghdr_from_user(&iomsg->msg, sr->umsg, |
5345 | &iomsg->uaddr, &uiov, &iov_len); | |
52de1fe1 JA |
5346 | if (ret) |
5347 | return ret; | |
5348 | ||
5349 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5350 | if (iov_len > 1) | |
5351 | return -EINVAL; | |
5476dfed | 5352 | if (copy_from_user(iomsg->fast_iov, uiov, sizeof(*uiov))) |
52de1fe1 | 5353 | return -EFAULT; |
5476dfed | 5354 | sr->len = iomsg->fast_iov[0].iov_len; |
257e84a5 | 5355 | iomsg->free_iov = NULL; |
52de1fe1 | 5356 | } else { |
257e84a5 | 5357 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5358 | ret = __import_iovec(READ, uiov, iov_len, UIO_FASTIOV, |
257e84a5 | 5359 | &iomsg->free_iov, &iomsg->msg.msg_iter, |
89cd35c5 | 5360 | false); |
52de1fe1 JA |
5361 | if (ret > 0) |
5362 | ret = 0; | |
5363 | } | |
5364 | ||
5365 | return ret; | |
5366 | } | |
5367 | ||
5368 | #ifdef CONFIG_COMPAT | |
5369 | static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req, | |
1400e697 | 5370 | struct io_async_msghdr *iomsg) |
52de1fe1 | 5371 | { |
52de1fe1 JA |
5372 | struct io_sr_msg *sr = &req->sr_msg; |
5373 | struct compat_iovec __user *uiov; | |
5374 | compat_uptr_t ptr; | |
5375 | compat_size_t len; | |
5376 | int ret; | |
5377 | ||
4af3417a PB |
5378 | ret = __get_compat_msghdr(&iomsg->msg, sr->umsg_compat, &iomsg->uaddr, |
5379 | &ptr, &len); | |
52de1fe1 JA |
5380 | if (ret) |
5381 | return ret; | |
5382 | ||
5383 | uiov = compat_ptr(ptr); | |
5384 | if (req->flags & REQ_F_BUFFER_SELECT) { | |
5385 | compat_ssize_t clen; | |
5386 | ||
5387 | if (len > 1) | |
5388 | return -EINVAL; | |
5389 | if (!access_ok(uiov, sizeof(*uiov))) | |
5390 | return -EFAULT; | |
5391 | if (__get_user(clen, &uiov->iov_len)) | |
5392 | return -EFAULT; | |
5393 | if (clen < 0) | |
5394 | return -EINVAL; | |
2d280bc8 | 5395 | sr->len = clen; |
257e84a5 | 5396 | iomsg->free_iov = NULL; |
52de1fe1 | 5397 | } else { |
257e84a5 | 5398 | iomsg->free_iov = iomsg->fast_iov; |
89cd35c5 | 5399 | ret = __import_iovec(READ, (struct iovec __user *)uiov, len, |
257e84a5 | 5400 | UIO_FASTIOV, &iomsg->free_iov, |
89cd35c5 | 5401 | &iomsg->msg.msg_iter, true); |
52de1fe1 JA |
5402 | if (ret < 0) |
5403 | return ret; | |
5404 | } | |
5405 | ||
5406 | return 0; | |
5407 | } | |
5408 | #endif | |
5409 | ||
1400e697 PB |
5410 | static int io_recvmsg_copy_hdr(struct io_kiocb *req, |
5411 | struct io_async_msghdr *iomsg) | |
52de1fe1 | 5412 | { |
1400e697 | 5413 | iomsg->msg.msg_name = &iomsg->addr; |
52de1fe1 JA |
5414 | |
5415 | #ifdef CONFIG_COMPAT | |
5416 | if (req->ctx->compat) | |
1400e697 | 5417 | return __io_compat_recvmsg_copy_hdr(req, iomsg); |
fddaface | 5418 | #endif |
52de1fe1 | 5419 | |
1400e697 | 5420 | return __io_recvmsg_copy_hdr(req, iomsg); |
52de1fe1 JA |
5421 | } |
5422 | ||
bcda7baa | 5423 | static struct io_buffer *io_recv_buffer_select(struct io_kiocb *req, |
51aac424 | 5424 | unsigned int issue_flags) |
bcda7baa JA |
5425 | { |
5426 | struct io_sr_msg *sr = &req->sr_msg; | |
bcda7baa | 5427 | |
51aac424 | 5428 | return io_buffer_select(req, &sr->len, sr->bgid, issue_flags); |
fddaface JA |
5429 | } |
5430 | ||
93642ef8 | 5431 | static int io_recvmsg_prep_async(struct io_kiocb *req) |
aa1fa28f | 5432 | { |
99bc4c38 | 5433 | int ret; |
3529d8c2 | 5434 | |
93642ef8 PB |
5435 | ret = io_recvmsg_copy_hdr(req, req->async_data); |
5436 | if (!ret) | |
5437 | req->flags |= REQ_F_NEED_CLEANUP; | |
5438 | return ret; | |
5439 | } | |
5440 | ||
5441 | static int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
5442 | { | |
5443 | struct io_sr_msg *sr = &req->sr_msg; | |
5444 | ||
d2b6f48b PB |
5445 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5446 | return -EINVAL; | |
5447 | ||
270a5940 | 5448 | sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
0b7b21e4 | 5449 | sr->len = READ_ONCE(sqe->len); |
bcda7baa | 5450 | sr->bgid = READ_ONCE(sqe->buf_group); |
04411806 PB |
5451 | sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL; |
5452 | if (sr->msg_flags & MSG_DONTWAIT) | |
5453 | req->flags |= REQ_F_NOWAIT; | |
06b76d44 | 5454 | |
d8768362 JA |
5455 | #ifdef CONFIG_COMPAT |
5456 | if (req->ctx->compat) | |
5457 | sr->msg_flags |= MSG_CMSG_COMPAT; | |
5458 | #endif | |
7ba89d2a | 5459 | sr->done_io = 0; |
93642ef8 | 5460 | return 0; |
aa1fa28f JA |
5461 | } |
5462 | ||
7ba89d2a JA |
5463 | static bool io_net_retry(struct socket *sock, int flags) |
5464 | { | |
5465 | if (!(flags & MSG_WAITALL)) | |
5466 | return false; | |
5467 | return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET; | |
5468 | } | |
5469 | ||
889fca73 | 5470 | static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags) |
aa1fa28f | 5471 | { |
6b754c8b | 5472 | struct io_async_msghdr iomsg, *kmsg; |
7ba89d2a | 5473 | struct io_sr_msg *sr = &req->sr_msg; |
03b1230c | 5474 | struct socket *sock; |
7fbb1b54 | 5475 | struct io_buffer *kbuf; |
7a7cacba | 5476 | unsigned flags; |
d1fd1c20 | 5477 | int ret, min_ret = 0; |
45d189c6 | 5478 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
03b1230c | 5479 | |
dba4a925 | 5480 | sock = sock_from_file(req->file); |
7a7cacba | 5481 | if (unlikely(!sock)) |
dba4a925 | 5482 | return -ENOTSOCK; |
3529d8c2 | 5483 | |
d886e185 PB |
5484 | if (req_has_async_data(req)) { |
5485 | kmsg = req->async_data; | |
5486 | } else { | |
7a7cacba PB |
5487 | ret = io_recvmsg_copy_hdr(req, &iomsg); |
5488 | if (ret) | |
681fda8d | 5489 | return ret; |
7a7cacba PB |
5490 | kmsg = &iomsg; |
5491 | } | |
03b1230c | 5492 | |
bc02ef33 | 5493 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5494 | kbuf = io_recv_buffer_select(req, issue_flags); |
bc02ef33 | 5495 | if (IS_ERR(kbuf)) |
52de1fe1 | 5496 | return PTR_ERR(kbuf); |
7a7cacba | 5497 | kmsg->fast_iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
5476dfed PB |
5498 | kmsg->fast_iov[0].iov_len = req->sr_msg.len; |
5499 | iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, | |
7a7cacba PB |
5500 | 1, req->sr_msg.len); |
5501 | } | |
52de1fe1 | 5502 | |
04411806 PB |
5503 | flags = req->sr_msg.msg_flags; |
5504 | if (force_nonblock) | |
7a7cacba | 5505 | flags |= MSG_DONTWAIT; |
0031275d SM |
5506 | if (flags & MSG_WAITALL) |
5507 | min_ret = iov_iter_count(&kmsg->msg.msg_iter); | |
5508 | ||
7a7cacba PB |
5509 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg, |
5510 | kmsg->uaddr, flags); | |
7297ce3d PB |
5511 | if (ret < min_ret) { |
5512 | if (ret == -EAGAIN && force_nonblock) | |
5513 | return io_setup_async_msg(req, kmsg); | |
5514 | if (ret == -ERESTARTSYS) | |
5515 | ret = -EINTR; | |
7ba89d2a JA |
5516 | if (ret > 0 && io_net_retry(sock, flags)) { |
5517 | sr->done_io += ret; | |
8a3e8ee5 | 5518 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5519 | return io_setup_async_msg(req, kmsg); |
5520 | } | |
7297ce3d PB |
5521 | req_set_fail(req); |
5522 | } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
5523 | req_set_fail(req); | |
5524 | } | |
03b1230c | 5525 | |
257e84a5 PB |
5526 | /* fast path, check for non-NULL to avoid function call */ |
5527 | if (kmsg->free_iov) | |
5528 | kfree(kmsg->free_iov); | |
99bc4c38 | 5529 | req->flags &= ~REQ_F_NEED_CLEANUP; |
7ba89d2a JA |
5530 | if (ret >= 0) |
5531 | ret += sr->done_io; | |
5532 | else if (sr->done_io) | |
5533 | ret = sr->done_io; | |
cc3cec83 | 5534 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
03b1230c | 5535 | return 0; |
0fa03c62 | 5536 | } |
5d17b4a4 | 5537 | |
889fca73 | 5538 | static int io_recv(struct io_kiocb *req, unsigned int issue_flags) |
fddaface | 5539 | { |
6b754c8b | 5540 | struct io_buffer *kbuf; |
7a7cacba PB |
5541 | struct io_sr_msg *sr = &req->sr_msg; |
5542 | struct msghdr msg; | |
5543 | void __user *buf = sr->buf; | |
fddaface | 5544 | struct socket *sock; |
7a7cacba PB |
5545 | struct iovec iov; |
5546 | unsigned flags; | |
d1fd1c20 | 5547 | int ret, min_ret = 0; |
45d189c6 | 5548 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
fddaface | 5549 | |
dba4a925 | 5550 | sock = sock_from_file(req->file); |
7a7cacba | 5551 | if (unlikely(!sock)) |
dba4a925 | 5552 | return -ENOTSOCK; |
fddaface | 5553 | |
bc02ef33 | 5554 | if (req->flags & REQ_F_BUFFER_SELECT) { |
51aac424 | 5555 | kbuf = io_recv_buffer_select(req, issue_flags); |
bcda7baa JA |
5556 | if (IS_ERR(kbuf)) |
5557 | return PTR_ERR(kbuf); | |
7a7cacba | 5558 | buf = u64_to_user_ptr(kbuf->addr); |
bc02ef33 | 5559 | } |
bcda7baa | 5560 | |
7a7cacba | 5561 | ret = import_single_range(READ, buf, sr->len, &iov, &msg.msg_iter); |
14c32eee PB |
5562 | if (unlikely(ret)) |
5563 | goto out_free; | |
fddaface | 5564 | |
7a7cacba PB |
5565 | msg.msg_name = NULL; |
5566 | msg.msg_control = NULL; | |
5567 | msg.msg_controllen = 0; | |
5568 | msg.msg_namelen = 0; | |
5569 | msg.msg_iocb = NULL; | |
5570 | msg.msg_flags = 0; | |
fddaface | 5571 | |
04411806 PB |
5572 | flags = req->sr_msg.msg_flags; |
5573 | if (force_nonblock) | |
7a7cacba | 5574 | flags |= MSG_DONTWAIT; |
0031275d SM |
5575 | if (flags & MSG_WAITALL) |
5576 | min_ret = iov_iter_count(&msg.msg_iter); | |
5577 | ||
7a7cacba | 5578 | ret = sock_recvmsg(sock, &msg, flags); |
7297ce3d PB |
5579 | if (ret < min_ret) { |
5580 | if (ret == -EAGAIN && force_nonblock) | |
5581 | return -EAGAIN; | |
5582 | if (ret == -ERESTARTSYS) | |
5583 | ret = -EINTR; | |
7ba89d2a JA |
5584 | if (ret > 0 && io_net_retry(sock, flags)) { |
5585 | sr->len -= ret; | |
5586 | sr->buf += ret; | |
5587 | sr->done_io += ret; | |
8a3e8ee5 | 5588 | req->flags |= REQ_F_PARTIAL_IO; |
7ba89d2a JA |
5589 | return -EAGAIN; |
5590 | } | |
7297ce3d PB |
5591 | req_set_fail(req); |
5592 | } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) { | |
0d7c1153 | 5593 | out_free: |
93d2bcd2 | 5594 | req_set_fail(req); |
7297ce3d | 5595 | } |
cc3cec83 | 5596 | |
7ba89d2a JA |
5597 | if (ret >= 0) |
5598 | ret += sr->done_io; | |
5599 | else if (sr->done_io) | |
5600 | ret = sr->done_io; | |
cc3cec83 | 5601 | __io_req_complete(req, issue_flags, ret, io_put_kbuf(req, issue_flags)); |
fddaface | 5602 | return 0; |
fddaface JA |
5603 | } |
5604 | ||
3529d8c2 | 5605 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 | 5606 | { |
8ed8d3c3 JA |
5607 | struct io_accept *accept = &req->accept; |
5608 | ||
14587a46 | 5609 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
17f2fe35 | 5610 | return -EINVAL; |
aaa4db12 | 5611 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
5612 | return -EINVAL; |
5613 | ||
d55e5f5b JA |
5614 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5615 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 5616 | accept->flags = READ_ONCE(sqe->accept_flags); |
09952e3e | 5617 | accept->nofile = rlimit(RLIMIT_NOFILE); |
a7083ad5 | 5618 | |
aaa4db12 | 5619 | accept->file_slot = READ_ONCE(sqe->file_index); |
adf3a9e9 | 5620 | if (accept->file_slot && (accept->flags & SOCK_CLOEXEC)) |
aaa4db12 | 5621 | return -EINVAL; |
a7083ad5 PB |
5622 | if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) |
5623 | return -EINVAL; | |
5624 | if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK)) | |
5625 | accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK; | |
8ed8d3c3 | 5626 | return 0; |
8ed8d3c3 | 5627 | } |
17f2fe35 | 5628 | |
889fca73 | 5629 | static int io_accept(struct io_kiocb *req, unsigned int issue_flags) |
8ed8d3c3 JA |
5630 | { |
5631 | struct io_accept *accept = &req->accept; | |
45d189c6 | 5632 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
ac45abc0 | 5633 | unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0; |
aaa4db12 | 5634 | bool fixed = !!accept->file_slot; |
a7083ad5 PB |
5635 | struct file *file; |
5636 | int ret, fd; | |
8ed8d3c3 | 5637 | |
aaa4db12 PB |
5638 | if (!fixed) { |
5639 | fd = __get_unused_fd_flags(accept->flags, accept->nofile); | |
5640 | if (unlikely(fd < 0)) | |
5641 | return fd; | |
5642 | } | |
a7083ad5 PB |
5643 | file = do_accept(req->file, file_flags, accept->addr, accept->addr_len, |
5644 | accept->flags); | |
5645 | if (IS_ERR(file)) { | |
aaa4db12 PB |
5646 | if (!fixed) |
5647 | put_unused_fd(fd); | |
a7083ad5 PB |
5648 | ret = PTR_ERR(file); |
5649 | if (ret == -EAGAIN && force_nonblock) | |
5650 | return -EAGAIN; | |
ac45abc0 PB |
5651 | if (ret == -ERESTARTSYS) |
5652 | ret = -EINTR; | |
93d2bcd2 | 5653 | req_set_fail(req); |
aaa4db12 | 5654 | } else if (!fixed) { |
a7083ad5 PB |
5655 | fd_install(fd, file); |
5656 | ret = fd; | |
aaa4db12 PB |
5657 | } else { |
5658 | ret = io_install_fixed_file(req, file, issue_flags, | |
5659 | accept->file_slot - 1); | |
ac45abc0 | 5660 | } |
889fca73 | 5661 | __io_req_complete(req, issue_flags, ret, 0); |
17f2fe35 | 5662 | return 0; |
8ed8d3c3 JA |
5663 | } |
5664 | ||
93642ef8 PB |
5665 | static int io_connect_prep_async(struct io_kiocb *req) |
5666 | { | |
5667 | struct io_async_connect *io = req->async_data; | |
5668 | struct io_connect *conn = &req->connect; | |
5669 | ||
5670 | return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address); | |
5671 | } | |
5672 | ||
3529d8c2 | 5673 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 | 5674 | { |
3529d8c2 | 5675 | struct io_connect *conn = &req->connect; |
f499a021 | 5676 | |
14587a46 | 5677 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3fbb51c1 | 5678 | return -EINVAL; |
26578cda PB |
5679 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags || |
5680 | sqe->splice_fd_in) | |
3fbb51c1 JA |
5681 | return -EINVAL; |
5682 | ||
3529d8c2 JA |
5683 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
5684 | conn->addr_len = READ_ONCE(sqe->addr2); | |
93642ef8 | 5685 | return 0; |
f499a021 JA |
5686 | } |
5687 | ||
889fca73 | 5688 | static int io_connect(struct io_kiocb *req, unsigned int issue_flags) |
f8e85cf2 | 5689 | { |
e8c2bc1f | 5690 | struct io_async_connect __io, *io; |
f8e85cf2 | 5691 | unsigned file_flags; |
3fbb51c1 | 5692 | int ret; |
45d189c6 | 5693 | bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK; |
f8e85cf2 | 5694 | |
d886e185 | 5695 | if (req_has_async_data(req)) { |
e8c2bc1f | 5696 | io = req->async_data; |
f499a021 | 5697 | } else { |
3529d8c2 JA |
5698 | ret = move_addr_to_kernel(req->connect.addr, |
5699 | req->connect.addr_len, | |
e8c2bc1f | 5700 | &__io.address); |
f499a021 JA |
5701 | if (ret) |
5702 | goto out; | |
5703 | io = &__io; | |
5704 | } | |
5705 | ||
3fbb51c1 JA |
5706 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
5707 | ||
e8c2bc1f | 5708 | ret = __sys_connect_file(req->file, &io->address, |
3fbb51c1 | 5709 | req->connect.addr_len, file_flags); |
87f80d62 | 5710 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
d886e185 | 5711 | if (req_has_async_data(req)) |
b7bb4f7d | 5712 | return -EAGAIN; |
e8c2bc1f | 5713 | if (io_alloc_async_data(req)) { |
f499a021 JA |
5714 | ret = -ENOMEM; |
5715 | goto out; | |
5716 | } | |
e8c2bc1f | 5717 | memcpy(req->async_data, &__io, sizeof(__io)); |
f8e85cf2 | 5718 | return -EAGAIN; |
f499a021 | 5719 | } |
f8e85cf2 JA |
5720 | if (ret == -ERESTARTSYS) |
5721 | ret = -EINTR; | |
f499a021 | 5722 | out: |
4e88d6e7 | 5723 | if (ret < 0) |
93d2bcd2 | 5724 | req_set_fail(req); |
889fca73 | 5725 | __io_req_complete(req, issue_flags, ret, 0); |
f8e85cf2 | 5726 | return 0; |
469956e8 Y |
5727 | } |
5728 | #else /* !CONFIG_NET */ | |
99a10081 JA |
5729 | #define IO_NETOP_FN(op) \ |
5730 | static int io_##op(struct io_kiocb *req, unsigned int issue_flags) \ | |
5731 | { \ | |
5732 | return -EOPNOTSUPP; \ | |
5733 | } | |
5734 | ||
5735 | #define IO_NETOP_PREP(op) \ | |
5736 | IO_NETOP_FN(op) \ | |
5737 | static int io_##op##_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) \ | |
5738 | { \ | |
5739 | return -EOPNOTSUPP; \ | |
5740 | } \ | |
5741 | ||
5742 | #define IO_NETOP_PREP_ASYNC(op) \ | |
5743 | IO_NETOP_PREP(op) \ | |
5744 | static int io_##op##_prep_async(struct io_kiocb *req) \ | |
5745 | { \ | |
5746 | return -EOPNOTSUPP; \ | |
5747 | } | |
5748 | ||
5749 | IO_NETOP_PREP_ASYNC(sendmsg); | |
5750 | IO_NETOP_PREP_ASYNC(recvmsg); | |
5751 | IO_NETOP_PREP_ASYNC(connect); | |
5752 | IO_NETOP_PREP(accept); | |
5753 | IO_NETOP_FN(send); | |
5754 | IO_NETOP_FN(recv); | |
469956e8 | 5755 | #endif /* CONFIG_NET */ |
f8e85cf2 | 5756 | |
adc8682e OL |
5757 | #ifdef CONFIG_NET_RX_BUSY_POLL |
5758 | ||
5759 | #define NAPI_TIMEOUT (60 * SEC_CONVERSION) | |
5760 | ||
5761 | struct napi_entry { | |
5762 | struct list_head list; | |
5763 | unsigned int napi_id; | |
5764 | unsigned long timeout; | |
5765 | }; | |
5766 | ||
5767 | /* | |
5768 | * Add busy poll NAPI ID from sk. | |
5769 | */ | |
5770 | static void io_add_napi(struct file *file, struct io_ring_ctx *ctx) | |
5771 | { | |
5772 | unsigned int napi_id; | |
5773 | struct socket *sock; | |
5774 | struct sock *sk; | |
5775 | struct napi_entry *ne; | |
5776 | ||
5777 | if (!net_busy_loop_on()) | |
5778 | return; | |
5779 | ||
5780 | sock = sock_from_file(file); | |
5781 | if (!sock) | |
5782 | return; | |
5783 | ||
5784 | sk = sock->sk; | |
5785 | if (!sk) | |
5786 | return; | |
5787 | ||
5788 | napi_id = READ_ONCE(sk->sk_napi_id); | |
5789 | ||
5790 | /* Non-NAPI IDs can be rejected */ | |
5791 | if (napi_id < MIN_NAPI_ID) | |
5792 | return; | |
5793 | ||
5794 | spin_lock(&ctx->napi_lock); | |
5795 | list_for_each_entry(ne, &ctx->napi_list, list) { | |
5796 | if (ne->napi_id == napi_id) { | |
5797 | ne->timeout = jiffies + NAPI_TIMEOUT; | |
5798 | goto out; | |
5799 | } | |
5800 | } | |
5801 | ||
5802 | ne = kmalloc(sizeof(*ne), GFP_NOWAIT); | |
5803 | if (!ne) | |
5804 | goto out; | |
5805 | ||
5806 | ne->napi_id = napi_id; | |
5807 | ne->timeout = jiffies + NAPI_TIMEOUT; | |
5808 | list_add_tail(&ne->list, &ctx->napi_list); | |
5809 | out: | |
5810 | spin_unlock(&ctx->napi_lock); | |
5811 | } | |
5812 | ||
5813 | static inline void io_check_napi_entry_timeout(struct napi_entry *ne) | |
5814 | { | |
5815 | if (time_after(jiffies, ne->timeout)) { | |
5816 | list_del(&ne->list); | |
5817 | kfree(ne); | |
5818 | } | |
5819 | } | |
5820 | ||
5821 | /* | |
5822 | * Busy poll if globally on and supporting sockets found | |
5823 | */ | |
5824 | static bool io_napi_busy_loop(struct list_head *napi_list) | |
5825 | { | |
5826 | struct napi_entry *ne, *n; | |
5827 | ||
5828 | list_for_each_entry_safe(ne, n, napi_list, list) { | |
5829 | napi_busy_loop(ne->napi_id, NULL, NULL, true, | |
5830 | BUSY_POLL_BUDGET); | |
5831 | io_check_napi_entry_timeout(ne); | |
5832 | } | |
5833 | return !list_empty(napi_list); | |
5834 | } | |
5835 | ||
5836 | static void io_free_napi_list(struct io_ring_ctx *ctx) | |
5837 | { | |
5838 | spin_lock(&ctx->napi_lock); | |
5839 | while (!list_empty(&ctx->napi_list)) { | |
5840 | struct napi_entry *ne = | |
5841 | list_first_entry(&ctx->napi_list, struct napi_entry, | |
5842 | list); | |
5843 | ||
5844 | list_del(&ne->list); | |
5845 | kfree(ne); | |
5846 | } | |
5847 | spin_unlock(&ctx->napi_lock); | |
5848 | } | |
5849 | #else | |
5850 | static inline void io_add_napi(struct file *file, struct io_ring_ctx *ctx) | |
5851 | { | |
5852 | } | |
5853 | ||
5854 | static inline void io_free_napi_list(struct io_ring_ctx *ctx) | |
5855 | { | |
5856 | } | |
5857 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
5858 | ||
d7718a9d JA |
5859 | struct io_poll_table { |
5860 | struct poll_table_struct pt; | |
5861 | struct io_kiocb *req; | |
68b11e8b | 5862 | int nr_entries; |
d7718a9d JA |
5863 | int error; |
5864 | }; | |
ce593a6c | 5865 | |
aa43477b | 5866 | #define IO_POLL_CANCEL_FLAG BIT(31) |
e2c0cb7c | 5867 | #define IO_POLL_REF_MASK GENMASK(30, 0) |
6d816e08 | 5868 | |
aa43477b PB |
5869 | /* |
5870 | * If refs part of ->poll_refs (see IO_POLL_REF_MASK) is 0, it's free. We can | |
5871 | * bump it and acquire ownership. It's disallowed to modify requests while not | |
5872 | * owning it, that prevents from races for enqueueing task_work's and b/w | |
5873 | * arming poll and wakeups. | |
5874 | */ | |
5875 | static inline bool io_poll_get_ownership(struct io_kiocb *req) | |
5876 | { | |
5877 | return !(atomic_fetch_inc(&req->poll_refs) & IO_POLL_REF_MASK); | |
d7718a9d JA |
5878 | } |
5879 | ||
aa43477b | 5880 | static void io_poll_mark_cancelled(struct io_kiocb *req) |
74ce6ce4 | 5881 | { |
aa43477b | 5882 | atomic_or(IO_POLL_CANCEL_FLAG, &req->poll_refs); |
74ce6ce4 JA |
5883 | } |
5884 | ||
d4e7cd36 | 5885 | static struct io_poll_iocb *io_poll_get_double(struct io_kiocb *req) |
18bceab1 | 5886 | { |
e8c2bc1f | 5887 | /* pure poll stashes this in ->async_data, poll driven retry elsewhere */ |
d4e7cd36 | 5888 | if (req->opcode == IORING_OP_POLL_ADD) |
e8c2bc1f | 5889 | return req->async_data; |
d4e7cd36 JA |
5890 | return req->apoll->double_poll; |
5891 | } | |
5892 | ||
5893 | static struct io_poll_iocb *io_poll_get_single(struct io_kiocb *req) | |
5894 | { | |
5895 | if (req->opcode == IORING_OP_POLL_ADD) | |
5896 | return &req->poll; | |
5897 | return &req->apoll->poll; | |
5898 | } | |
5899 | ||
5641897a | 5900 | static void io_poll_req_insert(struct io_kiocb *req) |
d4e7cd36 | 5901 | { |
5641897a PB |
5902 | struct io_ring_ctx *ctx = req->ctx; |
5903 | struct hlist_head *list; | |
18bceab1 | 5904 | |
5641897a PB |
5905 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; |
5906 | hlist_add_head(&req->hash_node, list); | |
18bceab1 JA |
5907 | } |
5908 | ||
5641897a PB |
5909 | static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events, |
5910 | wait_queue_func_t wake_func) | |
18bceab1 | 5911 | { |
5641897a | 5912 | poll->head = NULL; |
5641897a PB |
5913 | #define IO_POLL_UNMASK (EPOLLERR|EPOLLHUP|EPOLLNVAL|EPOLLRDHUP) |
5914 | /* mask in events that we always want/need */ | |
5915 | poll->events = events | IO_POLL_UNMASK; | |
5916 | INIT_LIST_HEAD(&poll->wait.entry); | |
5917 | init_waitqueue_func_entry(&poll->wait, wake_func); | |
18bceab1 JA |
5918 | } |
5919 | ||
aa43477b | 5920 | static inline void io_poll_remove_entry(struct io_poll_iocb *poll) |
18bceab1 | 5921 | { |
791f3465 | 5922 | struct wait_queue_head *head = smp_load_acquire(&poll->head); |
18bceab1 | 5923 | |
791f3465 PB |
5924 | if (head) { |
5925 | spin_lock_irq(&head->lock); | |
5926 | list_del_init(&poll->wait.entry); | |
5927 | poll->head = NULL; | |
5928 | spin_unlock_irq(&head->lock); | |
5929 | } | |
aa43477b | 5930 | } |
18bceab1 | 5931 | |
aa43477b PB |
5932 | static void io_poll_remove_entries(struct io_kiocb *req) |
5933 | { | |
91eac1c6 JA |
5934 | /* |
5935 | * Nothing to do if neither of those flags are set. Avoid dipping | |
5936 | * into the poll/apoll/double cachelines if we can. | |
5937 | */ | |
5938 | if (!(req->flags & (REQ_F_SINGLE_POLL | REQ_F_DOUBLE_POLL))) | |
5939 | return; | |
18bceab1 | 5940 | |
791f3465 PB |
5941 | /* |
5942 | * While we hold the waitqueue lock and the waitqueue is nonempty, | |
5943 | * wake_up_pollfree() will wait for us. However, taking the waitqueue | |
5944 | * lock in the first place can race with the waitqueue being freed. | |
5945 | * | |
5946 | * We solve this as eventpoll does: by taking advantage of the fact that | |
5947 | * all users of wake_up_pollfree() will RCU-delay the actual free. If | |
5948 | * we enter rcu_read_lock() and see that the pointer to the queue is | |
5949 | * non-NULL, we can then lock it without the memory being freed out from | |
5950 | * under us. | |
5951 | * | |
5952 | * Keep holding rcu_read_lock() as long as we hold the queue lock, in | |
5953 | * case the caller deletes the entry from the queue, leaving it empty. | |
5954 | * In that case, only RCU prevents the queue memory from being freed. | |
5955 | */ | |
5956 | rcu_read_lock(); | |
91eac1c6 JA |
5957 | if (req->flags & REQ_F_SINGLE_POLL) |
5958 | io_poll_remove_entry(io_poll_get_single(req)); | |
5959 | if (req->flags & REQ_F_DOUBLE_POLL) | |
5960 | io_poll_remove_entry(io_poll_get_double(req)); | |
791f3465 | 5961 | rcu_read_unlock(); |
18bceab1 JA |
5962 | } |
5963 | ||
aa43477b PB |
5964 | /* |
5965 | * All poll tw should go through this. Checks for poll events, manages | |
5966 | * references, does rewait, etc. | |
5967 | * | |
5968 | * Returns a negative error on failure. >0 when no action require, which is | |
5969 | * either spurious wakeup or multishot CQE is served. 0 when it's done with | |
5970 | * the request, then the mask is stored in req->result. | |
5971 | */ | |
5972 | static int io_poll_check_events(struct io_kiocb *req) | |
18bceab1 | 5973 | { |
74ce6ce4 | 5974 | struct io_ring_ctx *ctx = req->ctx; |
d4e7cd36 | 5975 | struct io_poll_iocb *poll = io_poll_get_single(req); |
aa43477b | 5976 | int v; |
18bceab1 | 5977 | |
316319e8 | 5978 | /* req->task == current here, checking PF_EXITING is safe */ |
e09ee510 | 5979 | if (unlikely(req->task->flags & PF_EXITING)) |
aa43477b | 5980 | io_poll_mark_cancelled(req); |
18bceab1 | 5981 | |
aa43477b PB |
5982 | do { |
5983 | v = atomic_read(&req->poll_refs); | |
74ce6ce4 | 5984 | |
aa43477b PB |
5985 | /* tw handler should be the owner, and so have some references */ |
5986 | if (WARN_ON_ONCE(!(v & IO_POLL_REF_MASK))) | |
5987 | return 0; | |
5988 | if (v & IO_POLL_CANCEL_FLAG) | |
5989 | return -ECANCELED; | |
8706e04e | 5990 | |
aa43477b | 5991 | if (!req->result) { |
81459350 | 5992 | struct poll_table_struct pt = { ._key = req->cflags }; |
18bceab1 | 5993 | |
81459350 | 5994 | req->result = vfs_poll(req->file, &pt) & req->cflags; |
c8b5e260 | 5995 | } |
74ce6ce4 | 5996 | |
aa43477b | 5997 | /* multishot, just fill an CQE and proceed */ |
81459350 | 5998 | if (req->result && !(req->cflags & EPOLLONESHOT)) { |
aa43477b PB |
5999 | __poll_t mask = mangle_poll(req->result & poll->events); |
6000 | bool filled; | |
18bceab1 | 6001 | |
aa43477b PB |
6002 | spin_lock(&ctx->completion_lock); |
6003 | filled = io_fill_cqe_aux(ctx, req->user_data, mask, | |
6004 | IORING_CQE_F_MORE); | |
6005 | io_commit_cqring(ctx); | |
6006 | spin_unlock(&ctx->completion_lock); | |
6007 | if (unlikely(!filled)) | |
6008 | return -ECANCELED; | |
6009 | io_cqring_ev_posted(ctx); | |
adc8682e | 6010 | io_add_napi(req->file, ctx); |
aa43477b PB |
6011 | } else if (req->result) { |
6012 | return 0; | |
6013 | } | |
18bceab1 | 6014 | |
aa43477b PB |
6015 | /* |
6016 | * Release all references, retry if someone tried to restart | |
6017 | * task_work while we were executing it. | |
6018 | */ | |
6019 | } while (atomic_sub_return(v & IO_POLL_REF_MASK, &req->poll_refs)); | |
18bceab1 | 6020 | |
18bceab1 JA |
6021 | return 1; |
6022 | } | |
6023 | ||
aa43477b | 6024 | static void io_poll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 | 6025 | { |
18bceab1 | 6026 | struct io_ring_ctx *ctx = req->ctx; |
aa43477b | 6027 | int ret; |
18bceab1 | 6028 | |
aa43477b PB |
6029 | ret = io_poll_check_events(req); |
6030 | if (ret > 0) | |
6031 | return; | |
6032 | ||
6033 | if (!ret) { | |
6034 | req->result = mangle_poll(req->result & req->poll.events); | |
e27414be | 6035 | } else { |
aa43477b PB |
6036 | req->result = ret; |
6037 | req_set_fail(req); | |
a62682f9 | 6038 | } |
aa43477b PB |
6039 | |
6040 | io_poll_remove_entries(req); | |
6041 | spin_lock(&ctx->completion_lock); | |
6042 | hash_del(&req->hash_node); | |
6043 | __io_req_complete_post(req, req->result, 0); | |
6044 | io_commit_cqring(ctx); | |
6045 | spin_unlock(&ctx->completion_lock); | |
6046 | io_cqring_ev_posted(ctx); | |
18bceab1 JA |
6047 | } |
6048 | ||
aa43477b | 6049 | static void io_apoll_task_func(struct io_kiocb *req, bool *locked) |
18bceab1 JA |
6050 | { |
6051 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6052 | int ret; |
18bceab1 | 6053 | |
aa43477b PB |
6054 | ret = io_poll_check_events(req); |
6055 | if (ret > 0) | |
6056 | return; | |
18bceab1 | 6057 | |
aa43477b PB |
6058 | io_poll_remove_entries(req); |
6059 | spin_lock(&ctx->completion_lock); | |
6060 | hash_del(&req->hash_node); | |
6061 | spin_unlock(&ctx->completion_lock); | |
18bceab1 | 6062 | |
aa43477b PB |
6063 | if (!ret) |
6064 | io_req_task_submit(req, locked); | |
6065 | else | |
6066 | io_req_complete_failed(req, ret); | |
18bceab1 JA |
6067 | } |
6068 | ||
81459350 | 6069 | static void __io_poll_execute(struct io_kiocb *req, int mask, int events) |
aa43477b PB |
6070 | { |
6071 | req->result = mask; | |
81459350 JA |
6072 | /* |
6073 | * This is useful for poll that is armed on behalf of another | |
6074 | * request, and where the wakeup path could be on a different | |
6075 | * CPU. We want to avoid pulling in req->apoll->events for that | |
6076 | * case. | |
6077 | */ | |
6078 | req->cflags = events; | |
aa43477b PB |
6079 | if (req->opcode == IORING_OP_POLL_ADD) |
6080 | req->io_task_work.func = io_poll_task_func; | |
6081 | else | |
6082 | req->io_task_work.func = io_apoll_task_func; | |
6083 | ||
502c87d6 | 6084 | trace_io_uring_task_add(req->ctx, req, req->user_data, req->opcode, mask); |
aa43477b PB |
6085 | io_req_task_work_add(req, false); |
6086 | } | |
6087 | ||
81459350 | 6088 | static inline void io_poll_execute(struct io_kiocb *req, int res, int events) |
aa43477b PB |
6089 | { |
6090 | if (io_poll_get_ownership(req)) | |
81459350 | 6091 | __io_poll_execute(req, res, events); |
aa43477b PB |
6092 | } |
6093 | ||
6094 | static void io_poll_cancel_req(struct io_kiocb *req) | |
6095 | { | |
6096 | io_poll_mark_cancelled(req); | |
6097 | /* kick tw, which should complete the request */ | |
81459350 | 6098 | io_poll_execute(req, 0, 0); |
aa43477b PB |
6099 | } |
6100 | ||
d89a4fac JA |
6101 | #define wqe_to_req(wait) ((void *)((unsigned long) (wait)->private & ~1)) |
6102 | #define wqe_is_double(wait) ((unsigned long) (wait)->private & 1) | |
6103 | ||
aa43477b PB |
6104 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
6105 | void *key) | |
18bceab1 | 6106 | { |
d89a4fac | 6107 | struct io_kiocb *req = wqe_to_req(wait); |
aa43477b PB |
6108 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
6109 | wait); | |
18bceab1 JA |
6110 | __poll_t mask = key_to_poll(key); |
6111 | ||
791f3465 PB |
6112 | if (unlikely(mask & POLLFREE)) { |
6113 | io_poll_mark_cancelled(req); | |
6114 | /* we have to kick tw in case it's not already */ | |
81459350 | 6115 | io_poll_execute(req, 0, poll->events); |
791f3465 PB |
6116 | |
6117 | /* | |
6118 | * If the waitqueue is being freed early but someone is already | |
6119 | * holds ownership over it, we have to tear down the request as | |
6120 | * best we can. That means immediately removing the request from | |
6121 | * its waitqueue and preventing all further accesses to the | |
6122 | * waitqueue via the request. | |
6123 | */ | |
6124 | list_del_init(&poll->wait.entry); | |
6125 | ||
6126 | /* | |
6127 | * Careful: this *must* be the last step, since as soon | |
6128 | * as req->head is NULL'ed out, the request can be | |
6129 | * completed and freed, since aio_poll_complete_work() | |
6130 | * will no longer need to take the waitqueue lock. | |
6131 | */ | |
6132 | smp_store_release(&poll->head, NULL); | |
6133 | return 1; | |
6134 | } | |
6135 | ||
aa43477b | 6136 | /* for instances that support it check for an event match first */ |
18bceab1 JA |
6137 | if (mask && !(mask & poll->events)) |
6138 | return 0; | |
6139 | ||
eb0089d6 PB |
6140 | if (io_poll_get_ownership(req)) { |
6141 | /* optional, saves extra locking for removal in tw handler */ | |
6142 | if (mask && poll->events & EPOLLONESHOT) { | |
6143 | list_del_init(&poll->wait.entry); | |
6144 | poll->head = NULL; | |
d89a4fac JA |
6145 | if (wqe_is_double(wait)) |
6146 | req->flags &= ~REQ_F_DOUBLE_POLL; | |
6147 | else | |
6148 | req->flags &= ~REQ_F_SINGLE_POLL; | |
eb0089d6 | 6149 | } |
81459350 | 6150 | __io_poll_execute(req, mask, poll->events); |
eb0089d6 | 6151 | } |
18bceab1 | 6152 | return 1; |
18bceab1 JA |
6153 | } |
6154 | ||
6155 | static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, | |
807abcb0 JA |
6156 | struct wait_queue_head *head, |
6157 | struct io_poll_iocb **poll_ptr) | |
18bceab1 JA |
6158 | { |
6159 | struct io_kiocb *req = pt->req; | |
d89a4fac | 6160 | unsigned long wqe_private = (unsigned long) req; |
18bceab1 JA |
6161 | |
6162 | /* | |
68b11e8b PB |
6163 | * The file being polled uses multiple waitqueues for poll handling |
6164 | * (e.g. one for read, one for write). Setup a separate io_poll_iocb | |
6165 | * if this happens. | |
18bceab1 | 6166 | */ |
68b11e8b | 6167 | if (unlikely(pt->nr_entries)) { |
aa43477b | 6168 | struct io_poll_iocb *first = poll; |
58852d4d | 6169 | |
23a65db8 | 6170 | /* double add on the same waitqueue head, ignore */ |
aa43477b | 6171 | if (first->head == head) |
23a65db8 | 6172 | return; |
18bceab1 | 6173 | /* already have a 2nd entry, fail a third attempt */ |
807abcb0 | 6174 | if (*poll_ptr) { |
23a65db8 PB |
6175 | if ((*poll_ptr)->head == head) |
6176 | return; | |
18bceab1 JA |
6177 | pt->error = -EINVAL; |
6178 | return; | |
6179 | } | |
aa43477b | 6180 | |
18bceab1 JA |
6181 | poll = kmalloc(sizeof(*poll), GFP_ATOMIC); |
6182 | if (!poll) { | |
6183 | pt->error = -ENOMEM; | |
6184 | return; | |
6185 | } | |
d89a4fac JA |
6186 | /* mark as double wq entry */ |
6187 | wqe_private |= 1; | |
91eac1c6 | 6188 | req->flags |= REQ_F_DOUBLE_POLL; |
aa43477b | 6189 | io_init_poll_iocb(poll, first->events, first->wait.func); |
807abcb0 | 6190 | *poll_ptr = poll; |
d886e185 PB |
6191 | if (req->opcode == IORING_OP_POLL_ADD) |
6192 | req->flags |= REQ_F_ASYNC_DATA; | |
18bceab1 JA |
6193 | } |
6194 | ||
91eac1c6 | 6195 | req->flags |= REQ_F_SINGLE_POLL; |
68b11e8b | 6196 | pt->nr_entries++; |
18bceab1 | 6197 | poll->head = head; |
d89a4fac | 6198 | poll->wait.private = (void *) wqe_private; |
a31eb4a2 JX |
6199 | |
6200 | if (poll->events & EPOLLEXCLUSIVE) | |
6201 | add_wait_queue_exclusive(head, &poll->wait); | |
6202 | else | |
6203 | add_wait_queue(head, &poll->wait); | |
18bceab1 JA |
6204 | } |
6205 | ||
aa43477b | 6206 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
18bceab1 JA |
6207 | struct poll_table_struct *p) |
6208 | { | |
6209 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
d7718a9d | 6210 | |
aa43477b PB |
6211 | __io_queue_proc(&pt->req->poll, pt, head, |
6212 | (struct io_poll_iocb **) &pt->req->async_data); | |
d7718a9d JA |
6213 | } |
6214 | ||
aa43477b PB |
6215 | static int __io_arm_poll_handler(struct io_kiocb *req, |
6216 | struct io_poll_iocb *poll, | |
6217 | struct io_poll_table *ipt, __poll_t mask) | |
d7718a9d JA |
6218 | { |
6219 | struct io_ring_ctx *ctx = req->ctx; | |
aa43477b | 6220 | int v; |
d7718a9d | 6221 | |
4d52f338 | 6222 | INIT_HLIST_NODE(&req->hash_node); |
aa43477b | 6223 | io_init_poll_iocb(poll, mask, io_poll_wake); |
b90cd197 | 6224 | poll->file = req->file; |
d7718a9d JA |
6225 | |
6226 | ipt->pt._key = mask; | |
6227 | ipt->req = req; | |
68b11e8b PB |
6228 | ipt->error = 0; |
6229 | ipt->nr_entries = 0; | |
d7718a9d | 6230 | |
aa43477b PB |
6231 | /* |
6232 | * Take the ownership to delay any tw execution up until we're done | |
6233 | * with poll arming. see io_poll_get_ownership(). | |
6234 | */ | |
6235 | atomic_set(&req->poll_refs, 1); | |
d7718a9d | 6236 | mask = vfs_poll(req->file, &ipt->pt) & poll->events; |
aa43477b PB |
6237 | |
6238 | if (mask && (poll->events & EPOLLONESHOT)) { | |
6239 | io_poll_remove_entries(req); | |
6240 | /* no one else has access to the req, forget about the ref */ | |
6241 | return mask; | |
6242 | } | |
6243 | if (!mask && unlikely(ipt->error || !ipt->nr_entries)) { | |
6244 | io_poll_remove_entries(req); | |
6245 | if (!ipt->error) | |
6246 | ipt->error = -EINVAL; | |
6247 | return 0; | |
6248 | } | |
d7718a9d | 6249 | |
79ebeaee | 6250 | spin_lock(&ctx->completion_lock); |
aa43477b PB |
6251 | io_poll_req_insert(req); |
6252 | spin_unlock(&ctx->completion_lock); | |
6253 | ||
6254 | if (mask) { | |
6255 | /* can't multishot if failed, just queue the event we've got */ | |
6256 | if (unlikely(ipt->error || !ipt->nr_entries)) | |
6257 | poll->events |= EPOLLONESHOT; | |
81459350 | 6258 | __io_poll_execute(req, mask, poll->events); |
aa43477b | 6259 | return 0; |
d7718a9d | 6260 | } |
adc8682e | 6261 | io_add_napi(req->file, req->ctx); |
d7718a9d | 6262 | |
aa43477b PB |
6263 | /* |
6264 | * Release ownership. If someone tried to queue a tw while it was | |
6265 | * locked, kick it off for them. | |
6266 | */ | |
6267 | v = atomic_dec_return(&req->poll_refs); | |
6268 | if (unlikely(v & IO_POLL_REF_MASK)) | |
81459350 | 6269 | __io_poll_execute(req, 0, poll->events); |
aa43477b PB |
6270 | return 0; |
6271 | } | |
6272 | ||
6273 | static void io_async_queue_proc(struct file *file, struct wait_queue_head *head, | |
6274 | struct poll_table_struct *p) | |
6275 | { | |
6276 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
6277 | struct async_poll *apoll = pt->req->apoll; | |
6278 | ||
6279 | __io_queue_proc(&apoll->poll, pt, head, &apoll->double_poll); | |
d7718a9d JA |
6280 | } |
6281 | ||
59b735ae OL |
6282 | enum { |
6283 | IO_APOLL_OK, | |
6284 | IO_APOLL_ABORTED, | |
6285 | IO_APOLL_READY | |
6286 | }; | |
6287 | ||
4d9237e3 | 6288 | static int io_arm_poll_handler(struct io_kiocb *req, unsigned issue_flags) |
d7718a9d JA |
6289 | { |
6290 | const struct io_op_def *def = &io_op_defs[req->opcode]; | |
6291 | struct io_ring_ctx *ctx = req->ctx; | |
6292 | struct async_poll *apoll; | |
6293 | struct io_poll_table ipt; | |
aa43477b PB |
6294 | __poll_t mask = EPOLLONESHOT | POLLERR | POLLPRI; |
6295 | int ret; | |
d7718a9d | 6296 | |
b2d9c3da PB |
6297 | if (!def->pollin && !def->pollout) |
6298 | return IO_APOLL_ABORTED; | |
658d0a40 PB |
6299 | if (!file_can_poll(req->file) || (req->flags & REQ_F_POLLED)) |
6300 | return IO_APOLL_ABORTED; | |
b2d9c3da PB |
6301 | |
6302 | if (def->pollin) { | |
b2d9c3da PB |
6303 | mask |= POLLIN | POLLRDNORM; |
6304 | ||
6305 | /* If reading from MSG_ERRQUEUE using recvmsg, ignore POLLIN */ | |
6306 | if ((req->opcode == IORING_OP_RECVMSG) && | |
6307 | (req->sr_msg.msg_flags & MSG_ERRQUEUE)) | |
6308 | mask &= ~POLLIN; | |
6309 | } else { | |
b2d9c3da PB |
6310 | mask |= POLLOUT | POLLWRNORM; |
6311 | } | |
52dd8640 DY |
6312 | if (def->poll_exclusive) |
6313 | mask |= EPOLLEXCLUSIVE; | |
4d9237e3 JA |
6314 | if (!(issue_flags & IO_URING_F_UNLOCKED) && |
6315 | !list_empty(&ctx->apoll_cache)) { | |
6316 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
6317 | poll.wait.entry); | |
6318 | list_del_init(&apoll->poll.wait.entry); | |
6319 | } else { | |
6320 | apoll = kmalloc(sizeof(*apoll), GFP_ATOMIC); | |
6321 | if (unlikely(!apoll)) | |
6322 | return IO_APOLL_ABORTED; | |
6323 | } | |
807abcb0 | 6324 | apoll->double_poll = NULL; |
d7718a9d | 6325 | req->apoll = apoll; |
b2d9c3da | 6326 | req->flags |= REQ_F_POLLED; |
d7718a9d JA |
6327 | ipt.pt._qproc = io_async_queue_proc; |
6328 | ||
4d55f238 | 6329 | io_kbuf_recycle(req, issue_flags); |
abdad709 | 6330 | |
aa43477b | 6331 | ret = __io_arm_poll_handler(req, &apoll->poll, &ipt, mask); |
41a5169c HX |
6332 | if (ret || ipt.error) |
6333 | return ret ? IO_APOLL_READY : IO_APOLL_ABORTED; | |
6334 | ||
502c87d6 | 6335 | trace_io_uring_poll_arm(ctx, req, req->user_data, req->opcode, |
236daeae | 6336 | mask, apoll->poll.events); |
59b735ae | 6337 | return IO_APOLL_OK; |
d7718a9d JA |
6338 | } |
6339 | ||
76e1b642 JA |
6340 | /* |
6341 | * Returns true if we found and killed one or more poll requests | |
6342 | */ | |
c072481d PB |
6343 | static __cold bool io_poll_remove_all(struct io_ring_ctx *ctx, |
6344 | struct task_struct *tsk, bool cancel_all) | |
221c5eb2 | 6345 | { |
78076bb6 | 6346 | struct hlist_node *tmp; |
221c5eb2 | 6347 | struct io_kiocb *req; |
aa43477b PB |
6348 | bool found = false; |
6349 | int i; | |
221c5eb2 | 6350 | |
79ebeaee | 6351 | spin_lock(&ctx->completion_lock); |
78076bb6 JA |
6352 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
6353 | struct hlist_head *list; | |
6354 | ||
6355 | list = &ctx->cancel_hash[i]; | |
f3606e3a | 6356 | hlist_for_each_entry_safe(req, tmp, list, hash_node) { |
42a7b4ed | 6357 | if (io_match_task_safe(req, tsk, cancel_all)) { |
61bc84c4 | 6358 | hlist_del_init(&req->hash_node); |
aa43477b PB |
6359 | io_poll_cancel_req(req); |
6360 | found = true; | |
6361 | } | |
f3606e3a | 6362 | } |
221c5eb2 | 6363 | } |
79ebeaee | 6364 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6365 | return found; |
221c5eb2 JA |
6366 | } |
6367 | ||
9ba5fac8 PB |
6368 | static struct io_kiocb *io_poll_find(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6369 | bool poll_only) | |
e07785b0 | 6370 | __must_hold(&ctx->completion_lock) |
47f46768 | 6371 | { |
78076bb6 | 6372 | struct hlist_head *list; |
47f46768 JA |
6373 | struct io_kiocb *req; |
6374 | ||
78076bb6 JA |
6375 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
6376 | hlist_for_each_entry(req, list, hash_node) { | |
b41e9852 JA |
6377 | if (sqe_addr != req->user_data) |
6378 | continue; | |
9ba5fac8 PB |
6379 | if (poll_only && req->opcode != IORING_OP_POLL_ADD) |
6380 | continue; | |
b2cb805f | 6381 | return req; |
47f46768 | 6382 | } |
b2cb805f JA |
6383 | return NULL; |
6384 | } | |
6385 | ||
aa43477b PB |
6386 | static bool io_poll_disarm(struct io_kiocb *req) |
6387 | __must_hold(&ctx->completion_lock) | |
6388 | { | |
6389 | if (!io_poll_get_ownership(req)) | |
6390 | return false; | |
6391 | io_poll_remove_entries(req); | |
6392 | hash_del(&req->hash_node); | |
6393 | return true; | |
6394 | } | |
6395 | ||
9ba5fac8 PB |
6396 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr, |
6397 | bool poll_only) | |
e07785b0 | 6398 | __must_hold(&ctx->completion_lock) |
b2cb805f | 6399 | { |
aa43477b | 6400 | struct io_kiocb *req = io_poll_find(ctx, sqe_addr, poll_only); |
b2cb805f | 6401 | |
b2cb805f JA |
6402 | if (!req) |
6403 | return -ENOENT; | |
aa43477b PB |
6404 | io_poll_cancel_req(req); |
6405 | return 0; | |
47f46768 JA |
6406 | } |
6407 | ||
9096af3e PB |
6408 | static __poll_t io_poll_parse_events(const struct io_uring_sqe *sqe, |
6409 | unsigned int flags) | |
6410 | { | |
6411 | u32 events; | |
47f46768 | 6412 | |
9096af3e PB |
6413 | events = READ_ONCE(sqe->poll32_events); |
6414 | #ifdef __BIG_ENDIAN | |
6415 | events = swahw32(events); | |
6416 | #endif | |
6417 | if (!(flags & IORING_POLL_ADD_MULTI)) | |
6418 | events |= EPOLLONESHOT; | |
6419 | return demangle_poll(events) | (events & (EPOLLEXCLUSIVE|EPOLLONESHOT)); | |
47f46768 JA |
6420 | } |
6421 | ||
c5de0036 | 6422 | static int io_poll_update_prep(struct io_kiocb *req, |
3529d8c2 | 6423 | const struct io_uring_sqe *sqe) |
0969e783 | 6424 | { |
c5de0036 PB |
6425 | struct io_poll_update *upd = &req->poll_update; |
6426 | u32 flags; | |
6427 | ||
0969e783 JA |
6428 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6429 | return -EINVAL; | |
26578cda | 6430 | if (sqe->ioprio || sqe->buf_index || sqe->splice_fd_in) |
c5de0036 PB |
6431 | return -EINVAL; |
6432 | flags = READ_ONCE(sqe->len); | |
6433 | if (flags & ~(IORING_POLL_UPDATE_EVENTS | IORING_POLL_UPDATE_USER_DATA | | |
6434 | IORING_POLL_ADD_MULTI)) | |
6435 | return -EINVAL; | |
6436 | /* meaningless without update */ | |
6437 | if (flags == IORING_POLL_ADD_MULTI) | |
0969e783 JA |
6438 | return -EINVAL; |
6439 | ||
c5de0036 PB |
6440 | upd->old_user_data = READ_ONCE(sqe->addr); |
6441 | upd->update_events = flags & IORING_POLL_UPDATE_EVENTS; | |
6442 | upd->update_user_data = flags & IORING_POLL_UPDATE_USER_DATA; | |
221c5eb2 | 6443 | |
c5de0036 PB |
6444 | upd->new_user_data = READ_ONCE(sqe->off); |
6445 | if (!upd->update_user_data && upd->new_user_data) | |
6446 | return -EINVAL; | |
6447 | if (upd->update_events) | |
6448 | upd->events = io_poll_parse_events(sqe, flags); | |
6449 | else if (sqe->poll32_events) | |
6450 | return -EINVAL; | |
221c5eb2 | 6451 | |
221c5eb2 JA |
6452 | return 0; |
6453 | } | |
6454 | ||
3529d8c2 | 6455 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
6456 | { |
6457 | struct io_poll_iocb *poll = &req->poll; | |
c5de0036 | 6458 | u32 flags; |
221c5eb2 JA |
6459 | |
6460 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
6461 | return -EINVAL; | |
c5de0036 | 6462 | if (sqe->ioprio || sqe->buf_index || sqe->off || sqe->addr) |
88e41cf9 JA |
6463 | return -EINVAL; |
6464 | flags = READ_ONCE(sqe->len); | |
c5de0036 | 6465 | if (flags & ~IORING_POLL_ADD_MULTI) |
221c5eb2 | 6466 | return -EINVAL; |
04c76b41 PB |
6467 | if ((flags & IORING_POLL_ADD_MULTI) && (req->flags & REQ_F_CQE_SKIP)) |
6468 | return -EINVAL; | |
221c5eb2 | 6469 | |
48dcd38d | 6470 | io_req_set_refcount(req); |
81459350 | 6471 | req->cflags = poll->events = io_poll_parse_events(sqe, flags); |
0969e783 JA |
6472 | return 0; |
6473 | } | |
6474 | ||
61e98203 | 6475 | static int io_poll_add(struct io_kiocb *req, unsigned int issue_flags) |
0969e783 JA |
6476 | { |
6477 | struct io_poll_iocb *poll = &req->poll; | |
0969e783 | 6478 | struct io_poll_table ipt; |
aa43477b | 6479 | int ret; |
0969e783 | 6480 | |
d7718a9d | 6481 | ipt.pt._qproc = io_poll_queue_proc; |
36703247 | 6482 | |
aa43477b PB |
6483 | ret = __io_arm_poll_handler(req, &req->poll, &ipt, poll->events); |
6484 | ret = ret ?: ipt.error; | |
6485 | if (ret) | |
6486 | __io_req_complete(req, issue_flags, ret, 0); | |
6487 | return 0; | |
221c5eb2 JA |
6488 | } |
6489 | ||
c5de0036 | 6490 | static int io_poll_update(struct io_kiocb *req, unsigned int issue_flags) |
b69de288 JA |
6491 | { |
6492 | struct io_ring_ctx *ctx = req->ctx; | |
6493 | struct io_kiocb *preq; | |
2bbb146d | 6494 | int ret2, ret = 0; |
cc8e9ba7 | 6495 | bool locked; |
b69de288 | 6496 | |
79ebeaee | 6497 | spin_lock(&ctx->completion_lock); |
9ba5fac8 | 6498 | preq = io_poll_find(ctx, req->poll_update.old_user_data, true); |
aa43477b | 6499 | if (!preq || !io_poll_disarm(preq)) { |
79ebeaee | 6500 | spin_unlock(&ctx->completion_lock); |
aa43477b | 6501 | ret = preq ? -EALREADY : -ENOENT; |
2bbb146d | 6502 | goto out; |
b69de288 | 6503 | } |
79ebeaee | 6504 | spin_unlock(&ctx->completion_lock); |
cb3b200e | 6505 | |
2bbb146d PB |
6506 | if (req->poll_update.update_events || req->poll_update.update_user_data) { |
6507 | /* only mask one event flags, keep behavior flags */ | |
6508 | if (req->poll_update.update_events) { | |
6509 | preq->poll.events &= ~0xffff; | |
6510 | preq->poll.events |= req->poll_update.events & 0xffff; | |
6511 | preq->poll.events |= IO_POLL_UNMASK; | |
cb3b200e | 6512 | } |
2bbb146d PB |
6513 | if (req->poll_update.update_user_data) |
6514 | preq->user_data = req->poll_update.new_user_data; | |
b69de288 | 6515 | |
2bbb146d PB |
6516 | ret2 = io_poll_add(preq, issue_flags); |
6517 | /* successfully updated, don't complete poll request */ | |
6518 | if (!ret2) | |
6519 | goto out; | |
b69de288 | 6520 | } |
6224590d | 6521 | |
2bbb146d | 6522 | req_set_fail(preq); |
cc8e9ba7 PB |
6523 | preq->result = -ECANCELED; |
6524 | locked = !(issue_flags & IO_URING_F_UNLOCKED); | |
6525 | io_req_task_complete(preq, &locked); | |
2bbb146d PB |
6526 | out: |
6527 | if (ret < 0) | |
6224590d | 6528 | req_set_fail(req); |
2bbb146d | 6529 | /* complete update request, we're done with it */ |
cc8e9ba7 | 6530 | __io_req_complete(req, issue_flags, ret, 0); |
b69de288 | 6531 | return 0; |
89850fce JA |
6532 | } |
6533 | ||
5262f567 JA |
6534 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
6535 | { | |
ad8a48ac JA |
6536 | struct io_timeout_data *data = container_of(timer, |
6537 | struct io_timeout_data, timer); | |
6538 | struct io_kiocb *req = data->req; | |
6539 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
6540 | unsigned long flags; |
6541 | ||
89850fce | 6542 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
a71976f3 | 6543 | list_del_init(&req->timeout.list); |
01cec8c1 PB |
6544 | atomic_set(&req->ctx->cq_timeouts, |
6545 | atomic_read(&req->ctx->cq_timeouts) + 1); | |
89850fce | 6546 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); |
01cec8c1 | 6547 | |
a90c8bf6 PB |
6548 | if (!(data->flags & IORING_TIMEOUT_ETIME_SUCCESS)) |
6549 | req_set_fail(req); | |
6550 | ||
6551 | req->result = -ETIME; | |
6552 | req->io_task_work.func = io_req_task_complete; | |
4813c377 | 6553 | io_req_task_work_add(req, false); |
5262f567 JA |
6554 | return HRTIMER_NORESTART; |
6555 | } | |
6556 | ||
fbd15848 PB |
6557 | static struct io_kiocb *io_timeout_extract(struct io_ring_ctx *ctx, |
6558 | __u64 user_data) | |
89850fce | 6559 | __must_hold(&ctx->timeout_lock) |
f254ac04 | 6560 | { |
fbd15848 | 6561 | struct io_timeout_data *io; |
47f46768 | 6562 | struct io_kiocb *req; |
fd9c7bc5 | 6563 | bool found = false; |
f254ac04 | 6564 | |
135fcde8 | 6565 | list_for_each_entry(req, &ctx->timeout_list, timeout.list) { |
fd9c7bc5 PB |
6566 | found = user_data == req->user_data; |
6567 | if (found) | |
47f46768 | 6568 | break; |
47f46768 | 6569 | } |
fd9c7bc5 PB |
6570 | if (!found) |
6571 | return ERR_PTR(-ENOENT); | |
fbd15848 PB |
6572 | |
6573 | io = req->async_data; | |
fd9c7bc5 | 6574 | if (hrtimer_try_to_cancel(&io->timer) == -1) |
fbd15848 | 6575 | return ERR_PTR(-EALREADY); |
a71976f3 | 6576 | list_del_init(&req->timeout.list); |
fbd15848 PB |
6577 | return req; |
6578 | } | |
47f46768 | 6579 | |
fbd15848 | 6580 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
ec3c3d0f | 6581 | __must_hold(&ctx->completion_lock) |
89850fce | 6582 | __must_hold(&ctx->timeout_lock) |
fbd15848 PB |
6583 | { |
6584 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); | |
6585 | ||
6586 | if (IS_ERR(req)) | |
6587 | return PTR_ERR(req); | |
6695490d | 6588 | io_req_task_queue_fail(req, -ECANCELED); |
f254ac04 JA |
6589 | return 0; |
6590 | } | |
6591 | ||
50c1df2b JA |
6592 | static clockid_t io_timeout_get_clock(struct io_timeout_data *data) |
6593 | { | |
6594 | switch (data->flags & IORING_TIMEOUT_CLOCK_MASK) { | |
6595 | case IORING_TIMEOUT_BOOTTIME: | |
6596 | return CLOCK_BOOTTIME; | |
6597 | case IORING_TIMEOUT_REALTIME: | |
6598 | return CLOCK_REALTIME; | |
6599 | default: | |
6600 | /* can't happen, vetted at prep time */ | |
6601 | WARN_ON_ONCE(1); | |
6602 | fallthrough; | |
6603 | case 0: | |
6604 | return CLOCK_MONOTONIC; | |
6605 | } | |
6606 | } | |
6607 | ||
f1042b6c PB |
6608 | static int io_linked_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6609 | struct timespec64 *ts, enum hrtimer_mode mode) | |
6610 | __must_hold(&ctx->timeout_lock) | |
6611 | { | |
6612 | struct io_timeout_data *io; | |
6613 | struct io_kiocb *req; | |
6614 | bool found = false; | |
6615 | ||
6616 | list_for_each_entry(req, &ctx->ltimeout_list, timeout.list) { | |
6617 | found = user_data == req->user_data; | |
6618 | if (found) | |
6619 | break; | |
6620 | } | |
6621 | if (!found) | |
6622 | return -ENOENT; | |
6623 | ||
6624 | io = req->async_data; | |
6625 | if (hrtimer_try_to_cancel(&io->timer) == -1) | |
6626 | return -EALREADY; | |
6627 | hrtimer_init(&io->timer, io_timeout_get_clock(io), mode); | |
6628 | io->timer.function = io_link_timeout_fn; | |
6629 | hrtimer_start(&io->timer, timespec64_to_ktime(*ts), mode); | |
6630 | return 0; | |
6631 | } | |
6632 | ||
9c8e11b3 PB |
6633 | static int io_timeout_update(struct io_ring_ctx *ctx, __u64 user_data, |
6634 | struct timespec64 *ts, enum hrtimer_mode mode) | |
89850fce | 6635 | __must_hold(&ctx->timeout_lock) |
47f46768 | 6636 | { |
9c8e11b3 PB |
6637 | struct io_kiocb *req = io_timeout_extract(ctx, user_data); |
6638 | struct io_timeout_data *data; | |
47f46768 | 6639 | |
9c8e11b3 PB |
6640 | if (IS_ERR(req)) |
6641 | return PTR_ERR(req); | |
47f46768 | 6642 | |
9c8e11b3 PB |
6643 | req->timeout.off = 0; /* noseq */ |
6644 | data = req->async_data; | |
6645 | list_add_tail(&req->timeout.list, &ctx->timeout_list); | |
50c1df2b | 6646 | hrtimer_init(&data->timer, io_timeout_get_clock(data), mode); |
9c8e11b3 PB |
6647 | data->timer.function = io_timeout_fn; |
6648 | hrtimer_start(&data->timer, timespec64_to_ktime(*ts), mode); | |
6649 | return 0; | |
47f46768 JA |
6650 | } |
6651 | ||
3529d8c2 JA |
6652 | static int io_timeout_remove_prep(struct io_kiocb *req, |
6653 | const struct io_uring_sqe *sqe) | |
b29472ee | 6654 | { |
9c8e11b3 PB |
6655 | struct io_timeout_rem *tr = &req->timeout_rem; |
6656 | ||
b29472ee JA |
6657 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
6658 | return -EINVAL; | |
61710e43 DA |
6659 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6660 | return -EINVAL; | |
26578cda | 6661 | if (sqe->ioprio || sqe->buf_index || sqe->len || sqe->splice_fd_in) |
b29472ee JA |
6662 | return -EINVAL; |
6663 | ||
f1042b6c | 6664 | tr->ltimeout = false; |
9c8e11b3 PB |
6665 | tr->addr = READ_ONCE(sqe->addr); |
6666 | tr->flags = READ_ONCE(sqe->timeout_flags); | |
f1042b6c PB |
6667 | if (tr->flags & IORING_TIMEOUT_UPDATE_MASK) { |
6668 | if (hweight32(tr->flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
6669 | return -EINVAL; | |
6670 | if (tr->flags & IORING_LINK_TIMEOUT_UPDATE) | |
6671 | tr->ltimeout = true; | |
6672 | if (tr->flags & ~(IORING_TIMEOUT_UPDATE_MASK|IORING_TIMEOUT_ABS)) | |
9c8e11b3 PB |
6673 | return -EINVAL; |
6674 | if (get_timespec64(&tr->ts, u64_to_user_ptr(sqe->addr2))) | |
6675 | return -EFAULT; | |
2087009c YB |
6676 | if (tr->ts.tv_sec < 0 || tr->ts.tv_nsec < 0) |
6677 | return -EINVAL; | |
9c8e11b3 PB |
6678 | } else if (tr->flags) { |
6679 | /* timeout removal doesn't support flags */ | |
b29472ee | 6680 | return -EINVAL; |
9c8e11b3 | 6681 | } |
b29472ee | 6682 | |
b29472ee JA |
6683 | return 0; |
6684 | } | |
6685 | ||
8662daec PB |
6686 | static inline enum hrtimer_mode io_translate_timeout_mode(unsigned int flags) |
6687 | { | |
6688 | return (flags & IORING_TIMEOUT_ABS) ? HRTIMER_MODE_ABS | |
6689 | : HRTIMER_MODE_REL; | |
6690 | } | |
6691 | ||
11365043 JA |
6692 | /* |
6693 | * Remove or update an existing timeout command | |
6694 | */ | |
61e98203 | 6695 | static int io_timeout_remove(struct io_kiocb *req, unsigned int issue_flags) |
11365043 | 6696 | { |
9c8e11b3 | 6697 | struct io_timeout_rem *tr = &req->timeout_rem; |
11365043 | 6698 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 | 6699 | int ret; |
11365043 | 6700 | |
ec3c3d0f PB |
6701 | if (!(req->timeout_rem.flags & IORING_TIMEOUT_UPDATE)) { |
6702 | spin_lock(&ctx->completion_lock); | |
6703 | spin_lock_irq(&ctx->timeout_lock); | |
9c8e11b3 | 6704 | ret = io_timeout_cancel(ctx, tr->addr); |
ec3c3d0f PB |
6705 | spin_unlock_irq(&ctx->timeout_lock); |
6706 | spin_unlock(&ctx->completion_lock); | |
6707 | } else { | |
f1042b6c PB |
6708 | enum hrtimer_mode mode = io_translate_timeout_mode(tr->flags); |
6709 | ||
ec3c3d0f | 6710 | spin_lock_irq(&ctx->timeout_lock); |
f1042b6c PB |
6711 | if (tr->ltimeout) |
6712 | ret = io_linked_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
6713 | else | |
6714 | ret = io_timeout_update(ctx, tr->addr, &tr->ts, mode); | |
ec3c3d0f PB |
6715 | spin_unlock_irq(&ctx->timeout_lock); |
6716 | } | |
11365043 | 6717 | |
4e88d6e7 | 6718 | if (ret < 0) |
93d2bcd2 | 6719 | req_set_fail(req); |
505657bc | 6720 | io_req_complete_post(req, ret, 0); |
11365043 | 6721 | return 0; |
5262f567 JA |
6722 | } |
6723 | ||
3529d8c2 | 6724 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 6725 | bool is_timeout_link) |
5262f567 | 6726 | { |
ad8a48ac | 6727 | struct io_timeout_data *data; |
a41525ab | 6728 | unsigned flags; |
56080b02 | 6729 | u32 off = READ_ONCE(sqe->off); |
5262f567 | 6730 | |
ad8a48ac | 6731 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 6732 | return -EINVAL; |
26578cda PB |
6733 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || |
6734 | sqe->splice_fd_in) | |
a41525ab | 6735 | return -EINVAL; |
56080b02 | 6736 | if (off && is_timeout_link) |
2d28390a | 6737 | return -EINVAL; |
a41525ab | 6738 | flags = READ_ONCE(sqe->timeout_flags); |
6224590d PB |
6739 | if (flags & ~(IORING_TIMEOUT_ABS | IORING_TIMEOUT_CLOCK_MASK | |
6740 | IORING_TIMEOUT_ETIME_SUCCESS)) | |
50c1df2b JA |
6741 | return -EINVAL; |
6742 | /* more than one clock specified is invalid, obviously */ | |
6743 | if (hweight32(flags & IORING_TIMEOUT_CLOCK_MASK) > 1) | |
5262f567 | 6744 | return -EINVAL; |
bdf20073 | 6745 | |
ef9dd637 | 6746 | INIT_LIST_HEAD(&req->timeout.list); |
bfe68a22 | 6747 | req->timeout.off = off; |
f18ee4cf PB |
6748 | if (unlikely(off && !req->ctx->off_timeout_used)) |
6749 | req->ctx->off_timeout_used = true; | |
26a61679 | 6750 | |
d6a644a7 PB |
6751 | if (WARN_ON_ONCE(req_has_async_data(req))) |
6752 | return -EFAULT; | |
6753 | if (io_alloc_async_data(req)) | |
26a61679 JA |
6754 | return -ENOMEM; |
6755 | ||
e8c2bc1f | 6756 | data = req->async_data; |
ad8a48ac | 6757 | data->req = req; |
50c1df2b | 6758 | data->flags = flags; |
ad8a48ac JA |
6759 | |
6760 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
6761 | return -EFAULT; |
6762 | ||
f6223ff7 YB |
6763 | if (data->ts.tv_sec < 0 || data->ts.tv_nsec < 0) |
6764 | return -EINVAL; | |
6765 | ||
8662daec | 6766 | data->mode = io_translate_timeout_mode(flags); |
50c1df2b | 6767 | hrtimer_init(&data->timer, io_timeout_get_clock(data), data->mode); |
b97e736a PB |
6768 | |
6769 | if (is_timeout_link) { | |
6770 | struct io_submit_link *link = &req->ctx->submit_state.link; | |
6771 | ||
6772 | if (!link->head) | |
6773 | return -EINVAL; | |
6774 | if (link->last->opcode == IORING_OP_LINK_TIMEOUT) | |
6775 | return -EINVAL; | |
4d13d1a4 PB |
6776 | req->timeout.head = link->last; |
6777 | link->last->flags |= REQ_F_ARM_LTIMEOUT; | |
b97e736a | 6778 | } |
ad8a48ac JA |
6779 | return 0; |
6780 | } | |
6781 | ||
61e98203 | 6782 | static int io_timeout(struct io_kiocb *req, unsigned int issue_flags) |
ad8a48ac | 6783 | { |
ad8a48ac | 6784 | struct io_ring_ctx *ctx = req->ctx; |
e8c2bc1f | 6785 | struct io_timeout_data *data = req->async_data; |
ad8a48ac | 6786 | struct list_head *entry; |
bfe68a22 | 6787 | u32 tail, off = req->timeout.off; |
ad8a48ac | 6788 | |
89850fce | 6789 | spin_lock_irq(&ctx->timeout_lock); |
93bd25bb | 6790 | |
5262f567 JA |
6791 | /* |
6792 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
6793 | * timeout event to be satisfied. If it isn't set, then this is |
6794 | * a pure timeout request, sequence isn't used. | |
5262f567 | 6795 | */ |
8eb7e2d0 | 6796 | if (io_is_timeout_noseq(req)) { |
93bd25bb JA |
6797 | entry = ctx->timeout_list.prev; |
6798 | goto add; | |
6799 | } | |
5262f567 | 6800 | |
bfe68a22 PB |
6801 | tail = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
6802 | req->timeout.target_seq = tail + off; | |
5262f567 | 6803 | |
f010505b MDG |
6804 | /* Update the last seq here in case io_flush_timeouts() hasn't. |
6805 | * This is safe because ->completion_lock is held, and submissions | |
6806 | * and completions are never mixed in the same ->completion_lock section. | |
6807 | */ | |
6808 | ctx->cq_last_tm_flush = tail; | |
6809 | ||
5262f567 JA |
6810 | /* |
6811 | * Insertion sort, ensuring the first entry in the list is always | |
6812 | * the one we need first. | |
6813 | */ | |
5262f567 | 6814 | list_for_each_prev(entry, &ctx->timeout_list) { |
135fcde8 PB |
6815 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, |
6816 | timeout.list); | |
5262f567 | 6817 | |
8eb7e2d0 | 6818 | if (io_is_timeout_noseq(nxt)) |
93bd25bb | 6819 | continue; |
bfe68a22 PB |
6820 | /* nxt.seq is behind @tail, otherwise would've been completed */ |
6821 | if (off >= nxt->timeout.target_seq - tail) | |
5262f567 JA |
6822 | break; |
6823 | } | |
93bd25bb | 6824 | add: |
135fcde8 | 6825 | list_add(&req->timeout.list, entry); |
ad8a48ac JA |
6826 | data->timer.function = io_timeout_fn; |
6827 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
89850fce | 6828 | spin_unlock_irq(&ctx->timeout_lock); |
5262f567 JA |
6829 | return 0; |
6830 | } | |
5262f567 | 6831 | |
f458dd84 PB |
6832 | struct io_cancel_data { |
6833 | struct io_ring_ctx *ctx; | |
6834 | u64 user_data; | |
6835 | }; | |
6836 | ||
62755e35 JA |
6837 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
6838 | { | |
6839 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
f458dd84 | 6840 | struct io_cancel_data *cd = data; |
62755e35 | 6841 | |
f458dd84 | 6842 | return req->ctx == cd->ctx && req->user_data == cd->user_data; |
62755e35 JA |
6843 | } |
6844 | ||
f458dd84 PB |
6845 | static int io_async_cancel_one(struct io_uring_task *tctx, u64 user_data, |
6846 | struct io_ring_ctx *ctx) | |
62755e35 | 6847 | { |
f458dd84 | 6848 | struct io_cancel_data data = { .ctx = ctx, .user_data = user_data, }; |
62755e35 | 6849 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
6850 | int ret = 0; |
6851 | ||
f458dd84 | 6852 | if (!tctx || !tctx->io_wq) |
5aa75ed5 JA |
6853 | return -ENOENT; |
6854 | ||
f458dd84 | 6855 | cancel_ret = io_wq_cancel_cb(tctx->io_wq, io_cancel_cb, &data, false); |
62755e35 JA |
6856 | switch (cancel_ret) { |
6857 | case IO_WQ_CANCEL_OK: | |
6858 | ret = 0; | |
6859 | break; | |
6860 | case IO_WQ_CANCEL_RUNNING: | |
6861 | ret = -EALREADY; | |
6862 | break; | |
6863 | case IO_WQ_CANCEL_NOTFOUND: | |
6864 | ret = -ENOENT; | |
6865 | break; | |
6866 | } | |
6867 | ||
e977d6d3 JA |
6868 | return ret; |
6869 | } | |
6870 | ||
8cb01fac | 6871 | static int io_try_cancel_userdata(struct io_kiocb *req, u64 sqe_addr) |
47f46768 | 6872 | { |
8cb01fac | 6873 | struct io_ring_ctx *ctx = req->ctx; |
47f46768 JA |
6874 | int ret; |
6875 | ||
dadebc35 | 6876 | WARN_ON_ONCE(!io_wq_current_is_worker() && req->task != current); |
8cb01fac | 6877 | |
f458dd84 | 6878 | ret = io_async_cancel_one(req->task->io_uring, sqe_addr, ctx); |
ccbf7261 JA |
6879 | /* |
6880 | * Fall-through even for -EALREADY, as we may have poll armed | |
6881 | * that need unarming. | |
6882 | */ | |
6883 | if (!ret) | |
6884 | return 0; | |
505657bc PB |
6885 | |
6886 | spin_lock(&ctx->completion_lock); | |
ccbf7261 JA |
6887 | ret = io_poll_cancel(ctx, sqe_addr, false); |
6888 | if (ret != -ENOENT) | |
6889 | goto out; | |
6890 | ||
79ebeaee | 6891 | spin_lock_irq(&ctx->timeout_lock); |
47f46768 | 6892 | ret = io_timeout_cancel(ctx, sqe_addr); |
79ebeaee | 6893 | spin_unlock_irq(&ctx->timeout_lock); |
505657bc PB |
6894 | out: |
6895 | spin_unlock(&ctx->completion_lock); | |
6896 | return ret; | |
47f46768 JA |
6897 | } |
6898 | ||
3529d8c2 JA |
6899 | static int io_async_cancel_prep(struct io_kiocb *req, |
6900 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 6901 | { |
fbf23849 | 6902 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 | 6903 | return -EINVAL; |
61710e43 DA |
6904 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6905 | return -EINVAL; | |
26578cda PB |
6906 | if (sqe->ioprio || sqe->off || sqe->len || sqe->cancel_flags || |
6907 | sqe->splice_fd_in) | |
e977d6d3 JA |
6908 | return -EINVAL; |
6909 | ||
fbf23849 JA |
6910 | req->cancel.addr = READ_ONCE(sqe->addr); |
6911 | return 0; | |
6912 | } | |
6913 | ||
61e98203 | 6914 | static int io_async_cancel(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6915 | { |
6916 | struct io_ring_ctx *ctx = req->ctx; | |
58f99373 | 6917 | u64 sqe_addr = req->cancel.addr; |
3b44b371 | 6918 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
58f99373 PB |
6919 | struct io_tctx_node *node; |
6920 | int ret; | |
6921 | ||
8cb01fac | 6922 | ret = io_try_cancel_userdata(req, sqe_addr); |
58f99373 PB |
6923 | if (ret != -ENOENT) |
6924 | goto done; | |
58f99373 PB |
6925 | |
6926 | /* slow path, try all io-wq's */ | |
3b44b371 | 6927 | io_ring_submit_lock(ctx, needs_lock); |
58f99373 PB |
6928 | ret = -ENOENT; |
6929 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
6930 | struct io_uring_task *tctx = node->task->io_uring; | |
fbf23849 | 6931 | |
58f99373 PB |
6932 | ret = io_async_cancel_one(tctx, req->cancel.addr, ctx); |
6933 | if (ret != -ENOENT) | |
6934 | break; | |
6935 | } | |
3b44b371 | 6936 | io_ring_submit_unlock(ctx, needs_lock); |
58f99373 | 6937 | done: |
58f99373 | 6938 | if (ret < 0) |
93d2bcd2 | 6939 | req_set_fail(req); |
505657bc | 6940 | io_req_complete_post(req, ret, 0); |
5262f567 JA |
6941 | return 0; |
6942 | } | |
6943 | ||
269bbe5f | 6944 | static int io_rsrc_update_prep(struct io_kiocb *req, |
05f3fb3c JA |
6945 | const struct io_uring_sqe *sqe) |
6946 | { | |
61710e43 DA |
6947 | if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT))) |
6948 | return -EINVAL; | |
26578cda | 6949 | if (sqe->ioprio || sqe->rw_flags || sqe->splice_fd_in) |
05f3fb3c JA |
6950 | return -EINVAL; |
6951 | ||
269bbe5f BM |
6952 | req->rsrc_update.offset = READ_ONCE(sqe->off); |
6953 | req->rsrc_update.nr_args = READ_ONCE(sqe->len); | |
6954 | if (!req->rsrc_update.nr_args) | |
05f3fb3c | 6955 | return -EINVAL; |
269bbe5f | 6956 | req->rsrc_update.arg = READ_ONCE(sqe->addr); |
05f3fb3c JA |
6957 | return 0; |
6958 | } | |
6959 | ||
889fca73 | 6960 | static int io_files_update(struct io_kiocb *req, unsigned int issue_flags) |
fbf23849 JA |
6961 | { |
6962 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 6963 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
c3bdad02 | 6964 | struct io_uring_rsrc_update2 up; |
05f3fb3c | 6965 | int ret; |
fbf23849 | 6966 | |
269bbe5f BM |
6967 | up.offset = req->rsrc_update.offset; |
6968 | up.data = req->rsrc_update.arg; | |
c3bdad02 PB |
6969 | up.nr = 0; |
6970 | up.tags = 0; | |
615cee49 | 6971 | up.resv = 0; |
05f3fb3c | 6972 | |
3b44b371 | 6973 | io_ring_submit_lock(ctx, needs_lock); |
fdecb662 | 6974 | ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE, |
98f0b3b4 | 6975 | &up, req->rsrc_update.nr_args); |
3b44b371 | 6976 | io_ring_submit_unlock(ctx, needs_lock); |
05f3fb3c JA |
6977 | |
6978 | if (ret < 0) | |
93d2bcd2 | 6979 | req_set_fail(req); |
889fca73 | 6980 | __io_req_complete(req, issue_flags, ret, 0); |
5262f567 JA |
6981 | return 0; |
6982 | } | |
6983 | ||
bfe76559 | 6984 | static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f67676d1 | 6985 | { |
d625c6ee | 6986 | switch (req->opcode) { |
e781573e | 6987 | case IORING_OP_NOP: |
bfe76559 | 6988 | return 0; |
f67676d1 JA |
6989 | case IORING_OP_READV: |
6990 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 6991 | case IORING_OP_READ: |
bfe76559 | 6992 | return io_read_prep(req, sqe); |
f67676d1 JA |
6993 | case IORING_OP_WRITEV: |
6994 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 6995 | case IORING_OP_WRITE: |
bfe76559 | 6996 | return io_write_prep(req, sqe); |
0969e783 | 6997 | case IORING_OP_POLL_ADD: |
bfe76559 | 6998 | return io_poll_add_prep(req, sqe); |
0969e783 | 6999 | case IORING_OP_POLL_REMOVE: |
c5de0036 | 7000 | return io_poll_update_prep(req, sqe); |
8ed8d3c3 | 7001 | case IORING_OP_FSYNC: |
1155c76a | 7002 | return io_fsync_prep(req, sqe); |
8ed8d3c3 | 7003 | case IORING_OP_SYNC_FILE_RANGE: |
1155c76a | 7004 | return io_sfr_prep(req, sqe); |
03b1230c | 7005 | case IORING_OP_SENDMSG: |
fddaface | 7006 | case IORING_OP_SEND: |
bfe76559 | 7007 | return io_sendmsg_prep(req, sqe); |
03b1230c | 7008 | case IORING_OP_RECVMSG: |
fddaface | 7009 | case IORING_OP_RECV: |
bfe76559 | 7010 | return io_recvmsg_prep(req, sqe); |
f499a021 | 7011 | case IORING_OP_CONNECT: |
bfe76559 | 7012 | return io_connect_prep(req, sqe); |
2d28390a | 7013 | case IORING_OP_TIMEOUT: |
bfe76559 | 7014 | return io_timeout_prep(req, sqe, false); |
b29472ee | 7015 | case IORING_OP_TIMEOUT_REMOVE: |
bfe76559 | 7016 | return io_timeout_remove_prep(req, sqe); |
fbf23849 | 7017 | case IORING_OP_ASYNC_CANCEL: |
bfe76559 | 7018 | return io_async_cancel_prep(req, sqe); |
2d28390a | 7019 | case IORING_OP_LINK_TIMEOUT: |
bfe76559 | 7020 | return io_timeout_prep(req, sqe, true); |
8ed8d3c3 | 7021 | case IORING_OP_ACCEPT: |
bfe76559 | 7022 | return io_accept_prep(req, sqe); |
d63d1b5e | 7023 | case IORING_OP_FALLOCATE: |
bfe76559 | 7024 | return io_fallocate_prep(req, sqe); |
15b71abe | 7025 | case IORING_OP_OPENAT: |
bfe76559 | 7026 | return io_openat_prep(req, sqe); |
b5dba59e | 7027 | case IORING_OP_CLOSE: |
bfe76559 | 7028 | return io_close_prep(req, sqe); |
05f3fb3c | 7029 | case IORING_OP_FILES_UPDATE: |
269bbe5f | 7030 | return io_rsrc_update_prep(req, sqe); |
eddc7ef5 | 7031 | case IORING_OP_STATX: |
bfe76559 | 7032 | return io_statx_prep(req, sqe); |
4840e418 | 7033 | case IORING_OP_FADVISE: |
bfe76559 | 7034 | return io_fadvise_prep(req, sqe); |
c1ca757b | 7035 | case IORING_OP_MADVISE: |
bfe76559 | 7036 | return io_madvise_prep(req, sqe); |
cebdb986 | 7037 | case IORING_OP_OPENAT2: |
bfe76559 | 7038 | return io_openat2_prep(req, sqe); |
3e4827b0 | 7039 | case IORING_OP_EPOLL_CTL: |
bfe76559 | 7040 | return io_epoll_ctl_prep(req, sqe); |
7d67af2c | 7041 | case IORING_OP_SPLICE: |
bfe76559 | 7042 | return io_splice_prep(req, sqe); |
ddf0322d | 7043 | case IORING_OP_PROVIDE_BUFFERS: |
bfe76559 | 7044 | return io_provide_buffers_prep(req, sqe); |
067524e9 | 7045 | case IORING_OP_REMOVE_BUFFERS: |
bfe76559 | 7046 | return io_remove_buffers_prep(req, sqe); |
f2a8d5c7 | 7047 | case IORING_OP_TEE: |
bfe76559 | 7048 | return io_tee_prep(req, sqe); |
36f4fa68 JA |
7049 | case IORING_OP_SHUTDOWN: |
7050 | return io_shutdown_prep(req, sqe); | |
80a261fd JA |
7051 | case IORING_OP_RENAMEAT: |
7052 | return io_renameat_prep(req, sqe); | |
14a1143b JA |
7053 | case IORING_OP_UNLINKAT: |
7054 | return io_unlinkat_prep(req, sqe); | |
e34a02dc DK |
7055 | case IORING_OP_MKDIRAT: |
7056 | return io_mkdirat_prep(req, sqe); | |
7a8721f8 DK |
7057 | case IORING_OP_SYMLINKAT: |
7058 | return io_symlinkat_prep(req, sqe); | |
cf30da90 DK |
7059 | case IORING_OP_LINKAT: |
7060 | return io_linkat_prep(req, sqe); | |
4f57f06c JA |
7061 | case IORING_OP_MSG_RING: |
7062 | return io_msg_ring_prep(req, sqe); | |
f67676d1 JA |
7063 | } |
7064 | ||
bfe76559 PB |
7065 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
7066 | req->opcode); | |
bd54b6fe | 7067 | return -EINVAL; |
bfe76559 PB |
7068 | } |
7069 | ||
93642ef8 | 7070 | static int io_req_prep_async(struct io_kiocb *req) |
bfe76559 | 7071 | { |
b7e298d2 PB |
7072 | if (!io_op_defs[req->opcode].needs_async_setup) |
7073 | return 0; | |
d886e185 | 7074 | if (WARN_ON_ONCE(req_has_async_data(req))) |
b7e298d2 PB |
7075 | return -EFAULT; |
7076 | if (io_alloc_async_data(req)) | |
7077 | return -EAGAIN; | |
7078 | ||
93642ef8 PB |
7079 | switch (req->opcode) { |
7080 | case IORING_OP_READV: | |
93642ef8 PB |
7081 | return io_rw_prep_async(req, READ); |
7082 | case IORING_OP_WRITEV: | |
93642ef8 PB |
7083 | return io_rw_prep_async(req, WRITE); |
7084 | case IORING_OP_SENDMSG: | |
93642ef8 PB |
7085 | return io_sendmsg_prep_async(req); |
7086 | case IORING_OP_RECVMSG: | |
93642ef8 PB |
7087 | return io_recvmsg_prep_async(req); |
7088 | case IORING_OP_CONNECT: | |
7089 | return io_connect_prep_async(req); | |
7090 | } | |
b7e298d2 PB |
7091 | printk_once(KERN_WARNING "io_uring: prep_async() bad opcode %d\n", |
7092 | req->opcode); | |
7093 | return -EFAULT; | |
f67676d1 JA |
7094 | } |
7095 | ||
9cf7c104 PB |
7096 | static u32 io_get_sequence(struct io_kiocb *req) |
7097 | { | |
a3dbdf54 | 7098 | u32 seq = req->ctx->cached_sq_head; |
9cf7c104 | 7099 | |
a3dbdf54 PB |
7100 | /* need original cached_sq_head, but it was increased for each req */ |
7101 | io_for_each_link(req, req) | |
7102 | seq--; | |
7103 | return seq; | |
9cf7c104 PB |
7104 | } |
7105 | ||
c072481d | 7106 | static __cold void io_drain_req(struct io_kiocb *req) |
de0617e4 | 7107 | { |
a197f664 | 7108 | struct io_ring_ctx *ctx = req->ctx; |
27dc8338 | 7109 | struct io_defer_entry *de; |
f67676d1 | 7110 | int ret; |
e0eb71dc | 7111 | u32 seq = io_get_sequence(req); |
3c19966d | 7112 | |
9d858b21 | 7113 | /* Still need defer if there is pending req in defer list. */ |
e302f104 | 7114 | spin_lock(&ctx->completion_lock); |
5e371265 | 7115 | if (!req_need_defer(req, seq) && list_empty_careful(&ctx->defer_list)) { |
e302f104 | 7116 | spin_unlock(&ctx->completion_lock); |
e0eb71dc | 7117 | queue: |
10c66904 | 7118 | ctx->drain_active = false; |
e0eb71dc PB |
7119 | io_req_task_queue(req); |
7120 | return; | |
10c66904 | 7121 | } |
e302f104 | 7122 | spin_unlock(&ctx->completion_lock); |
9cf7c104 | 7123 | |
b7e298d2 | 7124 | ret = io_req_prep_async(req); |
e0eb71dc PB |
7125 | if (ret) { |
7126 | fail: | |
7127 | io_req_complete_failed(req, ret); | |
7128 | return; | |
7129 | } | |
cbdcb435 | 7130 | io_prep_async_link(req); |
27dc8338 | 7131 | de = kmalloc(sizeof(*de), GFP_KERNEL); |
76cc33d7 | 7132 | if (!de) { |
1b48773f | 7133 | ret = -ENOMEM; |
e0eb71dc | 7134 | goto fail; |
76cc33d7 | 7135 | } |
2d28390a | 7136 | |
79ebeaee | 7137 | spin_lock(&ctx->completion_lock); |
9cf7c104 | 7138 | if (!req_need_defer(req, seq) && list_empty(&ctx->defer_list)) { |
79ebeaee | 7139 | spin_unlock(&ctx->completion_lock); |
27dc8338 | 7140 | kfree(de); |
e0eb71dc | 7141 | goto queue; |
de0617e4 JA |
7142 | } |
7143 | ||
502c87d6 | 7144 | trace_io_uring_defer(ctx, req, req->user_data, req->opcode); |
27dc8338 | 7145 | de->req = req; |
9cf7c104 | 7146 | de->seq = seq; |
27dc8338 | 7147 | list_add_tail(&de->list, &ctx->defer_list); |
79ebeaee | 7148 | spin_unlock(&ctx->completion_lock); |
de0617e4 JA |
7149 | } |
7150 | ||
68fb8979 | 7151 | static void io_clean_op(struct io_kiocb *req) |
99bc4c38 | 7152 | { |
8197b053 PB |
7153 | if (req->flags & REQ_F_BUFFER_SELECTED) { |
7154 | spin_lock(&req->ctx->completion_lock); | |
cc3cec83 | 7155 | io_put_kbuf_comp(req); |
8197b053 PB |
7156 | spin_unlock(&req->ctx->completion_lock); |
7157 | } | |
99bc4c38 | 7158 | |
0e1b6fe3 PB |
7159 | if (req->flags & REQ_F_NEED_CLEANUP) { |
7160 | switch (req->opcode) { | |
7161 | case IORING_OP_READV: | |
7162 | case IORING_OP_READ_FIXED: | |
7163 | case IORING_OP_READ: | |
7164 | case IORING_OP_WRITEV: | |
7165 | case IORING_OP_WRITE_FIXED: | |
e8c2bc1f JA |
7166 | case IORING_OP_WRITE: { |
7167 | struct io_async_rw *io = req->async_data; | |
1dacb4df PB |
7168 | |
7169 | kfree(io->free_iovec); | |
0e1b6fe3 | 7170 | break; |
e8c2bc1f | 7171 | } |
0e1b6fe3 | 7172 | case IORING_OP_RECVMSG: |
e8c2bc1f JA |
7173 | case IORING_OP_SENDMSG: { |
7174 | struct io_async_msghdr *io = req->async_data; | |
257e84a5 PB |
7175 | |
7176 | kfree(io->free_iov); | |
0e1b6fe3 | 7177 | break; |
e8c2bc1f | 7178 | } |
0e1b6fe3 PB |
7179 | case IORING_OP_SPLICE: |
7180 | case IORING_OP_TEE: | |
e1d767f0 PB |
7181 | if (!(req->splice.flags & SPLICE_F_FD_IN_FIXED)) |
7182 | io_put_file(req->splice.file_in); | |
0e1b6fe3 | 7183 | break; |
f3cd4850 JA |
7184 | case IORING_OP_OPENAT: |
7185 | case IORING_OP_OPENAT2: | |
7186 | if (req->open.filename) | |
7187 | putname(req->open.filename); | |
7188 | break; | |
80a261fd JA |
7189 | case IORING_OP_RENAMEAT: |
7190 | putname(req->rename.oldpath); | |
7191 | putname(req->rename.newpath); | |
7192 | break; | |
14a1143b JA |
7193 | case IORING_OP_UNLINKAT: |
7194 | putname(req->unlink.filename); | |
7195 | break; | |
e34a02dc DK |
7196 | case IORING_OP_MKDIRAT: |
7197 | putname(req->mkdir.filename); | |
7198 | break; | |
7a8721f8 DK |
7199 | case IORING_OP_SYMLINKAT: |
7200 | putname(req->symlink.oldpath); | |
7201 | putname(req->symlink.newpath); | |
7202 | break; | |
cf30da90 DK |
7203 | case IORING_OP_LINKAT: |
7204 | putname(req->hardlink.oldpath); | |
7205 | putname(req->hardlink.newpath); | |
7206 | break; | |
1b6fe6e0 SR |
7207 | case IORING_OP_STATX: |
7208 | if (req->statx.filename) | |
7209 | putname(req->statx.filename); | |
7210 | break; | |
0e1b6fe3 | 7211 | } |
99bc4c38 | 7212 | } |
75652a30 JA |
7213 | if ((req->flags & REQ_F_POLLED) && req->apoll) { |
7214 | kfree(req->apoll->double_poll); | |
7215 | kfree(req->apoll); | |
7216 | req->apoll = NULL; | |
7217 | } | |
3a0a6902 PB |
7218 | if (req->flags & REQ_F_INFLIGHT) { |
7219 | struct io_uring_task *tctx = req->task->io_uring; | |
7220 | ||
7221 | atomic_dec(&tctx->inflight_tracked); | |
3a0a6902 | 7222 | } |
c854357b | 7223 | if (req->flags & REQ_F_CREDS) |
b8e64b53 | 7224 | put_cred(req->creds); |
d886e185 PB |
7225 | if (req->flags & REQ_F_ASYNC_DATA) { |
7226 | kfree(req->async_data); | |
7227 | req->async_data = NULL; | |
7228 | } | |
c854357b | 7229 | req->flags &= ~IO_REQ_CLEAN_FLAGS; |
99bc4c38 PB |
7230 | } |
7231 | ||
889fca73 | 7232 | static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) |
2b188cc1 | 7233 | { |
5730b27e | 7234 | const struct cred *creds = NULL; |
d625c6ee | 7235 | int ret; |
2b188cc1 | 7236 | |
6878b40e | 7237 | if (unlikely((req->flags & REQ_F_CREDS) && req->creds != current_cred())) |
c10d1f98 | 7238 | creds = override_creds(req->creds); |
5730b27e | 7239 | |
5bd2182d PM |
7240 | if (!io_op_defs[req->opcode].audit_skip) |
7241 | audit_uring_entry(req->opcode); | |
7242 | ||
d625c6ee | 7243 | switch (req->opcode) { |
2b188cc1 | 7244 | case IORING_OP_NOP: |
889fca73 | 7245 | ret = io_nop(req, issue_flags); |
2b188cc1 JA |
7246 | break; |
7247 | case IORING_OP_READV: | |
edafccee | 7248 | case IORING_OP_READ_FIXED: |
3a6820f2 | 7249 | case IORING_OP_READ: |
889fca73 | 7250 | ret = io_read(req, issue_flags); |
edafccee | 7251 | break; |
3529d8c2 | 7252 | case IORING_OP_WRITEV: |
edafccee | 7253 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 7254 | case IORING_OP_WRITE: |
889fca73 | 7255 | ret = io_write(req, issue_flags); |
2b188cc1 | 7256 | break; |
c992fe29 | 7257 | case IORING_OP_FSYNC: |
45d189c6 | 7258 | ret = io_fsync(req, issue_flags); |
c992fe29 | 7259 | break; |
221c5eb2 | 7260 | case IORING_OP_POLL_ADD: |
61e98203 | 7261 | ret = io_poll_add(req, issue_flags); |
221c5eb2 JA |
7262 | break; |
7263 | case IORING_OP_POLL_REMOVE: | |
c5de0036 | 7264 | ret = io_poll_update(req, issue_flags); |
221c5eb2 | 7265 | break; |
5d17b4a4 | 7266 | case IORING_OP_SYNC_FILE_RANGE: |
45d189c6 | 7267 | ret = io_sync_file_range(req, issue_flags); |
5d17b4a4 | 7268 | break; |
0fa03c62 | 7269 | case IORING_OP_SENDMSG: |
889fca73 | 7270 | ret = io_sendmsg(req, issue_flags); |
062d04d7 | 7271 | break; |
fddaface | 7272 | case IORING_OP_SEND: |
889fca73 | 7273 | ret = io_send(req, issue_flags); |
0fa03c62 | 7274 | break; |
aa1fa28f | 7275 | case IORING_OP_RECVMSG: |
889fca73 | 7276 | ret = io_recvmsg(req, issue_flags); |
062d04d7 | 7277 | break; |
fddaface | 7278 | case IORING_OP_RECV: |
889fca73 | 7279 | ret = io_recv(req, issue_flags); |
aa1fa28f | 7280 | break; |
5262f567 | 7281 | case IORING_OP_TIMEOUT: |
61e98203 | 7282 | ret = io_timeout(req, issue_flags); |
5262f567 | 7283 | break; |
11365043 | 7284 | case IORING_OP_TIMEOUT_REMOVE: |
61e98203 | 7285 | ret = io_timeout_remove(req, issue_flags); |
11365043 | 7286 | break; |
17f2fe35 | 7287 | case IORING_OP_ACCEPT: |
889fca73 | 7288 | ret = io_accept(req, issue_flags); |
17f2fe35 | 7289 | break; |
f8e85cf2 | 7290 | case IORING_OP_CONNECT: |
889fca73 | 7291 | ret = io_connect(req, issue_flags); |
f8e85cf2 | 7292 | break; |
62755e35 | 7293 | case IORING_OP_ASYNC_CANCEL: |
61e98203 | 7294 | ret = io_async_cancel(req, issue_flags); |
62755e35 | 7295 | break; |
d63d1b5e | 7296 | case IORING_OP_FALLOCATE: |
45d189c6 | 7297 | ret = io_fallocate(req, issue_flags); |
d63d1b5e | 7298 | break; |
15b71abe | 7299 | case IORING_OP_OPENAT: |
45d189c6 | 7300 | ret = io_openat(req, issue_flags); |
15b71abe | 7301 | break; |
b5dba59e | 7302 | case IORING_OP_CLOSE: |
889fca73 | 7303 | ret = io_close(req, issue_flags); |
b5dba59e | 7304 | break; |
05f3fb3c | 7305 | case IORING_OP_FILES_UPDATE: |
889fca73 | 7306 | ret = io_files_update(req, issue_flags); |
05f3fb3c | 7307 | break; |
eddc7ef5 | 7308 | case IORING_OP_STATX: |
45d189c6 | 7309 | ret = io_statx(req, issue_flags); |
eddc7ef5 | 7310 | break; |
4840e418 | 7311 | case IORING_OP_FADVISE: |
45d189c6 | 7312 | ret = io_fadvise(req, issue_flags); |
4840e418 | 7313 | break; |
c1ca757b | 7314 | case IORING_OP_MADVISE: |
45d189c6 | 7315 | ret = io_madvise(req, issue_flags); |
c1ca757b | 7316 | break; |
cebdb986 | 7317 | case IORING_OP_OPENAT2: |
45d189c6 | 7318 | ret = io_openat2(req, issue_flags); |
cebdb986 | 7319 | break; |
3e4827b0 | 7320 | case IORING_OP_EPOLL_CTL: |
889fca73 | 7321 | ret = io_epoll_ctl(req, issue_flags); |
3e4827b0 | 7322 | break; |
7d67af2c | 7323 | case IORING_OP_SPLICE: |
45d189c6 | 7324 | ret = io_splice(req, issue_flags); |
7d67af2c | 7325 | break; |
ddf0322d | 7326 | case IORING_OP_PROVIDE_BUFFERS: |
889fca73 | 7327 | ret = io_provide_buffers(req, issue_flags); |
ddf0322d | 7328 | break; |
067524e9 | 7329 | case IORING_OP_REMOVE_BUFFERS: |
889fca73 | 7330 | ret = io_remove_buffers(req, issue_flags); |
3e4827b0 | 7331 | break; |
f2a8d5c7 | 7332 | case IORING_OP_TEE: |
45d189c6 | 7333 | ret = io_tee(req, issue_flags); |
f2a8d5c7 | 7334 | break; |
36f4fa68 | 7335 | case IORING_OP_SHUTDOWN: |
45d189c6 | 7336 | ret = io_shutdown(req, issue_flags); |
36f4fa68 | 7337 | break; |
80a261fd | 7338 | case IORING_OP_RENAMEAT: |
45d189c6 | 7339 | ret = io_renameat(req, issue_flags); |
80a261fd | 7340 | break; |
14a1143b | 7341 | case IORING_OP_UNLINKAT: |
45d189c6 | 7342 | ret = io_unlinkat(req, issue_flags); |
14a1143b | 7343 | break; |
e34a02dc DK |
7344 | case IORING_OP_MKDIRAT: |
7345 | ret = io_mkdirat(req, issue_flags); | |
7346 | break; | |
7a8721f8 DK |
7347 | case IORING_OP_SYMLINKAT: |
7348 | ret = io_symlinkat(req, issue_flags); | |
7349 | break; | |
cf30da90 DK |
7350 | case IORING_OP_LINKAT: |
7351 | ret = io_linkat(req, issue_flags); | |
7352 | break; | |
4f57f06c JA |
7353 | case IORING_OP_MSG_RING: |
7354 | ret = io_msg_ring(req, issue_flags); | |
7355 | break; | |
2b188cc1 JA |
7356 | default: |
7357 | ret = -EINVAL; | |
7358 | break; | |
7359 | } | |
7360 | ||
5bd2182d PM |
7361 | if (!io_op_defs[req->opcode].audit_skip) |
7362 | audit_uring_exit(!ret, ret); | |
7363 | ||
5730b27e JA |
7364 | if (creds) |
7365 | revert_creds(creds); | |
def596e9 JA |
7366 | if (ret) |
7367 | return ret; | |
b532576e | 7368 | /* If the op doesn't have a file, we're not polling for it */ |
9983028e | 7369 | if ((req->ctx->flags & IORING_SETUP_IOPOLL) && req->file) |
9882131c | 7370 | io_iopoll_req_issued(req, issue_flags); |
def596e9 JA |
7371 | |
7372 | return 0; | |
2b188cc1 JA |
7373 | } |
7374 | ||
ebc11b6c PB |
7375 | static struct io_wq_work *io_wq_free_work(struct io_wq_work *work) |
7376 | { | |
7377 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
7378 | ||
7379 | req = io_put_req_find_next(req); | |
7380 | return req ? &req->work : NULL; | |
7381 | } | |
7382 | ||
5280f7e5 | 7383 | static void io_wq_submit_work(struct io_wq_work *work) |
2b188cc1 JA |
7384 | { |
7385 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
d01905db PB |
7386 | unsigned int issue_flags = IO_URING_F_UNLOCKED; |
7387 | bool needs_poll = false; | |
6df1db6b | 7388 | struct io_kiocb *timeout; |
561fb04a | 7389 | int ret = 0; |
2b188cc1 | 7390 | |
48dcd38d PB |
7391 | /* one will be dropped by ->io_free_work() after returning to io-wq */ |
7392 | if (!(req->flags & REQ_F_REFCOUNT)) | |
7393 | __io_req_set_refcount(req, 2); | |
7394 | else | |
7395 | req_ref_get(req); | |
5d5901a3 | 7396 | |
6df1db6b PB |
7397 | timeout = io_prep_linked_timeout(req); |
7398 | if (timeout) | |
7399 | io_queue_linked_timeout(timeout); | |
d4c81f38 | 7400 | |
dadebc35 | 7401 | /* either cancelled or io-wq is dying, so don't touch tctx->iowq */ |
d01905db PB |
7402 | if (work->flags & IO_WQ_WORK_CANCEL) { |
7403 | io_req_task_queue_fail(req, -ECANCELED); | |
7404 | return; | |
7405 | } | |
31b51510 | 7406 | |
d01905db | 7407 | if (req->flags & REQ_F_FORCE_ASYNC) { |
afb7f56f PB |
7408 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
7409 | bool opcode_poll = def->pollin || def->pollout; | |
7410 | ||
7411 | if (opcode_poll && file_can_poll(req->file)) { | |
7412 | needs_poll = true; | |
d01905db | 7413 | issue_flags |= IO_URING_F_NONBLOCK; |
afb7f56f | 7414 | } |
561fb04a | 7415 | } |
31b51510 | 7416 | |
d01905db PB |
7417 | do { |
7418 | ret = io_issue_sqe(req, issue_flags); | |
7419 | if (ret != -EAGAIN) | |
7420 | break; | |
7421 | /* | |
7422 | * We can get EAGAIN for iopolled IO even though we're | |
7423 | * forcing a sync submission from here, since we can't | |
7424 | * wait for request slots on the block side. | |
7425 | */ | |
7426 | if (!needs_poll) { | |
7427 | cond_resched(); | |
7428 | continue; | |
90fa0288 HX |
7429 | } |
7430 | ||
4d9237e3 | 7431 | if (io_arm_poll_handler(req, issue_flags) == IO_APOLL_OK) |
d01905db PB |
7432 | return; |
7433 | /* aborted or ready, in either case retry blocking */ | |
7434 | needs_poll = false; | |
7435 | issue_flags &= ~IO_URING_F_NONBLOCK; | |
7436 | } while (1); | |
31b51510 | 7437 | |
a3df7698 | 7438 | /* avoid locking problems by failing it from a clean context */ |
5d5901a3 | 7439 | if (ret) |
a3df7698 | 7440 | io_req_task_queue_fail(req, ret); |
2b188cc1 JA |
7441 | } |
7442 | ||
aeca241b | 7443 | static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table, |
042b0d85 | 7444 | unsigned i) |
65e19f54 | 7445 | { |
042b0d85 | 7446 | return &table->files[i]; |
dafecf19 PB |
7447 | } |
7448 | ||
65e19f54 JA |
7449 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
7450 | int index) | |
7451 | { | |
aeca241b | 7452 | struct io_fixed_file *slot = io_fixed_file_slot(&ctx->file_table, index); |
65e19f54 | 7453 | |
a04b0ac0 | 7454 | return (struct file *) (slot->file_ptr & FFS_MASK); |
65e19f54 JA |
7455 | } |
7456 | ||
a04b0ac0 | 7457 | static void io_fixed_file_set(struct io_fixed_file *file_slot, struct file *file) |
9a321c98 PB |
7458 | { |
7459 | unsigned long file_ptr = (unsigned long) file; | |
7460 | ||
88459b50 | 7461 | file_ptr |= io_file_get_flags(file); |
a04b0ac0 | 7462 | file_slot->file_ptr = file_ptr; |
65e19f54 JA |
7463 | } |
7464 | ||
ac177053 PB |
7465 | static inline struct file *io_file_get_fixed(struct io_ring_ctx *ctx, |
7466 | struct io_kiocb *req, int fd) | |
09bb8394 | 7467 | { |
8da11c19 | 7468 | struct file *file; |
ac177053 | 7469 | unsigned long file_ptr; |
09bb8394 | 7470 | |
ac177053 PB |
7471 | if (unlikely((unsigned int)fd >= ctx->nr_user_files)) |
7472 | return NULL; | |
7473 | fd = array_index_nospec(fd, ctx->nr_user_files); | |
7474 | file_ptr = io_fixed_file_slot(&ctx->file_table, fd)->file_ptr; | |
7475 | file = (struct file *) (file_ptr & FFS_MASK); | |
7476 | file_ptr &= ~FFS_MASK; | |
7477 | /* mask in overlapping REQ_F and FFS bits */ | |
35645ac3 | 7478 | req->flags |= (file_ptr << REQ_F_SUPPORT_NOWAIT_BIT); |
a46be971 | 7479 | io_req_set_rsrc_node(req, ctx); |
ac177053 PB |
7480 | return file; |
7481 | } | |
d44f554e | 7482 | |
ac177053 | 7483 | static struct file *io_file_get_normal(struct io_ring_ctx *ctx, |
ac177053 PB |
7484 | struct io_kiocb *req, int fd) |
7485 | { | |
62906e89 | 7486 | struct file *file = fget(fd); |
ac177053 | 7487 | |
502c87d6 | 7488 | trace_io_uring_file_get(ctx, req, req->user_data, fd); |
09bb8394 | 7489 | |
ac177053 PB |
7490 | /* we don't allow fixed io_uring files */ |
7491 | if (file && unlikely(file->f_op == &io_uring_fops)) | |
7492 | io_req_track_inflight(req); | |
8371adf5 | 7493 | return file; |
09bb8394 JA |
7494 | } |
7495 | ||
ac177053 | 7496 | static inline struct file *io_file_get(struct io_ring_ctx *ctx, |
ac177053 PB |
7497 | struct io_kiocb *req, int fd, bool fixed) |
7498 | { | |
7499 | if (fixed) | |
7500 | return io_file_get_fixed(ctx, req, fd); | |
7501 | else | |
62906e89 | 7502 | return io_file_get_normal(ctx, req, fd); |
ac177053 PB |
7503 | } |
7504 | ||
f237c30a | 7505 | static void io_req_task_link_timeout(struct io_kiocb *req, bool *locked) |
89b263f6 JA |
7506 | { |
7507 | struct io_kiocb *prev = req->timeout.prev; | |
617a8948 | 7508 | int ret = -ENOENT; |
89b263f6 JA |
7509 | |
7510 | if (prev) { | |
617a8948 PB |
7511 | if (!(req->task->flags & PF_EXITING)) |
7512 | ret = io_try_cancel_userdata(req, prev->user_data); | |
505657bc | 7513 | io_req_complete_post(req, ret ?: -ETIME, 0); |
89b263f6 | 7514 | io_put_req(prev); |
89b263f6 JA |
7515 | } else { |
7516 | io_req_complete_post(req, -ETIME, 0); | |
7517 | } | |
7518 | } | |
7519 | ||
2665abfd | 7520 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 7521 | { |
ad8a48ac JA |
7522 | struct io_timeout_data *data = container_of(timer, |
7523 | struct io_timeout_data, timer); | |
90cd7e42 | 7524 | struct io_kiocb *prev, *req = data->req; |
2665abfd | 7525 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 7526 | unsigned long flags; |
2665abfd | 7527 | |
89b263f6 | 7528 | spin_lock_irqsave(&ctx->timeout_lock, flags); |
90cd7e42 PB |
7529 | prev = req->timeout.head; |
7530 | req->timeout.head = NULL; | |
2665abfd JA |
7531 | |
7532 | /* | |
7533 | * We don't expect the list to be empty, that will only happen if we | |
7534 | * race with the completion of the linked work. | |
7535 | */ | |
447c19f3 | 7536 | if (prev) { |
f2f87370 | 7537 | io_remove_next_linked(prev); |
447c19f3 PB |
7538 | if (!req_ref_inc_not_zero(prev)) |
7539 | prev = NULL; | |
7540 | } | |
ef9dd637 | 7541 | list_del(&req->timeout.list); |
89b263f6 JA |
7542 | req->timeout.prev = prev; |
7543 | spin_unlock_irqrestore(&ctx->timeout_lock, flags); | |
2665abfd | 7544 | |
89b263f6 | 7545 | req->io_task_work.func = io_req_task_link_timeout; |
4813c377 | 7546 | io_req_task_work_add(req, false); |
2665abfd JA |
7547 | return HRTIMER_NORESTART; |
7548 | } | |
7549 | ||
de968c18 | 7550 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 7551 | { |
de968c18 PB |
7552 | struct io_ring_ctx *ctx = req->ctx; |
7553 | ||
89b263f6 | 7554 | spin_lock_irq(&ctx->timeout_lock); |
76a46e06 | 7555 | /* |
f2f87370 PB |
7556 | * If the back reference is NULL, then our linked request finished |
7557 | * before we got a chance to setup the timer | |
76a46e06 | 7558 | */ |
90cd7e42 | 7559 | if (req->timeout.head) { |
e8c2bc1f | 7560 | struct io_timeout_data *data = req->async_data; |
94ae5e77 | 7561 | |
ad8a48ac JA |
7562 | data->timer.function = io_link_timeout_fn; |
7563 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
7564 | data->mode); | |
ef9dd637 | 7565 | list_add_tail(&req->timeout.list, &ctx->ltimeout_list); |
2665abfd | 7566 | } |
89b263f6 | 7567 | spin_unlock_irq(&ctx->timeout_lock); |
2665abfd | 7568 | /* drop submission reference */ |
76a46e06 JA |
7569 | io_put_req(req); |
7570 | } | |
2665abfd | 7571 | |
d475a9a6 PB |
7572 | static void io_queue_sqe_arm_apoll(struct io_kiocb *req) |
7573 | __must_hold(&req->ctx->uring_lock) | |
7574 | { | |
7575 | struct io_kiocb *linked_timeout = io_prep_linked_timeout(req); | |
7576 | ||
4d9237e3 | 7577 | switch (io_arm_poll_handler(req, 0)) { |
d475a9a6 | 7578 | case IO_APOLL_READY: |
d475a9a6 PB |
7579 | io_req_task_queue(req); |
7580 | break; | |
7581 | case IO_APOLL_ABORTED: | |
7582 | /* | |
7583 | * Queued up for async execution, worker will release | |
7584 | * submit reference when the iocb is actually submitted. | |
7585 | */ | |
7586 | io_queue_async_work(req, NULL); | |
7587 | break; | |
b1c62645 | 7588 | case IO_APOLL_OK: |
b1c62645 | 7589 | break; |
d475a9a6 PB |
7590 | } |
7591 | ||
7592 | if (linked_timeout) | |
7593 | io_queue_linked_timeout(linked_timeout); | |
7594 | } | |
7595 | ||
7596 | static inline void __io_queue_sqe(struct io_kiocb *req) | |
282cdc86 | 7597 | __must_hold(&req->ctx->uring_lock) |
2b188cc1 | 7598 | { |
906c6caa | 7599 | struct io_kiocb *linked_timeout; |
e0c5c576 | 7600 | int ret; |
2b188cc1 | 7601 | |
c5eef2b9 | 7602 | ret = io_issue_sqe(req, IO_URING_F_NONBLOCK|IO_URING_F_COMPLETE_DEFER); |
193155c8 | 7603 | |
fff4e40e PB |
7604 | if (req->flags & REQ_F_COMPLETE_INLINE) { |
7605 | io_req_add_compl_list(req); | |
d9f9d284 | 7606 | return; |
fff4e40e | 7607 | } |
491381ce JA |
7608 | /* |
7609 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
7610 | * doesn't support non-blocking read/write attempts | |
7611 | */ | |
1840038e | 7612 | if (likely(!ret)) { |
906c6caa PB |
7613 | linked_timeout = io_prep_linked_timeout(req); |
7614 | if (linked_timeout) | |
7615 | io_queue_linked_timeout(linked_timeout); | |
1840038e | 7616 | } else if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) { |
d475a9a6 | 7617 | io_queue_sqe_arm_apoll(req); |
0d63c148 | 7618 | } else { |
f41db273 | 7619 | io_req_complete_failed(req, ret); |
9e645e11 | 7620 | } |
2b188cc1 JA |
7621 | } |
7622 | ||
4652fe3f | 7623 | static void io_queue_sqe_fallback(struct io_kiocb *req) |
282cdc86 | 7624 | __must_hold(&req->ctx->uring_lock) |
4fe2c963 | 7625 | { |
4652fe3f | 7626 | if (req->flags & REQ_F_FAIL) { |
c6d3d9cb | 7627 | io_req_complete_fail_submit(req); |
e0eb71dc PB |
7628 | } else if (unlikely(req->ctx->drain_active)) { |
7629 | io_drain_req(req); | |
76cc33d7 PB |
7630 | } else { |
7631 | int ret = io_req_prep_async(req); | |
7632 | ||
7633 | if (unlikely(ret)) | |
7634 | io_req_complete_failed(req, ret); | |
7635 | else | |
f237c30a | 7636 | io_queue_async_work(req, NULL); |
ce35a47a | 7637 | } |
4fe2c963 JL |
7638 | } |
7639 | ||
4652fe3f PB |
7640 | static inline void io_queue_sqe(struct io_kiocb *req) |
7641 | __must_hold(&req->ctx->uring_lock) | |
7642 | { | |
7643 | if (likely(!(req->flags & (REQ_F_FORCE_ASYNC | REQ_F_FAIL)))) | |
7644 | __io_queue_sqe(req); | |
7645 | else | |
7646 | io_queue_sqe_fallback(req); | |
7647 | } | |
7648 | ||
b16fed66 PB |
7649 | /* |
7650 | * Check SQE restrictions (opcode and flags). | |
7651 | * | |
7652 | * Returns 'true' if SQE is allowed, 'false' otherwise. | |
7653 | */ | |
7654 | static inline bool io_check_restriction(struct io_ring_ctx *ctx, | |
7655 | struct io_kiocb *req, | |
7656 | unsigned int sqe_flags) | |
4fe2c963 | 7657 | { |
b16fed66 PB |
7658 | if (!test_bit(req->opcode, ctx->restrictions.sqe_op)) |
7659 | return false; | |
7660 | ||
7661 | if ((sqe_flags & ctx->restrictions.sqe_flags_required) != | |
7662 | ctx->restrictions.sqe_flags_required) | |
7663 | return false; | |
7664 | ||
7665 | if (sqe_flags & ~(ctx->restrictions.sqe_flags_allowed | | |
7666 | ctx->restrictions.sqe_flags_required)) | |
7667 | return false; | |
7668 | ||
7669 | return true; | |
4fe2c963 JL |
7670 | } |
7671 | ||
22b2ca31 PB |
7672 | static void io_init_req_drain(struct io_kiocb *req) |
7673 | { | |
7674 | struct io_ring_ctx *ctx = req->ctx; | |
7675 | struct io_kiocb *head = ctx->submit_state.link.head; | |
7676 | ||
7677 | ctx->drain_active = true; | |
7678 | if (head) { | |
7679 | /* | |
7680 | * If we need to drain a request in the middle of a link, drain | |
7681 | * the head request and the next request/link after the current | |
7682 | * link. Considering sequential execution of links, | |
b6c7db32 | 7683 | * REQ_F_IO_DRAIN will be maintained for every request of our |
22b2ca31 PB |
7684 | * link. |
7685 | */ | |
b6c7db32 | 7686 | head->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
22b2ca31 PB |
7687 | ctx->drain_next = true; |
7688 | } | |
7689 | } | |
7690 | ||
b16fed66 PB |
7691 | static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req, |
7692 | const struct io_uring_sqe *sqe) | |
282cdc86 | 7693 | __must_hold(&ctx->uring_lock) |
b16fed66 | 7694 | { |
b16fed66 | 7695 | unsigned int sqe_flags; |
fc0ae024 | 7696 | int personality; |
4a04d1d1 | 7697 | u8 opcode; |
b16fed66 | 7698 | |
864ea921 | 7699 | /* req is partially pre-initialised, see io_preinit_req() */ |
4a04d1d1 | 7700 | req->opcode = opcode = READ_ONCE(sqe->opcode); |
b16fed66 PB |
7701 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
7702 | req->flags = sqe_flags = READ_ONCE(sqe->flags); | |
7703 | req->user_data = READ_ONCE(sqe->user_data); | |
b16fed66 | 7704 | req->file = NULL; |
b16fed66 | 7705 | req->fixed_rsrc_refs = NULL; |
b16fed66 | 7706 | req->task = current; |
b16fed66 | 7707 | |
4a04d1d1 PB |
7708 | if (unlikely(opcode >= IORING_OP_LAST)) { |
7709 | req->opcode = 0; | |
b16fed66 | 7710 | return -EINVAL; |
4a04d1d1 | 7711 | } |
68fe256a PB |
7712 | if (unlikely(sqe_flags & ~SQE_COMMON_FLAGS)) { |
7713 | /* enforce forwards compatibility on users */ | |
7714 | if (sqe_flags & ~SQE_VALID_FLAGS) | |
7715 | return -EINVAL; | |
7716 | if ((sqe_flags & IOSQE_BUFFER_SELECT) && | |
4a04d1d1 | 7717 | !io_op_defs[opcode].buffer_select) |
68fe256a | 7718 | return -EOPNOTSUPP; |
5562a8d7 PB |
7719 | if (sqe_flags & IOSQE_CQE_SKIP_SUCCESS) |
7720 | ctx->drain_disabled = true; | |
7721 | if (sqe_flags & IOSQE_IO_DRAIN) { | |
7722 | if (ctx->drain_disabled) | |
7723 | return -EOPNOTSUPP; | |
22b2ca31 | 7724 | io_init_req_drain(req); |
5562a8d7 | 7725 | } |
2a56a9bd PB |
7726 | } |
7727 | if (unlikely(ctx->restricted || ctx->drain_active || ctx->drain_next)) { | |
7728 | if (ctx->restricted && !io_check_restriction(ctx, req, sqe_flags)) | |
7729 | return -EACCES; | |
7730 | /* knock it to the slow queue path, will be drained there */ | |
7731 | if (ctx->drain_active) | |
7732 | req->flags |= REQ_F_FORCE_ASYNC; | |
7733 | /* if there is no link, we're at "next" request and need to drain */ | |
7734 | if (unlikely(ctx->drain_next) && !ctx->submit_state.link.head) { | |
7735 | ctx->drain_next = false; | |
7736 | ctx->drain_active = true; | |
b6c7db32 | 7737 | req->flags |= REQ_F_IO_DRAIN | REQ_F_FORCE_ASYNC; |
2a56a9bd | 7738 | } |
68fe256a | 7739 | } |
b16fed66 | 7740 | |
4a04d1d1 | 7741 | if (io_op_defs[opcode].needs_file) { |
6d63416d PB |
7742 | struct io_submit_state *state = &ctx->submit_state; |
7743 | ||
7744 | /* | |
7745 | * Plug now if we have more than 2 IO left after this, and the | |
7746 | * target is potentially a read/write to block based storage. | |
7747 | */ | |
4a04d1d1 | 7748 | if (state->need_plug && io_op_defs[opcode].plug) { |
6d63416d PB |
7749 | state->plug_started = true; |
7750 | state->need_plug = false; | |
5ca7a8b3 | 7751 | blk_start_plug_nr_ios(&state->plug, state->submit_nr); |
6d63416d PB |
7752 | } |
7753 | ||
62906e89 | 7754 | req->file = io_file_get(ctx, req, READ_ONCE(sqe->fd), |
ac177053 | 7755 | (sqe_flags & IOSQE_FIXED_FILE)); |
b16fed66 | 7756 | if (unlikely(!req->file)) |
fc0ae024 | 7757 | return -EBADF; |
b16fed66 | 7758 | } |
863e0560 | 7759 | |
003e8dcc JA |
7760 | personality = READ_ONCE(sqe->personality); |
7761 | if (personality) { | |
cdab10bf LT |
7762 | int ret; |
7763 | ||
c10d1f98 PB |
7764 | req->creds = xa_load(&ctx->personalities, personality); |
7765 | if (!req->creds) | |
003e8dcc | 7766 | return -EINVAL; |
c10d1f98 | 7767 | get_cred(req->creds); |
cdc1404a PM |
7768 | ret = security_uring_override_creds(req->creds); |
7769 | if (ret) { | |
7770 | put_cred(req->creds); | |
7771 | return ret; | |
7772 | } | |
b8e64b53 | 7773 | req->flags |= REQ_F_CREDS; |
003e8dcc | 7774 | } |
b16fed66 | 7775 | |
fc0ae024 | 7776 | return io_req_prep(req, sqe); |
b16fed66 PB |
7777 | } |
7778 | ||
a6b8cadc | 7779 | static int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
a1ab7b35 | 7780 | const struct io_uring_sqe *sqe) |
282cdc86 | 7781 | __must_hold(&ctx->uring_lock) |
9e645e11 | 7782 | { |
a1ab7b35 | 7783 | struct io_submit_link *link = &ctx->submit_state.link; |
ef4ff581 | 7784 | int ret; |
9e645e11 | 7785 | |
a6b8cadc PB |
7786 | ret = io_init_req(ctx, req, sqe); |
7787 | if (unlikely(ret)) { | |
502c87d6 | 7788 | trace_io_uring_req_failed(sqe, ctx, req, ret); |
a87acfde | 7789 | |
a8295b98 | 7790 | /* fail even hard links since we don't submit */ |
de59bc10 | 7791 | if (link->head) { |
a8295b98 HX |
7792 | /* |
7793 | * we can judge a link req is failed or cancelled by if | |
7794 | * REQ_F_FAIL is set, but the head is an exception since | |
7795 | * it may be set REQ_F_FAIL because of other req's failure | |
7796 | * so let's leverage req->result to distinguish if a head | |
7797 | * is set REQ_F_FAIL because of its failure or other req's | |
7798 | * failure so that we can set the correct ret code for it. | |
7799 | * init result here to avoid affecting the normal path. | |
7800 | */ | |
7801 | if (!(link->head->flags & REQ_F_FAIL)) | |
7802 | req_fail_link_node(link->head, -ECANCELED); | |
7803 | } else if (!(req->flags & (REQ_F_LINK | REQ_F_HARDLINK))) { | |
7804 | /* | |
7805 | * the current req is a normal req, we should return | |
7806 | * error and thus break the submittion loop. | |
7807 | */ | |
7808 | io_req_complete_failed(req, ret); | |
7809 | return ret; | |
de59bc10 | 7810 | } |
a8295b98 | 7811 | req_fail_link_node(req, ret); |
a6b8cadc | 7812 | } |
441b8a78 | 7813 | |
be7053b7 | 7814 | /* don't need @sqe from now on */ |
502c87d6 | 7815 | trace_io_uring_submit_sqe(ctx, req, req->user_data, req->opcode, |
236daeae OL |
7816 | req->flags, true, |
7817 | ctx->flags & IORING_SETUP_SQPOLL); | |
a6b8cadc | 7818 | |
9e645e11 JA |
7819 | /* |
7820 | * If we already have a head request, queue this one for async | |
7821 | * submittal once the head completes. If we don't have a head but | |
7822 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
7823 | * submitted sync once the chain is complete. If none of those | |
7824 | * conditions are true (normal request), then just queue it. | |
7825 | */ | |
863e0560 PB |
7826 | if (link->head) { |
7827 | struct io_kiocb *head = link->head; | |
4e88d6e7 | 7828 | |
a8295b98 HX |
7829 | if (!(req->flags & REQ_F_FAIL)) { |
7830 | ret = io_req_prep_async(req); | |
7831 | if (unlikely(ret)) { | |
7832 | req_fail_link_node(req, ret); | |
7833 | if (!(head->flags & REQ_F_FAIL)) | |
7834 | req_fail_link_node(head, -ECANCELED); | |
7835 | } | |
7836 | } | |
9d76377f | 7837 | trace_io_uring_link(ctx, req, head); |
f2f87370 | 7838 | link->last->link = req; |
863e0560 | 7839 | link->last = req; |
32fe525b | 7840 | |
f15a3431 PB |
7841 | if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) |
7842 | return 0; | |
32fe525b | 7843 | /* last request of a link, enqueue the link */ |
f15a3431 PB |
7844 | link->head = NULL; |
7845 | req = head; | |
7846 | } else if (req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) { | |
7847 | link->head = req; | |
7848 | link->last = req; | |
7849 | return 0; | |
9e645e11 | 7850 | } |
2e6e1fde | 7851 | |
f15a3431 | 7852 | io_queue_sqe(req); |
1d4240cc | 7853 | return 0; |
9e645e11 JA |
7854 | } |
7855 | ||
9a56a232 JA |
7856 | /* |
7857 | * Batched submission is done, ensure local IO is flushed out. | |
7858 | */ | |
553deffd | 7859 | static void io_submit_state_end(struct io_ring_ctx *ctx) |
9a56a232 | 7860 | { |
553deffd PB |
7861 | struct io_submit_state *state = &ctx->submit_state; |
7862 | ||
a1ab7b35 | 7863 | if (state->link.head) |
de59bc10 | 7864 | io_queue_sqe(state->link.head); |
553deffd | 7865 | /* flush only after queuing links as they can generate completions */ |
c450178d | 7866 | io_submit_flush_completions(ctx); |
27926b68 JA |
7867 | if (state->plug_started) |
7868 | blk_finish_plug(&state->plug); | |
9a56a232 JA |
7869 | } |
7870 | ||
7871 | /* | |
7872 | * Start submission side cache. | |
7873 | */ | |
7874 | static void io_submit_state_start(struct io_submit_state *state, | |
ba88ff11 | 7875 | unsigned int max_ios) |
9a56a232 | 7876 | { |
27926b68 | 7877 | state->plug_started = false; |
4b628aeb | 7878 | state->need_plug = max_ios > 2; |
5ca7a8b3 | 7879 | state->submit_nr = max_ios; |
a1ab7b35 PB |
7880 | /* set only head, no need to init link_last in advance */ |
7881 | state->link.head = NULL; | |
9a56a232 JA |
7882 | } |
7883 | ||
2b188cc1 JA |
7884 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
7885 | { | |
75b28aff | 7886 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 7887 | |
caf582c6 PB |
7888 | /* |
7889 | * Ensure any loads from the SQEs are done at this point, | |
7890 | * since once we write the new head, the application could | |
7891 | * write new data to them. | |
7892 | */ | |
7893 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
7894 | } |
7895 | ||
2b188cc1 | 7896 | /* |
dd9ae8a0 | 7897 | * Fetch an sqe, if one is available. Note this returns a pointer to memory |
2b188cc1 JA |
7898 | * that is mapped by userspace. This means that care needs to be taken to |
7899 | * ensure that reads are stable, as we cannot rely on userspace always | |
7900 | * being a good citizen. If members of the sqe are validated and then later | |
7901 | * used, it's important that those reads are done through READ_ONCE() to | |
7902 | * prevent a re-load down the line. | |
7903 | */ | |
709b302f | 7904 | static const struct io_uring_sqe *io_get_sqe(struct io_ring_ctx *ctx) |
2b188cc1 | 7905 | { |
ea5ab3b5 | 7906 | unsigned head, mask = ctx->sq_entries - 1; |
17d3aeb3 | 7907 | unsigned sq_idx = ctx->cached_sq_head++ & mask; |
2b188cc1 JA |
7908 | |
7909 | /* | |
7910 | * The cached sq head (or cq tail) serves two purposes: | |
7911 | * | |
7912 | * 1) allows us to batch the cost of updating the user visible | |
7913 | * head updates. | |
7914 | * 2) allows the kernel side to track the head on its own, even | |
7915 | * though the application is the one updating it. | |
7916 | */ | |
17d3aeb3 | 7917 | head = READ_ONCE(ctx->sq_array[sq_idx]); |
709b302f PB |
7918 | if (likely(head < ctx->sq_entries)) |
7919 | return &ctx->sq_sqes[head]; | |
2b188cc1 JA |
7920 | |
7921 | /* drop invalid entries */ | |
15641e42 PB |
7922 | ctx->cq_extra--; |
7923 | WRITE_ONCE(ctx->rings->sq_dropped, | |
7924 | READ_ONCE(ctx->rings->sq_dropped) + 1); | |
709b302f PB |
7925 | return NULL; |
7926 | } | |
7927 | ||
0f212204 | 7928 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr) |
282cdc86 | 7929 | __must_hold(&ctx->uring_lock) |
6c271ce2 | 7930 | { |
69629809 | 7931 | unsigned int entries = io_sqring_entries(ctx); |
46c4e16a | 7932 | int submitted = 0; |
6c271ce2 | 7933 | |
51d48dab | 7934 | if (unlikely(!entries)) |
69629809 | 7935 | return 0; |
ee7d46d9 | 7936 | /* make sure SQ entry isn't read before tail */ |
69629809 | 7937 | nr = min3(nr, ctx->sq_entries, entries); |
9a10867a | 7938 | io_get_task_refs(nr); |
6c271ce2 | 7939 | |
ba88ff11 | 7940 | io_submit_state_start(&ctx->submit_state, nr); |
69629809 | 7941 | do { |
3529d8c2 | 7942 | const struct io_uring_sqe *sqe; |
196be95c | 7943 | struct io_kiocb *req; |
fb5ccc98 | 7944 | |
a33ae9ce | 7945 | if (unlikely(!io_alloc_req_refill(ctx))) { |
196be95c PB |
7946 | if (!submitted) |
7947 | submitted = -EAGAIN; | |
fb5ccc98 | 7948 | break; |
196be95c | 7949 | } |
a33ae9ce | 7950 | req = io_alloc_req(ctx); |
4fccfcbb PB |
7951 | sqe = io_get_sqe(ctx); |
7952 | if (unlikely(!sqe)) { | |
c2b6c6bc | 7953 | wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list); |
4fccfcbb PB |
7954 | break; |
7955 | } | |
d3656344 JA |
7956 | /* will complete beyond this point, count as submitted */ |
7957 | submitted++; | |
bcbb7bf6 JA |
7958 | if (io_submit_sqe(ctx, req, sqe)) { |
7959 | /* | |
7960 | * Continue submitting even for sqe failure if the | |
7961 | * ring was setup with IORING_SETUP_SUBMIT_ALL | |
7962 | */ | |
7963 | if (!(ctx->flags & IORING_SETUP_SUBMIT_ALL)) | |
7964 | break; | |
7965 | } | |
69629809 | 7966 | } while (submitted < nr); |
6c271ce2 | 7967 | |
9466f437 PB |
7968 | if (unlikely(submitted != nr)) { |
7969 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
d8a6df10 | 7970 | int unused = nr - ref_used; |
9466f437 | 7971 | |
09899b19 | 7972 | current->io_uring->cached_refs += unused; |
9466f437 | 7973 | } |
6c271ce2 | 7974 | |
553deffd | 7975 | io_submit_state_end(ctx); |
ae9428ca PB |
7976 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
7977 | io_commit_sqring(ctx); | |
7978 | ||
6c271ce2 JA |
7979 | return submitted; |
7980 | } | |
7981 | ||
e4b6d902 PB |
7982 | static inline bool io_sqd_events_pending(struct io_sq_data *sqd) |
7983 | { | |
7984 | return READ_ONCE(sqd->state); | |
7985 | } | |
7986 | ||
23b3628e XW |
7987 | static inline void io_ring_set_wakeup_flag(struct io_ring_ctx *ctx) |
7988 | { | |
7989 | /* Tell userspace we may need a wakeup call */ | |
79ebeaee | 7990 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7991 | WRITE_ONCE(ctx->rings->sq_flags, |
7992 | ctx->rings->sq_flags | IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 7993 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
7994 | } |
7995 | ||
7996 | static inline void io_ring_clear_wakeup_flag(struct io_ring_ctx *ctx) | |
7997 | { | |
79ebeaee | 7998 | spin_lock(&ctx->completion_lock); |
20c0b380 NA |
7999 | WRITE_ONCE(ctx->rings->sq_flags, |
8000 | ctx->rings->sq_flags & ~IORING_SQ_NEED_WAKEUP); | |
79ebeaee | 8001 | spin_unlock(&ctx->completion_lock); |
23b3628e XW |
8002 | } |
8003 | ||
08369246 | 8004 | static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries) |
6c271ce2 | 8005 | { |
c8d1ba58 | 8006 | unsigned int to_submit; |
bdcd3eab | 8007 | int ret = 0; |
6c271ce2 | 8008 | |
c8d1ba58 | 8009 | to_submit = io_sqring_entries(ctx); |
e95eee2d | 8010 | /* if we're handling multiple rings, cap submit size for fairness */ |
4ce8ad95 OL |
8011 | if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE) |
8012 | to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE; | |
e95eee2d | 8013 | |
5eef4e87 | 8014 | if (!wq_list_empty(&ctx->iopoll_list) || to_submit) { |
948e1947 PB |
8015 | const struct cred *creds = NULL; |
8016 | ||
8017 | if (ctx->sq_creds != current_cred()) | |
8018 | creds = override_creds(ctx->sq_creds); | |
a4c0b3de | 8019 | |
c8d1ba58 | 8020 | mutex_lock(&ctx->uring_lock); |
5eef4e87 | 8021 | if (!wq_list_empty(&ctx->iopoll_list)) |
5ba3c874 | 8022 | io_do_iopoll(ctx, true); |
906a3c6f | 8023 | |
3b763ba1 PB |
8024 | /* |
8025 | * Don't submit if refs are dying, good for io_uring_register(), | |
8026 | * but also it is relied upon by io_ring_exit_work() | |
8027 | */ | |
0298ef96 PB |
8028 | if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) && |
8029 | !(ctx->flags & IORING_SETUP_R_DISABLED)) | |
08369246 | 8030 | ret = io_submit_sqes(ctx, to_submit); |
c8d1ba58 | 8031 | mutex_unlock(&ctx->uring_lock); |
adc8682e OL |
8032 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8033 | spin_lock(&ctx->napi_lock); | |
8034 | if (!list_empty(&ctx->napi_list) && | |
8035 | io_napi_busy_loop(&ctx->napi_list)) | |
8036 | ++ret; | |
8037 | spin_unlock(&ctx->napi_lock); | |
8038 | #endif | |
acfb381d PB |
8039 | if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait)) |
8040 | wake_up(&ctx->sqo_sq_wait); | |
948e1947 PB |
8041 | if (creds) |
8042 | revert_creds(creds); | |
acfb381d | 8043 | } |
6c271ce2 | 8044 | |
08369246 XW |
8045 | return ret; |
8046 | } | |
6c271ce2 | 8047 | |
c072481d | 8048 | static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd) |
08369246 XW |
8049 | { |
8050 | struct io_ring_ctx *ctx; | |
8051 | unsigned sq_thread_idle = 0; | |
6c271ce2 | 8052 | |
c9dca27d PB |
8053 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8054 | sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle); | |
08369246 | 8055 | sqd->sq_thread_idle = sq_thread_idle; |
c8d1ba58 | 8056 | } |
6c271ce2 | 8057 | |
e4b6d902 PB |
8058 | static bool io_sqd_handle_event(struct io_sq_data *sqd) |
8059 | { | |
8060 | bool did_sig = false; | |
8061 | struct ksignal ksig; | |
8062 | ||
8063 | if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) || | |
8064 | signal_pending(current)) { | |
8065 | mutex_unlock(&sqd->lock); | |
8066 | if (signal_pending(current)) | |
8067 | did_sig = get_signal(&ksig); | |
8068 | cond_resched(); | |
8069 | mutex_lock(&sqd->lock); | |
8070 | } | |
e4b6d902 PB |
8071 | return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
8072 | } | |
8073 | ||
c8d1ba58 JA |
8074 | static int io_sq_thread(void *data) |
8075 | { | |
69fb2131 JA |
8076 | struct io_sq_data *sqd = data; |
8077 | struct io_ring_ctx *ctx; | |
a0d9205f | 8078 | unsigned long timeout = 0; |
37d1e2e3 | 8079 | char buf[TASK_COMM_LEN]; |
08369246 | 8080 | DEFINE_WAIT(wait); |
6c271ce2 | 8081 | |
696ee88a | 8082 | snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid); |
37d1e2e3 | 8083 | set_task_comm(current, buf); |
37d1e2e3 JA |
8084 | |
8085 | if (sqd->sq_cpu != -1) | |
8086 | set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu)); | |
8087 | else | |
8088 | set_cpus_allowed_ptr(current, cpu_online_mask); | |
8089 | current->flags |= PF_NO_SETAFFINITY; | |
8090 | ||
5bd2182d PM |
8091 | audit_alloc_kernel(current); |
8092 | ||
09a6f4ef | 8093 | mutex_lock(&sqd->lock); |
e4b6d902 | 8094 | while (1) { |
1a924a80 | 8095 | bool cap_entries, sqt_spin = false; |
c1edbf5f | 8096 | |
e4b6d902 PB |
8097 | if (io_sqd_events_pending(sqd) || signal_pending(current)) { |
8098 | if (io_sqd_handle_event(sqd)) | |
c7d95613 | 8099 | break; |
08369246 XW |
8100 | timeout = jiffies + sqd->sq_thread_idle; |
8101 | } | |
e4b6d902 | 8102 | |
e95eee2d | 8103 | cap_entries = !list_is_singular(&sqd->ctx_list); |
69fb2131 | 8104 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
948e1947 | 8105 | int ret = __io_sq_thread(ctx, cap_entries); |
7c30f36a | 8106 | |
5eef4e87 | 8107 | if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list))) |
08369246 | 8108 | sqt_spin = true; |
69fb2131 | 8109 | } |
dd432ea5 PB |
8110 | if (io_run_task_work()) |
8111 | sqt_spin = true; | |
6c271ce2 | 8112 | |
08369246 | 8113 | if (sqt_spin || !time_after(jiffies, timeout)) { |
c8d1ba58 | 8114 | cond_resched(); |
08369246 XW |
8115 | if (sqt_spin) |
8116 | timeout = jiffies + sqd->sq_thread_idle; | |
8117 | continue; | |
8118 | } | |
8119 | ||
08369246 | 8120 | prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE); |
7f62d40d | 8121 | if (!io_sqd_events_pending(sqd) && !task_work_pending(current)) { |
1a924a80 PB |
8122 | bool needs_sched = true; |
8123 | ||
724cb4f9 | 8124 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) { |
aaa9f0f4 PB |
8125 | io_ring_set_wakeup_flag(ctx); |
8126 | ||
724cb4f9 | 8127 | if ((ctx->flags & IORING_SETUP_IOPOLL) && |
5eef4e87 | 8128 | !wq_list_empty(&ctx->iopoll_list)) { |
724cb4f9 HX |
8129 | needs_sched = false; |
8130 | break; | |
8131 | } | |
649bb75d AK |
8132 | |
8133 | /* | |
8134 | * Ensure the store of the wakeup flag is not | |
8135 | * reordered with the load of the SQ tail | |
8136 | */ | |
8137 | smp_mb(); | |
8138 | ||
724cb4f9 HX |
8139 | if (io_sqring_entries(ctx)) { |
8140 | needs_sched = false; | |
8141 | break; | |
8142 | } | |
8143 | } | |
8144 | ||
8145 | if (needs_sched) { | |
8146 | mutex_unlock(&sqd->lock); | |
8147 | schedule(); | |
8148 | mutex_lock(&sqd->lock); | |
8149 | } | |
69fb2131 JA |
8150 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
8151 | io_ring_clear_wakeup_flag(ctx); | |
6c271ce2 | 8152 | } |
08369246 XW |
8153 | |
8154 | finish_wait(&sqd->wait, &wait); | |
8155 | timeout = jiffies + sqd->sq_thread_idle; | |
6c271ce2 | 8156 | } |
28cea78a | 8157 | |
78cc687b | 8158 | io_uring_cancel_generic(true, sqd); |
37d1e2e3 | 8159 | sqd->thread = NULL; |
05962f95 | 8160 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) |
5f3f26f9 | 8161 | io_ring_set_wakeup_flag(ctx); |
521d6a73 | 8162 | io_run_task_work(); |
734551df PB |
8163 | mutex_unlock(&sqd->lock); |
8164 | ||
5bd2182d PM |
8165 | audit_free(current); |
8166 | ||
37d1e2e3 JA |
8167 | complete(&sqd->exited); |
8168 | do_exit(0); | |
6c271ce2 JA |
8169 | } |
8170 | ||
bda52162 JA |
8171 | struct io_wait_queue { |
8172 | struct wait_queue_entry wq; | |
8173 | struct io_ring_ctx *ctx; | |
5fd46178 | 8174 | unsigned cq_tail; |
bda52162 | 8175 | unsigned nr_timeouts; |
adc8682e OL |
8176 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8177 | unsigned busy_poll_to; | |
8178 | #endif | |
bda52162 JA |
8179 | }; |
8180 | ||
6c503150 | 8181 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
bda52162 JA |
8182 | { |
8183 | struct io_ring_ctx *ctx = iowq->ctx; | |
5fd46178 | 8184 | int dist = ctx->cached_cq_tail - (int) iowq->cq_tail; |
bda52162 JA |
8185 | |
8186 | /* | |
d195a66e | 8187 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
8188 | * started waiting. For timeouts, we always want to return to userspace, |
8189 | * regardless of event count. | |
8190 | */ | |
5fd46178 | 8191 | return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
bda52162 JA |
8192 | } |
8193 | ||
8194 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
8195 | int wake_flags, void *key) | |
8196 | { | |
8197 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
8198 | wq); | |
8199 | ||
6c503150 PB |
8200 | /* |
8201 | * Cannot safely flush overflowed CQEs from here, ensure we wake up | |
8202 | * the task, and the next invocation will do it. | |
8203 | */ | |
5ed7a37d | 8204 | if (io_should_wake(iowq) || test_bit(0, &iowq->ctx->check_cq_overflow)) |
6c503150 PB |
8205 | return autoremove_wake_function(curr, mode, wake_flags, key); |
8206 | return -1; | |
bda52162 JA |
8207 | } |
8208 | ||
af9c1a44 JA |
8209 | static int io_run_task_work_sig(void) |
8210 | { | |
8211 | if (io_run_task_work()) | |
8212 | return 1; | |
0b8cfa97 | 8213 | if (test_thread_flag(TIF_NOTIFY_SIGNAL)) |
792ee0f6 | 8214 | return -ERESTARTSYS; |
c5020bc8 OL |
8215 | if (task_sigpending(current)) |
8216 | return -EINTR; | |
8217 | return 0; | |
af9c1a44 JA |
8218 | } |
8219 | ||
eeb60b9a PB |
8220 | /* when returns >0, the caller should retry */ |
8221 | static inline int io_cqring_wait_schedule(struct io_ring_ctx *ctx, | |
8222 | struct io_wait_queue *iowq, | |
22833966 | 8223 | ktime_t timeout) |
eeb60b9a PB |
8224 | { |
8225 | int ret; | |
8226 | ||
8227 | /* make sure we run task_work before checking for signals */ | |
8228 | ret = io_run_task_work_sig(); | |
8229 | if (ret || io_should_wake(iowq)) | |
8230 | return ret; | |
8231 | /* let the caller flush overflows, retry */ | |
5ed7a37d | 8232 | if (test_bit(0, &ctx->check_cq_overflow)) |
eeb60b9a PB |
8233 | return 1; |
8234 | ||
22833966 JA |
8235 | if (!schedule_hrtimeout(&timeout, HRTIMER_MODE_ABS)) |
8236 | return -ETIME; | |
8237 | return 1; | |
eeb60b9a PB |
8238 | } |
8239 | ||
adc8682e OL |
8240 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8241 | static void io_adjust_busy_loop_timeout(struct timespec64 *ts, | |
8242 | struct io_wait_queue *iowq) | |
8243 | { | |
8244 | unsigned busy_poll_to = READ_ONCE(sysctl_net_busy_poll); | |
8245 | struct timespec64 pollto = ns_to_timespec64(1000 * (s64)busy_poll_to); | |
8246 | ||
8247 | if (timespec64_compare(ts, &pollto) > 0) { | |
8248 | *ts = timespec64_sub(*ts, pollto); | |
8249 | iowq->busy_poll_to = busy_poll_to; | |
8250 | } else { | |
8251 | u64 to = timespec64_to_ns(ts); | |
8252 | ||
8253 | do_div(to, 1000); | |
8254 | iowq->busy_poll_to = to; | |
8255 | ts->tv_sec = 0; | |
8256 | ts->tv_nsec = 0; | |
8257 | } | |
8258 | } | |
8259 | ||
8260 | static inline bool io_busy_loop_timeout(unsigned long start_time, | |
8261 | unsigned long bp_usec) | |
8262 | { | |
8263 | if (bp_usec) { | |
8264 | unsigned long end_time = start_time + bp_usec; | |
8265 | unsigned long now = busy_loop_current_time(); | |
8266 | ||
8267 | return time_after(now, end_time); | |
8268 | } | |
8269 | return true; | |
8270 | } | |
8271 | ||
8272 | static bool io_busy_loop_end(void *p, unsigned long start_time) | |
8273 | { | |
8274 | struct io_wait_queue *iowq = p; | |
8275 | ||
8276 | return signal_pending(current) || | |
8277 | io_should_wake(iowq) || | |
8278 | io_busy_loop_timeout(start_time, iowq->busy_poll_to); | |
8279 | } | |
8280 | ||
8281 | static void io_blocking_napi_busy_loop(struct list_head *napi_list, | |
8282 | struct io_wait_queue *iowq) | |
8283 | { | |
8284 | unsigned long start_time = | |
8285 | list_is_singular(napi_list) ? 0 : | |
8286 | busy_loop_current_time(); | |
8287 | ||
8288 | do { | |
8289 | if (list_is_singular(napi_list)) { | |
8290 | struct napi_entry *ne = | |
8291 | list_first_entry(napi_list, | |
8292 | struct napi_entry, list); | |
8293 | ||
8294 | napi_busy_loop(ne->napi_id, io_busy_loop_end, iowq, | |
8295 | true, BUSY_POLL_BUDGET); | |
8296 | io_check_napi_entry_timeout(ne); | |
8297 | break; | |
8298 | } | |
8299 | } while (io_napi_busy_loop(napi_list) && | |
8300 | !io_busy_loop_end(iowq, start_time)); | |
8301 | } | |
8302 | ||
8303 | static void io_putback_napi_list(struct io_ring_ctx *ctx, | |
8304 | struct list_head *napi_list) | |
8305 | { | |
8306 | struct napi_entry *cne, *lne; | |
8307 | ||
8308 | spin_lock(&ctx->napi_lock); | |
8309 | list_for_each_entry(cne, &ctx->napi_list, list) | |
8310 | list_for_each_entry(lne, napi_list, list) | |
8311 | if (cne->napi_id == lne->napi_id) { | |
8312 | list_del(&lne->list); | |
8313 | kfree(lne); | |
8314 | break; | |
8315 | } | |
8316 | list_splice(napi_list, &ctx->napi_list); | |
8317 | spin_unlock(&ctx->napi_lock); | |
8318 | } | |
8319 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
8320 | ||
2b188cc1 JA |
8321 | /* |
8322 | * Wait until events become available, if we don't already have some. The | |
8323 | * application must reap them itself, as they reside on the shared cq ring. | |
8324 | */ | |
8325 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
c73ebb68 HX |
8326 | const sigset_t __user *sig, size_t sigsz, |
8327 | struct __kernel_timespec __user *uts) | |
2b188cc1 | 8328 | { |
90291099 | 8329 | struct io_wait_queue iowq; |
75b28aff | 8330 | struct io_rings *rings = ctx->rings; |
22833966 | 8331 | ktime_t timeout = KTIME_MAX; |
c1d5a224 | 8332 | int ret; |
adc8682e OL |
8333 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8334 | LIST_HEAD(local_napi_list); | |
8335 | #endif | |
2b188cc1 | 8336 | |
b41e9852 | 8337 | do { |
90f67366 | 8338 | io_cqring_overflow_flush(ctx); |
6c503150 | 8339 | if (io_cqring_events(ctx) >= min_events) |
b41e9852 | 8340 | return 0; |
4c6e277c | 8341 | if (!io_run_task_work()) |
b41e9852 | 8342 | break; |
b41e9852 | 8343 | } while (1); |
2b188cc1 JA |
8344 | |
8345 | if (sig) { | |
9e75ad5d AB |
8346 | #ifdef CONFIG_COMPAT |
8347 | if (in_compat_syscall()) | |
8348 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 8349 | sigsz); |
9e75ad5d AB |
8350 | else |
8351 | #endif | |
b772434b | 8352 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 8353 | |
2b188cc1 JA |
8354 | if (ret) |
8355 | return ret; | |
8356 | } | |
8357 | ||
adc8682e OL |
8358 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8359 | iowq.busy_poll_to = 0; | |
8360 | if (!(ctx->flags & IORING_SETUP_SQPOLL)) { | |
8361 | spin_lock(&ctx->napi_lock); | |
8362 | list_splice_init(&ctx->napi_list, &local_napi_list); | |
8363 | spin_unlock(&ctx->napi_lock); | |
8364 | } | |
8365 | #endif | |
950e79dd OL |
8366 | if (uts) { |
8367 | struct timespec64 ts; | |
8368 | ||
8369 | if (get_timespec64(&ts, uts)) | |
8370 | return -EFAULT; | |
adc8682e OL |
8371 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8372 | if (!list_empty(&local_napi_list)) | |
8373 | io_adjust_busy_loop_timeout(&ts, &iowq); | |
8374 | #endif | |
950e79dd OL |
8375 | timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns()); |
8376 | } | |
adc8682e OL |
8377 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8378 | else if (!list_empty(&local_napi_list)) | |
8379 | iowq.busy_poll_to = READ_ONCE(sysctl_net_busy_poll); | |
8380 | #endif | |
950e79dd | 8381 | |
90291099 PB |
8382 | init_waitqueue_func_entry(&iowq.wq, io_wake_function); |
8383 | iowq.wq.private = current; | |
8384 | INIT_LIST_HEAD(&iowq.wq.entry); | |
8385 | iowq.ctx = ctx; | |
bda52162 | 8386 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
5fd46178 | 8387 | iowq.cq_tail = READ_ONCE(ctx->rings->cq.head) + min_events; |
90291099 | 8388 | |
c826bd7a | 8389 | trace_io_uring_cqring_wait(ctx, min_events); |
adc8682e OL |
8390 | #ifdef CONFIG_NET_RX_BUSY_POLL |
8391 | if (iowq.busy_poll_to) | |
8392 | io_blocking_napi_busy_loop(&local_napi_list, &iowq); | |
8393 | if (!list_empty(&local_napi_list)) | |
8394 | io_putback_napi_list(ctx, &local_napi_list); | |
8395 | #endif | |
bda52162 | 8396 | do { |
ca0a2651 | 8397 | /* if we can't even flush overflow, don't wait for more */ |
90f67366 | 8398 | if (!io_cqring_overflow_flush(ctx)) { |
ca0a2651 JA |
8399 | ret = -EBUSY; |
8400 | break; | |
8401 | } | |
311997b3 | 8402 | prepare_to_wait_exclusive(&ctx->cq_wait, &iowq.wq, |
bda52162 | 8403 | TASK_INTERRUPTIBLE); |
22833966 | 8404 | ret = io_cqring_wait_schedule(ctx, &iowq, timeout); |
311997b3 | 8405 | finish_wait(&ctx->cq_wait, &iowq.wq); |
ca0a2651 | 8406 | cond_resched(); |
eeb60b9a | 8407 | } while (ret > 0); |
bda52162 | 8408 | |
b7db41c9 | 8409 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 8410 | |
75b28aff | 8411 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
8412 | } |
8413 | ||
9123c8ff | 8414 | static void io_free_page_table(void **table, size_t size) |
05f3fb3c | 8415 | { |
9123c8ff | 8416 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); |
05f3fb3c | 8417 | |
846a4ef2 | 8418 | for (i = 0; i < nr_tables; i++) |
9123c8ff PB |
8419 | kfree(table[i]); |
8420 | kfree(table); | |
8421 | } | |
8422 | ||
c072481d | 8423 | static __cold void **io_alloc_page_table(size_t size) |
9123c8ff PB |
8424 | { |
8425 | unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE); | |
8426 | size_t init_size = size; | |
8427 | void **table; | |
8428 | ||
0bea96f5 | 8429 | table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8430 | if (!table) |
8431 | return NULL; | |
8432 | ||
8433 | for (i = 0; i < nr_tables; i++) { | |
27f6b318 | 8434 | unsigned int this_size = min_t(size_t, size, PAGE_SIZE); |
9123c8ff | 8435 | |
0bea96f5 | 8436 | table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT); |
9123c8ff PB |
8437 | if (!table[i]) { |
8438 | io_free_page_table(table, init_size); | |
8439 | return NULL; | |
8440 | } | |
8441 | size -= this_size; | |
8442 | } | |
8443 | return table; | |
05f3fb3c JA |
8444 | } |
8445 | ||
28a9fe25 | 8446 | static void io_rsrc_node_destroy(struct io_rsrc_node *ref_node) |
1642b445 | 8447 | { |
28a9fe25 PB |
8448 | percpu_ref_exit(&ref_node->refs); |
8449 | kfree(ref_node); | |
1642b445 PB |
8450 | } |
8451 | ||
c072481d | 8452 | static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref) |
b9bd2bea PB |
8453 | { |
8454 | struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs); | |
8455 | struct io_ring_ctx *ctx = node->rsrc_data->ctx; | |
8456 | unsigned long flags; | |
8457 | bool first_add = false; | |
b36a2050 | 8458 | unsigned long delay = HZ; |
b9bd2bea PB |
8459 | |
8460 | spin_lock_irqsave(&ctx->rsrc_ref_lock, flags); | |
8461 | node->done = true; | |
8462 | ||
b36a2050 DY |
8463 | /* if we are mid-quiesce then do not delay */ |
8464 | if (node->rsrc_data->quiesce) | |
8465 | delay = 0; | |
8466 | ||
b9bd2bea PB |
8467 | while (!list_empty(&ctx->rsrc_ref_list)) { |
8468 | node = list_first_entry(&ctx->rsrc_ref_list, | |
8469 | struct io_rsrc_node, node); | |
8470 | /* recycle ref nodes in order */ | |
8471 | if (!node->done) | |
8472 | break; | |
8473 | list_del(&node->node); | |
8474 | first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist); | |
8475 | } | |
8476 | spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags); | |
8477 | ||
8478 | if (first_add) | |
b36a2050 | 8479 | mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay); |
b9bd2bea PB |
8480 | } |
8481 | ||
f6133fbd | 8482 | static struct io_rsrc_node *io_rsrc_node_alloc(void) |
b9bd2bea PB |
8483 | { |
8484 | struct io_rsrc_node *ref_node; | |
8485 | ||
8486 | ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL); | |
8487 | if (!ref_node) | |
8488 | return NULL; | |
8489 | ||
8490 | if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero, | |
8491 | 0, GFP_KERNEL)) { | |
8492 | kfree(ref_node); | |
8493 | return NULL; | |
8494 | } | |
8495 | INIT_LIST_HEAD(&ref_node->node); | |
8496 | INIT_LIST_HEAD(&ref_node->rsrc_list); | |
8497 | ref_node->done = false; | |
8498 | return ref_node; | |
8499 | } | |
8500 | ||
a7f0ed5a PB |
8501 | static void io_rsrc_node_switch(struct io_ring_ctx *ctx, |
8502 | struct io_rsrc_data *data_to_kill) | |
ab409402 | 8503 | __must_hold(&ctx->uring_lock) |
6b06314c | 8504 | { |
a7f0ed5a PB |
8505 | WARN_ON_ONCE(!ctx->rsrc_backup_node); |
8506 | WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node); | |
6b06314c | 8507 | |
ab409402 PB |
8508 | io_rsrc_refs_drop(ctx); |
8509 | ||
a7f0ed5a PB |
8510 | if (data_to_kill) { |
8511 | struct io_rsrc_node *rsrc_node = ctx->rsrc_node; | |
82fbcfa9 | 8512 | |
a7f0ed5a | 8513 | rsrc_node->rsrc_data = data_to_kill; |
4956b9ea | 8514 | spin_lock_irq(&ctx->rsrc_ref_lock); |
a7f0ed5a | 8515 | list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list); |
4956b9ea | 8516 | spin_unlock_irq(&ctx->rsrc_ref_lock); |
82fbcfa9 | 8517 | |
3e942498 | 8518 | atomic_inc(&data_to_kill->refs); |
a7f0ed5a PB |
8519 | percpu_ref_kill(&rsrc_node->refs); |
8520 | ctx->rsrc_node = NULL; | |
8521 | } | |
6b06314c | 8522 | |
a7f0ed5a PB |
8523 | if (!ctx->rsrc_node) { |
8524 | ctx->rsrc_node = ctx->rsrc_backup_node; | |
8525 | ctx->rsrc_backup_node = NULL; | |
8526 | } | |
8bad28d8 HX |
8527 | } |
8528 | ||
a7f0ed5a | 8529 | static int io_rsrc_node_switch_start(struct io_ring_ctx *ctx) |
8dd03afe PB |
8530 | { |
8531 | if (ctx->rsrc_backup_node) | |
8532 | return 0; | |
f6133fbd | 8533 | ctx->rsrc_backup_node = io_rsrc_node_alloc(); |
8dd03afe | 8534 | return ctx->rsrc_backup_node ? 0 : -ENOMEM; |
8bad28d8 HX |
8535 | } |
8536 | ||
c072481d PB |
8537 | static __cold int io_rsrc_ref_quiesce(struct io_rsrc_data *data, |
8538 | struct io_ring_ctx *ctx) | |
8bad28d8 HX |
8539 | { |
8540 | int ret; | |
05589553 | 8541 | |
215c3902 | 8542 | /* As we may drop ->uring_lock, other task may have started quiesce */ |
8bad28d8 HX |
8543 | if (data->quiesce) |
8544 | return -ENXIO; | |
05589553 | 8545 | |
8bad28d8 | 8546 | data->quiesce = true; |
1ffc5422 | 8547 | do { |
a7f0ed5a | 8548 | ret = io_rsrc_node_switch_start(ctx); |
8dd03afe | 8549 | if (ret) |
f2303b1f | 8550 | break; |
a7f0ed5a | 8551 | io_rsrc_node_switch(ctx, data); |
f2303b1f | 8552 | |
3e942498 PB |
8553 | /* kill initial ref, already quiesced if zero */ |
8554 | if (atomic_dec_and_test(&data->refs)) | |
8555 | break; | |
c018db4a | 8556 | mutex_unlock(&ctx->uring_lock); |
8bad28d8 | 8557 | flush_delayed_work(&ctx->rsrc_put_work); |
1ffc5422 | 8558 | ret = wait_for_completion_interruptible(&data->done); |
c018db4a JA |
8559 | if (!ret) { |
8560 | mutex_lock(&ctx->uring_lock); | |
80912cef DY |
8561 | if (atomic_read(&data->refs) > 0) { |
8562 | /* | |
8563 | * it has been revived by another thread while | |
8564 | * we were unlocked | |
8565 | */ | |
8566 | mutex_unlock(&ctx->uring_lock); | |
8567 | } else { | |
8568 | break; | |
8569 | } | |
c018db4a | 8570 | } |
8bad28d8 | 8571 | |
3e942498 PB |
8572 | atomic_inc(&data->refs); |
8573 | /* wait for all works potentially completing data->done */ | |
8574 | flush_delayed_work(&ctx->rsrc_put_work); | |
cb5e1b81 | 8575 | reinit_completion(&data->done); |
8dd03afe | 8576 | |
1ffc5422 | 8577 | ret = io_run_task_work_sig(); |
8bad28d8 | 8578 | mutex_lock(&ctx->uring_lock); |
f2303b1f | 8579 | } while (ret >= 0); |
8bad28d8 | 8580 | data->quiesce = false; |
05f3fb3c | 8581 | |
8bad28d8 | 8582 | return ret; |
d7954b2b BM |
8583 | } |
8584 | ||
2d091d62 PB |
8585 | static u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) |
8586 | { | |
8587 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
8588 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
8589 | ||
8590 | return &data->tags[table_idx][off]; | |
8591 | } | |
8592 | ||
44b31f2f | 8593 | static void io_rsrc_data_free(struct io_rsrc_data *data) |
1ad555c6 | 8594 | { |
2d091d62 PB |
8595 | size_t size = data->nr * sizeof(data->tags[0][0]); |
8596 | ||
8597 | if (data->tags) | |
8598 | io_free_page_table((void **)data->tags, size); | |
44b31f2f PB |
8599 | kfree(data); |
8600 | } | |
8601 | ||
c072481d PB |
8602 | static __cold int io_rsrc_data_alloc(struct io_ring_ctx *ctx, rsrc_put_fn *do_put, |
8603 | u64 __user *utags, unsigned nr, | |
8604 | struct io_rsrc_data **pdata) | |
1ad555c6 | 8605 | { |
b895c9a6 | 8606 | struct io_rsrc_data *data; |
2d091d62 | 8607 | int ret = -ENOMEM; |
d878c816 | 8608 | unsigned i; |
1ad555c6 BM |
8609 | |
8610 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
8611 | if (!data) | |
d878c816 | 8612 | return -ENOMEM; |
2d091d62 | 8613 | data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0])); |
b60c8dce | 8614 | if (!data->tags) { |
1ad555c6 | 8615 | kfree(data); |
d878c816 PB |
8616 | return -ENOMEM; |
8617 | } | |
2d091d62 PB |
8618 | |
8619 | data->nr = nr; | |
8620 | data->ctx = ctx; | |
8621 | data->do_put = do_put; | |
d878c816 | 8622 | if (utags) { |
2d091d62 | 8623 | ret = -EFAULT; |
d878c816 | 8624 | for (i = 0; i < nr; i++) { |
fdd1dc31 CIK |
8625 | u64 *tag_slot = io_get_tag_slot(data, i); |
8626 | ||
8627 | if (copy_from_user(tag_slot, &utags[i], | |
8628 | sizeof(*tag_slot))) | |
2d091d62 | 8629 | goto fail; |
d878c816 | 8630 | } |
1ad555c6 | 8631 | } |
b60c8dce | 8632 | |
3e942498 | 8633 | atomic_set(&data->refs, 1); |
1ad555c6 | 8634 | init_completion(&data->done); |
d878c816 PB |
8635 | *pdata = data; |
8636 | return 0; | |
2d091d62 PB |
8637 | fail: |
8638 | io_rsrc_data_free(data); | |
8639 | return ret; | |
1ad555c6 BM |
8640 | } |
8641 | ||
9123c8ff PB |
8642 | static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files) |
8643 | { | |
0bea96f5 PB |
8644 | table->files = kvcalloc(nr_files, sizeof(table->files[0]), |
8645 | GFP_KERNEL_ACCOUNT); | |
9123c8ff PB |
8646 | return !!table->files; |
8647 | } | |
8648 | ||
042b0d85 | 8649 | static void io_free_file_tables(struct io_file_table *table) |
9123c8ff | 8650 | { |
042b0d85 | 8651 | kvfree(table->files); |
9123c8ff PB |
8652 | table->files = NULL; |
8653 | } | |
8654 | ||
fff4db76 | 8655 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
1ad555c6 | 8656 | { |
fff4db76 PB |
8657 | #if defined(CONFIG_UNIX) |
8658 | if (ctx->ring_sock) { | |
8659 | struct sock *sock = ctx->ring_sock->sk; | |
8660 | struct sk_buff *skb; | |
8661 | ||
8662 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
8663 | kfree_skb(skb); | |
8664 | } | |
8665 | #else | |
8666 | int i; | |
8667 | ||
8668 | for (i = 0; i < ctx->nr_user_files; i++) { | |
8669 | struct file *file; | |
8670 | ||
8671 | file = io_file_from_index(ctx, i); | |
8672 | if (file) | |
8673 | fput(file); | |
8674 | } | |
8675 | #endif | |
042b0d85 | 8676 | io_free_file_tables(&ctx->file_table); |
44b31f2f | 8677 | io_rsrc_data_free(ctx->file_data); |
fff4db76 PB |
8678 | ctx->file_data = NULL; |
8679 | ctx->nr_user_files = 0; | |
1ad555c6 BM |
8680 | } |
8681 | ||
d7954b2b BM |
8682 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
8683 | { | |
d7954b2b BM |
8684 | int ret; |
8685 | ||
08480400 | 8686 | if (!ctx->file_data) |
d7954b2b | 8687 | return -ENXIO; |
08480400 PB |
8688 | ret = io_rsrc_ref_quiesce(ctx->file_data, ctx); |
8689 | if (!ret) | |
8690 | __io_sqe_files_unregister(ctx); | |
8691 | return ret; | |
6b06314c JA |
8692 | } |
8693 | ||
37d1e2e3 | 8694 | static void io_sq_thread_unpark(struct io_sq_data *sqd) |
09a6f4ef | 8695 | __releases(&sqd->lock) |
37d1e2e3 | 8696 | { |
521d6a73 PB |
8697 | WARN_ON_ONCE(sqd->thread == current); |
8698 | ||
9e138a48 PB |
8699 | /* |
8700 | * Do the dance but not conditional clear_bit() because it'd race with | |
8701 | * other threads incrementing park_pending and setting the bit. | |
8702 | */ | |
37d1e2e3 | 8703 | clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
9e138a48 PB |
8704 | if (atomic_dec_return(&sqd->park_pending)) |
8705 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); | |
09a6f4ef | 8706 | mutex_unlock(&sqd->lock); |
37d1e2e3 JA |
8707 | } |
8708 | ||
86e0d676 | 8709 | static void io_sq_thread_park(struct io_sq_data *sqd) |
09a6f4ef | 8710 | __acquires(&sqd->lock) |
37d1e2e3 | 8711 | { |
521d6a73 PB |
8712 | WARN_ON_ONCE(sqd->thread == current); |
8713 | ||
9e138a48 | 8714 | atomic_inc(&sqd->park_pending); |
86e0d676 | 8715 | set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state); |
09a6f4ef | 8716 | mutex_lock(&sqd->lock); |
05962f95 | 8717 | if (sqd->thread) |
86e0d676 | 8718 | wake_up_process(sqd->thread); |
37d1e2e3 JA |
8719 | } |
8720 | ||
8721 | static void io_sq_thread_stop(struct io_sq_data *sqd) | |
8722 | { | |
521d6a73 | 8723 | WARN_ON_ONCE(sqd->thread == current); |
88885f66 | 8724 | WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)); |
521d6a73 | 8725 | |
05962f95 | 8726 | set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state); |
88885f66 | 8727 | mutex_lock(&sqd->lock); |
e8f98f24 JA |
8728 | if (sqd->thread) |
8729 | wake_up_process(sqd->thread); | |
09a6f4ef | 8730 | mutex_unlock(&sqd->lock); |
05962f95 | 8731 | wait_for_completion(&sqd->exited); |
37d1e2e3 JA |
8732 | } |
8733 | ||
534ca6d6 | 8734 | static void io_put_sq_data(struct io_sq_data *sqd) |
6c271ce2 | 8735 | { |
534ca6d6 | 8736 | if (refcount_dec_and_test(&sqd->refs)) { |
9e138a48 PB |
8737 | WARN_ON_ONCE(atomic_read(&sqd->park_pending)); |
8738 | ||
37d1e2e3 JA |
8739 | io_sq_thread_stop(sqd); |
8740 | kfree(sqd); | |
8741 | } | |
8742 | } | |
8743 | ||
8744 | static void io_sq_thread_finish(struct io_ring_ctx *ctx) | |
8745 | { | |
8746 | struct io_sq_data *sqd = ctx->sq_data; | |
8747 | ||
8748 | if (sqd) { | |
05962f95 | 8749 | io_sq_thread_park(sqd); |
521d6a73 | 8750 | list_del_init(&ctx->sqd_list); |
37d1e2e3 | 8751 | io_sqd_update_thread_idle(sqd); |
05962f95 | 8752 | io_sq_thread_unpark(sqd); |
37d1e2e3 JA |
8753 | |
8754 | io_put_sq_data(sqd); | |
8755 | ctx->sq_data = NULL; | |
534ca6d6 JA |
8756 | } |
8757 | } | |
8758 | ||
aa06165d JA |
8759 | static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p) |
8760 | { | |
8761 | struct io_ring_ctx *ctx_attach; | |
8762 | struct io_sq_data *sqd; | |
8763 | struct fd f; | |
8764 | ||
8765 | f = fdget(p->wq_fd); | |
8766 | if (!f.file) | |
8767 | return ERR_PTR(-ENXIO); | |
8768 | if (f.file->f_op != &io_uring_fops) { | |
8769 | fdput(f); | |
8770 | return ERR_PTR(-EINVAL); | |
8771 | } | |
8772 | ||
8773 | ctx_attach = f.file->private_data; | |
8774 | sqd = ctx_attach->sq_data; | |
8775 | if (!sqd) { | |
8776 | fdput(f); | |
8777 | return ERR_PTR(-EINVAL); | |
8778 | } | |
5c2469e0 JA |
8779 | if (sqd->task_tgid != current->tgid) { |
8780 | fdput(f); | |
8781 | return ERR_PTR(-EPERM); | |
8782 | } | |
aa06165d JA |
8783 | |
8784 | refcount_inc(&sqd->refs); | |
8785 | fdput(f); | |
8786 | return sqd; | |
8787 | } | |
8788 | ||
26984fbf PB |
8789 | static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, |
8790 | bool *attached) | |
534ca6d6 JA |
8791 | { |
8792 | struct io_sq_data *sqd; | |
8793 | ||
26984fbf | 8794 | *attached = false; |
5c2469e0 JA |
8795 | if (p->flags & IORING_SETUP_ATTACH_WQ) { |
8796 | sqd = io_attach_sq_data(p); | |
26984fbf PB |
8797 | if (!IS_ERR(sqd)) { |
8798 | *attached = true; | |
5c2469e0 | 8799 | return sqd; |
26984fbf | 8800 | } |
5c2469e0 JA |
8801 | /* fall through for EPERM case, setup new sqd/task */ |
8802 | if (PTR_ERR(sqd) != -EPERM) | |
8803 | return sqd; | |
8804 | } | |
aa06165d | 8805 | |
534ca6d6 JA |
8806 | sqd = kzalloc(sizeof(*sqd), GFP_KERNEL); |
8807 | if (!sqd) | |
8808 | return ERR_PTR(-ENOMEM); | |
8809 | ||
9e138a48 | 8810 | atomic_set(&sqd->park_pending, 0); |
534ca6d6 | 8811 | refcount_set(&sqd->refs, 1); |
69fb2131 | 8812 | INIT_LIST_HEAD(&sqd->ctx_list); |
09a6f4ef | 8813 | mutex_init(&sqd->lock); |
534ca6d6 | 8814 | init_waitqueue_head(&sqd->wait); |
37d1e2e3 | 8815 | init_completion(&sqd->exited); |
534ca6d6 JA |
8816 | return sqd; |
8817 | } | |
8818 | ||
6b06314c | 8819 | #if defined(CONFIG_UNIX) |
6b06314c JA |
8820 | /* |
8821 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
8822 | * the io_uring can be safely unregistered on process exit, even if we have | |
8823 | * loops in the file referencing. | |
8824 | */ | |
8825 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
8826 | { | |
8827 | struct sock *sk = ctx->ring_sock->sk; | |
8828 | struct scm_fp_list *fpl; | |
8829 | struct sk_buff *skb; | |
08a45173 | 8830 | int i, nr_files; |
6b06314c | 8831 | |
6b06314c JA |
8832 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
8833 | if (!fpl) | |
8834 | return -ENOMEM; | |
8835 | ||
8836 | skb = alloc_skb(0, GFP_KERNEL); | |
8837 | if (!skb) { | |
8838 | kfree(fpl); | |
8839 | return -ENOMEM; | |
8840 | } | |
8841 | ||
8842 | skb->sk = sk; | |
6b06314c | 8843 | |
08a45173 | 8844 | nr_files = 0; |
62e398be | 8845 | fpl->user = get_uid(current_user()); |
6b06314c | 8846 | for (i = 0; i < nr; i++) { |
65e19f54 JA |
8847 | struct file *file = io_file_from_index(ctx, i + offset); |
8848 | ||
8849 | if (!file) | |
08a45173 | 8850 | continue; |
65e19f54 | 8851 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
8852 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
8853 | nr_files++; | |
6b06314c JA |
8854 | } |
8855 | ||
08a45173 JA |
8856 | if (nr_files) { |
8857 | fpl->max = SCM_MAX_FD; | |
8858 | fpl->count = nr_files; | |
8859 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 8860 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
8861 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
8862 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 8863 | |
08a45173 JA |
8864 | for (i = 0; i < nr_files; i++) |
8865 | fput(fpl->fp[i]); | |
8866 | } else { | |
8867 | kfree_skb(skb); | |
c86d18f4 | 8868 | free_uid(fpl->user); |
08a45173 JA |
8869 | kfree(fpl); |
8870 | } | |
6b06314c JA |
8871 | |
8872 | return 0; | |
8873 | } | |
8874 | ||
8875 | /* | |
8876 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
8877 | * causes regular reference counting to break down. We rely on the UNIX | |
8878 | * garbage collection to take care of this problem for us. | |
8879 | */ | |
8880 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8881 | { | |
8882 | unsigned left, total; | |
8883 | int ret = 0; | |
8884 | ||
8885 | total = 0; | |
8886 | left = ctx->nr_user_files; | |
8887 | while (left) { | |
8888 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
8889 | |
8890 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
8891 | if (ret) | |
8892 | break; | |
8893 | left -= this_files; | |
8894 | total += this_files; | |
8895 | } | |
8896 | ||
8897 | if (!ret) | |
8898 | return 0; | |
8899 | ||
8900 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
8901 | struct file *file = io_file_from_index(ctx, total); |
8902 | ||
8903 | if (file) | |
8904 | fput(file); | |
6b06314c JA |
8905 | total++; |
8906 | } | |
8907 | ||
8908 | return ret; | |
8909 | } | |
8910 | #else | |
8911 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
8912 | { | |
8913 | return 0; | |
8914 | } | |
8915 | #endif | |
8916 | ||
47e90392 | 8917 | static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
05f3fb3c | 8918 | { |
50238531 | 8919 | struct file *file = prsrc->file; |
05f3fb3c JA |
8920 | #if defined(CONFIG_UNIX) |
8921 | struct sock *sock = ctx->ring_sock->sk; | |
8922 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
8923 | struct sk_buff *skb; | |
8924 | int i; | |
8925 | ||
8926 | __skb_queue_head_init(&list); | |
8927 | ||
8928 | /* | |
8929 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
8930 | * remove this entry and rearrange the file array. | |
8931 | */ | |
8932 | skb = skb_dequeue(head); | |
8933 | while (skb) { | |
8934 | struct scm_fp_list *fp; | |
8935 | ||
8936 | fp = UNIXCB(skb).fp; | |
8937 | for (i = 0; i < fp->count; i++) { | |
8938 | int left; | |
8939 | ||
8940 | if (fp->fp[i] != file) | |
8941 | continue; | |
8942 | ||
8943 | unix_notinflight(fp->user, fp->fp[i]); | |
8944 | left = fp->count - 1 - i; | |
8945 | if (left) { | |
8946 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
8947 | left * sizeof(struct file *)); | |
8948 | } | |
8949 | fp->count--; | |
8950 | if (!fp->count) { | |
8951 | kfree_skb(skb); | |
8952 | skb = NULL; | |
8953 | } else { | |
8954 | __skb_queue_tail(&list, skb); | |
8955 | } | |
8956 | fput(file); | |
8957 | file = NULL; | |
8958 | break; | |
8959 | } | |
8960 | ||
8961 | if (!file) | |
8962 | break; | |
8963 | ||
8964 | __skb_queue_tail(&list, skb); | |
8965 | ||
8966 | skb = skb_dequeue(head); | |
8967 | } | |
8968 | ||
8969 | if (skb_peek(&list)) { | |
8970 | spin_lock_irq(&head->lock); | |
8971 | while ((skb = __skb_dequeue(&list)) != NULL) | |
8972 | __skb_queue_tail(head, skb); | |
8973 | spin_unlock_irq(&head->lock); | |
8974 | } | |
8975 | #else | |
8976 | fput(file); | |
8977 | #endif | |
8978 | } | |
8979 | ||
b895c9a6 | 8980 | static void __io_rsrc_put_work(struct io_rsrc_node *ref_node) |
65e19f54 | 8981 | { |
b895c9a6 | 8982 | struct io_rsrc_data *rsrc_data = ref_node->rsrc_data; |
269bbe5f BM |
8983 | struct io_ring_ctx *ctx = rsrc_data->ctx; |
8984 | struct io_rsrc_put *prsrc, *tmp; | |
05589553 | 8985 | |
269bbe5f BM |
8986 | list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) { |
8987 | list_del(&prsrc->list); | |
b60c8dce PB |
8988 | |
8989 | if (prsrc->tag) { | |
8990 | bool lock_ring = ctx->flags & IORING_SETUP_IOPOLL; | |
b60c8dce PB |
8991 | |
8992 | io_ring_submit_lock(ctx, lock_ring); | |
79ebeaee | 8993 | spin_lock(&ctx->completion_lock); |
913a571a | 8994 | io_fill_cqe_aux(ctx, prsrc->tag, 0, 0); |
b60c8dce | 8995 | io_commit_cqring(ctx); |
79ebeaee | 8996 | spin_unlock(&ctx->completion_lock); |
b60c8dce PB |
8997 | io_cqring_ev_posted(ctx); |
8998 | io_ring_submit_unlock(ctx, lock_ring); | |
8999 | } | |
9000 | ||
40ae0ff7 | 9001 | rsrc_data->do_put(ctx, prsrc); |
269bbe5f | 9002 | kfree(prsrc); |
65e19f54 | 9003 | } |
05589553 | 9004 | |
28a9fe25 | 9005 | io_rsrc_node_destroy(ref_node); |
3e942498 PB |
9006 | if (atomic_dec_and_test(&rsrc_data->refs)) |
9007 | complete(&rsrc_data->done); | |
2faf852d | 9008 | } |
65e19f54 | 9009 | |
269bbe5f | 9010 | static void io_rsrc_put_work(struct work_struct *work) |
4a38aed2 JA |
9011 | { |
9012 | struct io_ring_ctx *ctx; | |
9013 | struct llist_node *node; | |
9014 | ||
269bbe5f BM |
9015 | ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work); |
9016 | node = llist_del_all(&ctx->rsrc_put_llist); | |
4a38aed2 JA |
9017 | |
9018 | while (node) { | |
b895c9a6 | 9019 | struct io_rsrc_node *ref_node; |
4a38aed2 JA |
9020 | struct llist_node *next = node->next; |
9021 | ||
b895c9a6 | 9022 | ref_node = llist_entry(node, struct io_rsrc_node, llist); |
269bbe5f | 9023 | __io_rsrc_put_work(ref_node); |
4a38aed2 JA |
9024 | node = next; |
9025 | } | |
9026 | } | |
9027 | ||
6b06314c | 9028 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
792e3582 | 9029 | unsigned nr_args, u64 __user *tags) |
6b06314c JA |
9030 | { |
9031 | __s32 __user *fds = (__s32 __user *) arg; | |
05f3fb3c | 9032 | struct file *file; |
f3baed39 | 9033 | int fd, ret; |
846a4ef2 | 9034 | unsigned i; |
6b06314c | 9035 | |
05f3fb3c | 9036 | if (ctx->file_data) |
6b06314c JA |
9037 | return -EBUSY; |
9038 | if (!nr_args) | |
9039 | return -EINVAL; | |
9040 | if (nr_args > IORING_MAX_FIXED_FILES) | |
9041 | return -EMFILE; | |
3a1b8a4e PB |
9042 | if (nr_args > rlimit(RLIMIT_NOFILE)) |
9043 | return -EMFILE; | |
a7f0ed5a | 9044 | ret = io_rsrc_node_switch_start(ctx); |
f3baed39 PB |
9045 | if (ret) |
9046 | return ret; | |
d878c816 PB |
9047 | ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args, |
9048 | &ctx->file_data); | |
9049 | if (ret) | |
9050 | return ret; | |
6b06314c | 9051 | |
f3baed39 | 9052 | ret = -ENOMEM; |
aeca241b | 9053 | if (!io_alloc_file_tables(&ctx->file_table, nr_args)) |
1ad555c6 | 9054 | goto out_free; |
65e19f54 | 9055 | |
08a45173 | 9056 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
d878c816 | 9057 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) { |
600cf3f8 PB |
9058 | ret = -EFAULT; |
9059 | goto out_fput; | |
9060 | } | |
08a45173 | 9061 | /* allow sparse sets */ |
792e3582 PB |
9062 | if (fd == -1) { |
9063 | ret = -EINVAL; | |
2d091d62 | 9064 | if (unlikely(*io_get_tag_slot(ctx->file_data, i))) |
792e3582 | 9065 | goto out_fput; |
08a45173 | 9066 | continue; |
792e3582 | 9067 | } |
6b06314c | 9068 | |
05f3fb3c | 9069 | file = fget(fd); |
6b06314c | 9070 | ret = -EBADF; |
792e3582 | 9071 | if (unlikely(!file)) |
600cf3f8 | 9072 | goto out_fput; |
05f3fb3c | 9073 | |
6b06314c JA |
9074 | /* |
9075 | * Don't allow io_uring instances to be registered. If UNIX | |
9076 | * isn't enabled, then this causes a reference cycle and this | |
9077 | * instance can never get freed. If UNIX is enabled we'll | |
9078 | * handle it just fine, but there's still no point in allowing | |
9079 | * a ring fd as it doesn't support regular read/write anyway. | |
9080 | */ | |
05f3fb3c JA |
9081 | if (file->f_op == &io_uring_fops) { |
9082 | fput(file); | |
600cf3f8 | 9083 | goto out_fput; |
6b06314c | 9084 | } |
aeca241b | 9085 | io_fixed_file_set(io_fixed_file_slot(&ctx->file_table, i), file); |
6b06314c JA |
9086 | } |
9087 | ||
6b06314c | 9088 | ret = io_sqe_files_scm(ctx); |
05589553 | 9089 | if (ret) { |
08480400 | 9090 | __io_sqe_files_unregister(ctx); |
05589553 XW |
9091 | return ret; |
9092 | } | |
6b06314c | 9093 | |
a7f0ed5a | 9094 | io_rsrc_node_switch(ctx, NULL); |
6b06314c | 9095 | return ret; |
600cf3f8 PB |
9096 | out_fput: |
9097 | for (i = 0; i < ctx->nr_user_files; i++) { | |
9098 | file = io_file_from_index(ctx, i); | |
9099 | if (file) | |
9100 | fput(file); | |
9101 | } | |
042b0d85 | 9102 | io_free_file_tables(&ctx->file_table); |
600cf3f8 | 9103 | ctx->nr_user_files = 0; |
600cf3f8 | 9104 | out_free: |
44b31f2f | 9105 | io_rsrc_data_free(ctx->file_data); |
55cbc256 | 9106 | ctx->file_data = NULL; |
6b06314c JA |
9107 | return ret; |
9108 | } | |
9109 | ||
c3a31e60 JA |
9110 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
9111 | int index) | |
9112 | { | |
9113 | #if defined(CONFIG_UNIX) | |
9114 | struct sock *sock = ctx->ring_sock->sk; | |
9115 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
9116 | struct sk_buff *skb; | |
9117 | ||
9118 | /* | |
9119 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
9120 | * file set. If there's no room, fall back to allocating a new skb | |
9121 | * and filling it in. | |
9122 | */ | |
9123 | spin_lock_irq(&head->lock); | |
9124 | skb = skb_peek(head); | |
9125 | if (skb) { | |
9126 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
9127 | ||
9128 | if (fpl->count < SCM_MAX_FD) { | |
9129 | __skb_unlink(skb, head); | |
9130 | spin_unlock_irq(&head->lock); | |
9131 | fpl->fp[fpl->count] = get_file(file); | |
9132 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
9133 | fpl->count++; | |
9134 | spin_lock_irq(&head->lock); | |
9135 | __skb_queue_head(head, skb); | |
9136 | } else { | |
9137 | skb = NULL; | |
9138 | } | |
9139 | } | |
9140 | spin_unlock_irq(&head->lock); | |
9141 | ||
9142 | if (skb) { | |
9143 | fput(file); | |
9144 | return 0; | |
9145 | } | |
9146 | ||
9147 | return __io_sqe_files_scm(ctx, 1, index); | |
9148 | #else | |
9149 | return 0; | |
9150 | #endif | |
9151 | } | |
9152 | ||
9c7b0ba8 PB |
9153 | static int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, |
9154 | struct io_rsrc_node *node, void *rsrc) | |
9155 | { | |
9156 | struct io_rsrc_put *prsrc; | |
9157 | ||
9158 | prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL); | |
9159 | if (!prsrc) | |
9160 | return -ENOMEM; | |
9161 | ||
9162 | prsrc->tag = *io_get_tag_slot(data, idx); | |
9163 | prsrc->rsrc = rsrc; | |
9164 | list_add(&prsrc->list, &node->rsrc_list); | |
9165 | return 0; | |
9166 | } | |
9167 | ||
b9445598 PB |
9168 | static int io_install_fixed_file(struct io_kiocb *req, struct file *file, |
9169 | unsigned int issue_flags, u32 slot_index) | |
9170 | { | |
9171 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 9172 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
9c7b0ba8 | 9173 | bool needs_switch = false; |
b9445598 PB |
9174 | struct io_fixed_file *file_slot; |
9175 | int ret = -EBADF; | |
9176 | ||
3b44b371 | 9177 | io_ring_submit_lock(ctx, needs_lock); |
b9445598 PB |
9178 | if (file->f_op == &io_uring_fops) |
9179 | goto err; | |
9180 | ret = -ENXIO; | |
9181 | if (!ctx->file_data) | |
9182 | goto err; | |
9183 | ret = -EINVAL; | |
9184 | if (slot_index >= ctx->nr_user_files) | |
9185 | goto err; | |
9186 | ||
9187 | slot_index = array_index_nospec(slot_index, ctx->nr_user_files); | |
9188 | file_slot = io_fixed_file_slot(&ctx->file_table, slot_index); | |
9c7b0ba8 PB |
9189 | |
9190 | if (file_slot->file_ptr) { | |
9191 | struct file *old_file; | |
9192 | ||
9193 | ret = io_rsrc_node_switch_start(ctx); | |
9194 | if (ret) | |
9195 | goto err; | |
9196 | ||
9197 | old_file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9198 | ret = io_queue_rsrc_removal(ctx->file_data, slot_index, | |
9199 | ctx->rsrc_node, old_file); | |
9200 | if (ret) | |
9201 | goto err; | |
9202 | file_slot->file_ptr = 0; | |
9203 | needs_switch = true; | |
9204 | } | |
b9445598 PB |
9205 | |
9206 | *io_get_tag_slot(ctx->file_data, slot_index) = 0; | |
9207 | io_fixed_file_set(file_slot, file); | |
9208 | ret = io_sqe_file_register(ctx, file, slot_index); | |
9209 | if (ret) { | |
9210 | file_slot->file_ptr = 0; | |
9211 | goto err; | |
9212 | } | |
9213 | ||
9214 | ret = 0; | |
9215 | err: | |
9c7b0ba8 PB |
9216 | if (needs_switch) |
9217 | io_rsrc_node_switch(ctx, ctx->file_data); | |
3b44b371 | 9218 | io_ring_submit_unlock(ctx, needs_lock); |
b9445598 PB |
9219 | if (ret) |
9220 | fput(file); | |
9221 | return ret; | |
9222 | } | |
9223 | ||
7df778be PB |
9224 | static int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags) |
9225 | { | |
9226 | unsigned int offset = req->close.file_slot - 1; | |
9227 | struct io_ring_ctx *ctx = req->ctx; | |
3b44b371 | 9228 | bool needs_lock = issue_flags & IO_URING_F_UNLOCKED; |
7df778be PB |
9229 | struct io_fixed_file *file_slot; |
9230 | struct file *file; | |
9231 | int ret, i; | |
9232 | ||
3b44b371 | 9233 | io_ring_submit_lock(ctx, needs_lock); |
7df778be PB |
9234 | ret = -ENXIO; |
9235 | if (unlikely(!ctx->file_data)) | |
9236 | goto out; | |
9237 | ret = -EINVAL; | |
9238 | if (offset >= ctx->nr_user_files) | |
9239 | goto out; | |
9240 | ret = io_rsrc_node_switch_start(ctx); | |
9241 | if (ret) | |
9242 | goto out; | |
9243 | ||
9244 | i = array_index_nospec(offset, ctx->nr_user_files); | |
9245 | file_slot = io_fixed_file_slot(&ctx->file_table, i); | |
9246 | ret = -EBADF; | |
9247 | if (!file_slot->file_ptr) | |
9248 | goto out; | |
9249 | ||
9250 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
9251 | ret = io_queue_rsrc_removal(ctx->file_data, offset, ctx->rsrc_node, file); | |
9252 | if (ret) | |
9253 | goto out; | |
9254 | ||
9255 | file_slot->file_ptr = 0; | |
9256 | io_rsrc_node_switch(ctx, ctx->file_data); | |
9257 | ret = 0; | |
9258 | out: | |
3b44b371 | 9259 | io_ring_submit_unlock(ctx, needs_lock); |
7df778be PB |
9260 | return ret; |
9261 | } | |
9262 | ||
05f3fb3c | 9263 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
c3bdad02 | 9264 | struct io_uring_rsrc_update2 *up, |
05f3fb3c JA |
9265 | unsigned nr_args) |
9266 | { | |
c3bdad02 | 9267 | u64 __user *tags = u64_to_user_ptr(up->tags); |
98f0b3b4 | 9268 | __s32 __user *fds = u64_to_user_ptr(up->data); |
b895c9a6 | 9269 | struct io_rsrc_data *data = ctx->file_data; |
a04b0ac0 PB |
9270 | struct io_fixed_file *file_slot; |
9271 | struct file *file; | |
98f0b3b4 PB |
9272 | int fd, i, err = 0; |
9273 | unsigned int done; | |
05589553 | 9274 | bool needs_switch = false; |
c3a31e60 | 9275 | |
98f0b3b4 PB |
9276 | if (!ctx->file_data) |
9277 | return -ENXIO; | |
9278 | if (up->offset + nr_args > ctx->nr_user_files) | |
c3a31e60 JA |
9279 | return -EINVAL; |
9280 | ||
67973b93 | 9281 | for (done = 0; done < nr_args; done++) { |
c3bdad02 PB |
9282 | u64 tag = 0; |
9283 | ||
9284 | if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) || | |
9285 | copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
c3a31e60 JA |
9286 | err = -EFAULT; |
9287 | break; | |
9288 | } | |
c3bdad02 PB |
9289 | if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) { |
9290 | err = -EINVAL; | |
9291 | break; | |
9292 | } | |
4e0377a1 | 9293 | if (fd == IORING_REGISTER_FILES_SKIP) |
9294 | continue; | |
9295 | ||
67973b93 | 9296 | i = array_index_nospec(up->offset + done, ctx->nr_user_files); |
aeca241b | 9297 | file_slot = io_fixed_file_slot(&ctx->file_table, i); |
ea64ec02 | 9298 | |
a04b0ac0 PB |
9299 | if (file_slot->file_ptr) { |
9300 | file = (struct file *)(file_slot->file_ptr & FFS_MASK); | |
b60c8dce PB |
9301 | err = io_queue_rsrc_removal(data, up->offset + done, |
9302 | ctx->rsrc_node, file); | |
a5318d3c HD |
9303 | if (err) |
9304 | break; | |
a04b0ac0 | 9305 | file_slot->file_ptr = 0; |
05589553 | 9306 | needs_switch = true; |
c3a31e60 JA |
9307 | } |
9308 | if (fd != -1) { | |
c3a31e60 JA |
9309 | file = fget(fd); |
9310 | if (!file) { | |
9311 | err = -EBADF; | |
9312 | break; | |
9313 | } | |
9314 | /* | |
9315 | * Don't allow io_uring instances to be registered. If | |
9316 | * UNIX isn't enabled, then this causes a reference | |
9317 | * cycle and this instance can never get freed. If UNIX | |
9318 | * is enabled we'll handle it just fine, but there's | |
9319 | * still no point in allowing a ring fd as it doesn't | |
9320 | * support regular read/write anyway. | |
9321 | */ | |
9322 | if (file->f_op == &io_uring_fops) { | |
9323 | fput(file); | |
9324 | err = -EBADF; | |
9325 | break; | |
9326 | } | |
2d091d62 | 9327 | *io_get_tag_slot(data, up->offset + done) = tag; |
9a321c98 | 9328 | io_fixed_file_set(file_slot, file); |
c3a31e60 | 9329 | err = io_sqe_file_register(ctx, file, i); |
f3bd9dae | 9330 | if (err) { |
a04b0ac0 | 9331 | file_slot->file_ptr = 0; |
f3bd9dae | 9332 | fput(file); |
c3a31e60 | 9333 | break; |
f3bd9dae | 9334 | } |
c3a31e60 | 9335 | } |
05f3fb3c JA |
9336 | } |
9337 | ||
a7f0ed5a PB |
9338 | if (needs_switch) |
9339 | io_rsrc_node_switch(ctx, data); | |
c3a31e60 JA |
9340 | return done ? done : err; |
9341 | } | |
05589553 | 9342 | |
685fe7fe JA |
9343 | static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx, |
9344 | struct task_struct *task) | |
24369c2e | 9345 | { |
e941894e | 9346 | struct io_wq_hash *hash; |
24369c2e | 9347 | struct io_wq_data data; |
24369c2e | 9348 | unsigned int concurrency; |
24369c2e | 9349 | |
362a9e65 | 9350 | mutex_lock(&ctx->uring_lock); |
e941894e JA |
9351 | hash = ctx->hash_map; |
9352 | if (!hash) { | |
9353 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
362a9e65 YY |
9354 | if (!hash) { |
9355 | mutex_unlock(&ctx->uring_lock); | |
e941894e | 9356 | return ERR_PTR(-ENOMEM); |
362a9e65 | 9357 | } |
e941894e JA |
9358 | refcount_set(&hash->refs, 1); |
9359 | init_waitqueue_head(&hash->wait); | |
9360 | ctx->hash_map = hash; | |
24369c2e | 9361 | } |
362a9e65 | 9362 | mutex_unlock(&ctx->uring_lock); |
24369c2e | 9363 | |
e941894e | 9364 | data.hash = hash; |
685fe7fe | 9365 | data.task = task; |
ebc11b6c | 9366 | data.free_work = io_wq_free_work; |
f5fa38c5 | 9367 | data.do_work = io_wq_submit_work; |
24369c2e | 9368 | |
d25e3a3d JA |
9369 | /* Do QD, or 4 * CPUS, whatever is smallest */ |
9370 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
24369c2e | 9371 | |
5aa75ed5 | 9372 | return io_wq_create(concurrency, &data); |
24369c2e PB |
9373 | } |
9374 | ||
c072481d PB |
9375 | static __cold int io_uring_alloc_task_context(struct task_struct *task, |
9376 | struct io_ring_ctx *ctx) | |
0f212204 JA |
9377 | { |
9378 | struct io_uring_task *tctx; | |
d8a6df10 | 9379 | int ret; |
0f212204 | 9380 | |
09899b19 | 9381 | tctx = kzalloc(sizeof(*tctx), GFP_KERNEL); |
0f212204 JA |
9382 | if (unlikely(!tctx)) |
9383 | return -ENOMEM; | |
9384 | ||
e7a6c00d JA |
9385 | tctx->registered_rings = kcalloc(IO_RINGFD_REG_MAX, |
9386 | sizeof(struct file *), GFP_KERNEL); | |
9387 | if (unlikely(!tctx->registered_rings)) { | |
9388 | kfree(tctx); | |
9389 | return -ENOMEM; | |
9390 | } | |
9391 | ||
d8a6df10 JA |
9392 | ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL); |
9393 | if (unlikely(ret)) { | |
e7a6c00d | 9394 | kfree(tctx->registered_rings); |
d8a6df10 JA |
9395 | kfree(tctx); |
9396 | return ret; | |
9397 | } | |
9398 | ||
685fe7fe | 9399 | tctx->io_wq = io_init_wq_offload(ctx, task); |
5aa75ed5 JA |
9400 | if (IS_ERR(tctx->io_wq)) { |
9401 | ret = PTR_ERR(tctx->io_wq); | |
9402 | percpu_counter_destroy(&tctx->inflight); | |
e7a6c00d | 9403 | kfree(tctx->registered_rings); |
5aa75ed5 JA |
9404 | kfree(tctx); |
9405 | return ret; | |
9406 | } | |
9407 | ||
0f212204 JA |
9408 | xa_init(&tctx->xa); |
9409 | init_waitqueue_head(&tctx->wait); | |
fdaf083c | 9410 | atomic_set(&tctx->in_idle, 0); |
b303fe2e | 9411 | atomic_set(&tctx->inflight_tracked, 0); |
0f212204 | 9412 | task->io_uring = tctx; |
7cbf1722 JA |
9413 | spin_lock_init(&tctx->task_lock); |
9414 | INIT_WQ_LIST(&tctx->task_list); | |
4813c377 | 9415 | INIT_WQ_LIST(&tctx->prior_task_list); |
7cbf1722 | 9416 | init_task_work(&tctx->task_work, tctx_task_work); |
0f212204 JA |
9417 | return 0; |
9418 | } | |
9419 | ||
9420 | void __io_uring_free(struct task_struct *tsk) | |
9421 | { | |
9422 | struct io_uring_task *tctx = tsk->io_uring; | |
9423 | ||
9424 | WARN_ON_ONCE(!xa_empty(&tctx->xa)); | |
ef8eaa4e | 9425 | WARN_ON_ONCE(tctx->io_wq); |
09899b19 | 9426 | WARN_ON_ONCE(tctx->cached_refs); |
ef8eaa4e | 9427 | |
e7a6c00d | 9428 | kfree(tctx->registered_rings); |
d8a6df10 | 9429 | percpu_counter_destroy(&tctx->inflight); |
0f212204 JA |
9430 | kfree(tctx); |
9431 | tsk->io_uring = NULL; | |
9432 | } | |
9433 | ||
c072481d PB |
9434 | static __cold int io_sq_offload_create(struct io_ring_ctx *ctx, |
9435 | struct io_uring_params *p) | |
2b188cc1 JA |
9436 | { |
9437 | int ret; | |
9438 | ||
d25e3a3d JA |
9439 | /* Retain compatibility with failing for an invalid attach attempt */ |
9440 | if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) == | |
9441 | IORING_SETUP_ATTACH_WQ) { | |
9442 | struct fd f; | |
9443 | ||
9444 | f = fdget(p->wq_fd); | |
9445 | if (!f.file) | |
9446 | return -ENXIO; | |
0cc936f7 JA |
9447 | if (f.file->f_op != &io_uring_fops) { |
9448 | fdput(f); | |
f2a48dd0 | 9449 | return -EINVAL; |
0cc936f7 JA |
9450 | } |
9451 | fdput(f); | |
d25e3a3d | 9452 | } |
6c271ce2 | 9453 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
46fe18b1 | 9454 | struct task_struct *tsk; |
534ca6d6 | 9455 | struct io_sq_data *sqd; |
26984fbf | 9456 | bool attached; |
534ca6d6 | 9457 | |
cdc1404a PM |
9458 | ret = security_uring_sqpoll(); |
9459 | if (ret) | |
9460 | return ret; | |
9461 | ||
26984fbf | 9462 | sqd = io_get_sq_data(p, &attached); |
534ca6d6 JA |
9463 | if (IS_ERR(sqd)) { |
9464 | ret = PTR_ERR(sqd); | |
9465 | goto err; | |
9466 | } | |
69fb2131 | 9467 | |
7c30f36a | 9468 | ctx->sq_creds = get_current_cred(); |
534ca6d6 | 9469 | ctx->sq_data = sqd; |
917257da JA |
9470 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
9471 | if (!ctx->sq_thread_idle) | |
9472 | ctx->sq_thread_idle = HZ; | |
9473 | ||
78d7f6ba | 9474 | io_sq_thread_park(sqd); |
de75a3d3 PB |
9475 | list_add(&ctx->sqd_list, &sqd->ctx_list); |
9476 | io_sqd_update_thread_idle(sqd); | |
26984fbf | 9477 | /* don't attach to a dying SQPOLL thread, would be racy */ |
f2a48dd0 | 9478 | ret = (attached && !sqd->thread) ? -ENXIO : 0; |
78d7f6ba PB |
9479 | io_sq_thread_unpark(sqd); |
9480 | ||
de75a3d3 PB |
9481 | if (ret < 0) |
9482 | goto err; | |
9483 | if (attached) | |
5aa75ed5 | 9484 | return 0; |
aa06165d | 9485 | |
6c271ce2 | 9486 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 9487 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 9488 | |
917257da | 9489 | ret = -EINVAL; |
f2a48dd0 | 9490 | if (cpu >= nr_cpu_ids || !cpu_online(cpu)) |
e8f98f24 | 9491 | goto err_sqpoll; |
37d1e2e3 | 9492 | sqd->sq_cpu = cpu; |
6c271ce2 | 9493 | } else { |
37d1e2e3 | 9494 | sqd->sq_cpu = -1; |
6c271ce2 | 9495 | } |
37d1e2e3 JA |
9496 | |
9497 | sqd->task_pid = current->pid; | |
5c2469e0 | 9498 | sqd->task_tgid = current->tgid; |
46fe18b1 JA |
9499 | tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE); |
9500 | if (IS_ERR(tsk)) { | |
9501 | ret = PTR_ERR(tsk); | |
e8f98f24 | 9502 | goto err_sqpoll; |
6c271ce2 | 9503 | } |
97a73a0f | 9504 | |
46fe18b1 | 9505 | sqd->thread = tsk; |
97a73a0f | 9506 | ret = io_uring_alloc_task_context(tsk, ctx); |
46fe18b1 | 9507 | wake_up_new_task(tsk); |
0f212204 JA |
9508 | if (ret) |
9509 | goto err; | |
6c271ce2 JA |
9510 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
9511 | /* Can't have SQ_AFF without SQPOLL */ | |
9512 | ret = -EINVAL; | |
9513 | goto err; | |
9514 | } | |
9515 | ||
2b188cc1 | 9516 | return 0; |
f2a48dd0 PB |
9517 | err_sqpoll: |
9518 | complete(&ctx->sq_data->exited); | |
2b188cc1 | 9519 | err: |
37d1e2e3 | 9520 | io_sq_thread_finish(ctx); |
2b188cc1 JA |
9521 | return ret; |
9522 | } | |
9523 | ||
a087e2b5 BM |
9524 | static inline void __io_unaccount_mem(struct user_struct *user, |
9525 | unsigned long nr_pages) | |
2b188cc1 JA |
9526 | { |
9527 | atomic_long_sub(nr_pages, &user->locked_vm); | |
9528 | } | |
9529 | ||
a087e2b5 BM |
9530 | static inline int __io_account_mem(struct user_struct *user, |
9531 | unsigned long nr_pages) | |
2b188cc1 JA |
9532 | { |
9533 | unsigned long page_limit, cur_pages, new_pages; | |
9534 | ||
9535 | /* Don't allow more pages than we can safely lock */ | |
9536 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
9537 | ||
9538 | do { | |
9539 | cur_pages = atomic_long_read(&user->locked_vm); | |
9540 | new_pages = cur_pages + nr_pages; | |
9541 | if (new_pages > page_limit) | |
9542 | return -ENOMEM; | |
9543 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
9544 | new_pages) != cur_pages); | |
9545 | ||
9546 | return 0; | |
9547 | } | |
9548 | ||
26bfa89e | 9549 | static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9550 | { |
62e398be | 9551 | if (ctx->user) |
a087e2b5 | 9552 | __io_unaccount_mem(ctx->user, nr_pages); |
30975825 | 9553 | |
26bfa89e JA |
9554 | if (ctx->mm_account) |
9555 | atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9556 | } |
9557 | ||
26bfa89e | 9558 | static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages) |
a087e2b5 | 9559 | { |
30975825 BM |
9560 | int ret; |
9561 | ||
62e398be | 9562 | if (ctx->user) { |
30975825 BM |
9563 | ret = __io_account_mem(ctx->user, nr_pages); |
9564 | if (ret) | |
9565 | return ret; | |
9566 | } | |
9567 | ||
26bfa89e JA |
9568 | if (ctx->mm_account) |
9569 | atomic64_add(nr_pages, &ctx->mm_account->pinned_vm); | |
a087e2b5 BM |
9570 | |
9571 | return 0; | |
9572 | } | |
9573 | ||
2b188cc1 JA |
9574 | static void io_mem_free(void *ptr) |
9575 | { | |
52e04ef4 MR |
9576 | struct page *page; |
9577 | ||
9578 | if (!ptr) | |
9579 | return; | |
2b188cc1 | 9580 | |
52e04ef4 | 9581 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
9582 | if (put_page_testzero(page)) |
9583 | free_compound_page(page); | |
9584 | } | |
9585 | ||
9586 | static void *io_mem_alloc(size_t size) | |
9587 | { | |
0a3f1e0b | 9588 | gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; |
2b188cc1 | 9589 | |
0a3f1e0b | 9590 | return (void *) __get_free_pages(gfp, get_order(size)); |
2b188cc1 JA |
9591 | } |
9592 | ||
75b28aff HV |
9593 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
9594 | size_t *sq_offset) | |
9595 | { | |
9596 | struct io_rings *rings; | |
9597 | size_t off, sq_array_size; | |
9598 | ||
9599 | off = struct_size(rings, cqes, cq_entries); | |
9600 | if (off == SIZE_MAX) | |
9601 | return SIZE_MAX; | |
9602 | ||
9603 | #ifdef CONFIG_SMP | |
9604 | off = ALIGN(off, SMP_CACHE_BYTES); | |
9605 | if (off == 0) | |
9606 | return SIZE_MAX; | |
9607 | #endif | |
9608 | ||
b36200f5 DV |
9609 | if (sq_offset) |
9610 | *sq_offset = off; | |
9611 | ||
75b28aff HV |
9612 | sq_array_size = array_size(sizeof(u32), sq_entries); |
9613 | if (sq_array_size == SIZE_MAX) | |
9614 | return SIZE_MAX; | |
9615 | ||
9616 | if (check_add_overflow(off, sq_array_size, &off)) | |
9617 | return SIZE_MAX; | |
9618 | ||
75b28aff HV |
9619 | return off; |
9620 | } | |
9621 | ||
41edf1a5 | 9622 | static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot) |
7f61a1e9 | 9623 | { |
41edf1a5 | 9624 | struct io_mapped_ubuf *imu = *slot; |
7f61a1e9 PB |
9625 | unsigned int i; |
9626 | ||
6224843d PB |
9627 | if (imu != ctx->dummy_ubuf) { |
9628 | for (i = 0; i < imu->nr_bvecs; i++) | |
9629 | unpin_user_page(imu->bvec[i].bv_page); | |
9630 | if (imu->acct_pages) | |
9631 | io_unaccount_mem(ctx, imu->acct_pages); | |
9632 | kvfree(imu); | |
9633 | } | |
41edf1a5 | 9634 | *slot = NULL; |
7f61a1e9 PB |
9635 | } |
9636 | ||
bd54b6fe | 9637 | static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) |
edafccee | 9638 | { |
634d00df PB |
9639 | io_buffer_unmap(ctx, &prsrc->buf); |
9640 | prsrc->buf = NULL; | |
bd54b6fe | 9641 | } |
edafccee | 9642 | |
bd54b6fe BM |
9643 | static void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
9644 | { | |
9645 | unsigned int i; | |
edafccee | 9646 | |
7f61a1e9 PB |
9647 | for (i = 0; i < ctx->nr_user_bufs; i++) |
9648 | io_buffer_unmap(ctx, &ctx->user_bufs[i]); | |
edafccee | 9649 | kfree(ctx->user_bufs); |
bb6659cc | 9650 | io_rsrc_data_free(ctx->buf_data); |
edafccee | 9651 | ctx->user_bufs = NULL; |
bd54b6fe | 9652 | ctx->buf_data = NULL; |
edafccee | 9653 | ctx->nr_user_bufs = 0; |
bd54b6fe BM |
9654 | } |
9655 | ||
0a96bbe4 | 9656 | static int io_sqe_buffers_unregister(struct io_ring_ctx *ctx) |
edafccee | 9657 | { |
bd54b6fe | 9658 | int ret; |
edafccee | 9659 | |
bd54b6fe | 9660 | if (!ctx->buf_data) |
edafccee JA |
9661 | return -ENXIO; |
9662 | ||
bd54b6fe BM |
9663 | ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx); |
9664 | if (!ret) | |
9665 | __io_sqe_buffers_unregister(ctx); | |
9666 | return ret; | |
edafccee JA |
9667 | } |
9668 | ||
9669 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
9670 | void __user *arg, unsigned index) | |
9671 | { | |
9672 | struct iovec __user *src; | |
9673 | ||
9674 | #ifdef CONFIG_COMPAT | |
9675 | if (ctx->compat) { | |
9676 | struct compat_iovec __user *ciovs; | |
9677 | struct compat_iovec ciov; | |
9678 | ||
9679 | ciovs = (struct compat_iovec __user *) arg; | |
9680 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
9681 | return -EFAULT; | |
9682 | ||
d55e5f5b | 9683 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
9684 | dst->iov_len = ciov.iov_len; |
9685 | return 0; | |
9686 | } | |
9687 | #endif | |
9688 | src = (struct iovec __user *) arg; | |
9689 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
9690 | return -EFAULT; | |
9691 | return 0; | |
9692 | } | |
9693 | ||
de293938 JA |
9694 | /* |
9695 | * Not super efficient, but this is just a registration time. And we do cache | |
9696 | * the last compound head, so generally we'll only do a full search if we don't | |
9697 | * match that one. | |
9698 | * | |
9699 | * We check if the given compound head page has already been accounted, to | |
9700 | * avoid double accounting it. This allows us to account the full size of the | |
9701 | * page, not just the constituent pages of a huge page. | |
9702 | */ | |
9703 | static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages, | |
9704 | int nr_pages, struct page *hpage) | |
9705 | { | |
9706 | int i, j; | |
9707 | ||
9708 | /* check current page array */ | |
9709 | for (i = 0; i < nr_pages; i++) { | |
9710 | if (!PageCompound(pages[i])) | |
9711 | continue; | |
9712 | if (compound_head(pages[i]) == hpage) | |
9713 | return true; | |
9714 | } | |
9715 | ||
9716 | /* check previously registered pages */ | |
9717 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
41edf1a5 | 9718 | struct io_mapped_ubuf *imu = ctx->user_bufs[i]; |
de293938 JA |
9719 | |
9720 | for (j = 0; j < imu->nr_bvecs; j++) { | |
9721 | if (!PageCompound(imu->bvec[j].bv_page)) | |
9722 | continue; | |
9723 | if (compound_head(imu->bvec[j].bv_page) == hpage) | |
9724 | return true; | |
9725 | } | |
9726 | } | |
9727 | ||
9728 | return false; | |
9729 | } | |
9730 | ||
9731 | static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages, | |
9732 | int nr_pages, struct io_mapped_ubuf *imu, | |
9733 | struct page **last_hpage) | |
9734 | { | |
9735 | int i, ret; | |
9736 | ||
216e5835 | 9737 | imu->acct_pages = 0; |
de293938 JA |
9738 | for (i = 0; i < nr_pages; i++) { |
9739 | if (!PageCompound(pages[i])) { | |
9740 | imu->acct_pages++; | |
9741 | } else { | |
9742 | struct page *hpage; | |
9743 | ||
9744 | hpage = compound_head(pages[i]); | |
9745 | if (hpage == *last_hpage) | |
9746 | continue; | |
9747 | *last_hpage = hpage; | |
9748 | if (headpage_already_acct(ctx, pages, i, hpage)) | |
9749 | continue; | |
9750 | imu->acct_pages += page_size(hpage) >> PAGE_SHIFT; | |
9751 | } | |
9752 | } | |
9753 | ||
9754 | if (!imu->acct_pages) | |
9755 | return 0; | |
9756 | ||
26bfa89e | 9757 | ret = io_account_mem(ctx, imu->acct_pages); |
de293938 JA |
9758 | if (ret) |
9759 | imu->acct_pages = 0; | |
9760 | return ret; | |
9761 | } | |
9762 | ||
0a96bbe4 | 9763 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov, |
41edf1a5 | 9764 | struct io_mapped_ubuf **pimu, |
0a96bbe4 | 9765 | struct page **last_hpage) |
edafccee | 9766 | { |
41edf1a5 | 9767 | struct io_mapped_ubuf *imu = NULL; |
edafccee JA |
9768 | struct vm_area_struct **vmas = NULL; |
9769 | struct page **pages = NULL; | |
0a96bbe4 BM |
9770 | unsigned long off, start, end, ubuf; |
9771 | size_t size; | |
9772 | int ret, pret, nr_pages, i; | |
9773 | ||
6224843d PB |
9774 | if (!iov->iov_base) { |
9775 | *pimu = ctx->dummy_ubuf; | |
9776 | return 0; | |
9777 | } | |
9778 | ||
0a96bbe4 BM |
9779 | ubuf = (unsigned long) iov->iov_base; |
9780 | end = (ubuf + iov->iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
9781 | start = ubuf >> PAGE_SHIFT; | |
9782 | nr_pages = end - start; | |
9783 | ||
41edf1a5 | 9784 | *pimu = NULL; |
0a96bbe4 BM |
9785 | ret = -ENOMEM; |
9786 | ||
9787 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL); | |
9788 | if (!pages) | |
9789 | goto done; | |
9790 | ||
9791 | vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *), | |
9792 | GFP_KERNEL); | |
9793 | if (!vmas) | |
9794 | goto done; | |
edafccee | 9795 | |
41edf1a5 | 9796 | imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL); |
a2b4198c | 9797 | if (!imu) |
0a96bbe4 BM |
9798 | goto done; |
9799 | ||
9800 | ret = 0; | |
9801 | mmap_read_lock(current->mm); | |
9802 | pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, | |
9803 | pages, vmas); | |
9804 | if (pret == nr_pages) { | |
9805 | /* don't support file backed memory */ | |
9806 | for (i = 0; i < nr_pages; i++) { | |
9807 | struct vm_area_struct *vma = vmas[i]; | |
9808 | ||
40dad765 PB |
9809 | if (vma_is_shmem(vma)) |
9810 | continue; | |
0a96bbe4 BM |
9811 | if (vma->vm_file && |
9812 | !is_file_hugepages(vma->vm_file)) { | |
9813 | ret = -EOPNOTSUPP; | |
9814 | break; | |
9815 | } | |
9816 | } | |
9817 | } else { | |
9818 | ret = pret < 0 ? pret : -EFAULT; | |
9819 | } | |
9820 | mmap_read_unlock(current->mm); | |
9821 | if (ret) { | |
9822 | /* | |
9823 | * if we did partial map, or found file backed vmas, | |
9824 | * release any pages we did get | |
9825 | */ | |
9826 | if (pret > 0) | |
9827 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9828 | goto done; |
9829 | } | |
9830 | ||
9831 | ret = io_buffer_account_pin(ctx, pages, pret, imu, last_hpage); | |
9832 | if (ret) { | |
9833 | unpin_user_pages(pages, pret); | |
0a96bbe4 BM |
9834 | goto done; |
9835 | } | |
9836 | ||
9837 | off = ubuf & ~PAGE_MASK; | |
9838 | size = iov->iov_len; | |
9839 | for (i = 0; i < nr_pages; i++) { | |
9840 | size_t vec_len; | |
9841 | ||
9842 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
9843 | imu->bvec[i].bv_page = pages[i]; | |
9844 | imu->bvec[i].bv_len = vec_len; | |
9845 | imu->bvec[i].bv_offset = off; | |
9846 | off = 0; | |
9847 | size -= vec_len; | |
9848 | } | |
9849 | /* store original address for later verification */ | |
9850 | imu->ubuf = ubuf; | |
4751f53d | 9851 | imu->ubuf_end = ubuf + iov->iov_len; |
0a96bbe4 | 9852 | imu->nr_bvecs = nr_pages; |
41edf1a5 | 9853 | *pimu = imu; |
0a96bbe4 BM |
9854 | ret = 0; |
9855 | done: | |
41edf1a5 PB |
9856 | if (ret) |
9857 | kvfree(imu); | |
0a96bbe4 BM |
9858 | kvfree(pages); |
9859 | kvfree(vmas); | |
9860 | return ret; | |
9861 | } | |
9862 | ||
2b358604 | 9863 | static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args) |
0a96bbe4 | 9864 | { |
87094465 PB |
9865 | ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL); |
9866 | return ctx->user_bufs ? 0 : -ENOMEM; | |
2b358604 | 9867 | } |
edafccee | 9868 | |
2b358604 BM |
9869 | static int io_buffer_validate(struct iovec *iov) |
9870 | { | |
50e96989 PB |
9871 | unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1); |
9872 | ||
2b358604 BM |
9873 | /* |
9874 | * Don't impose further limits on the size and buffer | |
9875 | * constraints here, we'll -EINVAL later when IO is | |
9876 | * submitted if they are wrong. | |
9877 | */ | |
6224843d PB |
9878 | if (!iov->iov_base) |
9879 | return iov->iov_len ? -EFAULT : 0; | |
9880 | if (!iov->iov_len) | |
2b358604 | 9881 | return -EFAULT; |
edafccee | 9882 | |
2b358604 BM |
9883 | /* arbitrary limit, but we need something */ |
9884 | if (iov->iov_len > SZ_1G) | |
9885 | return -EFAULT; | |
edafccee | 9886 | |
50e96989 PB |
9887 | if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp)) |
9888 | return -EOVERFLOW; | |
9889 | ||
2b358604 BM |
9890 | return 0; |
9891 | } | |
edafccee | 9892 | |
2b358604 | 9893 | static int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, |
634d00df | 9894 | unsigned int nr_args, u64 __user *tags) |
2b358604 | 9895 | { |
bd54b6fe BM |
9896 | struct page *last_hpage = NULL; |
9897 | struct io_rsrc_data *data; | |
2b358604 BM |
9898 | int i, ret; |
9899 | struct iovec iov; | |
edafccee | 9900 | |
87094465 PB |
9901 | if (ctx->user_bufs) |
9902 | return -EBUSY; | |
489809e2 | 9903 | if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS) |
87094465 | 9904 | return -EINVAL; |
bd54b6fe | 9905 | ret = io_rsrc_node_switch_start(ctx); |
2b358604 BM |
9906 | if (ret) |
9907 | return ret; | |
d878c816 PB |
9908 | ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data); |
9909 | if (ret) | |
9910 | return ret; | |
bd54b6fe BM |
9911 | ret = io_buffers_map_alloc(ctx, nr_args); |
9912 | if (ret) { | |
bb6659cc | 9913 | io_rsrc_data_free(data); |
bd54b6fe BM |
9914 | return ret; |
9915 | } | |
edafccee | 9916 | |
87094465 | 9917 | for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) { |
edafccee JA |
9918 | ret = io_copy_iov(ctx, &iov, arg, i); |
9919 | if (ret) | |
0a96bbe4 | 9920 | break; |
2b358604 BM |
9921 | ret = io_buffer_validate(&iov); |
9922 | if (ret) | |
0a96bbe4 | 9923 | break; |
2d091d62 | 9924 | if (!iov.iov_base && *io_get_tag_slot(data, i)) { |
cf3770e7 CIK |
9925 | ret = -EINVAL; |
9926 | break; | |
9927 | } | |
edafccee | 9928 | |
41edf1a5 PB |
9929 | ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i], |
9930 | &last_hpage); | |
0a96bbe4 BM |
9931 | if (ret) |
9932 | break; | |
edafccee | 9933 | } |
0a96bbe4 | 9934 | |
bd54b6fe | 9935 | WARN_ON_ONCE(ctx->buf_data); |
0a96bbe4 | 9936 | |
bd54b6fe BM |
9937 | ctx->buf_data = data; |
9938 | if (ret) | |
9939 | __io_sqe_buffers_unregister(ctx); | |
9940 | else | |
9941 | io_rsrc_node_switch(ctx, NULL); | |
edafccee JA |
9942 | return ret; |
9943 | } | |
9944 | ||
634d00df PB |
9945 | static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, |
9946 | struct io_uring_rsrc_update2 *up, | |
9947 | unsigned int nr_args) | |
9948 | { | |
9949 | u64 __user *tags = u64_to_user_ptr(up->tags); | |
9950 | struct iovec iov, __user *iovs = u64_to_user_ptr(up->data); | |
634d00df PB |
9951 | struct page *last_hpage = NULL; |
9952 | bool needs_switch = false; | |
9953 | __u32 done; | |
9954 | int i, err; | |
9955 | ||
9956 | if (!ctx->buf_data) | |
9957 | return -ENXIO; | |
9958 | if (up->offset + nr_args > ctx->nr_user_bufs) | |
9959 | return -EINVAL; | |
9960 | ||
9961 | for (done = 0; done < nr_args; done++) { | |
0b8c0e7c PB |
9962 | struct io_mapped_ubuf *imu; |
9963 | int offset = up->offset + done; | |
634d00df PB |
9964 | u64 tag = 0; |
9965 | ||
9966 | err = io_copy_iov(ctx, &iov, iovs, done); | |
9967 | if (err) | |
9968 | break; | |
9969 | if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { | |
9970 | err = -EFAULT; | |
9971 | break; | |
9972 | } | |
0b8c0e7c PB |
9973 | err = io_buffer_validate(&iov); |
9974 | if (err) | |
9975 | break; | |
cf3770e7 CIK |
9976 | if (!iov.iov_base && tag) { |
9977 | err = -EINVAL; | |
9978 | break; | |
9979 | } | |
0b8c0e7c PB |
9980 | err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage); |
9981 | if (err) | |
9982 | break; | |
634d00df | 9983 | |
0b8c0e7c | 9984 | i = array_index_nospec(offset, ctx->nr_user_bufs); |
6224843d | 9985 | if (ctx->user_bufs[i] != ctx->dummy_ubuf) { |
0b8c0e7c PB |
9986 | err = io_queue_rsrc_removal(ctx->buf_data, offset, |
9987 | ctx->rsrc_node, ctx->user_bufs[i]); | |
9988 | if (unlikely(err)) { | |
9989 | io_buffer_unmap(ctx, &imu); | |
634d00df | 9990 | break; |
0b8c0e7c | 9991 | } |
634d00df PB |
9992 | ctx->user_bufs[i] = NULL; |
9993 | needs_switch = true; | |
9994 | } | |
9995 | ||
0b8c0e7c | 9996 | ctx->user_bufs[i] = imu; |
2d091d62 | 9997 | *io_get_tag_slot(ctx->buf_data, offset) = tag; |
634d00df PB |
9998 | } |
9999 | ||
10000 | if (needs_switch) | |
10001 | io_rsrc_node_switch(ctx, ctx->buf_data); | |
10002 | return done ? done : err; | |
10003 | } | |
10004 | ||
c75312dd UA |
10005 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, |
10006 | unsigned int eventfd_async) | |
9b402849 | 10007 | { |
77bc59b4 | 10008 | struct io_ev_fd *ev_fd; |
9b402849 | 10009 | __s32 __user *fds = arg; |
f0a4e62b | 10010 | int fd; |
9b402849 | 10011 | |
77bc59b4 UA |
10012 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, |
10013 | lockdep_is_held(&ctx->uring_lock)); | |
10014 | if (ev_fd) | |
9b402849 JA |
10015 | return -EBUSY; |
10016 | ||
10017 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
10018 | return -EFAULT; | |
10019 | ||
77bc59b4 UA |
10020 | ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); |
10021 | if (!ev_fd) | |
10022 | return -ENOMEM; | |
fe7e3257 | 10023 | |
77bc59b4 UA |
10024 | ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); |
10025 | if (IS_ERR(ev_fd->cq_ev_fd)) { | |
f0a4e62b | 10026 | int ret = PTR_ERR(ev_fd->cq_ev_fd); |
77bc59b4 | 10027 | kfree(ev_fd); |
9b402849 JA |
10028 | return ret; |
10029 | } | |
c75312dd | 10030 | ev_fd->eventfd_async = eventfd_async; |
9aa8dfde | 10031 | ctx->has_evfd = true; |
77bc59b4 | 10032 | rcu_assign_pointer(ctx->io_ev_fd, ev_fd); |
f0a4e62b | 10033 | return 0; |
77bc59b4 UA |
10034 | } |
10035 | ||
10036 | static void io_eventfd_put(struct rcu_head *rcu) | |
10037 | { | |
10038 | struct io_ev_fd *ev_fd = container_of(rcu, struct io_ev_fd, rcu); | |
10039 | ||
10040 | eventfd_ctx_put(ev_fd->cq_ev_fd); | |
10041 | kfree(ev_fd); | |
9b402849 JA |
10042 | } |
10043 | ||
10044 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
10045 | { | |
77bc59b4 UA |
10046 | struct io_ev_fd *ev_fd; |
10047 | ||
10048 | ev_fd = rcu_dereference_protected(ctx->io_ev_fd, | |
10049 | lockdep_is_held(&ctx->uring_lock)); | |
10050 | if (ev_fd) { | |
9aa8dfde | 10051 | ctx->has_evfd = false; |
77bc59b4 UA |
10052 | rcu_assign_pointer(ctx->io_ev_fd, NULL); |
10053 | call_rcu(&ev_fd->rcu, io_eventfd_put); | |
9b402849 JA |
10054 | return 0; |
10055 | } | |
10056 | ||
10057 | return -ENXIO; | |
10058 | } | |
10059 | ||
5a2e745d JA |
10060 | static void io_destroy_buffers(struct io_ring_ctx *ctx) |
10061 | { | |
dbc7d452 JA |
10062 | int i; |
10063 | ||
10064 | for (i = 0; i < (1U << IO_BUFFERS_HASH_BITS); i++) { | |
10065 | struct list_head *list = &ctx->io_buffers[i]; | |
9e15c3a0 | 10066 | |
dbc7d452 JA |
10067 | while (!list_empty(list)) { |
10068 | struct io_buffer_list *bl; | |
10069 | ||
10070 | bl = list_first_entry(list, struct io_buffer_list, list); | |
10071 | __io_remove_buffers(ctx, bl, -1U); | |
10072 | list_del(&bl->list); | |
10073 | kfree(bl); | |
10074 | } | |
10075 | } | |
cc3cec83 JA |
10076 | |
10077 | while (!list_empty(&ctx->io_buffers_pages)) { | |
10078 | struct page *page; | |
10079 | ||
10080 | page = list_first_entry(&ctx->io_buffers_pages, struct page, lru); | |
10081 | list_del_init(&page->lru); | |
10082 | __free_page(page); | |
10083 | } | |
5a2e745d JA |
10084 | } |
10085 | ||
4010fec4 | 10086 | static void io_req_caches_free(struct io_ring_ctx *ctx) |
2b188cc1 | 10087 | { |
cd0ca2e0 | 10088 | struct io_submit_state *state = &ctx->submit_state; |
37f0e767 | 10089 | int nr = 0; |
bf019da7 | 10090 | |
9a4fdbd8 | 10091 | mutex_lock(&ctx->uring_lock); |
cd0ca2e0 | 10092 | io_flush_cached_locked_reqs(ctx, state); |
9a4fdbd8 | 10093 | |
c2b6c6bc PB |
10094 | while (state->free_list.next) { |
10095 | struct io_wq_work_node *node; | |
10096 | struct io_kiocb *req; | |
9a4fdbd8 | 10097 | |
c2b6c6bc PB |
10098 | node = wq_stack_extract(&state->free_list); |
10099 | req = container_of(node, struct io_kiocb, comp_list); | |
10100 | kmem_cache_free(req_cachep, req); | |
37f0e767 | 10101 | nr++; |
c2b6c6bc | 10102 | } |
37f0e767 PB |
10103 | if (nr) |
10104 | percpu_ref_put_many(&ctx->refs, nr); | |
9a4fdbd8 JA |
10105 | mutex_unlock(&ctx->uring_lock); |
10106 | } | |
10107 | ||
43597aac | 10108 | static void io_wait_rsrc_data(struct io_rsrc_data *data) |
2b188cc1 | 10109 | { |
43597aac | 10110 | if (data && !atomic_dec_and_test(&data->refs)) |
bd54b6fe | 10111 | wait_for_completion(&data->done); |
bd54b6fe | 10112 | } |
04fc6c80 | 10113 | |
4d9237e3 JA |
10114 | static void io_flush_apoll_cache(struct io_ring_ctx *ctx) |
10115 | { | |
10116 | struct async_poll *apoll; | |
10117 | ||
10118 | while (!list_empty(&ctx->apoll_cache)) { | |
10119 | apoll = list_first_entry(&ctx->apoll_cache, struct async_poll, | |
10120 | poll.wait.entry); | |
10121 | list_del(&apoll->poll.wait.entry); | |
10122 | kfree(apoll); | |
10123 | } | |
10124 | } | |
10125 | ||
c072481d | 10126 | static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) |
2b188cc1 | 10127 | { |
37d1e2e3 | 10128 | io_sq_thread_finish(ctx); |
2aede0e4 | 10129 | |
37d1e2e3 | 10130 | if (ctx->mm_account) { |
2aede0e4 JA |
10131 | mmdrop(ctx->mm_account); |
10132 | ctx->mm_account = NULL; | |
30975825 | 10133 | } |
def596e9 | 10134 | |
ab409402 | 10135 | io_rsrc_refs_drop(ctx); |
43597aac PB |
10136 | /* __io_rsrc_put_work() may need uring_lock to progress, wait w/o it */ |
10137 | io_wait_rsrc_data(ctx->buf_data); | |
10138 | io_wait_rsrc_data(ctx->file_data); | |
10139 | ||
8bad28d8 | 10140 | mutex_lock(&ctx->uring_lock); |
43597aac | 10141 | if (ctx->buf_data) |
bd54b6fe | 10142 | __io_sqe_buffers_unregister(ctx); |
43597aac | 10143 | if (ctx->file_data) |
08480400 | 10144 | __io_sqe_files_unregister(ctx); |
c4ea060e PB |
10145 | if (ctx->rings) |
10146 | __io_cqring_overflow_flush(ctx, true); | |
9b402849 | 10147 | io_eventfd_unregister(ctx); |
4d9237e3 | 10148 | io_flush_apoll_cache(ctx); |
77bc59b4 | 10149 | mutex_unlock(&ctx->uring_lock); |
5a2e745d | 10150 | io_destroy_buffers(ctx); |
07db298a PB |
10151 | if (ctx->sq_creds) |
10152 | put_cred(ctx->sq_creds); | |
def596e9 | 10153 | |
a7f0ed5a PB |
10154 | /* there are no registered resources left, nobody uses it */ |
10155 | if (ctx->rsrc_node) | |
10156 | io_rsrc_node_destroy(ctx->rsrc_node); | |
8dd03afe | 10157 | if (ctx->rsrc_backup_node) |
b895c9a6 | 10158 | io_rsrc_node_destroy(ctx->rsrc_backup_node); |
a7f0ed5a | 10159 | flush_delayed_work(&ctx->rsrc_put_work); |
756ab7c0 | 10160 | flush_delayed_work(&ctx->fallback_work); |
a7f0ed5a PB |
10161 | |
10162 | WARN_ON_ONCE(!list_empty(&ctx->rsrc_ref_list)); | |
10163 | WARN_ON_ONCE(!llist_empty(&ctx->rsrc_put_llist)); | |
def596e9 | 10164 | |
2b188cc1 | 10165 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
10166 | if (ctx->ring_sock) { |
10167 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 10168 | sock_release(ctx->ring_sock); |
355e8d26 | 10169 | } |
2b188cc1 | 10170 | #endif |
ef9dd637 | 10171 | WARN_ON_ONCE(!list_empty(&ctx->ltimeout_list)); |
2b188cc1 | 10172 | |
75b28aff | 10173 | io_mem_free(ctx->rings); |
2b188cc1 | 10174 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
10175 | |
10176 | percpu_ref_exit(&ctx->refs); | |
2b188cc1 | 10177 | free_uid(ctx->user); |
4010fec4 | 10178 | io_req_caches_free(ctx); |
e941894e JA |
10179 | if (ctx->hash_map) |
10180 | io_wq_put_hash(ctx->hash_map); | |
adc8682e | 10181 | io_free_napi_list(ctx); |
78076bb6 | 10182 | kfree(ctx->cancel_hash); |
6224843d | 10183 | kfree(ctx->dummy_ubuf); |
dbc7d452 | 10184 | kfree(ctx->io_buffers); |
2b188cc1 JA |
10185 | kfree(ctx); |
10186 | } | |
10187 | ||
10188 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
10189 | { | |
10190 | struct io_ring_ctx *ctx = file->private_data; | |
10191 | __poll_t mask = 0; | |
10192 | ||
d60aa65b | 10193 | poll_wait(file, &ctx->cq_wait, wait); |
4f7067c3 SB |
10194 | /* |
10195 | * synchronizes with barrier from wq_has_sleeper call in | |
10196 | * io_commit_cqring | |
10197 | */ | |
2b188cc1 | 10198 | smp_rmb(); |
90554200 | 10199 | if (!io_sqring_full(ctx)) |
2b188cc1 | 10200 | mask |= EPOLLOUT | EPOLLWRNORM; |
ed670c3f HX |
10201 | |
10202 | /* | |
10203 | * Don't flush cqring overflow list here, just do a simple check. | |
10204 | * Otherwise there could possible be ABBA deadlock: | |
10205 | * CPU0 CPU1 | |
10206 | * ---- ---- | |
10207 | * lock(&ctx->uring_lock); | |
10208 | * lock(&ep->mtx); | |
10209 | * lock(&ctx->uring_lock); | |
10210 | * lock(&ep->mtx); | |
10211 | * | |
10212 | * Users may get EPOLLIN meanwhile seeing nothing in cqring, this | |
10213 | * pushs them to do the flush. | |
10214 | */ | |
5ed7a37d | 10215 | if (io_cqring_events(ctx) || test_bit(0, &ctx->check_cq_overflow)) |
2b188cc1 JA |
10216 | mask |= EPOLLIN | EPOLLRDNORM; |
10217 | ||
10218 | return mask; | |
10219 | } | |
10220 | ||
0bead8cd | 10221 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) |
071698e1 | 10222 | { |
4379bf8b | 10223 | const struct cred *creds; |
071698e1 | 10224 | |
61cf9370 | 10225 | creds = xa_erase(&ctx->personalities, id); |
4379bf8b JA |
10226 | if (creds) { |
10227 | put_cred(creds); | |
0bead8cd | 10228 | return 0; |
1e6fa521 | 10229 | } |
0bead8cd YD |
10230 | |
10231 | return -EINVAL; | |
10232 | } | |
10233 | ||
d56d938b PB |
10234 | struct io_tctx_exit { |
10235 | struct callback_head task_work; | |
10236 | struct completion completion; | |
baf186c4 | 10237 | struct io_ring_ctx *ctx; |
d56d938b PB |
10238 | }; |
10239 | ||
c072481d | 10240 | static __cold void io_tctx_exit_cb(struct callback_head *cb) |
d56d938b PB |
10241 | { |
10242 | struct io_uring_task *tctx = current->io_uring; | |
10243 | struct io_tctx_exit *work; | |
10244 | ||
10245 | work = container_of(cb, struct io_tctx_exit, task_work); | |
10246 | /* | |
10247 | * When @in_idle, we're in cancellation and it's racy to remove the | |
10248 | * node. It'll be removed by the end of cancellation, just ignore it. | |
10249 | */ | |
10250 | if (!atomic_read(&tctx->in_idle)) | |
eef51daa | 10251 | io_uring_del_tctx_node((unsigned long)work->ctx); |
d56d938b PB |
10252 | complete(&work->completion); |
10253 | } | |
10254 | ||
c072481d | 10255 | static __cold bool io_cancel_ctx_cb(struct io_wq_work *work, void *data) |
28090c13 PB |
10256 | { |
10257 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
10258 | ||
10259 | return req->ctx == data; | |
10260 | } | |
10261 | ||
c072481d | 10262 | static __cold void io_ring_exit_work(struct work_struct *work) |
85faa7b8 | 10263 | { |
d56d938b | 10264 | struct io_ring_ctx *ctx = container_of(work, struct io_ring_ctx, exit_work); |
b5bb3a24 | 10265 | unsigned long timeout = jiffies + HZ * 60 * 5; |
58d3be2c | 10266 | unsigned long interval = HZ / 20; |
d56d938b PB |
10267 | struct io_tctx_exit exit; |
10268 | struct io_tctx_node *node; | |
10269 | int ret; | |
85faa7b8 | 10270 | |
56952e91 JA |
10271 | /* |
10272 | * If we're doing polled IO and end up having requests being | |
10273 | * submitted async (out-of-line), then completions can come in while | |
10274 | * we're waiting for refs to drop. We need to reap these manually, | |
10275 | * as nobody else will be looking for them. | |
10276 | */ | |
b2edc0a7 | 10277 | do { |
3dd0c97a | 10278 | io_uring_try_cancel_requests(ctx, NULL, true); |
28090c13 PB |
10279 | if (ctx->sq_data) { |
10280 | struct io_sq_data *sqd = ctx->sq_data; | |
10281 | struct task_struct *tsk; | |
10282 | ||
10283 | io_sq_thread_park(sqd); | |
10284 | tsk = sqd->thread; | |
10285 | if (tsk && tsk->io_uring && tsk->io_uring->io_wq) | |
10286 | io_wq_cancel_cb(tsk->io_uring->io_wq, | |
10287 | io_cancel_ctx_cb, ctx, true); | |
10288 | io_sq_thread_unpark(sqd); | |
10289 | } | |
b5bb3a24 | 10290 | |
37f0e767 PB |
10291 | io_req_caches_free(ctx); |
10292 | ||
58d3be2c PB |
10293 | if (WARN_ON_ONCE(time_after(jiffies, timeout))) { |
10294 | /* there is little hope left, don't run it too often */ | |
10295 | interval = HZ * 60; | |
10296 | } | |
10297 | } while (!wait_for_completion_timeout(&ctx->ref_comp, interval)); | |
d56d938b | 10298 | |
7f00651a PB |
10299 | init_completion(&exit.completion); |
10300 | init_task_work(&exit.task_work, io_tctx_exit_cb); | |
10301 | exit.ctx = ctx; | |
89b5066e PB |
10302 | /* |
10303 | * Some may use context even when all refs and requests have been put, | |
10304 | * and they are free to do so while still holding uring_lock or | |
5b0a6acc | 10305 | * completion_lock, see io_req_task_submit(). Apart from other work, |
89b5066e PB |
10306 | * this lock/unlock section also waits them to finish. |
10307 | */ | |
d56d938b PB |
10308 | mutex_lock(&ctx->uring_lock); |
10309 | while (!list_empty(&ctx->tctx_list)) { | |
b5bb3a24 PB |
10310 | WARN_ON_ONCE(time_after(jiffies, timeout)); |
10311 | ||
d56d938b PB |
10312 | node = list_first_entry(&ctx->tctx_list, struct io_tctx_node, |
10313 | ctx_node); | |
7f00651a PB |
10314 | /* don't spin on a single task if cancellation failed */ |
10315 | list_rotate_left(&ctx->tctx_list); | |
d56d938b PB |
10316 | ret = task_work_add(node->task, &exit.task_work, TWA_SIGNAL); |
10317 | if (WARN_ON_ONCE(ret)) | |
10318 | continue; | |
d56d938b PB |
10319 | |
10320 | mutex_unlock(&ctx->uring_lock); | |
10321 | wait_for_completion(&exit.completion); | |
d56d938b PB |
10322 | mutex_lock(&ctx->uring_lock); |
10323 | } | |
10324 | mutex_unlock(&ctx->uring_lock); | |
79ebeaee JA |
10325 | spin_lock(&ctx->completion_lock); |
10326 | spin_unlock(&ctx->completion_lock); | |
d56d938b | 10327 | |
85faa7b8 JA |
10328 | io_ring_ctx_free(ctx); |
10329 | } | |
10330 | ||
80c4cbdb | 10331 | /* Returns true if we found and killed one or more timeouts */ |
c072481d PB |
10332 | static __cold bool io_kill_timeouts(struct io_ring_ctx *ctx, |
10333 | struct task_struct *tsk, bool cancel_all) | |
80c4cbdb PB |
10334 | { |
10335 | struct io_kiocb *req, *tmp; | |
10336 | int canceled = 0; | |
10337 | ||
79ebeaee JA |
10338 | spin_lock(&ctx->completion_lock); |
10339 | spin_lock_irq(&ctx->timeout_lock); | |
80c4cbdb | 10340 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
3dd0c97a | 10341 | if (io_match_task(req, tsk, cancel_all)) { |
80c4cbdb PB |
10342 | io_kill_timeout(req, -ECANCELED); |
10343 | canceled++; | |
10344 | } | |
10345 | } | |
79ebeaee | 10346 | spin_unlock_irq(&ctx->timeout_lock); |
51520426 PB |
10347 | if (canceled != 0) |
10348 | io_commit_cqring(ctx); | |
79ebeaee | 10349 | spin_unlock(&ctx->completion_lock); |
80c4cbdb PB |
10350 | if (canceled != 0) |
10351 | io_cqring_ev_posted(ctx); | |
10352 | return canceled != 0; | |
10353 | } | |
10354 | ||
c072481d | 10355 | static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
2b188cc1 | 10356 | { |
61cf9370 MWO |
10357 | unsigned long index; |
10358 | struct creds *creds; | |
10359 | ||
2b188cc1 JA |
10360 | mutex_lock(&ctx->uring_lock); |
10361 | percpu_ref_kill(&ctx->refs); | |
634578f8 | 10362 | if (ctx->rings) |
6c2450ae | 10363 | __io_cqring_overflow_flush(ctx, true); |
61cf9370 MWO |
10364 | xa_for_each(&ctx->personalities, index, creds) |
10365 | io_unregister_personality(ctx, index); | |
2b188cc1 JA |
10366 | mutex_unlock(&ctx->uring_lock); |
10367 | ||
3dd0c97a PB |
10368 | io_kill_timeouts(ctx, NULL, true); |
10369 | io_poll_remove_all(ctx, NULL, true); | |
561fb04a | 10370 | |
15dff286 | 10371 | /* if we failed setting up the ctx, we might not have any rings */ |
b2edc0a7 | 10372 | io_iopoll_try_reap_events(ctx); |
309fc03a | 10373 | |
85faa7b8 | 10374 | INIT_WORK(&ctx->exit_work, io_ring_exit_work); |
fc666777 JA |
10375 | /* |
10376 | * Use system_unbound_wq to avoid spawning tons of event kworkers | |
10377 | * if we're exiting a ton of rings at the same time. It just adds | |
10378 | * noise and overhead, there's no discernable change in runtime | |
10379 | * over using system_wq. | |
10380 | */ | |
10381 | queue_work(system_unbound_wq, &ctx->exit_work); | |
2b188cc1 JA |
10382 | } |
10383 | ||
10384 | static int io_uring_release(struct inode *inode, struct file *file) | |
10385 | { | |
10386 | struct io_ring_ctx *ctx = file->private_data; | |
10387 | ||
10388 | file->private_data = NULL; | |
10389 | io_ring_ctx_wait_and_kill(ctx); | |
10390 | return 0; | |
10391 | } | |
10392 | ||
f6edbabb PB |
10393 | struct io_task_cancel { |
10394 | struct task_struct *task; | |
3dd0c97a | 10395 | bool all; |
f6edbabb | 10396 | }; |
f254ac04 | 10397 | |
f6edbabb | 10398 | static bool io_cancel_task_cb(struct io_wq_work *work, void *data) |
b711d4ea | 10399 | { |
9a472ef7 | 10400 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
f6edbabb | 10401 | struct io_task_cancel *cancel = data; |
9a472ef7 | 10402 | |
6af3f48b | 10403 | return io_match_task_safe(req, cancel->task, cancel->all); |
b711d4ea JA |
10404 | } |
10405 | ||
c072481d PB |
10406 | static __cold bool io_cancel_defer_files(struct io_ring_ctx *ctx, |
10407 | struct task_struct *task, | |
10408 | bool cancel_all) | |
b7ddce3c | 10409 | { |
e1915f76 | 10410 | struct io_defer_entry *de; |
b7ddce3c PB |
10411 | LIST_HEAD(list); |
10412 | ||
79ebeaee | 10413 | spin_lock(&ctx->completion_lock); |
b7ddce3c | 10414 | list_for_each_entry_reverse(de, &ctx->defer_list, list) { |
6af3f48b | 10415 | if (io_match_task_safe(de->req, task, cancel_all)) { |
b7ddce3c PB |
10416 | list_cut_position(&list, &ctx->defer_list, &de->list); |
10417 | break; | |
10418 | } | |
10419 | } | |
79ebeaee | 10420 | spin_unlock(&ctx->completion_lock); |
e1915f76 PB |
10421 | if (list_empty(&list)) |
10422 | return false; | |
b7ddce3c PB |
10423 | |
10424 | while (!list_empty(&list)) { | |
10425 | de = list_first_entry(&list, struct io_defer_entry, list); | |
10426 | list_del_init(&de->list); | |
f41db273 | 10427 | io_req_complete_failed(de->req, -ECANCELED); |
b7ddce3c PB |
10428 | kfree(de); |
10429 | } | |
e1915f76 | 10430 | return true; |
b7ddce3c PB |
10431 | } |
10432 | ||
c072481d | 10433 | static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx) |
1b00764f PB |
10434 | { |
10435 | struct io_tctx_node *node; | |
10436 | enum io_wq_cancel cret; | |
10437 | bool ret = false; | |
10438 | ||
10439 | mutex_lock(&ctx->uring_lock); | |
10440 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
10441 | struct io_uring_task *tctx = node->task->io_uring; | |
10442 | ||
10443 | /* | |
10444 | * io_wq will stay alive while we hold uring_lock, because it's | |
10445 | * killed after ctx nodes, which requires to take the lock. | |
10446 | */ | |
10447 | if (!tctx || !tctx->io_wq) | |
10448 | continue; | |
10449 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_ctx_cb, ctx, true); | |
10450 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10451 | } | |
10452 | mutex_unlock(&ctx->uring_lock); | |
10453 | ||
10454 | return ret; | |
10455 | } | |
10456 | ||
c072481d PB |
10457 | static __cold void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, |
10458 | struct task_struct *task, | |
10459 | bool cancel_all) | |
9936c7c2 | 10460 | { |
3dd0c97a | 10461 | struct io_task_cancel cancel = { .task = task, .all = cancel_all, }; |
1b00764f | 10462 | struct io_uring_task *tctx = task ? task->io_uring : NULL; |
9936c7c2 PB |
10463 | |
10464 | while (1) { | |
10465 | enum io_wq_cancel cret; | |
10466 | bool ret = false; | |
10467 | ||
1b00764f PB |
10468 | if (!task) { |
10469 | ret |= io_uring_try_cancel_iowq(ctx); | |
10470 | } else if (tctx && tctx->io_wq) { | |
10471 | /* | |
10472 | * Cancels requests of all rings, not only @ctx, but | |
10473 | * it's fine as the task is in exit/exec. | |
10474 | */ | |
5aa75ed5 | 10475 | cret = io_wq_cancel_cb(tctx->io_wq, io_cancel_task_cb, |
9936c7c2 PB |
10476 | &cancel, true); |
10477 | ret |= (cret != IO_WQ_CANCEL_NOTFOUND); | |
10478 | } | |
10479 | ||
10480 | /* SQPOLL thread does its own polling */ | |
3dd0c97a | 10481 | if ((!(ctx->flags & IORING_SETUP_SQPOLL) && cancel_all) || |
d052d1d6 | 10482 | (ctx->sq_data && ctx->sq_data->thread == current)) { |
5eef4e87 | 10483 | while (!wq_list_empty(&ctx->iopoll_list)) { |
9936c7c2 PB |
10484 | io_iopoll_try_reap_events(ctx); |
10485 | ret = true; | |
10486 | } | |
10487 | } | |
10488 | ||
3dd0c97a PB |
10489 | ret |= io_cancel_defer_files(ctx, task, cancel_all); |
10490 | ret |= io_poll_remove_all(ctx, task, cancel_all); | |
10491 | ret |= io_kill_timeouts(ctx, task, cancel_all); | |
e5dc480d PB |
10492 | if (task) |
10493 | ret |= io_run_task_work(); | |
9936c7c2 PB |
10494 | if (!ret) |
10495 | break; | |
10496 | cond_resched(); | |
10497 | } | |
10498 | } | |
10499 | ||
eef51daa | 10500 | static int __io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
0f212204 | 10501 | { |
236434c3 | 10502 | struct io_uring_task *tctx = current->io_uring; |
13bf43f5 | 10503 | struct io_tctx_node *node; |
a528b04e | 10504 | int ret; |
236434c3 MWO |
10505 | |
10506 | if (unlikely(!tctx)) { | |
5aa75ed5 | 10507 | ret = io_uring_alloc_task_context(current, ctx); |
0f212204 JA |
10508 | if (unlikely(ret)) |
10509 | return ret; | |
e139a1ec | 10510 | |
236434c3 | 10511 | tctx = current->io_uring; |
e139a1ec PB |
10512 | if (ctx->iowq_limits_set) { |
10513 | unsigned int limits[2] = { ctx->iowq_limits[0], | |
10514 | ctx->iowq_limits[1], }; | |
10515 | ||
10516 | ret = io_wq_max_workers(tctx->io_wq, limits); | |
10517 | if (ret) | |
10518 | return ret; | |
10519 | } | |
0f212204 | 10520 | } |
cf27f3b1 PB |
10521 | if (!xa_load(&tctx->xa, (unsigned long)ctx)) { |
10522 | node = kmalloc(sizeof(*node), GFP_KERNEL); | |
10523 | if (!node) | |
10524 | return -ENOMEM; | |
10525 | node->ctx = ctx; | |
10526 | node->task = current; | |
13bf43f5 | 10527 | |
cf27f3b1 PB |
10528 | ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx, |
10529 | node, GFP_KERNEL)); | |
10530 | if (ret) { | |
10531 | kfree(node); | |
10532 | return ret; | |
0f212204 | 10533 | } |
cf27f3b1 PB |
10534 | |
10535 | mutex_lock(&ctx->uring_lock); | |
10536 | list_add(&node->ctx_node, &ctx->tctx_list); | |
10537 | mutex_unlock(&ctx->uring_lock); | |
0f212204 | 10538 | } |
cf27f3b1 | 10539 | tctx->last = ctx; |
0f212204 JA |
10540 | return 0; |
10541 | } | |
10542 | ||
cf27f3b1 PB |
10543 | /* |
10544 | * Note that this task has used io_uring. We use it for cancelation purposes. | |
10545 | */ | |
eef51daa | 10546 | static inline int io_uring_add_tctx_node(struct io_ring_ctx *ctx) |
cf27f3b1 PB |
10547 | { |
10548 | struct io_uring_task *tctx = current->io_uring; | |
10549 | ||
10550 | if (likely(tctx && tctx->last == ctx)) | |
10551 | return 0; | |
eef51daa | 10552 | return __io_uring_add_tctx_node(ctx); |
cf27f3b1 PB |
10553 | } |
10554 | ||
0f212204 JA |
10555 | /* |
10556 | * Remove this io_uring_file -> task mapping. | |
10557 | */ | |
c072481d | 10558 | static __cold void io_uring_del_tctx_node(unsigned long index) |
0f212204 JA |
10559 | { |
10560 | struct io_uring_task *tctx = current->io_uring; | |
13bf43f5 | 10561 | struct io_tctx_node *node; |
2941267b | 10562 | |
eebd2e37 PB |
10563 | if (!tctx) |
10564 | return; | |
13bf43f5 PB |
10565 | node = xa_erase(&tctx->xa, index); |
10566 | if (!node) | |
2941267b | 10567 | return; |
0f212204 | 10568 | |
13bf43f5 PB |
10569 | WARN_ON_ONCE(current != node->task); |
10570 | WARN_ON_ONCE(list_empty(&node->ctx_node)); | |
10571 | ||
10572 | mutex_lock(&node->ctx->uring_lock); | |
10573 | list_del(&node->ctx_node); | |
10574 | mutex_unlock(&node->ctx->uring_lock); | |
10575 | ||
baf186c4 | 10576 | if (tctx->last == node->ctx) |
0f212204 | 10577 | tctx->last = NULL; |
13bf43f5 | 10578 | kfree(node); |
0f212204 JA |
10579 | } |
10580 | ||
c072481d | 10581 | static __cold void io_uring_clean_tctx(struct io_uring_task *tctx) |
de7f1d9e | 10582 | { |
ba5ef6dc | 10583 | struct io_wq *wq = tctx->io_wq; |
13bf43f5 | 10584 | struct io_tctx_node *node; |
de7f1d9e PB |
10585 | unsigned long index; |
10586 | ||
8bab4c09 | 10587 | xa_for_each(&tctx->xa, index, node) { |
eef51daa | 10588 | io_uring_del_tctx_node(index); |
8bab4c09 JA |
10589 | cond_resched(); |
10590 | } | |
b16ef427 ME |
10591 | if (wq) { |
10592 | /* | |
f6f9b278 | 10593 | * Must be after io_uring_del_tctx_node() (removes nodes under |
b16ef427 ME |
10594 | * uring_lock) to avoid race with io_uring_try_cancel_iowq(). |
10595 | */ | |
ba5ef6dc | 10596 | io_wq_put_and_exit(wq); |
dadebc35 | 10597 | tctx->io_wq = NULL; |
b16ef427 | 10598 | } |
de7f1d9e PB |
10599 | } |
10600 | ||
3f48cf18 | 10601 | static s64 tctx_inflight(struct io_uring_task *tctx, bool tracked) |
521d6a73 | 10602 | { |
3f48cf18 PB |
10603 | if (tracked) |
10604 | return atomic_read(&tctx->inflight_tracked); | |
521d6a73 PB |
10605 | return percpu_counter_sum(&tctx->inflight); |
10606 | } | |
10607 | ||
78cc687b PB |
10608 | /* |
10609 | * Find any io_uring ctx that this task has registered or done IO on, and cancel | |
78a78060 | 10610 | * requests. @sqd should be not-null IFF it's an SQPOLL thread cancellation. |
78cc687b | 10611 | */ |
c072481d PB |
10612 | static __cold void io_uring_cancel_generic(bool cancel_all, |
10613 | struct io_sq_data *sqd) | |
0e9ddb39 | 10614 | { |
521d6a73 | 10615 | struct io_uring_task *tctx = current->io_uring; |
734551df | 10616 | struct io_ring_ctx *ctx; |
0e9ddb39 PB |
10617 | s64 inflight; |
10618 | DEFINE_WAIT(wait); | |
fdaf083c | 10619 | |
78cc687b PB |
10620 | WARN_ON_ONCE(sqd && sqd->thread != current); |
10621 | ||
6d042ffb PO |
10622 | if (!current->io_uring) |
10623 | return; | |
17a91051 PB |
10624 | if (tctx->io_wq) |
10625 | io_wq_exit_start(tctx->io_wq); | |
10626 | ||
0e9ddb39 PB |
10627 | atomic_inc(&tctx->in_idle); |
10628 | do { | |
e9dbe221 | 10629 | io_uring_drop_tctx_refs(current); |
0e9ddb39 | 10630 | /* read completions before cancelations */ |
78cc687b | 10631 | inflight = tctx_inflight(tctx, !cancel_all); |
0e9ddb39 PB |
10632 | if (!inflight) |
10633 | break; | |
fdaf083c | 10634 | |
78cc687b PB |
10635 | if (!sqd) { |
10636 | struct io_tctx_node *node; | |
10637 | unsigned long index; | |
0f212204 | 10638 | |
78cc687b PB |
10639 | xa_for_each(&tctx->xa, index, node) { |
10640 | /* sqpoll task will cancel all its requests */ | |
10641 | if (node->ctx->sq_data) | |
10642 | continue; | |
10643 | io_uring_try_cancel_requests(node->ctx, current, | |
10644 | cancel_all); | |
10645 | } | |
10646 | } else { | |
10647 | list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) | |
10648 | io_uring_try_cancel_requests(ctx, current, | |
10649 | cancel_all); | |
10650 | } | |
17a91051 | 10651 | |
78a78060 JA |
10652 | prepare_to_wait(&tctx->wait, &wait, TASK_INTERRUPTIBLE); |
10653 | io_run_task_work(); | |
e9dbe221 | 10654 | io_uring_drop_tctx_refs(current); |
78a78060 | 10655 | |
0f212204 | 10656 | /* |
a1bb3cd5 PB |
10657 | * If we've seen completions, retry without waiting. This |
10658 | * avoids a race where a completion comes in before we did | |
10659 | * prepare_to_wait(). | |
0f212204 | 10660 | */ |
3dd0c97a | 10661 | if (inflight == tctx_inflight(tctx, !cancel_all)) |
a1bb3cd5 | 10662 | schedule(); |
f57555ed | 10663 | finish_wait(&tctx->wait, &wait); |
d8a6df10 | 10664 | } while (1); |
de7f1d9e | 10665 | |
8452d4a6 | 10666 | io_uring_clean_tctx(tctx); |
3dd0c97a | 10667 | if (cancel_all) { |
3cc7fdb9 PB |
10668 | /* |
10669 | * We shouldn't run task_works after cancel, so just leave | |
10670 | * ->in_idle set for normal exit. | |
10671 | */ | |
10672 | atomic_dec(&tctx->in_idle); | |
3f48cf18 PB |
10673 | /* for exec all current's requests should be gone, kill tctx */ |
10674 | __io_uring_free(current); | |
10675 | } | |
44e728b8 PB |
10676 | } |
10677 | ||
f552a27a | 10678 | void __io_uring_cancel(bool cancel_all) |
78cc687b | 10679 | { |
f552a27a | 10680 | io_uring_cancel_generic(cancel_all, NULL); |
78cc687b PB |
10681 | } |
10682 | ||
e7a6c00d JA |
10683 | void io_uring_unreg_ringfd(void) |
10684 | { | |
10685 | struct io_uring_task *tctx = current->io_uring; | |
10686 | int i; | |
10687 | ||
10688 | for (i = 0; i < IO_RINGFD_REG_MAX; i++) { | |
10689 | if (tctx->registered_rings[i]) { | |
10690 | fput(tctx->registered_rings[i]); | |
10691 | tctx->registered_rings[i] = NULL; | |
10692 | } | |
10693 | } | |
10694 | } | |
10695 | ||
10696 | static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd, | |
10697 | int start, int end) | |
10698 | { | |
10699 | struct file *file; | |
10700 | int offset; | |
10701 | ||
10702 | for (offset = start; offset < end; offset++) { | |
10703 | offset = array_index_nospec(offset, IO_RINGFD_REG_MAX); | |
10704 | if (tctx->registered_rings[offset]) | |
10705 | continue; | |
10706 | ||
10707 | file = fget(fd); | |
10708 | if (!file) { | |
10709 | return -EBADF; | |
10710 | } else if (file->f_op != &io_uring_fops) { | |
10711 | fput(file); | |
10712 | return -EOPNOTSUPP; | |
10713 | } | |
10714 | tctx->registered_rings[offset] = file; | |
10715 | return offset; | |
10716 | } | |
10717 | ||
10718 | return -EBUSY; | |
10719 | } | |
10720 | ||
10721 | /* | |
10722 | * Register a ring fd to avoid fdget/fdput for each io_uring_enter() | |
10723 | * invocation. User passes in an array of struct io_uring_rsrc_update | |
10724 | * with ->data set to the ring_fd, and ->offset given for the desired | |
10725 | * index. If no index is desired, application may set ->offset == -1U | |
10726 | * and we'll find an available index. Returns number of entries | |
10727 | * successfully processed, or < 0 on error if none were processed. | |
10728 | */ | |
10729 | static int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg, | |
10730 | unsigned nr_args) | |
10731 | { | |
10732 | struct io_uring_rsrc_update __user *arg = __arg; | |
10733 | struct io_uring_rsrc_update reg; | |
10734 | struct io_uring_task *tctx; | |
10735 | int ret, i; | |
10736 | ||
10737 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10738 | return -EINVAL; | |
10739 | ||
10740 | mutex_unlock(&ctx->uring_lock); | |
10741 | ret = io_uring_add_tctx_node(ctx); | |
10742 | mutex_lock(&ctx->uring_lock); | |
10743 | if (ret) | |
10744 | return ret; | |
10745 | ||
10746 | tctx = current->io_uring; | |
10747 | for (i = 0; i < nr_args; i++) { | |
10748 | int start, end; | |
10749 | ||
10750 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10751 | ret = -EFAULT; | |
10752 | break; | |
10753 | } | |
10754 | ||
10755 | if (reg.offset == -1U) { | |
10756 | start = 0; | |
10757 | end = IO_RINGFD_REG_MAX; | |
10758 | } else { | |
10759 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10760 | ret = -EINVAL; | |
10761 | break; | |
10762 | } | |
10763 | start = reg.offset; | |
10764 | end = start + 1; | |
10765 | } | |
10766 | ||
10767 | ret = io_ring_add_registered_fd(tctx, reg.data, start, end); | |
10768 | if (ret < 0) | |
10769 | break; | |
10770 | ||
10771 | reg.offset = ret; | |
10772 | if (copy_to_user(&arg[i], ®, sizeof(reg))) { | |
10773 | fput(tctx->registered_rings[reg.offset]); | |
10774 | tctx->registered_rings[reg.offset] = NULL; | |
10775 | ret = -EFAULT; | |
10776 | break; | |
10777 | } | |
10778 | } | |
10779 | ||
10780 | return i ? i : ret; | |
10781 | } | |
10782 | ||
10783 | static int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg, | |
10784 | unsigned nr_args) | |
10785 | { | |
10786 | struct io_uring_rsrc_update __user *arg = __arg; | |
10787 | struct io_uring_task *tctx = current->io_uring; | |
10788 | struct io_uring_rsrc_update reg; | |
10789 | int ret = 0, i; | |
10790 | ||
10791 | if (!nr_args || nr_args > IO_RINGFD_REG_MAX) | |
10792 | return -EINVAL; | |
10793 | if (!tctx) | |
10794 | return 0; | |
10795 | ||
10796 | for (i = 0; i < nr_args; i++) { | |
10797 | if (copy_from_user(®, &arg[i], sizeof(reg))) { | |
10798 | ret = -EFAULT; | |
10799 | break; | |
10800 | } | |
10801 | if (reg.offset >= IO_RINGFD_REG_MAX) { | |
10802 | ret = -EINVAL; | |
10803 | break; | |
10804 | } | |
10805 | ||
10806 | reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX); | |
10807 | if (tctx->registered_rings[reg.offset]) { | |
10808 | fput(tctx->registered_rings[reg.offset]); | |
10809 | tctx->registered_rings[reg.offset] = NULL; | |
10810 | } | |
10811 | } | |
10812 | ||
10813 | return i ? i : ret; | |
10814 | } | |
10815 | ||
6c5c240e RP |
10816 | static void *io_uring_validate_mmap_request(struct file *file, |
10817 | loff_t pgoff, size_t sz) | |
2b188cc1 | 10818 | { |
2b188cc1 | 10819 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 10820 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
10821 | struct page *page; |
10822 | void *ptr; | |
10823 | ||
10824 | switch (offset) { | |
10825 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
10826 | case IORING_OFF_CQ_RING: |
10827 | ptr = ctx->rings; | |
2b188cc1 JA |
10828 | break; |
10829 | case IORING_OFF_SQES: | |
10830 | ptr = ctx->sq_sqes; | |
10831 | break; | |
2b188cc1 | 10832 | default: |
6c5c240e | 10833 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
10834 | } |
10835 | ||
10836 | page = virt_to_head_page(ptr); | |
a50b854e | 10837 | if (sz > page_size(page)) |
6c5c240e RP |
10838 | return ERR_PTR(-EINVAL); |
10839 | ||
10840 | return ptr; | |
10841 | } | |
10842 | ||
10843 | #ifdef CONFIG_MMU | |
10844 | ||
c072481d | 10845 | static __cold int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
6c5c240e RP |
10846 | { |
10847 | size_t sz = vma->vm_end - vma->vm_start; | |
10848 | unsigned long pfn; | |
10849 | void *ptr; | |
10850 | ||
10851 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
10852 | if (IS_ERR(ptr)) | |
10853 | return PTR_ERR(ptr); | |
2b188cc1 JA |
10854 | |
10855 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
10856 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
10857 | } | |
10858 | ||
6c5c240e RP |
10859 | #else /* !CONFIG_MMU */ |
10860 | ||
10861 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
10862 | { | |
10863 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
10864 | } | |
10865 | ||
10866 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
10867 | { | |
10868 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
10869 | } | |
10870 | ||
10871 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
10872 | unsigned long addr, unsigned long len, | |
10873 | unsigned long pgoff, unsigned long flags) | |
10874 | { | |
10875 | void *ptr; | |
10876 | ||
10877 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
10878 | if (IS_ERR(ptr)) | |
10879 | return PTR_ERR(ptr); | |
10880 | ||
10881 | return (unsigned long) ptr; | |
10882 | } | |
10883 | ||
10884 | #endif /* !CONFIG_MMU */ | |
10885 | ||
d9d05217 | 10886 | static int io_sqpoll_wait_sq(struct io_ring_ctx *ctx) |
90554200 JA |
10887 | { |
10888 | DEFINE_WAIT(wait); | |
10889 | ||
10890 | do { | |
10891 | if (!io_sqring_full(ctx)) | |
10892 | break; | |
90554200 JA |
10893 | prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE); |
10894 | ||
10895 | if (!io_sqring_full(ctx)) | |
10896 | break; | |
90554200 JA |
10897 | schedule(); |
10898 | } while (!signal_pending(current)); | |
10899 | ||
10900 | finish_wait(&ctx->sqo_sq_wait, &wait); | |
5199328a | 10901 | return 0; |
90554200 JA |
10902 | } |
10903 | ||
c73ebb68 HX |
10904 | static int io_get_ext_arg(unsigned flags, const void __user *argp, size_t *argsz, |
10905 | struct __kernel_timespec __user **ts, | |
10906 | const sigset_t __user **sig) | |
10907 | { | |
10908 | struct io_uring_getevents_arg arg; | |
10909 | ||
10910 | /* | |
10911 | * If EXT_ARG isn't set, then we have no timespec and the argp pointer | |
10912 | * is just a pointer to the sigset_t. | |
10913 | */ | |
10914 | if (!(flags & IORING_ENTER_EXT_ARG)) { | |
10915 | *sig = (const sigset_t __user *) argp; | |
10916 | *ts = NULL; | |
10917 | return 0; | |
10918 | } | |
10919 | ||
10920 | /* | |
10921 | * EXT_ARG is set - ensure we agree on the size of it and copy in our | |
10922 | * timespec and sigset_t pointers if good. | |
10923 | */ | |
10924 | if (*argsz != sizeof(arg)) | |
10925 | return -EINVAL; | |
10926 | if (copy_from_user(&arg, argp, sizeof(arg))) | |
10927 | return -EFAULT; | |
10928 | *sig = u64_to_user_ptr(arg.sigmask); | |
10929 | *argsz = arg.sigmask_sz; | |
10930 | *ts = u64_to_user_ptr(arg.ts); | |
10931 | return 0; | |
10932 | } | |
10933 | ||
2b188cc1 | 10934 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
c73ebb68 HX |
10935 | u32, min_complete, u32, flags, const void __user *, argp, |
10936 | size_t, argsz) | |
2b188cc1 JA |
10937 | { |
10938 | struct io_ring_ctx *ctx; | |
2b188cc1 JA |
10939 | int submitted = 0; |
10940 | struct fd f; | |
33f993da | 10941 | long ret; |
2b188cc1 | 10942 | |
4c6e277c | 10943 | io_run_task_work(); |
b41e9852 | 10944 | |
33f993da | 10945 | if (unlikely(flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP | |
e7a6c00d JA |
10946 | IORING_ENTER_SQ_WAIT | IORING_ENTER_EXT_ARG | |
10947 | IORING_ENTER_REGISTERED_RING))) | |
2b188cc1 JA |
10948 | return -EINVAL; |
10949 | ||
e7a6c00d JA |
10950 | /* |
10951 | * Ring fd has been registered via IORING_REGISTER_RING_FDS, we | |
10952 | * need only dereference our task private array to find it. | |
10953 | */ | |
10954 | if (flags & IORING_ENTER_REGISTERED_RING) { | |
10955 | struct io_uring_task *tctx = current->io_uring; | |
10956 | ||
10957 | if (!tctx || fd >= IO_RINGFD_REG_MAX) | |
10958 | return -EINVAL; | |
10959 | fd = array_index_nospec(fd, IO_RINGFD_REG_MAX); | |
10960 | f.file = tctx->registered_rings[fd]; | |
10961 | if (unlikely(!f.file)) | |
10962 | return -EBADF; | |
10963 | } else { | |
10964 | f = fdget(fd); | |
10965 | if (unlikely(!f.file)) | |
10966 | return -EBADF; | |
10967 | } | |
2b188cc1 JA |
10968 | |
10969 | ret = -EOPNOTSUPP; | |
33f993da | 10970 | if (unlikely(f.file->f_op != &io_uring_fops)) |
2b188cc1 JA |
10971 | goto out_fput; |
10972 | ||
10973 | ret = -ENXIO; | |
10974 | ctx = f.file->private_data; | |
33f993da | 10975 | if (unlikely(!percpu_ref_tryget(&ctx->refs))) |
2b188cc1 JA |
10976 | goto out_fput; |
10977 | ||
7e84e1c7 | 10978 | ret = -EBADFD; |
33f993da | 10979 | if (unlikely(ctx->flags & IORING_SETUP_R_DISABLED)) |
7e84e1c7 SG |
10980 | goto out; |
10981 | ||
6c271ce2 JA |
10982 | /* |
10983 | * For SQ polling, the thread will do all submissions and completions. | |
10984 | * Just return the requested submit count, and wake the thread if | |
10985 | * we were asked to. | |
10986 | */ | |
b2a9eada | 10987 | ret = 0; |
6c271ce2 | 10988 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
90f67366 | 10989 | io_cqring_overflow_flush(ctx); |
89448c47 | 10990 | |
21f96522 JA |
10991 | if (unlikely(ctx->sq_data->thread == NULL)) { |
10992 | ret = -EOWNERDEAD; | |
04147488 | 10993 | goto out; |
21f96522 | 10994 | } |
6c271ce2 | 10995 | if (flags & IORING_ENTER_SQ_WAKEUP) |
534ca6d6 | 10996 | wake_up(&ctx->sq_data->wait); |
d9d05217 PB |
10997 | if (flags & IORING_ENTER_SQ_WAIT) { |
10998 | ret = io_sqpoll_wait_sq(ctx); | |
10999 | if (ret) | |
11000 | goto out; | |
11001 | } | |
6c271ce2 | 11002 | submitted = to_submit; |
b2a9eada | 11003 | } else if (to_submit) { |
eef51daa | 11004 | ret = io_uring_add_tctx_node(ctx); |
0f212204 JA |
11005 | if (unlikely(ret)) |
11006 | goto out; | |
2b188cc1 | 11007 | mutex_lock(&ctx->uring_lock); |
0f212204 | 11008 | submitted = io_submit_sqes(ctx, to_submit); |
2b188cc1 | 11009 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
11010 | |
11011 | if (submitted != to_submit) | |
11012 | goto out; | |
2b188cc1 JA |
11013 | } |
11014 | if (flags & IORING_ENTER_GETEVENTS) { | |
c73ebb68 HX |
11015 | const sigset_t __user *sig; |
11016 | struct __kernel_timespec __user *ts; | |
11017 | ||
11018 | ret = io_get_ext_arg(flags, argp, &argsz, &ts, &sig); | |
11019 | if (unlikely(ret)) | |
11020 | goto out; | |
11021 | ||
2b188cc1 JA |
11022 | min_complete = min(min_complete, ctx->cq_entries); |
11023 | ||
32b2244a XW |
11024 | /* |
11025 | * When SETUP_IOPOLL and SETUP_SQPOLL are both enabled, user | |
11026 | * space applications don't need to do io completion events | |
11027 | * polling again, they can rely on io_sq_thread to do polling | |
11028 | * work, which can reduce cpu usage and uring_lock contention. | |
11029 | */ | |
11030 | if (ctx->flags & IORING_SETUP_IOPOLL && | |
11031 | !(ctx->flags & IORING_SETUP_SQPOLL)) { | |
7668b92a | 11032 | ret = io_iopoll_check(ctx, min_complete); |
def596e9 | 11033 | } else { |
c73ebb68 | 11034 | ret = io_cqring_wait(ctx, min_complete, sig, argsz, ts); |
def596e9 | 11035 | } |
2b188cc1 JA |
11036 | } |
11037 | ||
7c504e65 | 11038 | out: |
6805b32e | 11039 | percpu_ref_put(&ctx->refs); |
2b188cc1 | 11040 | out_fput: |
e7a6c00d JA |
11041 | if (!(flags & IORING_ENTER_REGISTERED_RING)) |
11042 | fdput(f); | |
2b188cc1 JA |
11043 | return submitted ? submitted : ret; |
11044 | } | |
11045 | ||
bebdb65e | 11046 | #ifdef CONFIG_PROC_FS |
c072481d | 11047 | static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id, |
61cf9370 | 11048 | const struct cred *cred) |
87ce955b | 11049 | { |
87ce955b JA |
11050 | struct user_namespace *uns = seq_user_ns(m); |
11051 | struct group_info *gi; | |
11052 | kernel_cap_t cap; | |
11053 | unsigned __capi; | |
11054 | int g; | |
11055 | ||
11056 | seq_printf(m, "%5d\n", id); | |
11057 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
11058 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
11059 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
11060 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
11061 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
11062 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
11063 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
11064 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
11065 | seq_puts(m, "\n\tGroups:\t"); | |
11066 | gi = cred->group_info; | |
11067 | for (g = 0; g < gi->ngroups; g++) { | |
11068 | seq_put_decimal_ull(m, g ? " " : "", | |
11069 | from_kgid_munged(uns, gi->gid[g])); | |
11070 | } | |
11071 | seq_puts(m, "\n\tCapEff:\t"); | |
11072 | cap = cred->cap_effective; | |
11073 | CAP_FOR_EACH_U32(__capi) | |
11074 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
11075 | seq_putc(m, '\n'); | |
11076 | return 0; | |
11077 | } | |
11078 | ||
c072481d PB |
11079 | static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, |
11080 | struct seq_file *m) | |
87ce955b | 11081 | { |
dbbe9c64 | 11082 | struct io_sq_data *sq = NULL; |
83f84356 HX |
11083 | struct io_overflow_cqe *ocqe; |
11084 | struct io_rings *r = ctx->rings; | |
11085 | unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; | |
83f84356 HX |
11086 | unsigned int sq_head = READ_ONCE(r->sq.head); |
11087 | unsigned int sq_tail = READ_ONCE(r->sq.tail); | |
11088 | unsigned int cq_head = READ_ONCE(r->cq.head); | |
11089 | unsigned int cq_tail = READ_ONCE(r->cq.tail); | |
f75d1183 | 11090 | unsigned int sq_entries, cq_entries; |
fad8e0de | 11091 | bool has_lock; |
83f84356 HX |
11092 | unsigned int i; |
11093 | ||
11094 | /* | |
11095 | * we may get imprecise sqe and cqe info if uring is actively running | |
11096 | * since we get cached_sq_head and cached_cq_tail without uring_lock | |
11097 | * and sq_tail and cq_head are changed by userspace. But it's ok since | |
11098 | * we usually use these info when it is stuck. | |
11099 | */ | |
c0235652 | 11100 | seq_printf(m, "SqMask:\t0x%x\n", sq_mask); |
f75d1183 JA |
11101 | seq_printf(m, "SqHead:\t%u\n", sq_head); |
11102 | seq_printf(m, "SqTail:\t%u\n", sq_tail); | |
11103 | seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head); | |
11104 | seq_printf(m, "CqMask:\t0x%x\n", cq_mask); | |
11105 | seq_printf(m, "CqHead:\t%u\n", cq_head); | |
11106 | seq_printf(m, "CqTail:\t%u\n", cq_tail); | |
11107 | seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail); | |
11108 | seq_printf(m, "SQEs:\t%u\n", sq_tail - ctx->cached_sq_head); | |
11109 | sq_entries = min(sq_tail - sq_head, ctx->sq_entries); | |
11110 | for (i = 0; i < sq_entries; i++) { | |
11111 | unsigned int entry = i + sq_head; | |
11112 | unsigned int sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); | |
a1957780 | 11113 | struct io_uring_sqe *sqe; |
f75d1183 JA |
11114 | |
11115 | if (sq_idx > sq_mask) | |
11116 | continue; | |
11117 | sqe = &ctx->sq_sqes[sq_idx]; | |
11118 | seq_printf(m, "%5u: opcode:%d, fd:%d, flags:%x, user_data:%llu\n", | |
11119 | sq_idx, sqe->opcode, sqe->fd, sqe->flags, | |
11120 | sqe->user_data); | |
83f84356 | 11121 | } |
f75d1183 JA |
11122 | seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); |
11123 | cq_entries = min(cq_tail - cq_head, ctx->cq_entries); | |
11124 | for (i = 0; i < cq_entries; i++) { | |
11125 | unsigned int entry = i + cq_head; | |
11126 | struct io_uring_cqe *cqe = &r->cqes[entry & cq_mask]; | |
83f84356 HX |
11127 | |
11128 | seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x\n", | |
f75d1183 JA |
11129 | entry & cq_mask, cqe->user_data, cqe->res, |
11130 | cqe->flags); | |
83f84356 | 11131 | } |
87ce955b | 11132 | |
fad8e0de JA |
11133 | /* |
11134 | * Avoid ABBA deadlock between the seq lock and the io_uring mutex, | |
11135 | * since fdinfo case grabs it in the opposite direction of normal use | |
11136 | * cases. If we fail to get the lock, we just don't iterate any | |
11137 | * structures that could be going away outside the io_uring mutex. | |
11138 | */ | |
11139 | has_lock = mutex_trylock(&ctx->uring_lock); | |
11140 | ||
5f3f26f9 | 11141 | if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) { |
dbbe9c64 | 11142 | sq = ctx->sq_data; |
5f3f26f9 JA |
11143 | if (!sq->thread) |
11144 | sq = NULL; | |
11145 | } | |
dbbe9c64 JQ |
11146 | |
11147 | seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1); | |
11148 | seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1); | |
87ce955b | 11149 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); |
fad8e0de | 11150 | for (i = 0; has_lock && i < ctx->nr_user_files; i++) { |
7b29f92d | 11151 | struct file *f = io_file_from_index(ctx, i); |
87ce955b | 11152 | |
87ce955b JA |
11153 | if (f) |
11154 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
11155 | else | |
11156 | seq_printf(m, "%5u: <none>\n", i); | |
11157 | } | |
11158 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
fad8e0de | 11159 | for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) { |
41edf1a5 | 11160 | struct io_mapped_ubuf *buf = ctx->user_bufs[i]; |
4751f53d | 11161 | unsigned int len = buf->ubuf_end - buf->ubuf; |
87ce955b | 11162 | |
4751f53d | 11163 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len); |
87ce955b | 11164 | } |
61cf9370 MWO |
11165 | if (has_lock && !xa_empty(&ctx->personalities)) { |
11166 | unsigned long index; | |
11167 | const struct cred *cred; | |
11168 | ||
87ce955b | 11169 | seq_printf(m, "Personalities:\n"); |
61cf9370 MWO |
11170 | xa_for_each(&ctx->personalities, index, cred) |
11171 | io_uring_show_cred(m, index, cred); | |
87ce955b | 11172 | } |
83f84356 HX |
11173 | if (has_lock) |
11174 | mutex_unlock(&ctx->uring_lock); | |
11175 | ||
11176 | seq_puts(m, "PollList:\n"); | |
79ebeaee | 11177 | spin_lock(&ctx->completion_lock); |
d7718a9d JA |
11178 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
11179 | struct hlist_head *list = &ctx->cancel_hash[i]; | |
11180 | struct io_kiocb *req; | |
11181 | ||
11182 | hlist_for_each_entry(req, list, hash_node) | |
11183 | seq_printf(m, " op=%d, task_works=%d\n", req->opcode, | |
7f62d40d | 11184 | task_work_pending(req->task)); |
d7718a9d | 11185 | } |
83f84356 HX |
11186 | |
11187 | seq_puts(m, "CqOverflowList:\n"); | |
11188 | list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { | |
11189 | struct io_uring_cqe *cqe = &ocqe->cqe; | |
11190 | ||
11191 | seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", | |
11192 | cqe->user_data, cqe->res, cqe->flags); | |
11193 | ||
11194 | } | |
11195 | ||
79ebeaee | 11196 | spin_unlock(&ctx->completion_lock); |
87ce955b JA |
11197 | } |
11198 | ||
c072481d | 11199 | static __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f) |
87ce955b JA |
11200 | { |
11201 | struct io_ring_ctx *ctx = f->private_data; | |
11202 | ||
11203 | if (percpu_ref_tryget(&ctx->refs)) { | |
11204 | __io_uring_show_fdinfo(ctx, m); | |
11205 | percpu_ref_put(&ctx->refs); | |
11206 | } | |
11207 | } | |
bebdb65e | 11208 | #endif |
87ce955b | 11209 | |
2b188cc1 JA |
11210 | static const struct file_operations io_uring_fops = { |
11211 | .release = io_uring_release, | |
11212 | .mmap = io_uring_mmap, | |
6c5c240e RP |
11213 | #ifndef CONFIG_MMU |
11214 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
11215 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
11216 | #endif | |
2b188cc1 | 11217 | .poll = io_uring_poll, |
bebdb65e | 11218 | #ifdef CONFIG_PROC_FS |
87ce955b | 11219 | .show_fdinfo = io_uring_show_fdinfo, |
bebdb65e | 11220 | #endif |
2b188cc1 JA |
11221 | }; |
11222 | ||
c072481d PB |
11223 | static __cold int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
11224 | struct io_uring_params *p) | |
2b188cc1 | 11225 | { |
75b28aff HV |
11226 | struct io_rings *rings; |
11227 | size_t size, sq_array_offset; | |
2b188cc1 | 11228 | |
bd740481 JA |
11229 | /* make sure these are sane, as we already accounted them */ |
11230 | ctx->sq_entries = p->sq_entries; | |
11231 | ctx->cq_entries = p->cq_entries; | |
11232 | ||
75b28aff HV |
11233 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
11234 | if (size == SIZE_MAX) | |
11235 | return -EOVERFLOW; | |
11236 | ||
11237 | rings = io_mem_alloc(size); | |
11238 | if (!rings) | |
2b188cc1 JA |
11239 | return -ENOMEM; |
11240 | ||
75b28aff HV |
11241 | ctx->rings = rings; |
11242 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
11243 | rings->sq_ring_mask = p->sq_entries - 1; | |
11244 | rings->cq_ring_mask = p->cq_entries - 1; | |
11245 | rings->sq_ring_entries = p->sq_entries; | |
11246 | rings->cq_ring_entries = p->cq_entries; | |
2b188cc1 JA |
11247 | |
11248 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
11249 | if (size == SIZE_MAX) { |
11250 | io_mem_free(ctx->rings); | |
11251 | ctx->rings = NULL; | |
2b188cc1 | 11252 | return -EOVERFLOW; |
eb065d30 | 11253 | } |
2b188cc1 JA |
11254 | |
11255 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
11256 | if (!ctx->sq_sqes) { |
11257 | io_mem_free(ctx->rings); | |
11258 | ctx->rings = NULL; | |
2b188cc1 | 11259 | return -ENOMEM; |
eb065d30 | 11260 | } |
2b188cc1 | 11261 | |
2b188cc1 JA |
11262 | return 0; |
11263 | } | |
11264 | ||
9faadcc8 PB |
11265 | static int io_uring_install_fd(struct io_ring_ctx *ctx, struct file *file) |
11266 | { | |
11267 | int ret, fd; | |
11268 | ||
11269 | fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
11270 | if (fd < 0) | |
11271 | return fd; | |
11272 | ||
eef51daa | 11273 | ret = io_uring_add_tctx_node(ctx); |
9faadcc8 PB |
11274 | if (ret) { |
11275 | put_unused_fd(fd); | |
11276 | return ret; | |
11277 | } | |
11278 | fd_install(fd, file); | |
11279 | return fd; | |
11280 | } | |
11281 | ||
2b188cc1 JA |
11282 | /* |
11283 | * Allocate an anonymous fd, this is what constitutes the application | |
11284 | * visible backing of an io_uring instance. The application mmaps this | |
11285 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
11286 | * we have to tie this fd to a socket for file garbage collection purposes. | |
11287 | */ | |
9faadcc8 | 11288 | static struct file *io_uring_get_file(struct io_ring_ctx *ctx) |
2b188cc1 JA |
11289 | { |
11290 | struct file *file; | |
9faadcc8 | 11291 | #if defined(CONFIG_UNIX) |
2b188cc1 JA |
11292 | int ret; |
11293 | ||
2b188cc1 JA |
11294 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
11295 | &ctx->ring_sock); | |
11296 | if (ret) | |
9faadcc8 | 11297 | return ERR_PTR(ret); |
2b188cc1 JA |
11298 | #endif |
11299 | ||
91a9ab7c PM |
11300 | file = anon_inode_getfile_secure("[io_uring]", &io_uring_fops, ctx, |
11301 | O_RDWR | O_CLOEXEC, NULL); | |
2b188cc1 | 11302 | #if defined(CONFIG_UNIX) |
9faadcc8 PB |
11303 | if (IS_ERR(file)) { |
11304 | sock_release(ctx->ring_sock); | |
11305 | ctx->ring_sock = NULL; | |
11306 | } else { | |
11307 | ctx->ring_sock->file = file; | |
0f212204 | 11308 | } |
2b188cc1 | 11309 | #endif |
9faadcc8 | 11310 | return file; |
2b188cc1 JA |
11311 | } |
11312 | ||
c072481d PB |
11313 | static __cold int io_uring_create(unsigned entries, struct io_uring_params *p, |
11314 | struct io_uring_params __user *params) | |
2b188cc1 | 11315 | { |
2b188cc1 | 11316 | struct io_ring_ctx *ctx; |
9faadcc8 | 11317 | struct file *file; |
2b188cc1 JA |
11318 | int ret; |
11319 | ||
8110c1a6 | 11320 | if (!entries) |
2b188cc1 | 11321 | return -EINVAL; |
8110c1a6 JA |
11322 | if (entries > IORING_MAX_ENTRIES) { |
11323 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11324 | return -EINVAL; | |
11325 | entries = IORING_MAX_ENTRIES; | |
11326 | } | |
2b188cc1 JA |
11327 | |
11328 | /* | |
11329 | * Use twice as many entries for the CQ ring. It's possible for the | |
11330 | * application to drive a higher depth than the size of the SQ ring, | |
11331 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
11332 | * some flexibility in overcommitting a bit. If the application has |
11333 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
11334 | * of CQ ring entries manually. | |
2b188cc1 JA |
11335 | */ |
11336 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
11337 | if (p->flags & IORING_SETUP_CQSIZE) { |
11338 | /* | |
11339 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
11340 | * to a power-of-two, if it isn't already. We do NOT impose | |
11341 | * any cq vs sq ring sizing. | |
11342 | */ | |
eb2667b3 | 11343 | if (!p->cq_entries) |
33a107f0 | 11344 | return -EINVAL; |
8110c1a6 JA |
11345 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
11346 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
11347 | return -EINVAL; | |
11348 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
11349 | } | |
eb2667b3 JQ |
11350 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
11351 | if (p->cq_entries < p->sq_entries) | |
11352 | return -EINVAL; | |
33a107f0 JA |
11353 | } else { |
11354 | p->cq_entries = 2 * p->sq_entries; | |
11355 | } | |
2b188cc1 | 11356 | |
2b188cc1 | 11357 | ctx = io_ring_ctx_alloc(p); |
62e398be | 11358 | if (!ctx) |
2b188cc1 | 11359 | return -ENOMEM; |
2b188cc1 | 11360 | ctx->compat = in_compat_syscall(); |
62e398be JA |
11361 | if (!capable(CAP_IPC_LOCK)) |
11362 | ctx->user = get_uid(current_user()); | |
2aede0e4 JA |
11363 | |
11364 | /* | |
11365 | * This is just grabbed for accounting purposes. When a process exits, | |
11366 | * the mm is exited and dropped before the files, hence we need to hang | |
11367 | * on to this mm purely for the purposes of being able to unaccount | |
11368 | * memory (locked/pinned vm). It's not used for anything else. | |
11369 | */ | |
6b7898eb | 11370 | mmgrab(current->mm); |
2aede0e4 | 11371 | ctx->mm_account = current->mm; |
6b7898eb | 11372 | |
2b188cc1 JA |
11373 | ret = io_allocate_scq_urings(ctx, p); |
11374 | if (ret) | |
11375 | goto err; | |
11376 | ||
7e84e1c7 | 11377 | ret = io_sq_offload_create(ctx, p); |
2b188cc1 JA |
11378 | if (ret) |
11379 | goto err; | |
eae071c9 | 11380 | /* always set a rsrc node */ |
47b228ce PB |
11381 | ret = io_rsrc_node_switch_start(ctx); |
11382 | if (ret) | |
11383 | goto err; | |
eae071c9 | 11384 | io_rsrc_node_switch(ctx, NULL); |
2b188cc1 | 11385 | |
2b188cc1 | 11386 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
11387 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
11388 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
11389 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
11390 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
11391 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
11392 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
11393 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
11394 | |
11395 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
11396 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
11397 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
11398 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
11399 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
11400 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
11401 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
0d9b5b3a | 11402 | p->cq_off.flags = offsetof(struct io_rings, cq_flags); |
ac90f249 | 11403 | |
7f13657d XW |
11404 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
11405 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | | |
5769a351 | 11406 | IORING_FEAT_CUR_PERSONALITY | IORING_FEAT_FAST_POLL | |
c73ebb68 | 11407 | IORING_FEAT_POLL_32BITS | IORING_FEAT_SQPOLL_NONFIXED | |
9690557e | 11408 | IORING_FEAT_EXT_ARG | IORING_FEAT_NATIVE_WORKERS | |
04c76b41 | 11409 | IORING_FEAT_RSRC_TAGS | IORING_FEAT_CQE_SKIP; |
7f13657d XW |
11410 | |
11411 | if (copy_to_user(params, p, sizeof(*p))) { | |
11412 | ret = -EFAULT; | |
11413 | goto err; | |
11414 | } | |
d1719f70 | 11415 | |
9faadcc8 PB |
11416 | file = io_uring_get_file(ctx); |
11417 | if (IS_ERR(file)) { | |
11418 | ret = PTR_ERR(file); | |
11419 | goto err; | |
11420 | } | |
11421 | ||
044c1ab3 JA |
11422 | /* |
11423 | * Install ring fd as the very last thing, so we don't risk someone | |
11424 | * having closed it before we finish setup | |
11425 | */ | |
9faadcc8 PB |
11426 | ret = io_uring_install_fd(ctx, file); |
11427 | if (ret < 0) { | |
11428 | /* fput will clean it up */ | |
11429 | fput(file); | |
11430 | return ret; | |
11431 | } | |
044c1ab3 | 11432 | |
c826bd7a | 11433 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
11434 | return ret; |
11435 | err: | |
11436 | io_ring_ctx_wait_and_kill(ctx); | |
11437 | return ret; | |
11438 | } | |
11439 | ||
11440 | /* | |
11441 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
11442 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
11443 | * params structure passed in. | |
11444 | */ | |
11445 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
11446 | { | |
11447 | struct io_uring_params p; | |
2b188cc1 JA |
11448 | int i; |
11449 | ||
11450 | if (copy_from_user(&p, params, sizeof(p))) | |
11451 | return -EFAULT; | |
11452 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
11453 | if (p.resv[i]) | |
11454 | return -EINVAL; | |
11455 | } | |
11456 | ||
6c271ce2 | 11457 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 11458 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
7e84e1c7 | 11459 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | |
bcbb7bf6 | 11460 | IORING_SETUP_R_DISABLED | IORING_SETUP_SUBMIT_ALL)) |
2b188cc1 JA |
11461 | return -EINVAL; |
11462 | ||
7f13657d | 11463 | return io_uring_create(entries, &p, params); |
2b188cc1 JA |
11464 | } |
11465 | ||
11466 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
11467 | struct io_uring_params __user *, params) | |
11468 | { | |
11469 | return io_uring_setup(entries, params); | |
11470 | } | |
11471 | ||
c072481d PB |
11472 | static __cold int io_probe(struct io_ring_ctx *ctx, void __user *arg, |
11473 | unsigned nr_args) | |
66f4af93 JA |
11474 | { |
11475 | struct io_uring_probe *p; | |
11476 | size_t size; | |
11477 | int i, ret; | |
11478 | ||
11479 | size = struct_size(p, ops, nr_args); | |
11480 | if (size == SIZE_MAX) | |
11481 | return -EOVERFLOW; | |
11482 | p = kzalloc(size, GFP_KERNEL); | |
11483 | if (!p) | |
11484 | return -ENOMEM; | |
11485 | ||
11486 | ret = -EFAULT; | |
11487 | if (copy_from_user(p, arg, size)) | |
11488 | goto out; | |
11489 | ret = -EINVAL; | |
11490 | if (memchr_inv(p, 0, size)) | |
11491 | goto out; | |
11492 | ||
11493 | p->last_op = IORING_OP_LAST - 1; | |
11494 | if (nr_args > IORING_OP_LAST) | |
11495 | nr_args = IORING_OP_LAST; | |
11496 | ||
11497 | for (i = 0; i < nr_args; i++) { | |
11498 | p->ops[i].op = i; | |
11499 | if (!io_op_defs[i].not_supported) | |
11500 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
11501 | } | |
11502 | p->ops_len = i; | |
11503 | ||
11504 | ret = 0; | |
11505 | if (copy_to_user(arg, p, size)) | |
11506 | ret = -EFAULT; | |
11507 | out: | |
11508 | kfree(p); | |
11509 | return ret; | |
11510 | } | |
11511 | ||
071698e1 JA |
11512 | static int io_register_personality(struct io_ring_ctx *ctx) |
11513 | { | |
4379bf8b | 11514 | const struct cred *creds; |
61cf9370 | 11515 | u32 id; |
1e6fa521 | 11516 | int ret; |
071698e1 | 11517 | |
4379bf8b | 11518 | creds = get_current_cred(); |
1e6fa521 | 11519 | |
61cf9370 MWO |
11520 | ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds, |
11521 | XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL); | |
a30f895a JA |
11522 | if (ret < 0) { |
11523 | put_cred(creds); | |
11524 | return ret; | |
11525 | } | |
11526 | return id; | |
071698e1 JA |
11527 | } |
11528 | ||
c072481d PB |
11529 | static __cold int io_register_restrictions(struct io_ring_ctx *ctx, |
11530 | void __user *arg, unsigned int nr_args) | |
21b55dbc SG |
11531 | { |
11532 | struct io_uring_restriction *res; | |
11533 | size_t size; | |
11534 | int i, ret; | |
11535 | ||
7e84e1c7 SG |
11536 | /* Restrictions allowed only if rings started disabled */ |
11537 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11538 | return -EBADFD; | |
11539 | ||
21b55dbc | 11540 | /* We allow only a single restrictions registration */ |
7e84e1c7 | 11541 | if (ctx->restrictions.registered) |
21b55dbc SG |
11542 | return -EBUSY; |
11543 | ||
11544 | if (!arg || nr_args > IORING_MAX_RESTRICTIONS) | |
11545 | return -EINVAL; | |
11546 | ||
11547 | size = array_size(nr_args, sizeof(*res)); | |
11548 | if (size == SIZE_MAX) | |
11549 | return -EOVERFLOW; | |
11550 | ||
11551 | res = memdup_user(arg, size); | |
11552 | if (IS_ERR(res)) | |
11553 | return PTR_ERR(res); | |
11554 | ||
11555 | ret = 0; | |
11556 | ||
11557 | for (i = 0; i < nr_args; i++) { | |
11558 | switch (res[i].opcode) { | |
11559 | case IORING_RESTRICTION_REGISTER_OP: | |
11560 | if (res[i].register_op >= IORING_REGISTER_LAST) { | |
11561 | ret = -EINVAL; | |
11562 | goto out; | |
11563 | } | |
11564 | ||
11565 | __set_bit(res[i].register_op, | |
11566 | ctx->restrictions.register_op); | |
11567 | break; | |
11568 | case IORING_RESTRICTION_SQE_OP: | |
11569 | if (res[i].sqe_op >= IORING_OP_LAST) { | |
11570 | ret = -EINVAL; | |
11571 | goto out; | |
11572 | } | |
11573 | ||
11574 | __set_bit(res[i].sqe_op, ctx->restrictions.sqe_op); | |
11575 | break; | |
11576 | case IORING_RESTRICTION_SQE_FLAGS_ALLOWED: | |
11577 | ctx->restrictions.sqe_flags_allowed = res[i].sqe_flags; | |
11578 | break; | |
11579 | case IORING_RESTRICTION_SQE_FLAGS_REQUIRED: | |
11580 | ctx->restrictions.sqe_flags_required = res[i].sqe_flags; | |
11581 | break; | |
11582 | default: | |
11583 | ret = -EINVAL; | |
11584 | goto out; | |
11585 | } | |
11586 | } | |
11587 | ||
11588 | out: | |
11589 | /* Reset all restrictions if an error happened */ | |
11590 | if (ret != 0) | |
11591 | memset(&ctx->restrictions, 0, sizeof(ctx->restrictions)); | |
11592 | else | |
7e84e1c7 | 11593 | ctx->restrictions.registered = true; |
21b55dbc SG |
11594 | |
11595 | kfree(res); | |
11596 | return ret; | |
11597 | } | |
11598 | ||
7e84e1c7 SG |
11599 | static int io_register_enable_rings(struct io_ring_ctx *ctx) |
11600 | { | |
11601 | if (!(ctx->flags & IORING_SETUP_R_DISABLED)) | |
11602 | return -EBADFD; | |
11603 | ||
11604 | if (ctx->restrictions.registered) | |
11605 | ctx->restricted = 1; | |
11606 | ||
0298ef96 PB |
11607 | ctx->flags &= ~IORING_SETUP_R_DISABLED; |
11608 | if (ctx->sq_data && wq_has_sleeper(&ctx->sq_data->wait)) | |
11609 | wake_up(&ctx->sq_data->wait); | |
7e84e1c7 SG |
11610 | return 0; |
11611 | } | |
11612 | ||
fdecb662 | 11613 | static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type, |
c3bdad02 | 11614 | struct io_uring_rsrc_update2 *up, |
98f0b3b4 PB |
11615 | unsigned nr_args) |
11616 | { | |
11617 | __u32 tmp; | |
11618 | int err; | |
11619 | ||
c3bdad02 PB |
11620 | if (up->resv) |
11621 | return -EINVAL; | |
98f0b3b4 PB |
11622 | if (check_add_overflow(up->offset, nr_args, &tmp)) |
11623 | return -EOVERFLOW; | |
11624 | err = io_rsrc_node_switch_start(ctx); | |
11625 | if (err) | |
11626 | return err; | |
11627 | ||
fdecb662 PB |
11628 | switch (type) { |
11629 | case IORING_RSRC_FILE: | |
98f0b3b4 | 11630 | return __io_sqe_files_update(ctx, up, nr_args); |
634d00df PB |
11631 | case IORING_RSRC_BUFFER: |
11632 | return __io_sqe_buffers_update(ctx, up, nr_args); | |
98f0b3b4 PB |
11633 | } |
11634 | return -EINVAL; | |
11635 | } | |
11636 | ||
c3bdad02 PB |
11637 | static int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
11638 | unsigned nr_args) | |
98f0b3b4 | 11639 | { |
c3bdad02 | 11640 | struct io_uring_rsrc_update2 up; |
98f0b3b4 PB |
11641 | |
11642 | if (!nr_args) | |
11643 | return -EINVAL; | |
c3bdad02 PB |
11644 | memset(&up, 0, sizeof(up)); |
11645 | if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) | |
11646 | return -EFAULT; | |
11647 | return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); | |
11648 | } | |
11649 | ||
11650 | static int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
992da01a | 11651 | unsigned size, unsigned type) |
c3bdad02 PB |
11652 | { |
11653 | struct io_uring_rsrc_update2 up; | |
11654 | ||
11655 | if (size != sizeof(up)) | |
11656 | return -EINVAL; | |
98f0b3b4 PB |
11657 | if (copy_from_user(&up, arg, sizeof(up))) |
11658 | return -EFAULT; | |
992da01a | 11659 | if (!up.nr || up.resv) |
98f0b3b4 | 11660 | return -EINVAL; |
992da01a | 11661 | return __io_register_rsrc_update(ctx, type, &up, up.nr); |
98f0b3b4 PB |
11662 | } |
11663 | ||
c072481d | 11664 | static __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, |
992da01a | 11665 | unsigned int size, unsigned int type) |
792e3582 PB |
11666 | { |
11667 | struct io_uring_rsrc_register rr; | |
11668 | ||
11669 | /* keep it extendible */ | |
11670 | if (size != sizeof(rr)) | |
11671 | return -EINVAL; | |
11672 | ||
11673 | memset(&rr, 0, sizeof(rr)); | |
11674 | if (copy_from_user(&rr, arg, size)) | |
11675 | return -EFAULT; | |
992da01a | 11676 | if (!rr.nr || rr.resv || rr.resv2) |
792e3582 PB |
11677 | return -EINVAL; |
11678 | ||
992da01a | 11679 | switch (type) { |
792e3582 PB |
11680 | case IORING_RSRC_FILE: |
11681 | return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data), | |
11682 | rr.nr, u64_to_user_ptr(rr.tags)); | |
634d00df PB |
11683 | case IORING_RSRC_BUFFER: |
11684 | return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data), | |
11685 | rr.nr, u64_to_user_ptr(rr.tags)); | |
792e3582 PB |
11686 | } |
11687 | return -EINVAL; | |
11688 | } | |
11689 | ||
c072481d PB |
11690 | static __cold int io_register_iowq_aff(struct io_ring_ctx *ctx, |
11691 | void __user *arg, unsigned len) | |
fe76421d JA |
11692 | { |
11693 | struct io_uring_task *tctx = current->io_uring; | |
11694 | cpumask_var_t new_mask; | |
11695 | int ret; | |
11696 | ||
11697 | if (!tctx || !tctx->io_wq) | |
11698 | return -EINVAL; | |
11699 | ||
11700 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) | |
11701 | return -ENOMEM; | |
11702 | ||
11703 | cpumask_clear(new_mask); | |
11704 | if (len > cpumask_size()) | |
11705 | len = cpumask_size(); | |
11706 | ||
11707 | if (copy_from_user(new_mask, arg, len)) { | |
11708 | free_cpumask_var(new_mask); | |
11709 | return -EFAULT; | |
11710 | } | |
11711 | ||
11712 | ret = io_wq_cpu_affinity(tctx->io_wq, new_mask); | |
11713 | free_cpumask_var(new_mask); | |
11714 | return ret; | |
11715 | } | |
11716 | ||
c072481d | 11717 | static __cold int io_unregister_iowq_aff(struct io_ring_ctx *ctx) |
fe76421d JA |
11718 | { |
11719 | struct io_uring_task *tctx = current->io_uring; | |
11720 | ||
11721 | if (!tctx || !tctx->io_wq) | |
11722 | return -EINVAL; | |
11723 | ||
11724 | return io_wq_cpu_affinity(tctx->io_wq, NULL); | |
11725 | } | |
11726 | ||
c072481d PB |
11727 | static __cold int io_register_iowq_max_workers(struct io_ring_ctx *ctx, |
11728 | void __user *arg) | |
b22fa62a | 11729 | __must_hold(&ctx->uring_lock) |
2e480058 | 11730 | { |
b22fa62a | 11731 | struct io_tctx_node *node; |
fa84693b JA |
11732 | struct io_uring_task *tctx = NULL; |
11733 | struct io_sq_data *sqd = NULL; | |
2e480058 JA |
11734 | __u32 new_count[2]; |
11735 | int i, ret; | |
11736 | ||
2e480058 JA |
11737 | if (copy_from_user(new_count, arg, sizeof(new_count))) |
11738 | return -EFAULT; | |
11739 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11740 | if (new_count[i] > INT_MAX) | |
11741 | return -EINVAL; | |
11742 | ||
fa84693b JA |
11743 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
11744 | sqd = ctx->sq_data; | |
11745 | if (sqd) { | |
009ad9f0 JA |
11746 | /* |
11747 | * Observe the correct sqd->lock -> ctx->uring_lock | |
11748 | * ordering. Fine to drop uring_lock here, we hold | |
11749 | * a ref to the ctx. | |
11750 | */ | |
41d3a6bd | 11751 | refcount_inc(&sqd->refs); |
009ad9f0 | 11752 | mutex_unlock(&ctx->uring_lock); |
fa84693b | 11753 | mutex_lock(&sqd->lock); |
009ad9f0 | 11754 | mutex_lock(&ctx->uring_lock); |
41d3a6bd JA |
11755 | if (sqd->thread) |
11756 | tctx = sqd->thread->io_uring; | |
fa84693b JA |
11757 | } |
11758 | } else { | |
11759 | tctx = current->io_uring; | |
11760 | } | |
11761 | ||
e139a1ec | 11762 | BUILD_BUG_ON(sizeof(new_count) != sizeof(ctx->iowq_limits)); |
fa84693b | 11763 | |
bad119b9 PB |
11764 | for (i = 0; i < ARRAY_SIZE(new_count); i++) |
11765 | if (new_count[i]) | |
11766 | ctx->iowq_limits[i] = new_count[i]; | |
e139a1ec PB |
11767 | ctx->iowq_limits_set = true; |
11768 | ||
e139a1ec PB |
11769 | if (tctx && tctx->io_wq) { |
11770 | ret = io_wq_max_workers(tctx->io_wq, new_count); | |
11771 | if (ret) | |
11772 | goto err; | |
11773 | } else { | |
11774 | memset(new_count, 0, sizeof(new_count)); | |
11775 | } | |
fa84693b | 11776 | |
41d3a6bd | 11777 | if (sqd) { |
fa84693b | 11778 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11779 | io_put_sq_data(sqd); |
11780 | } | |
2e480058 JA |
11781 | |
11782 | if (copy_to_user(arg, new_count, sizeof(new_count))) | |
11783 | return -EFAULT; | |
11784 | ||
b22fa62a PB |
11785 | /* that's it for SQPOLL, only the SQPOLL task creates requests */ |
11786 | if (sqd) | |
11787 | return 0; | |
11788 | ||
11789 | /* now propagate the restriction to all registered users */ | |
11790 | list_for_each_entry(node, &ctx->tctx_list, ctx_node) { | |
11791 | struct io_uring_task *tctx = node->task->io_uring; | |
11792 | ||
11793 | if (WARN_ON_ONCE(!tctx->io_wq)) | |
11794 | continue; | |
11795 | ||
11796 | for (i = 0; i < ARRAY_SIZE(new_count); i++) | |
11797 | new_count[i] = ctx->iowq_limits[i]; | |
11798 | /* ignore errors, it always returns zero anyway */ | |
11799 | (void)io_wq_max_workers(tctx->io_wq, new_count); | |
11800 | } | |
2e480058 | 11801 | return 0; |
fa84693b | 11802 | err: |
41d3a6bd | 11803 | if (sqd) { |
fa84693b | 11804 | mutex_unlock(&sqd->lock); |
41d3a6bd JA |
11805 | io_put_sq_data(sqd); |
11806 | } | |
fa84693b | 11807 | return ret; |
2e480058 JA |
11808 | } |
11809 | ||
edafccee JA |
11810 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
11811 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
11812 | __releases(ctx->uring_lock) |
11813 | __acquires(ctx->uring_lock) | |
edafccee JA |
11814 | { |
11815 | int ret; | |
11816 | ||
35fa71a0 JA |
11817 | /* |
11818 | * We're inside the ring mutex, if the ref is already dying, then | |
11819 | * someone else killed the ctx or is already going through | |
11820 | * io_uring_register(). | |
11821 | */ | |
11822 | if (percpu_ref_is_dying(&ctx->refs)) | |
11823 | return -ENXIO; | |
11824 | ||
75c4021a PB |
11825 | if (ctx->restricted) { |
11826 | if (opcode >= IORING_REGISTER_LAST) | |
11827 | return -EINVAL; | |
11828 | opcode = array_index_nospec(opcode, IORING_REGISTER_LAST); | |
11829 | if (!test_bit(opcode, ctx->restrictions.register_op)) | |
11830 | return -EACCES; | |
11831 | } | |
11832 | ||
edafccee JA |
11833 | switch (opcode) { |
11834 | case IORING_REGISTER_BUFFERS: | |
634d00df | 11835 | ret = io_sqe_buffers_register(ctx, arg, nr_args, NULL); |
edafccee JA |
11836 | break; |
11837 | case IORING_UNREGISTER_BUFFERS: | |
11838 | ret = -EINVAL; | |
11839 | if (arg || nr_args) | |
11840 | break; | |
0a96bbe4 | 11841 | ret = io_sqe_buffers_unregister(ctx); |
edafccee | 11842 | break; |
6b06314c | 11843 | case IORING_REGISTER_FILES: |
792e3582 | 11844 | ret = io_sqe_files_register(ctx, arg, nr_args, NULL); |
6b06314c JA |
11845 | break; |
11846 | case IORING_UNREGISTER_FILES: | |
11847 | ret = -EINVAL; | |
11848 | if (arg || nr_args) | |
11849 | break; | |
11850 | ret = io_sqe_files_unregister(ctx); | |
11851 | break; | |
c3a31e60 | 11852 | case IORING_REGISTER_FILES_UPDATE: |
c3bdad02 | 11853 | ret = io_register_files_update(ctx, arg, nr_args); |
c3a31e60 | 11854 | break; |
9b402849 JA |
11855 | case IORING_REGISTER_EVENTFD: |
11856 | ret = -EINVAL; | |
11857 | if (nr_args != 1) | |
11858 | break; | |
c75312dd UA |
11859 | ret = io_eventfd_register(ctx, arg, 0); |
11860 | break; | |
11861 | case IORING_REGISTER_EVENTFD_ASYNC: | |
11862 | ret = -EINVAL; | |
11863 | if (nr_args != 1) | |
f2842ab5 | 11864 | break; |
c75312dd | 11865 | ret = io_eventfd_register(ctx, arg, 1); |
9b402849 JA |
11866 | break; |
11867 | case IORING_UNREGISTER_EVENTFD: | |
11868 | ret = -EINVAL; | |
11869 | if (arg || nr_args) | |
11870 | break; | |
11871 | ret = io_eventfd_unregister(ctx); | |
11872 | break; | |
66f4af93 JA |
11873 | case IORING_REGISTER_PROBE: |
11874 | ret = -EINVAL; | |
11875 | if (!arg || nr_args > 256) | |
11876 | break; | |
11877 | ret = io_probe(ctx, arg, nr_args); | |
11878 | break; | |
071698e1 JA |
11879 | case IORING_REGISTER_PERSONALITY: |
11880 | ret = -EINVAL; | |
11881 | if (arg || nr_args) | |
11882 | break; | |
11883 | ret = io_register_personality(ctx); | |
11884 | break; | |
11885 | case IORING_UNREGISTER_PERSONALITY: | |
11886 | ret = -EINVAL; | |
11887 | if (arg) | |
11888 | break; | |
11889 | ret = io_unregister_personality(ctx, nr_args); | |
11890 | break; | |
7e84e1c7 SG |
11891 | case IORING_REGISTER_ENABLE_RINGS: |
11892 | ret = -EINVAL; | |
11893 | if (arg || nr_args) | |
11894 | break; | |
11895 | ret = io_register_enable_rings(ctx); | |
11896 | break; | |
21b55dbc SG |
11897 | case IORING_REGISTER_RESTRICTIONS: |
11898 | ret = io_register_restrictions(ctx, arg, nr_args); | |
11899 | break; | |
992da01a PB |
11900 | case IORING_REGISTER_FILES2: |
11901 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_FILE); | |
11902 | break; | |
11903 | case IORING_REGISTER_FILES_UPDATE2: | |
11904 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11905 | IORING_RSRC_FILE); | |
11906 | break; | |
11907 | case IORING_REGISTER_BUFFERS2: | |
11908 | ret = io_register_rsrc(ctx, arg, nr_args, IORING_RSRC_BUFFER); | |
792e3582 | 11909 | break; |
992da01a PB |
11910 | case IORING_REGISTER_BUFFERS_UPDATE: |
11911 | ret = io_register_rsrc_update(ctx, arg, nr_args, | |
11912 | IORING_RSRC_BUFFER); | |
c3bdad02 | 11913 | break; |
fe76421d JA |
11914 | case IORING_REGISTER_IOWQ_AFF: |
11915 | ret = -EINVAL; | |
11916 | if (!arg || !nr_args) | |
11917 | break; | |
11918 | ret = io_register_iowq_aff(ctx, arg, nr_args); | |
11919 | break; | |
11920 | case IORING_UNREGISTER_IOWQ_AFF: | |
11921 | ret = -EINVAL; | |
11922 | if (arg || nr_args) | |
11923 | break; | |
11924 | ret = io_unregister_iowq_aff(ctx); | |
11925 | break; | |
2e480058 JA |
11926 | case IORING_REGISTER_IOWQ_MAX_WORKERS: |
11927 | ret = -EINVAL; | |
11928 | if (!arg || nr_args != 2) | |
11929 | break; | |
11930 | ret = io_register_iowq_max_workers(ctx, arg); | |
11931 | break; | |
e7a6c00d JA |
11932 | case IORING_REGISTER_RING_FDS: |
11933 | ret = io_ringfd_register(ctx, arg, nr_args); | |
11934 | break; | |
11935 | case IORING_UNREGISTER_RING_FDS: | |
11936 | ret = io_ringfd_unregister(ctx, arg, nr_args); | |
11937 | break; | |
edafccee JA |
11938 | default: |
11939 | ret = -EINVAL; | |
11940 | break; | |
11941 | } | |
11942 | ||
edafccee JA |
11943 | return ret; |
11944 | } | |
11945 | ||
11946 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
11947 | void __user *, arg, unsigned int, nr_args) | |
11948 | { | |
11949 | struct io_ring_ctx *ctx; | |
11950 | long ret = -EBADF; | |
11951 | struct fd f; | |
11952 | ||
11953 | f = fdget(fd); | |
11954 | if (!f.file) | |
11955 | return -EBADF; | |
11956 | ||
11957 | ret = -EOPNOTSUPP; | |
11958 | if (f.file->f_op != &io_uring_fops) | |
11959 | goto out_fput; | |
11960 | ||
11961 | ctx = f.file->private_data; | |
11962 | ||
b6c23dd5 PB |
11963 | io_run_task_work(); |
11964 | ||
edafccee JA |
11965 | mutex_lock(&ctx->uring_lock); |
11966 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
11967 | mutex_unlock(&ctx->uring_lock); | |
2757be22 | 11968 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, ret); |
edafccee JA |
11969 | out_fput: |
11970 | fdput(f); | |
11971 | return ret; | |
11972 | } | |
11973 | ||
2b188cc1 JA |
11974 | static int __init io_uring_init(void) |
11975 | { | |
d7f62e82 SM |
11976 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
11977 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
11978 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
11979 | } while (0) | |
11980 | ||
11981 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
11982 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
11983 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
11984 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
11985 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
11986 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
11987 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
11988 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
11989 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
11990 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7d67af2c | 11991 | BUILD_BUG_SQE_ELEM(16, __u64, splice_off_in); |
d7f62e82 SM |
11992 | BUILD_BUG_SQE_ELEM(24, __u32, len); |
11993 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
11994 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
11995 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
11996 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
5769a351 JX |
11997 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u16, poll_events); |
11998 | BUILD_BUG_SQE_ELEM(28, __u32, poll32_events); | |
d7f62e82 SM |
11999 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); |
12000 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
12001 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
12002 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
12003 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
12004 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
12005 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
12006 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7d67af2c | 12007 | BUILD_BUG_SQE_ELEM(28, __u32, splice_flags); |
d7f62e82 SM |
12008 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); |
12009 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
16340eab | 12010 | BUILD_BUG_SQE_ELEM(40, __u16, buf_group); |
d7f62e82 | 12011 | BUILD_BUG_SQE_ELEM(42, __u16, personality); |
7d67af2c | 12012 | BUILD_BUG_SQE_ELEM(44, __s32, splice_fd_in); |
b9445598 | 12013 | BUILD_BUG_SQE_ELEM(44, __u32, file_index); |
d7f62e82 | 12014 | |
b0d658ec PB |
12015 | BUILD_BUG_ON(sizeof(struct io_uring_files_update) != |
12016 | sizeof(struct io_uring_rsrc_update)); | |
12017 | BUILD_BUG_ON(sizeof(struct io_uring_rsrc_update) > | |
12018 | sizeof(struct io_uring_rsrc_update2)); | |
90499ad0 PB |
12019 | |
12020 | /* ->buf_index is u16 */ | |
12021 | BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16)); | |
12022 | ||
b0d658ec PB |
12023 | /* should fit into one byte */ |
12024 | BUILD_BUG_ON(SQE_VALID_FLAGS >= (1 << 8)); | |
68fe256a PB |
12025 | BUILD_BUG_ON(SQE_COMMON_FLAGS >= (1 << 8)); |
12026 | BUILD_BUG_ON((SQE_VALID_FLAGS | SQE_COMMON_FLAGS) != SQE_VALID_FLAGS); | |
b0d658ec | 12027 | |
d3656344 | 12028 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
32c2d33e | 12029 | BUILD_BUG_ON(__REQ_F_LAST_BIT > 8 * sizeof(int)); |
16340eab | 12030 | |
91f245d5 JA |
12031 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC | |
12032 | SLAB_ACCOUNT); | |
2b188cc1 JA |
12033 | return 0; |
12034 | }; | |
12035 | __initcall(io_uring_init); |