KVM: Provide generic interface for checking memory private/shared status

Introduce a generic kvm_is_private_gfn() interface using a static call to
determine if a GFN is private. This allows the implementation for checking
a GFN's private/shared status to be set at runtime.

In preparation for choosing implementations between a guest_memfd lookup
and the existing VM attribute lookup, rename the existing
VM-attribute-based check to kvm_vm_is_private_gfn() to emphasize that it
looks up VM attributes.

Signed-off-by: Ackerley Tng <ackerleytng@google.com>
Tested-by: Shivank Garg <shivankg@amd.com>
Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>
Reviewed-by: Fuad Tabba <tabba@google.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Tested-by: Yan Zhao <yan.y.zhao@intel.com>
Link: https://patch.msgid.link/20260910-gmem-inplace-conversion-v13-8-dd6fbf94f4e1@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
This commit is contained in:
Sean Christopherson 2026-09-10 16:55:34 -07:00
parent 98fd6e30bd
commit 8ab5719fbc
2 changed files with 27 additions and 2 deletions

View File

@ -2573,16 +2573,26 @@ bool kvm_arch_pre_set_vm_memory_attributes(struct kvm *kvm,
bool kvm_arch_post_set_vm_memory_attributes(struct kvm *kvm,
struct kvm_gfn_range *range);
static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
static inline bool kvm_vm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
{
return kvm_get_vm_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE;
}
#endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
#ifdef kvm_arch_has_private_mem
typedef bool (kvm_is_private_gfn_t)(struct kvm *kvm, gfn_t gfn);
DECLARE_STATIC_CALL(__kvm_is_private_gfn, kvm_is_private_gfn_t);
static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
{
return static_call(__kvm_is_private_gfn)(kvm, gfn);
}
#else
static inline bool kvm_is_private_gfn(struct kvm *kvm, gfn_t gfn)
{
return false;
}
#endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
#endif /* kvm_arch_has_private_mem */
#ifdef CONFIG_KVM_GUEST_MEMFD
int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot,

View File

@ -2647,6 +2647,20 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm,
}
#endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */
#ifdef kvm_arch_has_private_mem
DEFINE_STATIC_CALL_RET0(__kvm_is_private_gfn, kvm_is_private_gfn_t);
EXPORT_STATIC_CALL_GPL(__kvm_is_private_gfn);
static void kvm_init_memory_attributes(void)
{
#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES
static_call_update(__kvm_is_private_gfn, kvm_vm_is_private_gfn);
#endif
}
#else
static void kvm_init_memory_attributes(void) { }
#endif
struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
{
return __gfn_to_memslot(kvm_memslots(kvm), gfn);
@ -6560,6 +6574,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module)
kvm_preempt_ops.sched_in = kvm_sched_in;
kvm_preempt_ops.sched_out = kvm_sched_out;
kvm_init_memory_attributes();
kvm_init_debug();
r = kvm_vfio_ops_init();