Skip to content

Commit 2ea2ff9

Browse files
Quentin PerretMarc Zyngier
authored andcommitted
KVM: arm64: Refcount hyp stage-1 pgtable pages
To prepare the ground for allowing hyp stage-1 mappings to be removed at run-time, update the KVM page-table code to maintain a correct refcount using the ->{get,put}_page() function callbacks. Signed-off-by: Quentin Perret <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 1fac3cf commit 2ea2ff9

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

arch/arm64/kvm/hyp/pgtable.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -383,21 +383,6 @@ enum kvm_pgtable_prot kvm_pgtable_hyp_pte_prot(kvm_pte_t pte)
383383
return prot;
384384
}
385385

386-
static bool hyp_pte_needs_update(kvm_pte_t old, kvm_pte_t new)
387-
{
388-
/*
389-
* Tolerate KVM recreating the exact same mapping, or changing software
390-
* bits if the existing mapping was valid.
391-
*/
392-
if (old == new)
393-
return false;
394-
395-
if (!kvm_pte_valid(old))
396-
return true;
397-
398-
return !WARN_ON((old ^ new) & ~KVM_PTE_LEAF_ATTR_HI_SW);
399-
}
400-
401386
static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level,
402387
kvm_pte_t *ptep, struct hyp_map_data *data)
403388
{
@@ -407,11 +392,16 @@ static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level,
407392
if (!kvm_block_mapping_supported(addr, end, phys, level))
408393
return false;
409394

395+
data->phys += granule;
410396
new = kvm_init_valid_leaf_pte(phys, data->attr, level);
411-
if (hyp_pte_needs_update(old, new))
412-
smp_store_release(ptep, new);
397+
if (old == new)
398+
return true;
399+
if (!kvm_pte_valid(old))
400+
data->mm_ops->get_page(ptep);
401+
else if (WARN_ON((old ^ new) & ~KVM_PTE_LEAF_ATTR_HI_SW))
402+
return false;
413403

414-
data->phys += granule;
404+
smp_store_release(ptep, new);
415405
return true;
416406
}
417407

@@ -433,6 +423,7 @@ static int hyp_map_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
433423
return -ENOMEM;
434424

435425
kvm_set_table_pte(ptep, childp, mm_ops);
426+
mm_ops->get_page(ptep);
436427
return 0;
437428
}
438429

@@ -482,16 +473,24 @@ static int hyp_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
482473
enum kvm_pgtable_walk_flags flag, void * const arg)
483474
{
484475
struct kvm_pgtable_mm_ops *mm_ops = arg;
476+
kvm_pte_t pte = *ptep;
477+
478+
if (!kvm_pte_valid(pte))
479+
return 0;
480+
481+
mm_ops->put_page(ptep);
482+
483+
if (kvm_pte_table(pte, level))
484+
mm_ops->put_page(kvm_pte_follow(pte, mm_ops));
485485

486-
mm_ops->put_page((void *)kvm_pte_follow(*ptep, mm_ops));
487486
return 0;
488487
}
489488

490489
void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt)
491490
{
492491
struct kvm_pgtable_walker walker = {
493492
.cb = hyp_free_walker,
494-
.flags = KVM_PGTABLE_WALK_TABLE_POST,
493+
.flags = KVM_PGTABLE_WALK_LEAF | KVM_PGTABLE_WALK_TABLE_POST,
495494
.arg = pgt->mm_ops,
496495
};
497496

0 commit comments

Comments
 (0)