xref: /openbmc/linux/arch/arm64/kvm/hyp/nvhe/page_alloc.c (revision 23baf831)
18e17c662SQuentin Perret // SPDX-License-Identifier: GPL-2.0-only
28e17c662SQuentin Perret /*
38e17c662SQuentin Perret  * Copyright (C) 2020 Google LLC
48e17c662SQuentin Perret  * Author: Quentin Perret <qperret@google.com>
58e17c662SQuentin Perret  */
68e17c662SQuentin Perret 
78e17c662SQuentin Perret #include <asm/kvm_hyp.h>
88e17c662SQuentin Perret #include <nvhe/gfp.h>
98e17c662SQuentin Perret 
108e17c662SQuentin Perret u64 __hyp_vmemmap;
118e17c662SQuentin Perret 
128e17c662SQuentin Perret /*
138e17c662SQuentin Perret  * Index the hyp_vmemmap to find a potential buddy page, but make no assumption
148e17c662SQuentin Perret  * about its current state.
158e17c662SQuentin Perret  *
168e17c662SQuentin Perret  * Example buddy-tree for a 4-pages physically contiguous pool:
178e17c662SQuentin Perret  *
188e17c662SQuentin Perret  *                 o : Page 3
198e17c662SQuentin Perret  *                /
208e17c662SQuentin Perret  *               o-o : Page 2
218e17c662SQuentin Perret  *              /
228e17c662SQuentin Perret  *             /   o : Page 1
238e17c662SQuentin Perret  *            /   /
248e17c662SQuentin Perret  *           o---o-o : Page 0
258e17c662SQuentin Perret  *    Order  2   1 0
268e17c662SQuentin Perret  *
278e17c662SQuentin Perret  * Example of requests on this pool:
288e17c662SQuentin Perret  *   __find_buddy_nocheck(pool, page 0, order 0) => page 1
298e17c662SQuentin Perret  *   __find_buddy_nocheck(pool, page 0, order 1) => page 2
308e17c662SQuentin Perret  *   __find_buddy_nocheck(pool, page 1, order 0) => page 0
318e17c662SQuentin Perret  *   __find_buddy_nocheck(pool, page 2, order 0) => page 3
328e17c662SQuentin Perret  */
__find_buddy_nocheck(struct hyp_pool * pool,struct hyp_page * p,unsigned short order)338e17c662SQuentin Perret static struct hyp_page *__find_buddy_nocheck(struct hyp_pool *pool,
348e17c662SQuentin Perret 					     struct hyp_page *p,
3587ec0606SQuentin Perret 					     unsigned short order)
368e17c662SQuentin Perret {
378e17c662SQuentin Perret 	phys_addr_t addr = hyp_page_to_phys(p);
388e17c662SQuentin Perret 
398e17c662SQuentin Perret 	addr ^= (PAGE_SIZE << order);
408e17c662SQuentin Perret 
418e17c662SQuentin Perret 	/*
428e17c662SQuentin Perret 	 * Don't return a page outside the pool range -- it belongs to
438e17c662SQuentin Perret 	 * something else and may not be mapped in hyp_vmemmap.
448e17c662SQuentin Perret 	 */
458e17c662SQuentin Perret 	if (addr < pool->range_start || addr >= pool->range_end)
468e17c662SQuentin Perret 		return NULL;
478e17c662SQuentin Perret 
488e17c662SQuentin Perret 	return hyp_phys_to_page(addr);
498e17c662SQuentin Perret }
508e17c662SQuentin Perret 
518e17c662SQuentin Perret /* Find a buddy page currently available for allocation */
__find_buddy_avail(struct hyp_pool * pool,struct hyp_page * p,unsigned short order)528e17c662SQuentin Perret static struct hyp_page *__find_buddy_avail(struct hyp_pool *pool,
538e17c662SQuentin Perret 					   struct hyp_page *p,
5487ec0606SQuentin Perret 					   unsigned short order)
558e17c662SQuentin Perret {
568e17c662SQuentin Perret 	struct hyp_page *buddy = __find_buddy_nocheck(pool, p, order);
578e17c662SQuentin Perret 
58581982deSQuentin Perret 	if (!buddy || buddy->order != order || buddy->refcount)
598e17c662SQuentin Perret 		return NULL;
608e17c662SQuentin Perret 
618e17c662SQuentin Perret 	return buddy;
628e17c662SQuentin Perret 
638e17c662SQuentin Perret }
648e17c662SQuentin Perret 
65914cde58SQuentin Perret /*
66914cde58SQuentin Perret  * Pages that are available for allocation are tracked in free-lists, so we use
67914cde58SQuentin Perret  * the pages themselves to store the list nodes to avoid wasting space. As the
68914cde58SQuentin Perret  * allocator always returns zeroed pages (which are zeroed on the hyp_put_page()
69914cde58SQuentin Perret  * path to optimize allocation speed), we also need to clean-up the list node in
70914cde58SQuentin Perret  * each page when we take it out of the list.
71914cde58SQuentin Perret  */
page_remove_from_list(struct hyp_page * p)72914cde58SQuentin Perret static inline void page_remove_from_list(struct hyp_page *p)
73914cde58SQuentin Perret {
74914cde58SQuentin Perret 	struct list_head *node = hyp_page_to_virt(p);
75914cde58SQuentin Perret 
76914cde58SQuentin Perret 	__list_del_entry(node);
77914cde58SQuentin Perret 	memset(node, 0, sizeof(*node));
78914cde58SQuentin Perret }
79914cde58SQuentin Perret 
page_add_to_list(struct hyp_page * p,struct list_head * head)80914cde58SQuentin Perret static inline void page_add_to_list(struct hyp_page *p, struct list_head *head)
81914cde58SQuentin Perret {
82914cde58SQuentin Perret 	struct list_head *node = hyp_page_to_virt(p);
83914cde58SQuentin Perret 
84914cde58SQuentin Perret 	INIT_LIST_HEAD(node);
85914cde58SQuentin Perret 	list_add_tail(node, head);
86914cde58SQuentin Perret }
87914cde58SQuentin Perret 
node_to_page(struct list_head * node)88914cde58SQuentin Perret static inline struct hyp_page *node_to_page(struct list_head *node)
89914cde58SQuentin Perret {
90914cde58SQuentin Perret 	return hyp_virt_to_page(node);
91914cde58SQuentin Perret }
92914cde58SQuentin Perret 
__hyp_attach_page(struct hyp_pool * pool,struct hyp_page * p)938e17c662SQuentin Perret static void __hyp_attach_page(struct hyp_pool *pool,
948e17c662SQuentin Perret 			      struct hyp_page *p)
958e17c662SQuentin Perret {
9672a5bc0fSQuentin Perret 	phys_addr_t phys = hyp_page_to_phys(p);
9787ec0606SQuentin Perret 	unsigned short order = p->order;
988e17c662SQuentin Perret 	struct hyp_page *buddy;
998e17c662SQuentin Perret 
1008e17c662SQuentin Perret 	memset(hyp_page_to_virt(p), 0, PAGE_SIZE << p->order);
1018e17c662SQuentin Perret 
10272a5bc0fSQuentin Perret 	/* Skip coalescing for 'external' pages being freed into the pool. */
10372a5bc0fSQuentin Perret 	if (phys < pool->range_start || phys >= pool->range_end)
10472a5bc0fSQuentin Perret 		goto insert;
10572a5bc0fSQuentin Perret 
1068e17c662SQuentin Perret 	/*
1078e17c662SQuentin Perret 	 * Only the first struct hyp_page of a high-order page (otherwise known
1088e17c662SQuentin Perret 	 * as the 'head') should have p->order set. The non-head pages should
1098e17c662SQuentin Perret 	 * have p->order = HYP_NO_ORDER. Here @p may no longer be the head
11021ea4578SJulia Lawall 	 * after coalescing, so make sure to mark it HYP_NO_ORDER proactively.
1118e17c662SQuentin Perret 	 */
1128e17c662SQuentin Perret 	p->order = HYP_NO_ORDER;
113*23baf831SKirill A. Shutemov 	for (; (order + 1) <= pool->max_order; order++) {
1148e17c662SQuentin Perret 		buddy = __find_buddy_avail(pool, p, order);
1158e17c662SQuentin Perret 		if (!buddy)
1168e17c662SQuentin Perret 			break;
1178e17c662SQuentin Perret 
11821ea4578SJulia Lawall 		/* Take the buddy out of its list, and coalesce with @p */
119914cde58SQuentin Perret 		page_remove_from_list(buddy);
1208e17c662SQuentin Perret 		buddy->order = HYP_NO_ORDER;
1218e17c662SQuentin Perret 		p = min(p, buddy);
1228e17c662SQuentin Perret 	}
1238e17c662SQuentin Perret 
12472a5bc0fSQuentin Perret insert:
1258e17c662SQuentin Perret 	/* Mark the new head, and insert it */
1268e17c662SQuentin Perret 	p->order = order;
127914cde58SQuentin Perret 	page_add_to_list(p, &pool->free_area[order]);
1288e17c662SQuentin Perret }
1298e17c662SQuentin Perret 
__hyp_extract_page(struct hyp_pool * pool,struct hyp_page * p,unsigned short order)1308e17c662SQuentin Perret static struct hyp_page *__hyp_extract_page(struct hyp_pool *pool,
1318e17c662SQuentin Perret 					   struct hyp_page *p,
13287ec0606SQuentin Perret 					   unsigned short order)
1338e17c662SQuentin Perret {
1348e17c662SQuentin Perret 	struct hyp_page *buddy;
1358e17c662SQuentin Perret 
136914cde58SQuentin Perret 	page_remove_from_list(p);
1378e17c662SQuentin Perret 	while (p->order > order) {
1388e17c662SQuentin Perret 		/*
1398e17c662SQuentin Perret 		 * The buddy of order n - 1 currently has HYP_NO_ORDER as it
1408e17c662SQuentin Perret 		 * is covered by a higher-level page (whose head is @p). Use
1418e17c662SQuentin Perret 		 * __find_buddy_nocheck() to find it and inject it in the
1428e17c662SQuentin Perret 		 * free_list[n - 1], effectively splitting @p in half.
1438e17c662SQuentin Perret 		 */
1448e17c662SQuentin Perret 		p->order--;
1458e17c662SQuentin Perret 		buddy = __find_buddy_nocheck(pool, p, p->order);
1468e17c662SQuentin Perret 		buddy->order = p->order;
147914cde58SQuentin Perret 		page_add_to_list(buddy, &pool->free_area[buddy->order]);
1488e17c662SQuentin Perret 	}
1498e17c662SQuentin Perret 
1508e17c662SQuentin Perret 	return p;
1518e17c662SQuentin Perret }
1528e17c662SQuentin Perret 
__hyp_put_page(struct hyp_pool * pool,struct hyp_page * p)153581982deSQuentin Perret static void __hyp_put_page(struct hyp_pool *pool, struct hyp_page *p)
154581982deSQuentin Perret {
155581982deSQuentin Perret 	if (hyp_page_ref_dec_and_test(p))
156581982deSQuentin Perret 		__hyp_attach_page(pool, p);
157581982deSQuentin Perret }
158581982deSQuentin Perret 
1596cbf874eSQuentin Perret /*
1606cbf874eSQuentin Perret  * Changes to the buddy tree and page refcounts must be done with the hyp_pool
1616cbf874eSQuentin Perret  * lock held. If a refcount change requires an update to the buddy tree (e.g.
1626cbf874eSQuentin Perret  * hyp_put_page()), both operations must be done within the same critical
1636cbf874eSQuentin Perret  * section to guarantee transient states (e.g. a page with null refcount but
1646cbf874eSQuentin Perret  * not yet attached to a free list) can't be observed by well-behaved readers.
1656cbf874eSQuentin Perret  */
hyp_put_page(struct hyp_pool * pool,void * addr)166d978b9cfSQuentin Perret void hyp_put_page(struct hyp_pool *pool, void *addr)
1678e17c662SQuentin Perret {
1688e17c662SQuentin Perret 	struct hyp_page *p = hyp_virt_to_page(addr);
1698e17c662SQuentin Perret 
1706cbf874eSQuentin Perret 	hyp_spin_lock(&pool->lock);
171581982deSQuentin Perret 	__hyp_put_page(pool, p);
1726cbf874eSQuentin Perret 	hyp_spin_unlock(&pool->lock);
1738e17c662SQuentin Perret }
1748e17c662SQuentin Perret 
hyp_get_page(struct hyp_pool * pool,void * addr)175d978b9cfSQuentin Perret void hyp_get_page(struct hyp_pool *pool, void *addr)
1768e17c662SQuentin Perret {
1778e17c662SQuentin Perret 	struct hyp_page *p = hyp_virt_to_page(addr);
1788e17c662SQuentin Perret 
1796cbf874eSQuentin Perret 	hyp_spin_lock(&pool->lock);
1808e17c662SQuentin Perret 	hyp_page_ref_inc(p);
1816cbf874eSQuentin Perret 	hyp_spin_unlock(&pool->lock);
1828e17c662SQuentin Perret }
1838e17c662SQuentin Perret 
hyp_split_page(struct hyp_page * p)1841d58a17eSQuentin Perret void hyp_split_page(struct hyp_page *p)
1851d58a17eSQuentin Perret {
1861d58a17eSQuentin Perret 	unsigned short order = p->order;
1871d58a17eSQuentin Perret 	unsigned int i;
1881d58a17eSQuentin Perret 
1891d58a17eSQuentin Perret 	p->order = 0;
1901d58a17eSQuentin Perret 	for (i = 1; i < (1 << order); i++) {
1911d58a17eSQuentin Perret 		struct hyp_page *tail = p + i;
1921d58a17eSQuentin Perret 
1931d58a17eSQuentin Perret 		tail->order = 0;
1941d58a17eSQuentin Perret 		hyp_set_page_refcounted(tail);
1951d58a17eSQuentin Perret 	}
1961d58a17eSQuentin Perret }
1971d58a17eSQuentin Perret 
hyp_alloc_pages(struct hyp_pool * pool,unsigned short order)19887ec0606SQuentin Perret void *hyp_alloc_pages(struct hyp_pool *pool, unsigned short order)
1998e17c662SQuentin Perret {
20087ec0606SQuentin Perret 	unsigned short i = order;
2018e17c662SQuentin Perret 	struct hyp_page *p;
2028e17c662SQuentin Perret 
2038e17c662SQuentin Perret 	hyp_spin_lock(&pool->lock);
2048e17c662SQuentin Perret 
2058e17c662SQuentin Perret 	/* Look for a high-enough-order page */
206*23baf831SKirill A. Shutemov 	while (i <= pool->max_order && list_empty(&pool->free_area[i]))
2078e17c662SQuentin Perret 		i++;
208*23baf831SKirill A. Shutemov 	if (i > pool->max_order) {
2098e17c662SQuentin Perret 		hyp_spin_unlock(&pool->lock);
2108e17c662SQuentin Perret 		return NULL;
2118e17c662SQuentin Perret 	}
2128e17c662SQuentin Perret 
2138e17c662SQuentin Perret 	/* Extract it from the tree at the right order */
214914cde58SQuentin Perret 	p = node_to_page(pool->free_area[i].next);
2158e17c662SQuentin Perret 	p = __hyp_extract_page(pool, p, order);
2168e17c662SQuentin Perret 
2178e17c662SQuentin Perret 	hyp_set_page_refcounted(p);
2186cbf874eSQuentin Perret 	hyp_spin_unlock(&pool->lock);
2198e17c662SQuentin Perret 
2208e17c662SQuentin Perret 	return hyp_page_to_virt(p);
2218e17c662SQuentin Perret }
2228e17c662SQuentin Perret 
hyp_pool_init(struct hyp_pool * pool,u64 pfn,unsigned int nr_pages,unsigned int reserved_pages)2238e17c662SQuentin Perret int hyp_pool_init(struct hyp_pool *pool, u64 pfn, unsigned int nr_pages,
2248e17c662SQuentin Perret 		  unsigned int reserved_pages)
2258e17c662SQuentin Perret {
2268e17c662SQuentin Perret 	phys_addr_t phys = hyp_pfn_to_phys(pfn);
2278e17c662SQuentin Perret 	struct hyp_page *p;
2288e17c662SQuentin Perret 	int i;
2298e17c662SQuentin Perret 
2308e17c662SQuentin Perret 	hyp_spin_lock_init(&pool->lock);
231*23baf831SKirill A. Shutemov 	pool->max_order = min(MAX_ORDER, get_order(nr_pages << PAGE_SHIFT));
232*23baf831SKirill A. Shutemov 	for (i = 0; i <= pool->max_order; i++)
2338e17c662SQuentin Perret 		INIT_LIST_HEAD(&pool->free_area[i]);
2348e17c662SQuentin Perret 	pool->range_start = phys;
2358e17c662SQuentin Perret 	pool->range_end = phys + (nr_pages << PAGE_SHIFT);
2368e17c662SQuentin Perret 
2378e17c662SQuentin Perret 	/* Init the vmemmap portion */
2388e17c662SQuentin Perret 	p = hyp_phys_to_page(phys);
2398e6bcc3aSQuentin Perret 	for (i = 0; i < nr_pages; i++)
240581982deSQuentin Perret 		hyp_set_page_refcounted(&p[i]);
2418e17c662SQuentin Perret 
2428e17c662SQuentin Perret 	/* Attach the unused pages to the buddy tree */
2438e17c662SQuentin Perret 	for (i = reserved_pages; i < nr_pages; i++)
244581982deSQuentin Perret 		__hyp_put_page(pool, &p[i]);
2458e17c662SQuentin Perret 
2468e17c662SQuentin Perret 	return 0;
2478e17c662SQuentin Perret }
248