Re: [PATCH v3 03/15] KVM: arm64: Refcount hyp stage-1 pgtable pages

Will Deacon
2021-12-09T10:31:02Z

On Wed, Dec 01, 2021 at 05:03:57PM +0000, Quentin Perret wrote:
> 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 <qperret@google.com>
> ---
> arch/arm64/kvm/hyp/pgtable.c | 17 ++++++++++++++---
> 1 file changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
> index f8ceebe4982e..768a58835153 100644
> --- a/arch/arm64/kvm/hyp/pgtable.c
> +++ b/arch/arm64/kvm/hyp/pgtable.c
> @@ -408,8 +408,10 @@ static bool hyp_map_walker_try_leaf(u64 addr, u64 end, u32 level,
> return false;
>
> new = kvm_init_valid_leaf_pte(phys, data->attr, level);
> - if (hyp_pte_needs_update(old, new))
> + if (hyp_pte_needs_update(old, new)) {
> smp_store_release(ptep, new);
> + data->mm_ops->get_page(ptep);

In the case where we're just updating software bits for a valid pte, doesn't
this result in us taking a spurious reference to the page?

> @@ -482,8 +485,16 @@ static int hyp_free_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep,
> enum kvm_pgtable_walk_flags flag, void * const arg)
> {
> struct kvm_pgtable_mm_ops *mm_ops = arg;
> + kvm_pte_t pte = *ptep;
> +
> + if (!kvm_pte_valid(pte))
> + return 0;
> +
> + mm_ops->put_page(ptep);
> +
> + if (kvm_pte_table(pte, level))
> + mm_ops->put_page(kvm_pte_follow(pte, mm_ops));
>
> - mm_ops->put_page((void *)kvm_pte_follow(*ptep, mm_ops));
> return 0;

This looks pretty similar to the stage-2 walker now, but given how small the
functions are, I'm not sure we'd really gain much by abstracting the "pte
counted" check.

Will