]>
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 | |
14 | * through a control-dependency in io_get_cqring (smp_store_release to | |
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> | |
47 | #include <linux/refcount.h> | |
48 | #include <linux/uio.h> | |
6b47ee6e | 49 | #include <linux/bits.h> |
2b188cc1 JA |
50 | |
51 | #include <linux/sched/signal.h> | |
52 | #include <linux/fs.h> | |
53 | #include <linux/file.h> | |
54 | #include <linux/fdtable.h> | |
55 | #include <linux/mm.h> | |
56 | #include <linux/mman.h> | |
57 | #include <linux/mmu_context.h> | |
58 | #include <linux/percpu.h> | |
59 | #include <linux/slab.h> | |
6c271ce2 | 60 | #include <linux/kthread.h> |
2b188cc1 | 61 | #include <linux/blkdev.h> |
edafccee | 62 | #include <linux/bvec.h> |
2b188cc1 JA |
63 | #include <linux/net.h> |
64 | #include <net/sock.h> | |
65 | #include <net/af_unix.h> | |
6b06314c | 66 | #include <net/scm.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> |
2b188cc1 | 78 | |
c826bd7a DD |
79 | #define CREATE_TRACE_POINTS |
80 | #include <trace/events/io_uring.h> | |
81 | ||
2b188cc1 JA |
82 | #include <uapi/linux/io_uring.h> |
83 | ||
84 | #include "internal.h" | |
561fb04a | 85 | #include "io-wq.h" |
2b188cc1 | 86 | |
5277deaa | 87 | #define IORING_MAX_ENTRIES 32768 |
33a107f0 | 88 | #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
65e19f54 JA |
89 | |
90 | /* | |
91 | * Shift of 9 is 512 entries, or exactly one page on 64-bit archs | |
92 | */ | |
93 | #define IORING_FILE_TABLE_SHIFT 9 | |
94 | #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) | |
95 | #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) | |
96 | #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) | |
2b188cc1 JA |
97 | |
98 | struct io_uring { | |
99 | u32 head ____cacheline_aligned_in_smp; | |
100 | u32 tail ____cacheline_aligned_in_smp; | |
101 | }; | |
102 | ||
1e84b97b | 103 | /* |
75b28aff HV |
104 | * This data is shared with the application through the mmap at offsets |
105 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. | |
1e84b97b SB |
106 | * |
107 | * The offsets to the member fields are published through struct | |
108 | * io_sqring_offsets when calling io_uring_setup. | |
109 | */ | |
75b28aff | 110 | struct io_rings { |
1e84b97b SB |
111 | /* |
112 | * Head and tail offsets into the ring; the offsets need to be | |
113 | * masked to get valid indices. | |
114 | * | |
75b28aff HV |
115 | * The kernel controls head of the sq ring and the tail of the cq ring, |
116 | * and the application controls tail of the sq ring and the head of the | |
117 | * cq ring. | |
1e84b97b | 118 | */ |
75b28aff | 119 | struct io_uring sq, cq; |
1e84b97b | 120 | /* |
75b28aff | 121 | * Bitmasks to apply to head and tail offsets (constant, equals |
1e84b97b SB |
122 | * ring_entries - 1) |
123 | */ | |
75b28aff HV |
124 | u32 sq_ring_mask, cq_ring_mask; |
125 | /* Ring sizes (constant, power of 2) */ | |
126 | u32 sq_ring_entries, cq_ring_entries; | |
1e84b97b SB |
127 | /* |
128 | * Number of invalid entries dropped by the kernel due to | |
129 | * invalid index stored in array | |
130 | * | |
131 | * Written by the kernel, shouldn't be modified by the | |
132 | * application (i.e. get number of "new events" by comparing to | |
133 | * cached value). | |
134 | * | |
135 | * After a new SQ head value was read by the application this | |
136 | * counter includes all submissions that were dropped reaching | |
137 | * the new SQ head (and possibly more). | |
138 | */ | |
75b28aff | 139 | u32 sq_dropped; |
1e84b97b SB |
140 | /* |
141 | * Runtime flags | |
142 | * | |
143 | * Written by the kernel, shouldn't be modified by the | |
144 | * application. | |
145 | * | |
146 | * The application needs a full memory barrier before checking | |
147 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. | |
148 | */ | |
75b28aff | 149 | u32 sq_flags; |
1e84b97b SB |
150 | /* |
151 | * Number of completion events lost because the queue was full; | |
152 | * this should be avoided by the application by making sure | |
0b4295b5 | 153 | * there are not more requests pending than there is space in |
1e84b97b SB |
154 | * the completion queue. |
155 | * | |
156 | * Written by the kernel, shouldn't be modified by the | |
157 | * application (i.e. get number of "new events" by comparing to | |
158 | * cached value). | |
159 | * | |
160 | * As completion events come in out of order this counter is not | |
161 | * ordered with any other data. | |
162 | */ | |
75b28aff | 163 | u32 cq_overflow; |
1e84b97b SB |
164 | /* |
165 | * Ring buffer of completion events. | |
166 | * | |
167 | * The kernel writes completion events fresh every time they are | |
168 | * produced, so the application is allowed to modify pending | |
169 | * entries. | |
170 | */ | |
75b28aff | 171 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
2b188cc1 JA |
172 | }; |
173 | ||
edafccee JA |
174 | struct io_mapped_ubuf { |
175 | u64 ubuf; | |
176 | size_t len; | |
177 | struct bio_vec *bvec; | |
178 | unsigned int nr_bvecs; | |
179 | }; | |
180 | ||
65e19f54 JA |
181 | struct fixed_file_table { |
182 | struct file **files; | |
31b51510 JA |
183 | }; |
184 | ||
05f3fb3c JA |
185 | enum { |
186 | FFD_F_ATOMIC, | |
187 | }; | |
188 | ||
189 | struct fixed_file_data { | |
190 | struct fixed_file_table *table; | |
191 | struct io_ring_ctx *ctx; | |
192 | ||
193 | struct percpu_ref refs; | |
194 | struct llist_head put_llist; | |
195 | unsigned long state; | |
196 | struct work_struct ref_work; | |
197 | struct completion done; | |
198 | }; | |
199 | ||
2b188cc1 JA |
200 | struct io_ring_ctx { |
201 | struct { | |
202 | struct percpu_ref refs; | |
203 | } ____cacheline_aligned_in_smp; | |
204 | ||
205 | struct { | |
206 | unsigned int flags; | |
e1d85334 RD |
207 | unsigned int compat: 1; |
208 | unsigned int account_mem: 1; | |
209 | unsigned int cq_overflow_flushed: 1; | |
210 | unsigned int drain_next: 1; | |
211 | unsigned int eventfd_async: 1; | |
2b188cc1 | 212 | |
75b28aff HV |
213 | /* |
214 | * Ring buffer of indices into array of io_uring_sqe, which is | |
215 | * mmapped by the application using the IORING_OFF_SQES offset. | |
216 | * | |
217 | * This indirection could e.g. be used to assign fixed | |
218 | * io_uring_sqe entries to operations and only submit them to | |
219 | * the queue when needed. | |
220 | * | |
221 | * The kernel modifies neither the indices array nor the entries | |
222 | * array. | |
223 | */ | |
224 | u32 *sq_array; | |
2b188cc1 JA |
225 | unsigned cached_sq_head; |
226 | unsigned sq_entries; | |
227 | unsigned sq_mask; | |
6c271ce2 | 228 | unsigned sq_thread_idle; |
498ccd9e | 229 | unsigned cached_sq_dropped; |
206aefde | 230 | atomic_t cached_cq_overflow; |
ad3eb2c8 | 231 | unsigned long sq_check_overflow; |
de0617e4 JA |
232 | |
233 | struct list_head defer_list; | |
5262f567 | 234 | struct list_head timeout_list; |
1d7bb1d5 | 235 | struct list_head cq_overflow_list; |
fcb323cc JA |
236 | |
237 | wait_queue_head_t inflight_wait; | |
ad3eb2c8 | 238 | struct io_uring_sqe *sq_sqes; |
2b188cc1 JA |
239 | } ____cacheline_aligned_in_smp; |
240 | ||
206aefde JA |
241 | struct io_rings *rings; |
242 | ||
2b188cc1 | 243 | /* IO offload */ |
561fb04a | 244 | struct io_wq *io_wq; |
6c271ce2 | 245 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
2b188cc1 | 246 | struct mm_struct *sqo_mm; |
6c271ce2 | 247 | wait_queue_head_t sqo_wait; |
75b28aff | 248 | |
6b06314c JA |
249 | /* |
250 | * If used, fixed file set. Writers must ensure that ->refs is dead, | |
251 | * readers must ensure that ->refs is alive as long as the file* is | |
252 | * used. Only updated through io_uring_register(2). | |
253 | */ | |
05f3fb3c | 254 | struct fixed_file_data *file_data; |
6b06314c | 255 | unsigned nr_user_files; |
b14cca0c PB |
256 | int ring_fd; |
257 | struct file *ring_file; | |
6b06314c | 258 | |
edafccee JA |
259 | /* if used, fixed mapped user buffers */ |
260 | unsigned nr_user_bufs; | |
261 | struct io_mapped_ubuf *user_bufs; | |
262 | ||
2b188cc1 JA |
263 | struct user_struct *user; |
264 | ||
0b8c0ec7 | 265 | const struct cred *creds; |
181e448d | 266 | |
206aefde JA |
267 | /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */ |
268 | struct completion *completions; | |
269 | ||
0ddf92e8 JA |
270 | /* if all else fails... */ |
271 | struct io_kiocb *fallback_req; | |
272 | ||
206aefde JA |
273 | #if defined(CONFIG_UNIX) |
274 | struct socket *ring_sock; | |
275 | #endif | |
276 | ||
071698e1 JA |
277 | struct idr personality_idr; |
278 | ||
206aefde JA |
279 | struct { |
280 | unsigned cached_cq_tail; | |
281 | unsigned cq_entries; | |
282 | unsigned cq_mask; | |
283 | atomic_t cq_timeouts; | |
ad3eb2c8 | 284 | unsigned long cq_check_overflow; |
206aefde JA |
285 | struct wait_queue_head cq_wait; |
286 | struct fasync_struct *cq_fasync; | |
287 | struct eventfd_ctx *cq_ev_fd; | |
288 | } ____cacheline_aligned_in_smp; | |
2b188cc1 JA |
289 | |
290 | struct { | |
291 | struct mutex uring_lock; | |
292 | wait_queue_head_t wait; | |
293 | } ____cacheline_aligned_in_smp; | |
294 | ||
295 | struct { | |
296 | spinlock_t completion_lock; | |
e94f141b JA |
297 | struct llist_head poll_llist; |
298 | ||
def596e9 JA |
299 | /* |
300 | * ->poll_list is protected by the ctx->uring_lock for | |
301 | * io_uring instances that don't use IORING_SETUP_SQPOLL. | |
302 | * For SQPOLL, only the single threaded io_sq_thread() will | |
303 | * manipulate the list, hence no extra locking is needed there. | |
304 | */ | |
305 | struct list_head poll_list; | |
78076bb6 JA |
306 | struct hlist_head *cancel_hash; |
307 | unsigned cancel_hash_bits; | |
e94f141b | 308 | bool poll_multi_file; |
31b51510 | 309 | |
fcb323cc JA |
310 | spinlock_t inflight_lock; |
311 | struct list_head inflight_list; | |
2b188cc1 | 312 | } ____cacheline_aligned_in_smp; |
2b188cc1 JA |
313 | }; |
314 | ||
09bb8394 JA |
315 | /* |
316 | * First field must be the file pointer in all the | |
317 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> | |
318 | */ | |
221c5eb2 JA |
319 | struct io_poll_iocb { |
320 | struct file *file; | |
0969e783 JA |
321 | union { |
322 | struct wait_queue_head *head; | |
323 | u64 addr; | |
324 | }; | |
221c5eb2 | 325 | __poll_t events; |
8c838788 | 326 | bool done; |
221c5eb2 | 327 | bool canceled; |
392edb45 | 328 | struct wait_queue_entry wait; |
221c5eb2 JA |
329 | }; |
330 | ||
b5dba59e JA |
331 | struct io_close { |
332 | struct file *file; | |
333 | struct file *put_file; | |
334 | int fd; | |
335 | }; | |
336 | ||
ad8a48ac JA |
337 | struct io_timeout_data { |
338 | struct io_kiocb *req; | |
339 | struct hrtimer timer; | |
340 | struct timespec64 ts; | |
341 | enum hrtimer_mode mode; | |
cc42e0ac | 342 | u32 seq_offset; |
ad8a48ac JA |
343 | }; |
344 | ||
8ed8d3c3 JA |
345 | struct io_accept { |
346 | struct file *file; | |
347 | struct sockaddr __user *addr; | |
348 | int __user *addr_len; | |
349 | int flags; | |
350 | }; | |
351 | ||
352 | struct io_sync { | |
353 | struct file *file; | |
354 | loff_t len; | |
355 | loff_t off; | |
356 | int flags; | |
d63d1b5e | 357 | int mode; |
8ed8d3c3 JA |
358 | }; |
359 | ||
fbf23849 JA |
360 | struct io_cancel { |
361 | struct file *file; | |
362 | u64 addr; | |
363 | }; | |
364 | ||
b29472ee JA |
365 | struct io_timeout { |
366 | struct file *file; | |
367 | u64 addr; | |
368 | int flags; | |
26a61679 | 369 | unsigned count; |
b29472ee JA |
370 | }; |
371 | ||
9adbd45d JA |
372 | struct io_rw { |
373 | /* NOTE: kiocb has the file as the first member, so don't do it here */ | |
374 | struct kiocb kiocb; | |
375 | u64 addr; | |
376 | u64 len; | |
377 | }; | |
378 | ||
3fbb51c1 JA |
379 | struct io_connect { |
380 | struct file *file; | |
381 | struct sockaddr __user *addr; | |
382 | int addr_len; | |
383 | }; | |
384 | ||
e47293fd JA |
385 | struct io_sr_msg { |
386 | struct file *file; | |
fddaface JA |
387 | union { |
388 | struct user_msghdr __user *msg; | |
389 | void __user *buf; | |
390 | }; | |
e47293fd | 391 | int msg_flags; |
fddaface | 392 | size_t len; |
e47293fd JA |
393 | }; |
394 | ||
15b71abe JA |
395 | struct io_open { |
396 | struct file *file; | |
397 | int dfd; | |
eddc7ef5 | 398 | union { |
eddc7ef5 JA |
399 | unsigned mask; |
400 | }; | |
15b71abe | 401 | struct filename *filename; |
eddc7ef5 | 402 | struct statx __user *buffer; |
c12cedf2 | 403 | struct open_how how; |
15b71abe JA |
404 | }; |
405 | ||
05f3fb3c JA |
406 | struct io_files_update { |
407 | struct file *file; | |
408 | u64 arg; | |
409 | u32 nr_args; | |
410 | u32 offset; | |
411 | }; | |
412 | ||
4840e418 JA |
413 | struct io_fadvise { |
414 | struct file *file; | |
415 | u64 offset; | |
416 | u32 len; | |
417 | u32 advice; | |
418 | }; | |
419 | ||
c1ca757b JA |
420 | struct io_madvise { |
421 | struct file *file; | |
422 | u64 addr; | |
423 | u32 len; | |
424 | u32 advice; | |
425 | }; | |
426 | ||
3e4827b0 JA |
427 | struct io_epoll { |
428 | struct file *file; | |
429 | int epfd; | |
430 | int op; | |
431 | int fd; | |
432 | struct epoll_event event; | |
e47293fd JA |
433 | }; |
434 | ||
f499a021 JA |
435 | struct io_async_connect { |
436 | struct sockaddr_storage address; | |
437 | }; | |
438 | ||
03b1230c JA |
439 | struct io_async_msghdr { |
440 | struct iovec fast_iov[UIO_FASTIOV]; | |
441 | struct iovec *iov; | |
442 | struct sockaddr __user *uaddr; | |
443 | struct msghdr msg; | |
444 | }; | |
445 | ||
f67676d1 JA |
446 | struct io_async_rw { |
447 | struct iovec fast_iov[UIO_FASTIOV]; | |
448 | struct iovec *iov; | |
449 | ssize_t nr_segs; | |
450 | ssize_t size; | |
451 | }; | |
452 | ||
15b71abe JA |
453 | struct io_async_open { |
454 | struct filename *filename; | |
455 | }; | |
456 | ||
1a6b74fc | 457 | struct io_async_ctx { |
f67676d1 JA |
458 | union { |
459 | struct io_async_rw rw; | |
03b1230c | 460 | struct io_async_msghdr msg; |
f499a021 | 461 | struct io_async_connect connect; |
2d28390a | 462 | struct io_timeout_data timeout; |
15b71abe | 463 | struct io_async_open open; |
f67676d1 | 464 | }; |
1a6b74fc JA |
465 | }; |
466 | ||
6b47ee6e PB |
467 | enum { |
468 | REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, | |
469 | REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, | |
470 | REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, | |
471 | REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, | |
472 | REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, | |
473 | ||
474 | REQ_F_LINK_NEXT_BIT, | |
475 | REQ_F_FAIL_LINK_BIT, | |
476 | REQ_F_INFLIGHT_BIT, | |
477 | REQ_F_CUR_POS_BIT, | |
478 | REQ_F_NOWAIT_BIT, | |
479 | REQ_F_IOPOLL_COMPLETED_BIT, | |
480 | REQ_F_LINK_TIMEOUT_BIT, | |
481 | REQ_F_TIMEOUT_BIT, | |
482 | REQ_F_ISREG_BIT, | |
483 | REQ_F_MUST_PUNT_BIT, | |
484 | REQ_F_TIMEOUT_NOSEQ_BIT, | |
485 | REQ_F_COMP_LOCKED_BIT, | |
486 | }; | |
487 | ||
488 | enum { | |
489 | /* ctx owns file */ | |
490 | REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), | |
491 | /* drain existing IO first */ | |
492 | REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), | |
493 | /* linked sqes */ | |
494 | REQ_F_LINK = BIT(REQ_F_LINK_BIT), | |
495 | /* doesn't sever on completion < 0 */ | |
496 | REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), | |
497 | /* IOSQE_ASYNC */ | |
498 | REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), | |
499 | ||
500 | /* already grabbed next link */ | |
501 | REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT), | |
502 | /* fail rest of links */ | |
503 | REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), | |
504 | /* on inflight list */ | |
505 | REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), | |
506 | /* read/write uses file position */ | |
507 | REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), | |
508 | /* must not punt to workers */ | |
509 | REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), | |
510 | /* polled IO has completed */ | |
511 | REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT), | |
512 | /* has linked timeout */ | |
513 | REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), | |
514 | /* timeout request */ | |
515 | REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT), | |
516 | /* regular file */ | |
517 | REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), | |
518 | /* must be punted even for NONBLOCK */ | |
519 | REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT), | |
520 | /* no timeout sequence */ | |
521 | REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT), | |
522 | /* completion under lock */ | |
523 | REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT), | |
524 | }; | |
525 | ||
09bb8394 JA |
526 | /* |
527 | * NOTE! Each of the iocb union members has the file pointer | |
528 | * as the first entry in their struct definition. So you can | |
529 | * access the file pointer through any of the sub-structs, | |
530 | * or directly as just 'ki_filp' in this struct. | |
531 | */ | |
2b188cc1 | 532 | struct io_kiocb { |
221c5eb2 | 533 | union { |
09bb8394 | 534 | struct file *file; |
9adbd45d | 535 | struct io_rw rw; |
221c5eb2 | 536 | struct io_poll_iocb poll; |
8ed8d3c3 JA |
537 | struct io_accept accept; |
538 | struct io_sync sync; | |
fbf23849 | 539 | struct io_cancel cancel; |
b29472ee | 540 | struct io_timeout timeout; |
3fbb51c1 | 541 | struct io_connect connect; |
e47293fd | 542 | struct io_sr_msg sr_msg; |
15b71abe | 543 | struct io_open open; |
b5dba59e | 544 | struct io_close close; |
05f3fb3c | 545 | struct io_files_update files_update; |
4840e418 | 546 | struct io_fadvise fadvise; |
c1ca757b | 547 | struct io_madvise madvise; |
3e4827b0 | 548 | struct io_epoll epoll; |
221c5eb2 | 549 | }; |
2b188cc1 | 550 | |
1a6b74fc | 551 | struct io_async_ctx *io; |
b14cca0c PB |
552 | /* |
553 | * llist_node is only used for poll deferred completions | |
554 | */ | |
555 | struct llist_node llist_node; | |
cf6fd4bd PB |
556 | bool in_async; |
557 | bool needs_fixed_file; | |
d625c6ee | 558 | u8 opcode; |
2b188cc1 JA |
559 | |
560 | struct io_ring_ctx *ctx; | |
eac406c6 JA |
561 | union { |
562 | struct list_head list; | |
78076bb6 | 563 | struct hlist_node hash_node; |
eac406c6 | 564 | }; |
9e645e11 | 565 | struct list_head link_list; |
2b188cc1 | 566 | unsigned int flags; |
c16361c1 | 567 | refcount_t refs; |
2b188cc1 | 568 | u64 user_data; |
9e645e11 | 569 | u32 result; |
de0617e4 | 570 | u32 sequence; |
2b188cc1 | 571 | |
fcb323cc JA |
572 | struct list_head inflight_entry; |
573 | ||
561fb04a | 574 | struct io_wq_work work; |
2b188cc1 JA |
575 | }; |
576 | ||
577 | #define IO_PLUG_THRESHOLD 2 | |
def596e9 | 578 | #define IO_IOPOLL_BATCH 8 |
2b188cc1 | 579 | |
9a56a232 JA |
580 | struct io_submit_state { |
581 | struct blk_plug plug; | |
582 | ||
2579f913 JA |
583 | /* |
584 | * io_kiocb alloc cache | |
585 | */ | |
586 | void *reqs[IO_IOPOLL_BATCH]; | |
6c8a3134 | 587 | unsigned int free_reqs; |
2579f913 | 588 | |
9a56a232 JA |
589 | /* |
590 | * File reference cache | |
591 | */ | |
592 | struct file *file; | |
593 | unsigned int fd; | |
594 | unsigned int has_refs; | |
595 | unsigned int used_refs; | |
596 | unsigned int ios_left; | |
597 | }; | |
598 | ||
d3656344 JA |
599 | struct io_op_def { |
600 | /* needs req->io allocated for deferral/async */ | |
601 | unsigned async_ctx : 1; | |
602 | /* needs current->mm setup, does mm access */ | |
603 | unsigned needs_mm : 1; | |
604 | /* needs req->file assigned */ | |
605 | unsigned needs_file : 1; | |
606 | /* needs req->file assigned IFF fd is >= 0 */ | |
607 | unsigned fd_non_neg : 1; | |
608 | /* hash wq insertion if file is a regular file */ | |
609 | unsigned hash_reg_file : 1; | |
610 | /* unbound wq insertion if file is a non-regular file */ | |
611 | unsigned unbound_nonreg_file : 1; | |
66f4af93 JA |
612 | /* opcode is not supported by this kernel */ |
613 | unsigned not_supported : 1; | |
f86cd20c JA |
614 | /* needs file table */ |
615 | unsigned file_table : 1; | |
d3656344 JA |
616 | }; |
617 | ||
618 | static const struct io_op_def io_op_defs[] = { | |
0463b6c5 PB |
619 | [IORING_OP_NOP] = {}, |
620 | [IORING_OP_READV] = { | |
d3656344 JA |
621 | .async_ctx = 1, |
622 | .needs_mm = 1, | |
623 | .needs_file = 1, | |
624 | .unbound_nonreg_file = 1, | |
625 | }, | |
0463b6c5 | 626 | [IORING_OP_WRITEV] = { |
d3656344 JA |
627 | .async_ctx = 1, |
628 | .needs_mm = 1, | |
629 | .needs_file = 1, | |
630 | .hash_reg_file = 1, | |
631 | .unbound_nonreg_file = 1, | |
632 | }, | |
0463b6c5 | 633 | [IORING_OP_FSYNC] = { |
d3656344 JA |
634 | .needs_file = 1, |
635 | }, | |
0463b6c5 | 636 | [IORING_OP_READ_FIXED] = { |
d3656344 JA |
637 | .needs_file = 1, |
638 | .unbound_nonreg_file = 1, | |
639 | }, | |
0463b6c5 | 640 | [IORING_OP_WRITE_FIXED] = { |
d3656344 JA |
641 | .needs_file = 1, |
642 | .hash_reg_file = 1, | |
643 | .unbound_nonreg_file = 1, | |
644 | }, | |
0463b6c5 | 645 | [IORING_OP_POLL_ADD] = { |
d3656344 JA |
646 | .needs_file = 1, |
647 | .unbound_nonreg_file = 1, | |
648 | }, | |
0463b6c5 PB |
649 | [IORING_OP_POLL_REMOVE] = {}, |
650 | [IORING_OP_SYNC_FILE_RANGE] = { | |
d3656344 JA |
651 | .needs_file = 1, |
652 | }, | |
0463b6c5 | 653 | [IORING_OP_SENDMSG] = { |
d3656344 JA |
654 | .async_ctx = 1, |
655 | .needs_mm = 1, | |
656 | .needs_file = 1, | |
657 | .unbound_nonreg_file = 1, | |
658 | }, | |
0463b6c5 | 659 | [IORING_OP_RECVMSG] = { |
d3656344 JA |
660 | .async_ctx = 1, |
661 | .needs_mm = 1, | |
662 | .needs_file = 1, | |
663 | .unbound_nonreg_file = 1, | |
664 | }, | |
0463b6c5 | 665 | [IORING_OP_TIMEOUT] = { |
d3656344 JA |
666 | .async_ctx = 1, |
667 | .needs_mm = 1, | |
668 | }, | |
0463b6c5 PB |
669 | [IORING_OP_TIMEOUT_REMOVE] = {}, |
670 | [IORING_OP_ACCEPT] = { | |
d3656344 JA |
671 | .needs_mm = 1, |
672 | .needs_file = 1, | |
673 | .unbound_nonreg_file = 1, | |
f86cd20c | 674 | .file_table = 1, |
d3656344 | 675 | }, |
0463b6c5 PB |
676 | [IORING_OP_ASYNC_CANCEL] = {}, |
677 | [IORING_OP_LINK_TIMEOUT] = { | |
d3656344 JA |
678 | .async_ctx = 1, |
679 | .needs_mm = 1, | |
680 | }, | |
0463b6c5 | 681 | [IORING_OP_CONNECT] = { |
d3656344 JA |
682 | .async_ctx = 1, |
683 | .needs_mm = 1, | |
684 | .needs_file = 1, | |
685 | .unbound_nonreg_file = 1, | |
686 | }, | |
0463b6c5 | 687 | [IORING_OP_FALLOCATE] = { |
d3656344 JA |
688 | .needs_file = 1, |
689 | }, | |
0463b6c5 | 690 | [IORING_OP_OPENAT] = { |
d3656344 JA |
691 | .needs_file = 1, |
692 | .fd_non_neg = 1, | |
f86cd20c | 693 | .file_table = 1, |
d3656344 | 694 | }, |
0463b6c5 | 695 | [IORING_OP_CLOSE] = { |
d3656344 | 696 | .needs_file = 1, |
f86cd20c | 697 | .file_table = 1, |
d3656344 | 698 | }, |
0463b6c5 | 699 | [IORING_OP_FILES_UPDATE] = { |
d3656344 | 700 | .needs_mm = 1, |
f86cd20c | 701 | .file_table = 1, |
d3656344 | 702 | }, |
0463b6c5 | 703 | [IORING_OP_STATX] = { |
d3656344 JA |
704 | .needs_mm = 1, |
705 | .needs_file = 1, | |
706 | .fd_non_neg = 1, | |
707 | }, | |
0463b6c5 | 708 | [IORING_OP_READ] = { |
3a6820f2 JA |
709 | .needs_mm = 1, |
710 | .needs_file = 1, | |
711 | .unbound_nonreg_file = 1, | |
712 | }, | |
0463b6c5 | 713 | [IORING_OP_WRITE] = { |
3a6820f2 JA |
714 | .needs_mm = 1, |
715 | .needs_file = 1, | |
716 | .unbound_nonreg_file = 1, | |
717 | }, | |
0463b6c5 | 718 | [IORING_OP_FADVISE] = { |
4840e418 JA |
719 | .needs_file = 1, |
720 | }, | |
0463b6c5 | 721 | [IORING_OP_MADVISE] = { |
c1ca757b JA |
722 | .needs_mm = 1, |
723 | }, | |
0463b6c5 | 724 | [IORING_OP_SEND] = { |
fddaface JA |
725 | .needs_mm = 1, |
726 | .needs_file = 1, | |
727 | .unbound_nonreg_file = 1, | |
728 | }, | |
0463b6c5 | 729 | [IORING_OP_RECV] = { |
fddaface JA |
730 | .needs_mm = 1, |
731 | .needs_file = 1, | |
732 | .unbound_nonreg_file = 1, | |
733 | }, | |
0463b6c5 | 734 | [IORING_OP_OPENAT2] = { |
cebdb986 JA |
735 | .needs_file = 1, |
736 | .fd_non_neg = 1, | |
f86cd20c | 737 | .file_table = 1, |
cebdb986 | 738 | }, |
3e4827b0 JA |
739 | [IORING_OP_EPOLL_CTL] = { |
740 | .unbound_nonreg_file = 1, | |
741 | .file_table = 1, | |
742 | }, | |
d3656344 JA |
743 | }; |
744 | ||
561fb04a | 745 | static void io_wq_submit_work(struct io_wq_work **workptr); |
78e19bbe | 746 | static void io_cqring_fill_event(struct io_kiocb *req, long res); |
ec9c02ad | 747 | static void io_put_req(struct io_kiocb *req); |
978db57e | 748 | static void __io_double_put_req(struct io_kiocb *req); |
94ae5e77 JA |
749 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
750 | static void io_queue_linked_timeout(struct io_kiocb *req); | |
05f3fb3c JA |
751 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
752 | struct io_uring_files_update *ip, | |
753 | unsigned nr_args); | |
f86cd20c | 754 | static int io_grab_files(struct io_kiocb *req); |
2faf852d | 755 | static void io_ring_file_ref_flush(struct fixed_file_data *data); |
de0617e4 | 756 | |
2b188cc1 JA |
757 | static struct kmem_cache *req_cachep; |
758 | ||
759 | static const struct file_operations io_uring_fops; | |
760 | ||
761 | struct sock *io_uring_get_socket(struct file *file) | |
762 | { | |
763 | #if defined(CONFIG_UNIX) | |
764 | if (file->f_op == &io_uring_fops) { | |
765 | struct io_ring_ctx *ctx = file->private_data; | |
766 | ||
767 | return ctx->ring_sock->sk; | |
768 | } | |
769 | #endif | |
770 | return NULL; | |
771 | } | |
772 | EXPORT_SYMBOL(io_uring_get_socket); | |
773 | ||
774 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) | |
775 | { | |
776 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); | |
777 | ||
206aefde | 778 | complete(&ctx->completions[0]); |
2b188cc1 JA |
779 | } |
780 | ||
781 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) | |
782 | { | |
783 | struct io_ring_ctx *ctx; | |
78076bb6 | 784 | int hash_bits; |
2b188cc1 JA |
785 | |
786 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
787 | if (!ctx) | |
788 | return NULL; | |
789 | ||
0ddf92e8 JA |
790 | ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
791 | if (!ctx->fallback_req) | |
792 | goto err; | |
793 | ||
206aefde JA |
794 | ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL); |
795 | if (!ctx->completions) | |
796 | goto err; | |
797 | ||
78076bb6 JA |
798 | /* |
799 | * Use 5 bits less than the max cq entries, that should give us around | |
800 | * 32 entries per hash list if totally full and uniformly spread. | |
801 | */ | |
802 | hash_bits = ilog2(p->cq_entries); | |
803 | hash_bits -= 5; | |
804 | if (hash_bits <= 0) | |
805 | hash_bits = 1; | |
806 | ctx->cancel_hash_bits = hash_bits; | |
807 | ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), | |
808 | GFP_KERNEL); | |
809 | if (!ctx->cancel_hash) | |
810 | goto err; | |
811 | __hash_init(ctx->cancel_hash, 1U << hash_bits); | |
812 | ||
21482896 | 813 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
206aefde JA |
814 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
815 | goto err; | |
2b188cc1 JA |
816 | |
817 | ctx->flags = p->flags; | |
818 | init_waitqueue_head(&ctx->cq_wait); | |
1d7bb1d5 | 819 | INIT_LIST_HEAD(&ctx->cq_overflow_list); |
206aefde JA |
820 | init_completion(&ctx->completions[0]); |
821 | init_completion(&ctx->completions[1]); | |
071698e1 | 822 | idr_init(&ctx->personality_idr); |
2b188cc1 JA |
823 | mutex_init(&ctx->uring_lock); |
824 | init_waitqueue_head(&ctx->wait); | |
825 | spin_lock_init(&ctx->completion_lock); | |
e94f141b | 826 | init_llist_head(&ctx->poll_llist); |
def596e9 | 827 | INIT_LIST_HEAD(&ctx->poll_list); |
de0617e4 | 828 | INIT_LIST_HEAD(&ctx->defer_list); |
5262f567 | 829 | INIT_LIST_HEAD(&ctx->timeout_list); |
fcb323cc JA |
830 | init_waitqueue_head(&ctx->inflight_wait); |
831 | spin_lock_init(&ctx->inflight_lock); | |
832 | INIT_LIST_HEAD(&ctx->inflight_list); | |
2b188cc1 | 833 | return ctx; |
206aefde | 834 | err: |
0ddf92e8 JA |
835 | if (ctx->fallback_req) |
836 | kmem_cache_free(req_cachep, ctx->fallback_req); | |
206aefde | 837 | kfree(ctx->completions); |
78076bb6 | 838 | kfree(ctx->cancel_hash); |
206aefde JA |
839 | kfree(ctx); |
840 | return NULL; | |
2b188cc1 JA |
841 | } |
842 | ||
9d858b21 | 843 | static inline bool __req_need_defer(struct io_kiocb *req) |
7adf4eaf | 844 | { |
a197f664 JL |
845 | struct io_ring_ctx *ctx = req->ctx; |
846 | ||
498ccd9e JA |
847 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
848 | + atomic_read(&ctx->cached_cq_overflow); | |
7adf4eaf JA |
849 | } |
850 | ||
9d858b21 | 851 | static inline bool req_need_defer(struct io_kiocb *req) |
de0617e4 | 852 | { |
87987898 | 853 | if (unlikely(req->flags & REQ_F_IO_DRAIN)) |
9d858b21 | 854 | return __req_need_defer(req); |
de0617e4 | 855 | |
9d858b21 | 856 | return false; |
de0617e4 JA |
857 | } |
858 | ||
7adf4eaf | 859 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
de0617e4 JA |
860 | { |
861 | struct io_kiocb *req; | |
862 | ||
7adf4eaf | 863 | req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list); |
9d858b21 | 864 | if (req && !req_need_defer(req)) { |
de0617e4 JA |
865 | list_del_init(&req->list); |
866 | return req; | |
867 | } | |
868 | ||
869 | return NULL; | |
870 | } | |
871 | ||
5262f567 JA |
872 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
873 | { | |
7adf4eaf JA |
874 | struct io_kiocb *req; |
875 | ||
876 | req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list); | |
93bd25bb JA |
877 | if (req) { |
878 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) | |
879 | return NULL; | |
fb4b3d3f | 880 | if (!__req_need_defer(req)) { |
93bd25bb JA |
881 | list_del_init(&req->list); |
882 | return req; | |
883 | } | |
7adf4eaf JA |
884 | } |
885 | ||
886 | return NULL; | |
5262f567 JA |
887 | } |
888 | ||
de0617e4 | 889 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
2b188cc1 | 890 | { |
75b28aff | 891 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 892 | |
07910158 PB |
893 | /* order cqe stores with ring update */ |
894 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); | |
2b188cc1 | 895 | |
07910158 PB |
896 | if (wq_has_sleeper(&ctx->cq_wait)) { |
897 | wake_up_interruptible(&ctx->cq_wait); | |
898 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); | |
2b188cc1 JA |
899 | } |
900 | } | |
901 | ||
cccf0ee8 JA |
902 | static inline void io_req_work_grab_env(struct io_kiocb *req, |
903 | const struct io_op_def *def) | |
904 | { | |
905 | if (!req->work.mm && def->needs_mm) { | |
906 | mmgrab(current->mm); | |
907 | req->work.mm = current->mm; | |
2b188cc1 | 908 | } |
cccf0ee8 JA |
909 | if (!req->work.creds) |
910 | req->work.creds = get_current_cred(); | |
2b188cc1 JA |
911 | } |
912 | ||
cccf0ee8 | 913 | static inline void io_req_work_drop_env(struct io_kiocb *req) |
18d9be1a | 914 | { |
cccf0ee8 JA |
915 | if (req->work.mm) { |
916 | mmdrop(req->work.mm); | |
917 | req->work.mm = NULL; | |
918 | } | |
919 | if (req->work.creds) { | |
920 | put_cred(req->work.creds); | |
921 | req->work.creds = NULL; | |
922 | } | |
561fb04a JA |
923 | } |
924 | ||
94ae5e77 JA |
925 | static inline bool io_prep_async_work(struct io_kiocb *req, |
926 | struct io_kiocb **link) | |
18d9be1a | 927 | { |
d3656344 | 928 | const struct io_op_def *def = &io_op_defs[req->opcode]; |
561fb04a | 929 | bool do_hashed = false; |
54a91f3b | 930 | |
d3656344 JA |
931 | if (req->flags & REQ_F_ISREG) { |
932 | if (def->hash_reg_file) | |
3529d8c2 | 933 | do_hashed = true; |
d3656344 JA |
934 | } else { |
935 | if (def->unbound_nonreg_file) | |
3529d8c2 | 936 | req->work.flags |= IO_WQ_WORK_UNBOUND; |
54a91f3b | 937 | } |
cccf0ee8 JA |
938 | |
939 | io_req_work_grab_env(req, def); | |
54a91f3b | 940 | |
94ae5e77 | 941 | *link = io_prep_linked_timeout(req); |
561fb04a JA |
942 | return do_hashed; |
943 | } | |
944 | ||
a197f664 | 945 | static inline void io_queue_async_work(struct io_kiocb *req) |
561fb04a | 946 | { |
a197f664 | 947 | struct io_ring_ctx *ctx = req->ctx; |
94ae5e77 JA |
948 | struct io_kiocb *link; |
949 | bool do_hashed; | |
950 | ||
951 | do_hashed = io_prep_async_work(req, &link); | |
561fb04a JA |
952 | |
953 | trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work, | |
954 | req->flags); | |
955 | if (!do_hashed) { | |
956 | io_wq_enqueue(ctx->io_wq, &req->work); | |
957 | } else { | |
958 | io_wq_enqueue_hashed(ctx->io_wq, &req->work, | |
959 | file_inode(req->file)); | |
960 | } | |
94ae5e77 JA |
961 | |
962 | if (link) | |
963 | io_queue_linked_timeout(link); | |
18d9be1a JA |
964 | } |
965 | ||
5262f567 JA |
966 | static void io_kill_timeout(struct io_kiocb *req) |
967 | { | |
968 | int ret; | |
969 | ||
2d28390a | 970 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
5262f567 JA |
971 | if (ret != -1) { |
972 | atomic_inc(&req->ctx->cq_timeouts); | |
842f9612 | 973 | list_del_init(&req->list); |
78e19bbe | 974 | io_cqring_fill_event(req, 0); |
ec9c02ad | 975 | io_put_req(req); |
5262f567 JA |
976 | } |
977 | } | |
978 | ||
979 | static void io_kill_timeouts(struct io_ring_ctx *ctx) | |
980 | { | |
981 | struct io_kiocb *req, *tmp; | |
982 | ||
983 | spin_lock_irq(&ctx->completion_lock); | |
984 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) | |
985 | io_kill_timeout(req); | |
986 | spin_unlock_irq(&ctx->completion_lock); | |
987 | } | |
988 | ||
de0617e4 JA |
989 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
990 | { | |
991 | struct io_kiocb *req; | |
992 | ||
5262f567 JA |
993 | while ((req = io_get_timeout_req(ctx)) != NULL) |
994 | io_kill_timeout(req); | |
995 | ||
de0617e4 JA |
996 | __io_commit_cqring(ctx); |
997 | ||
87987898 | 998 | while ((req = io_get_deferred_req(ctx)) != NULL) |
a197f664 | 999 | io_queue_async_work(req); |
de0617e4 JA |
1000 | } |
1001 | ||
2b188cc1 JA |
1002 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
1003 | { | |
75b28aff | 1004 | struct io_rings *rings = ctx->rings; |
2b188cc1 JA |
1005 | unsigned tail; |
1006 | ||
1007 | tail = ctx->cached_cq_tail; | |
115e12e5 SB |
1008 | /* |
1009 | * writes to the cq entry need to come after reading head; the | |
1010 | * control dependency is enough as we're using WRITE_ONCE to | |
1011 | * fill the cq entry | |
1012 | */ | |
75b28aff | 1013 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
2b188cc1 JA |
1014 | return NULL; |
1015 | ||
1016 | ctx->cached_cq_tail++; | |
75b28aff | 1017 | return &rings->cqes[tail & ctx->cq_mask]; |
2b188cc1 JA |
1018 | } |
1019 | ||
f2842ab5 JA |
1020 | static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
1021 | { | |
f0b493e6 JA |
1022 | if (!ctx->cq_ev_fd) |
1023 | return false; | |
f2842ab5 JA |
1024 | if (!ctx->eventfd_async) |
1025 | return true; | |
1026 | return io_wq_current_is_worker() || in_interrupt(); | |
1027 | } | |
1028 | ||
f0b493e6 | 1029 | static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev) |
1d7bb1d5 JA |
1030 | { |
1031 | if (waitqueue_active(&ctx->wait)) | |
1032 | wake_up(&ctx->wait); | |
1033 | if (waitqueue_active(&ctx->sqo_wait)) | |
1034 | wake_up(&ctx->sqo_wait); | |
f0b493e6 | 1035 | if (trigger_ev) |
1d7bb1d5 JA |
1036 | eventfd_signal(ctx->cq_ev_fd, 1); |
1037 | } | |
1038 | ||
f0b493e6 JA |
1039 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
1040 | { | |
1041 | __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx)); | |
1042 | } | |
1043 | ||
c4a2ed72 JA |
1044 | /* Returns true if there are no backlogged entries after the flush */ |
1045 | static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force) | |
1d7bb1d5 JA |
1046 | { |
1047 | struct io_rings *rings = ctx->rings; | |
1048 | struct io_uring_cqe *cqe; | |
1049 | struct io_kiocb *req; | |
1050 | unsigned long flags; | |
1051 | LIST_HEAD(list); | |
1052 | ||
1053 | if (!force) { | |
1054 | if (list_empty_careful(&ctx->cq_overflow_list)) | |
c4a2ed72 | 1055 | return true; |
1d7bb1d5 JA |
1056 | if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
1057 | rings->cq_ring_entries)) | |
c4a2ed72 | 1058 | return false; |
1d7bb1d5 JA |
1059 | } |
1060 | ||
1061 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1062 | ||
1063 | /* if force is set, the ring is going away. always drop after that */ | |
1064 | if (force) | |
69b3e546 | 1065 | ctx->cq_overflow_flushed = 1; |
1d7bb1d5 | 1066 | |
c4a2ed72 | 1067 | cqe = NULL; |
1d7bb1d5 JA |
1068 | while (!list_empty(&ctx->cq_overflow_list)) { |
1069 | cqe = io_get_cqring(ctx); | |
1070 | if (!cqe && !force) | |
1071 | break; | |
1072 | ||
1073 | req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb, | |
1074 | list); | |
1075 | list_move(&req->list, &list); | |
1076 | if (cqe) { | |
1077 | WRITE_ONCE(cqe->user_data, req->user_data); | |
1078 | WRITE_ONCE(cqe->res, req->result); | |
1079 | WRITE_ONCE(cqe->flags, 0); | |
1080 | } else { | |
1081 | WRITE_ONCE(ctx->rings->cq_overflow, | |
1082 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | io_commit_cqring(ctx); | |
ad3eb2c8 JA |
1087 | if (cqe) { |
1088 | clear_bit(0, &ctx->sq_check_overflow); | |
1089 | clear_bit(0, &ctx->cq_check_overflow); | |
1090 | } | |
1d7bb1d5 JA |
1091 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
1092 | io_cqring_ev_posted(ctx); | |
1093 | ||
1094 | while (!list_empty(&list)) { | |
1095 | req = list_first_entry(&list, struct io_kiocb, list); | |
1096 | list_del(&req->list); | |
ec9c02ad | 1097 | io_put_req(req); |
1d7bb1d5 | 1098 | } |
c4a2ed72 JA |
1099 | |
1100 | return cqe != NULL; | |
1d7bb1d5 JA |
1101 | } |
1102 | ||
78e19bbe | 1103 | static void io_cqring_fill_event(struct io_kiocb *req, long res) |
2b188cc1 | 1104 | { |
78e19bbe | 1105 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1106 | struct io_uring_cqe *cqe; |
1107 | ||
78e19bbe | 1108 | trace_io_uring_complete(ctx, req->user_data, res); |
51c3ff62 | 1109 | |
2b188cc1 JA |
1110 | /* |
1111 | * If we can't get a cq entry, userspace overflowed the | |
1112 | * submission (by quite a lot). Increment the overflow count in | |
1113 | * the ring. | |
1114 | */ | |
1115 | cqe = io_get_cqring(ctx); | |
1d7bb1d5 | 1116 | if (likely(cqe)) { |
78e19bbe | 1117 | WRITE_ONCE(cqe->user_data, req->user_data); |
2b188cc1 | 1118 | WRITE_ONCE(cqe->res, res); |
c71ffb67 | 1119 | WRITE_ONCE(cqe->flags, 0); |
1d7bb1d5 | 1120 | } else if (ctx->cq_overflow_flushed) { |
498ccd9e JA |
1121 | WRITE_ONCE(ctx->rings->cq_overflow, |
1122 | atomic_inc_return(&ctx->cached_cq_overflow)); | |
1d7bb1d5 | 1123 | } else { |
ad3eb2c8 JA |
1124 | if (list_empty(&ctx->cq_overflow_list)) { |
1125 | set_bit(0, &ctx->sq_check_overflow); | |
1126 | set_bit(0, &ctx->cq_check_overflow); | |
1127 | } | |
1d7bb1d5 JA |
1128 | refcount_inc(&req->refs); |
1129 | req->result = res; | |
1130 | list_add_tail(&req->list, &ctx->cq_overflow_list); | |
2b188cc1 JA |
1131 | } |
1132 | } | |
1133 | ||
78e19bbe | 1134 | static void io_cqring_add_event(struct io_kiocb *req, long res) |
2b188cc1 | 1135 | { |
78e19bbe | 1136 | struct io_ring_ctx *ctx = req->ctx; |
2b188cc1 JA |
1137 | unsigned long flags; |
1138 | ||
1139 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
78e19bbe | 1140 | io_cqring_fill_event(req, res); |
2b188cc1 JA |
1141 | io_commit_cqring(ctx); |
1142 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1143 | ||
8c838788 | 1144 | io_cqring_ev_posted(ctx); |
2b188cc1 JA |
1145 | } |
1146 | ||
0ddf92e8 JA |
1147 | static inline bool io_is_fallback_req(struct io_kiocb *req) |
1148 | { | |
1149 | return req == (struct io_kiocb *) | |
1150 | ((unsigned long) req->ctx->fallback_req & ~1UL); | |
1151 | } | |
1152 | ||
1153 | static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) | |
1154 | { | |
1155 | struct io_kiocb *req; | |
1156 | ||
1157 | req = ctx->fallback_req; | |
1158 | if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req)) | |
1159 | return req; | |
1160 | ||
1161 | return NULL; | |
1162 | } | |
1163 | ||
2579f913 JA |
1164 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
1165 | struct io_submit_state *state) | |
2b188cc1 | 1166 | { |
fd6fab2c | 1167 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
2b188cc1 JA |
1168 | struct io_kiocb *req; |
1169 | ||
2579f913 | 1170 | if (!state) { |
fd6fab2c | 1171 | req = kmem_cache_alloc(req_cachep, gfp); |
2579f913 | 1172 | if (unlikely(!req)) |
0ddf92e8 | 1173 | goto fallback; |
2579f913 JA |
1174 | } else if (!state->free_reqs) { |
1175 | size_t sz; | |
1176 | int ret; | |
1177 | ||
1178 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); | |
fd6fab2c JA |
1179 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
1180 | ||
1181 | /* | |
1182 | * Bulk alloc is all-or-nothing. If we fail to get a batch, | |
1183 | * retry single alloc to be on the safe side. | |
1184 | */ | |
1185 | if (unlikely(ret <= 0)) { | |
1186 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); | |
1187 | if (!state->reqs[0]) | |
0ddf92e8 | 1188 | goto fallback; |
fd6fab2c JA |
1189 | ret = 1; |
1190 | } | |
2579f913 | 1191 | state->free_reqs = ret - 1; |
6c8a3134 | 1192 | req = state->reqs[ret - 1]; |
2579f913 | 1193 | } else { |
2579f913 | 1194 | state->free_reqs--; |
6c8a3134 | 1195 | req = state->reqs[state->free_reqs]; |
2b188cc1 JA |
1196 | } |
1197 | ||
0ddf92e8 | 1198 | got_it: |
1a6b74fc | 1199 | req->io = NULL; |
60c112b0 | 1200 | req->file = NULL; |
2579f913 JA |
1201 | req->ctx = ctx; |
1202 | req->flags = 0; | |
e65ef56d JA |
1203 | /* one is dropped after submission, the other at completion */ |
1204 | refcount_set(&req->refs, 2); | |
9e645e11 | 1205 | req->result = 0; |
561fb04a | 1206 | INIT_IO_WORK(&req->work, io_wq_submit_work); |
2579f913 | 1207 | return req; |
0ddf92e8 JA |
1208 | fallback: |
1209 | req = io_get_fallback_req(ctx); | |
1210 | if (req) | |
1211 | goto got_it; | |
6805b32e | 1212 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
1213 | return NULL; |
1214 | } | |
1215 | ||
2b85edfc | 1216 | static void __io_req_do_free(struct io_kiocb *req) |
def596e9 | 1217 | { |
2b85edfc PB |
1218 | if (likely(!io_is_fallback_req(req))) |
1219 | kmem_cache_free(req_cachep, req); | |
1220 | else | |
1221 | clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req); | |
1222 | } | |
1223 | ||
c6ca97b3 | 1224 | static void __io_req_aux_free(struct io_kiocb *req) |
2b188cc1 | 1225 | { |
fcb323cc JA |
1226 | struct io_ring_ctx *ctx = req->ctx; |
1227 | ||
96fd84d8 | 1228 | kfree(req->io); |
05f3fb3c JA |
1229 | if (req->file) { |
1230 | if (req->flags & REQ_F_FIXED_FILE) | |
1231 | percpu_ref_put(&ctx->file_data->refs); | |
1232 | else | |
1233 | fput(req->file); | |
def596e9 | 1234 | } |
cccf0ee8 JA |
1235 | |
1236 | io_req_work_drop_env(req); | |
def596e9 JA |
1237 | } |
1238 | ||
9e645e11 | 1239 | static void __io_free_req(struct io_kiocb *req) |
2b188cc1 | 1240 | { |
c6ca97b3 | 1241 | __io_req_aux_free(req); |
fcb323cc | 1242 | |
fcb323cc | 1243 | if (req->flags & REQ_F_INFLIGHT) { |
c6ca97b3 | 1244 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc JA |
1245 | unsigned long flags; |
1246 | ||
1247 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1248 | list_del(&req->inflight_entry); | |
1249 | if (waitqueue_active(&ctx->inflight_wait)) | |
1250 | wake_up(&ctx->inflight_wait); | |
1251 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1252 | } | |
2b85edfc PB |
1253 | |
1254 | percpu_ref_put(&req->ctx->refs); | |
1255 | __io_req_do_free(req); | |
e65ef56d JA |
1256 | } |
1257 | ||
c6ca97b3 JA |
1258 | struct req_batch { |
1259 | void *reqs[IO_IOPOLL_BATCH]; | |
1260 | int to_free; | |
1261 | int need_iter; | |
1262 | }; | |
1263 | ||
1264 | static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb) | |
1265 | { | |
10fef4be JA |
1266 | int fixed_refs = rb->to_free; |
1267 | ||
c6ca97b3 JA |
1268 | if (!rb->to_free) |
1269 | return; | |
1270 | if (rb->need_iter) { | |
1271 | int i, inflight = 0; | |
1272 | unsigned long flags; | |
1273 | ||
10fef4be | 1274 | fixed_refs = 0; |
c6ca97b3 JA |
1275 | for (i = 0; i < rb->to_free; i++) { |
1276 | struct io_kiocb *req = rb->reqs[i]; | |
1277 | ||
10fef4be | 1278 | if (req->flags & REQ_F_FIXED_FILE) { |
c6ca97b3 | 1279 | req->file = NULL; |
10fef4be JA |
1280 | fixed_refs++; |
1281 | } | |
c6ca97b3 JA |
1282 | if (req->flags & REQ_F_INFLIGHT) |
1283 | inflight++; | |
c6ca97b3 JA |
1284 | __io_req_aux_free(req); |
1285 | } | |
1286 | if (!inflight) | |
1287 | goto do_free; | |
1288 | ||
1289 | spin_lock_irqsave(&ctx->inflight_lock, flags); | |
1290 | for (i = 0; i < rb->to_free; i++) { | |
1291 | struct io_kiocb *req = rb->reqs[i]; | |
1292 | ||
10fef4be | 1293 | if (req->flags & REQ_F_INFLIGHT) { |
c6ca97b3 JA |
1294 | list_del(&req->inflight_entry); |
1295 | if (!--inflight) | |
1296 | break; | |
1297 | } | |
1298 | } | |
1299 | spin_unlock_irqrestore(&ctx->inflight_lock, flags); | |
1300 | ||
1301 | if (waitqueue_active(&ctx->inflight_wait)) | |
1302 | wake_up(&ctx->inflight_wait); | |
1303 | } | |
1304 | do_free: | |
1305 | kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); | |
10fef4be JA |
1306 | if (fixed_refs) |
1307 | percpu_ref_put_many(&ctx->file_data->refs, fixed_refs); | |
c6ca97b3 | 1308 | percpu_ref_put_many(&ctx->refs, rb->to_free); |
c6ca97b3 | 1309 | rb->to_free = rb->need_iter = 0; |
e65ef56d JA |
1310 | } |
1311 | ||
a197f664 | 1312 | static bool io_link_cancel_timeout(struct io_kiocb *req) |
2665abfd | 1313 | { |
a197f664 | 1314 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd JA |
1315 | int ret; |
1316 | ||
2d28390a | 1317 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
2665abfd | 1318 | if (ret != -1) { |
78e19bbe | 1319 | io_cqring_fill_event(req, -ECANCELED); |
2665abfd JA |
1320 | io_commit_cqring(ctx); |
1321 | req->flags &= ~REQ_F_LINK; | |
ec9c02ad | 1322 | io_put_req(req); |
2665abfd JA |
1323 | return true; |
1324 | } | |
1325 | ||
1326 | return false; | |
e65ef56d JA |
1327 | } |
1328 | ||
ba816ad6 | 1329 | static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
9e645e11 | 1330 | { |
2665abfd | 1331 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 1332 | bool wake_ev = false; |
9e645e11 | 1333 | |
4d7dd462 JA |
1334 | /* Already got next link */ |
1335 | if (req->flags & REQ_F_LINK_NEXT) | |
1336 | return; | |
1337 | ||
9e645e11 JA |
1338 | /* |
1339 | * The list should never be empty when we are called here. But could | |
1340 | * potentially happen if the chain is messed up, check to be on the | |
1341 | * safe side. | |
1342 | */ | |
4493233e PB |
1343 | while (!list_empty(&req->link_list)) { |
1344 | struct io_kiocb *nxt = list_first_entry(&req->link_list, | |
1345 | struct io_kiocb, link_list); | |
94ae5e77 | 1346 | |
4493233e PB |
1347 | if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) && |
1348 | (nxt->flags & REQ_F_TIMEOUT))) { | |
1349 | list_del_init(&nxt->link_list); | |
94ae5e77 | 1350 | wake_ev |= io_link_cancel_timeout(nxt); |
94ae5e77 JA |
1351 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
1352 | continue; | |
1353 | } | |
9e645e11 | 1354 | |
4493233e PB |
1355 | list_del_init(&req->link_list); |
1356 | if (!list_empty(&nxt->link_list)) | |
1357 | nxt->flags |= REQ_F_LINK; | |
b18fdf71 | 1358 | *nxtptr = nxt; |
94ae5e77 | 1359 | break; |
9e645e11 | 1360 | } |
2665abfd | 1361 | |
4d7dd462 | 1362 | req->flags |= REQ_F_LINK_NEXT; |
2665abfd JA |
1363 | if (wake_ev) |
1364 | io_cqring_ev_posted(ctx); | |
9e645e11 JA |
1365 | } |
1366 | ||
1367 | /* | |
1368 | * Called if REQ_F_LINK is set, and we fail the head request | |
1369 | */ | |
1370 | static void io_fail_links(struct io_kiocb *req) | |
1371 | { | |
2665abfd | 1372 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd JA |
1373 | unsigned long flags; |
1374 | ||
1375 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
9e645e11 JA |
1376 | |
1377 | while (!list_empty(&req->link_list)) { | |
4493233e PB |
1378 | struct io_kiocb *link = list_first_entry(&req->link_list, |
1379 | struct io_kiocb, link_list); | |
9e645e11 | 1380 | |
4493233e | 1381 | list_del_init(&link->link_list); |
c826bd7a | 1382 | trace_io_uring_fail_link(req, link); |
2665abfd JA |
1383 | |
1384 | if ((req->flags & REQ_F_LINK_TIMEOUT) && | |
d625c6ee | 1385 | link->opcode == IORING_OP_LINK_TIMEOUT) { |
a197f664 | 1386 | io_link_cancel_timeout(link); |
2665abfd | 1387 | } else { |
78e19bbe | 1388 | io_cqring_fill_event(link, -ECANCELED); |
978db57e | 1389 | __io_double_put_req(link); |
2665abfd | 1390 | } |
5d960724 | 1391 | req->flags &= ~REQ_F_LINK_TIMEOUT; |
9e645e11 | 1392 | } |
2665abfd JA |
1393 | |
1394 | io_commit_cqring(ctx); | |
1395 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1396 | io_cqring_ev_posted(ctx); | |
9e645e11 JA |
1397 | } |
1398 | ||
4d7dd462 | 1399 | static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt) |
9e645e11 | 1400 | { |
4d7dd462 | 1401 | if (likely(!(req->flags & REQ_F_LINK))) |
2665abfd | 1402 | return; |
2665abfd | 1403 | |
9e645e11 JA |
1404 | /* |
1405 | * If LINK is set, we have dependent requests in this chain. If we | |
1406 | * didn't fail this request, queue the first one up, moving any other | |
1407 | * dependencies to the next request. In case of failure, fail the rest | |
1408 | * of the chain. | |
1409 | */ | |
2665abfd JA |
1410 | if (req->flags & REQ_F_FAIL_LINK) { |
1411 | io_fail_links(req); | |
7c9e7f0f JA |
1412 | } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) == |
1413 | REQ_F_LINK_TIMEOUT) { | |
2665abfd JA |
1414 | struct io_ring_ctx *ctx = req->ctx; |
1415 | unsigned long flags; | |
1416 | ||
1417 | /* | |
1418 | * If this is a timeout link, we could be racing with the | |
1419 | * timeout timer. Grab the completion lock for this case to | |
7c9e7f0f | 1420 | * protect against that. |
2665abfd JA |
1421 | */ |
1422 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
1423 | io_req_link_next(req, nxt); | |
1424 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
1425 | } else { | |
1426 | io_req_link_next(req, nxt); | |
9e645e11 | 1427 | } |
4d7dd462 | 1428 | } |
9e645e11 | 1429 | |
c69f8dbe JL |
1430 | static void io_free_req(struct io_kiocb *req) |
1431 | { | |
944e58bf PB |
1432 | struct io_kiocb *nxt = NULL; |
1433 | ||
1434 | io_req_find_next(req, &nxt); | |
70cf9f32 | 1435 | __io_free_req(req); |
944e58bf PB |
1436 | |
1437 | if (nxt) | |
1438 | io_queue_async_work(nxt); | |
c69f8dbe JL |
1439 | } |
1440 | ||
ba816ad6 JA |
1441 | /* |
1442 | * Drop reference to request, return next in chain (if there is one) if this | |
1443 | * was the last reference to this request. | |
1444 | */ | |
f9bd67f6 | 1445 | __attribute__((nonnull)) |
ec9c02ad | 1446 | static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr) |
e65ef56d | 1447 | { |
f9bd67f6 | 1448 | io_req_find_next(req, nxtptr); |
4d7dd462 | 1449 | |
e65ef56d | 1450 | if (refcount_dec_and_test(&req->refs)) |
4d7dd462 | 1451 | __io_free_req(req); |
2b188cc1 JA |
1452 | } |
1453 | ||
e65ef56d JA |
1454 | static void io_put_req(struct io_kiocb *req) |
1455 | { | |
1456 | if (refcount_dec_and_test(&req->refs)) | |
1457 | io_free_req(req); | |
2b188cc1 JA |
1458 | } |
1459 | ||
978db57e JA |
1460 | /* |
1461 | * Must only be used if we don't need to care about links, usually from | |
1462 | * within the completion handling itself. | |
1463 | */ | |
1464 | static void __io_double_put_req(struct io_kiocb *req) | |
78e19bbe JA |
1465 | { |
1466 | /* drop both submit and complete references */ | |
1467 | if (refcount_sub_and_test(2, &req->refs)) | |
1468 | __io_free_req(req); | |
1469 | } | |
1470 | ||
978db57e JA |
1471 | static void io_double_put_req(struct io_kiocb *req) |
1472 | { | |
1473 | /* drop both submit and complete references */ | |
1474 | if (refcount_sub_and_test(2, &req->refs)) | |
1475 | io_free_req(req); | |
1476 | } | |
1477 | ||
1d7bb1d5 | 1478 | static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush) |
a3a0e43f | 1479 | { |
84f97dc2 JA |
1480 | struct io_rings *rings = ctx->rings; |
1481 | ||
ad3eb2c8 JA |
1482 | if (test_bit(0, &ctx->cq_check_overflow)) { |
1483 | /* | |
1484 | * noflush == true is from the waitqueue handler, just ensure | |
1485 | * we wake up the task, and the next invocation will flush the | |
1486 | * entries. We cannot safely to it from here. | |
1487 | */ | |
1488 | if (noflush && !list_empty(&ctx->cq_overflow_list)) | |
1489 | return -1U; | |
1d7bb1d5 | 1490 | |
ad3eb2c8 JA |
1491 | io_cqring_overflow_flush(ctx, false); |
1492 | } | |
1d7bb1d5 | 1493 | |
a3a0e43f JA |
1494 | /* See comment at the top of this file */ |
1495 | smp_rmb(); | |
ad3eb2c8 | 1496 | return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
a3a0e43f JA |
1497 | } |
1498 | ||
fb5ccc98 PB |
1499 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
1500 | { | |
1501 | struct io_rings *rings = ctx->rings; | |
1502 | ||
1503 | /* make sure SQ entry isn't read before tail */ | |
1504 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; | |
1505 | } | |
1506 | ||
8237e045 | 1507 | static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req) |
e94f141b | 1508 | { |
c6ca97b3 JA |
1509 | if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req)) |
1510 | return false; | |
e94f141b | 1511 | |
c6ca97b3 JA |
1512 | if (!(req->flags & REQ_F_FIXED_FILE) || req->io) |
1513 | rb->need_iter++; | |
1514 | ||
1515 | rb->reqs[rb->to_free++] = req; | |
1516 | if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) | |
1517 | io_free_req_many(req->ctx, rb); | |
1518 | return true; | |
e94f141b JA |
1519 | } |
1520 | ||
def596e9 JA |
1521 | /* |
1522 | * Find and free completed poll iocbs | |
1523 | */ | |
1524 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1525 | struct list_head *done) | |
1526 | { | |
8237e045 | 1527 | struct req_batch rb; |
def596e9 | 1528 | struct io_kiocb *req; |
def596e9 | 1529 | |
c6ca97b3 | 1530 | rb.to_free = rb.need_iter = 0; |
def596e9 JA |
1531 | while (!list_empty(done)) { |
1532 | req = list_first_entry(done, struct io_kiocb, list); | |
1533 | list_del(&req->list); | |
1534 | ||
78e19bbe | 1535 | io_cqring_fill_event(req, req->result); |
def596e9 JA |
1536 | (*nr_events)++; |
1537 | ||
8237e045 JA |
1538 | if (refcount_dec_and_test(&req->refs) && |
1539 | !io_req_multi_free(&rb, req)) | |
1540 | io_free_req(req); | |
def596e9 | 1541 | } |
def596e9 | 1542 | |
09bb8394 | 1543 | io_commit_cqring(ctx); |
8237e045 | 1544 | io_free_req_many(ctx, &rb); |
def596e9 JA |
1545 | } |
1546 | ||
1547 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1548 | long min) | |
1549 | { | |
1550 | struct io_kiocb *req, *tmp; | |
1551 | LIST_HEAD(done); | |
1552 | bool spin; | |
1553 | int ret; | |
1554 | ||
1555 | /* | |
1556 | * Only spin for completions if we don't have multiple devices hanging | |
1557 | * off our complete list, and we're under the requested amount. | |
1558 | */ | |
1559 | spin = !ctx->poll_multi_file && *nr_events < min; | |
1560 | ||
1561 | ret = 0; | |
1562 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { | |
9adbd45d | 1563 | struct kiocb *kiocb = &req->rw.kiocb; |
def596e9 JA |
1564 | |
1565 | /* | |
1566 | * Move completed entries to our local list. If we find a | |
1567 | * request that requires polling, break out and complete | |
1568 | * the done list first, if we have entries there. | |
1569 | */ | |
1570 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { | |
1571 | list_move_tail(&req->list, &done); | |
1572 | continue; | |
1573 | } | |
1574 | if (!list_empty(&done)) | |
1575 | break; | |
1576 | ||
1577 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); | |
1578 | if (ret < 0) | |
1579 | break; | |
1580 | ||
1581 | if (ret && spin) | |
1582 | spin = false; | |
1583 | ret = 0; | |
1584 | } | |
1585 | ||
1586 | if (!list_empty(&done)) | |
1587 | io_iopoll_complete(ctx, nr_events, &done); | |
1588 | ||
1589 | return ret; | |
1590 | } | |
1591 | ||
1592 | /* | |
d195a66e | 1593 | * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
def596e9 JA |
1594 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
1595 | * as a non-spinning completion check. | |
1596 | */ | |
1597 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, | |
1598 | long min) | |
1599 | { | |
08f5439f | 1600 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
def596e9 JA |
1601 | int ret; |
1602 | ||
1603 | ret = io_do_iopoll(ctx, nr_events, min); | |
1604 | if (ret < 0) | |
1605 | return ret; | |
1606 | if (!min || *nr_events >= min) | |
1607 | return 0; | |
1608 | } | |
1609 | ||
1610 | return 1; | |
1611 | } | |
1612 | ||
1613 | /* | |
1614 | * We can't just wait for polled events to come to us, we have to actively | |
1615 | * find and complete them. | |
1616 | */ | |
1617 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) | |
1618 | { | |
1619 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) | |
1620 | return; | |
1621 | ||
1622 | mutex_lock(&ctx->uring_lock); | |
1623 | while (!list_empty(&ctx->poll_list)) { | |
1624 | unsigned int nr_events = 0; | |
1625 | ||
1626 | io_iopoll_getevents(ctx, &nr_events, 1); | |
08f5439f JA |
1627 | |
1628 | /* | |
1629 | * Ensure we allow local-to-the-cpu processing to take place, | |
1630 | * in this case we need to ensure that we reap all events. | |
1631 | */ | |
1632 | cond_resched(); | |
def596e9 JA |
1633 | } |
1634 | mutex_unlock(&ctx->uring_lock); | |
1635 | } | |
1636 | ||
2b2ed975 JA |
1637 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
1638 | long min) | |
def596e9 | 1639 | { |
2b2ed975 | 1640 | int iters = 0, ret = 0; |
500f9fba | 1641 | |
def596e9 JA |
1642 | do { |
1643 | int tmin = 0; | |
1644 | ||
a3a0e43f JA |
1645 | /* |
1646 | * Don't enter poll loop if we already have events pending. | |
1647 | * If we do, we can potentially be spinning for commands that | |
1648 | * already triggered a CQE (eg in error). | |
1649 | */ | |
1d7bb1d5 | 1650 | if (io_cqring_events(ctx, false)) |
a3a0e43f JA |
1651 | break; |
1652 | ||
500f9fba JA |
1653 | /* |
1654 | * If a submit got punted to a workqueue, we can have the | |
1655 | * application entering polling for a command before it gets | |
1656 | * issued. That app will hold the uring_lock for the duration | |
1657 | * of the poll right here, so we need to take a breather every | |
1658 | * now and then to ensure that the issue has a chance to add | |
1659 | * the poll to the issued list. Otherwise we can spin here | |
1660 | * forever, while the workqueue is stuck trying to acquire the | |
1661 | * very same mutex. | |
1662 | */ | |
1663 | if (!(++iters & 7)) { | |
1664 | mutex_unlock(&ctx->uring_lock); | |
1665 | mutex_lock(&ctx->uring_lock); | |
1666 | } | |
1667 | ||
def596e9 JA |
1668 | if (*nr_events < min) |
1669 | tmin = min - *nr_events; | |
1670 | ||
1671 | ret = io_iopoll_getevents(ctx, nr_events, tmin); | |
1672 | if (ret <= 0) | |
1673 | break; | |
1674 | ret = 0; | |
1675 | } while (min && !*nr_events && !need_resched()); | |
1676 | ||
2b2ed975 JA |
1677 | return ret; |
1678 | } | |
1679 | ||
1680 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, | |
1681 | long min) | |
1682 | { | |
1683 | int ret; | |
1684 | ||
1685 | /* | |
1686 | * We disallow the app entering submit/complete with polling, but we | |
1687 | * still need to lock the ring to prevent racing with polled issue | |
1688 | * that got punted to a workqueue. | |
1689 | */ | |
1690 | mutex_lock(&ctx->uring_lock); | |
1691 | ret = __io_iopoll_check(ctx, nr_events, min); | |
500f9fba | 1692 | mutex_unlock(&ctx->uring_lock); |
def596e9 JA |
1693 | return ret; |
1694 | } | |
1695 | ||
491381ce | 1696 | static void kiocb_end_write(struct io_kiocb *req) |
2b188cc1 | 1697 | { |
491381ce JA |
1698 | /* |
1699 | * Tell lockdep we inherited freeze protection from submission | |
1700 | * thread. | |
1701 | */ | |
1702 | if (req->flags & REQ_F_ISREG) { | |
1703 | struct inode *inode = file_inode(req->file); | |
2b188cc1 | 1704 | |
491381ce | 1705 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
2b188cc1 | 1706 | } |
491381ce | 1707 | file_end_write(req->file); |
2b188cc1 JA |
1708 | } |
1709 | ||
4e88d6e7 JA |
1710 | static inline void req_set_fail_links(struct io_kiocb *req) |
1711 | { | |
1712 | if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) | |
1713 | req->flags |= REQ_F_FAIL_LINK; | |
1714 | } | |
1715 | ||
ba816ad6 | 1716 | static void io_complete_rw_common(struct kiocb *kiocb, long res) |
2b188cc1 | 1717 | { |
9adbd45d | 1718 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
2b188cc1 | 1719 | |
491381ce JA |
1720 | if (kiocb->ki_flags & IOCB_WRITE) |
1721 | kiocb_end_write(req); | |
2b188cc1 | 1722 | |
4e88d6e7 JA |
1723 | if (res != req->result) |
1724 | req_set_fail_links(req); | |
78e19bbe | 1725 | io_cqring_add_event(req, res); |
ba816ad6 JA |
1726 | } |
1727 | ||
1728 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) | |
1729 | { | |
9adbd45d | 1730 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ba816ad6 JA |
1731 | |
1732 | io_complete_rw_common(kiocb, res); | |
e65ef56d | 1733 | io_put_req(req); |
2b188cc1 JA |
1734 | } |
1735 | ||
ba816ad6 JA |
1736 | static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res) |
1737 | { | |
9adbd45d | 1738 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
ec9c02ad | 1739 | struct io_kiocb *nxt = NULL; |
ba816ad6 JA |
1740 | |
1741 | io_complete_rw_common(kiocb, res); | |
ec9c02ad JL |
1742 | io_put_req_find_next(req, &nxt); |
1743 | ||
1744 | return nxt; | |
2b188cc1 JA |
1745 | } |
1746 | ||
def596e9 JA |
1747 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
1748 | { | |
9adbd45d | 1749 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
def596e9 | 1750 | |
491381ce JA |
1751 | if (kiocb->ki_flags & IOCB_WRITE) |
1752 | kiocb_end_write(req); | |
def596e9 | 1753 | |
4e88d6e7 JA |
1754 | if (res != req->result) |
1755 | req_set_fail_links(req); | |
9e645e11 | 1756 | req->result = res; |
def596e9 JA |
1757 | if (res != -EAGAIN) |
1758 | req->flags |= REQ_F_IOPOLL_COMPLETED; | |
1759 | } | |
1760 | ||
1761 | /* | |
1762 | * After the iocb has been issued, it's safe to be found on the poll list. | |
1763 | * Adding the kiocb to the list AFTER submission ensures that we don't | |
1764 | * find it from a io_iopoll_getevents() thread before the issuer is done | |
1765 | * accessing the kiocb cookie. | |
1766 | */ | |
1767 | static void io_iopoll_req_issued(struct io_kiocb *req) | |
1768 | { | |
1769 | struct io_ring_ctx *ctx = req->ctx; | |
1770 | ||
1771 | /* | |
1772 | * Track whether we have multiple files in our lists. This will impact | |
1773 | * how we do polling eventually, not spinning if we're on potentially | |
1774 | * different devices. | |
1775 | */ | |
1776 | if (list_empty(&ctx->poll_list)) { | |
1777 | ctx->poll_multi_file = false; | |
1778 | } else if (!ctx->poll_multi_file) { | |
1779 | struct io_kiocb *list_req; | |
1780 | ||
1781 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, | |
1782 | list); | |
9adbd45d | 1783 | if (list_req->file != req->file) |
def596e9 JA |
1784 | ctx->poll_multi_file = true; |
1785 | } | |
1786 | ||
1787 | /* | |
1788 | * For fast devices, IO may have already completed. If it has, add | |
1789 | * it to the front so we find it first. | |
1790 | */ | |
1791 | if (req->flags & REQ_F_IOPOLL_COMPLETED) | |
1792 | list_add(&req->list, &ctx->poll_list); | |
1793 | else | |
1794 | list_add_tail(&req->list, &ctx->poll_list); | |
1795 | } | |
1796 | ||
3d6770fb | 1797 | static void io_file_put(struct io_submit_state *state) |
9a56a232 | 1798 | { |
3d6770fb | 1799 | if (state->file) { |
9a56a232 JA |
1800 | int diff = state->has_refs - state->used_refs; |
1801 | ||
1802 | if (diff) | |
1803 | fput_many(state->file, diff); | |
1804 | state->file = NULL; | |
1805 | } | |
1806 | } | |
1807 | ||
1808 | /* | |
1809 | * Get as many references to a file as we have IOs left in this submission, | |
1810 | * assuming most submissions are for one file, or at least that each file | |
1811 | * has more than one submission. | |
1812 | */ | |
1813 | static struct file *io_file_get(struct io_submit_state *state, int fd) | |
1814 | { | |
1815 | if (!state) | |
1816 | return fget(fd); | |
1817 | ||
1818 | if (state->file) { | |
1819 | if (state->fd == fd) { | |
1820 | state->used_refs++; | |
1821 | state->ios_left--; | |
1822 | return state->file; | |
1823 | } | |
3d6770fb | 1824 | io_file_put(state); |
9a56a232 JA |
1825 | } |
1826 | state->file = fget_many(fd, state->ios_left); | |
1827 | if (!state->file) | |
1828 | return NULL; | |
1829 | ||
1830 | state->fd = fd; | |
1831 | state->has_refs = state->ios_left; | |
1832 | state->used_refs = 1; | |
1833 | state->ios_left--; | |
1834 | return state->file; | |
1835 | } | |
1836 | ||
2b188cc1 JA |
1837 | /* |
1838 | * If we tracked the file through the SCM inflight mechanism, we could support | |
1839 | * any file. For now, just ensure that anything potentially problematic is done | |
1840 | * inline. | |
1841 | */ | |
1842 | static bool io_file_supports_async(struct file *file) | |
1843 | { | |
1844 | umode_t mode = file_inode(file)->i_mode; | |
1845 | ||
10d59345 | 1846 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode)) |
2b188cc1 JA |
1847 | return true; |
1848 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) | |
1849 | return true; | |
1850 | ||
1851 | return false; | |
1852 | } | |
1853 | ||
3529d8c2 JA |
1854 | static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
1855 | bool force_nonblock) | |
2b188cc1 | 1856 | { |
def596e9 | 1857 | struct io_ring_ctx *ctx = req->ctx; |
9adbd45d | 1858 | struct kiocb *kiocb = &req->rw.kiocb; |
09bb8394 JA |
1859 | unsigned ioprio; |
1860 | int ret; | |
2b188cc1 | 1861 | |
491381ce JA |
1862 | if (S_ISREG(file_inode(req->file)->i_mode)) |
1863 | req->flags |= REQ_F_ISREG; | |
1864 | ||
2b188cc1 | 1865 | kiocb->ki_pos = READ_ONCE(sqe->off); |
ba04291e JA |
1866 | if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
1867 | req->flags |= REQ_F_CUR_POS; | |
1868 | kiocb->ki_pos = req->file->f_pos; | |
1869 | } | |
2b188cc1 | 1870 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
3e577dcd PB |
1871 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
1872 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); | |
1873 | if (unlikely(ret)) | |
1874 | return ret; | |
2b188cc1 JA |
1875 | |
1876 | ioprio = READ_ONCE(sqe->ioprio); | |
1877 | if (ioprio) { | |
1878 | ret = ioprio_check_cap(ioprio); | |
1879 | if (ret) | |
09bb8394 | 1880 | return ret; |
2b188cc1 JA |
1881 | |
1882 | kiocb->ki_ioprio = ioprio; | |
1883 | } else | |
1884 | kiocb->ki_ioprio = get_current_ioprio(); | |
1885 | ||
8449eeda | 1886 | /* don't allow async punt if RWF_NOWAIT was requested */ |
491381ce JA |
1887 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
1888 | (req->file->f_flags & O_NONBLOCK)) | |
8449eeda SB |
1889 | req->flags |= REQ_F_NOWAIT; |
1890 | ||
1891 | if (force_nonblock) | |
2b188cc1 | 1892 | kiocb->ki_flags |= IOCB_NOWAIT; |
8449eeda | 1893 | |
def596e9 | 1894 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 JA |
1895 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
1896 | !kiocb->ki_filp->f_op->iopoll) | |
09bb8394 | 1897 | return -EOPNOTSUPP; |
2b188cc1 | 1898 | |
def596e9 JA |
1899 | kiocb->ki_flags |= IOCB_HIPRI; |
1900 | kiocb->ki_complete = io_complete_rw_iopoll; | |
6873e0bd | 1901 | req->result = 0; |
def596e9 | 1902 | } else { |
09bb8394 JA |
1903 | if (kiocb->ki_flags & IOCB_HIPRI) |
1904 | return -EINVAL; | |
def596e9 JA |
1905 | kiocb->ki_complete = io_complete_rw; |
1906 | } | |
9adbd45d | 1907 | |
3529d8c2 JA |
1908 | req->rw.addr = READ_ONCE(sqe->addr); |
1909 | req->rw.len = READ_ONCE(sqe->len); | |
9adbd45d JA |
1910 | /* we own ->private, reuse it for the buffer index */ |
1911 | req->rw.kiocb.private = (void *) (unsigned long) | |
3529d8c2 | 1912 | READ_ONCE(sqe->buf_index); |
2b188cc1 | 1913 | return 0; |
2b188cc1 JA |
1914 | } |
1915 | ||
1916 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) | |
1917 | { | |
1918 | switch (ret) { | |
1919 | case -EIOCBQUEUED: | |
1920 | break; | |
1921 | case -ERESTARTSYS: | |
1922 | case -ERESTARTNOINTR: | |
1923 | case -ERESTARTNOHAND: | |
1924 | case -ERESTART_RESTARTBLOCK: | |
1925 | /* | |
1926 | * We can't just restart the syscall, since previously | |
1927 | * submitted sqes may already be in progress. Just fail this | |
1928 | * IO with EINTR. | |
1929 | */ | |
1930 | ret = -EINTR; | |
1931 | /* fall through */ | |
1932 | default: | |
1933 | kiocb->ki_complete(kiocb, ret, 0); | |
1934 | } | |
1935 | } | |
1936 | ||
ba816ad6 JA |
1937 | static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt, |
1938 | bool in_async) | |
1939 | { | |
ba04291e JA |
1940 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
1941 | ||
1942 | if (req->flags & REQ_F_CUR_POS) | |
1943 | req->file->f_pos = kiocb->ki_pos; | |
f9bd67f6 | 1944 | if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw) |
ba816ad6 JA |
1945 | *nxt = __io_complete_rw(kiocb, ret); |
1946 | else | |
1947 | io_rw_done(kiocb, ret); | |
1948 | } | |
1949 | ||
9adbd45d | 1950 | static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
7d009165 | 1951 | struct iov_iter *iter) |
edafccee | 1952 | { |
9adbd45d JA |
1953 | struct io_ring_ctx *ctx = req->ctx; |
1954 | size_t len = req->rw.len; | |
edafccee JA |
1955 | struct io_mapped_ubuf *imu; |
1956 | unsigned index, buf_index; | |
1957 | size_t offset; | |
1958 | u64 buf_addr; | |
1959 | ||
1960 | /* attempt to use fixed buffers without having provided iovecs */ | |
1961 | if (unlikely(!ctx->user_bufs)) | |
1962 | return -EFAULT; | |
1963 | ||
9adbd45d | 1964 | buf_index = (unsigned long) req->rw.kiocb.private; |
edafccee JA |
1965 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
1966 | return -EFAULT; | |
1967 | ||
1968 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); | |
1969 | imu = &ctx->user_bufs[index]; | |
9adbd45d | 1970 | buf_addr = req->rw.addr; |
edafccee JA |
1971 | |
1972 | /* overflow */ | |
1973 | if (buf_addr + len < buf_addr) | |
1974 | return -EFAULT; | |
1975 | /* not inside the mapped region */ | |
1976 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) | |
1977 | return -EFAULT; | |
1978 | ||
1979 | /* | |
1980 | * May not be a start of buffer, set size appropriately | |
1981 | * and advance us to the beginning. | |
1982 | */ | |
1983 | offset = buf_addr - imu->ubuf; | |
1984 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); | |
bd11b3a3 JA |
1985 | |
1986 | if (offset) { | |
1987 | /* | |
1988 | * Don't use iov_iter_advance() here, as it's really slow for | |
1989 | * using the latter parts of a big fixed buffer - it iterates | |
1990 | * over each segment manually. We can cheat a bit here, because | |
1991 | * we know that: | |
1992 | * | |
1993 | * 1) it's a BVEC iter, we set it up | |
1994 | * 2) all bvecs are PAGE_SIZE in size, except potentially the | |
1995 | * first and last bvec | |
1996 | * | |
1997 | * So just find our index, and adjust the iterator afterwards. | |
1998 | * If the offset is within the first bvec (or the whole first | |
1999 | * bvec, just use iov_iter_advance(). This makes it easier | |
2000 | * since we can just skip the first segment, which may not | |
2001 | * be PAGE_SIZE aligned. | |
2002 | */ | |
2003 | const struct bio_vec *bvec = imu->bvec; | |
2004 | ||
2005 | if (offset <= bvec->bv_len) { | |
2006 | iov_iter_advance(iter, offset); | |
2007 | } else { | |
2008 | unsigned long seg_skip; | |
2009 | ||
2010 | /* skip first vec */ | |
2011 | offset -= bvec->bv_len; | |
2012 | seg_skip = 1 + (offset >> PAGE_SHIFT); | |
2013 | ||
2014 | iter->bvec = bvec + seg_skip; | |
2015 | iter->nr_segs -= seg_skip; | |
99c79f66 | 2016 | iter->count -= bvec->bv_len + offset; |
bd11b3a3 | 2017 | iter->iov_offset = offset & ~PAGE_MASK; |
bd11b3a3 JA |
2018 | } |
2019 | } | |
2020 | ||
5e559561 | 2021 | return len; |
edafccee JA |
2022 | } |
2023 | ||
cf6fd4bd PB |
2024 | static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
2025 | struct iovec **iovec, struct iov_iter *iter) | |
2b188cc1 | 2026 | { |
9adbd45d JA |
2027 | void __user *buf = u64_to_user_ptr(req->rw.addr); |
2028 | size_t sqe_len = req->rw.len; | |
edafccee JA |
2029 | u8 opcode; |
2030 | ||
d625c6ee | 2031 | opcode = req->opcode; |
7d009165 | 2032 | if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
edafccee | 2033 | *iovec = NULL; |
9adbd45d | 2034 | return io_import_fixed(req, rw, iter); |
edafccee | 2035 | } |
2b188cc1 | 2036 | |
9adbd45d JA |
2037 | /* buffer index only valid with fixed read/write */ |
2038 | if (req->rw.kiocb.private) | |
2039 | return -EINVAL; | |
2040 | ||
3a6820f2 JA |
2041 | if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
2042 | ssize_t ret; | |
2043 | ret = import_single_range(rw, buf, sqe_len, *iovec, iter); | |
2044 | *iovec = NULL; | |
2045 | return ret; | |
2046 | } | |
2047 | ||
f67676d1 JA |
2048 | if (req->io) { |
2049 | struct io_async_rw *iorw = &req->io->rw; | |
2050 | ||
2051 | *iovec = iorw->iov; | |
2052 | iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size); | |
2053 | if (iorw->iov == iorw->fast_iov) | |
2054 | *iovec = NULL; | |
2055 | return iorw->size; | |
2056 | } | |
2057 | ||
2b188cc1 | 2058 | #ifdef CONFIG_COMPAT |
cf6fd4bd | 2059 | if (req->ctx->compat) |
2b188cc1 JA |
2060 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
2061 | iovec, iter); | |
2062 | #endif | |
2063 | ||
2064 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); | |
2065 | } | |
2066 | ||
31b51510 | 2067 | /* |
32960613 JA |
2068 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
2069 | * by looping over ->read() or ->write() manually. | |
31b51510 | 2070 | */ |
32960613 JA |
2071 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
2072 | struct iov_iter *iter) | |
2073 | { | |
2074 | ssize_t ret = 0; | |
2075 | ||
2076 | /* | |
2077 | * Don't support polled IO through this interface, and we can't | |
2078 | * support non-blocking either. For the latter, this just causes | |
2079 | * the kiocb to be handled from an async context. | |
2080 | */ | |
2081 | if (kiocb->ki_flags & IOCB_HIPRI) | |
2082 | return -EOPNOTSUPP; | |
2083 | if (kiocb->ki_flags & IOCB_NOWAIT) | |
2084 | return -EAGAIN; | |
2085 | ||
2086 | while (iov_iter_count(iter)) { | |
311ae9e1 | 2087 | struct iovec iovec; |
32960613 JA |
2088 | ssize_t nr; |
2089 | ||
311ae9e1 PB |
2090 | if (!iov_iter_is_bvec(iter)) { |
2091 | iovec = iov_iter_iovec(iter); | |
2092 | } else { | |
2093 | /* fixed buffers import bvec */ | |
2094 | iovec.iov_base = kmap(iter->bvec->bv_page) | |
2095 | + iter->iov_offset; | |
2096 | iovec.iov_len = min(iter->count, | |
2097 | iter->bvec->bv_len - iter->iov_offset); | |
2098 | } | |
2099 | ||
32960613 JA |
2100 | if (rw == READ) { |
2101 | nr = file->f_op->read(file, iovec.iov_base, | |
2102 | iovec.iov_len, &kiocb->ki_pos); | |
2103 | } else { | |
2104 | nr = file->f_op->write(file, iovec.iov_base, | |
2105 | iovec.iov_len, &kiocb->ki_pos); | |
2106 | } | |
2107 | ||
311ae9e1 PB |
2108 | if (iov_iter_is_bvec(iter)) |
2109 | kunmap(iter->bvec->bv_page); | |
2110 | ||
32960613 JA |
2111 | if (nr < 0) { |
2112 | if (!ret) | |
2113 | ret = nr; | |
2114 | break; | |
2115 | } | |
2116 | ret += nr; | |
2117 | if (nr != iovec.iov_len) | |
2118 | break; | |
2119 | iov_iter_advance(iter, nr); | |
2120 | } | |
2121 | ||
2122 | return ret; | |
2123 | } | |
2124 | ||
b7bb4f7d | 2125 | static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size, |
f67676d1 JA |
2126 | struct iovec *iovec, struct iovec *fast_iov, |
2127 | struct iov_iter *iter) | |
2128 | { | |
2129 | req->io->rw.nr_segs = iter->nr_segs; | |
2130 | req->io->rw.size = io_size; | |
2131 | req->io->rw.iov = iovec; | |
2132 | if (!req->io->rw.iov) { | |
2133 | req->io->rw.iov = req->io->rw.fast_iov; | |
2134 | memcpy(req->io->rw.iov, fast_iov, | |
2135 | sizeof(struct iovec) * iter->nr_segs); | |
2136 | } | |
2137 | } | |
2138 | ||
b7bb4f7d | 2139 | static int io_alloc_async_ctx(struct io_kiocb *req) |
f67676d1 | 2140 | { |
d3656344 JA |
2141 | if (!io_op_defs[req->opcode].async_ctx) |
2142 | return 0; | |
f67676d1 | 2143 | req->io = kmalloc(sizeof(*req->io), GFP_KERNEL); |
06b76d44 | 2144 | return req->io == NULL; |
b7bb4f7d JA |
2145 | } |
2146 | ||
b7bb4f7d JA |
2147 | static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size, |
2148 | struct iovec *iovec, struct iovec *fast_iov, | |
2149 | struct iov_iter *iter) | |
2150 | { | |
980ad263 | 2151 | if (!io_op_defs[req->opcode].async_ctx) |
74566df3 | 2152 | return 0; |
5d204bcf JA |
2153 | if (!req->io) { |
2154 | if (io_alloc_async_ctx(req)) | |
2155 | return -ENOMEM; | |
b7bb4f7d | 2156 | |
5d204bcf JA |
2157 | io_req_map_rw(req, io_size, iovec, fast_iov, iter); |
2158 | } | |
b7bb4f7d | 2159 | return 0; |
f67676d1 JA |
2160 | } |
2161 | ||
3529d8c2 JA |
2162 | static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2163 | bool force_nonblock) | |
f67676d1 | 2164 | { |
3529d8c2 JA |
2165 | struct io_async_ctx *io; |
2166 | struct iov_iter iter; | |
f67676d1 JA |
2167 | ssize_t ret; |
2168 | ||
3529d8c2 JA |
2169 | ret = io_prep_rw(req, sqe, force_nonblock); |
2170 | if (ret) | |
2171 | return ret; | |
f67676d1 | 2172 | |
3529d8c2 JA |
2173 | if (unlikely(!(req->file->f_mode & FMODE_READ))) |
2174 | return -EBADF; | |
f67676d1 | 2175 | |
3529d8c2 JA |
2176 | if (!req->io) |
2177 | return 0; | |
2178 | ||
2179 | io = req->io; | |
2180 | io->rw.iov = io->rw.fast_iov; | |
2181 | req->io = NULL; | |
2182 | ret = io_import_iovec(READ, req, &io->rw.iov, &iter); | |
2183 | req->io = io; | |
2184 | if (ret < 0) | |
2185 | return ret; | |
2186 | ||
2187 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); | |
2188 | return 0; | |
f67676d1 JA |
2189 | } |
2190 | ||
267bc904 | 2191 | static int io_read(struct io_kiocb *req, struct io_kiocb **nxt, |
8358e3a8 | 2192 | bool force_nonblock) |
2b188cc1 JA |
2193 | { |
2194 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 2195 | struct kiocb *kiocb = &req->rw.kiocb; |
2b188cc1 | 2196 | struct iov_iter iter; |
31b51510 | 2197 | size_t iov_count; |
f67676d1 | 2198 | ssize_t io_size, ret; |
2b188cc1 | 2199 | |
3529d8c2 | 2200 | ret = io_import_iovec(READ, req, &iovec, &iter); |
06b76d44 JA |
2201 | if (ret < 0) |
2202 | return ret; | |
2b188cc1 | 2203 | |
fd6c2e4c JA |
2204 | /* Ensure we clear previously set non-block flag */ |
2205 | if (!force_nonblock) | |
9adbd45d | 2206 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 2207 | |
797f3f53 | 2208 | req->result = 0; |
f67676d1 | 2209 | io_size = ret; |
9e645e11 | 2210 | if (req->flags & REQ_F_LINK) |
f67676d1 JA |
2211 | req->result = io_size; |
2212 | ||
2213 | /* | |
2214 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so | |
2215 | * we know to async punt it even if it was opened O_NONBLOCK | |
2216 | */ | |
9adbd45d | 2217 | if (force_nonblock && !io_file_supports_async(req->file)) { |
f67676d1 JA |
2218 | req->flags |= REQ_F_MUST_PUNT; |
2219 | goto copy_iov; | |
2220 | } | |
9e645e11 | 2221 | |
31b51510 | 2222 | iov_count = iov_iter_count(&iter); |
9adbd45d | 2223 | ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count); |
2b188cc1 JA |
2224 | if (!ret) { |
2225 | ssize_t ret2; | |
2226 | ||
9adbd45d JA |
2227 | if (req->file->f_op->read_iter) |
2228 | ret2 = call_read_iter(req->file, kiocb, &iter); | |
32960613 | 2229 | else |
9adbd45d | 2230 | ret2 = loop_rw_iter(READ, req->file, kiocb, &iter); |
32960613 | 2231 | |
9d93a3f5 | 2232 | /* Catch -EAGAIN return for forced non-blocking submission */ |
f67676d1 | 2233 | if (!force_nonblock || ret2 != -EAGAIN) { |
cf6fd4bd | 2234 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
f67676d1 JA |
2235 | } else { |
2236 | copy_iov: | |
b7bb4f7d | 2237 | ret = io_setup_async_rw(req, io_size, iovec, |
f67676d1 JA |
2238 | inline_vecs, &iter); |
2239 | if (ret) | |
2240 | goto out_free; | |
2241 | return -EAGAIN; | |
2242 | } | |
2b188cc1 | 2243 | } |
f67676d1 | 2244 | out_free: |
1e95081c | 2245 | kfree(iovec); |
2b188cc1 JA |
2246 | return ret; |
2247 | } | |
2248 | ||
3529d8c2 JA |
2249 | static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2250 | bool force_nonblock) | |
f67676d1 | 2251 | { |
3529d8c2 JA |
2252 | struct io_async_ctx *io; |
2253 | struct iov_iter iter; | |
f67676d1 JA |
2254 | ssize_t ret; |
2255 | ||
3529d8c2 JA |
2256 | ret = io_prep_rw(req, sqe, force_nonblock); |
2257 | if (ret) | |
2258 | return ret; | |
f67676d1 | 2259 | |
3529d8c2 JA |
2260 | if (unlikely(!(req->file->f_mode & FMODE_WRITE))) |
2261 | return -EBADF; | |
f67676d1 | 2262 | |
3529d8c2 JA |
2263 | if (!req->io) |
2264 | return 0; | |
2265 | ||
2266 | io = req->io; | |
2267 | io->rw.iov = io->rw.fast_iov; | |
2268 | req->io = NULL; | |
2269 | ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter); | |
2270 | req->io = io; | |
2271 | if (ret < 0) | |
2272 | return ret; | |
2273 | ||
2274 | io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter); | |
2275 | return 0; | |
f67676d1 JA |
2276 | } |
2277 | ||
267bc904 | 2278 | static int io_write(struct io_kiocb *req, struct io_kiocb **nxt, |
8358e3a8 | 2279 | bool force_nonblock) |
2b188cc1 JA |
2280 | { |
2281 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; | |
9adbd45d | 2282 | struct kiocb *kiocb = &req->rw.kiocb; |
2b188cc1 | 2283 | struct iov_iter iter; |
31b51510 | 2284 | size_t iov_count; |
f67676d1 | 2285 | ssize_t ret, io_size; |
2b188cc1 | 2286 | |
3529d8c2 | 2287 | ret = io_import_iovec(WRITE, req, &iovec, &iter); |
06b76d44 JA |
2288 | if (ret < 0) |
2289 | return ret; | |
2b188cc1 | 2290 | |
fd6c2e4c JA |
2291 | /* Ensure we clear previously set non-block flag */ |
2292 | if (!force_nonblock) | |
9adbd45d | 2293 | req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT; |
fd6c2e4c | 2294 | |
797f3f53 | 2295 | req->result = 0; |
f67676d1 | 2296 | io_size = ret; |
9e645e11 | 2297 | if (req->flags & REQ_F_LINK) |
f67676d1 | 2298 | req->result = io_size; |
9e645e11 | 2299 | |
f67676d1 JA |
2300 | /* |
2301 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so | |
2302 | * we know to async punt it even if it was opened O_NONBLOCK | |
2303 | */ | |
2304 | if (force_nonblock && !io_file_supports_async(req->file)) { | |
2305 | req->flags |= REQ_F_MUST_PUNT; | |
2306 | goto copy_iov; | |
2307 | } | |
31b51510 | 2308 | |
10d59345 JA |
2309 | /* file path doesn't support NOWAIT for non-direct_IO */ |
2310 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) && | |
2311 | (req->flags & REQ_F_ISREG)) | |
f67676d1 | 2312 | goto copy_iov; |
31b51510 | 2313 | |
f67676d1 | 2314 | iov_count = iov_iter_count(&iter); |
9adbd45d | 2315 | ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count); |
2b188cc1 | 2316 | if (!ret) { |
9bf7933f RP |
2317 | ssize_t ret2; |
2318 | ||
2b188cc1 JA |
2319 | /* |
2320 | * Open-code file_start_write here to grab freeze protection, | |
2321 | * which will be released by another thread in | |
2322 | * io_complete_rw(). Fool lockdep by telling it the lock got | |
2323 | * released so that it doesn't complain about the held lock when | |
2324 | * we return to userspace. | |
2325 | */ | |
491381ce | 2326 | if (req->flags & REQ_F_ISREG) { |
9adbd45d | 2327 | __sb_start_write(file_inode(req->file)->i_sb, |
2b188cc1 | 2328 | SB_FREEZE_WRITE, true); |
9adbd45d | 2329 | __sb_writers_release(file_inode(req->file)->i_sb, |
2b188cc1 JA |
2330 | SB_FREEZE_WRITE); |
2331 | } | |
2332 | kiocb->ki_flags |= IOCB_WRITE; | |
9bf7933f | 2333 | |
9adbd45d JA |
2334 | if (req->file->f_op->write_iter) |
2335 | ret2 = call_write_iter(req->file, kiocb, &iter); | |
32960613 | 2336 | else |
9adbd45d | 2337 | ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter); |
f67676d1 | 2338 | if (!force_nonblock || ret2 != -EAGAIN) { |
cf6fd4bd | 2339 | kiocb_done(kiocb, ret2, nxt, req->in_async); |
f67676d1 JA |
2340 | } else { |
2341 | copy_iov: | |
b7bb4f7d | 2342 | ret = io_setup_async_rw(req, io_size, iovec, |
f67676d1 JA |
2343 | inline_vecs, &iter); |
2344 | if (ret) | |
2345 | goto out_free; | |
2346 | return -EAGAIN; | |
2347 | } | |
2b188cc1 | 2348 | } |
31b51510 | 2349 | out_free: |
1e95081c | 2350 | kfree(iovec); |
2b188cc1 JA |
2351 | return ret; |
2352 | } | |
2353 | ||
2354 | /* | |
2355 | * IORING_OP_NOP just posts a completion event, nothing else. | |
2356 | */ | |
78e19bbe | 2357 | static int io_nop(struct io_kiocb *req) |
2b188cc1 JA |
2358 | { |
2359 | struct io_ring_ctx *ctx = req->ctx; | |
2b188cc1 | 2360 | |
def596e9 JA |
2361 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
2362 | return -EINVAL; | |
2363 | ||
78e19bbe | 2364 | io_cqring_add_event(req, 0); |
e65ef56d | 2365 | io_put_req(req); |
2b188cc1 JA |
2366 | return 0; |
2367 | } | |
2368 | ||
3529d8c2 | 2369 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
c992fe29 | 2370 | { |
6b06314c | 2371 | struct io_ring_ctx *ctx = req->ctx; |
c992fe29 | 2372 | |
09bb8394 JA |
2373 | if (!req->file) |
2374 | return -EBADF; | |
c992fe29 | 2375 | |
6b06314c | 2376 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
def596e9 | 2377 | return -EINVAL; |
edafccee | 2378 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
c992fe29 CH |
2379 | return -EINVAL; |
2380 | ||
8ed8d3c3 JA |
2381 | req->sync.flags = READ_ONCE(sqe->fsync_flags); |
2382 | if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC)) | |
2383 | return -EINVAL; | |
2384 | ||
2385 | req->sync.off = READ_ONCE(sqe->off); | |
2386 | req->sync.len = READ_ONCE(sqe->len); | |
c992fe29 CH |
2387 | return 0; |
2388 | } | |
2389 | ||
8ed8d3c3 JA |
2390 | static bool io_req_cancelled(struct io_kiocb *req) |
2391 | { | |
2392 | if (req->work.flags & IO_WQ_WORK_CANCEL) { | |
2393 | req_set_fail_links(req); | |
2394 | io_cqring_add_event(req, -ECANCELED); | |
2395 | io_put_req(req); | |
2396 | return true; | |
2397 | } | |
2398 | ||
2399 | return false; | |
2400 | } | |
2401 | ||
78912934 JA |
2402 | static void io_link_work_cb(struct io_wq_work **workptr) |
2403 | { | |
2404 | struct io_wq_work *work = *workptr; | |
2405 | struct io_kiocb *link = work->data; | |
2406 | ||
2407 | io_queue_linked_timeout(link); | |
2408 | work->func = io_wq_submit_work; | |
2409 | } | |
2410 | ||
2411 | static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt) | |
2412 | { | |
2413 | struct io_kiocb *link; | |
2414 | ||
2415 | io_prep_async_work(nxt, &link); | |
2416 | *workptr = &nxt->work; | |
2417 | if (link) { | |
2418 | nxt->work.flags |= IO_WQ_WORK_CB; | |
2419 | nxt->work.func = io_link_work_cb; | |
2420 | nxt->work.data = link; | |
2421 | } | |
2422 | } | |
2423 | ||
8ed8d3c3 JA |
2424 | static void io_fsync_finish(struct io_wq_work **workptr) |
2425 | { | |
2426 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2427 | loff_t end = req->sync.off + req->sync.len; | |
2428 | struct io_kiocb *nxt = NULL; | |
2429 | int ret; | |
2430 | ||
2431 | if (io_req_cancelled(req)) | |
2432 | return; | |
2433 | ||
9adbd45d | 2434 | ret = vfs_fsync_range(req->file, req->sync.off, |
8ed8d3c3 JA |
2435 | end > 0 ? end : LLONG_MAX, |
2436 | req->sync.flags & IORING_FSYNC_DATASYNC); | |
2437 | if (ret < 0) | |
2438 | req_set_fail_links(req); | |
2439 | io_cqring_add_event(req, ret); | |
2440 | io_put_req_find_next(req, &nxt); | |
2441 | if (nxt) | |
78912934 | 2442 | io_wq_assign_next(workptr, nxt); |
8ed8d3c3 JA |
2443 | } |
2444 | ||
fc4df999 JA |
2445 | static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt, |
2446 | bool force_nonblock) | |
c992fe29 | 2447 | { |
8ed8d3c3 | 2448 | struct io_wq_work *work, *old_work; |
c992fe29 CH |
2449 | |
2450 | /* fsync always requires a blocking context */ | |
8ed8d3c3 JA |
2451 | if (force_nonblock) { |
2452 | io_put_req(req); | |
2453 | req->work.func = io_fsync_finish; | |
c992fe29 | 2454 | return -EAGAIN; |
8ed8d3c3 | 2455 | } |
c992fe29 | 2456 | |
8ed8d3c3 JA |
2457 | work = old_work = &req->work; |
2458 | io_fsync_finish(&work); | |
2459 | if (work && work != old_work) | |
2460 | *nxt = container_of(work, struct io_kiocb, work); | |
c992fe29 CH |
2461 | return 0; |
2462 | } | |
2463 | ||
d63d1b5e | 2464 | static void io_fallocate_finish(struct io_wq_work **workptr) |
8ed8d3c3 JA |
2465 | { |
2466 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2467 | struct io_kiocb *nxt = NULL; | |
2468 | int ret; | |
2469 | ||
d63d1b5e JA |
2470 | ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off, |
2471 | req->sync.len); | |
8ed8d3c3 JA |
2472 | if (ret < 0) |
2473 | req_set_fail_links(req); | |
2474 | io_cqring_add_event(req, ret); | |
2475 | io_put_req_find_next(req, &nxt); | |
2476 | if (nxt) | |
78912934 | 2477 | io_wq_assign_next(workptr, nxt); |
5d17b4a4 JA |
2478 | } |
2479 | ||
d63d1b5e JA |
2480 | static int io_fallocate_prep(struct io_kiocb *req, |
2481 | const struct io_uring_sqe *sqe) | |
2482 | { | |
2483 | if (sqe->ioprio || sqe->buf_index || sqe->rw_flags) | |
2484 | return -EINVAL; | |
2485 | ||
2486 | req->sync.off = READ_ONCE(sqe->off); | |
2487 | req->sync.len = READ_ONCE(sqe->addr); | |
2488 | req->sync.mode = READ_ONCE(sqe->len); | |
2489 | return 0; | |
2490 | } | |
2491 | ||
2492 | static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt, | |
2493 | bool force_nonblock) | |
5d17b4a4 | 2494 | { |
8ed8d3c3 | 2495 | struct io_wq_work *work, *old_work; |
5d17b4a4 | 2496 | |
d63d1b5e | 2497 | /* fallocate always requiring blocking context */ |
8ed8d3c3 JA |
2498 | if (force_nonblock) { |
2499 | io_put_req(req); | |
d63d1b5e | 2500 | req->work.func = io_fallocate_finish; |
5d17b4a4 | 2501 | return -EAGAIN; |
8ed8d3c3 | 2502 | } |
5d17b4a4 | 2503 | |
8ed8d3c3 | 2504 | work = old_work = &req->work; |
d63d1b5e | 2505 | io_fallocate_finish(&work); |
8ed8d3c3 JA |
2506 | if (work && work != old_work) |
2507 | *nxt = container_of(work, struct io_kiocb, work); | |
d63d1b5e | 2508 | |
5d17b4a4 JA |
2509 | return 0; |
2510 | } | |
2511 | ||
15b71abe | 2512 | static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
b7bb4f7d | 2513 | { |
f8748881 | 2514 | const char __user *fname; |
15b71abe | 2515 | int ret; |
b7bb4f7d | 2516 | |
15b71abe JA |
2517 | if (sqe->ioprio || sqe->buf_index) |
2518 | return -EINVAL; | |
cf3040ca JA |
2519 | if (sqe->flags & IOSQE_FIXED_FILE) |
2520 | return -EBADF; | |
03b1230c | 2521 | |
15b71abe | 2522 | req->open.dfd = READ_ONCE(sqe->fd); |
c12cedf2 | 2523 | req->open.how.mode = READ_ONCE(sqe->len); |
f8748881 | 2524 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
c12cedf2 | 2525 | req->open.how.flags = READ_ONCE(sqe->open_flags); |
3529d8c2 | 2526 | |
f8748881 | 2527 | req->open.filename = getname(fname); |
15b71abe JA |
2528 | if (IS_ERR(req->open.filename)) { |
2529 | ret = PTR_ERR(req->open.filename); | |
2530 | req->open.filename = NULL; | |
2531 | return ret; | |
2532 | } | |
3529d8c2 | 2533 | |
15b71abe | 2534 | return 0; |
03b1230c JA |
2535 | } |
2536 | ||
cebdb986 | 2537 | static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
aa1fa28f | 2538 | { |
cebdb986 JA |
2539 | struct open_how __user *how; |
2540 | const char __user *fname; | |
2541 | size_t len; | |
0fa03c62 JA |
2542 | int ret; |
2543 | ||
cebdb986 | 2544 | if (sqe->ioprio || sqe->buf_index) |
0fa03c62 | 2545 | return -EINVAL; |
cf3040ca JA |
2546 | if (sqe->flags & IOSQE_FIXED_FILE) |
2547 | return -EBADF; | |
0fa03c62 | 2548 | |
cebdb986 JA |
2549 | req->open.dfd = READ_ONCE(sqe->fd); |
2550 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
2551 | how = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
2552 | len = READ_ONCE(sqe->len); | |
0fa03c62 | 2553 | |
cebdb986 JA |
2554 | if (len < OPEN_HOW_SIZE_VER0) |
2555 | return -EINVAL; | |
3529d8c2 | 2556 | |
cebdb986 JA |
2557 | ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how, |
2558 | len); | |
2559 | if (ret) | |
2560 | return ret; | |
3529d8c2 | 2561 | |
cebdb986 JA |
2562 | if (!(req->open.how.flags & O_PATH) && force_o_largefile()) |
2563 | req->open.how.flags |= O_LARGEFILE; | |
0fa03c62 | 2564 | |
cebdb986 JA |
2565 | req->open.filename = getname(fname); |
2566 | if (IS_ERR(req->open.filename)) { | |
2567 | ret = PTR_ERR(req->open.filename); | |
2568 | req->open.filename = NULL; | |
2569 | return ret; | |
2570 | } | |
2571 | ||
2572 | return 0; | |
2573 | } | |
2574 | ||
2575 | static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt, | |
2576 | bool force_nonblock) | |
15b71abe JA |
2577 | { |
2578 | struct open_flags op; | |
15b71abe JA |
2579 | struct file *file; |
2580 | int ret; | |
2581 | ||
f86cd20c | 2582 | if (force_nonblock) |
15b71abe | 2583 | return -EAGAIN; |
15b71abe | 2584 | |
cebdb986 | 2585 | ret = build_open_flags(&req->open.how, &op); |
15b71abe JA |
2586 | if (ret) |
2587 | goto err; | |
2588 | ||
cebdb986 | 2589 | ret = get_unused_fd_flags(req->open.how.flags); |
15b71abe JA |
2590 | if (ret < 0) |
2591 | goto err; | |
2592 | ||
2593 | file = do_filp_open(req->open.dfd, req->open.filename, &op); | |
2594 | if (IS_ERR(file)) { | |
2595 | put_unused_fd(ret); | |
2596 | ret = PTR_ERR(file); | |
2597 | } else { | |
2598 | fsnotify_open(file); | |
2599 | fd_install(ret, file); | |
2600 | } | |
2601 | err: | |
2602 | putname(req->open.filename); | |
2603 | if (ret < 0) | |
2604 | req_set_fail_links(req); | |
2605 | io_cqring_add_event(req, ret); | |
2606 | io_put_req_find_next(req, nxt); | |
2607 | return 0; | |
2608 | } | |
2609 | ||
cebdb986 JA |
2610 | static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt, |
2611 | bool force_nonblock) | |
2612 | { | |
2613 | req->open.how = build_open_how(req->open.how.flags, req->open.how.mode); | |
2614 | return io_openat2(req, nxt, force_nonblock); | |
2615 | } | |
2616 | ||
3e4827b0 JA |
2617 | static int io_epoll_ctl_prep(struct io_kiocb *req, |
2618 | const struct io_uring_sqe *sqe) | |
2619 | { | |
2620 | #if defined(CONFIG_EPOLL) | |
2621 | if (sqe->ioprio || sqe->buf_index) | |
2622 | return -EINVAL; | |
2623 | ||
2624 | req->epoll.epfd = READ_ONCE(sqe->fd); | |
2625 | req->epoll.op = READ_ONCE(sqe->len); | |
2626 | req->epoll.fd = READ_ONCE(sqe->off); | |
2627 | ||
2628 | if (ep_op_has_event(req->epoll.op)) { | |
2629 | struct epoll_event __user *ev; | |
2630 | ||
2631 | ev = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
2632 | if (copy_from_user(&req->epoll.event, ev, sizeof(*ev))) | |
2633 | return -EFAULT; | |
2634 | } | |
2635 | ||
2636 | return 0; | |
2637 | #else | |
2638 | return -EOPNOTSUPP; | |
2639 | #endif | |
2640 | } | |
2641 | ||
2642 | static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt, | |
2643 | bool force_nonblock) | |
2644 | { | |
2645 | #if defined(CONFIG_EPOLL) | |
2646 | struct io_epoll *ie = &req->epoll; | |
2647 | int ret; | |
2648 | ||
2649 | ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock); | |
2650 | if (force_nonblock && ret == -EAGAIN) | |
2651 | return -EAGAIN; | |
2652 | ||
2653 | if (ret < 0) | |
2654 | req_set_fail_links(req); | |
2655 | io_cqring_add_event(req, ret); | |
2656 | io_put_req_find_next(req, nxt); | |
2657 | return 0; | |
2658 | #else | |
2659 | return -EOPNOTSUPP; | |
2660 | #endif | |
2661 | } | |
2662 | ||
c1ca757b JA |
2663 | static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2664 | { | |
2665 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
2666 | if (sqe->ioprio || sqe->buf_index || sqe->off) | |
2667 | return -EINVAL; | |
2668 | ||
2669 | req->madvise.addr = READ_ONCE(sqe->addr); | |
2670 | req->madvise.len = READ_ONCE(sqe->len); | |
2671 | req->madvise.advice = READ_ONCE(sqe->fadvise_advice); | |
2672 | return 0; | |
2673 | #else | |
2674 | return -EOPNOTSUPP; | |
2675 | #endif | |
2676 | } | |
2677 | ||
2678 | static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt, | |
2679 | bool force_nonblock) | |
2680 | { | |
2681 | #if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU) | |
2682 | struct io_madvise *ma = &req->madvise; | |
2683 | int ret; | |
2684 | ||
2685 | if (force_nonblock) | |
2686 | return -EAGAIN; | |
2687 | ||
2688 | ret = do_madvise(ma->addr, ma->len, ma->advice); | |
2689 | if (ret < 0) | |
2690 | req_set_fail_links(req); | |
2691 | io_cqring_add_event(req, ret); | |
2692 | io_put_req_find_next(req, nxt); | |
2693 | return 0; | |
2694 | #else | |
2695 | return -EOPNOTSUPP; | |
2696 | #endif | |
2697 | } | |
2698 | ||
4840e418 JA |
2699 | static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2700 | { | |
2701 | if (sqe->ioprio || sqe->buf_index || sqe->addr) | |
2702 | return -EINVAL; | |
2703 | ||
2704 | req->fadvise.offset = READ_ONCE(sqe->off); | |
2705 | req->fadvise.len = READ_ONCE(sqe->len); | |
2706 | req->fadvise.advice = READ_ONCE(sqe->fadvise_advice); | |
2707 | return 0; | |
2708 | } | |
2709 | ||
2710 | static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt, | |
2711 | bool force_nonblock) | |
2712 | { | |
2713 | struct io_fadvise *fa = &req->fadvise; | |
2714 | int ret; | |
2715 | ||
3e69426d JA |
2716 | if (force_nonblock) { |
2717 | switch (fa->advice) { | |
2718 | case POSIX_FADV_NORMAL: | |
2719 | case POSIX_FADV_RANDOM: | |
2720 | case POSIX_FADV_SEQUENTIAL: | |
2721 | break; | |
2722 | default: | |
2723 | return -EAGAIN; | |
2724 | } | |
2725 | } | |
4840e418 JA |
2726 | |
2727 | ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice); | |
2728 | if (ret < 0) | |
2729 | req_set_fail_links(req); | |
2730 | io_cqring_add_event(req, ret); | |
2731 | io_put_req_find_next(req, nxt); | |
2732 | return 0; | |
2733 | } | |
2734 | ||
eddc7ef5 JA |
2735 | static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2736 | { | |
f8748881 | 2737 | const char __user *fname; |
eddc7ef5 JA |
2738 | unsigned lookup_flags; |
2739 | int ret; | |
2740 | ||
2741 | if (sqe->ioprio || sqe->buf_index) | |
2742 | return -EINVAL; | |
cf3040ca JA |
2743 | if (sqe->flags & IOSQE_FIXED_FILE) |
2744 | return -EBADF; | |
eddc7ef5 JA |
2745 | |
2746 | req->open.dfd = READ_ONCE(sqe->fd); | |
2747 | req->open.mask = READ_ONCE(sqe->len); | |
f8748881 | 2748 | fname = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
eddc7ef5 | 2749 | req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2)); |
c12cedf2 | 2750 | req->open.how.flags = READ_ONCE(sqe->statx_flags); |
eddc7ef5 | 2751 | |
c12cedf2 | 2752 | if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags)) |
eddc7ef5 JA |
2753 | return -EINVAL; |
2754 | ||
f8748881 | 2755 | req->open.filename = getname_flags(fname, lookup_flags, NULL); |
eddc7ef5 JA |
2756 | if (IS_ERR(req->open.filename)) { |
2757 | ret = PTR_ERR(req->open.filename); | |
2758 | req->open.filename = NULL; | |
2759 | return ret; | |
2760 | } | |
2761 | ||
2762 | return 0; | |
2763 | } | |
2764 | ||
2765 | static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt, | |
2766 | bool force_nonblock) | |
2767 | { | |
2768 | struct io_open *ctx = &req->open; | |
2769 | unsigned lookup_flags; | |
2770 | struct path path; | |
2771 | struct kstat stat; | |
2772 | int ret; | |
2773 | ||
2774 | if (force_nonblock) | |
2775 | return -EAGAIN; | |
2776 | ||
c12cedf2 | 2777 | if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags)) |
eddc7ef5 JA |
2778 | return -EINVAL; |
2779 | ||
2780 | retry: | |
2781 | /* filename_lookup() drops it, keep a reference */ | |
2782 | ctx->filename->refcnt++; | |
2783 | ||
2784 | ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path, | |
2785 | NULL); | |
2786 | if (ret) | |
2787 | goto err; | |
2788 | ||
c12cedf2 | 2789 | ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags); |
eddc7ef5 JA |
2790 | path_put(&path); |
2791 | if (retry_estale(ret, lookup_flags)) { | |
2792 | lookup_flags |= LOOKUP_REVAL; | |
2793 | goto retry; | |
2794 | } | |
2795 | if (!ret) | |
2796 | ret = cp_statx(&stat, ctx->buffer); | |
2797 | err: | |
2798 | putname(ctx->filename); | |
2799 | if (ret < 0) | |
2800 | req_set_fail_links(req); | |
2801 | io_cqring_add_event(req, ret); | |
2802 | io_put_req_find_next(req, nxt); | |
2803 | return 0; | |
2804 | } | |
2805 | ||
b5dba59e JA |
2806 | static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2807 | { | |
2808 | /* | |
2809 | * If we queue this for async, it must not be cancellable. That would | |
2810 | * leave the 'file' in an undeterminate state. | |
2811 | */ | |
2812 | req->work.flags |= IO_WQ_WORK_NO_CANCEL; | |
2813 | ||
2814 | if (sqe->ioprio || sqe->off || sqe->addr || sqe->len || | |
2815 | sqe->rw_flags || sqe->buf_index) | |
2816 | return -EINVAL; | |
2817 | if (sqe->flags & IOSQE_FIXED_FILE) | |
cf3040ca | 2818 | return -EBADF; |
b5dba59e JA |
2819 | |
2820 | req->close.fd = READ_ONCE(sqe->fd); | |
2821 | if (req->file->f_op == &io_uring_fops || | |
b14cca0c | 2822 | req->close.fd == req->ctx->ring_fd) |
b5dba59e JA |
2823 | return -EBADF; |
2824 | ||
2825 | return 0; | |
2826 | } | |
2827 | ||
2828 | static void io_close_finish(struct io_wq_work **workptr) | |
2829 | { | |
2830 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2831 | struct io_kiocb *nxt = NULL; | |
2832 | ||
2833 | /* Invoked with files, we need to do the close */ | |
2834 | if (req->work.files) { | |
2835 | int ret; | |
2836 | ||
2837 | ret = filp_close(req->close.put_file, req->work.files); | |
1a417f4e | 2838 | if (ret < 0) |
b5dba59e | 2839 | req_set_fail_links(req); |
b5dba59e JA |
2840 | io_cqring_add_event(req, ret); |
2841 | } | |
2842 | ||
2843 | fput(req->close.put_file); | |
2844 | ||
b5dba59e JA |
2845 | io_put_req_find_next(req, &nxt); |
2846 | if (nxt) | |
2847 | io_wq_assign_next(workptr, nxt); | |
2848 | } | |
2849 | ||
2850 | static int io_close(struct io_kiocb *req, struct io_kiocb **nxt, | |
2851 | bool force_nonblock) | |
2852 | { | |
2853 | int ret; | |
2854 | ||
2855 | req->close.put_file = NULL; | |
2856 | ret = __close_fd_get_file(req->close.fd, &req->close.put_file); | |
2857 | if (ret < 0) | |
2858 | return ret; | |
2859 | ||
2860 | /* if the file has a flush method, be safe and punt to async */ | |
f86cd20c | 2861 | if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) |
b5dba59e | 2862 | goto eagain; |
b5dba59e JA |
2863 | |
2864 | /* | |
2865 | * No ->flush(), safely close from here and just punt the | |
2866 | * fput() to async context. | |
2867 | */ | |
2868 | ret = filp_close(req->close.put_file, current->files); | |
2869 | ||
2870 | if (ret < 0) | |
2871 | req_set_fail_links(req); | |
2872 | io_cqring_add_event(req, ret); | |
2873 | ||
2874 | if (io_wq_current_is_worker()) { | |
2875 | struct io_wq_work *old_work, *work; | |
2876 | ||
2877 | old_work = work = &req->work; | |
2878 | io_close_finish(&work); | |
2879 | if (work && work != old_work) | |
2880 | *nxt = container_of(work, struct io_kiocb, work); | |
2881 | return 0; | |
2882 | } | |
2883 | ||
2884 | eagain: | |
2885 | req->work.func = io_close_finish; | |
1a417f4e JA |
2886 | /* |
2887 | * Do manual async queue here to avoid grabbing files - we don't | |
2888 | * need the files, and it'll cause io_close_finish() to close | |
2889 | * the file again and cause a double CQE entry for this request | |
2890 | */ | |
2891 | io_queue_async_work(req); | |
2892 | return 0; | |
b5dba59e JA |
2893 | } |
2894 | ||
3529d8c2 | 2895 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
5d17b4a4 JA |
2896 | { |
2897 | struct io_ring_ctx *ctx = req->ctx; | |
5d17b4a4 JA |
2898 | |
2899 | if (!req->file) | |
2900 | return -EBADF; | |
5d17b4a4 JA |
2901 | |
2902 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) | |
2903 | return -EINVAL; | |
2904 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) | |
2905 | return -EINVAL; | |
2906 | ||
8ed8d3c3 JA |
2907 | req->sync.off = READ_ONCE(sqe->off); |
2908 | req->sync.len = READ_ONCE(sqe->len); | |
2909 | req->sync.flags = READ_ONCE(sqe->sync_range_flags); | |
8ed8d3c3 JA |
2910 | return 0; |
2911 | } | |
2912 | ||
2913 | static void io_sync_file_range_finish(struct io_wq_work **workptr) | |
2914 | { | |
2915 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
2916 | struct io_kiocb *nxt = NULL; | |
2917 | int ret; | |
2918 | ||
2919 | if (io_req_cancelled(req)) | |
2920 | return; | |
2921 | ||
9adbd45d | 2922 | ret = sync_file_range(req->file, req->sync.off, req->sync.len, |
8ed8d3c3 JA |
2923 | req->sync.flags); |
2924 | if (ret < 0) | |
2925 | req_set_fail_links(req); | |
2926 | io_cqring_add_event(req, ret); | |
2927 | io_put_req_find_next(req, &nxt); | |
2928 | if (nxt) | |
78912934 | 2929 | io_wq_assign_next(workptr, nxt); |
5d17b4a4 JA |
2930 | } |
2931 | ||
fc4df999 | 2932 | static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt, |
5d17b4a4 JA |
2933 | bool force_nonblock) |
2934 | { | |
8ed8d3c3 | 2935 | struct io_wq_work *work, *old_work; |
5d17b4a4 JA |
2936 | |
2937 | /* sync_file_range always requires a blocking context */ | |
8ed8d3c3 JA |
2938 | if (force_nonblock) { |
2939 | io_put_req(req); | |
2940 | req->work.func = io_sync_file_range_finish; | |
5d17b4a4 | 2941 | return -EAGAIN; |
8ed8d3c3 | 2942 | } |
5d17b4a4 | 2943 | |
8ed8d3c3 JA |
2944 | work = old_work = &req->work; |
2945 | io_sync_file_range_finish(&work); | |
2946 | if (work && work != old_work) | |
2947 | *nxt = container_of(work, struct io_kiocb, work); | |
5d17b4a4 JA |
2948 | return 0; |
2949 | } | |
2950 | ||
3529d8c2 | 2951 | static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
03b1230c | 2952 | { |
0fa03c62 | 2953 | #if defined(CONFIG_NET) |
e47293fd | 2954 | struct io_sr_msg *sr = &req->sr_msg; |
3529d8c2 | 2955 | struct io_async_ctx *io = req->io; |
03b1230c | 2956 | |
e47293fd JA |
2957 | sr->msg_flags = READ_ONCE(sqe->msg_flags); |
2958 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
fddaface | 2959 | sr->len = READ_ONCE(sqe->len); |
3529d8c2 | 2960 | |
fddaface | 2961 | if (!io || req->opcode == IORING_OP_SEND) |
3529d8c2 JA |
2962 | return 0; |
2963 | ||
d9688565 | 2964 | io->msg.iov = io->msg.fast_iov; |
3529d8c2 | 2965 | return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
e47293fd | 2966 | &io->msg.iov); |
03b1230c | 2967 | #else |
e47293fd | 2968 | return -EOPNOTSUPP; |
03b1230c JA |
2969 | #endif |
2970 | } | |
2971 | ||
fc4df999 JA |
2972 | static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
2973 | bool force_nonblock) | |
aa1fa28f | 2974 | { |
03b1230c | 2975 | #if defined(CONFIG_NET) |
0b416c3e | 2976 | struct io_async_msghdr *kmsg = NULL; |
0fa03c62 JA |
2977 | struct socket *sock; |
2978 | int ret; | |
2979 | ||
2980 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
2981 | return -EINVAL; | |
2982 | ||
2983 | sock = sock_from_file(req->file, &ret); | |
2984 | if (sock) { | |
b7bb4f7d | 2985 | struct io_async_ctx io; |
03b1230c | 2986 | struct sockaddr_storage addr; |
0fa03c62 JA |
2987 | unsigned flags; |
2988 | ||
03b1230c | 2989 | if (req->io) { |
0b416c3e JA |
2990 | kmsg = &req->io->msg; |
2991 | kmsg->msg.msg_name = &addr; | |
2992 | /* if iov is set, it's allocated already */ | |
2993 | if (!kmsg->iov) | |
2994 | kmsg->iov = kmsg->fast_iov; | |
2995 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
03b1230c | 2996 | } else { |
3529d8c2 JA |
2997 | struct io_sr_msg *sr = &req->sr_msg; |
2998 | ||
0b416c3e JA |
2999 | kmsg = &io.msg; |
3000 | kmsg->msg.msg_name = &addr; | |
3529d8c2 JA |
3001 | |
3002 | io.msg.iov = io.msg.fast_iov; | |
3003 | ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg, | |
3004 | sr->msg_flags, &io.msg.iov); | |
03b1230c | 3005 | if (ret) |
3529d8c2 | 3006 | return ret; |
03b1230c | 3007 | } |
0fa03c62 | 3008 | |
e47293fd JA |
3009 | flags = req->sr_msg.msg_flags; |
3010 | if (flags & MSG_DONTWAIT) | |
3011 | req->flags |= REQ_F_NOWAIT; | |
3012 | else if (force_nonblock) | |
3013 | flags |= MSG_DONTWAIT; | |
3014 | ||
0b416c3e | 3015 | ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags); |
03b1230c | 3016 | if (force_nonblock && ret == -EAGAIN) { |
b7bb4f7d JA |
3017 | if (req->io) |
3018 | return -EAGAIN; | |
1e95081c PB |
3019 | if (io_alloc_async_ctx(req)) { |
3020 | if (kmsg && kmsg->iov != kmsg->fast_iov) | |
3021 | kfree(kmsg->iov); | |
b7bb4f7d | 3022 | return -ENOMEM; |
1e95081c | 3023 | } |
b7bb4f7d | 3024 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
0b416c3e | 3025 | return -EAGAIN; |
03b1230c | 3026 | } |
441cdbd5 JA |
3027 | if (ret == -ERESTARTSYS) |
3028 | ret = -EINTR; | |
0fa03c62 JA |
3029 | } |
3030 | ||
1e95081c | 3031 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
0b416c3e | 3032 | kfree(kmsg->iov); |
78e19bbe | 3033 | io_cqring_add_event(req, ret); |
4e88d6e7 JA |
3034 | if (ret < 0) |
3035 | req_set_fail_links(req); | |
ec9c02ad | 3036 | io_put_req_find_next(req, nxt); |
5d17b4a4 | 3037 | return 0; |
03b1230c JA |
3038 | #else |
3039 | return -EOPNOTSUPP; | |
aa1fa28f | 3040 | #endif |
03b1230c | 3041 | } |
aa1fa28f | 3042 | |
fddaface JA |
3043 | static int io_send(struct io_kiocb *req, struct io_kiocb **nxt, |
3044 | bool force_nonblock) | |
3045 | { | |
3046 | #if defined(CONFIG_NET) | |
3047 | struct socket *sock; | |
3048 | int ret; | |
3049 | ||
3050 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3051 | return -EINVAL; | |
3052 | ||
3053 | sock = sock_from_file(req->file, &ret); | |
3054 | if (sock) { | |
3055 | struct io_sr_msg *sr = &req->sr_msg; | |
3056 | struct msghdr msg; | |
3057 | struct iovec iov; | |
3058 | unsigned flags; | |
3059 | ||
3060 | ret = import_single_range(WRITE, sr->buf, sr->len, &iov, | |
3061 | &msg.msg_iter); | |
3062 | if (ret) | |
3063 | return ret; | |
3064 | ||
3065 | msg.msg_name = NULL; | |
3066 | msg.msg_control = NULL; | |
3067 | msg.msg_controllen = 0; | |
3068 | msg.msg_namelen = 0; | |
3069 | ||
3070 | flags = req->sr_msg.msg_flags; | |
3071 | if (flags & MSG_DONTWAIT) | |
3072 | req->flags |= REQ_F_NOWAIT; | |
3073 | else if (force_nonblock) | |
3074 | flags |= MSG_DONTWAIT; | |
3075 | ||
0b7b21e4 JA |
3076 | msg.msg_flags = flags; |
3077 | ret = sock_sendmsg(sock, &msg); | |
fddaface JA |
3078 | if (force_nonblock && ret == -EAGAIN) |
3079 | return -EAGAIN; | |
3080 | if (ret == -ERESTARTSYS) | |
3081 | ret = -EINTR; | |
3082 | } | |
3083 | ||
3084 | io_cqring_add_event(req, ret); | |
3085 | if (ret < 0) | |
3086 | req_set_fail_links(req); | |
3087 | io_put_req_find_next(req, nxt); | |
3088 | return 0; | |
3089 | #else | |
3090 | return -EOPNOTSUPP; | |
3091 | #endif | |
3092 | } | |
3093 | ||
3529d8c2 JA |
3094 | static int io_recvmsg_prep(struct io_kiocb *req, |
3095 | const struct io_uring_sqe *sqe) | |
aa1fa28f JA |
3096 | { |
3097 | #if defined(CONFIG_NET) | |
e47293fd | 3098 | struct io_sr_msg *sr = &req->sr_msg; |
3529d8c2 JA |
3099 | struct io_async_ctx *io = req->io; |
3100 | ||
3101 | sr->msg_flags = READ_ONCE(sqe->msg_flags); | |
3102 | sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr)); | |
0b7b21e4 | 3103 | sr->len = READ_ONCE(sqe->len); |
06b76d44 | 3104 | |
fddaface | 3105 | if (!io || req->opcode == IORING_OP_RECV) |
06b76d44 | 3106 | return 0; |
03b1230c | 3107 | |
d9688565 | 3108 | io->msg.iov = io->msg.fast_iov; |
3529d8c2 | 3109 | return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags, |
e47293fd | 3110 | &io->msg.uaddr, &io->msg.iov); |
aa1fa28f | 3111 | #else |
e47293fd | 3112 | return -EOPNOTSUPP; |
aa1fa28f JA |
3113 | #endif |
3114 | } | |
3115 | ||
fc4df999 JA |
3116 | static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt, |
3117 | bool force_nonblock) | |
aa1fa28f JA |
3118 | { |
3119 | #if defined(CONFIG_NET) | |
0b416c3e | 3120 | struct io_async_msghdr *kmsg = NULL; |
03b1230c JA |
3121 | struct socket *sock; |
3122 | int ret; | |
3123 | ||
3124 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3125 | return -EINVAL; | |
3126 | ||
3127 | sock = sock_from_file(req->file, &ret); | |
3128 | if (sock) { | |
b7bb4f7d | 3129 | struct io_async_ctx io; |
03b1230c | 3130 | struct sockaddr_storage addr; |
03b1230c JA |
3131 | unsigned flags; |
3132 | ||
03b1230c | 3133 | if (req->io) { |
0b416c3e JA |
3134 | kmsg = &req->io->msg; |
3135 | kmsg->msg.msg_name = &addr; | |
3136 | /* if iov is set, it's allocated already */ | |
3137 | if (!kmsg->iov) | |
3138 | kmsg->iov = kmsg->fast_iov; | |
3139 | kmsg->msg.msg_iter.iov = kmsg->iov; | |
03b1230c | 3140 | } else { |
3529d8c2 JA |
3141 | struct io_sr_msg *sr = &req->sr_msg; |
3142 | ||
0b416c3e JA |
3143 | kmsg = &io.msg; |
3144 | kmsg->msg.msg_name = &addr; | |
3529d8c2 JA |
3145 | |
3146 | io.msg.iov = io.msg.fast_iov; | |
3147 | ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg, | |
3148 | sr->msg_flags, &io.msg.uaddr, | |
3149 | &io.msg.iov); | |
03b1230c | 3150 | if (ret) |
3529d8c2 | 3151 | return ret; |
03b1230c JA |
3152 | } |
3153 | ||
e47293fd JA |
3154 | flags = req->sr_msg.msg_flags; |
3155 | if (flags & MSG_DONTWAIT) | |
3156 | req->flags |= REQ_F_NOWAIT; | |
3157 | else if (force_nonblock) | |
3158 | flags |= MSG_DONTWAIT; | |
3159 | ||
3160 | ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg, | |
3161 | kmsg->uaddr, flags); | |
03b1230c | 3162 | if (force_nonblock && ret == -EAGAIN) { |
b7bb4f7d JA |
3163 | if (req->io) |
3164 | return -EAGAIN; | |
1e95081c PB |
3165 | if (io_alloc_async_ctx(req)) { |
3166 | if (kmsg && kmsg->iov != kmsg->fast_iov) | |
3167 | kfree(kmsg->iov); | |
b7bb4f7d | 3168 | return -ENOMEM; |
1e95081c | 3169 | } |
b7bb4f7d | 3170 | memcpy(&req->io->msg, &io.msg, sizeof(io.msg)); |
0b416c3e | 3171 | return -EAGAIN; |
03b1230c JA |
3172 | } |
3173 | if (ret == -ERESTARTSYS) | |
3174 | ret = -EINTR; | |
3175 | } | |
3176 | ||
1e95081c | 3177 | if (kmsg && kmsg->iov != kmsg->fast_iov) |
0b416c3e | 3178 | kfree(kmsg->iov); |
03b1230c | 3179 | io_cqring_add_event(req, ret); |
4e88d6e7 JA |
3180 | if (ret < 0) |
3181 | req_set_fail_links(req); | |
03b1230c JA |
3182 | io_put_req_find_next(req, nxt); |
3183 | return 0; | |
0fa03c62 JA |
3184 | #else |
3185 | return -EOPNOTSUPP; | |
3186 | #endif | |
3187 | } | |
5d17b4a4 | 3188 | |
fddaface JA |
3189 | static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt, |
3190 | bool force_nonblock) | |
3191 | { | |
3192 | #if defined(CONFIG_NET) | |
3193 | struct socket *sock; | |
3194 | int ret; | |
3195 | ||
3196 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3197 | return -EINVAL; | |
3198 | ||
3199 | sock = sock_from_file(req->file, &ret); | |
3200 | if (sock) { | |
3201 | struct io_sr_msg *sr = &req->sr_msg; | |
3202 | struct msghdr msg; | |
3203 | struct iovec iov; | |
3204 | unsigned flags; | |
3205 | ||
3206 | ret = import_single_range(READ, sr->buf, sr->len, &iov, | |
3207 | &msg.msg_iter); | |
3208 | if (ret) | |
3209 | return ret; | |
3210 | ||
3211 | msg.msg_name = NULL; | |
3212 | msg.msg_control = NULL; | |
3213 | msg.msg_controllen = 0; | |
3214 | msg.msg_namelen = 0; | |
3215 | msg.msg_iocb = NULL; | |
3216 | msg.msg_flags = 0; | |
3217 | ||
3218 | flags = req->sr_msg.msg_flags; | |
3219 | if (flags & MSG_DONTWAIT) | |
3220 | req->flags |= REQ_F_NOWAIT; | |
3221 | else if (force_nonblock) | |
3222 | flags |= MSG_DONTWAIT; | |
3223 | ||
0b7b21e4 | 3224 | ret = sock_recvmsg(sock, &msg, flags); |
fddaface JA |
3225 | if (force_nonblock && ret == -EAGAIN) |
3226 | return -EAGAIN; | |
3227 | if (ret == -ERESTARTSYS) | |
3228 | ret = -EINTR; | |
3229 | } | |
3230 | ||
3231 | io_cqring_add_event(req, ret); | |
3232 | if (ret < 0) | |
3233 | req_set_fail_links(req); | |
3234 | io_put_req_find_next(req, nxt); | |
3235 | return 0; | |
3236 | #else | |
3237 | return -EOPNOTSUPP; | |
3238 | #endif | |
3239 | } | |
3240 | ||
3241 | ||
3529d8c2 | 3242 | static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
17f2fe35 JA |
3243 | { |
3244 | #if defined(CONFIG_NET) | |
8ed8d3c3 JA |
3245 | struct io_accept *accept = &req->accept; |
3246 | ||
17f2fe35 JA |
3247 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3248 | return -EINVAL; | |
8042d6ce | 3249 | if (sqe->ioprio || sqe->len || sqe->buf_index) |
17f2fe35 JA |
3250 | return -EINVAL; |
3251 | ||
d55e5f5b JA |
3252 | accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
3253 | accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2)); | |
8ed8d3c3 | 3254 | accept->flags = READ_ONCE(sqe->accept_flags); |
8ed8d3c3 JA |
3255 | return 0; |
3256 | #else | |
3257 | return -EOPNOTSUPP; | |
3258 | #endif | |
3259 | } | |
17f2fe35 | 3260 | |
8ed8d3c3 JA |
3261 | #if defined(CONFIG_NET) |
3262 | static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt, | |
3263 | bool force_nonblock) | |
3264 | { | |
3265 | struct io_accept *accept = &req->accept; | |
3266 | unsigned file_flags; | |
3267 | int ret; | |
3268 | ||
3269 | file_flags = force_nonblock ? O_NONBLOCK : 0; | |
3270 | ret = __sys_accept4_file(req->file, file_flags, accept->addr, | |
3271 | accept->addr_len, accept->flags); | |
3272 | if (ret == -EAGAIN && force_nonblock) | |
17f2fe35 | 3273 | return -EAGAIN; |
8e3cca12 JA |
3274 | if (ret == -ERESTARTSYS) |
3275 | ret = -EINTR; | |
4e88d6e7 JA |
3276 | if (ret < 0) |
3277 | req_set_fail_links(req); | |
78e19bbe | 3278 | io_cqring_add_event(req, ret); |
ec9c02ad | 3279 | io_put_req_find_next(req, nxt); |
17f2fe35 | 3280 | return 0; |
8ed8d3c3 JA |
3281 | } |
3282 | ||
3283 | static void io_accept_finish(struct io_wq_work **workptr) | |
3284 | { | |
3285 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
3286 | struct io_kiocb *nxt = NULL; | |
3287 | ||
3288 | if (io_req_cancelled(req)) | |
3289 | return; | |
3290 | __io_accept(req, &nxt, false); | |
3291 | if (nxt) | |
78912934 | 3292 | io_wq_assign_next(workptr, nxt); |
8ed8d3c3 JA |
3293 | } |
3294 | #endif | |
3295 | ||
3296 | static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt, | |
3297 | bool force_nonblock) | |
3298 | { | |
3299 | #if defined(CONFIG_NET) | |
3300 | int ret; | |
3301 | ||
8ed8d3c3 JA |
3302 | ret = __io_accept(req, nxt, force_nonblock); |
3303 | if (ret == -EAGAIN && force_nonblock) { | |
3304 | req->work.func = io_accept_finish; | |
8ed8d3c3 JA |
3305 | io_put_req(req); |
3306 | return -EAGAIN; | |
3307 | } | |
3308 | return 0; | |
0fa03c62 JA |
3309 | #else |
3310 | return -EOPNOTSUPP; | |
3311 | #endif | |
3312 | } | |
5d17b4a4 | 3313 | |
3529d8c2 | 3314 | static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
f499a021 JA |
3315 | { |
3316 | #if defined(CONFIG_NET) | |
3529d8c2 JA |
3317 | struct io_connect *conn = &req->connect; |
3318 | struct io_async_ctx *io = req->io; | |
f499a021 | 3319 | |
3fbb51c1 JA |
3320 | if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL))) |
3321 | return -EINVAL; | |
3322 | if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags) | |
3323 | return -EINVAL; | |
3324 | ||
3529d8c2 JA |
3325 | conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
3326 | conn->addr_len = READ_ONCE(sqe->addr2); | |
3327 | ||
3328 | if (!io) | |
3329 | return 0; | |
3330 | ||
3331 | return move_addr_to_kernel(conn->addr, conn->addr_len, | |
3fbb51c1 | 3332 | &io->connect.address); |
f499a021 | 3333 | #else |
3fbb51c1 | 3334 | return -EOPNOTSUPP; |
f499a021 JA |
3335 | #endif |
3336 | } | |
3337 | ||
fc4df999 JA |
3338 | static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt, |
3339 | bool force_nonblock) | |
f8e85cf2 JA |
3340 | { |
3341 | #if defined(CONFIG_NET) | |
f499a021 | 3342 | struct io_async_ctx __io, *io; |
f8e85cf2 | 3343 | unsigned file_flags; |
3fbb51c1 | 3344 | int ret; |
f8e85cf2 | 3345 | |
f499a021 JA |
3346 | if (req->io) { |
3347 | io = req->io; | |
3348 | } else { | |
3529d8c2 JA |
3349 | ret = move_addr_to_kernel(req->connect.addr, |
3350 | req->connect.addr_len, | |
3351 | &__io.connect.address); | |
f499a021 JA |
3352 | if (ret) |
3353 | goto out; | |
3354 | io = &__io; | |
3355 | } | |
3356 | ||
3fbb51c1 JA |
3357 | file_flags = force_nonblock ? O_NONBLOCK : 0; |
3358 | ||
3359 | ret = __sys_connect_file(req->file, &io->connect.address, | |
3360 | req->connect.addr_len, file_flags); | |
87f80d62 | 3361 | if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) { |
b7bb4f7d JA |
3362 | if (req->io) |
3363 | return -EAGAIN; | |
3364 | if (io_alloc_async_ctx(req)) { | |
f499a021 JA |
3365 | ret = -ENOMEM; |
3366 | goto out; | |
3367 | } | |
b7bb4f7d | 3368 | memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect)); |
f8e85cf2 | 3369 | return -EAGAIN; |
f499a021 | 3370 | } |
f8e85cf2 JA |
3371 | if (ret == -ERESTARTSYS) |
3372 | ret = -EINTR; | |
f499a021 | 3373 | out: |
4e88d6e7 JA |
3374 | if (ret < 0) |
3375 | req_set_fail_links(req); | |
f8e85cf2 JA |
3376 | io_cqring_add_event(req, ret); |
3377 | io_put_req_find_next(req, nxt); | |
3378 | return 0; | |
3379 | #else | |
3380 | return -EOPNOTSUPP; | |
3381 | #endif | |
3382 | } | |
3383 | ||
221c5eb2 JA |
3384 | static void io_poll_remove_one(struct io_kiocb *req) |
3385 | { | |
3386 | struct io_poll_iocb *poll = &req->poll; | |
3387 | ||
3388 | spin_lock(&poll->head->lock); | |
3389 | WRITE_ONCE(poll->canceled, true); | |
392edb45 JA |
3390 | if (!list_empty(&poll->wait.entry)) { |
3391 | list_del_init(&poll->wait.entry); | |
a197f664 | 3392 | io_queue_async_work(req); |
221c5eb2 JA |
3393 | } |
3394 | spin_unlock(&poll->head->lock); | |
78076bb6 | 3395 | hash_del(&req->hash_node); |
221c5eb2 JA |
3396 | } |
3397 | ||
3398 | static void io_poll_remove_all(struct io_ring_ctx *ctx) | |
3399 | { | |
78076bb6 | 3400 | struct hlist_node *tmp; |
221c5eb2 | 3401 | struct io_kiocb *req; |
78076bb6 | 3402 | int i; |
221c5eb2 JA |
3403 | |
3404 | spin_lock_irq(&ctx->completion_lock); | |
78076bb6 JA |
3405 | for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) { |
3406 | struct hlist_head *list; | |
3407 | ||
3408 | list = &ctx->cancel_hash[i]; | |
3409 | hlist_for_each_entry_safe(req, tmp, list, hash_node) | |
3410 | io_poll_remove_one(req); | |
221c5eb2 JA |
3411 | } |
3412 | spin_unlock_irq(&ctx->completion_lock); | |
3413 | } | |
3414 | ||
47f46768 JA |
3415 | static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr) |
3416 | { | |
78076bb6 | 3417 | struct hlist_head *list; |
47f46768 JA |
3418 | struct io_kiocb *req; |
3419 | ||
78076bb6 JA |
3420 | list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)]; |
3421 | hlist_for_each_entry(req, list, hash_node) { | |
3422 | if (sqe_addr == req->user_data) { | |
eac406c6 JA |
3423 | io_poll_remove_one(req); |
3424 | return 0; | |
3425 | } | |
47f46768 JA |
3426 | } |
3427 | ||
3428 | return -ENOENT; | |
3429 | } | |
3430 | ||
3529d8c2 JA |
3431 | static int io_poll_remove_prep(struct io_kiocb *req, |
3432 | const struct io_uring_sqe *sqe) | |
0969e783 | 3433 | { |
0969e783 JA |
3434 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3435 | return -EINVAL; | |
3436 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || | |
3437 | sqe->poll_events) | |
3438 | return -EINVAL; | |
3439 | ||
3440 | req->poll.addr = READ_ONCE(sqe->addr); | |
0969e783 JA |
3441 | return 0; |
3442 | } | |
3443 | ||
221c5eb2 JA |
3444 | /* |
3445 | * Find a running poll command that matches one specified in sqe->addr, | |
3446 | * and remove it if found. | |
3447 | */ | |
fc4df999 | 3448 | static int io_poll_remove(struct io_kiocb *req) |
221c5eb2 JA |
3449 | { |
3450 | struct io_ring_ctx *ctx = req->ctx; | |
0969e783 | 3451 | u64 addr; |
47f46768 | 3452 | int ret; |
221c5eb2 | 3453 | |
0969e783 | 3454 | addr = req->poll.addr; |
221c5eb2 | 3455 | spin_lock_irq(&ctx->completion_lock); |
0969e783 | 3456 | ret = io_poll_cancel(ctx, addr); |
221c5eb2 JA |
3457 | spin_unlock_irq(&ctx->completion_lock); |
3458 | ||
78e19bbe | 3459 | io_cqring_add_event(req, ret); |
4e88d6e7 JA |
3460 | if (ret < 0) |
3461 | req_set_fail_links(req); | |
e65ef56d | 3462 | io_put_req(req); |
221c5eb2 JA |
3463 | return 0; |
3464 | } | |
3465 | ||
b0dd8a41 | 3466 | static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error) |
221c5eb2 | 3467 | { |
a197f664 JL |
3468 | struct io_ring_ctx *ctx = req->ctx; |
3469 | ||
8c838788 | 3470 | req->poll.done = true; |
b0dd8a41 JA |
3471 | if (error) |
3472 | io_cqring_fill_event(req, error); | |
3473 | else | |
3474 | io_cqring_fill_event(req, mangle_poll(mask)); | |
8c838788 | 3475 | io_commit_cqring(ctx); |
221c5eb2 JA |
3476 | } |
3477 | ||
561fb04a | 3478 | static void io_poll_complete_work(struct io_wq_work **workptr) |
221c5eb2 | 3479 | { |
561fb04a | 3480 | struct io_wq_work *work = *workptr; |
221c5eb2 JA |
3481 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
3482 | struct io_poll_iocb *poll = &req->poll; | |
3483 | struct poll_table_struct pt = { ._key = poll->events }; | |
3484 | struct io_ring_ctx *ctx = req->ctx; | |
89723d0b | 3485 | struct io_kiocb *nxt = NULL; |
221c5eb2 | 3486 | __poll_t mask = 0; |
b0dd8a41 | 3487 | int ret = 0; |
221c5eb2 | 3488 | |
b0dd8a41 | 3489 | if (work->flags & IO_WQ_WORK_CANCEL) { |
561fb04a | 3490 | WRITE_ONCE(poll->canceled, true); |
b0dd8a41 JA |
3491 | ret = -ECANCELED; |
3492 | } else if (READ_ONCE(poll->canceled)) { | |
3493 | ret = -ECANCELED; | |
3494 | } | |
561fb04a | 3495 | |
b0dd8a41 | 3496 | if (ret != -ECANCELED) |
221c5eb2 JA |
3497 | mask = vfs_poll(poll->file, &pt) & poll->events; |
3498 | ||
3499 | /* | |
3500 | * Note that ->ki_cancel callers also delete iocb from active_reqs after | |
3501 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to | |
3502 | * synchronize with them. In the cancellation case the list_del_init | |
3503 | * itself is not actually needed, but harmless so we keep it in to | |
3504 | * avoid further branches in the fast path. | |
3505 | */ | |
3506 | spin_lock_irq(&ctx->completion_lock); | |
b0dd8a41 | 3507 | if (!mask && ret != -ECANCELED) { |
392edb45 | 3508 | add_wait_queue(poll->head, &poll->wait); |
221c5eb2 JA |
3509 | spin_unlock_irq(&ctx->completion_lock); |
3510 | return; | |
3511 | } | |
78076bb6 | 3512 | hash_del(&req->hash_node); |
b0dd8a41 | 3513 | io_poll_complete(req, mask, ret); |
221c5eb2 JA |
3514 | spin_unlock_irq(&ctx->completion_lock); |
3515 | ||
8c838788 | 3516 | io_cqring_ev_posted(ctx); |
89723d0b | 3517 | |
4e88d6e7 JA |
3518 | if (ret < 0) |
3519 | req_set_fail_links(req); | |
ec9c02ad | 3520 | io_put_req_find_next(req, &nxt); |
89723d0b | 3521 | if (nxt) |
78912934 | 3522 | io_wq_assign_next(workptr, nxt); |
221c5eb2 JA |
3523 | } |
3524 | ||
e94f141b JA |
3525 | static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes) |
3526 | { | |
e94f141b | 3527 | struct io_kiocb *req, *tmp; |
8237e045 | 3528 | struct req_batch rb; |
e94f141b | 3529 | |
c6ca97b3 | 3530 | rb.to_free = rb.need_iter = 0; |
e94f141b JA |
3531 | spin_lock_irq(&ctx->completion_lock); |
3532 | llist_for_each_entry_safe(req, tmp, nodes, llist_node) { | |
3533 | hash_del(&req->hash_node); | |
3534 | io_poll_complete(req, req->result, 0); | |
3535 | ||
8237e045 JA |
3536 | if (refcount_dec_and_test(&req->refs) && |
3537 | !io_req_multi_free(&rb, req)) { | |
3538 | req->flags |= REQ_F_COMP_LOCKED; | |
3539 | io_free_req(req); | |
e94f141b JA |
3540 | } |
3541 | } | |
3542 | spin_unlock_irq(&ctx->completion_lock); | |
3543 | ||
3544 | io_cqring_ev_posted(ctx); | |
8237e045 | 3545 | io_free_req_many(ctx, &rb); |
e94f141b JA |
3546 | } |
3547 | ||
3548 | static void io_poll_flush(struct io_wq_work **workptr) | |
3549 | { | |
3550 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
3551 | struct llist_node *nodes; | |
3552 | ||
3553 | nodes = llist_del_all(&req->ctx->poll_llist); | |
3554 | if (nodes) | |
3555 | __io_poll_flush(req->ctx, nodes); | |
3556 | } | |
3557 | ||
f0b493e6 JA |
3558 | static void io_poll_trigger_evfd(struct io_wq_work **workptr) |
3559 | { | |
3560 | struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work); | |
3561 | ||
3562 | eventfd_signal(req->ctx->cq_ev_fd, 1); | |
3563 | io_put_req(req); | |
3564 | } | |
3565 | ||
221c5eb2 JA |
3566 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
3567 | void *key) | |
3568 | { | |
e944475e | 3569 | struct io_poll_iocb *poll = wait->private; |
221c5eb2 JA |
3570 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
3571 | struct io_ring_ctx *ctx = req->ctx; | |
3572 | __poll_t mask = key_to_poll(key); | |
221c5eb2 JA |
3573 | |
3574 | /* for instances that support it check for an event match first: */ | |
8c838788 JA |
3575 | if (mask && !(mask & poll->events)) |
3576 | return 0; | |
221c5eb2 | 3577 | |
392edb45 | 3578 | list_del_init(&poll->wait.entry); |
221c5eb2 | 3579 | |
7c9e7f0f JA |
3580 | /* |
3581 | * Run completion inline if we can. We're using trylock here because | |
3582 | * we are violating the completion_lock -> poll wq lock ordering. | |
3583 | * If we have a link timeout we're going to need the completion_lock | |
3584 | * for finalizing the request, mark us as having grabbed that already. | |
3585 | */ | |
e94f141b JA |
3586 | if (mask) { |
3587 | unsigned long flags; | |
221c5eb2 | 3588 | |
e94f141b JA |
3589 | if (llist_empty(&ctx->poll_llist) && |
3590 | spin_trylock_irqsave(&ctx->completion_lock, flags)) { | |
f0b493e6 JA |
3591 | bool trigger_ev; |
3592 | ||
e94f141b JA |
3593 | hash_del(&req->hash_node); |
3594 | io_poll_complete(req, mask, 0); | |
e94f141b | 3595 | |
f0b493e6 JA |
3596 | trigger_ev = io_should_trigger_evfd(ctx); |
3597 | if (trigger_ev && eventfd_signal_count()) { | |
3598 | trigger_ev = false; | |
3599 | req->work.func = io_poll_trigger_evfd; | |
3600 | } else { | |
3601 | req->flags |= REQ_F_COMP_LOCKED; | |
3602 | io_put_req(req); | |
3603 | req = NULL; | |
3604 | } | |
3605 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
3606 | __io_cqring_ev_posted(ctx, trigger_ev); | |
e94f141b JA |
3607 | } else { |
3608 | req->result = mask; | |
3609 | req->llist_node.next = NULL; | |
3610 | /* if the list wasn't empty, we're done */ | |
3611 | if (!llist_add(&req->llist_node, &ctx->poll_llist)) | |
3612 | req = NULL; | |
3613 | else | |
3614 | req->work.func = io_poll_flush; | |
3615 | } | |
221c5eb2 | 3616 | } |
e94f141b JA |
3617 | if (req) |
3618 | io_queue_async_work(req); | |
221c5eb2 | 3619 | |
221c5eb2 JA |
3620 | return 1; |
3621 | } | |
3622 | ||
3623 | struct io_poll_table { | |
3624 | struct poll_table_struct pt; | |
3625 | struct io_kiocb *req; | |
3626 | int error; | |
3627 | }; | |
3628 | ||
3629 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, | |
3630 | struct poll_table_struct *p) | |
3631 | { | |
3632 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); | |
3633 | ||
3634 | if (unlikely(pt->req->poll.head)) { | |
3635 | pt->error = -EINVAL; | |
3636 | return; | |
3637 | } | |
3638 | ||
3639 | pt->error = 0; | |
3640 | pt->req->poll.head = head; | |
392edb45 | 3641 | add_wait_queue(head, &pt->req->poll.wait); |
221c5eb2 JA |
3642 | } |
3643 | ||
eac406c6 JA |
3644 | static void io_poll_req_insert(struct io_kiocb *req) |
3645 | { | |
3646 | struct io_ring_ctx *ctx = req->ctx; | |
78076bb6 JA |
3647 | struct hlist_head *list; |
3648 | ||
3649 | list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)]; | |
3650 | hlist_add_head(&req->hash_node, list); | |
eac406c6 JA |
3651 | } |
3652 | ||
3529d8c2 | 3653 | static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
221c5eb2 JA |
3654 | { |
3655 | struct io_poll_iocb *poll = &req->poll; | |
221c5eb2 | 3656 | u16 events; |
221c5eb2 JA |
3657 | |
3658 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) | |
3659 | return -EINVAL; | |
3660 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) | |
3661 | return -EINVAL; | |
09bb8394 JA |
3662 | if (!poll->file) |
3663 | return -EBADF; | |
221c5eb2 | 3664 | |
221c5eb2 JA |
3665 | events = READ_ONCE(sqe->poll_events); |
3666 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; | |
0969e783 JA |
3667 | return 0; |
3668 | } | |
3669 | ||
3670 | static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt) | |
3671 | { | |
3672 | struct io_poll_iocb *poll = &req->poll; | |
3673 | struct io_ring_ctx *ctx = req->ctx; | |
3674 | struct io_poll_table ipt; | |
3675 | bool cancel = false; | |
3676 | __poll_t mask; | |
0969e783 JA |
3677 | |
3678 | INIT_IO_WORK(&req->work, io_poll_complete_work); | |
78076bb6 | 3679 | INIT_HLIST_NODE(&req->hash_node); |
221c5eb2 | 3680 | |
221c5eb2 | 3681 | poll->head = NULL; |
8c838788 | 3682 | poll->done = false; |
221c5eb2 JA |
3683 | poll->canceled = false; |
3684 | ||
3685 | ipt.pt._qproc = io_poll_queue_proc; | |
3686 | ipt.pt._key = poll->events; | |
3687 | ipt.req = req; | |
3688 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ | |
3689 | ||
3690 | /* initialized the list so that we can do list_empty checks */ | |
392edb45 JA |
3691 | INIT_LIST_HEAD(&poll->wait.entry); |
3692 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); | |
3693 | poll->wait.private = poll; | |
221c5eb2 | 3694 | |
36703247 JA |
3695 | INIT_LIST_HEAD(&req->list); |
3696 | ||
221c5eb2 | 3697 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
221c5eb2 JA |
3698 | |
3699 | spin_lock_irq(&ctx->completion_lock); | |
8c838788 JA |
3700 | if (likely(poll->head)) { |
3701 | spin_lock(&poll->head->lock); | |
392edb45 | 3702 | if (unlikely(list_empty(&poll->wait.entry))) { |
8c838788 JA |
3703 | if (ipt.error) |
3704 | cancel = true; | |
3705 | ipt.error = 0; | |
3706 | mask = 0; | |
3707 | } | |
3708 | if (mask || ipt.error) | |
392edb45 | 3709 | list_del_init(&poll->wait.entry); |
8c838788 JA |
3710 | else if (cancel) |
3711 | WRITE_ONCE(poll->canceled, true); | |
3712 | else if (!poll->done) /* actually waiting for an event */ | |
eac406c6 | 3713 | io_poll_req_insert(req); |
8c838788 JA |
3714 | spin_unlock(&poll->head->lock); |
3715 | } | |
3716 | if (mask) { /* no async, we'd stolen it */ | |
221c5eb2 | 3717 | ipt.error = 0; |
b0dd8a41 | 3718 | io_poll_complete(req, mask, 0); |
221c5eb2 | 3719 | } |
221c5eb2 JA |
3720 | spin_unlock_irq(&ctx->completion_lock); |
3721 | ||
8c838788 JA |
3722 | if (mask) { |
3723 | io_cqring_ev_posted(ctx); | |
ec9c02ad | 3724 | io_put_req_find_next(req, nxt); |
221c5eb2 | 3725 | } |
8c838788 | 3726 | return ipt.error; |
221c5eb2 JA |
3727 | } |
3728 | ||
5262f567 JA |
3729 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
3730 | { | |
ad8a48ac JA |
3731 | struct io_timeout_data *data = container_of(timer, |
3732 | struct io_timeout_data, timer); | |
3733 | struct io_kiocb *req = data->req; | |
3734 | struct io_ring_ctx *ctx = req->ctx; | |
5262f567 JA |
3735 | unsigned long flags; |
3736 | ||
5262f567 JA |
3737 | atomic_inc(&ctx->cq_timeouts); |
3738 | ||
3739 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
ef03681a | 3740 | /* |
11365043 JA |
3741 | * We could be racing with timeout deletion. If the list is empty, |
3742 | * then timeout lookup already found it and will be handling it. | |
ef03681a | 3743 | */ |
842f9612 | 3744 | if (!list_empty(&req->list)) { |
11365043 | 3745 | struct io_kiocb *prev; |
5262f567 | 3746 | |
11365043 JA |
3747 | /* |
3748 | * Adjust the reqs sequence before the current one because it | |
d195a66e | 3749 | * will consume a slot in the cq_ring and the cq_tail |
11365043 JA |
3750 | * pointer will be increased, otherwise other timeout reqs may |
3751 | * return in advance without waiting for enough wait_nr. | |
3752 | */ | |
3753 | prev = req; | |
3754 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) | |
3755 | prev->sequence++; | |
11365043 | 3756 | list_del_init(&req->list); |
11365043 | 3757 | } |
5262f567 | 3758 | |
78e19bbe | 3759 | io_cqring_fill_event(req, -ETIME); |
5262f567 JA |
3760 | io_commit_cqring(ctx); |
3761 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
3762 | ||
3763 | io_cqring_ev_posted(ctx); | |
4e88d6e7 | 3764 | req_set_fail_links(req); |
5262f567 JA |
3765 | io_put_req(req); |
3766 | return HRTIMER_NORESTART; | |
3767 | } | |
3768 | ||
47f46768 JA |
3769 | static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data) |
3770 | { | |
3771 | struct io_kiocb *req; | |
3772 | int ret = -ENOENT; | |
3773 | ||
3774 | list_for_each_entry(req, &ctx->timeout_list, list) { | |
3775 | if (user_data == req->user_data) { | |
3776 | list_del_init(&req->list); | |
3777 | ret = 0; | |
3778 | break; | |
3779 | } | |
3780 | } | |
3781 | ||
3782 | if (ret == -ENOENT) | |
3783 | return ret; | |
3784 | ||
2d28390a | 3785 | ret = hrtimer_try_to_cancel(&req->io->timeout.timer); |
47f46768 JA |
3786 | if (ret == -1) |
3787 | return -EALREADY; | |
3788 | ||
4e88d6e7 | 3789 | req_set_fail_links(req); |
47f46768 JA |
3790 | io_cqring_fill_event(req, -ECANCELED); |
3791 | io_put_req(req); | |
3792 | return 0; | |
3793 | } | |
3794 | ||
3529d8c2 JA |
3795 | static int io_timeout_remove_prep(struct io_kiocb *req, |
3796 | const struct io_uring_sqe *sqe) | |
b29472ee | 3797 | { |
b29472ee JA |
3798 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
3799 | return -EINVAL; | |
3800 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len) | |
3801 | return -EINVAL; | |
3802 | ||
3803 | req->timeout.addr = READ_ONCE(sqe->addr); | |
3804 | req->timeout.flags = READ_ONCE(sqe->timeout_flags); | |
3805 | if (req->timeout.flags) | |
3806 | return -EINVAL; | |
3807 | ||
b29472ee JA |
3808 | return 0; |
3809 | } | |
3810 | ||
11365043 JA |
3811 | /* |
3812 | * Remove or update an existing timeout command | |
3813 | */ | |
fc4df999 | 3814 | static int io_timeout_remove(struct io_kiocb *req) |
11365043 JA |
3815 | { |
3816 | struct io_ring_ctx *ctx = req->ctx; | |
47f46768 | 3817 | int ret; |
11365043 | 3818 | |
11365043 | 3819 | spin_lock_irq(&ctx->completion_lock); |
b29472ee | 3820 | ret = io_timeout_cancel(ctx, req->timeout.addr); |
11365043 | 3821 | |
47f46768 | 3822 | io_cqring_fill_event(req, ret); |
11365043 JA |
3823 | io_commit_cqring(ctx); |
3824 | spin_unlock_irq(&ctx->completion_lock); | |
5262f567 | 3825 | io_cqring_ev_posted(ctx); |
4e88d6e7 JA |
3826 | if (ret < 0) |
3827 | req_set_fail_links(req); | |
ec9c02ad | 3828 | io_put_req(req); |
11365043 | 3829 | return 0; |
5262f567 JA |
3830 | } |
3831 | ||
3529d8c2 | 3832 | static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
2d28390a | 3833 | bool is_timeout_link) |
5262f567 | 3834 | { |
ad8a48ac | 3835 | struct io_timeout_data *data; |
a41525ab | 3836 | unsigned flags; |
5262f567 | 3837 | |
ad8a48ac | 3838 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
5262f567 | 3839 | return -EINVAL; |
ad8a48ac | 3840 | if (sqe->ioprio || sqe->buf_index || sqe->len != 1) |
a41525ab | 3841 | return -EINVAL; |
2d28390a JA |
3842 | if (sqe->off && is_timeout_link) |
3843 | return -EINVAL; | |
a41525ab JA |
3844 | flags = READ_ONCE(sqe->timeout_flags); |
3845 | if (flags & ~IORING_TIMEOUT_ABS) | |
5262f567 | 3846 | return -EINVAL; |
bdf20073 | 3847 | |
26a61679 JA |
3848 | req->timeout.count = READ_ONCE(sqe->off); |
3849 | ||
3529d8c2 | 3850 | if (!req->io && io_alloc_async_ctx(req)) |
26a61679 JA |
3851 | return -ENOMEM; |
3852 | ||
3853 | data = &req->io->timeout; | |
ad8a48ac | 3854 | data->req = req; |
ad8a48ac JA |
3855 | req->flags |= REQ_F_TIMEOUT; |
3856 | ||
3857 | if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr))) | |
5262f567 JA |
3858 | return -EFAULT; |
3859 | ||
11365043 | 3860 | if (flags & IORING_TIMEOUT_ABS) |
ad8a48ac | 3861 | data->mode = HRTIMER_MODE_ABS; |
11365043 | 3862 | else |
ad8a48ac | 3863 | data->mode = HRTIMER_MODE_REL; |
11365043 | 3864 | |
ad8a48ac JA |
3865 | hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode); |
3866 | return 0; | |
3867 | } | |
3868 | ||
fc4df999 | 3869 | static int io_timeout(struct io_kiocb *req) |
ad8a48ac JA |
3870 | { |
3871 | unsigned count; | |
3872 | struct io_ring_ctx *ctx = req->ctx; | |
3873 | struct io_timeout_data *data; | |
3874 | struct list_head *entry; | |
3875 | unsigned span = 0; | |
ad8a48ac | 3876 | |
2d28390a | 3877 | data = &req->io->timeout; |
93bd25bb | 3878 | |
5262f567 JA |
3879 | /* |
3880 | * sqe->off holds how many events that need to occur for this | |
93bd25bb JA |
3881 | * timeout event to be satisfied. If it isn't set, then this is |
3882 | * a pure timeout request, sequence isn't used. | |
5262f567 | 3883 | */ |
26a61679 | 3884 | count = req->timeout.count; |
93bd25bb JA |
3885 | if (!count) { |
3886 | req->flags |= REQ_F_TIMEOUT_NOSEQ; | |
3887 | spin_lock_irq(&ctx->completion_lock); | |
3888 | entry = ctx->timeout_list.prev; | |
3889 | goto add; | |
3890 | } | |
5262f567 JA |
3891 | |
3892 | req->sequence = ctx->cached_sq_head + count - 1; | |
2d28390a | 3893 | data->seq_offset = count; |
5262f567 JA |
3894 | |
3895 | /* | |
3896 | * Insertion sort, ensuring the first entry in the list is always | |
3897 | * the one we need first. | |
3898 | */ | |
5262f567 JA |
3899 | spin_lock_irq(&ctx->completion_lock); |
3900 | list_for_each_prev(entry, &ctx->timeout_list) { | |
3901 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); | |
5da0fb1a | 3902 | unsigned nxt_sq_head; |
3903 | long long tmp, tmp_nxt; | |
2d28390a | 3904 | u32 nxt_offset = nxt->io->timeout.seq_offset; |
5262f567 | 3905 | |
93bd25bb JA |
3906 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
3907 | continue; | |
3908 | ||
5da0fb1a | 3909 | /* |
3910 | * Since cached_sq_head + count - 1 can overflow, use type long | |
3911 | * long to store it. | |
3912 | */ | |
3913 | tmp = (long long)ctx->cached_sq_head + count - 1; | |
cc42e0ac PB |
3914 | nxt_sq_head = nxt->sequence - nxt_offset + 1; |
3915 | tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1; | |
5da0fb1a | 3916 | |
3917 | /* | |
3918 | * cached_sq_head may overflow, and it will never overflow twice | |
3919 | * once there is some timeout req still be valid. | |
3920 | */ | |
3921 | if (ctx->cached_sq_head < nxt_sq_head) | |
8b07a65a | 3922 | tmp += UINT_MAX; |
5da0fb1a | 3923 | |
a1f58ba4 | 3924 | if (tmp > tmp_nxt) |
5262f567 | 3925 | break; |
a1f58ba4 | 3926 | |
3927 | /* | |
3928 | * Sequence of reqs after the insert one and itself should | |
3929 | * be adjusted because each timeout req consumes a slot. | |
3930 | */ | |
3931 | span++; | |
3932 | nxt->sequence++; | |
5262f567 | 3933 | } |
a1f58ba4 | 3934 | req->sequence -= span; |
93bd25bb | 3935 | add: |
5262f567 | 3936 | list_add(&req->list, entry); |
ad8a48ac JA |
3937 | data->timer.function = io_timeout_fn; |
3938 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode); | |
5262f567 | 3939 | spin_unlock_irq(&ctx->completion_lock); |
5262f567 JA |
3940 | return 0; |
3941 | } | |
5262f567 | 3942 | |
62755e35 JA |
3943 | static bool io_cancel_cb(struct io_wq_work *work, void *data) |
3944 | { | |
3945 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
3946 | ||
3947 | return req->user_data == (unsigned long) data; | |
3948 | } | |
3949 | ||
e977d6d3 | 3950 | static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr) |
62755e35 | 3951 | { |
62755e35 | 3952 | enum io_wq_cancel cancel_ret; |
62755e35 JA |
3953 | int ret = 0; |
3954 | ||
62755e35 JA |
3955 | cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr); |
3956 | switch (cancel_ret) { | |
3957 | case IO_WQ_CANCEL_OK: | |
3958 | ret = 0; | |
3959 | break; | |
3960 | case IO_WQ_CANCEL_RUNNING: | |
3961 | ret = -EALREADY; | |
3962 | break; | |
3963 | case IO_WQ_CANCEL_NOTFOUND: | |
3964 | ret = -ENOENT; | |
3965 | break; | |
3966 | } | |
3967 | ||
e977d6d3 JA |
3968 | return ret; |
3969 | } | |
3970 | ||
47f46768 JA |
3971 | static void io_async_find_and_cancel(struct io_ring_ctx *ctx, |
3972 | struct io_kiocb *req, __u64 sqe_addr, | |
b0dd8a41 | 3973 | struct io_kiocb **nxt, int success_ret) |
47f46768 JA |
3974 | { |
3975 | unsigned long flags; | |
3976 | int ret; | |
3977 | ||
3978 | ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr); | |
3979 | if (ret != -ENOENT) { | |
3980 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
3981 | goto done; | |
3982 | } | |
3983 | ||
3984 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
3985 | ret = io_timeout_cancel(ctx, sqe_addr); | |
3986 | if (ret != -ENOENT) | |
3987 | goto done; | |
3988 | ret = io_poll_cancel(ctx, sqe_addr); | |
3989 | done: | |
b0dd8a41 JA |
3990 | if (!ret) |
3991 | ret = success_ret; | |
47f46768 JA |
3992 | io_cqring_fill_event(req, ret); |
3993 | io_commit_cqring(ctx); | |
3994 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
3995 | io_cqring_ev_posted(ctx); | |
3996 | ||
4e88d6e7 JA |
3997 | if (ret < 0) |
3998 | req_set_fail_links(req); | |
47f46768 JA |
3999 | io_put_req_find_next(req, nxt); |
4000 | } | |
4001 | ||
3529d8c2 JA |
4002 | static int io_async_cancel_prep(struct io_kiocb *req, |
4003 | const struct io_uring_sqe *sqe) | |
e977d6d3 | 4004 | { |
fbf23849 | 4005 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
e977d6d3 JA |
4006 | return -EINVAL; |
4007 | if (sqe->flags || sqe->ioprio || sqe->off || sqe->len || | |
4008 | sqe->cancel_flags) | |
4009 | return -EINVAL; | |
4010 | ||
fbf23849 JA |
4011 | req->cancel.addr = READ_ONCE(sqe->addr); |
4012 | return 0; | |
4013 | } | |
4014 | ||
4015 | static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt) | |
4016 | { | |
4017 | struct io_ring_ctx *ctx = req->ctx; | |
fbf23849 JA |
4018 | |
4019 | io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0); | |
5262f567 JA |
4020 | return 0; |
4021 | } | |
4022 | ||
05f3fb3c JA |
4023 | static int io_files_update_prep(struct io_kiocb *req, |
4024 | const struct io_uring_sqe *sqe) | |
4025 | { | |
4026 | if (sqe->flags || sqe->ioprio || sqe->rw_flags) | |
4027 | return -EINVAL; | |
4028 | ||
4029 | req->files_update.offset = READ_ONCE(sqe->off); | |
4030 | req->files_update.nr_args = READ_ONCE(sqe->len); | |
4031 | if (!req->files_update.nr_args) | |
4032 | return -EINVAL; | |
4033 | req->files_update.arg = READ_ONCE(sqe->addr); | |
4034 | return 0; | |
4035 | } | |
4036 | ||
4037 | static int io_files_update(struct io_kiocb *req, bool force_nonblock) | |
fbf23849 JA |
4038 | { |
4039 | struct io_ring_ctx *ctx = req->ctx; | |
05f3fb3c JA |
4040 | struct io_uring_files_update up; |
4041 | int ret; | |
fbf23849 | 4042 | |
f86cd20c | 4043 | if (force_nonblock) |
05f3fb3c | 4044 | return -EAGAIN; |
05f3fb3c JA |
4045 | |
4046 | up.offset = req->files_update.offset; | |
4047 | up.fds = req->files_update.arg; | |
4048 | ||
4049 | mutex_lock(&ctx->uring_lock); | |
4050 | ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args); | |
4051 | mutex_unlock(&ctx->uring_lock); | |
4052 | ||
4053 | if (ret < 0) | |
4054 | req_set_fail_links(req); | |
4055 | io_cqring_add_event(req, ret); | |
4056 | io_put_req(req); | |
5262f567 JA |
4057 | return 0; |
4058 | } | |
4059 | ||
3529d8c2 JA |
4060 | static int io_req_defer_prep(struct io_kiocb *req, |
4061 | const struct io_uring_sqe *sqe) | |
f67676d1 | 4062 | { |
e781573e | 4063 | ssize_t ret = 0; |
f67676d1 | 4064 | |
f86cd20c JA |
4065 | if (io_op_defs[req->opcode].file_table) { |
4066 | ret = io_grab_files(req); | |
4067 | if (unlikely(ret)) | |
4068 | return ret; | |
4069 | } | |
4070 | ||
cccf0ee8 JA |
4071 | io_req_work_grab_env(req, &io_op_defs[req->opcode]); |
4072 | ||
d625c6ee | 4073 | switch (req->opcode) { |
e781573e JA |
4074 | case IORING_OP_NOP: |
4075 | break; | |
f67676d1 JA |
4076 | case IORING_OP_READV: |
4077 | case IORING_OP_READ_FIXED: | |
3a6820f2 | 4078 | case IORING_OP_READ: |
3529d8c2 | 4079 | ret = io_read_prep(req, sqe, true); |
f67676d1 JA |
4080 | break; |
4081 | case IORING_OP_WRITEV: | |
4082 | case IORING_OP_WRITE_FIXED: | |
3a6820f2 | 4083 | case IORING_OP_WRITE: |
3529d8c2 | 4084 | ret = io_write_prep(req, sqe, true); |
f67676d1 | 4085 | break; |
0969e783 | 4086 | case IORING_OP_POLL_ADD: |
3529d8c2 | 4087 | ret = io_poll_add_prep(req, sqe); |
0969e783 JA |
4088 | break; |
4089 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 | 4090 | ret = io_poll_remove_prep(req, sqe); |
0969e783 | 4091 | break; |
8ed8d3c3 | 4092 | case IORING_OP_FSYNC: |
3529d8c2 | 4093 | ret = io_prep_fsync(req, sqe); |
8ed8d3c3 JA |
4094 | break; |
4095 | case IORING_OP_SYNC_FILE_RANGE: | |
3529d8c2 | 4096 | ret = io_prep_sfr(req, sqe); |
8ed8d3c3 | 4097 | break; |
03b1230c | 4098 | case IORING_OP_SENDMSG: |
fddaface | 4099 | case IORING_OP_SEND: |
3529d8c2 | 4100 | ret = io_sendmsg_prep(req, sqe); |
03b1230c JA |
4101 | break; |
4102 | case IORING_OP_RECVMSG: | |
fddaface | 4103 | case IORING_OP_RECV: |
3529d8c2 | 4104 | ret = io_recvmsg_prep(req, sqe); |
03b1230c | 4105 | break; |
f499a021 | 4106 | case IORING_OP_CONNECT: |
3529d8c2 | 4107 | ret = io_connect_prep(req, sqe); |
f499a021 | 4108 | break; |
2d28390a | 4109 | case IORING_OP_TIMEOUT: |
3529d8c2 | 4110 | ret = io_timeout_prep(req, sqe, false); |
b7bb4f7d | 4111 | break; |
b29472ee | 4112 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 | 4113 | ret = io_timeout_remove_prep(req, sqe); |
b29472ee | 4114 | break; |
fbf23849 | 4115 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 | 4116 | ret = io_async_cancel_prep(req, sqe); |
fbf23849 | 4117 | break; |
2d28390a | 4118 | case IORING_OP_LINK_TIMEOUT: |
3529d8c2 | 4119 | ret = io_timeout_prep(req, sqe, true); |
b7bb4f7d | 4120 | break; |
8ed8d3c3 | 4121 | case IORING_OP_ACCEPT: |
3529d8c2 | 4122 | ret = io_accept_prep(req, sqe); |
8ed8d3c3 | 4123 | break; |
d63d1b5e JA |
4124 | case IORING_OP_FALLOCATE: |
4125 | ret = io_fallocate_prep(req, sqe); | |
4126 | break; | |
15b71abe JA |
4127 | case IORING_OP_OPENAT: |
4128 | ret = io_openat_prep(req, sqe); | |
4129 | break; | |
b5dba59e JA |
4130 | case IORING_OP_CLOSE: |
4131 | ret = io_close_prep(req, sqe); | |
4132 | break; | |
05f3fb3c JA |
4133 | case IORING_OP_FILES_UPDATE: |
4134 | ret = io_files_update_prep(req, sqe); | |
4135 | break; | |
eddc7ef5 JA |
4136 | case IORING_OP_STATX: |
4137 | ret = io_statx_prep(req, sqe); | |
4138 | break; | |
4840e418 JA |
4139 | case IORING_OP_FADVISE: |
4140 | ret = io_fadvise_prep(req, sqe); | |
4141 | break; | |
c1ca757b JA |
4142 | case IORING_OP_MADVISE: |
4143 | ret = io_madvise_prep(req, sqe); | |
4144 | break; | |
cebdb986 JA |
4145 | case IORING_OP_OPENAT2: |
4146 | ret = io_openat2_prep(req, sqe); | |
4147 | break; | |
3e4827b0 JA |
4148 | case IORING_OP_EPOLL_CTL: |
4149 | ret = io_epoll_ctl_prep(req, sqe); | |
4150 | break; | |
f67676d1 | 4151 | default: |
e781573e JA |
4152 | printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", |
4153 | req->opcode); | |
4154 | ret = -EINVAL; | |
b7bb4f7d | 4155 | break; |
f67676d1 JA |
4156 | } |
4157 | ||
b7bb4f7d | 4158 | return ret; |
f67676d1 JA |
4159 | } |
4160 | ||
3529d8c2 | 4161 | static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
de0617e4 | 4162 | { |
a197f664 | 4163 | struct io_ring_ctx *ctx = req->ctx; |
f67676d1 | 4164 | int ret; |
de0617e4 | 4165 | |
9d858b21 BL |
4166 | /* Still need defer if there is pending req in defer list. */ |
4167 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) | |
de0617e4 JA |
4168 | return 0; |
4169 | ||
3529d8c2 | 4170 | if (!req->io && io_alloc_async_ctx(req)) |
de0617e4 JA |
4171 | return -EAGAIN; |
4172 | ||
3529d8c2 | 4173 | ret = io_req_defer_prep(req, sqe); |
b7bb4f7d | 4174 | if (ret < 0) |
2d28390a | 4175 | return ret; |
2d28390a | 4176 | |
de0617e4 | 4177 | spin_lock_irq(&ctx->completion_lock); |
9d858b21 | 4178 | if (!req_need_defer(req) && list_empty(&ctx->defer_list)) { |
de0617e4 | 4179 | spin_unlock_irq(&ctx->completion_lock); |
de0617e4 JA |
4180 | return 0; |
4181 | } | |
4182 | ||
915967f6 | 4183 | trace_io_uring_defer(ctx, req, req->user_data); |
de0617e4 JA |
4184 | list_add_tail(&req->list, &ctx->defer_list); |
4185 | spin_unlock_irq(&ctx->completion_lock); | |
4186 | return -EIOCBQUEUED; | |
4187 | } | |
4188 | ||
3529d8c2 JA |
4189 | static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
4190 | struct io_kiocb **nxt, bool force_nonblock) | |
2b188cc1 | 4191 | { |
a197f664 | 4192 | struct io_ring_ctx *ctx = req->ctx; |
d625c6ee | 4193 | int ret; |
2b188cc1 | 4194 | |
d625c6ee | 4195 | switch (req->opcode) { |
2b188cc1 | 4196 | case IORING_OP_NOP: |
78e19bbe | 4197 | ret = io_nop(req); |
2b188cc1 JA |
4198 | break; |
4199 | case IORING_OP_READV: | |
edafccee | 4200 | case IORING_OP_READ_FIXED: |
3a6820f2 | 4201 | case IORING_OP_READ: |
3529d8c2 JA |
4202 | if (sqe) { |
4203 | ret = io_read_prep(req, sqe, force_nonblock); | |
4204 | if (ret < 0) | |
4205 | break; | |
4206 | } | |
267bc904 | 4207 | ret = io_read(req, nxt, force_nonblock); |
edafccee | 4208 | break; |
3529d8c2 | 4209 | case IORING_OP_WRITEV: |
edafccee | 4210 | case IORING_OP_WRITE_FIXED: |
3a6820f2 | 4211 | case IORING_OP_WRITE: |
3529d8c2 JA |
4212 | if (sqe) { |
4213 | ret = io_write_prep(req, sqe, force_nonblock); | |
4214 | if (ret < 0) | |
4215 | break; | |
4216 | } | |
267bc904 | 4217 | ret = io_write(req, nxt, force_nonblock); |
2b188cc1 | 4218 | break; |
c992fe29 | 4219 | case IORING_OP_FSYNC: |
3529d8c2 JA |
4220 | if (sqe) { |
4221 | ret = io_prep_fsync(req, sqe); | |
4222 | if (ret < 0) | |
4223 | break; | |
4224 | } | |
fc4df999 | 4225 | ret = io_fsync(req, nxt, force_nonblock); |
c992fe29 | 4226 | break; |
221c5eb2 | 4227 | case IORING_OP_POLL_ADD: |
3529d8c2 JA |
4228 | if (sqe) { |
4229 | ret = io_poll_add_prep(req, sqe); | |
4230 | if (ret) | |
4231 | break; | |
4232 | } | |
fc4df999 | 4233 | ret = io_poll_add(req, nxt); |
221c5eb2 JA |
4234 | break; |
4235 | case IORING_OP_POLL_REMOVE: | |
3529d8c2 JA |
4236 | if (sqe) { |
4237 | ret = io_poll_remove_prep(req, sqe); | |
4238 | if (ret < 0) | |
4239 | break; | |
4240 | } | |
fc4df999 | 4241 | ret = io_poll_remove(req); |
221c5eb2 | 4242 | break; |
5d17b4a4 | 4243 | case IORING_OP_SYNC_FILE_RANGE: |
3529d8c2 JA |
4244 | if (sqe) { |
4245 | ret = io_prep_sfr(req, sqe); | |
4246 | if (ret < 0) | |
4247 | break; | |
4248 | } | |
fc4df999 | 4249 | ret = io_sync_file_range(req, nxt, force_nonblock); |
5d17b4a4 | 4250 | break; |
0fa03c62 | 4251 | case IORING_OP_SENDMSG: |
fddaface | 4252 | case IORING_OP_SEND: |
3529d8c2 JA |
4253 | if (sqe) { |
4254 | ret = io_sendmsg_prep(req, sqe); | |
4255 | if (ret < 0) | |
4256 | break; | |
4257 | } | |
fddaface JA |
4258 | if (req->opcode == IORING_OP_SENDMSG) |
4259 | ret = io_sendmsg(req, nxt, force_nonblock); | |
4260 | else | |
4261 | ret = io_send(req, nxt, force_nonblock); | |
0fa03c62 | 4262 | break; |
aa1fa28f | 4263 | case IORING_OP_RECVMSG: |
fddaface | 4264 | case IORING_OP_RECV: |
3529d8c2 JA |
4265 | if (sqe) { |
4266 | ret = io_recvmsg_prep(req, sqe); | |
4267 | if (ret) | |
4268 | break; | |
4269 | } | |
fddaface JA |
4270 | if (req->opcode == IORING_OP_RECVMSG) |
4271 | ret = io_recvmsg(req, nxt, force_nonblock); | |
4272 | else | |
4273 | ret = io_recv(req, nxt, force_nonblock); | |
aa1fa28f | 4274 | break; |
5262f567 | 4275 | case IORING_OP_TIMEOUT: |
3529d8c2 JA |
4276 | if (sqe) { |
4277 | ret = io_timeout_prep(req, sqe, false); | |
4278 | if (ret) | |
4279 | break; | |
4280 | } | |
fc4df999 | 4281 | ret = io_timeout(req); |
5262f567 | 4282 | break; |
11365043 | 4283 | case IORING_OP_TIMEOUT_REMOVE: |
3529d8c2 JA |
4284 | if (sqe) { |
4285 | ret = io_timeout_remove_prep(req, sqe); | |
4286 | if (ret) | |
4287 | break; | |
4288 | } | |
fc4df999 | 4289 | ret = io_timeout_remove(req); |
11365043 | 4290 | break; |
17f2fe35 | 4291 | case IORING_OP_ACCEPT: |
3529d8c2 JA |
4292 | if (sqe) { |
4293 | ret = io_accept_prep(req, sqe); | |
4294 | if (ret) | |
4295 | break; | |
4296 | } | |
fc4df999 | 4297 | ret = io_accept(req, nxt, force_nonblock); |
17f2fe35 | 4298 | break; |
f8e85cf2 | 4299 | case IORING_OP_CONNECT: |
3529d8c2 JA |
4300 | if (sqe) { |
4301 | ret = io_connect_prep(req, sqe); | |
4302 | if (ret) | |
4303 | break; | |
4304 | } | |
fc4df999 | 4305 | ret = io_connect(req, nxt, force_nonblock); |
f8e85cf2 | 4306 | break; |
62755e35 | 4307 | case IORING_OP_ASYNC_CANCEL: |
3529d8c2 JA |
4308 | if (sqe) { |
4309 | ret = io_async_cancel_prep(req, sqe); | |
4310 | if (ret) | |
4311 | break; | |
4312 | } | |
fc4df999 | 4313 | ret = io_async_cancel(req, nxt); |
62755e35 | 4314 | break; |
d63d1b5e JA |
4315 | case IORING_OP_FALLOCATE: |
4316 | if (sqe) { | |
4317 | ret = io_fallocate_prep(req, sqe); | |
4318 | if (ret) | |
4319 | break; | |
4320 | } | |
4321 | ret = io_fallocate(req, nxt, force_nonblock); | |
4322 | break; | |
15b71abe JA |
4323 | case IORING_OP_OPENAT: |
4324 | if (sqe) { | |
4325 | ret = io_openat_prep(req, sqe); | |
4326 | if (ret) | |
4327 | break; | |
4328 | } | |
4329 | ret = io_openat(req, nxt, force_nonblock); | |
4330 | break; | |
b5dba59e JA |
4331 | case IORING_OP_CLOSE: |
4332 | if (sqe) { | |
4333 | ret = io_close_prep(req, sqe); | |
4334 | if (ret) | |
4335 | break; | |
4336 | } | |
4337 | ret = io_close(req, nxt, force_nonblock); | |
4338 | break; | |
05f3fb3c JA |
4339 | case IORING_OP_FILES_UPDATE: |
4340 | if (sqe) { | |
4341 | ret = io_files_update_prep(req, sqe); | |
4342 | if (ret) | |
4343 | break; | |
4344 | } | |
4345 | ret = io_files_update(req, force_nonblock); | |
4346 | break; | |
eddc7ef5 JA |
4347 | case IORING_OP_STATX: |
4348 | if (sqe) { | |
4349 | ret = io_statx_prep(req, sqe); | |
4350 | if (ret) | |
4351 | break; | |
4352 | } | |
4353 | ret = io_statx(req, nxt, force_nonblock); | |
4354 | break; | |
4840e418 JA |
4355 | case IORING_OP_FADVISE: |
4356 | if (sqe) { | |
4357 | ret = io_fadvise_prep(req, sqe); | |
4358 | if (ret) | |
4359 | break; | |
4360 | } | |
4361 | ret = io_fadvise(req, nxt, force_nonblock); | |
4362 | break; | |
c1ca757b JA |
4363 | case IORING_OP_MADVISE: |
4364 | if (sqe) { | |
4365 | ret = io_madvise_prep(req, sqe); | |
4366 | if (ret) | |
4367 | break; | |
4368 | } | |
4369 | ret = io_madvise(req, nxt, force_nonblock); | |
4370 | break; | |
cebdb986 JA |
4371 | case IORING_OP_OPENAT2: |
4372 | if (sqe) { | |
4373 | ret = io_openat2_prep(req, sqe); | |
4374 | if (ret) | |
4375 | break; | |
4376 | } | |
4377 | ret = io_openat2(req, nxt, force_nonblock); | |
4378 | break; | |
3e4827b0 JA |
4379 | case IORING_OP_EPOLL_CTL: |
4380 | if (sqe) { | |
4381 | ret = io_epoll_ctl_prep(req, sqe); | |
4382 | if (ret) | |
4383 | break; | |
4384 | } | |
4385 | ret = io_epoll_ctl(req, nxt, force_nonblock); | |
4386 | break; | |
2b188cc1 JA |
4387 | default: |
4388 | ret = -EINVAL; | |
4389 | break; | |
4390 | } | |
4391 | ||
def596e9 JA |
4392 | if (ret) |
4393 | return ret; | |
4394 | ||
4395 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
11ba820b JA |
4396 | const bool in_async = io_wq_current_is_worker(); |
4397 | ||
9e645e11 | 4398 | if (req->result == -EAGAIN) |
def596e9 JA |
4399 | return -EAGAIN; |
4400 | ||
11ba820b JA |
4401 | /* workqueue context doesn't hold uring_lock, grab it now */ |
4402 | if (in_async) | |
4403 | mutex_lock(&ctx->uring_lock); | |
4404 | ||
def596e9 | 4405 | io_iopoll_req_issued(req); |
11ba820b JA |
4406 | |
4407 | if (in_async) | |
4408 | mutex_unlock(&ctx->uring_lock); | |
def596e9 JA |
4409 | } |
4410 | ||
4411 | return 0; | |
2b188cc1 JA |
4412 | } |
4413 | ||
561fb04a | 4414 | static void io_wq_submit_work(struct io_wq_work **workptr) |
2b188cc1 | 4415 | { |
561fb04a | 4416 | struct io_wq_work *work = *workptr; |
2b188cc1 | 4417 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
561fb04a JA |
4418 | struct io_kiocb *nxt = NULL; |
4419 | int ret = 0; | |
2b188cc1 | 4420 | |
0c9d5ccd JA |
4421 | /* if NO_CANCEL is set, we must still run the work */ |
4422 | if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) == | |
4423 | IO_WQ_WORK_CANCEL) { | |
561fb04a | 4424 | ret = -ECANCELED; |
0c9d5ccd | 4425 | } |
31b51510 | 4426 | |
561fb04a | 4427 | if (!ret) { |
cf6fd4bd | 4428 | req->in_async = true; |
561fb04a | 4429 | do { |
3529d8c2 | 4430 | ret = io_issue_sqe(req, NULL, &nxt, false); |
561fb04a JA |
4431 | /* |
4432 | * We can get EAGAIN for polled IO even though we're | |
4433 | * forcing a sync submission from here, since we can't | |
4434 | * wait for request slots on the block side. | |
4435 | */ | |
4436 | if (ret != -EAGAIN) | |
4437 | break; | |
4438 | cond_resched(); | |
4439 | } while (1); | |
4440 | } | |
31b51510 | 4441 | |
561fb04a | 4442 | /* drop submission reference */ |
ec9c02ad | 4443 | io_put_req(req); |
817869d2 | 4444 | |
561fb04a | 4445 | if (ret) { |
4e88d6e7 | 4446 | req_set_fail_links(req); |
78e19bbe | 4447 | io_cqring_add_event(req, ret); |
817869d2 | 4448 | io_put_req(req); |
edafccee | 4449 | } |
2b188cc1 | 4450 | |
561fb04a | 4451 | /* if a dependent link is ready, pass it back */ |
78912934 JA |
4452 | if (!ret && nxt) |
4453 | io_wq_assign_next(workptr, nxt); | |
2b188cc1 JA |
4454 | } |
4455 | ||
15b71abe | 4456 | static int io_req_needs_file(struct io_kiocb *req, int fd) |
9e3aa61a | 4457 | { |
d3656344 | 4458 | if (!io_op_defs[req->opcode].needs_file) |
9e3aa61a | 4459 | return 0; |
d3656344 JA |
4460 | if (fd == -1 && io_op_defs[req->opcode].fd_non_neg) |
4461 | return 0; | |
4462 | return 1; | |
09bb8394 JA |
4463 | } |
4464 | ||
65e19f54 JA |
4465 | static inline struct file *io_file_from_index(struct io_ring_ctx *ctx, |
4466 | int index) | |
4467 | { | |
4468 | struct fixed_file_table *table; | |
4469 | ||
05f3fb3c JA |
4470 | table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT]; |
4471 | return table->files[index & IORING_FILE_TABLE_MASK];; | |
65e19f54 JA |
4472 | } |
4473 | ||
3529d8c2 JA |
4474 | static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req, |
4475 | const struct io_uring_sqe *sqe) | |
09bb8394 | 4476 | { |
a197f664 | 4477 | struct io_ring_ctx *ctx = req->ctx; |
09bb8394 | 4478 | unsigned flags; |
d3656344 | 4479 | int fd; |
09bb8394 | 4480 | |
3529d8c2 JA |
4481 | flags = READ_ONCE(sqe->flags); |
4482 | fd = READ_ONCE(sqe->fd); | |
09bb8394 | 4483 | |
d3656344 JA |
4484 | if (!io_req_needs_file(req, fd)) |
4485 | return 0; | |
09bb8394 JA |
4486 | |
4487 | if (flags & IOSQE_FIXED_FILE) { | |
05f3fb3c | 4488 | if (unlikely(!ctx->file_data || |
09bb8394 JA |
4489 | (unsigned) fd >= ctx->nr_user_files)) |
4490 | return -EBADF; | |
b7620121 | 4491 | fd = array_index_nospec(fd, ctx->nr_user_files); |
65e19f54 JA |
4492 | req->file = io_file_from_index(ctx, fd); |
4493 | if (!req->file) | |
08a45173 | 4494 | return -EBADF; |
09bb8394 | 4495 | req->flags |= REQ_F_FIXED_FILE; |
05f3fb3c | 4496 | percpu_ref_get(&ctx->file_data->refs); |
09bb8394 | 4497 | } else { |
cf6fd4bd | 4498 | if (req->needs_fixed_file) |
09bb8394 | 4499 | return -EBADF; |
c826bd7a | 4500 | trace_io_uring_file_get(ctx, fd); |
09bb8394 JA |
4501 | req->file = io_file_get(state, fd); |
4502 | if (unlikely(!req->file)) | |
4503 | return -EBADF; | |
4504 | } | |
4505 | ||
4506 | return 0; | |
4507 | } | |
4508 | ||
a197f664 | 4509 | static int io_grab_files(struct io_kiocb *req) |
fcb323cc JA |
4510 | { |
4511 | int ret = -EBADF; | |
a197f664 | 4512 | struct io_ring_ctx *ctx = req->ctx; |
fcb323cc | 4513 | |
f86cd20c JA |
4514 | if (req->work.files) |
4515 | return 0; | |
b14cca0c | 4516 | if (!ctx->ring_file) |
b5dba59e JA |
4517 | return -EBADF; |
4518 | ||
fcb323cc JA |
4519 | rcu_read_lock(); |
4520 | spin_lock_irq(&ctx->inflight_lock); | |
4521 | /* | |
4522 | * We use the f_ops->flush() handler to ensure that we can flush | |
4523 | * out work accessing these files if the fd is closed. Check if | |
4524 | * the fd has changed since we started down this path, and disallow | |
4525 | * this operation if it has. | |
4526 | */ | |
b14cca0c | 4527 | if (fcheck(ctx->ring_fd) == ctx->ring_file) { |
fcb323cc JA |
4528 | list_add(&req->inflight_entry, &ctx->inflight_list); |
4529 | req->flags |= REQ_F_INFLIGHT; | |
4530 | req->work.files = current->files; | |
4531 | ret = 0; | |
4532 | } | |
4533 | spin_unlock_irq(&ctx->inflight_lock); | |
4534 | rcu_read_unlock(); | |
4535 | ||
4536 | return ret; | |
4537 | } | |
4538 | ||
2665abfd | 4539 | static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer) |
2b188cc1 | 4540 | { |
ad8a48ac JA |
4541 | struct io_timeout_data *data = container_of(timer, |
4542 | struct io_timeout_data, timer); | |
4543 | struct io_kiocb *req = data->req; | |
2665abfd JA |
4544 | struct io_ring_ctx *ctx = req->ctx; |
4545 | struct io_kiocb *prev = NULL; | |
4546 | unsigned long flags; | |
2665abfd JA |
4547 | |
4548 | spin_lock_irqsave(&ctx->completion_lock, flags); | |
4549 | ||
4550 | /* | |
4551 | * We don't expect the list to be empty, that will only happen if we | |
4552 | * race with the completion of the linked work. | |
4553 | */ | |
4493233e PB |
4554 | if (!list_empty(&req->link_list)) { |
4555 | prev = list_entry(req->link_list.prev, struct io_kiocb, | |
4556 | link_list); | |
5d960724 | 4557 | if (refcount_inc_not_zero(&prev->refs)) { |
4493233e | 4558 | list_del_init(&req->link_list); |
5d960724 JA |
4559 | prev->flags &= ~REQ_F_LINK_TIMEOUT; |
4560 | } else | |
76a46e06 | 4561 | prev = NULL; |
2665abfd JA |
4562 | } |
4563 | ||
4564 | spin_unlock_irqrestore(&ctx->completion_lock, flags); | |
4565 | ||
4566 | if (prev) { | |
4e88d6e7 | 4567 | req_set_fail_links(prev); |
b0dd8a41 JA |
4568 | io_async_find_and_cancel(ctx, req, prev->user_data, NULL, |
4569 | -ETIME); | |
76a46e06 | 4570 | io_put_req(prev); |
47f46768 JA |
4571 | } else { |
4572 | io_cqring_add_event(req, -ETIME); | |
4573 | io_put_req(req); | |
2665abfd | 4574 | } |
2665abfd JA |
4575 | return HRTIMER_NORESTART; |
4576 | } | |
4577 | ||
ad8a48ac | 4578 | static void io_queue_linked_timeout(struct io_kiocb *req) |
2665abfd | 4579 | { |
76a46e06 | 4580 | struct io_ring_ctx *ctx = req->ctx; |
2665abfd | 4581 | |
76a46e06 JA |
4582 | /* |
4583 | * If the list is now empty, then our linked request finished before | |
4584 | * we got a chance to setup the timer | |
4585 | */ | |
4586 | spin_lock_irq(&ctx->completion_lock); | |
4493233e | 4587 | if (!list_empty(&req->link_list)) { |
2d28390a | 4588 | struct io_timeout_data *data = &req->io->timeout; |
94ae5e77 | 4589 | |
ad8a48ac JA |
4590 | data->timer.function = io_link_timeout_fn; |
4591 | hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), | |
4592 | data->mode); | |
2665abfd | 4593 | } |
76a46e06 | 4594 | spin_unlock_irq(&ctx->completion_lock); |
2665abfd | 4595 | |
2665abfd | 4596 | /* drop submission reference */ |
76a46e06 JA |
4597 | io_put_req(req); |
4598 | } | |
2665abfd | 4599 | |
ad8a48ac | 4600 | static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req) |
2665abfd JA |
4601 | { |
4602 | struct io_kiocb *nxt; | |
4603 | ||
4604 | if (!(req->flags & REQ_F_LINK)) | |
4605 | return NULL; | |
4606 | ||
4493233e PB |
4607 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
4608 | link_list); | |
d625c6ee | 4609 | if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT) |
76a46e06 | 4610 | return NULL; |
2665abfd | 4611 | |
76a46e06 | 4612 | req->flags |= REQ_F_LINK_TIMEOUT; |
76a46e06 | 4613 | return nxt; |
2665abfd JA |
4614 | } |
4615 | ||
3529d8c2 | 4616 | static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
2b188cc1 | 4617 | { |
4a0a7a18 | 4618 | struct io_kiocb *linked_timeout; |
f9bd67f6 | 4619 | struct io_kiocb *nxt = NULL; |
e0c5c576 | 4620 | int ret; |
2b188cc1 | 4621 | |
4a0a7a18 JA |
4622 | again: |
4623 | linked_timeout = io_prep_linked_timeout(req); | |
4624 | ||
3529d8c2 | 4625 | ret = io_issue_sqe(req, sqe, &nxt, true); |
491381ce JA |
4626 | |
4627 | /* | |
4628 | * We async punt it if the file wasn't marked NOWAIT, or if the file | |
4629 | * doesn't support non-blocking read/write attempts | |
4630 | */ | |
4631 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || | |
4632 | (req->flags & REQ_F_MUST_PUNT))) { | |
86a761f8 | 4633 | punt: |
f86cd20c | 4634 | if (io_op_defs[req->opcode].file_table) { |
bbad27b2 PB |
4635 | ret = io_grab_files(req); |
4636 | if (ret) | |
4637 | goto err; | |
2b188cc1 | 4638 | } |
bbad27b2 PB |
4639 | |
4640 | /* | |
4641 | * Queued up for async execution, worker will release | |
4642 | * submit reference when the iocb is actually submitted. | |
4643 | */ | |
4644 | io_queue_async_work(req); | |
4a0a7a18 | 4645 | goto done_req; |
2b188cc1 | 4646 | } |
e65ef56d | 4647 | |
fcb323cc | 4648 | err: |
76a46e06 | 4649 | /* drop submission reference */ |
ec9c02ad | 4650 | io_put_req(req); |
e65ef56d | 4651 | |
f9bd67f6 | 4652 | if (linked_timeout) { |
76a46e06 | 4653 | if (!ret) |
f9bd67f6 | 4654 | io_queue_linked_timeout(linked_timeout); |
76a46e06 | 4655 | else |
f9bd67f6 | 4656 | io_put_req(linked_timeout); |
76a46e06 JA |
4657 | } |
4658 | ||
e65ef56d | 4659 | /* and drop final reference, if we failed */ |
9e645e11 | 4660 | if (ret) { |
78e19bbe | 4661 | io_cqring_add_event(req, ret); |
4e88d6e7 | 4662 | req_set_fail_links(req); |
e65ef56d | 4663 | io_put_req(req); |
9e645e11 | 4664 | } |
4a0a7a18 JA |
4665 | done_req: |
4666 | if (nxt) { | |
4667 | req = nxt; | |
4668 | nxt = NULL; | |
86a761f8 PB |
4669 | |
4670 | if (req->flags & REQ_F_FORCE_ASYNC) | |
4671 | goto punt; | |
4a0a7a18 JA |
4672 | goto again; |
4673 | } | |
2b188cc1 JA |
4674 | } |
4675 | ||
3529d8c2 | 4676 | static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
4fe2c963 JL |
4677 | { |
4678 | int ret; | |
4679 | ||
3529d8c2 | 4680 | ret = io_req_defer(req, sqe); |
4fe2c963 JL |
4681 | if (ret) { |
4682 | if (ret != -EIOCBQUEUED) { | |
1118591a | 4683 | fail_req: |
78e19bbe | 4684 | io_cqring_add_event(req, ret); |
4e88d6e7 | 4685 | req_set_fail_links(req); |
78e19bbe | 4686 | io_double_put_req(req); |
4fe2c963 | 4687 | } |
2550878f | 4688 | } else if (req->flags & REQ_F_FORCE_ASYNC) { |
1118591a PB |
4689 | ret = io_req_defer_prep(req, sqe); |
4690 | if (unlikely(ret < 0)) | |
4691 | goto fail_req; | |
ce35a47a JA |
4692 | /* |
4693 | * Never try inline submit of IOSQE_ASYNC is set, go straight | |
4694 | * to async execution. | |
4695 | */ | |
4696 | req->work.flags |= IO_WQ_WORK_CONCURRENT; | |
4697 | io_queue_async_work(req); | |
4698 | } else { | |
3529d8c2 | 4699 | __io_queue_sqe(req, sqe); |
ce35a47a | 4700 | } |
4fe2c963 JL |
4701 | } |
4702 | ||
1b4a51b6 | 4703 | static inline void io_queue_link_head(struct io_kiocb *req) |
4fe2c963 | 4704 | { |
94ae5e77 | 4705 | if (unlikely(req->flags & REQ_F_FAIL_LINK)) { |
1b4a51b6 PB |
4706 | io_cqring_add_event(req, -ECANCELED); |
4707 | io_double_put_req(req); | |
4708 | } else | |
3529d8c2 | 4709 | io_queue_sqe(req, NULL); |
4fe2c963 JL |
4710 | } |
4711 | ||
4e88d6e7 | 4712 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \ |
ce35a47a | 4713 | IOSQE_IO_HARDLINK | IOSQE_ASYNC) |
9e645e11 | 4714 | |
3529d8c2 JA |
4715 | static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
4716 | struct io_submit_state *state, struct io_kiocb **link) | |
9e645e11 | 4717 | { |
75c6a039 | 4718 | const struct cred *old_creds = NULL; |
a197f664 | 4719 | struct io_ring_ctx *ctx = req->ctx; |
32fe525b | 4720 | unsigned int sqe_flags; |
75c6a039 | 4721 | int ret, id; |
9e645e11 | 4722 | |
32fe525b | 4723 | sqe_flags = READ_ONCE(sqe->flags); |
9e645e11 JA |
4724 | |
4725 | /* enforce forwards compatibility on users */ | |
32fe525b | 4726 | if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) { |
9e645e11 | 4727 | ret = -EINVAL; |
196be95c | 4728 | goto err_req; |
9e645e11 JA |
4729 | } |
4730 | ||
75c6a039 JA |
4731 | id = READ_ONCE(sqe->personality); |
4732 | if (id) { | |
4733 | const struct cred *personality_creds; | |
4734 | ||
4735 | personality_creds = idr_find(&ctx->personality_idr, id); | |
4736 | if (unlikely(!personality_creds)) { | |
4737 | ret = -EINVAL; | |
4738 | goto err_req; | |
4739 | } | |
4740 | old_creds = override_creds(personality_creds); | |
4741 | } | |
4742 | ||
6b47ee6e PB |
4743 | /* same numerical values with corresponding REQ_F_*, safe to copy */ |
4744 | req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK| | |
4745 | IOSQE_ASYNC); | |
9e645e11 | 4746 | |
3529d8c2 | 4747 | ret = io_req_set_file(state, req, sqe); |
9e645e11 JA |
4748 | if (unlikely(ret)) { |
4749 | err_req: | |
78e19bbe JA |
4750 | io_cqring_add_event(req, ret); |
4751 | io_double_put_req(req); | |
75c6a039 JA |
4752 | if (old_creds) |
4753 | revert_creds(old_creds); | |
2e6e1fde | 4754 | return false; |
9e645e11 JA |
4755 | } |
4756 | ||
9e645e11 JA |
4757 | /* |
4758 | * If we already have a head request, queue this one for async | |
4759 | * submittal once the head completes. If we don't have a head but | |
4760 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be | |
4761 | * submitted sync once the chain is complete. If none of those | |
4762 | * conditions are true (normal request), then just queue it. | |
4763 | */ | |
4764 | if (*link) { | |
9d76377f | 4765 | struct io_kiocb *head = *link; |
4e88d6e7 | 4766 | |
8cdf2193 PB |
4767 | /* |
4768 | * Taking sequential execution of a link, draining both sides | |
4769 | * of the link also fullfils IOSQE_IO_DRAIN semantics for all | |
4770 | * requests in the link. So, it drains the head and the | |
4771 | * next after the link request. The last one is done via | |
4772 | * drain_next flag to persist the effect across calls. | |
4773 | */ | |
711be031 PB |
4774 | if (sqe_flags & IOSQE_IO_DRAIN) { |
4775 | head->flags |= REQ_F_IO_DRAIN; | |
4776 | ctx->drain_next = 1; | |
4777 | } | |
b7bb4f7d | 4778 | if (io_alloc_async_ctx(req)) { |
9e645e11 JA |
4779 | ret = -EAGAIN; |
4780 | goto err_req; | |
4781 | } | |
4782 | ||
3529d8c2 | 4783 | ret = io_req_defer_prep(req, sqe); |
2d28390a | 4784 | if (ret) { |
4e88d6e7 | 4785 | /* fail even hard links since we don't submit */ |
9d76377f | 4786 | head->flags |= REQ_F_FAIL_LINK; |
f67676d1 | 4787 | goto err_req; |
2d28390a | 4788 | } |
9d76377f PB |
4789 | trace_io_uring_link(ctx, req, head); |
4790 | list_add_tail(&req->link_list, &head->link_list); | |
32fe525b PB |
4791 | |
4792 | /* last request of a link, enqueue the link */ | |
4793 | if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) { | |
4794 | io_queue_link_head(head); | |
4795 | *link = NULL; | |
4796 | } | |
9e645e11 | 4797 | } else { |
711be031 PB |
4798 | if (unlikely(ctx->drain_next)) { |
4799 | req->flags |= REQ_F_IO_DRAIN; | |
4800 | req->ctx->drain_next = 0; | |
4801 | } | |
4802 | if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) { | |
4803 | req->flags |= REQ_F_LINK; | |
711be031 PB |
4804 | INIT_LIST_HEAD(&req->link_list); |
4805 | ret = io_req_defer_prep(req, sqe); | |
4806 | if (ret) | |
4807 | req->flags |= REQ_F_FAIL_LINK; | |
4808 | *link = req; | |
4809 | } else { | |
4810 | io_queue_sqe(req, sqe); | |
4811 | } | |
9e645e11 | 4812 | } |
2e6e1fde | 4813 | |
75c6a039 JA |
4814 | if (old_creds) |
4815 | revert_creds(old_creds); | |
2e6e1fde | 4816 | return true; |
9e645e11 JA |
4817 | } |
4818 | ||
9a56a232 JA |
4819 | /* |
4820 | * Batched submission is done, ensure local IO is flushed out. | |
4821 | */ | |
4822 | static void io_submit_state_end(struct io_submit_state *state) | |
4823 | { | |
4824 | blk_finish_plug(&state->plug); | |
3d6770fb | 4825 | io_file_put(state); |
2579f913 | 4826 | if (state->free_reqs) |
6c8a3134 | 4827 | kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs); |
9a56a232 JA |
4828 | } |
4829 | ||
4830 | /* | |
4831 | * Start submission side cache. | |
4832 | */ | |
4833 | static void io_submit_state_start(struct io_submit_state *state, | |
22efde59 | 4834 | unsigned int max_ios) |
9a56a232 JA |
4835 | { |
4836 | blk_start_plug(&state->plug); | |
2579f913 | 4837 | state->free_reqs = 0; |
9a56a232 JA |
4838 | state->file = NULL; |
4839 | state->ios_left = max_ios; | |
4840 | } | |
4841 | ||
2b188cc1 JA |
4842 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
4843 | { | |
75b28aff | 4844 | struct io_rings *rings = ctx->rings; |
2b188cc1 | 4845 | |
caf582c6 PB |
4846 | /* |
4847 | * Ensure any loads from the SQEs are done at this point, | |
4848 | * since once we write the new head, the application could | |
4849 | * write new data to them. | |
4850 | */ | |
4851 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); | |
2b188cc1 JA |
4852 | } |
4853 | ||
2b188cc1 | 4854 | /* |
3529d8c2 | 4855 | * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory |
2b188cc1 JA |
4856 | * that is mapped by userspace. This means that care needs to be taken to |
4857 | * ensure that reads are stable, as we cannot rely on userspace always | |
4858 | * being a good citizen. If members of the sqe are validated and then later | |
4859 | * used, it's important that those reads are done through READ_ONCE() to | |
4860 | * prevent a re-load down the line. | |
4861 | */ | |
3529d8c2 JA |
4862 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req, |
4863 | const struct io_uring_sqe **sqe_ptr) | |
2b188cc1 | 4864 | { |
75b28aff | 4865 | u32 *sq_array = ctx->sq_array; |
2b188cc1 JA |
4866 | unsigned head; |
4867 | ||
4868 | /* | |
4869 | * The cached sq head (or cq tail) serves two purposes: | |
4870 | * | |
4871 | * 1) allows us to batch the cost of updating the user visible | |
4872 | * head updates. | |
4873 | * 2) allows the kernel side to track the head on its own, even | |
4874 | * though the application is the one updating it. | |
4875 | */ | |
ee7d46d9 | 4876 | head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]); |
9835d6fa | 4877 | if (likely(head < ctx->sq_entries)) { |
cf6fd4bd PB |
4878 | /* |
4879 | * All io need record the previous position, if LINK vs DARIN, | |
4880 | * it can be used to mark the position of the first IO in the | |
4881 | * link list. | |
4882 | */ | |
4883 | req->sequence = ctx->cached_sq_head; | |
3529d8c2 JA |
4884 | *sqe_ptr = &ctx->sq_sqes[head]; |
4885 | req->opcode = READ_ONCE((*sqe_ptr)->opcode); | |
4886 | req->user_data = READ_ONCE((*sqe_ptr)->user_data); | |
2b188cc1 JA |
4887 | ctx->cached_sq_head++; |
4888 | return true; | |
4889 | } | |
4890 | ||
4891 | /* drop invalid entries */ | |
4892 | ctx->cached_sq_head++; | |
498ccd9e | 4893 | ctx->cached_sq_dropped++; |
ee7d46d9 | 4894 | WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped); |
2b188cc1 JA |
4895 | return false; |
4896 | } | |
4897 | ||
fb5ccc98 | 4898 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
ae9428ca PB |
4899 | struct file *ring_file, int ring_fd, |
4900 | struct mm_struct **mm, bool async) | |
6c271ce2 JA |
4901 | { |
4902 | struct io_submit_state state, *statep = NULL; | |
9e645e11 | 4903 | struct io_kiocb *link = NULL; |
9e645e11 | 4904 | int i, submitted = 0; |
95a1b3ff | 4905 | bool mm_fault = false; |
6c271ce2 | 4906 | |
c4a2ed72 | 4907 | /* if we have a backlog and couldn't flush it all, return BUSY */ |
ad3eb2c8 JA |
4908 | if (test_bit(0, &ctx->sq_check_overflow)) { |
4909 | if (!list_empty(&ctx->cq_overflow_list) && | |
4910 | !io_cqring_overflow_flush(ctx, false)) | |
4911 | return -EBUSY; | |
4912 | } | |
6c271ce2 | 4913 | |
ee7d46d9 PB |
4914 | /* make sure SQ entry isn't read before tail */ |
4915 | nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx)); | |
9ef4f124 | 4916 | |
2b85edfc PB |
4917 | if (!percpu_ref_tryget_many(&ctx->refs, nr)) |
4918 | return -EAGAIN; | |
6c271ce2 JA |
4919 | |
4920 | if (nr > IO_PLUG_THRESHOLD) { | |
22efde59 | 4921 | io_submit_state_start(&state, nr); |
6c271ce2 JA |
4922 | statep = &state; |
4923 | } | |
4924 | ||
b14cca0c PB |
4925 | ctx->ring_fd = ring_fd; |
4926 | ctx->ring_file = ring_file; | |
4927 | ||
6c271ce2 | 4928 | for (i = 0; i < nr; i++) { |
3529d8c2 | 4929 | const struct io_uring_sqe *sqe; |
196be95c | 4930 | struct io_kiocb *req; |
1cb1edb2 | 4931 | int err; |
fb5ccc98 | 4932 | |
196be95c PB |
4933 | req = io_get_req(ctx, statep); |
4934 | if (unlikely(!req)) { | |
4935 | if (!submitted) | |
4936 | submitted = -EAGAIN; | |
fb5ccc98 | 4937 | break; |
196be95c | 4938 | } |
3529d8c2 | 4939 | if (!io_get_sqring(ctx, req, &sqe)) { |
2b85edfc | 4940 | __io_req_do_free(req); |
196be95c PB |
4941 | break; |
4942 | } | |
fb5ccc98 | 4943 | |
d3656344 JA |
4944 | /* will complete beyond this point, count as submitted */ |
4945 | submitted++; | |
4946 | ||
4947 | if (unlikely(req->opcode >= IORING_OP_LAST)) { | |
1cb1edb2 PB |
4948 | err = -EINVAL; |
4949 | fail_req: | |
4950 | io_cqring_add_event(req, err); | |
d3656344 | 4951 | io_double_put_req(req); |
196be95c PB |
4952 | break; |
4953 | } | |
fb5ccc98 | 4954 | |
d3656344 | 4955 | if (io_op_defs[req->opcode].needs_mm && !*mm) { |
95a1b3ff | 4956 | mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm); |
1cb1edb2 PB |
4957 | if (unlikely(mm_fault)) { |
4958 | err = -EFAULT; | |
4959 | goto fail_req; | |
95a1b3ff | 4960 | } |
1cb1edb2 PB |
4961 | use_mm(ctx->sqo_mm); |
4962 | *mm = ctx->sqo_mm; | |
9e645e11 | 4963 | } |
9e645e11 | 4964 | |
cf6fd4bd PB |
4965 | req->in_async = async; |
4966 | req->needs_fixed_file = async; | |
354420f7 JA |
4967 | trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data, |
4968 | true, async); | |
3529d8c2 | 4969 | if (!io_submit_sqe(req, sqe, statep, &link)) |
2e6e1fde | 4970 | break; |
6c271ce2 JA |
4971 | } |
4972 | ||
9466f437 PB |
4973 | if (unlikely(submitted != nr)) { |
4974 | int ref_used = (submitted == -EAGAIN) ? 0 : submitted; | |
4975 | ||
4976 | percpu_ref_put_many(&ctx->refs, nr - ref_used); | |
4977 | } | |
9e645e11 | 4978 | if (link) |
1b4a51b6 | 4979 | io_queue_link_head(link); |
6c271ce2 JA |
4980 | if (statep) |
4981 | io_submit_state_end(&state); | |
4982 | ||
ae9428ca PB |
4983 | /* Commit SQ ring head once we've consumed and submitted all SQEs */ |
4984 | io_commit_sqring(ctx); | |
4985 | ||
6c271ce2 JA |
4986 | return submitted; |
4987 | } | |
4988 | ||
4989 | static int io_sq_thread(void *data) | |
4990 | { | |
6c271ce2 JA |
4991 | struct io_ring_ctx *ctx = data; |
4992 | struct mm_struct *cur_mm = NULL; | |
181e448d | 4993 | const struct cred *old_cred; |
6c271ce2 JA |
4994 | mm_segment_t old_fs; |
4995 | DEFINE_WAIT(wait); | |
4996 | unsigned inflight; | |
4997 | unsigned long timeout; | |
c1edbf5f | 4998 | int ret; |
6c271ce2 | 4999 | |
206aefde | 5000 | complete(&ctx->completions[1]); |
a4c0b3de | 5001 | |
6c271ce2 JA |
5002 | old_fs = get_fs(); |
5003 | set_fs(USER_DS); | |
181e448d | 5004 | old_cred = override_creds(ctx->creds); |
6c271ce2 | 5005 | |
c1edbf5f | 5006 | ret = timeout = inflight = 0; |
2bbcd6d3 | 5007 | while (!kthread_should_park()) { |
fb5ccc98 | 5008 | unsigned int to_submit; |
6c271ce2 JA |
5009 | |
5010 | if (inflight) { | |
5011 | unsigned nr_events = 0; | |
5012 | ||
5013 | if (ctx->flags & IORING_SETUP_IOPOLL) { | |
2b2ed975 JA |
5014 | /* |
5015 | * inflight is the count of the maximum possible | |
5016 | * entries we submitted, but it can be smaller | |
5017 | * if we dropped some of them. If we don't have | |
5018 | * poll entries available, then we know that we | |
5019 | * have nothing left to poll for. Reset the | |
5020 | * inflight count to zero in that case. | |
5021 | */ | |
5022 | mutex_lock(&ctx->uring_lock); | |
5023 | if (!list_empty(&ctx->poll_list)) | |
5024 | __io_iopoll_check(ctx, &nr_events, 0); | |
5025 | else | |
5026 | inflight = 0; | |
5027 | mutex_unlock(&ctx->uring_lock); | |
6c271ce2 JA |
5028 | } else { |
5029 | /* | |
5030 | * Normal IO, just pretend everything completed. | |
5031 | * We don't have to poll completions for that. | |
5032 | */ | |
5033 | nr_events = inflight; | |
5034 | } | |
5035 | ||
5036 | inflight -= nr_events; | |
5037 | if (!inflight) | |
5038 | timeout = jiffies + ctx->sq_thread_idle; | |
5039 | } | |
5040 | ||
fb5ccc98 | 5041 | to_submit = io_sqring_entries(ctx); |
c1edbf5f JA |
5042 | |
5043 | /* | |
5044 | * If submit got -EBUSY, flag us as needing the application | |
5045 | * to enter the kernel to reap and flush events. | |
5046 | */ | |
5047 | if (!to_submit || ret == -EBUSY) { | |
6c271ce2 JA |
5048 | /* |
5049 | * We're polling. If we're within the defined idle | |
5050 | * period, then let us spin without work before going | |
c1edbf5f JA |
5051 | * to sleep. The exception is if we got EBUSY doing |
5052 | * more IO, we should wait for the application to | |
5053 | * reap events and wake us up. | |
6c271ce2 | 5054 | */ |
c1edbf5f | 5055 | if (inflight || |
df069d80 JA |
5056 | (!time_after(jiffies, timeout) && ret != -EBUSY && |
5057 | !percpu_ref_is_dying(&ctx->refs))) { | |
9831a90c | 5058 | cond_resched(); |
6c271ce2 JA |
5059 | continue; |
5060 | } | |
5061 | ||
5062 | /* | |
5063 | * Drop cur_mm before scheduling, we can't hold it for | |
5064 | * long periods (or over schedule()). Do this before | |
5065 | * adding ourselves to the waitqueue, as the unuse/drop | |
5066 | * may sleep. | |
5067 | */ | |
5068 | if (cur_mm) { | |
5069 | unuse_mm(cur_mm); | |
5070 | mmput(cur_mm); | |
5071 | cur_mm = NULL; | |
5072 | } | |
5073 | ||
5074 | prepare_to_wait(&ctx->sqo_wait, &wait, | |
5075 | TASK_INTERRUPTIBLE); | |
5076 | ||
5077 | /* Tell userspace we may need a wakeup call */ | |
75b28aff | 5078 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
0d7bae69 SB |
5079 | /* make sure to read SQ tail after writing flags */ |
5080 | smp_mb(); | |
6c271ce2 | 5081 | |
fb5ccc98 | 5082 | to_submit = io_sqring_entries(ctx); |
c1edbf5f | 5083 | if (!to_submit || ret == -EBUSY) { |
2bbcd6d3 | 5084 | if (kthread_should_park()) { |
6c271ce2 JA |
5085 | finish_wait(&ctx->sqo_wait, &wait); |
5086 | break; | |
5087 | } | |
5088 | if (signal_pending(current)) | |
5089 | flush_signals(current); | |
5090 | schedule(); | |
5091 | finish_wait(&ctx->sqo_wait, &wait); | |
5092 | ||
75b28aff | 5093 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
5094 | continue; |
5095 | } | |
5096 | finish_wait(&ctx->sqo_wait, &wait); | |
5097 | ||
75b28aff | 5098 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
6c271ce2 JA |
5099 | } |
5100 | ||
8a4955ff | 5101 | mutex_lock(&ctx->uring_lock); |
1d7bb1d5 | 5102 | ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true); |
8a4955ff | 5103 | mutex_unlock(&ctx->uring_lock); |
1d7bb1d5 JA |
5104 | if (ret > 0) |
5105 | inflight += ret; | |
6c271ce2 JA |
5106 | } |
5107 | ||
5108 | set_fs(old_fs); | |
5109 | if (cur_mm) { | |
5110 | unuse_mm(cur_mm); | |
5111 | mmput(cur_mm); | |
5112 | } | |
181e448d | 5113 | revert_creds(old_cred); |
06058632 | 5114 | |
2bbcd6d3 | 5115 | kthread_parkme(); |
06058632 | 5116 | |
6c271ce2 JA |
5117 | return 0; |
5118 | } | |
5119 | ||
bda52162 JA |
5120 | struct io_wait_queue { |
5121 | struct wait_queue_entry wq; | |
5122 | struct io_ring_ctx *ctx; | |
5123 | unsigned to_wait; | |
5124 | unsigned nr_timeouts; | |
5125 | }; | |
5126 | ||
1d7bb1d5 | 5127 | static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush) |
bda52162 JA |
5128 | { |
5129 | struct io_ring_ctx *ctx = iowq->ctx; | |
5130 | ||
5131 | /* | |
d195a66e | 5132 | * Wake up if we have enough events, or if a timeout occurred since we |
bda52162 JA |
5133 | * started waiting. For timeouts, we always want to return to userspace, |
5134 | * regardless of event count. | |
5135 | */ | |
1d7bb1d5 | 5136 | return io_cqring_events(ctx, noflush) >= iowq->to_wait || |
bda52162 JA |
5137 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
5138 | } | |
5139 | ||
5140 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, | |
5141 | int wake_flags, void *key) | |
5142 | { | |
5143 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, | |
5144 | wq); | |
5145 | ||
1d7bb1d5 JA |
5146 | /* use noflush == true, as we can't safely rely on locking context */ |
5147 | if (!io_should_wake(iowq, true)) | |
bda52162 JA |
5148 | return -1; |
5149 | ||
5150 | return autoremove_wake_function(curr, mode, wake_flags, key); | |
5151 | } | |
5152 | ||
2b188cc1 JA |
5153 | /* |
5154 | * Wait until events become available, if we don't already have some. The | |
5155 | * application must reap them itself, as they reside on the shared cq ring. | |
5156 | */ | |
5157 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, | |
5158 | const sigset_t __user *sig, size_t sigsz) | |
5159 | { | |
bda52162 JA |
5160 | struct io_wait_queue iowq = { |
5161 | .wq = { | |
5162 | .private = current, | |
5163 | .func = io_wake_function, | |
5164 | .entry = LIST_HEAD_INIT(iowq.wq.entry), | |
5165 | }, | |
5166 | .ctx = ctx, | |
5167 | .to_wait = min_events, | |
5168 | }; | |
75b28aff | 5169 | struct io_rings *rings = ctx->rings; |
e9ffa5c2 | 5170 | int ret = 0; |
2b188cc1 | 5171 | |
1d7bb1d5 | 5172 | if (io_cqring_events(ctx, false) >= min_events) |
2b188cc1 JA |
5173 | return 0; |
5174 | ||
5175 | if (sig) { | |
9e75ad5d AB |
5176 | #ifdef CONFIG_COMPAT |
5177 | if (in_compat_syscall()) | |
5178 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, | |
b772434b | 5179 | sigsz); |
9e75ad5d AB |
5180 | else |
5181 | #endif | |
b772434b | 5182 | ret = set_user_sigmask(sig, sigsz); |
9e75ad5d | 5183 | |
2b188cc1 JA |
5184 | if (ret) |
5185 | return ret; | |
5186 | } | |
5187 | ||
bda52162 | 5188 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
c826bd7a | 5189 | trace_io_uring_cqring_wait(ctx, min_events); |
bda52162 JA |
5190 | do { |
5191 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, | |
5192 | TASK_INTERRUPTIBLE); | |
1d7bb1d5 | 5193 | if (io_should_wake(&iowq, false)) |
bda52162 JA |
5194 | break; |
5195 | schedule(); | |
5196 | if (signal_pending(current)) { | |
e9ffa5c2 | 5197 | ret = -EINTR; |
bda52162 JA |
5198 | break; |
5199 | } | |
5200 | } while (1); | |
5201 | finish_wait(&ctx->wait, &iowq.wq); | |
5202 | ||
e9ffa5c2 | 5203 | restore_saved_sigmask_unless(ret == -EINTR); |
2b188cc1 | 5204 | |
75b28aff | 5205 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
2b188cc1 JA |
5206 | } |
5207 | ||
6b06314c JA |
5208 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
5209 | { | |
5210 | #if defined(CONFIG_UNIX) | |
5211 | if (ctx->ring_sock) { | |
5212 | struct sock *sock = ctx->ring_sock->sk; | |
5213 | struct sk_buff *skb; | |
5214 | ||
5215 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) | |
5216 | kfree_skb(skb); | |
5217 | } | |
5218 | #else | |
5219 | int i; | |
5220 | ||
65e19f54 JA |
5221 | for (i = 0; i < ctx->nr_user_files; i++) { |
5222 | struct file *file; | |
5223 | ||
5224 | file = io_file_from_index(ctx, i); | |
5225 | if (file) | |
5226 | fput(file); | |
5227 | } | |
6b06314c JA |
5228 | #endif |
5229 | } | |
5230 | ||
05f3fb3c JA |
5231 | static void io_file_ref_kill(struct percpu_ref *ref) |
5232 | { | |
5233 | struct fixed_file_data *data; | |
5234 | ||
5235 | data = container_of(ref, struct fixed_file_data, refs); | |
5236 | complete(&data->done); | |
5237 | } | |
5238 | ||
6b06314c JA |
5239 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
5240 | { | |
05f3fb3c | 5241 | struct fixed_file_data *data = ctx->file_data; |
65e19f54 JA |
5242 | unsigned nr_tables, i; |
5243 | ||
05f3fb3c | 5244 | if (!data) |
6b06314c JA |
5245 | return -ENXIO; |
5246 | ||
05f3fb3c | 5247 | percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill); |
e46a7950 | 5248 | flush_work(&data->ref_work); |
2faf852d JA |
5249 | wait_for_completion(&data->done); |
5250 | io_ring_file_ref_flush(data); | |
05f3fb3c JA |
5251 | percpu_ref_exit(&data->refs); |
5252 | ||
6b06314c | 5253 | __io_sqe_files_unregister(ctx); |
65e19f54 JA |
5254 | nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE); |
5255 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c JA |
5256 | kfree(data->table[i].files); |
5257 | kfree(data->table); | |
5258 | kfree(data); | |
5259 | ctx->file_data = NULL; | |
6b06314c JA |
5260 | ctx->nr_user_files = 0; |
5261 | return 0; | |
5262 | } | |
5263 | ||
6c271ce2 JA |
5264 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
5265 | { | |
5266 | if (ctx->sqo_thread) { | |
206aefde | 5267 | wait_for_completion(&ctx->completions[1]); |
2bbcd6d3 RP |
5268 | /* |
5269 | * The park is a bit of a work-around, without it we get | |
5270 | * warning spews on shutdown with SQPOLL set and affinity | |
5271 | * set to a single CPU. | |
5272 | */ | |
06058632 | 5273 | kthread_park(ctx->sqo_thread); |
6c271ce2 JA |
5274 | kthread_stop(ctx->sqo_thread); |
5275 | ctx->sqo_thread = NULL; | |
5276 | } | |
5277 | } | |
5278 | ||
6b06314c JA |
5279 | static void io_finish_async(struct io_ring_ctx *ctx) |
5280 | { | |
6c271ce2 JA |
5281 | io_sq_thread_stop(ctx); |
5282 | ||
561fb04a JA |
5283 | if (ctx->io_wq) { |
5284 | io_wq_destroy(ctx->io_wq); | |
5285 | ctx->io_wq = NULL; | |
6b06314c JA |
5286 | } |
5287 | } | |
5288 | ||
5289 | #if defined(CONFIG_UNIX) | |
6b06314c JA |
5290 | /* |
5291 | * Ensure the UNIX gc is aware of our file set, so we are certain that | |
5292 | * the io_uring can be safely unregistered on process exit, even if we have | |
5293 | * loops in the file referencing. | |
5294 | */ | |
5295 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) | |
5296 | { | |
5297 | struct sock *sk = ctx->ring_sock->sk; | |
5298 | struct scm_fp_list *fpl; | |
5299 | struct sk_buff *skb; | |
08a45173 | 5300 | int i, nr_files; |
6b06314c JA |
5301 | |
5302 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { | |
5303 | unsigned long inflight = ctx->user->unix_inflight + nr; | |
5304 | ||
5305 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) | |
5306 | return -EMFILE; | |
5307 | } | |
5308 | ||
5309 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); | |
5310 | if (!fpl) | |
5311 | return -ENOMEM; | |
5312 | ||
5313 | skb = alloc_skb(0, GFP_KERNEL); | |
5314 | if (!skb) { | |
5315 | kfree(fpl); | |
5316 | return -ENOMEM; | |
5317 | } | |
5318 | ||
5319 | skb->sk = sk; | |
6b06314c | 5320 | |
08a45173 | 5321 | nr_files = 0; |
6b06314c JA |
5322 | fpl->user = get_uid(ctx->user); |
5323 | for (i = 0; i < nr; i++) { | |
65e19f54 JA |
5324 | struct file *file = io_file_from_index(ctx, i + offset); |
5325 | ||
5326 | if (!file) | |
08a45173 | 5327 | continue; |
65e19f54 | 5328 | fpl->fp[nr_files] = get_file(file); |
08a45173 JA |
5329 | unix_inflight(fpl->user, fpl->fp[nr_files]); |
5330 | nr_files++; | |
6b06314c JA |
5331 | } |
5332 | ||
08a45173 JA |
5333 | if (nr_files) { |
5334 | fpl->max = SCM_MAX_FD; | |
5335 | fpl->count = nr_files; | |
5336 | UNIXCB(skb).fp = fpl; | |
05f3fb3c | 5337 | skb->destructor = unix_destruct_scm; |
08a45173 JA |
5338 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
5339 | skb_queue_head(&sk->sk_receive_queue, skb); | |
6b06314c | 5340 | |
08a45173 JA |
5341 | for (i = 0; i < nr_files; i++) |
5342 | fput(fpl->fp[i]); | |
5343 | } else { | |
5344 | kfree_skb(skb); | |
5345 | kfree(fpl); | |
5346 | } | |
6b06314c JA |
5347 | |
5348 | return 0; | |
5349 | } | |
5350 | ||
5351 | /* | |
5352 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which | |
5353 | * causes regular reference counting to break down. We rely on the UNIX | |
5354 | * garbage collection to take care of this problem for us. | |
5355 | */ | |
5356 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
5357 | { | |
5358 | unsigned left, total; | |
5359 | int ret = 0; | |
5360 | ||
5361 | total = 0; | |
5362 | left = ctx->nr_user_files; | |
5363 | while (left) { | |
5364 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); | |
6b06314c JA |
5365 | |
5366 | ret = __io_sqe_files_scm(ctx, this_files, total); | |
5367 | if (ret) | |
5368 | break; | |
5369 | left -= this_files; | |
5370 | total += this_files; | |
5371 | } | |
5372 | ||
5373 | if (!ret) | |
5374 | return 0; | |
5375 | ||
5376 | while (total < ctx->nr_user_files) { | |
65e19f54 JA |
5377 | struct file *file = io_file_from_index(ctx, total); |
5378 | ||
5379 | if (file) | |
5380 | fput(file); | |
6b06314c JA |
5381 | total++; |
5382 | } | |
5383 | ||
5384 | return ret; | |
5385 | } | |
5386 | #else | |
5387 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) | |
5388 | { | |
5389 | return 0; | |
5390 | } | |
5391 | #endif | |
5392 | ||
65e19f54 JA |
5393 | static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables, |
5394 | unsigned nr_files) | |
5395 | { | |
5396 | int i; | |
5397 | ||
5398 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 5399 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
5400 | unsigned this_files; |
5401 | ||
5402 | this_files = min(nr_files, IORING_MAX_FILES_TABLE); | |
5403 | table->files = kcalloc(this_files, sizeof(struct file *), | |
5404 | GFP_KERNEL); | |
5405 | if (!table->files) | |
5406 | break; | |
5407 | nr_files -= this_files; | |
5408 | } | |
5409 | ||
5410 | if (i == nr_tables) | |
5411 | return 0; | |
5412 | ||
5413 | for (i = 0; i < nr_tables; i++) { | |
05f3fb3c | 5414 | struct fixed_file_table *table = &ctx->file_data->table[i]; |
65e19f54 JA |
5415 | kfree(table->files); |
5416 | } | |
5417 | return 1; | |
5418 | } | |
5419 | ||
05f3fb3c JA |
5420 | static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file) |
5421 | { | |
5422 | #if defined(CONFIG_UNIX) | |
5423 | struct sock *sock = ctx->ring_sock->sk; | |
5424 | struct sk_buff_head list, *head = &sock->sk_receive_queue; | |
5425 | struct sk_buff *skb; | |
5426 | int i; | |
5427 | ||
5428 | __skb_queue_head_init(&list); | |
5429 | ||
5430 | /* | |
5431 | * Find the skb that holds this file in its SCM_RIGHTS. When found, | |
5432 | * remove this entry and rearrange the file array. | |
5433 | */ | |
5434 | skb = skb_dequeue(head); | |
5435 | while (skb) { | |
5436 | struct scm_fp_list *fp; | |
5437 | ||
5438 | fp = UNIXCB(skb).fp; | |
5439 | for (i = 0; i < fp->count; i++) { | |
5440 | int left; | |
5441 | ||
5442 | if (fp->fp[i] != file) | |
5443 | continue; | |
5444 | ||
5445 | unix_notinflight(fp->user, fp->fp[i]); | |
5446 | left = fp->count - 1 - i; | |
5447 | if (left) { | |
5448 | memmove(&fp->fp[i], &fp->fp[i + 1], | |
5449 | left * sizeof(struct file *)); | |
5450 | } | |
5451 | fp->count--; | |
5452 | if (!fp->count) { | |
5453 | kfree_skb(skb); | |
5454 | skb = NULL; | |
5455 | } else { | |
5456 | __skb_queue_tail(&list, skb); | |
5457 | } | |
5458 | fput(file); | |
5459 | file = NULL; | |
5460 | break; | |
5461 | } | |
5462 | ||
5463 | if (!file) | |
5464 | break; | |
5465 | ||
5466 | __skb_queue_tail(&list, skb); | |
5467 | ||
5468 | skb = skb_dequeue(head); | |
5469 | } | |
5470 | ||
5471 | if (skb_peek(&list)) { | |
5472 | spin_lock_irq(&head->lock); | |
5473 | while ((skb = __skb_dequeue(&list)) != NULL) | |
5474 | __skb_queue_tail(head, skb); | |
5475 | spin_unlock_irq(&head->lock); | |
5476 | } | |
5477 | #else | |
5478 | fput(file); | |
5479 | #endif | |
5480 | } | |
5481 | ||
5482 | struct io_file_put { | |
5483 | struct llist_node llist; | |
5484 | struct file *file; | |
5485 | struct completion *done; | |
5486 | }; | |
5487 | ||
2faf852d | 5488 | static void io_ring_file_ref_flush(struct fixed_file_data *data) |
65e19f54 | 5489 | { |
05f3fb3c | 5490 | struct io_file_put *pfile, *tmp; |
05f3fb3c | 5491 | struct llist_node *node; |
65e19f54 | 5492 | |
05f3fb3c JA |
5493 | while ((node = llist_del_all(&data->put_llist)) != NULL) { |
5494 | llist_for_each_entry_safe(pfile, tmp, node, llist) { | |
5495 | io_ring_file_put(data->ctx, pfile->file); | |
5496 | if (pfile->done) | |
5497 | complete(pfile->done); | |
5498 | else | |
5499 | kfree(pfile); | |
5500 | } | |
65e19f54 | 5501 | } |
2faf852d | 5502 | } |
65e19f54 | 5503 | |
2faf852d JA |
5504 | static void io_ring_file_ref_switch(struct work_struct *work) |
5505 | { | |
5506 | struct fixed_file_data *data; | |
65e19f54 | 5507 | |
2faf852d JA |
5508 | data = container_of(work, struct fixed_file_data, ref_work); |
5509 | io_ring_file_ref_flush(data); | |
05f3fb3c JA |
5510 | percpu_ref_get(&data->refs); |
5511 | percpu_ref_switch_to_percpu(&data->refs); | |
5512 | } | |
65e19f54 | 5513 | |
05f3fb3c JA |
5514 | static void io_file_data_ref_zero(struct percpu_ref *ref) |
5515 | { | |
5516 | struct fixed_file_data *data; | |
5517 | ||
5518 | data = container_of(ref, struct fixed_file_data, refs); | |
5519 | ||
2faf852d JA |
5520 | /* |
5521 | * We can't safely switch from inside this context, punt to wq. If | |
5522 | * the table ref is going away, the table is being unregistered. | |
5523 | * Don't queue up the async work for that case, the caller will | |
5524 | * handle it. | |
5525 | */ | |
5526 | if (!percpu_ref_is_dying(&data->refs)) | |
5527 | queue_work(system_wq, &data->ref_work); | |
65e19f54 JA |
5528 | } |
5529 | ||
6b06314c JA |
5530 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
5531 | unsigned nr_args) | |
5532 | { | |
5533 | __s32 __user *fds = (__s32 __user *) arg; | |
65e19f54 | 5534 | unsigned nr_tables; |
05f3fb3c | 5535 | struct file *file; |
6b06314c JA |
5536 | int fd, ret = 0; |
5537 | unsigned i; | |
5538 | ||
05f3fb3c | 5539 | if (ctx->file_data) |
6b06314c JA |
5540 | return -EBUSY; |
5541 | if (!nr_args) | |
5542 | return -EINVAL; | |
5543 | if (nr_args > IORING_MAX_FIXED_FILES) | |
5544 | return -EMFILE; | |
5545 | ||
05f3fb3c JA |
5546 | ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL); |
5547 | if (!ctx->file_data) | |
5548 | return -ENOMEM; | |
5549 | ctx->file_data->ctx = ctx; | |
5550 | init_completion(&ctx->file_data->done); | |
5551 | ||
65e19f54 | 5552 | nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE); |
05f3fb3c JA |
5553 | ctx->file_data->table = kcalloc(nr_tables, |
5554 | sizeof(struct fixed_file_table), | |
65e19f54 | 5555 | GFP_KERNEL); |
05f3fb3c JA |
5556 | if (!ctx->file_data->table) { |
5557 | kfree(ctx->file_data); | |
5558 | ctx->file_data = NULL; | |
6b06314c | 5559 | return -ENOMEM; |
05f3fb3c JA |
5560 | } |
5561 | ||
5562 | if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero, | |
5563 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { | |
5564 | kfree(ctx->file_data->table); | |
5565 | kfree(ctx->file_data); | |
5566 | ctx->file_data = NULL; | |
6b06314c | 5567 | return -ENOMEM; |
05f3fb3c JA |
5568 | } |
5569 | ctx->file_data->put_llist.first = NULL; | |
5570 | INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch); | |
6b06314c | 5571 | |
65e19f54 | 5572 | if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) { |
05f3fb3c JA |
5573 | percpu_ref_exit(&ctx->file_data->refs); |
5574 | kfree(ctx->file_data->table); | |
5575 | kfree(ctx->file_data); | |
5576 | ctx->file_data = NULL; | |
65e19f54 JA |
5577 | return -ENOMEM; |
5578 | } | |
5579 | ||
08a45173 | 5580 | for (i = 0; i < nr_args; i++, ctx->nr_user_files++) { |
65e19f54 JA |
5581 | struct fixed_file_table *table; |
5582 | unsigned index; | |
5583 | ||
6b06314c JA |
5584 | ret = -EFAULT; |
5585 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) | |
5586 | break; | |
08a45173 JA |
5587 | /* allow sparse sets */ |
5588 | if (fd == -1) { | |
5589 | ret = 0; | |
5590 | continue; | |
5591 | } | |
6b06314c | 5592 | |
05f3fb3c | 5593 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; |
65e19f54 | 5594 | index = i & IORING_FILE_TABLE_MASK; |
05f3fb3c | 5595 | file = fget(fd); |
6b06314c JA |
5596 | |
5597 | ret = -EBADF; | |
05f3fb3c | 5598 | if (!file) |
6b06314c | 5599 | break; |
05f3fb3c | 5600 | |
6b06314c JA |
5601 | /* |
5602 | * Don't allow io_uring instances to be registered. If UNIX | |
5603 | * isn't enabled, then this causes a reference cycle and this | |
5604 | * instance can never get freed. If UNIX is enabled we'll | |
5605 | * handle it just fine, but there's still no point in allowing | |
5606 | * a ring fd as it doesn't support regular read/write anyway. | |
5607 | */ | |
05f3fb3c JA |
5608 | if (file->f_op == &io_uring_fops) { |
5609 | fput(file); | |
6b06314c JA |
5610 | break; |
5611 | } | |
6b06314c | 5612 | ret = 0; |
05f3fb3c | 5613 | table->files[index] = file; |
6b06314c JA |
5614 | } |
5615 | ||
5616 | if (ret) { | |
65e19f54 | 5617 | for (i = 0; i < ctx->nr_user_files; i++) { |
65e19f54 JA |
5618 | file = io_file_from_index(ctx, i); |
5619 | if (file) | |
5620 | fput(file); | |
5621 | } | |
5622 | for (i = 0; i < nr_tables; i++) | |
05f3fb3c | 5623 | kfree(ctx->file_data->table[i].files); |
6b06314c | 5624 | |
05f3fb3c JA |
5625 | kfree(ctx->file_data->table); |
5626 | kfree(ctx->file_data); | |
5627 | ctx->file_data = NULL; | |
6b06314c JA |
5628 | ctx->nr_user_files = 0; |
5629 | return ret; | |
5630 | } | |
5631 | ||
5632 | ret = io_sqe_files_scm(ctx); | |
5633 | if (ret) | |
5634 | io_sqe_files_unregister(ctx); | |
5635 | ||
5636 | return ret; | |
5637 | } | |
5638 | ||
c3a31e60 JA |
5639 | static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, |
5640 | int index) | |
5641 | { | |
5642 | #if defined(CONFIG_UNIX) | |
5643 | struct sock *sock = ctx->ring_sock->sk; | |
5644 | struct sk_buff_head *head = &sock->sk_receive_queue; | |
5645 | struct sk_buff *skb; | |
5646 | ||
5647 | /* | |
5648 | * See if we can merge this file into an existing skb SCM_RIGHTS | |
5649 | * file set. If there's no room, fall back to allocating a new skb | |
5650 | * and filling it in. | |
5651 | */ | |
5652 | spin_lock_irq(&head->lock); | |
5653 | skb = skb_peek(head); | |
5654 | if (skb) { | |
5655 | struct scm_fp_list *fpl = UNIXCB(skb).fp; | |
5656 | ||
5657 | if (fpl->count < SCM_MAX_FD) { | |
5658 | __skb_unlink(skb, head); | |
5659 | spin_unlock_irq(&head->lock); | |
5660 | fpl->fp[fpl->count] = get_file(file); | |
5661 | unix_inflight(fpl->user, fpl->fp[fpl->count]); | |
5662 | fpl->count++; | |
5663 | spin_lock_irq(&head->lock); | |
5664 | __skb_queue_head(head, skb); | |
5665 | } else { | |
5666 | skb = NULL; | |
5667 | } | |
5668 | } | |
5669 | spin_unlock_irq(&head->lock); | |
5670 | ||
5671 | if (skb) { | |
5672 | fput(file); | |
5673 | return 0; | |
5674 | } | |
5675 | ||
5676 | return __io_sqe_files_scm(ctx, 1, index); | |
5677 | #else | |
5678 | return 0; | |
5679 | #endif | |
5680 | } | |
5681 | ||
05f3fb3c | 5682 | static void io_atomic_switch(struct percpu_ref *ref) |
c3a31e60 | 5683 | { |
05f3fb3c JA |
5684 | struct fixed_file_data *data; |
5685 | ||
5686 | data = container_of(ref, struct fixed_file_data, refs); | |
5687 | clear_bit(FFD_F_ATOMIC, &data->state); | |
5688 | } | |
5689 | ||
5690 | static bool io_queue_file_removal(struct fixed_file_data *data, | |
5691 | struct file *file) | |
5692 | { | |
5693 | struct io_file_put *pfile, pfile_stack; | |
5694 | DECLARE_COMPLETION_ONSTACK(done); | |
5695 | ||
5696 | /* | |
5697 | * If we fail allocating the struct we need for doing async reomval | |
5698 | * of this file, just punt to sync and wait for it. | |
5699 | */ | |
5700 | pfile = kzalloc(sizeof(*pfile), GFP_KERNEL); | |
5701 | if (!pfile) { | |
5702 | pfile = &pfile_stack; | |
5703 | pfile->done = &done; | |
5704 | } | |
5705 | ||
5706 | pfile->file = file; | |
5707 | llist_add(&pfile->llist, &data->put_llist); | |
5708 | ||
5709 | if (pfile == &pfile_stack) { | |
5710 | if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) { | |
5711 | percpu_ref_put(&data->refs); | |
5712 | percpu_ref_switch_to_atomic(&data->refs, | |
5713 | io_atomic_switch); | |
5714 | } | |
5715 | wait_for_completion(&done); | |
5716 | flush_work(&data->ref_work); | |
5717 | return false; | |
5718 | } | |
5719 | ||
5720 | return true; | |
5721 | } | |
5722 | ||
5723 | static int __io_sqe_files_update(struct io_ring_ctx *ctx, | |
5724 | struct io_uring_files_update *up, | |
5725 | unsigned nr_args) | |
5726 | { | |
5727 | struct fixed_file_data *data = ctx->file_data; | |
5728 | bool ref_switch = false; | |
5729 | struct file *file; | |
c3a31e60 JA |
5730 | __s32 __user *fds; |
5731 | int fd, i, err; | |
5732 | __u32 done; | |
5733 | ||
05f3fb3c | 5734 | if (check_add_overflow(up->offset, nr_args, &done)) |
c3a31e60 JA |
5735 | return -EOVERFLOW; |
5736 | if (done > ctx->nr_user_files) | |
5737 | return -EINVAL; | |
5738 | ||
5739 | done = 0; | |
05f3fb3c | 5740 | fds = u64_to_user_ptr(up->fds); |
c3a31e60 | 5741 | while (nr_args) { |
65e19f54 JA |
5742 | struct fixed_file_table *table; |
5743 | unsigned index; | |
5744 | ||
c3a31e60 JA |
5745 | err = 0; |
5746 | if (copy_from_user(&fd, &fds[done], sizeof(fd))) { | |
5747 | err = -EFAULT; | |
5748 | break; | |
5749 | } | |
05f3fb3c JA |
5750 | i = array_index_nospec(up->offset, ctx->nr_user_files); |
5751 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
65e19f54 JA |
5752 | index = i & IORING_FILE_TABLE_MASK; |
5753 | if (table->files[index]) { | |
05f3fb3c | 5754 | file = io_file_from_index(ctx, index); |
65e19f54 | 5755 | table->files[index] = NULL; |
05f3fb3c JA |
5756 | if (io_queue_file_removal(data, file)) |
5757 | ref_switch = true; | |
c3a31e60 JA |
5758 | } |
5759 | if (fd != -1) { | |
c3a31e60 JA |
5760 | file = fget(fd); |
5761 | if (!file) { | |
5762 | err = -EBADF; | |
5763 | break; | |
5764 | } | |
5765 | /* | |
5766 | * Don't allow io_uring instances to be registered. If | |
5767 | * UNIX isn't enabled, then this causes a reference | |
5768 | * cycle and this instance can never get freed. If UNIX | |
5769 | * is enabled we'll handle it just fine, but there's | |
5770 | * still no point in allowing a ring fd as it doesn't | |
5771 | * support regular read/write anyway. | |
5772 | */ | |
5773 | if (file->f_op == &io_uring_fops) { | |
5774 | fput(file); | |
5775 | err = -EBADF; | |
5776 | break; | |
5777 | } | |
65e19f54 | 5778 | table->files[index] = file; |
c3a31e60 JA |
5779 | err = io_sqe_file_register(ctx, file, i); |
5780 | if (err) | |
5781 | break; | |
5782 | } | |
5783 | nr_args--; | |
5784 | done++; | |
05f3fb3c JA |
5785 | up->offset++; |
5786 | } | |
5787 | ||
5788 | if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) { | |
5789 | percpu_ref_put(&data->refs); | |
5790 | percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch); | |
c3a31e60 JA |
5791 | } |
5792 | ||
5793 | return done ? done : err; | |
5794 | } | |
05f3fb3c JA |
5795 | static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg, |
5796 | unsigned nr_args) | |
5797 | { | |
5798 | struct io_uring_files_update up; | |
5799 | ||
5800 | if (!ctx->file_data) | |
5801 | return -ENXIO; | |
5802 | if (!nr_args) | |
5803 | return -EINVAL; | |
5804 | if (copy_from_user(&up, arg, sizeof(up))) | |
5805 | return -EFAULT; | |
5806 | if (up.resv) | |
5807 | return -EINVAL; | |
5808 | ||
5809 | return __io_sqe_files_update(ctx, &up, nr_args); | |
5810 | } | |
c3a31e60 | 5811 | |
7d723065 JA |
5812 | static void io_put_work(struct io_wq_work *work) |
5813 | { | |
5814 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5815 | ||
5816 | io_put_req(req); | |
5817 | } | |
5818 | ||
5819 | static void io_get_work(struct io_wq_work *work) | |
5820 | { | |
5821 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); | |
5822 | ||
5823 | refcount_inc(&req->refs); | |
5824 | } | |
5825 | ||
24369c2e PB |
5826 | static int io_init_wq_offload(struct io_ring_ctx *ctx, |
5827 | struct io_uring_params *p) | |
5828 | { | |
5829 | struct io_wq_data data; | |
5830 | struct fd f; | |
5831 | struct io_ring_ctx *ctx_attach; | |
5832 | unsigned int concurrency; | |
5833 | int ret = 0; | |
5834 | ||
5835 | data.user = ctx->user; | |
5836 | data.get_work = io_get_work; | |
5837 | data.put_work = io_put_work; | |
5838 | ||
5839 | if (!(p->flags & IORING_SETUP_ATTACH_WQ)) { | |
5840 | /* Do QD, or 4 * CPUS, whatever is smallest */ | |
5841 | concurrency = min(ctx->sq_entries, 4 * num_online_cpus()); | |
5842 | ||
5843 | ctx->io_wq = io_wq_create(concurrency, &data); | |
5844 | if (IS_ERR(ctx->io_wq)) { | |
5845 | ret = PTR_ERR(ctx->io_wq); | |
5846 | ctx->io_wq = NULL; | |
5847 | } | |
5848 | return ret; | |
5849 | } | |
5850 | ||
5851 | f = fdget(p->wq_fd); | |
5852 | if (!f.file) | |
5853 | return -EBADF; | |
5854 | ||
5855 | if (f.file->f_op != &io_uring_fops) { | |
5856 | ret = -EINVAL; | |
5857 | goto out_fput; | |
5858 | } | |
5859 | ||
5860 | ctx_attach = f.file->private_data; | |
5861 | /* @io_wq is protected by holding the fd */ | |
5862 | if (!io_wq_get(ctx_attach->io_wq, &data)) { | |
5863 | ret = -EINVAL; | |
5864 | goto out_fput; | |
5865 | } | |
5866 | ||
5867 | ctx->io_wq = ctx_attach->io_wq; | |
5868 | out_fput: | |
5869 | fdput(f); | |
5870 | return ret; | |
5871 | } | |
5872 | ||
6c271ce2 JA |
5873 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
5874 | struct io_uring_params *p) | |
2b188cc1 JA |
5875 | { |
5876 | int ret; | |
5877 | ||
6c271ce2 | 5878 | init_waitqueue_head(&ctx->sqo_wait); |
2b188cc1 JA |
5879 | mmgrab(current->mm); |
5880 | ctx->sqo_mm = current->mm; | |
5881 | ||
6c271ce2 | 5882 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
3ec482d1 JA |
5883 | ret = -EPERM; |
5884 | if (!capable(CAP_SYS_ADMIN)) | |
5885 | goto err; | |
5886 | ||
917257da JA |
5887 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
5888 | if (!ctx->sq_thread_idle) | |
5889 | ctx->sq_thread_idle = HZ; | |
5890 | ||
6c271ce2 | 5891 | if (p->flags & IORING_SETUP_SQ_AFF) { |
44a9bd18 | 5892 | int cpu = p->sq_thread_cpu; |
6c271ce2 | 5893 | |
917257da | 5894 | ret = -EINVAL; |
44a9bd18 JA |
5895 | if (cpu >= nr_cpu_ids) |
5896 | goto err; | |
7889f44d | 5897 | if (!cpu_online(cpu)) |
917257da JA |
5898 | goto err; |
5899 | ||
6c271ce2 JA |
5900 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
5901 | ctx, cpu, | |
5902 | "io_uring-sq"); | |
5903 | } else { | |
5904 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, | |
5905 | "io_uring-sq"); | |
5906 | } | |
5907 | if (IS_ERR(ctx->sqo_thread)) { | |
5908 | ret = PTR_ERR(ctx->sqo_thread); | |
5909 | ctx->sqo_thread = NULL; | |
5910 | goto err; | |
5911 | } | |
5912 | wake_up_process(ctx->sqo_thread); | |
5913 | } else if (p->flags & IORING_SETUP_SQ_AFF) { | |
5914 | /* Can't have SQ_AFF without SQPOLL */ | |
5915 | ret = -EINVAL; | |
5916 | goto err; | |
5917 | } | |
5918 | ||
24369c2e PB |
5919 | ret = io_init_wq_offload(ctx, p); |
5920 | if (ret) | |
2b188cc1 | 5921 | goto err; |
2b188cc1 JA |
5922 | |
5923 | return 0; | |
5924 | err: | |
54a91f3b | 5925 | io_finish_async(ctx); |
2b188cc1 JA |
5926 | mmdrop(ctx->sqo_mm); |
5927 | ctx->sqo_mm = NULL; | |
5928 | return ret; | |
5929 | } | |
5930 | ||
5931 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) | |
5932 | { | |
5933 | atomic_long_sub(nr_pages, &user->locked_vm); | |
5934 | } | |
5935 | ||
5936 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) | |
5937 | { | |
5938 | unsigned long page_limit, cur_pages, new_pages; | |
5939 | ||
5940 | /* Don't allow more pages than we can safely lock */ | |
5941 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; | |
5942 | ||
5943 | do { | |
5944 | cur_pages = atomic_long_read(&user->locked_vm); | |
5945 | new_pages = cur_pages + nr_pages; | |
5946 | if (new_pages > page_limit) | |
5947 | return -ENOMEM; | |
5948 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, | |
5949 | new_pages) != cur_pages); | |
5950 | ||
5951 | return 0; | |
5952 | } | |
5953 | ||
5954 | static void io_mem_free(void *ptr) | |
5955 | { | |
52e04ef4 MR |
5956 | struct page *page; |
5957 | ||
5958 | if (!ptr) | |
5959 | return; | |
2b188cc1 | 5960 | |
52e04ef4 | 5961 | page = virt_to_head_page(ptr); |
2b188cc1 JA |
5962 | if (put_page_testzero(page)) |
5963 | free_compound_page(page); | |
5964 | } | |
5965 | ||
5966 | static void *io_mem_alloc(size_t size) | |
5967 | { | |
5968 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | | |
5969 | __GFP_NORETRY; | |
5970 | ||
5971 | return (void *) __get_free_pages(gfp_flags, get_order(size)); | |
5972 | } | |
5973 | ||
75b28aff HV |
5974 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
5975 | size_t *sq_offset) | |
5976 | { | |
5977 | struct io_rings *rings; | |
5978 | size_t off, sq_array_size; | |
5979 | ||
5980 | off = struct_size(rings, cqes, cq_entries); | |
5981 | if (off == SIZE_MAX) | |
5982 | return SIZE_MAX; | |
5983 | ||
5984 | #ifdef CONFIG_SMP | |
5985 | off = ALIGN(off, SMP_CACHE_BYTES); | |
5986 | if (off == 0) | |
5987 | return SIZE_MAX; | |
5988 | #endif | |
5989 | ||
5990 | sq_array_size = array_size(sizeof(u32), sq_entries); | |
5991 | if (sq_array_size == SIZE_MAX) | |
5992 | return SIZE_MAX; | |
5993 | ||
5994 | if (check_add_overflow(off, sq_array_size, &off)) | |
5995 | return SIZE_MAX; | |
5996 | ||
5997 | if (sq_offset) | |
5998 | *sq_offset = off; | |
5999 | ||
6000 | return off; | |
6001 | } | |
6002 | ||
2b188cc1 JA |
6003 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
6004 | { | |
75b28aff | 6005 | size_t pages; |
2b188cc1 | 6006 | |
75b28aff HV |
6007 | pages = (size_t)1 << get_order( |
6008 | rings_size(sq_entries, cq_entries, NULL)); | |
6009 | pages += (size_t)1 << get_order( | |
6010 | array_size(sizeof(struct io_uring_sqe), sq_entries)); | |
2b188cc1 | 6011 | |
75b28aff | 6012 | return pages; |
2b188cc1 JA |
6013 | } |
6014 | ||
edafccee JA |
6015 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
6016 | { | |
6017 | int i, j; | |
6018 | ||
6019 | if (!ctx->user_bufs) | |
6020 | return -ENXIO; | |
6021 | ||
6022 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
6023 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
6024 | ||
6025 | for (j = 0; j < imu->nr_bvecs; j++) | |
f1f6a7dd | 6026 | unpin_user_page(imu->bvec[j].bv_page); |
edafccee JA |
6027 | |
6028 | if (ctx->account_mem) | |
6029 | io_unaccount_mem(ctx->user, imu->nr_bvecs); | |
d4ef6475 | 6030 | kvfree(imu->bvec); |
edafccee JA |
6031 | imu->nr_bvecs = 0; |
6032 | } | |
6033 | ||
6034 | kfree(ctx->user_bufs); | |
6035 | ctx->user_bufs = NULL; | |
6036 | ctx->nr_user_bufs = 0; | |
6037 | return 0; | |
6038 | } | |
6039 | ||
6040 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, | |
6041 | void __user *arg, unsigned index) | |
6042 | { | |
6043 | struct iovec __user *src; | |
6044 | ||
6045 | #ifdef CONFIG_COMPAT | |
6046 | if (ctx->compat) { | |
6047 | struct compat_iovec __user *ciovs; | |
6048 | struct compat_iovec ciov; | |
6049 | ||
6050 | ciovs = (struct compat_iovec __user *) arg; | |
6051 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) | |
6052 | return -EFAULT; | |
6053 | ||
d55e5f5b | 6054 | dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base); |
edafccee JA |
6055 | dst->iov_len = ciov.iov_len; |
6056 | return 0; | |
6057 | } | |
6058 | #endif | |
6059 | src = (struct iovec __user *) arg; | |
6060 | if (copy_from_user(dst, &src[index], sizeof(*dst))) | |
6061 | return -EFAULT; | |
6062 | return 0; | |
6063 | } | |
6064 | ||
6065 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, | |
6066 | unsigned nr_args) | |
6067 | { | |
6068 | struct vm_area_struct **vmas = NULL; | |
6069 | struct page **pages = NULL; | |
6070 | int i, j, got_pages = 0; | |
6071 | int ret = -EINVAL; | |
6072 | ||
6073 | if (ctx->user_bufs) | |
6074 | return -EBUSY; | |
6075 | if (!nr_args || nr_args > UIO_MAXIOV) | |
6076 | return -EINVAL; | |
6077 | ||
6078 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), | |
6079 | GFP_KERNEL); | |
6080 | if (!ctx->user_bufs) | |
6081 | return -ENOMEM; | |
6082 | ||
6083 | for (i = 0; i < nr_args; i++) { | |
6084 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; | |
6085 | unsigned long off, start, end, ubuf; | |
6086 | int pret, nr_pages; | |
6087 | struct iovec iov; | |
6088 | size_t size; | |
6089 | ||
6090 | ret = io_copy_iov(ctx, &iov, arg, i); | |
6091 | if (ret) | |
a278682d | 6092 | goto err; |
edafccee JA |
6093 | |
6094 | /* | |
6095 | * Don't impose further limits on the size and buffer | |
6096 | * constraints here, we'll -EINVAL later when IO is | |
6097 | * submitted if they are wrong. | |
6098 | */ | |
6099 | ret = -EFAULT; | |
6100 | if (!iov.iov_base || !iov.iov_len) | |
6101 | goto err; | |
6102 | ||
6103 | /* arbitrary limit, but we need something */ | |
6104 | if (iov.iov_len > SZ_1G) | |
6105 | goto err; | |
6106 | ||
6107 | ubuf = (unsigned long) iov.iov_base; | |
6108 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
6109 | start = ubuf >> PAGE_SHIFT; | |
6110 | nr_pages = end - start; | |
6111 | ||
6112 | if (ctx->account_mem) { | |
6113 | ret = io_account_mem(ctx->user, nr_pages); | |
6114 | if (ret) | |
6115 | goto err; | |
6116 | } | |
6117 | ||
6118 | ret = 0; | |
6119 | if (!pages || nr_pages > got_pages) { | |
6120 | kfree(vmas); | |
6121 | kfree(pages); | |
d4ef6475 | 6122 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
edafccee | 6123 | GFP_KERNEL); |
d4ef6475 | 6124 | vmas = kvmalloc_array(nr_pages, |
edafccee JA |
6125 | sizeof(struct vm_area_struct *), |
6126 | GFP_KERNEL); | |
6127 | if (!pages || !vmas) { | |
6128 | ret = -ENOMEM; | |
6129 | if (ctx->account_mem) | |
6130 | io_unaccount_mem(ctx->user, nr_pages); | |
6131 | goto err; | |
6132 | } | |
6133 | got_pages = nr_pages; | |
6134 | } | |
6135 | ||
d4ef6475 | 6136 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
edafccee JA |
6137 | GFP_KERNEL); |
6138 | ret = -ENOMEM; | |
6139 | if (!imu->bvec) { | |
6140 | if (ctx->account_mem) | |
6141 | io_unaccount_mem(ctx->user, nr_pages); | |
6142 | goto err; | |
6143 | } | |
6144 | ||
6145 | ret = 0; | |
6146 | down_read(¤t->mm->mmap_sem); | |
2113b05d | 6147 | pret = pin_user_pages(ubuf, nr_pages, |
932f4a63 IW |
6148 | FOLL_WRITE | FOLL_LONGTERM, |
6149 | pages, vmas); | |
edafccee JA |
6150 | if (pret == nr_pages) { |
6151 | /* don't support file backed memory */ | |
6152 | for (j = 0; j < nr_pages; j++) { | |
6153 | struct vm_area_struct *vma = vmas[j]; | |
6154 | ||
6155 | if (vma->vm_file && | |
6156 | !is_file_hugepages(vma->vm_file)) { | |
6157 | ret = -EOPNOTSUPP; | |
6158 | break; | |
6159 | } | |
6160 | } | |
6161 | } else { | |
6162 | ret = pret < 0 ? pret : -EFAULT; | |
6163 | } | |
6164 | up_read(¤t->mm->mmap_sem); | |
6165 | if (ret) { | |
6166 | /* | |
6167 | * if we did partial map, or found file backed vmas, | |
6168 | * release any pages we did get | |
6169 | */ | |
27c4d3a3 | 6170 | if (pret > 0) |
f1f6a7dd | 6171 | unpin_user_pages(pages, pret); |
edafccee JA |
6172 | if (ctx->account_mem) |
6173 | io_unaccount_mem(ctx->user, nr_pages); | |
d4ef6475 | 6174 | kvfree(imu->bvec); |
edafccee JA |
6175 | goto err; |
6176 | } | |
6177 | ||
6178 | off = ubuf & ~PAGE_MASK; | |
6179 | size = iov.iov_len; | |
6180 | for (j = 0; j < nr_pages; j++) { | |
6181 | size_t vec_len; | |
6182 | ||
6183 | vec_len = min_t(size_t, size, PAGE_SIZE - off); | |
6184 | imu->bvec[j].bv_page = pages[j]; | |
6185 | imu->bvec[j].bv_len = vec_len; | |
6186 | imu->bvec[j].bv_offset = off; | |
6187 | off = 0; | |
6188 | size -= vec_len; | |
6189 | } | |
6190 | /* store original address for later verification */ | |
6191 | imu->ubuf = ubuf; | |
6192 | imu->len = iov.iov_len; | |
6193 | imu->nr_bvecs = nr_pages; | |
6194 | ||
6195 | ctx->nr_user_bufs++; | |
6196 | } | |
d4ef6475 MR |
6197 | kvfree(pages); |
6198 | kvfree(vmas); | |
edafccee JA |
6199 | return 0; |
6200 | err: | |
d4ef6475 MR |
6201 | kvfree(pages); |
6202 | kvfree(vmas); | |
edafccee JA |
6203 | io_sqe_buffer_unregister(ctx); |
6204 | return ret; | |
6205 | } | |
6206 | ||
9b402849 JA |
6207 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
6208 | { | |
6209 | __s32 __user *fds = arg; | |
6210 | int fd; | |
6211 | ||
6212 | if (ctx->cq_ev_fd) | |
6213 | return -EBUSY; | |
6214 | ||
6215 | if (copy_from_user(&fd, fds, sizeof(*fds))) | |
6216 | return -EFAULT; | |
6217 | ||
6218 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); | |
6219 | if (IS_ERR(ctx->cq_ev_fd)) { | |
6220 | int ret = PTR_ERR(ctx->cq_ev_fd); | |
6221 | ctx->cq_ev_fd = NULL; | |
6222 | return ret; | |
6223 | } | |
6224 | ||
6225 | return 0; | |
6226 | } | |
6227 | ||
6228 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) | |
6229 | { | |
6230 | if (ctx->cq_ev_fd) { | |
6231 | eventfd_ctx_put(ctx->cq_ev_fd); | |
6232 | ctx->cq_ev_fd = NULL; | |
6233 | return 0; | |
6234 | } | |
6235 | ||
6236 | return -ENXIO; | |
6237 | } | |
6238 | ||
2b188cc1 JA |
6239 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
6240 | { | |
6b06314c | 6241 | io_finish_async(ctx); |
2b188cc1 JA |
6242 | if (ctx->sqo_mm) |
6243 | mmdrop(ctx->sqo_mm); | |
def596e9 JA |
6244 | |
6245 | io_iopoll_reap_events(ctx); | |
edafccee | 6246 | io_sqe_buffer_unregister(ctx); |
6b06314c | 6247 | io_sqe_files_unregister(ctx); |
9b402849 | 6248 | io_eventfd_unregister(ctx); |
def596e9 | 6249 | |
2b188cc1 | 6250 | #if defined(CONFIG_UNIX) |
355e8d26 EB |
6251 | if (ctx->ring_sock) { |
6252 | ctx->ring_sock->file = NULL; /* so that iput() is called */ | |
2b188cc1 | 6253 | sock_release(ctx->ring_sock); |
355e8d26 | 6254 | } |
2b188cc1 JA |
6255 | #endif |
6256 | ||
75b28aff | 6257 | io_mem_free(ctx->rings); |
2b188cc1 | 6258 | io_mem_free(ctx->sq_sqes); |
2b188cc1 JA |
6259 | |
6260 | percpu_ref_exit(&ctx->refs); | |
6261 | if (ctx->account_mem) | |
6262 | io_unaccount_mem(ctx->user, | |
6263 | ring_pages(ctx->sq_entries, ctx->cq_entries)); | |
6264 | free_uid(ctx->user); | |
181e448d | 6265 | put_cred(ctx->creds); |
206aefde | 6266 | kfree(ctx->completions); |
78076bb6 | 6267 | kfree(ctx->cancel_hash); |
0ddf92e8 | 6268 | kmem_cache_free(req_cachep, ctx->fallback_req); |
2b188cc1 JA |
6269 | kfree(ctx); |
6270 | } | |
6271 | ||
6272 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) | |
6273 | { | |
6274 | struct io_ring_ctx *ctx = file->private_data; | |
6275 | __poll_t mask = 0; | |
6276 | ||
6277 | poll_wait(file, &ctx->cq_wait, wait); | |
4f7067c3 SB |
6278 | /* |
6279 | * synchronizes with barrier from wq_has_sleeper call in | |
6280 | * io_commit_cqring | |
6281 | */ | |
2b188cc1 | 6282 | smp_rmb(); |
75b28aff HV |
6283 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
6284 | ctx->rings->sq_ring_entries) | |
2b188cc1 | 6285 | mask |= EPOLLOUT | EPOLLWRNORM; |
daa5de54 | 6286 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
2b188cc1 JA |
6287 | mask |= EPOLLIN | EPOLLRDNORM; |
6288 | ||
6289 | return mask; | |
6290 | } | |
6291 | ||
6292 | static int io_uring_fasync(int fd, struct file *file, int on) | |
6293 | { | |
6294 | struct io_ring_ctx *ctx = file->private_data; | |
6295 | ||
6296 | return fasync_helper(fd, file, on, &ctx->cq_fasync); | |
6297 | } | |
6298 | ||
071698e1 JA |
6299 | static int io_remove_personalities(int id, void *p, void *data) |
6300 | { | |
6301 | struct io_ring_ctx *ctx = data; | |
6302 | const struct cred *cred; | |
6303 | ||
6304 | cred = idr_remove(&ctx->personality_idr, id); | |
6305 | if (cred) | |
6306 | put_cred(cred); | |
6307 | return 0; | |
6308 | } | |
6309 | ||
2b188cc1 JA |
6310 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
6311 | { | |
6312 | mutex_lock(&ctx->uring_lock); | |
6313 | percpu_ref_kill(&ctx->refs); | |
6314 | mutex_unlock(&ctx->uring_lock); | |
6315 | ||
df069d80 JA |
6316 | /* |
6317 | * Wait for sq thread to idle, if we have one. It won't spin on new | |
6318 | * work after we've killed the ctx ref above. This is important to do | |
6319 | * before we cancel existing commands, as the thread could otherwise | |
6320 | * be queueing new work post that. If that's work we need to cancel, | |
6321 | * it could cause shutdown to hang. | |
6322 | */ | |
6323 | while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait)) | |
6324 | cpu_relax(); | |
6325 | ||
5262f567 | 6326 | io_kill_timeouts(ctx); |
221c5eb2 | 6327 | io_poll_remove_all(ctx); |
561fb04a JA |
6328 | |
6329 | if (ctx->io_wq) | |
6330 | io_wq_cancel_all(ctx->io_wq); | |
6331 | ||
def596e9 | 6332 | io_iopoll_reap_events(ctx); |
15dff286 JA |
6333 | /* if we failed setting up the ctx, we might not have any rings */ |
6334 | if (ctx->rings) | |
6335 | io_cqring_overflow_flush(ctx, true); | |
071698e1 | 6336 | idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); |
206aefde | 6337 | wait_for_completion(&ctx->completions[0]); |
2b188cc1 JA |
6338 | io_ring_ctx_free(ctx); |
6339 | } | |
6340 | ||
6341 | static int io_uring_release(struct inode *inode, struct file *file) | |
6342 | { | |
6343 | struct io_ring_ctx *ctx = file->private_data; | |
6344 | ||
6345 | file->private_data = NULL; | |
6346 | io_ring_ctx_wait_and_kill(ctx); | |
6347 | return 0; | |
6348 | } | |
6349 | ||
fcb323cc JA |
6350 | static void io_uring_cancel_files(struct io_ring_ctx *ctx, |
6351 | struct files_struct *files) | |
6352 | { | |
6353 | struct io_kiocb *req; | |
6354 | DEFINE_WAIT(wait); | |
6355 | ||
6356 | while (!list_empty_careful(&ctx->inflight_list)) { | |
768134d4 | 6357 | struct io_kiocb *cancel_req = NULL; |
fcb323cc JA |
6358 | |
6359 | spin_lock_irq(&ctx->inflight_lock); | |
6360 | list_for_each_entry(req, &ctx->inflight_list, inflight_entry) { | |
768134d4 JA |
6361 | if (req->work.files != files) |
6362 | continue; | |
6363 | /* req is being completed, ignore */ | |
6364 | if (!refcount_inc_not_zero(&req->refs)) | |
6365 | continue; | |
6366 | cancel_req = req; | |
6367 | break; | |
fcb323cc | 6368 | } |
768134d4 | 6369 | if (cancel_req) |
fcb323cc | 6370 | prepare_to_wait(&ctx->inflight_wait, &wait, |
768134d4 | 6371 | TASK_UNINTERRUPTIBLE); |
fcb323cc JA |
6372 | spin_unlock_irq(&ctx->inflight_lock); |
6373 | ||
768134d4 JA |
6374 | /* We need to keep going until we don't find a matching req */ |
6375 | if (!cancel_req) | |
fcb323cc | 6376 | break; |
2f6d9b9d BL |
6377 | |
6378 | io_wq_cancel_work(ctx->io_wq, &cancel_req->work); | |
6379 | io_put_req(cancel_req); | |
fcb323cc JA |
6380 | schedule(); |
6381 | } | |
768134d4 | 6382 | finish_wait(&ctx->inflight_wait, &wait); |
fcb323cc JA |
6383 | } |
6384 | ||
6385 | static int io_uring_flush(struct file *file, void *data) | |
6386 | { | |
6387 | struct io_ring_ctx *ctx = file->private_data; | |
6388 | ||
6389 | io_uring_cancel_files(ctx, data); | |
fcb323cc JA |
6390 | return 0; |
6391 | } | |
6392 | ||
6c5c240e RP |
6393 | static void *io_uring_validate_mmap_request(struct file *file, |
6394 | loff_t pgoff, size_t sz) | |
2b188cc1 | 6395 | { |
2b188cc1 | 6396 | struct io_ring_ctx *ctx = file->private_data; |
6c5c240e | 6397 | loff_t offset = pgoff << PAGE_SHIFT; |
2b188cc1 JA |
6398 | struct page *page; |
6399 | void *ptr; | |
6400 | ||
6401 | switch (offset) { | |
6402 | case IORING_OFF_SQ_RING: | |
75b28aff HV |
6403 | case IORING_OFF_CQ_RING: |
6404 | ptr = ctx->rings; | |
2b188cc1 JA |
6405 | break; |
6406 | case IORING_OFF_SQES: | |
6407 | ptr = ctx->sq_sqes; | |
6408 | break; | |
2b188cc1 | 6409 | default: |
6c5c240e | 6410 | return ERR_PTR(-EINVAL); |
2b188cc1 JA |
6411 | } |
6412 | ||
6413 | page = virt_to_head_page(ptr); | |
a50b854e | 6414 | if (sz > page_size(page)) |
6c5c240e RP |
6415 | return ERR_PTR(-EINVAL); |
6416 | ||
6417 | return ptr; | |
6418 | } | |
6419 | ||
6420 | #ifdef CONFIG_MMU | |
6421 | ||
6422 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
6423 | { | |
6424 | size_t sz = vma->vm_end - vma->vm_start; | |
6425 | unsigned long pfn; | |
6426 | void *ptr; | |
6427 | ||
6428 | ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz); | |
6429 | if (IS_ERR(ptr)) | |
6430 | return PTR_ERR(ptr); | |
2b188cc1 JA |
6431 | |
6432 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; | |
6433 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); | |
6434 | } | |
6435 | ||
6c5c240e RP |
6436 | #else /* !CONFIG_MMU */ |
6437 | ||
6438 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) | |
6439 | { | |
6440 | return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL; | |
6441 | } | |
6442 | ||
6443 | static unsigned int io_uring_nommu_mmap_capabilities(struct file *file) | |
6444 | { | |
6445 | return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE; | |
6446 | } | |
6447 | ||
6448 | static unsigned long io_uring_nommu_get_unmapped_area(struct file *file, | |
6449 | unsigned long addr, unsigned long len, | |
6450 | unsigned long pgoff, unsigned long flags) | |
6451 | { | |
6452 | void *ptr; | |
6453 | ||
6454 | ptr = io_uring_validate_mmap_request(file, pgoff, len); | |
6455 | if (IS_ERR(ptr)) | |
6456 | return PTR_ERR(ptr); | |
6457 | ||
6458 | return (unsigned long) ptr; | |
6459 | } | |
6460 | ||
6461 | #endif /* !CONFIG_MMU */ | |
6462 | ||
2b188cc1 JA |
6463 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
6464 | u32, min_complete, u32, flags, const sigset_t __user *, sig, | |
6465 | size_t, sigsz) | |
6466 | { | |
6467 | struct io_ring_ctx *ctx; | |
6468 | long ret = -EBADF; | |
6469 | int submitted = 0; | |
6470 | struct fd f; | |
6471 | ||
6c271ce2 | 6472 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
2b188cc1 JA |
6473 | return -EINVAL; |
6474 | ||
6475 | f = fdget(fd); | |
6476 | if (!f.file) | |
6477 | return -EBADF; | |
6478 | ||
6479 | ret = -EOPNOTSUPP; | |
6480 | if (f.file->f_op != &io_uring_fops) | |
6481 | goto out_fput; | |
6482 | ||
6483 | ret = -ENXIO; | |
6484 | ctx = f.file->private_data; | |
6485 | if (!percpu_ref_tryget(&ctx->refs)) | |
6486 | goto out_fput; | |
6487 | ||
6c271ce2 JA |
6488 | /* |
6489 | * For SQ polling, the thread will do all submissions and completions. | |
6490 | * Just return the requested submit count, and wake the thread if | |
6491 | * we were asked to. | |
6492 | */ | |
b2a9eada | 6493 | ret = 0; |
6c271ce2 | 6494 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
c1edbf5f JA |
6495 | if (!list_empty_careful(&ctx->cq_overflow_list)) |
6496 | io_cqring_overflow_flush(ctx, false); | |
6c271ce2 JA |
6497 | if (flags & IORING_ENTER_SQ_WAKEUP) |
6498 | wake_up(&ctx->sqo_wait); | |
6499 | submitted = to_submit; | |
b2a9eada | 6500 | } else if (to_submit) { |
ae9428ca | 6501 | struct mm_struct *cur_mm; |
2b188cc1 JA |
6502 | |
6503 | mutex_lock(&ctx->uring_lock); | |
ae9428ca PB |
6504 | /* already have mm, so io_submit_sqes() won't try to grab it */ |
6505 | cur_mm = ctx->sqo_mm; | |
6506 | submitted = io_submit_sqes(ctx, to_submit, f.file, fd, | |
6507 | &cur_mm, false); | |
2b188cc1 | 6508 | mutex_unlock(&ctx->uring_lock); |
7c504e65 PB |
6509 | |
6510 | if (submitted != to_submit) | |
6511 | goto out; | |
2b188cc1 JA |
6512 | } |
6513 | if (flags & IORING_ENTER_GETEVENTS) { | |
def596e9 JA |
6514 | unsigned nr_events = 0; |
6515 | ||
2b188cc1 JA |
6516 | min_complete = min(min_complete, ctx->cq_entries); |
6517 | ||
def596e9 | 6518 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
def596e9 | 6519 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
def596e9 JA |
6520 | } else { |
6521 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); | |
6522 | } | |
2b188cc1 JA |
6523 | } |
6524 | ||
7c504e65 | 6525 | out: |
6805b32e | 6526 | percpu_ref_put(&ctx->refs); |
2b188cc1 JA |
6527 | out_fput: |
6528 | fdput(f); | |
6529 | return submitted ? submitted : ret; | |
6530 | } | |
6531 | ||
87ce955b JA |
6532 | static int io_uring_show_cred(int id, void *p, void *data) |
6533 | { | |
6534 | const struct cred *cred = p; | |
6535 | struct seq_file *m = data; | |
6536 | struct user_namespace *uns = seq_user_ns(m); | |
6537 | struct group_info *gi; | |
6538 | kernel_cap_t cap; | |
6539 | unsigned __capi; | |
6540 | int g; | |
6541 | ||
6542 | seq_printf(m, "%5d\n", id); | |
6543 | seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid)); | |
6544 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid)); | |
6545 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid)); | |
6546 | seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid)); | |
6547 | seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid)); | |
6548 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid)); | |
6549 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid)); | |
6550 | seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid)); | |
6551 | seq_puts(m, "\n\tGroups:\t"); | |
6552 | gi = cred->group_info; | |
6553 | for (g = 0; g < gi->ngroups; g++) { | |
6554 | seq_put_decimal_ull(m, g ? " " : "", | |
6555 | from_kgid_munged(uns, gi->gid[g])); | |
6556 | } | |
6557 | seq_puts(m, "\n\tCapEff:\t"); | |
6558 | cap = cred->cap_effective; | |
6559 | CAP_FOR_EACH_U32(__capi) | |
6560 | seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8); | |
6561 | seq_putc(m, '\n'); | |
6562 | return 0; | |
6563 | } | |
6564 | ||
6565 | static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) | |
6566 | { | |
6567 | int i; | |
6568 | ||
6569 | mutex_lock(&ctx->uring_lock); | |
6570 | seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files); | |
6571 | for (i = 0; i < ctx->nr_user_files; i++) { | |
6572 | struct fixed_file_table *table; | |
6573 | struct file *f; | |
6574 | ||
6575 | table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT]; | |
6576 | f = table->files[i & IORING_FILE_TABLE_MASK]; | |
6577 | if (f) | |
6578 | seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname); | |
6579 | else | |
6580 | seq_printf(m, "%5u: <none>\n", i); | |
6581 | } | |
6582 | seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs); | |
6583 | for (i = 0; i < ctx->nr_user_bufs; i++) { | |
6584 | struct io_mapped_ubuf *buf = &ctx->user_bufs[i]; | |
6585 | ||
6586 | seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, | |
6587 | (unsigned int) buf->len); | |
6588 | } | |
6589 | if (!idr_is_empty(&ctx->personality_idr)) { | |
6590 | seq_printf(m, "Personalities:\n"); | |
6591 | idr_for_each(&ctx->personality_idr, io_uring_show_cred, m); | |
6592 | } | |
6593 | mutex_unlock(&ctx->uring_lock); | |
6594 | } | |
6595 | ||
6596 | static void io_uring_show_fdinfo(struct seq_file *m, struct file *f) | |
6597 | { | |
6598 | struct io_ring_ctx *ctx = f->private_data; | |
6599 | ||
6600 | if (percpu_ref_tryget(&ctx->refs)) { | |
6601 | __io_uring_show_fdinfo(ctx, m); | |
6602 | percpu_ref_put(&ctx->refs); | |
6603 | } | |
6604 | } | |
6605 | ||
2b188cc1 JA |
6606 | static const struct file_operations io_uring_fops = { |
6607 | .release = io_uring_release, | |
fcb323cc | 6608 | .flush = io_uring_flush, |
2b188cc1 | 6609 | .mmap = io_uring_mmap, |
6c5c240e RP |
6610 | #ifndef CONFIG_MMU |
6611 | .get_unmapped_area = io_uring_nommu_get_unmapped_area, | |
6612 | .mmap_capabilities = io_uring_nommu_mmap_capabilities, | |
6613 | #endif | |
2b188cc1 JA |
6614 | .poll = io_uring_poll, |
6615 | .fasync = io_uring_fasync, | |
87ce955b | 6616 | .show_fdinfo = io_uring_show_fdinfo, |
2b188cc1 JA |
6617 | }; |
6618 | ||
6619 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, | |
6620 | struct io_uring_params *p) | |
6621 | { | |
75b28aff HV |
6622 | struct io_rings *rings; |
6623 | size_t size, sq_array_offset; | |
2b188cc1 | 6624 | |
75b28aff HV |
6625 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
6626 | if (size == SIZE_MAX) | |
6627 | return -EOVERFLOW; | |
6628 | ||
6629 | rings = io_mem_alloc(size); | |
6630 | if (!rings) | |
2b188cc1 JA |
6631 | return -ENOMEM; |
6632 | ||
75b28aff HV |
6633 | ctx->rings = rings; |
6634 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); | |
6635 | rings->sq_ring_mask = p->sq_entries - 1; | |
6636 | rings->cq_ring_mask = p->cq_entries - 1; | |
6637 | rings->sq_ring_entries = p->sq_entries; | |
6638 | rings->cq_ring_entries = p->cq_entries; | |
6639 | ctx->sq_mask = rings->sq_ring_mask; | |
6640 | ctx->cq_mask = rings->cq_ring_mask; | |
6641 | ctx->sq_entries = rings->sq_ring_entries; | |
6642 | ctx->cq_entries = rings->cq_ring_entries; | |
2b188cc1 JA |
6643 | |
6644 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); | |
eb065d30 JA |
6645 | if (size == SIZE_MAX) { |
6646 | io_mem_free(ctx->rings); | |
6647 | ctx->rings = NULL; | |
2b188cc1 | 6648 | return -EOVERFLOW; |
eb065d30 | 6649 | } |
2b188cc1 JA |
6650 | |
6651 | ctx->sq_sqes = io_mem_alloc(size); | |
eb065d30 JA |
6652 | if (!ctx->sq_sqes) { |
6653 | io_mem_free(ctx->rings); | |
6654 | ctx->rings = NULL; | |
2b188cc1 | 6655 | return -ENOMEM; |
eb065d30 | 6656 | } |
2b188cc1 | 6657 | |
2b188cc1 JA |
6658 | return 0; |
6659 | } | |
6660 | ||
6661 | /* | |
6662 | * Allocate an anonymous fd, this is what constitutes the application | |
6663 | * visible backing of an io_uring instance. The application mmaps this | |
6664 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, | |
6665 | * we have to tie this fd to a socket for file garbage collection purposes. | |
6666 | */ | |
6667 | static int io_uring_get_fd(struct io_ring_ctx *ctx) | |
6668 | { | |
6669 | struct file *file; | |
6670 | int ret; | |
6671 | ||
6672 | #if defined(CONFIG_UNIX) | |
6673 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, | |
6674 | &ctx->ring_sock); | |
6675 | if (ret) | |
6676 | return ret; | |
6677 | #endif | |
6678 | ||
6679 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); | |
6680 | if (ret < 0) | |
6681 | goto err; | |
6682 | ||
6683 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, | |
6684 | O_RDWR | O_CLOEXEC); | |
6685 | if (IS_ERR(file)) { | |
6686 | put_unused_fd(ret); | |
6687 | ret = PTR_ERR(file); | |
6688 | goto err; | |
6689 | } | |
6690 | ||
6691 | #if defined(CONFIG_UNIX) | |
6692 | ctx->ring_sock->file = file; | |
6693 | #endif | |
6694 | fd_install(ret, file); | |
6695 | return ret; | |
6696 | err: | |
6697 | #if defined(CONFIG_UNIX) | |
6698 | sock_release(ctx->ring_sock); | |
6699 | ctx->ring_sock = NULL; | |
6700 | #endif | |
6701 | return ret; | |
6702 | } | |
6703 | ||
6704 | static int io_uring_create(unsigned entries, struct io_uring_params *p) | |
6705 | { | |
6706 | struct user_struct *user = NULL; | |
6707 | struct io_ring_ctx *ctx; | |
6708 | bool account_mem; | |
6709 | int ret; | |
6710 | ||
8110c1a6 | 6711 | if (!entries) |
2b188cc1 | 6712 | return -EINVAL; |
8110c1a6 JA |
6713 | if (entries > IORING_MAX_ENTRIES) { |
6714 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
6715 | return -EINVAL; | |
6716 | entries = IORING_MAX_ENTRIES; | |
6717 | } | |
2b188cc1 JA |
6718 | |
6719 | /* | |
6720 | * Use twice as many entries for the CQ ring. It's possible for the | |
6721 | * application to drive a higher depth than the size of the SQ ring, | |
6722 | * since the sqes are only used at submission time. This allows for | |
33a107f0 JA |
6723 | * some flexibility in overcommitting a bit. If the application has |
6724 | * set IORING_SETUP_CQSIZE, it will have passed in the desired number | |
6725 | * of CQ ring entries manually. | |
2b188cc1 JA |
6726 | */ |
6727 | p->sq_entries = roundup_pow_of_two(entries); | |
33a107f0 JA |
6728 | if (p->flags & IORING_SETUP_CQSIZE) { |
6729 | /* | |
6730 | * If IORING_SETUP_CQSIZE is set, we do the same roundup | |
6731 | * to a power-of-two, if it isn't already. We do NOT impose | |
6732 | * any cq vs sq ring sizing. | |
6733 | */ | |
8110c1a6 | 6734 | if (p->cq_entries < p->sq_entries) |
33a107f0 | 6735 | return -EINVAL; |
8110c1a6 JA |
6736 | if (p->cq_entries > IORING_MAX_CQ_ENTRIES) { |
6737 | if (!(p->flags & IORING_SETUP_CLAMP)) | |
6738 | return -EINVAL; | |
6739 | p->cq_entries = IORING_MAX_CQ_ENTRIES; | |
6740 | } | |
33a107f0 JA |
6741 | p->cq_entries = roundup_pow_of_two(p->cq_entries); |
6742 | } else { | |
6743 | p->cq_entries = 2 * p->sq_entries; | |
6744 | } | |
2b188cc1 JA |
6745 | |
6746 | user = get_uid(current_user()); | |
6747 | account_mem = !capable(CAP_IPC_LOCK); | |
6748 | ||
6749 | if (account_mem) { | |
6750 | ret = io_account_mem(user, | |
6751 | ring_pages(p->sq_entries, p->cq_entries)); | |
6752 | if (ret) { | |
6753 | free_uid(user); | |
6754 | return ret; | |
6755 | } | |
6756 | } | |
6757 | ||
6758 | ctx = io_ring_ctx_alloc(p); | |
6759 | if (!ctx) { | |
6760 | if (account_mem) | |
6761 | io_unaccount_mem(user, ring_pages(p->sq_entries, | |
6762 | p->cq_entries)); | |
6763 | free_uid(user); | |
6764 | return -ENOMEM; | |
6765 | } | |
6766 | ctx->compat = in_compat_syscall(); | |
6767 | ctx->account_mem = account_mem; | |
6768 | ctx->user = user; | |
0b8c0ec7 | 6769 | ctx->creds = get_current_cred(); |
2b188cc1 JA |
6770 | |
6771 | ret = io_allocate_scq_urings(ctx, p); | |
6772 | if (ret) | |
6773 | goto err; | |
6774 | ||
6c271ce2 | 6775 | ret = io_sq_offload_start(ctx, p); |
2b188cc1 JA |
6776 | if (ret) |
6777 | goto err; | |
6778 | ||
2b188cc1 | 6779 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
75b28aff HV |
6780 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
6781 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); | |
6782 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); | |
6783 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); | |
6784 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); | |
6785 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); | |
6786 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; | |
2b188cc1 JA |
6787 | |
6788 | memset(&p->cq_off, 0, sizeof(p->cq_off)); | |
75b28aff HV |
6789 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
6790 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); | |
6791 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); | |
6792 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); | |
6793 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); | |
6794 | p->cq_off.cqes = offsetof(struct io_rings, cqes); | |
ac90f249 | 6795 | |
044c1ab3 JA |
6796 | /* |
6797 | * Install ring fd as the very last thing, so we don't risk someone | |
6798 | * having closed it before we finish setup | |
6799 | */ | |
6800 | ret = io_uring_get_fd(ctx); | |
6801 | if (ret < 0) | |
6802 | goto err; | |
6803 | ||
da8c9690 | 6804 | p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP | |
cccf0ee8 JA |
6805 | IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS | |
6806 | IORING_FEAT_CUR_PERSONALITY; | |
c826bd7a | 6807 | trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags); |
2b188cc1 JA |
6808 | return ret; |
6809 | err: | |
6810 | io_ring_ctx_wait_and_kill(ctx); | |
6811 | return ret; | |
6812 | } | |
6813 | ||
6814 | /* | |
6815 | * Sets up an aio uring context, and returns the fd. Applications asks for a | |
6816 | * ring size, we return the actual sq/cq ring sizes (among other things) in the | |
6817 | * params structure passed in. | |
6818 | */ | |
6819 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) | |
6820 | { | |
6821 | struct io_uring_params p; | |
6822 | long ret; | |
6823 | int i; | |
6824 | ||
6825 | if (copy_from_user(&p, params, sizeof(p))) | |
6826 | return -EFAULT; | |
6827 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { | |
6828 | if (p.resv[i]) | |
6829 | return -EINVAL; | |
6830 | } | |
6831 | ||
6c271ce2 | 6832 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
8110c1a6 | 6833 | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | |
24369c2e | 6834 | IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) |
2b188cc1 JA |
6835 | return -EINVAL; |
6836 | ||
6837 | ret = io_uring_create(entries, &p); | |
6838 | if (ret < 0) | |
6839 | return ret; | |
6840 | ||
6841 | if (copy_to_user(params, &p, sizeof(p))) | |
6842 | return -EFAULT; | |
6843 | ||
6844 | return ret; | |
6845 | } | |
6846 | ||
6847 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, | |
6848 | struct io_uring_params __user *, params) | |
6849 | { | |
6850 | return io_uring_setup(entries, params); | |
6851 | } | |
6852 | ||
66f4af93 JA |
6853 | static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args) |
6854 | { | |
6855 | struct io_uring_probe *p; | |
6856 | size_t size; | |
6857 | int i, ret; | |
6858 | ||
6859 | size = struct_size(p, ops, nr_args); | |
6860 | if (size == SIZE_MAX) | |
6861 | return -EOVERFLOW; | |
6862 | p = kzalloc(size, GFP_KERNEL); | |
6863 | if (!p) | |
6864 | return -ENOMEM; | |
6865 | ||
6866 | ret = -EFAULT; | |
6867 | if (copy_from_user(p, arg, size)) | |
6868 | goto out; | |
6869 | ret = -EINVAL; | |
6870 | if (memchr_inv(p, 0, size)) | |
6871 | goto out; | |
6872 | ||
6873 | p->last_op = IORING_OP_LAST - 1; | |
6874 | if (nr_args > IORING_OP_LAST) | |
6875 | nr_args = IORING_OP_LAST; | |
6876 | ||
6877 | for (i = 0; i < nr_args; i++) { | |
6878 | p->ops[i].op = i; | |
6879 | if (!io_op_defs[i].not_supported) | |
6880 | p->ops[i].flags = IO_URING_OP_SUPPORTED; | |
6881 | } | |
6882 | p->ops_len = i; | |
6883 | ||
6884 | ret = 0; | |
6885 | if (copy_to_user(arg, p, size)) | |
6886 | ret = -EFAULT; | |
6887 | out: | |
6888 | kfree(p); | |
6889 | return ret; | |
6890 | } | |
6891 | ||
071698e1 JA |
6892 | static int io_register_personality(struct io_ring_ctx *ctx) |
6893 | { | |
6894 | const struct cred *creds = get_current_cred(); | |
6895 | int id; | |
6896 | ||
6897 | id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1, | |
6898 | USHRT_MAX, GFP_KERNEL); | |
6899 | if (id < 0) | |
6900 | put_cred(creds); | |
6901 | return id; | |
6902 | } | |
6903 | ||
6904 | static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id) | |
6905 | { | |
6906 | const struct cred *old_creds; | |
6907 | ||
6908 | old_creds = idr_remove(&ctx->personality_idr, id); | |
6909 | if (old_creds) { | |
6910 | put_cred(old_creds); | |
6911 | return 0; | |
6912 | } | |
6913 | ||
6914 | return -EINVAL; | |
6915 | } | |
6916 | ||
6917 | static bool io_register_op_must_quiesce(int op) | |
6918 | { | |
6919 | switch (op) { | |
6920 | case IORING_UNREGISTER_FILES: | |
6921 | case IORING_REGISTER_FILES_UPDATE: | |
6922 | case IORING_REGISTER_PROBE: | |
6923 | case IORING_REGISTER_PERSONALITY: | |
6924 | case IORING_UNREGISTER_PERSONALITY: | |
6925 | return false; | |
6926 | default: | |
6927 | return true; | |
6928 | } | |
6929 | } | |
6930 | ||
edafccee JA |
6931 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
6932 | void __user *arg, unsigned nr_args) | |
b19062a5 JA |
6933 | __releases(ctx->uring_lock) |
6934 | __acquires(ctx->uring_lock) | |
edafccee JA |
6935 | { |
6936 | int ret; | |
6937 | ||
35fa71a0 JA |
6938 | /* |
6939 | * We're inside the ring mutex, if the ref is already dying, then | |
6940 | * someone else killed the ctx or is already going through | |
6941 | * io_uring_register(). | |
6942 | */ | |
6943 | if (percpu_ref_is_dying(&ctx->refs)) | |
6944 | return -ENXIO; | |
6945 | ||
071698e1 | 6946 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 6947 | percpu_ref_kill(&ctx->refs); |
b19062a5 | 6948 | |
05f3fb3c JA |
6949 | /* |
6950 | * Drop uring mutex before waiting for references to exit. If | |
6951 | * another thread is currently inside io_uring_enter() it might | |
6952 | * need to grab the uring_lock to make progress. If we hold it | |
6953 | * here across the drain wait, then we can deadlock. It's safe | |
6954 | * to drop the mutex here, since no new references will come in | |
6955 | * after we've killed the percpu ref. | |
6956 | */ | |
6957 | mutex_unlock(&ctx->uring_lock); | |
c150368b | 6958 | ret = wait_for_completion_interruptible(&ctx->completions[0]); |
05f3fb3c | 6959 | mutex_lock(&ctx->uring_lock); |
c150368b JA |
6960 | if (ret) { |
6961 | percpu_ref_resurrect(&ctx->refs); | |
6962 | ret = -EINTR; | |
6963 | goto out; | |
6964 | } | |
05f3fb3c | 6965 | } |
edafccee JA |
6966 | |
6967 | switch (opcode) { | |
6968 | case IORING_REGISTER_BUFFERS: | |
6969 | ret = io_sqe_buffer_register(ctx, arg, nr_args); | |
6970 | break; | |
6971 | case IORING_UNREGISTER_BUFFERS: | |
6972 | ret = -EINVAL; | |
6973 | if (arg || nr_args) | |
6974 | break; | |
6975 | ret = io_sqe_buffer_unregister(ctx); | |
6976 | break; | |
6b06314c JA |
6977 | case IORING_REGISTER_FILES: |
6978 | ret = io_sqe_files_register(ctx, arg, nr_args); | |
6979 | break; | |
6980 | case IORING_UNREGISTER_FILES: | |
6981 | ret = -EINVAL; | |
6982 | if (arg || nr_args) | |
6983 | break; | |
6984 | ret = io_sqe_files_unregister(ctx); | |
6985 | break; | |
c3a31e60 JA |
6986 | case IORING_REGISTER_FILES_UPDATE: |
6987 | ret = io_sqe_files_update(ctx, arg, nr_args); | |
6988 | break; | |
9b402849 | 6989 | case IORING_REGISTER_EVENTFD: |
f2842ab5 | 6990 | case IORING_REGISTER_EVENTFD_ASYNC: |
9b402849 JA |
6991 | ret = -EINVAL; |
6992 | if (nr_args != 1) | |
6993 | break; | |
6994 | ret = io_eventfd_register(ctx, arg); | |
f2842ab5 JA |
6995 | if (ret) |
6996 | break; | |
6997 | if (opcode == IORING_REGISTER_EVENTFD_ASYNC) | |
6998 | ctx->eventfd_async = 1; | |
6999 | else | |
7000 | ctx->eventfd_async = 0; | |
9b402849 JA |
7001 | break; |
7002 | case IORING_UNREGISTER_EVENTFD: | |
7003 | ret = -EINVAL; | |
7004 | if (arg || nr_args) | |
7005 | break; | |
7006 | ret = io_eventfd_unregister(ctx); | |
7007 | break; | |
66f4af93 JA |
7008 | case IORING_REGISTER_PROBE: |
7009 | ret = -EINVAL; | |
7010 | if (!arg || nr_args > 256) | |
7011 | break; | |
7012 | ret = io_probe(ctx, arg, nr_args); | |
7013 | break; | |
071698e1 JA |
7014 | case IORING_REGISTER_PERSONALITY: |
7015 | ret = -EINVAL; | |
7016 | if (arg || nr_args) | |
7017 | break; | |
7018 | ret = io_register_personality(ctx); | |
7019 | break; | |
7020 | case IORING_UNREGISTER_PERSONALITY: | |
7021 | ret = -EINVAL; | |
7022 | if (arg) | |
7023 | break; | |
7024 | ret = io_unregister_personality(ctx, nr_args); | |
7025 | break; | |
edafccee JA |
7026 | default: |
7027 | ret = -EINVAL; | |
7028 | break; | |
7029 | } | |
7030 | ||
071698e1 | 7031 | if (io_register_op_must_quiesce(opcode)) { |
05f3fb3c | 7032 | /* bring the ctx back to life */ |
05f3fb3c | 7033 | percpu_ref_reinit(&ctx->refs); |
c150368b JA |
7034 | out: |
7035 | reinit_completion(&ctx->completions[0]); | |
05f3fb3c | 7036 | } |
edafccee JA |
7037 | return ret; |
7038 | } | |
7039 | ||
7040 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, | |
7041 | void __user *, arg, unsigned int, nr_args) | |
7042 | { | |
7043 | struct io_ring_ctx *ctx; | |
7044 | long ret = -EBADF; | |
7045 | struct fd f; | |
7046 | ||
7047 | f = fdget(fd); | |
7048 | if (!f.file) | |
7049 | return -EBADF; | |
7050 | ||
7051 | ret = -EOPNOTSUPP; | |
7052 | if (f.file->f_op != &io_uring_fops) | |
7053 | goto out_fput; | |
7054 | ||
7055 | ctx = f.file->private_data; | |
7056 | ||
7057 | mutex_lock(&ctx->uring_lock); | |
7058 | ret = __io_uring_register(ctx, opcode, arg, nr_args); | |
7059 | mutex_unlock(&ctx->uring_lock); | |
c826bd7a DD |
7060 | trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs, |
7061 | ctx->cq_ev_fd != NULL, ret); | |
edafccee JA |
7062 | out_fput: |
7063 | fdput(f); | |
7064 | return ret; | |
7065 | } | |
7066 | ||
2b188cc1 JA |
7067 | static int __init io_uring_init(void) |
7068 | { | |
d7f62e82 SM |
7069 | #define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \ |
7070 | BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \ | |
7071 | BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \ | |
7072 | } while (0) | |
7073 | ||
7074 | #define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \ | |
7075 | __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename) | |
7076 | BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64); | |
7077 | BUILD_BUG_SQE_ELEM(0, __u8, opcode); | |
7078 | BUILD_BUG_SQE_ELEM(1, __u8, flags); | |
7079 | BUILD_BUG_SQE_ELEM(2, __u16, ioprio); | |
7080 | BUILD_BUG_SQE_ELEM(4, __s32, fd); | |
7081 | BUILD_BUG_SQE_ELEM(8, __u64, off); | |
7082 | BUILD_BUG_SQE_ELEM(8, __u64, addr2); | |
7083 | BUILD_BUG_SQE_ELEM(16, __u64, addr); | |
7084 | BUILD_BUG_SQE_ELEM(24, __u32, len); | |
7085 | BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags); | |
7086 | BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags); | |
7087 | BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags); | |
7088 | BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags); | |
7089 | BUILD_BUG_SQE_ELEM(28, __u16, poll_events); | |
7090 | BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags); | |
7091 | BUILD_BUG_SQE_ELEM(28, __u32, msg_flags); | |
7092 | BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags); | |
7093 | BUILD_BUG_SQE_ELEM(28, __u32, accept_flags); | |
7094 | BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags); | |
7095 | BUILD_BUG_SQE_ELEM(28, __u32, open_flags); | |
7096 | BUILD_BUG_SQE_ELEM(28, __u32, statx_flags); | |
7097 | BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice); | |
7098 | BUILD_BUG_SQE_ELEM(32, __u64, user_data); | |
7099 | BUILD_BUG_SQE_ELEM(40, __u16, buf_index); | |
7100 | BUILD_BUG_SQE_ELEM(42, __u16, personality); | |
7101 | ||
d3656344 | 7102 | BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST); |
2b188cc1 JA |
7103 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
7104 | return 0; | |
7105 | }; | |
7106 | __initcall(io_uring_init); |