Commit | Line | Data |
---|---|---|
73572984 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #ifndef IOU_RSRC_H | |
3 | #define IOU_RSRC_H | |
4 | ||
a1967280 | 5 | #include <linux/io_uring_types.h> |
2839ab71 JH |
6 | #include <linux/lockdep.h> |
7 | ||
0396ad37 PB |
8 | #define IO_VEC_CACHE_SOFT_CAP 256 |
9 | ||
73572984 JA |
10 | enum { |
11 | IORING_RSRC_FILE = 0, | |
12 | IORING_RSRC_BUFFER = 1, | |
13 | }; | |
14 | ||
73572984 | 15 | struct io_rsrc_node { |
4f219fcc | 16 | unsigned char type; |
ef8ae64f | 17 | int refs; |
73572984 | 18 | |
7029acd8 JA |
19 | u64 tag; |
20 | union { | |
21 | unsigned long file_ptr; | |
22 | struct io_mapped_ubuf *buf; | |
23 | }; | |
ff1256b8 JA |
24 | }; |
25 | ||
27cb27b6 KB |
26 | enum { |
27 | IO_IMU_DEST = 1 << ITER_DEST, | |
28 | IO_IMU_SOURCE = 1 << ITER_SOURCE, | |
29 | }; | |
30 | ||
ad163a7e JA |
31 | struct io_mapped_ubuf { |
32 | u64 ubuf; | |
9753c642 | 33 | unsigned int len; |
ad163a7e | 34 | unsigned int nr_bvecs; |
3d6106ae | 35 | unsigned int folio_shift; |
bfc0aa7a | 36 | refcount_t refs; |
9753c642 | 37 | unsigned long acct_pages; |
27cb27b6 KB |
38 | void (*release)(void *); |
39 | void *priv; | |
40 | bool is_kbuf; | |
41 | u8 dir; | |
04d9244c | 42 | struct bio_vec bvec[] __counted_by(nr_bvecs); |
ad163a7e JA |
43 | }; |
44 | ||
a8edbb42 CL |
45 | struct io_imu_folio_data { |
46 | /* Head folio can be partially included in the fixed buf */ | |
47 | unsigned int nr_pages_head; | |
48 | /* For non-head/tail folios, has to be fully included */ | |
49 | unsigned int nr_pages_mid; | |
50 | unsigned int folio_shift; | |
7427b0b4 | 51 | unsigned int nr_folios; |
3a3c6d61 | 52 | unsigned long first_folio_page_idx; |
a8edbb42 CL |
53 | }; |
54 | ||
ed9f3112 KB |
55 | bool io_rsrc_cache_init(struct io_ring_ctx *ctx); |
56 | void io_rsrc_cache_free(struct io_ring_ctx *ctx); | |
57 | struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type); | |
0d98c509 ML |
58 | void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node); |
59 | void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data); | |
3597f278 | 60 | int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr); |
73572984 | 61 | |
bf931be5 CSM |
62 | struct io_rsrc_node *io_find_buf_node(struct io_kiocb *req, |
63 | unsigned issue_flags); | |
5d309914 PB |
64 | int io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter, |
65 | u64 buf_addr, size_t len, int ddir, | |
66 | unsigned issue_flags); | |
9ef4cbbc PB |
67 | int io_import_reg_vec(int ddir, struct iov_iter *iter, |
68 | struct io_kiocb *req, struct iou_vec *vec, | |
146acfd0 | 69 | unsigned nr_iovs, unsigned issue_flags); |
d291fb65 PB |
70 | int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv, |
71 | const struct iovec __user *uvec, size_t uvec_segs); | |
73572984 | 72 | |
636119af | 73 | int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg); |
73572984 JA |
74 | int io_sqe_buffers_unregister(struct io_ring_ctx *ctx); |
75 | int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, | |
76 | unsigned int nr_args, u64 __user *tags); | |
73572984 JA |
77 | int io_sqe_files_unregister(struct io_ring_ctx *ctx); |
78 | int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, | |
79 | unsigned nr_args, u64 __user *tags); | |
80 | ||
73572984 JA |
81 | int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, |
82 | unsigned nr_args); | |
83 | int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
84 | unsigned size, unsigned type); | |
85 | int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, | |
86 | unsigned int size, unsigned int type); | |
d760d3f5 | 87 | int io_validate_user_buf_range(u64 uaddr, u64 ulen); |
73572984 | 88 | |
7427b0b4 PB |
89 | bool io_check_coalesce_buffer(struct page **page_array, int nr_pages, |
90 | struct io_imu_folio_data *data); | |
91 | ||
b54a1404 JA |
92 | static inline struct io_rsrc_node *io_rsrc_node_lookup(struct io_rsrc_data *data, |
93 | int index) | |
94 | { | |
95 | if (index < data->nr) | |
96 | return data->nodes[array_index_nospec(index, data->nr)]; | |
97 | return NULL; | |
98 | } | |
99 | ||
0d98c509 | 100 | static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node) |
73572984 | 101 | { |
2839ab71 | 102 | lockdep_assert_held(&ctx->uring_lock); |
496f56bf | 103 | if (!--node->refs) |
0d98c509 | 104 | io_free_rsrc_node(ctx, node); |
3f302388 JA |
105 | } |
106 | ||
0d98c509 ML |
107 | static inline bool io_reset_rsrc_node(struct io_ring_ctx *ctx, |
108 | struct io_rsrc_data *data, int index) | |
4007c3d8 JA |
109 | { |
110 | struct io_rsrc_node *node = data->nodes[index]; | |
111 | ||
112 | if (!node) | |
113 | return false; | |
0d98c509 | 114 | io_put_rsrc_node(ctx, node); |
4007c3d8 JA |
115 | data->nodes[index] = NULL; |
116 | return true; | |
117 | } | |
118 | ||
d9808ceb PB |
119 | int io_files_update(struct io_kiocb *req, unsigned int issue_flags); |
120 | int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); | |
6a9ce66f PB |
121 | |
122 | int __io_account_mem(struct user_struct *user, unsigned long nr_pages); | |
123 | ||
124 | static inline void __io_unaccount_mem(struct user_struct *user, | |
125 | unsigned long nr_pages) | |
126 | { | |
127 | atomic_long_sub(nr_pages, &user->locked_vm); | |
128 | } | |
129 | ||
e1d49959 | 130 | void io_vec_free(struct iou_vec *iv); |
9ef4cbbc | 131 | int io_vec_realloc(struct iou_vec *iv, unsigned nr_entries); |
e1d49959 PB |
132 | |
133 | static inline void io_vec_reset_iovec(struct iou_vec *iv, | |
134 | struct iovec *iovec, unsigned nr) | |
135 | { | |
136 | io_vec_free(iv); | |
137 | iv->iovec = iovec; | |
138 | iv->nr = nr; | |
139 | } | |
140 | ||
141 | static inline void io_alloc_cache_vec_kasan(struct iou_vec *iv) | |
142 | { | |
143 | if (IS_ENABLED(CONFIG_KASAN)) | |
144 | io_vec_free(iv); | |
145 | } | |
146 | ||
73572984 | 147 | #endif |