Re: [PATCH v9 3/6] KVM: mmu: Improve handling of non-refcounted pfns

From: Sean Christopherson
Date: Mon Feb 12 2024 - 22:44:45 EST


On Mon, Sep 11, 2023, David Stevens wrote:
> diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
> index c2e0ddf14dba..2ed08ae1a9be 100644
> --- a/include/linux/kvm_host.h
> +++ b/include/linux/kvm_host.h
> @@ -1185,10 +1185,31 @@ struct kvm_follow_pfn {
> bool atomic;
> /* Try to create a writable mapping even for a read fault */
> bool try_map_writable;
> + /* Usage of the returned pfn will be guared by a mmu notifier. */
> + bool guarded_by_mmu_notifier;
> + /*
> + * When false, do not return pfns for non-refcounted struct pages.
> + *
> + * TODO: This allows callers to use kvm_release_pfn on the pfns
> + * returned by gfn_to_pfn without worrying about corrupting the
> + * refcounted of non-refcounted pages. Once all callers respect
> + * is_refcounted_page, this flag should be removed.
> + */
> + bool allow_non_refcounted_struct_page;
>
> /* Outputs of __kvm_follow_pfn */
> hva_t hva;
> bool writable;
> + /*
> + * True if the returned pfn is for a page with a valid refcount. False
> + * if the returned pfn has no struct page or if the struct page is not
> + * being refcounted (e.g. tail pages of non-compound higher order
> + * allocations from IO/PFNMAP mappings).
> + *
> + * When this output flag is false, callers should not try to convert
> + * the pfn to a struct page.
> + */
> + bool is_refcounted_page;

Idea. Hopefully a good one. Rather than tracking a bool, what if we track:

struct page *refcounted_page;

and then make kvm_xxx_page_clean() wrappers around inner helpers that play nice
with NULL pages, e.g.

static inline void kvm_release_page_clean(struct page *page)
{
if (!page)
return

__kvm_release_page_clean(page);
}

Then callers of __kvm_follow_pfn() can do:

kvm_release_page_clean(fault->refcounted_page);

instead of

if (fault->is_refcounted_page)
kvm_release_page_clean(pfn_to_page(fault->pfn));