Re: [PATCH v3 01/25] KVM: arm64: Move hyp refcount manipulation helpers to common header file

Oliver Upton
2022-09-15T08:36:47Z

Hi Will,

On Wed, Sep 14, 2022 at 09:34:36AM +0100, Will Deacon wrote:
> From: Quentin Perret <qperret@google.com>
>
> We will soon need to manipulate 'struct hyp_page' refcounts from outside
> page_alloc.c, so move the helpers to a common header file to allow them
> to be reused easily.
>
> Signed-off-by: Quentin Perret <qperret@google.com>
> Signed-off-by: Will Deacon <will@kernel.org>
> ---
> arch/arm64/kvm/hyp/include/nvhe/memory.h | 18 ++++++++++++++++++
> arch/arm64/kvm/hyp/nvhe/page_alloc.c | 19 -------------------
> 2 files changed, 18 insertions(+), 19 deletions(-)
>
> diff --git a/arch/arm64/kvm/hyp/include/nvhe/memory.h b/arch/arm64/kvm/hyp/include/nvhe/memory.h
> index 592b7edb3edb..418b66a82a50 100644
> --- a/arch/arm64/kvm/hyp/include/nvhe/memory.h
> +++ b/arch/arm64/kvm/hyp/include/nvhe/memory.h
> @@ -45,4 +45,22 @@ static inline int hyp_page_count(void *addr)
> return p->refcount;
> }
>
> +static inline void hyp_page_ref_inc(struct hyp_page *p)
> +{
> + BUG_ON(p->refcount == USHRT_MAX);
> + p->refcount++;
> +}
> +
> +static inline int hyp_page_ref_dec_and_test(struct hyp_page *p)
> +{
> + BUG_ON(!p->refcount);
> + p->refcount--;
> + return (p->refcount == 0);
> +}
> +
> +static inline void hyp_set_page_refcounted(struct hyp_page *p)
> +{
> + BUG_ON(p->refcount);
> + p->refcount = 1;
> +}

It might be good to add a comment mentioning the hyp_pool::lock must
be held to update refcounts, as that part is slightly less clear when
the helpers are hoisted out of page_alloc.c

With that:

Reviewed-by: Oliver Upton <oliver.upton@linux.dev>

--
Thanks,
Oliver