[PATCH v5 42/43] arm64: kvm: Expose support for private memory

Steven Price posted 43 patches 1 month, 3 weeks ago
[PATCH v5 42/43] arm64: kvm: Expose support for private memory
Posted by Steven Price 1 month, 3 weeks ago
Select KVM_GENERIC_PRIVATE_MEM and provide the necessary support
functions.

Signed-off-by: Steven Price <steven.price@arm.com>
---
Changes since v2:
 * Switch kvm_arch_has_private_mem() to a macro to avoid overhead of a
   function call.
 * Guard definitions of kvm_arch_{pre,post}_set_memory_attributes() with
   #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES.
 * Early out in kvm_arch_post_set_memory_attributes() if the WARN_ON
   should trigger.
---
 arch/arm64/include/asm/kvm_host.h |  6 ++++++
 arch/arm64/kvm/Kconfig            |  1 +
 arch/arm64/kvm/mmu.c              | 22 ++++++++++++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 1dbb45927e03..b9efaf967f29 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -1385,6 +1385,12 @@ struct kvm *kvm_arch_alloc_vm(void);
 
 #define vcpu_is_protected(vcpu)		kvm_vm_is_protected((vcpu)->kvm)
 
+#ifdef CONFIG_KVM_PRIVATE_MEM
+#define kvm_arch_has_private_mem(kvm) ((kvm)->arch.is_realm)
+#else
+#define kvm_arch_has_private_mem(kvm) false
+#endif
+
 int kvm_arm_vcpu_finalize(struct kvm_vcpu *vcpu, int feature);
 bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
 
diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig
index ead632ad01b4..7bc1a2c89b3a 100644
--- a/arch/arm64/kvm/Kconfig
+++ b/arch/arm64/kvm/Kconfig
@@ -38,6 +38,7 @@ menuconfig KVM
 	select HAVE_KVM_VCPU_RUN_PID_CHANGE
 	select SCHED_INFO
 	select GUEST_PERF_EVENTS if PERF_EVENTS
+	select KVM_GENERIC_PRIVATE_MEM
 	help
 	  Support hosting virtualized guest machines.
 
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 602c49eae90d..26d550ad8393 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -2293,6 +2293,28 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 	return ret;
 }
 
+#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
+bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
+					struct kvm_gfn_range *range)
+{
+	WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm));
+	return false;
+}
+
+bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
+					 struct kvm_gfn_range *range)
+{
+	if (WARN_ON_ONCE(!kvm_arch_has_private_mem(kvm)))
+		return false;
+
+	if (range->arg.attributes & KVM_MEMORY_ATTRIBUTE_PRIVATE)
+		range->only_shared = true;
+	kvm_unmap_gfn_range(kvm, range);
+
+	return false;
+}
+#endif
+
 void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
 {
 }
-- 
2.34.1
Re: [PATCH v5 42/43] arm64: kvm: Expose support for private memory
Posted by kernel test robot 1 month, 2 weeks ago
Hi Steven,

kernel test robot noticed the following build warnings:

[auto build test WARNING on arm64/for-next/core]
[also build test WARNING on linus/master v6.12-rc2 next-20241008]
[cannot apply to kvmarm/next kvm/queue kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Steven-Price/KVM-Prepare-for-handling-only-shared-mappings-in-mmu_notifier-events/20241005-000420
base:   https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git for-next/core
patch link:    https://lore.kernel.org/r/20241004152804.72508-43-steven.price%40arm.com
patch subject: [PATCH v5 42/43] arm64: kvm: Expose support for private memory
config: arm64-randconfig-r121-20241008 (https://download.01.org/0day-ci/archive/20241009/202410091403.EUd787Qt-lkp@intel.com/config)
compiler: aarch64-linux-gcc (GCC) 14.1.0
reproduce: (https://download.01.org/0day-ci/archive/20241009/202410091403.EUd787Qt-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202410091403.EUd787Qt-lkp@intel.com/

sparse warnings: (new ones prefixed by >>)
>> arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:563:18: sparse: sparse: incompatible types in comparison expression (different address spaces):
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:563:18: sparse:    struct file *
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:563:18: sparse:    struct file [noderef] __rcu *
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:130:17: sparse: sparse: context imbalance in 'kvm_gmem_invalidate_begin' - different lock contexts for basic block
>> arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected struct file **f @@     got struct file [noderef] __rcu ** @@
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse:     expected struct file **f
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse:     got struct file [noderef] __rcu **
>> arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected struct file **f @@     got struct file [noderef] __rcu ** @@
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse:     expected struct file **f
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse:     got struct file [noderef] __rcu **
>> arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected struct file **f @@     got struct file [noderef] __rcu ** @@
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse:     expected struct file **f
   arch/arm64/kvm/../../../virt/kvm/guest_memfd.c:302:33: sparse:     got struct file [noderef] __rcu **

vim +563 arch/arm64/kvm/../../../virt/kvm/guest_memfd.c

a7800aa80ea4d5 Sean Christopherson 2023-11-13  552  
78c4293372fe1f Paolo Bonzini       2024-07-11  553  /* Returns a locked folio on success.  */
d0d87226f53596 Paolo Bonzini       2024-07-11  554  static struct folio *
d0d87226f53596 Paolo Bonzini       2024-07-11  555  __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot,
66a644c09fbed0 Paolo Bonzini       2024-07-26  556  		   gfn_t gfn, kvm_pfn_t *pfn, bool *is_prepared,
66a644c09fbed0 Paolo Bonzini       2024-07-26  557  		   int *max_order)
a7800aa80ea4d5 Sean Christopherson 2023-11-13  558  {
a7800aa80ea4d5 Sean Christopherson 2023-11-13  559  	pgoff_t index = gfn - slot->base_gfn + slot->gmem.pgoff;
17573fd971f9e3 Paolo Bonzini       2024-04-04  560  	struct kvm_gmem *gmem = file->private_data;
a7800aa80ea4d5 Sean Christopherson 2023-11-13  561  	struct folio *folio;
a7800aa80ea4d5 Sean Christopherson 2023-11-13  562  
17573fd971f9e3 Paolo Bonzini       2024-04-04 @563  	if (file != slot->gmem.file) {
17573fd971f9e3 Paolo Bonzini       2024-04-04  564  		WARN_ON_ONCE(slot->gmem.file);
d0d87226f53596 Paolo Bonzini       2024-07-11  565  		return ERR_PTR(-EFAULT);
17573fd971f9e3 Paolo Bonzini       2024-04-04  566  	}
a7800aa80ea4d5 Sean Christopherson 2023-11-13  567  
a7800aa80ea4d5 Sean Christopherson 2023-11-13  568  	gmem = file->private_data;
fa30b0dc91c815 Paolo Bonzini       2024-04-04  569  	if (xa_load(&gmem->bindings, index) != slot) {
fa30b0dc91c815 Paolo Bonzini       2024-04-04  570  		WARN_ON_ONCE(xa_load(&gmem->bindings, index));
d0d87226f53596 Paolo Bonzini       2024-07-11  571  		return ERR_PTR(-EIO);
a7800aa80ea4d5 Sean Christopherson 2023-11-13  572  	}
a7800aa80ea4d5 Sean Christopherson 2023-11-13  573  
b85524314a3db6 Paolo Bonzini       2024-07-11  574  	folio = kvm_gmem_get_folio(file_inode(file), index);
17573fd971f9e3 Paolo Bonzini       2024-04-04  575  	if (IS_ERR(folio))
d0d87226f53596 Paolo Bonzini       2024-07-11  576  		return folio;
a7800aa80ea4d5 Sean Christopherson 2023-11-13  577  
a7800aa80ea4d5 Sean Christopherson 2023-11-13  578  	if (folio_test_hwpoison(folio)) {
c31745d2c50879 Paolo Bonzini       2024-06-11  579  		folio_unlock(folio);
c31745d2c50879 Paolo Bonzini       2024-06-11  580  		folio_put(folio);
d0d87226f53596 Paolo Bonzini       2024-07-11  581  		return ERR_PTR(-EHWPOISON);
a7800aa80ea4d5 Sean Christopherson 2023-11-13  582  	}
a7800aa80ea4d5 Sean Christopherson 2023-11-13  583  
7fbdda31b0a14f Paolo Bonzini       2024-07-11  584  	*pfn = folio_file_pfn(folio, index);
a7800aa80ea4d5 Sean Christopherson 2023-11-13  585  	if (max_order)
a7800aa80ea4d5 Sean Christopherson 2023-11-13  586  		*max_order = 0;
a7800aa80ea4d5 Sean Christopherson 2023-11-13  587  
66a644c09fbed0 Paolo Bonzini       2024-07-26  588  	*is_prepared = folio_test_uptodate(folio);
d0d87226f53596 Paolo Bonzini       2024-07-11  589  	return folio;
a7800aa80ea4d5 Sean Christopherson 2023-11-13  590  }
17573fd971f9e3 Paolo Bonzini       2024-04-04  591  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki