Re: [PATCH v10 7/8] KVM: x86/mmu: Track if sptes refer to refcounted pages

From: Sean Christopherson
Date: Fri Feb 23 2024 - 12:39:40 EST


On Wed, Feb 21, 2024, David Stevens wrote:
> + * Extra bits are only available for TDP SPTEs, since bits 62:52 are reserved
> + * for PAE paging, including NPT PAE. When a tracking bit isn't available, we
> + * will reject mapping non-refcounted struct pages.
> + */
> +static inline bool spte_has_refcount_bit(void)
> +{
> + return tdp_enabled && IS_ENABLED(CONFIG_X86_64);
> +}
> +
> +static inline bool is_refcounted_page_spte(u64 spte)
> +{
> + return !spte_has_refcount_bit() || (spte & SPTE_MMU_PAGE_REFCOUNTED);

The preferred way to handle this in KVM's MMU is to use a global variable, e.g.

extern u64 __read_mostly shadow_refcounted_mask;

that avoids conditional branches when creating SPTEs, and arguably yields slightly
more readable code, e.g.

return !shadow_refcounted_mask || (spte & shadow_refcounted_mask);