|
| 1 | +From 85b1525e138e76dd43f58e8b5cfd2f0f861ae6a6 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Fuad Tabba < [email protected]> |
| 3 | +Date: Wed, 9 Jul 2025 11:59:29 +0100 |
| 4 | +Subject: [PATCH 03/45] KVM: Introduce kvm_arch_supports_gmem() |
| 5 | + |
| 6 | +Introduce kvm_arch_supports_gmem() to explicitly indicate whether an |
| 7 | +architecture supports guest_memfd. |
| 8 | + |
| 9 | +Previously, kvm_arch_has_private_mem() was used to check for guest_memfd |
| 10 | +support. However, this conflated guest_memfd with "private" memory, |
| 11 | +implying that guest_memfd was exclusively for CoCo VMs or other private |
| 12 | +memory use cases. |
| 13 | + |
| 14 | +With the expansion of guest_memfd to support non-private memory, such as |
| 15 | +shared host mappings, it is necessary to decouple these concepts. The |
| 16 | +new kvm_arch_supports_gmem() function provides a clear way to check for |
| 17 | +guest_memfd support. |
| 18 | + |
| 19 | +Reviewed-by: Ira Weiny < [email protected]> |
| 20 | +Reviewed-by: Gavin Shan < [email protected]> |
| 21 | +Reviewed-by: Shivank Garg < [email protected]> |
| 22 | +Reviewed-by: Vlastimil Babka < [email protected]> |
| 23 | +Co-developed-by: David Hildenbrand < [email protected]> |
| 24 | +Signed-off-by: David Hildenbrand < [email protected]> |
| 25 | +Signed-off-by: Fuad Tabba < [email protected]> |
| 26 | +--- |
| 27 | + arch/x86/include/asm/kvm_host.h | 4 +++- |
| 28 | + include/linux/kvm_host.h | 11 +++++++++++ |
| 29 | + virt/kvm/kvm_main.c | 4 ++-- |
| 30 | + 3 files changed, 16 insertions(+), 3 deletions(-) |
| 31 | + |
| 32 | +diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h |
| 33 | +index 66bdd0759d27..09f4f6240d9d 100644 |
| 34 | +--- a/arch/x86/include/asm/kvm_host.h |
| 35 | ++++ b/arch/x86/include/asm/kvm_host.h |
| 36 | +@@ -2271,8 +2271,10 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, |
| 37 | + |
| 38 | + #ifdef CONFIG_KVM_GMEM |
| 39 | + #define kvm_arch_has_private_mem(kvm) ((kvm)->arch.has_private_mem) |
| 40 | ++#define kvm_arch_supports_gmem(kvm) kvm_arch_has_private_mem(kvm) |
| 41 | + #else |
| 42 | + #define kvm_arch_has_private_mem(kvm) false |
| 43 | ++#define kvm_arch_supports_gmem(kvm) false |
| 44 | + #endif |
| 45 | + |
| 46 | + #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state) |
| 47 | +@@ -2325,7 +2327,7 @@ enum { |
| 48 | + #define HF_SMM_INSIDE_NMI_MASK (1 << 2) |
| 49 | + |
| 50 | + # define KVM_MAX_NR_ADDRESS_SPACES 2 |
| 51 | +-/* SMM is currently unsupported for guests with private memory. */ |
| 52 | ++/* SMM is currently unsupported for guests with guest_memfd private memory. */ |
| 53 | + # define kvm_arch_nr_memslot_as_ids(kvm) (kvm_arch_has_private_mem(kvm) ? 1 : 2) |
| 54 | + # define kvm_arch_vcpu_memslots_id(vcpu) ((vcpu)->arch.hflags & HF_SMM_MASK ? 1 : 0) |
| 55 | + # define kvm_memslots_for_spte_role(kvm, role) __kvm_memslots(kvm, (role).smm) |
| 56 | +diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h |
| 57 | +index 359baaae5e9f..ab1bde048034 100644 |
| 58 | +--- a/include/linux/kvm_host.h |
| 59 | ++++ b/include/linux/kvm_host.h |
| 60 | +@@ -729,6 +729,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm) |
| 61 | + } |
| 62 | + #endif |
| 63 | + |
| 64 | ++/* |
| 65 | ++ * Arch code must define kvm_arch_supports_gmem if support for guest_memfd is |
| 66 | ++ * enabled. |
| 67 | ++ */ |
| 68 | ++#if !defined(kvm_arch_supports_gmem) && !IS_ENABLED(CONFIG_KVM_GMEM) |
| 69 | ++static inline bool kvm_arch_supports_gmem(struct kvm *kvm) |
| 70 | ++{ |
| 71 | ++ return false; |
| 72 | ++} |
| 73 | ++#endif |
| 74 | ++ |
| 75 | + #ifndef kvm_arch_has_readonly_mem |
| 76 | + static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm) |
| 77 | + { |
| 78 | +diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c |
| 79 | +index 898c3d5a7ba8..afbc025ce4d3 100644 |
| 80 | +--- a/virt/kvm/kvm_main.c |
| 81 | ++++ b/virt/kvm/kvm_main.c |
| 82 | +@@ -1588,7 +1588,7 @@ static int check_memory_region_flags(struct kvm *kvm, |
| 83 | + { |
| 84 | + u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; |
| 85 | + |
| 86 | +- if (kvm_arch_has_private_mem(kvm)) |
| 87 | ++ if (kvm_arch_supports_gmem(kvm)) |
| 88 | + valid_flags |= KVM_MEM_GUEST_MEMFD; |
| 89 | + |
| 90 | + /* Dirty logging private memory is not currently supported. */ |
| 91 | +@@ -4912,7 +4912,7 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) |
| 92 | + #endif |
| 93 | + #ifdef CONFIG_KVM_GMEM |
| 94 | + case KVM_CAP_GUEST_MEMFD: |
| 95 | +- return !kvm || kvm_arch_has_private_mem(kvm); |
| 96 | ++ return !kvm || kvm_arch_supports_gmem(kvm); |
| 97 | + #endif |
| 98 | + default: |
| 99 | + break; |
| 100 | +-- |
| 101 | +2.49.0 |
| 102 | + |
0 commit comments