Skip to content

Commit 1d58a17

Browse files
Quentin PerretMarc Zyngier
authored andcommitted
KVM: arm64: Fix host stage-2 PGD refcount
The KVM page-table library refcounts the pages of concatenated stage-2 PGDs individually. However, when running KVM in protected mode, the host's stage-2 PGD is currently managed by EL2 as a single high-order compound page, which can cause the refcount of the tail pages to reach 0 when they shouldn't, hence corrupting the page-table. Fix this by introducing a new hyp_split_page() helper in the EL2 page allocator (matching the kernel's split_page() function), and make use of it from host_s2_zalloc_pages_exact(). Fixes: 1025c8c ("KVM: arm64: Wrap the host with a stage 2") Acked-by: Will Deacon <[email protected]> Suggested-by: Will Deacon <[email protected]> Signed-off-by: Quentin Perret <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent e840f42 commit 1d58a17

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

arch/arm64/kvm/hyp/include/nvhe/gfp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct hyp_pool {
2424

2525
/* Allocation */
2626
void *hyp_alloc_pages(struct hyp_pool *pool, unsigned short order);
27+
void hyp_split_page(struct hyp_page *page);
2728
void hyp_get_page(struct hyp_pool *pool, void *addr);
2829
void hyp_put_page(struct hyp_pool *pool, void *addr);
2930

arch/arm64/kvm/hyp/nvhe/mem_protect.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ const u8 pkvm_hyp_id = 1;
3535

3636
static void *host_s2_zalloc_pages_exact(size_t size)
3737
{
38-
return hyp_alloc_pages(&host_s2_pool, get_order(size));
38+
void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size));
39+
40+
hyp_split_page(hyp_virt_to_page(addr));
41+
42+
/*
43+
* The size of concatenated PGDs is always a power of two of PAGE_SIZE,
44+
* so there should be no need to free any of the tail pages to make the
45+
* allocation exact.
46+
*/
47+
WARN_ON(size != (PAGE_SIZE << get_order(size)));
48+
49+
return addr;
3950
}
4051

4152
static void *host_s2_zalloc_page(void *pool)

arch/arm64/kvm/hyp/nvhe/page_alloc.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ void hyp_get_page(struct hyp_pool *pool, void *addr)
193193
hyp_spin_unlock(&pool->lock);
194194
}
195195

196+
void hyp_split_page(struct hyp_page *p)
197+
{
198+
unsigned short order = p->order;
199+
unsigned int i;
200+
201+
p->order = 0;
202+
for (i = 1; i < (1 << order); i++) {
203+
struct hyp_page *tail = p + i;
204+
205+
tail->order = 0;
206+
hyp_set_page_refcounted(tail);
207+
}
208+
}
209+
196210
void *hyp_alloc_pages(struct hyp_pool *pool, unsigned short order)
197211
{
198212
unsigned short i = order;

0 commit comments

Comments
 (0)