1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020 Google LLC
4 * Author: Quentin Perret <qperret@google.com>
5 */
6
7 #include <asm/kvm_hyp.h>
8 #include <nvhe/gfp.h>
9
10 u64 __hyp_vmemmap;
11
12 /*
13 * Index the hyp_vmemmap to find a potential buddy page, but make no assumption
14 * about its current state.
15 *
16 * Example buddy-tree for a 4-pages physically contiguous pool:
17 *
18 * o : Page 3
19 * /
20 * o-o : Page 2
21 * /
22 * / o : Page 1
23 * / /
24 * o---o-o : Page 0
25 * Order 2 1 0
26 *
27 * Example of requests on this pool:
28 * __find_buddy_nocheck(pool, page 0, order 0) => page 1
29 * __find_buddy_nocheck(pool, page 0, order 1) => page 2
30 * __find_buddy_nocheck(pool, page 1, order 0) => page 0
31 * __find_buddy_nocheck(pool, page 2, order 0) => page 3
32 */
__find_buddy_nocheck(struct hyp_pool * pool,struct hyp_page * p,u8 order)33 static struct hyp_page *__find_buddy_nocheck(struct hyp_pool *pool,
34 struct hyp_page *p,
35 u8 order)
36 {
37 phys_addr_t addr = hyp_page_to_phys(p);
38
39 addr ^= (PAGE_SIZE << order);
40
41 /*
42 * Don't return a page outside the pool range -- it belongs to
43 * something else and may not be mapped in hyp_vmemmap.
44 */
45 if (addr < pool->range_start || addr >= pool->range_end)
46 return NULL;
47
48 return hyp_phys_to_page(addr);
49 }
50
51 /* Find a buddy page currently available for allocation */
__find_buddy_avail(struct hyp_pool * pool,struct hyp_page * p,u8 order)52 static struct hyp_page *__find_buddy_avail(struct hyp_pool *pool,
53 struct hyp_page *p,
54 u8 order)
55 {
56 struct hyp_page *buddy = __find_buddy_nocheck(pool, p, order);
57
58 if (!buddy)
59 return NULL;
60
61 if (buddy->order != order || hyp_refcount_get(buddy->refcount))
62 return NULL;
63
64 return buddy;
65
66 }
67
68 /*
69 * Pages that are available for allocation are tracked in free-lists, so we use
70 * the pages themselves to store the list nodes to avoid wasting space. As the
71 * allocator always returns zeroed pages (which are zeroed on the hyp_put_page()
72 * path to optimize allocation speed), we also need to clean-up the list node in
73 * each page when we take it out of the list.
74 */
page_remove_from_list(struct hyp_page * p)75 static inline void page_remove_from_list(struct hyp_page *p)
76 {
77 struct list_head *node = hyp_page_to_virt(p);
78
79 __list_del_entry(node);
80 memset(node, 0, sizeof(*node));
81 }
82
page_add_to_list(struct hyp_page * p,struct list_head * head)83 static inline void page_add_to_list(struct hyp_page *p, struct list_head *head)
84 {
85 struct list_head *node = hyp_page_to_virt(p);
86
87 INIT_LIST_HEAD(node);
88 list_add_tail(node, head);
89 }
90
node_to_page(struct list_head * node)91 static inline struct hyp_page *node_to_page(struct list_head *node)
92 {
93 return hyp_virt_to_page(node);
94 }
95
__hyp_attach_page(struct hyp_pool * pool,struct hyp_page * p)96 static void __hyp_attach_page(struct hyp_pool *pool,
97 struct hyp_page *p)
98 {
99 phys_addr_t phys = hyp_page_to_phys(p);
100 struct hyp_page *buddy;
101 u8 order = p->order;
102
103 memset(hyp_page_to_virt(p), 0, PAGE_SIZE << p->order);
104
105 /* Skip coalescing for 'external' pages being freed into the pool. */
106 if (phys < pool->range_start || phys >= pool->range_end)
107 goto insert;
108
109 /*
110 * Only the first struct hyp_page of a high-order page (otherwise known
111 * as the 'head') should have p->order set. The non-head pages should
112 * have p->order = HYP_NO_ORDER. Here @p may no longer be the head
113 * after coalescing, so make sure to mark it HYP_NO_ORDER proactively.
114 */
115 p->order = HYP_NO_ORDER;
116 for (; (order + 1) <= pool->max_order; order++) {
117 buddy = __find_buddy_avail(pool, p, order);
118 if (!buddy)
119 break;
120
121 /* Take the buddy out of its list, and coalesce with @p */
122 page_remove_from_list(buddy);
123 buddy->order = HYP_NO_ORDER;
124 p = min(p, buddy);
125 }
126
127 insert:
128 /* Mark the new head, and insert it */
129 p->order = order;
130 page_add_to_list(p, &pool->free_area[order]);
131 }
132
__hyp_extract_page(struct hyp_pool * pool,struct hyp_page * p,u8 order)133 static struct hyp_page *__hyp_extract_page(struct hyp_pool *pool,
134 struct hyp_page *p,
135 u8 order)
136 {
137 struct hyp_page *buddy;
138
139 page_remove_from_list(p);
140 while (p->order > order) {
141 /*
142 * The buddy of order n - 1 currently has HYP_NO_ORDER as it
143 * is covered by a higher-level page (whose head is @p). Use
144 * __find_buddy_nocheck() to find it and inject it in the
145 * free_list[n - 1], effectively splitting @p in half.
146 */
147 buddy = __find_buddy_nocheck(pool, p, p->order - 1);
148 if (!buddy)
149 return p;
150 p->order--;
151 buddy->order = p->order;
152 page_add_to_list(buddy, &pool->free_area[buddy->order]);
153 }
154
155 return p;
156 }
157
__hyp_put_page(struct hyp_pool * pool,struct hyp_page * p)158 static void __hyp_put_page(struct hyp_pool *pool, struct hyp_page *p)
159 {
160 u64 free_pages;
161
162 if (hyp_page_ref_dec_and_test(p)) {
163 hyp_spin_lock(&pool->lock);
164 __hyp_attach_page(pool, p);
165 free_pages = pool->free_pages + (1 << p->order);
166 WRITE_ONCE(pool->free_pages, free_pages);
167 hyp_spin_unlock(&pool->lock);
168 }
169 }
170
hyp_put_page(struct hyp_pool * pool,void * addr)171 void hyp_put_page(struct hyp_pool *pool, void *addr)
172 {
173 struct hyp_page *p = hyp_virt_to_page(addr);
174
175 BUG_ON(p->order > pool->max_order);
176 __hyp_put_page(pool, p);
177 }
178
hyp_get_page(struct hyp_pool * pool,void * addr)179 void hyp_get_page(struct hyp_pool *pool, void *addr)
180 {
181 struct hyp_page *p = hyp_virt_to_page(addr);
182
183 hyp_page_ref_inc(p);
184 }
185
hyp_split_page(struct hyp_page * p)186 void hyp_split_page(struct hyp_page *p)
187 {
188 u8 order = p->order;
189 unsigned int i;
190
191 p->order = 0;
192 for (i = 1; i < (1 << order); i++) {
193 struct hyp_page *tail = p + i;
194
195 tail->order = 0;
196 hyp_set_page_refcounted(tail);
197 }
198 }
199
hyp_alloc_pages(struct hyp_pool * pool,u8 order)200 void *hyp_alloc_pages(struct hyp_pool *pool, u8 order)
201 {
202 struct hyp_page *p;
203 u8 i = order;
204 u64 free_pages;
205
206 hyp_spin_lock(&pool->lock);
207
208 /* Look for a high-enough-order page */
209 while (i <= pool->max_order && list_empty(&pool->free_area[i]))
210 i++;
211 if (i > pool->max_order) {
212 hyp_spin_unlock(&pool->lock);
213 return NULL;
214 }
215
216 /* Extract it from the tree at the right order */
217 p = node_to_page(pool->free_area[i].next);
218 p = __hyp_extract_page(pool, p, order);
219
220 hyp_set_page_refcounted(p);
221
222 free_pages = pool->free_pages - (1 << p->order);
223 WRITE_ONCE(pool->free_pages, free_pages);
224 hyp_spin_unlock(&pool->lock);
225
226 return hyp_page_to_virt(p);
227 }
228
229 /*
230 * Return how many pages are free at the moment.
231 * Instead of walking the free areas list + synchronization
232 * we can just keep track for allocation/deallocation in one variable
233 * with free_pages size, all updates to this variable are protected only
234 * read is not.
235 */
hyp_pool_free_pages(struct hyp_pool * pool)236 u64 hyp_pool_free_pages(struct hyp_pool *pool)
237 {
238 return READ_ONCE(pool->free_pages);
239 }
240
241 /*
242 * empty_alloc alloc set true when pool has no pages initially, but we still want
243 * to use it in the future, which means nr_pages only has to be valid to init
244 * the free areas.
245 */
__hyp_pool_init(struct hyp_pool * pool,u64 pfn,unsigned int nr_pages,unsigned int reserved_pages,bool empty_alloc)246 static int __hyp_pool_init(struct hyp_pool *pool, u64 pfn, unsigned int nr_pages,
247 unsigned int reserved_pages, bool empty_alloc)
248 {
249 phys_addr_t phys = hyp_pfn_to_phys(pfn);
250 struct hyp_page *p;
251 int i;
252
253 hyp_spin_lock_init(&pool->lock);
254 pool->max_order = min(MAX_ORDER, get_order(nr_pages << PAGE_SHIFT));
255 for (i = 0; i <= pool->max_order; i++)
256 INIT_LIST_HEAD(&pool->free_area[i]);
257
258 if (empty_alloc) {
259 /* All pages are attached from outside. */
260 pool->range_start = -1ULL;
261 pool->range_end = 0;
262 return 0;
263 }
264
265 pool->range_start = phys;
266 pool->range_end = phys + (nr_pages << PAGE_SHIFT);
267
268 /* Init the vmemmap portion */
269 p = hyp_phys_to_page(phys);
270 for (i = 0; i < nr_pages; i++)
271 hyp_set_page_refcounted(&p[i]);
272
273 /* Attach the unused pages to the buddy tree */
274 for (i = reserved_pages; i < nr_pages; i++)
275 __hyp_put_page(pool, &p[i]);
276
277 return 0;
278 }
279
hyp_pool_init(struct hyp_pool * pool,u64 pfn,unsigned int nr_pages,unsigned int reserved_pages)280 int hyp_pool_init(struct hyp_pool *pool, u64 pfn, unsigned int nr_pages,
281 unsigned int reserved_pages)
282 {
283 return __hyp_pool_init(pool, pfn, nr_pages, reserved_pages, false);
284 }
285
hyp_pool_init_empty(struct hyp_pool * pool,unsigned int nr_pages)286 int hyp_pool_init_empty(struct hyp_pool *pool, unsigned int nr_pages)
287 {
288 return __hyp_pool_init(pool, 0, nr_pages, 0, true);
289 }
290