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