Commit | Line | Data |
---|---|---|
73572984 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #ifndef IOU_RSRC_H | |
3 | #define IOU_RSRC_H | |
4 | ||
5 | #include <net/af_unix.h> | |
6 | ||
9eae8655 PB |
7 | #include "alloc_cache.h" |
8 | ||
69bbc6ad PB |
9 | #define IO_NODE_ALLOC_CACHE_MAX 32 |
10 | ||
73572984 JA |
11 | #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3) |
12 | #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT) | |
13 | #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1) | |
14 | ||
15 | enum { | |
16 | IORING_RSRC_FILE = 0, | |
17 | IORING_RSRC_BUFFER = 1, | |
18 | }; | |
19 | ||
20 | struct io_rsrc_put { | |
73572984 JA |
21 | u64 tag; |
22 | union { | |
23 | void *rsrc; | |
24 | struct file *file; | |
25 | struct io_mapped_ubuf *buf; | |
26 | }; | |
27 | }; | |
28 | ||
29 | typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc); | |
30 | ||
31 | struct io_rsrc_data { | |
32 | struct io_ring_ctx *ctx; | |
33 | ||
34 | u64 **tags; | |
35 | unsigned int nr; | |
fc7f3a8d | 36 | u16 rsrc_type; |
73572984 JA |
37 | bool quiesce; |
38 | }; | |
39 | ||
40 | struct io_rsrc_node { | |
9eae8655 PB |
41 | union { |
42 | struct io_cache_entry cache; | |
2236b390 | 43 | struct io_ring_ctx *ctx; |
9eae8655 | 44 | }; |
ef8ae64f | 45 | int refs; |
26147da3 | 46 | bool empty; |
2236b390 | 47 | u16 type; |
c376644f PB |
48 | struct list_head node; |
49 | struct io_rsrc_put item; | |
73572984 JA |
50 | }; |
51 | ||
ad163a7e JA |
52 | struct io_mapped_ubuf { |
53 | u64 ubuf; | |
54 | u64 ubuf_end; | |
55 | unsigned int nr_bvecs; | |
56 | unsigned long acct_pages; | |
57 | struct bio_vec bvec[]; | |
58 | }; | |
59 | ||
d34b1b0b | 60 | void io_rsrc_put_tw(struct callback_head *cb); |
b8fb5b4f | 61 | void io_rsrc_node_ref_zero(struct io_rsrc_node *node); |
9eae8655 | 62 | void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *ref_node); |
2933ae6e | 63 | struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx); |
63fea890 | 64 | int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc); |
73572984 | 65 | |
c059f785 PB |
66 | int io_import_fixed(int ddir, struct iov_iter *iter, |
67 | struct io_mapped_ubuf *imu, | |
68 | u64 buf_addr, size_t len); | |
73572984 JA |
69 | |
70 | void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx); | |
71 | int io_sqe_buffers_unregister(struct io_ring_ctx *ctx); | |
72 | int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, | |
73 | unsigned int nr_args, u64 __user *tags); | |
74 | void __io_sqe_files_unregister(struct io_ring_ctx *ctx); | |
75 | int io_sqe_files_unregister(struct io_ring_ctx *ctx); | |
76 | int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, | |
77 | unsigned nr_args, u64 __user *tags); | |
78 | ||
79 | int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file); | |
80 | ||
81 | #if defined(CONFIG_UNIX) | |
82 | static inline bool io_file_need_scm(struct file *filp) | |
83 | { | |
73572984 | 84 | return !!unix_get_socket(filp); |
73572984 JA |
85 | } |
86 | #else | |
87 | static inline bool io_file_need_scm(struct file *filp) | |
88 | { | |
89 | return false; | |
90 | } | |
91 | #endif | |
92 | ||
93 | static inline int io_scm_file_account(struct io_ring_ctx *ctx, | |
94 | struct file *file) | |
95 | { | |
96 | if (likely(!io_file_need_scm(file))) | |
97 | return 0; | |
98 | return __io_scm_file_account(ctx, file); | |
99 | } | |
100 | ||
101 | int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg, | |
102 | unsigned nr_args); | |
103 | int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, | |
104 | unsigned size, unsigned type); | |
105 | int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg, | |
106 | unsigned int size, unsigned int type); | |
107 | ||
1f2c8f61 | 108 | static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node) |
73572984 | 109 | { |
1f2c8f61 PB |
110 | lockdep_assert_held(&ctx->uring_lock); |
111 | ||
ef8ae64f PB |
112 | if (node && !--node->refs) |
113 | io_rsrc_node_ref_zero(node); | |
73572984 JA |
114 | } |
115 | ||
116 | static inline void io_req_put_rsrc_locked(struct io_kiocb *req, | |
117 | struct io_ring_ctx *ctx) | |
73572984 | 118 | { |
1f2c8f61 | 119 | io_put_rsrc_node(ctx, req->rsrc_node); |
73572984 JA |
120 | } |
121 | ||
8e15c0e7 PB |
122 | static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx, |
123 | struct io_rsrc_node *node) | |
68ef5578 | 124 | { |
ef8ae64f | 125 | node->refs++; |
68ef5578 PB |
126 | } |
127 | ||
73572984 JA |
128 | static inline void io_req_set_rsrc_node(struct io_kiocb *req, |
129 | struct io_ring_ctx *ctx, | |
130 | unsigned int issue_flags) | |
131 | { | |
132 | if (!req->rsrc_node) { | |
4ff0b50d | 133 | io_ring_submit_lock(ctx, issue_flags); |
73572984 | 134 | |
4ff0b50d | 135 | lockdep_assert_held(&ctx->uring_lock); |
68ef5578 | 136 | |
4ff0b50d | 137 | req->rsrc_node = ctx->rsrc_node; |
8e15c0e7 | 138 | io_charge_rsrc_node(ctx, ctx->rsrc_node); |
4ff0b50d | 139 | io_ring_submit_unlock(ctx, issue_flags); |
73572984 JA |
140 | } |
141 | } | |
142 | ||
143 | static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx) | |
144 | { | |
145 | unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK; | |
146 | unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT; | |
147 | ||
148 | return &data->tags[table_idx][off]; | |
149 | } | |
150 | ||
2933ae6e PB |
151 | static inline int io_rsrc_init(struct io_ring_ctx *ctx) |
152 | { | |
153 | ctx->rsrc_node = io_rsrc_node_alloc(ctx); | |
154 | return ctx->rsrc_node ? 0 : -ENOMEM; | |
155 | } | |
156 | ||
d9808ceb PB |
157 | int io_files_update(struct io_kiocb *req, unsigned int issue_flags); |
158 | int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); | |
6a9ce66f PB |
159 | |
160 | int __io_account_mem(struct user_struct *user, unsigned long nr_pages); | |
161 | ||
162 | static inline void __io_unaccount_mem(struct user_struct *user, | |
163 | unsigned long nr_pages) | |
164 | { | |
165 | atomic_long_sub(nr_pages, &user->locked_vm); | |
166 | } | |
167 | ||
73572984 | 168 | #endif |