Skip to contentSkip to conversation

KVM: arm64: Map GPU device memory as cacheable

PATCH Superseded [email protected] 5 participants 33 messages Jun 21, 2025 – Jul 8, 2025

Subsystems (5)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover KVM: arm64: Map GPU device memory as cacheable

From: Ankit Agrawal <[email protected]>

Grace based platforms such as Grace Hopper/Blackwell Superchips have CPU accessible cache coherent GPU memory. The GPU device memory is essentially a DDR memory and retains properties such as cacheability, unaligned accesses, atomics and handling of executable faults. This requires the device memory to be mapped as NORMAL in stage-2.

Today KVM forces the memory to either NORMAL or DEVICE_nGnRE depending on whether the memory region is added to the kernel. The KVM code is thus restrictive and prevents device memory that is not added to the kernel to be marked as cacheable. The patch aims to solve this.

A cachebility check is made by consulting the VMA pgprot value. If the pgprot mapping type is cacheable, it is considered safe to be mapped cacheable as the KVM S2 will have the same Normal memory type as the VMA has in the S1 and KVM has no additional responsibility for safety.

Note when FWB (Force Write Back) is not enabled, the kernel expects to trivially do cache management by flushing the memory by linearly converting a kvm_pte to phys_addr to a KVA. The cache management thus relies on memory being mapped. Since the GPU device memory is not kernel mapped, exit when the FWB is not supported. Similarly, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. So the cacheable PFNMAP is made contingent on these two hardware features.

The ability to safely do the cacheable mapping of PFNMAP is exposed through a KVM capability for userspace consumption.

The changes are heavily influenced by the discussions among maintainers Marc Zyngier and Oliver Upton besides Jason Gunthorpe, Catalin Marinas, David Hildenbrand, Sean Christopherson [1]. Many thanks for their valuable suggestions.

Applied over next-20250610 and tested on the Grace Blackwell platform by booting up VM, loading NVIDIA module [2] and running nvidia-smi in the VM.

To run CUDA workloads, there is a dependency on the IOMMUFD and the Nested Page Table patches being worked on separately by Nicolin Chen. ([email protected]). NVIDIA has provided git repositories which includes all the requisite kernel [3] and Qemu [4] patches in case one wants to try.

v8 -> v9
1. Included MIXEDMAP to also be considered for cacheable mapping. (Jason Gunthorpe).
2. Minor text nits (Jason Gunthorpe).

v7 -> v8
1. Renamed device variable to s2_force_noncacheable. (Jason Gunthorpe, Catalin Marinas)
2. Updated code location that block S1 cacheable, S2 non-cacheable mapping. (Jason Gunthorpe, Catalin Marinas)
3. Added comments in the code for COW and cacheability checks. (Jason Gunthorpe, Catalin Marinas)
4. Reorganised the code to setup s2_force_noncacheable variable. (Jason Gunthorpe).
5. Collected Reviewed-By on patch 4/6. (Catalin Marinas)

v6 -> v7
1. New patch to rename symbols to more accurately reflect the CMO usage functionality (Jason Gunthorpe).
2. Updated the block cacheable PFNMAP patch invert the cacheability check function (Sean Christopherson).
3. Removed the memslot flag KVM_MEM_ENABLE_CACHEABLE_PFNMAP. (Jason Gunthorpe, Sean Christopherson, Oliver Upton).
4. Commit message changes in 2/5. (Jason Gunthorpe)

v5 -> v6
1. 2/5 updated to add kvm_arch_supports_cacheable_pfnmap weak definition to avoid build warnings. (Donald Dutile).

v4 -> v5
1. Invert the check to allow MT_DEVICE_* or NORMAL_NC instead of disallowing MT_NORMAL in 1/5. (Catalin Marinas)
2. Removed usage of stage2_has_fwb and directly using the FWB cap check. (Oliver Upton)
3. Introduced kvm_arch_supports_cacheable_pfnmap to check if the prereq features are present. (David Hildenbrand)

v3 -> v4
1. Fixed a security bug due to mismatched attributes between S1 and S2 mapping to move it to a separate patch. Suggestion by Jason Gunthorpe ([email protected]).
2. New minor patch to change the scope of the FWB support indicator function.
3. Patch to introduce a new memslot flag. Suggestion by Oliver Upton ([email protected]) and Marc Zyngier ([email protected])
4. Patch to introduce a new KVM cap to expose cacheable PFNMAP support. Suggestion by Marc Zyngier ([email protected]).
5. Added checks for ARM64_HAS_CACHE_DIC. Suggestion by Catalin Marinas ([email protected])

v2 -> v3
1. Restricted the new changes to check for cacheability to VM_PFNMAP based on David Hildenbrand's ([email protected]) suggestion.
2. Removed the MTE checks based on Jason Gunthorpe's ([email protected]) observation that it already done earlier in kvm_arch_prepare_memory_region.
3. Dropped the pfn_valid() checks based on suggestions by Catalin Marinas ([email protected]).
4. Removed the code for exec fault handling as it is not needed anymore.

v1 -> v2
1. Removed kvm_is_device_pfn() as a determiner for device type memory determination. Instead using pfn_valid()
2. Added handling for MTE.
3. Minor cleanup.

v8 Link:

Ankit Agrawal (6):
KVM: arm64: Rename the device variable to s2_force_noncacheable
KVM: arm64: Update the check to detect device memory
KVM: arm64: Block cacheable PFNMAP mapping
KVM: arm64: New function to determine hardware cache management
support
KVM: arm64: Allow cacheable stage 2 mapping using VMA flags
KVM: arm64: Expose new KVM cap for cacheable PFNMAP

 Documentation/virt/kvm/api.rst |  13 +++-
 arch/arm64/kvm/arm.c           |   7 ++
 arch/arm64/kvm/mmu.c           | 134 +++++++++++++++++++++++++++------
 include/linux/kvm_host.h       |   2 +
 include/uapi/linux/kvm.h       |   1 +
 virt/kvm/kvm_main.c            |   5 ++
 6 files changed, 140 insertions(+), 22 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v9 1/6 KVM: arm64: Rename the device variable to s2_force_noncacheable

From: Ankit Agrawal <[email protected]>

For cache maintenance on a region, ARM KVM relies on that region to be mapped to the Kernal virtual address as CMOs operate on VA.

Currently the device variable is effectively trying to setup the S2 mapping as non cacheable for memory regions that are not mapped in the Kernel VA. This could be either device or Normal_NC depending on the VM_ALLOW_ANY_UNCACHED flag in the VMA.

Thus "device" could be better renamed to s2_force_noncacheable which implies that it is ensuring that region be mapped as non-cacheable.

---
 arch/arm64/kvm/mmu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +6 -6
index 2942ec92c5a4..1601ab9527d4 100644
@@ -1478,7 +1478,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	int ret = 0;
 	bool write_fault, writable, force_pte = false;
 	bool exec_fault, mte_allowed;
-	bool device = false, vfio_allow_any_uc = false;
+	bool s2_force_noncacheable = false, vfio_allow_any_uc = false;
 	unsigned long mmu_seq;
 	phys_addr_t ipa = fault_ipa;
 	struct kvm *kvm = vcpu->kvm;
@@ -1653,7 +1653,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		 * In both cases, we don't let transparent_hugepage_adjust()
 		 * change things at the last minute.
 		 */
-		device = true;
+		s2_force_noncacheable = true;
 	} else if (logging_active && !write_fault) {
 		/*
 		 * Only actually map the page as writable if this was a write
@@ -1662,7 +1662,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		writable = false;
 	}
 
-	if (exec_fault && device)
+	if (exec_fault && s2_force_noncacheable)
 		return -ENOEXEC;
 
 	/*
@@ -1695,7 +1695,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	 * If we are not forced to use page mapping, check if we are
 	 * backed by a THP and thus use block mapping if possible.
 	 */
-	if (vma_pagesize == PAGE_SIZE && !(force_pte || device)) {
+	if (vma_pagesize == PAGE_SIZE && !(force_pte || s2_force_noncacheable)) {
 		if (fault_is_perm && fault_granule > PAGE_SIZE)
 			vma_pagesize = fault_granule;
 		else
@@ -1709,7 +1709,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		}
 	}
 
-	if (!fault_is_perm && !device && kvm_has_mte(kvm)) {
+	if (!fault_is_perm && !s2_force_noncacheable && kvm_has_mte(kvm)) {
 		/* Check the VMM hasn't introduced a new disallowed VMA */
 		if (mte_allowed) {
 			sanitise_mte_tags(kvm, pfn, vma_pagesize);
@@ -1725,7 +1725,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (exec_fault)
 		prot |= KVM_PGTABLE_PROT_X;
 
-	if (device) {
+	if (s2_force_noncacheable) {
 		if (vfio_allow_any_uc)
 			prot |= KVM_PGTABLE_PROT_NORMAL_NC;
 		else
Signature · 1 line
2.34.1
Reviewed-by on v9 1/6
#2 lore raw

Re: [PATCH v9 1/6] KVM: arm64: Rename the device variable to s2_force_noncacheable

Quoted 16 lines · On Sat, Jun 21, 2025 at 04:21:06AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

For cache maintenance on a region, ARM KVM relies on that region to be mapped to the Kernal virtual address as CMOs operate on VA.

Currently the device variable is effectively trying to setup the S2 mapping as non cacheable for memory regions that are not mapped in the Kernel VA. This could be either device or Normal_NC depending on the VM_ALLOW_ANY_UNCACHED flag in the VMA.

Thus "device" could be better renamed to s2_force_noncacheable which implies that it is ensuring that region be mapped as non-cacheable.

Quoted 6 lines
---
 arch/arm64/kvm/mmu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

I think this is much clearer

Jason

Reviewed-by on v9 1/6
#3 lore raw

Re: [PATCH v9 1/6] KVM: arm64: Rename the device variable to s2_force_noncacheable

In reply to [email protected]
Quoted 12 lines · On 21.06.25 06:21, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

For cache maintenance on a region, ARM KVM relies on that region to be mapped to the Kernal virtual address as CMOs operate on VA.

Currently the device variable is effectively trying to setup the S2 mapping as non cacheable for memory regions that are not mapped in the Kernel VA. This could be either device or Normal_NC depending on the VM_ALLOW_ANY_UNCACHED flag in the VMA.

Quoted 8 lines

Thus "device" could be better renamed to s2_force_noncacheable which implies that it is ensuring that region be mapped as non-cacheable.

---

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw

PATCH v9 2/6 KVM: arm64: Update the check to detect device memory

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

Currently, the kvm_is_device_pfn() detects if the memory is kernel mapped through pfn_is_map_memory. It thus implies whether KVM can use Cache Maintenance Operations (CMOs) on that PFN. It is a bit of a misnomer as it does not necessarily detect whether a PFN is for a device memory. Moreover, the function is only used at one place.

It would be better to directly call pfn_is_map_memory. Moreover we should restrict this call to VM_PFNMAP or VM_MIXEDMAP. Otherwise the VMA has struct pages in it and mapped in the kernel VA.

---
 arch/arm64/kvm/mmu.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +4 -6
index 1601ab9527d4..5fe24f30999d 100644
@@ -193,11 +193,6 @@ int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
 	return 0;
 }
 
-static bool kvm_is_device_pfn(unsigned long pfn)
-{
-	return !pfn_is_map_memory(pfn);
-}
-
 static void *stage2_memcache_zalloc_page(void *arg)
 {
 	struct kvm_mmu_memory_cache *mc = arg;
@@ -1492,6 +1487,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
 	struct kvm_pgtable *pgt;
 	struct page *page;
+	vm_flags_t vm_flags;
 	enum kvm_pgtable_walk_flags flags = KVM_PGTABLE_WALK_HANDLE_FAULT | KVM_PGTABLE_WALK_SHARED;
 
 	if (fault_is_perm)
@@ -1619,6 +1615,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
 	vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
 
+	vm_flags = vma->vm_flags;
+
 	/* Don't use the VMA after the unlock -- it may have vanished */
 	vma = NULL;
 
@@ -1642,7 +1640,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (is_error_noslot_pfn(pfn))
 		return -EFAULT;
 
-	if (kvm_is_device_pfn(pfn)) {
+	if (vm_flags & (VM_PFNMAP | VM_MIXEDMAP) && !pfn_is_map_memory(pfn)) {
 		/*
 		 * If the page was identified as device early by looking at
 		 * the VMA flags, vma_pagesize is already representing the
Signature · 1 line
2.34.1
Reviewed-by on v9 2/6
#5 lore raw

Re: [PATCH v9 2/6] KVM: arm64: Update the check to detect device memory

Quoted 13 lines · On Sat, Jun 21, 2025 at 04:21:07AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Currently, the kvm_is_device_pfn() detects if the memory is kernel mapped through pfn_is_map_memory. It thus implies whether KVM can use Cache Maintenance Operations (CMOs) on that PFN. It is a bit of a misnomer as it does not necessarily detect whether a PFN is for a device memory. Moreover, the function is only used at one place.

It would be better to directly call pfn_is_map_memory. Moreover we should restrict this call to VM_PFNMAP or VM_MIXEDMAP. Otherwise the VMA has struct pages in it and mapped in the kernel VA.

Quoted 5 lines
---
 arch/arm64/kvm/mmu.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

Non PFMAP or MIXEDMAP VMA's must always contain normal pages which are struct page backed, have KVA's and are cachable.

So we should always be able to go from phys to KVA to do a CMO.

Jason

Reviewed-by on v9 2/6
#6 lore raw

Re: [PATCH v9 2/6] KVM: arm64: Update the check to detect device memory

In reply to [email protected]
Quoted 53 lines · On 21.06.25 06:21, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Currently, the kvm_is_device_pfn() detects if the memory is kernel mapped through pfn_is_map_memory. It thus implies whether KVM can use Cache Maintenance Operations (CMOs) on that PFN. It is a bit of a misnomer as it does not necessarily detect whether a PFN is for a device memory. Moreover, the function is only used at one place.

It would be better to directly call pfn_is_map_memory. Moreover we should restrict this call to VM_PFNMAP or VM_MIXEDMAP. Otherwise the VMA has struct pages in it and mapped in the kernel VA.

---
  arch/arm64/kvm/mmu.c | 10 ++++------
  1 file changed, 4 insertions(+), 6 deletions(-)
arch/arm64/kvm/mmu.c +3 -5
index 1601ab9527d4..5fe24f30999d 100644
@@ -193,11 +193,6 @@ int kvm_arch_flush_remote_tlbs_range(struct kvm *kvm,
  	return 0;
  }
 
-static bool kvm_is_device_pfn(unsigned long pfn)
-{
-	return !pfn_is_map_memory(pfn);
-}
-
  static void *stage2_memcache_zalloc_page(void *arg)
  {
  	struct kvm_mmu_memory_cache *mc = arg;
@@ -1492,6 +1487,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R;
  	struct kvm_pgtable *pgt;
  	struct page *page;
+	vm_flags_t vm_flags;
  	enum kvm_pgtable_walk_flags flags = KVM_PGTABLE_WALK_HANDLE_FAULT | KVM_PGTABLE_WALK_SHARED;
 
  	if (fault_is_perm)
@@ -1619,6 +1615,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
  	vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED;
 
+	vm_flags = vma->vm_flags;
+
  	/* Don't use the VMA after the unlock -- it may have vanished */
  	vma = NULL;
 

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1643

Quoted 5 lines
arch/arm64/kvm/mmu.c +1 -1
@@ -1642,7 +1640,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (is_error_noslot_pfn(pfn))
  		return -EFAULT;
 
-	if (kvm_is_device_pfn(pfn)) {
+	if (vm_flags & (VM_PFNMAP | VM_MIXEDMAP) && !pfn_is_map_memory(pfn)) {
Signature · 3 lines
Cheers,

David / dhildenb
#7 lore raw

PATCH v9 3/6 KVM: arm64: Block cacheable PFNMAP mapping

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

Fixes a security bug due to mismatched attributes between S1 and S2 mapping.

Currently, it is possible for a region to be cacheable in the userspace VMA, but mapped non cached in S2. This creates a potential issue where the VMM may sanitize cacheable memory across VMs using cacheable stores, ensuring it is zeroed. However, if KVM subsequently assigns this memory to a VM as uncached, the VM could end up accessing stale, non-zeroed data from a previous VM, leading to unintended data exposure. This is a security risk.

Block such mismatch attributes case by returning EINVAL when userspace try to map PFNMAP cacheable. Only allow NORMAL_NC and DEVICE_*.

CC Oliver Upton <[email protected]>
CC Catalin Marinas <[email protected]>
CC Sean Christopherson <[email protected]>
---
 arch/arm64/kvm/mmu.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)
arch/arm64/kvm/mmu.c +33 -1
index 5fe24f30999d..68c0f1c25dec 100644
@@ -1465,6 +1465,22 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)
 	return vma->vm_flags & VM_MTE_ALLOWED;
 }
 
+/*
+ * Determine the memory region cacheability from VMA's pgprot. This
+ * is used to set the stage 2 PTEs.
+ */
+static bool kvm_vma_is_cacheable(struct vm_area_struct *vma)
+{
+	switch (FIELD_GET(PTE_ATTRINDX_MASK, pgprot_val(vma->vm_page_prot))) {
+	case MT_NORMAL_NC:
+	case MT_DEVICE_nGnRnE:
+	case MT_DEVICE_nGnRE:
+		return false;
+	default:
+		return true;
+	}
+}
+
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_s2_trans *nested,
 			  struct kvm_memory_slot *memslot, unsigned long hva,
@@ -1472,7 +1488,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 {
 	int ret = 0;
 	bool write_fault, writable, force_pte = false;
-	bool exec_fault, mte_allowed;
+	bool exec_fault, mte_allowed, is_vma_cacheable;
 	bool s2_force_noncacheable = false, vfio_allow_any_uc = false;
 	unsigned long mmu_seq;
 	phys_addr_t ipa = fault_ipa;
@@ -1617,6 +1633,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
 	vm_flags = vma->vm_flags;
 
+	is_vma_cacheable = kvm_vma_is_cacheable(vma);
+
 	/* Don't use the VMA after the unlock -- it may have vanished */
 	vma = NULL;
 
@@ -1660,6 +1678,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 		writable = false;
 	}
 
+	/*
+	 * Prohibit a region to be mapped non cacheable in S2 and marked as
+	 * cacheabled in the userspace VMA. Such mismatched mapping is a
+	 * security risk.
+	 */
+	if (is_vma_cacheable && s2_force_noncacheable)
+		return -EINVAL;
+
 	if (exec_fault && s2_force_noncacheable)
 		return -ENOEXEC;
 
@@ -2219,6 +2245,12 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 				ret = -EINVAL;
 				break;
 			}
+
+			/* Cacheable PFNMAP is not allowed */
+			if (kvm_vma_is_cacheable(vma)) {
+				ret = -EINVAL;
+				break;
+			}
 		}
 		hva = min(reg_end, vma->vm_end);
 	} while (hva < reg_end);
Signature · 1 line
2.34.1
maintainer
#8 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Quoted 16 lines · On Sat, Jun 21, 2025 at 04:21:08AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Fixes a security bug due to mismatched attributes between S1 and S2 mapping.

Currently, it is possible for a region to be cacheable in the userspace VMA, but mapped non cached in S2. This creates a potential issue where the VMM may sanitize cacheable memory across VMs using cacheable stores, ensuring it is zeroed. However, if KVM subsequently assigns this memory to a VM as uncached, the VM could end up accessing stale, non-zeroed data from a previous VM, leading to unintended data exposure. This is a security risk.

Block such mismatch attributes case by returning EINVAL when userspace try to map PFNMAP cacheable. Only allow NORMAL_NC and DEVICE_*.

Quoted 8 lines
CC Oliver Upton <[email protected]>
CC Catalin Marinas <[email protected]>
CC Sean Christopherson <[email protected]>
---
 arch/arm64/kvm/mmu.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

Sorry for the drive-by comment, but I was looking at this old series from Paolo (look at the cover letter and patch 5):

https://lore.kernel.org/r/[email protected]

in which he points out that the arm64 get_vma_page_shift() function incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which may not be the case if a driver calls remap_pfn_range() to mess around with mappings within the VMA. I think that implies that the optimisation in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device MMIO") is unsound.

But it got me thinking -- given that remap_pfn_range() also takes a 'prot' argument, how do we ensure that this is reflected in the guest? It feels a bit dodgy to rely on drivers always passing 'vma->vm_page_prot'.

Will

#9 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Quoted 11 lines

Sorry for the drive-by comment, but I was looking at this old series from Paolo (look at the cover letter and patch 5):

https://lore.kernel.org/r/[email protected]

in which he points out that the arm64 get_vma_page_shift() function incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which may not be the case if a driver calls remap_pfn_range() to mess around with mappings within the VMA. I think that implies that the optimisation in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device MMIO") is unsound.

Hm yeah, that does seem problematic. Perhaps we need a new vma flag that could help the driver communicate to the KVM that the mapping is contiguous and it can go ahead with the optimization? E.g. something similar to VM_ALLOW_ANY_UNCACHED.

Quoted 5 lines

But it got me thinking -- given that remap_pfn_range() also takes a 'prot' argument, how do we ensure that this is reflected in the guest? It feels a bit dodgy to rely on drivers always passing 'vma->vm_page_prot'.

Will

The driver use the vm_page_prot sent through the VMM VMA and may update some flags. Is your question that if we can trust the driver doing the right thing and properly map through remap_pfn_range so as to not conflict with VMM (vma->vm_page_prot)?

#10 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Quoted 12 lines · Ankit Agrawal
Quoted 11 lines

Sorry for the drive-by comment, but I was looking at this old series from Paolo (look at the cover letter and patch 5):

https://lore.kernel.org/r/[email protected]

in which he points out that the arm64 get_vma_page_shift() function incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which may not be the case if a driver calls remap_pfn_range() to mess around with mappings within the VMA. I think that implies that the optimisation in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device MMIO") is unsound.

Quoted 4 lines

Hm yeah, that does seem problematic. Perhaps we need a new vma flag that could help the driver communicate to the KVM that the mapping is contiguous and it can go ahead with the optimization? E.g. something similar to VM_ALLOW_ANY_UNCACHED.

I think Paolo has the right direction - remove any attempts by KVM to expand contiguity, it should only copy the primary's PTEs and rely on the primary to discover contiguity. No new flags.

Instead we need to keep pushing forward with Peter Xu's work to have PFNMAPs use large PTEs. I believe ARM still has some missing parts for full support bot overall it is a far better direction.

Quoted 3 lines
Quoted 3 lines

But it got me thinking -- given that remap_pfn_range() also takes a 'prot' argument, how do we ensure that this is reflected in the guest? It feels a bit dodgy to rely on drivers always passing 'vma->vm_page_prot'.

But broadly, if you take that perspective KVM as a misdesign.. KVM should be getting the VMA's PTE so that the S2 PTE can duplicate the page-by-page flags, instead of only copying the writable bit.

So, let's call this out of scope for this series. ARM64 KVM does not support page-by-page cachability, it never has, and there is no known use case right now.. The only user of VMA_CACHED PFNMAPs is being consistent.

Jason

#11 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Quoted 13 lines · Jason Gunthorpe
Quoted 12 lines · Ankit Agrawal
Quoted 11 lines

Sorry for the drive-by comment, but I was looking at this old series from Paolo (look at the cover letter and patch 5):

https://lore.kernel.org/r/[email protected]

in which he points out that the arm64 get_vma_page_shift() function incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which may not be the case if a driver calls remap_pfn_range() to mess around with mappings within the VMA. I think that implies that the optimisation in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device MMIO") is unsound.

Quoted 8 lines
Quoted 4 lines

Hm yeah, that does seem problematic. Perhaps we need a new vma flag that could help the driver communicate to the KVM that the mapping is contiguous and it can go ahead with the optimization? E.g. something similar to VM_ALLOW_ANY_UNCACHED.

I think Paolo has the right direction - remove any attempts by KVM to expand contiguity, it should only copy the primary's PTEs and rely on the primary to discover contiguity. No new flags.

100%

Signature · 3 lines
Cheers,

David / dhildenb
maintainer
#12 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Quoted 19 lines · David Hildenbrand
Quoted 18 lines · Jason Gunthorpe
Quoted 16 lines · Ankit Agrawal
Quoted 11 lines

Sorry for the drive-by comment, but I was looking at this old series from Paolo (look at the cover letter and patch 5):

https://lore.kernel.org/r/[email protected]

in which he points out that the arm64 get_vma_page_shift() function incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which may not be the case if a driver calls remap_pfn_range() to mess around with mappings within the VMA. I think that implies that the optimisation in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device MMIO") is unsound.

Hm yeah, that does seem problematic. Perhaps we need a new vma flag that could help the driver communicate to the KVM that the mapping is contiguous and it can go ahead with the optimization? E.g. something similar to VM_ALLOW_ANY_UNCACHED.

Quoted 5 lines
Quoted 3 lines

I think Paolo has the right direction - remove any attempts by KVM to expand contiguity, it should only copy the primary's PTEs and rely on the primary to discover contiguity. No new flags.

100%

The part I don't understand, however, is that I can't see an MMU notifier anywhere on the successful remap_pfn_range() path. So if a driver is using that interface to change the mapping properties of a VM_PFNMAP VMA, how do we ensure that the guest doesn't use whatever stale mappings it's faulted in previously? Did I just miss something?

Will

#13 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Quoted 24 lines · Will Deacon
Quoted 23 lines · David Hildenbrand
Quoted 21 lines · Jason Gunthorpe
Quoted 12 lines · Ankit Agrawal
Quoted 11 lines

Sorry for the drive-by comment, but I was looking at this old series from Paolo (look at the cover letter and patch 5):

https://lore.kernel.org/r/[email protected]

in which he points out that the arm64 get_vma_page_shift() function incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which may not be the case if a driver calls remap_pfn_range() to mess around with mappings within the VMA. I think that implies that the optimisation in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device MMIO") is unsound.

Quoted 4 lines

Hm yeah, that does seem problematic. Perhaps we need a new vma flag that could help the driver communicate to the KVM that the mapping is contiguous and it can go ahead with the optimization? E.g. something similar to VM_ALLOW_ANY_UNCACHED.

I think Paolo has the right direction - remove any attempts by KVM to expand contiguity, it should only copy the primary's PTEs and rely on the primary to discover contiguity. No new flags.

Quoted 7 lines
Quoted 1 line

100%

The part I don't understand, however, is that I can't see an MMU notifier anywhere on the successful remap_pfn_range() path. So if a driver is using that interface to change the mapping properties of a VM_PFNMAP VMA, how do we ensure that the guest doesn't use whatever stale mappings it's faulted in previously? Did I just miss something?

Generally mmu notifiers are for invalidation, not used when establishing new mappings.

It is not legal to use remap_pfn_range() to replace already mapped PTEs. It can only be used during a fop mmap callback to establish the first mapping during VMA creation. Thus there can be no present mapping cached in a secondary and no need to invalidate.

To have dynamic mappings you have to use the fault path and a vmf_XX function - which only happens if the address is non-present, so again no invalidation.

Jason

maintainer
#14 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

Quoted 33 lines · Jason Gunthorpe
Quoted 24 lines · Will Deacon
Quoted 23 lines · David Hildenbrand
Quoted 21 lines · Jason Gunthorpe
Quoted 12 lines · Ankit Agrawal
Quoted 11 lines

Sorry for the drive-by comment, but I was looking at this old series from Paolo (look at the cover letter and patch 5):

https://lore.kernel.org/r/[email protected]

in which he points out that the arm64 get_vma_page_shift() function incorrectly assumes that a VM_PFNMAP VMA is physically contiguous, which may not be the case if a driver calls remap_pfn_range() to mess around with mappings within the VMA. I think that implies that the optimisation in 2aa53d68cee6 ("KVM: arm64: Try stage2 block mapping for host device MMIO") is unsound.

Quoted 4 lines

Hm yeah, that does seem problematic. Perhaps we need a new vma flag that could help the driver communicate to the KVM that the mapping is contiguous and it can go ahead with the optimization? E.g. something similar to VM_ALLOW_ANY_UNCACHED.

I think Paolo has the right direction - remove any attempts by KVM to expand contiguity, it should only copy the primary's PTEs and rely on the primary to discover contiguity. No new flags.

Quoted 7 lines
Quoted 1 line

100%

The part I don't understand, however, is that I can't see an MMU notifier anywhere on the successful remap_pfn_range() path. So if a driver is using that interface to change the mapping properties of a VM_PFNMAP VMA, how do we ensure that the guest doesn't use whatever stale mappings it's faulted in previously? Did I just miss something?

Quoted 7 lines

Generally mmu notifiers are for invalidation, not used when establishing new mappings.

It is not legal to use remap_pfn_range() to replace already mapped PTEs. It can only be used during a fop mmap callback to establish the first mapping during VMA creation. Thus there can be no present mapping cached in a secondary and no need to invalidate.

Aha, thanks, I had completely missed that. I thought remap_pfn_range() was quietly unmapping any existing mapping it ran into, but that's not the case at all.

Cheers,

Will

Reviewed-by on v9 3/6
#15 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

In reply to [email protected]
Quoted 16 lines · On Sat, Jun 21, 2025 at 04:21:08AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Fixes a security bug due to mismatched attributes between S1 and S2 mapping.

Currently, it is possible for a region to be cacheable in the userspace VMA, but mapped non cached in S2. This creates a potential issue where the VMM may sanitize cacheable memory across VMs using cacheable stores, ensuring it is zeroed. However, if KVM subsequently assigns this memory to a VM as uncached, the VM could end up accessing stale, non-zeroed data from a previous VM, leading to unintended data exposure. This is a security risk.

Block such mismatch attributes case by returning EINVAL when userspace try to map PFNMAP cacheable. Only allow NORMAL_NC and DEVICE_*.

Quoted 8 lines
CC Oliver Upton <[email protected]>
CC Catalin Marinas <[email protected]>
CC Sean Christopherson <[email protected]>
---
 arch/arm64/kvm/mmu.c | 34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

Looks straightforward now

Jason

Reviewed-by on v9 3/6
#16 lore raw

Re: [PATCH v9 3/6] KVM: arm64: Block cacheable PFNMAP mapping

In reply to [email protected]
Quoted 16 lines · On 21.06.25 06:21, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Fixes a security bug due to mismatched attributes between S1 and S2 mapping.

Currently, it is possible for a region to be cacheable in the userspace VMA, but mapped non cached in S2. This creates a potential issue where the VMM may sanitize cacheable memory across VMs using cacheable stores, ensuring it is zeroed. However, if KVM subsequently assigns this memory to a VM as uncached, the VM could end up accessing stale, non-zeroed data from a previous VM, leading to unintended data exposure. This is a security risk.

Block such mismatch attributes case by returning EINVAL when userspace try to map PFNMAP cacheable. Only allow NORMAL_NC and DEVICE_*.

Quoted 6 lines
CC Oliver Upton <[email protected]>
CC Catalin Marinas <[email protected]>
CC Sean Christopherson <[email protected]>

---

Signature · 3 lines
Cheers,

David / dhildenb
#17 lore raw

PATCH v9 4/6 KVM: arm64: New function to determine hardware cache management support

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

VM_PFNMAP VMA's are allowed to contain PTE's which point to physical addresses that does not have a struct page and may not be in the kernel direct map.

However ARM64 KVM relies on a simple conversion from physaddr to a kernel virtual address when it does cache maintenance as the CMO instructions work on virtual addresses. This simple approach does not work for physical addresses from VM_PFNMAP since those addresses may not have a kernel virtual address, or it may be difficult to find it.

Fortunately if the ARM64 CPU has two features, S2FWB and CACHE DIC, then KVM no longer needs to do cache flushing and NOP's all the CMOs. This has the effect of no longer requiring a KVA for addresses mapped into the S2.

Add a new function, kvm_arch_supports_cacheable_pfnmap(), to report this capability. From a core prespective it means the arch can accept a cachable VM_PFNMAP as a memslot. From an ARM64 perspective it means that no KVA is required.

CC Jason Gunthorpe <[email protected]>
CC David Hildenbrand <[email protected]>
CC Donald Dutile <[email protected]>
---
 arch/arm64/kvm/mmu.c     | 23 +++++++++++++++++++++++
 include/linux/kvm_host.h |  2 ++
 virt/kvm/kvm_main.c      |  5 +++++
 3 files changed, 30 insertions(+)
arch/arm64/kvm/mmu.c +23 -0
index 68c0f1c25dec..d8d2eb8a409e 100644
@@ -1282,6 +1282,29 @@ void kvm_arch_mmu_enable_log_dirty_pt_masked(struct kvm *kvm,
 	kvm_nested_s2_wp(kvm);
 }
 
+/**
+ * kvm_arch_supports_cacheable_pfnmap() - Determine whether hardware
+ *      supports cache management.
+ *
+ * ARM64 KVM relies on a simple conversion from physaddr to a kernel
+ * virtual address (KVA) when it does cache maintenance as the CMO
+ * instructions work on virtual addresses. This is incompatible with
+ * VM_PFNMAP VMAs which may not have a kernel direct mapping to a
+ * virtual address.
+ *
+ * With S2FWB and CACHE DIC features, KVM need not do cache flushing
+ * and CMOs are NOP'd. This has the effect of no longer requiring a
+ * KVA for addresses mapped into the S2. The presence of these features
+ * are thus necessary to support cacheable S2 mapping of VM_PFNMAP.
+ *
+ * Return: True if FWB and DIC is supported.
+ */
+bool kvm_arch_supports_cacheable_pfnmap(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_STAGE2_FWB) &&
+	       cpus_have_final_cap(ARM64_HAS_CACHE_DIC);
+}
+
 static void kvm_send_hwpoison_signal(unsigned long address, short lsb)
 {
 	send_sig_mceerr(BUS_MCEERR_AR, (void __user *)address, lsb, current);
include/linux/kvm_host.h +2 -0
index 3bde4fb5c6aa..c91d5b5f8c39 100644
@@ -1235,6 +1235,8 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm);
 /* flush memory translations pointing to 'slot' */
 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
 				   struct kvm_memory_slot *slot);
+/* hardware supports cache management */
+bool kvm_arch_supports_cacheable_pfnmap(void);
 
 int kvm_prefetch_pages(struct kvm_memory_slot *slot, gfn_t gfn,
 		       struct page **pages, int nr_pages);
virt/kvm/kvm_main.c +5 -0
index eec82775c5bf..feacfb203a70 100644
@@ -1583,6 +1583,11 @@ static void kvm_replace_memslot(struct kvm *kvm,
 #define KVM_SET_USER_MEMORY_REGION_V1_FLAGS \
 	(KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY)
 
+bool __weak kvm_arch_supports_cacheable_pfnmap(void)
+{
+	return false;
+}
+
 static int check_memory_region_flags(struct kvm *kvm,
 				     const struct kvm_userspace_memory_region2 *mem)
 {
Signature · 1 line
2.34.1
Reviewed-by on v9 4/6
#18 lore raw

Re: [PATCH v9 4/6] KVM: arm64: New function to determine hardware cache management support

Quoted 27 lines · On Sat, Jun 21, 2025 at 04:21:09AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

VM_PFNMAP VMA's are allowed to contain PTE's which point to physical addresses that does not have a struct page and may not be in the kernel direct map.

However ARM64 KVM relies on a simple conversion from physaddr to a kernel virtual address when it does cache maintenance as the CMO instructions work on virtual addresses. This simple approach does not work for physical addresses from VM_PFNMAP since those addresses may not have a kernel virtual address, or it may be difficult to find it.

Fortunately if the ARM64 CPU has two features, S2FWB and CACHE DIC, then KVM no longer needs to do cache flushing and NOP's all the CMOs. This has the effect of no longer requiring a KVA for addresses mapped into the S2.

Add a new function, kvm_arch_supports_cacheable_pfnmap(), to report this capability. From a core prespective it means the arch can accept a cachable VM_PFNMAP as a memslot. From an ARM64 perspective it means that no KVA is required.

CC Jason Gunthorpe <[email protected]>
CC David Hildenbrand <[email protected]>
CC Donald Dutile <[email protected]>
Quoted 5 lines
---
 arch/arm64/kvm/mmu.c     | 23 +++++++++++++++++++++++
 include/linux/kvm_host.h |  2 ++
 virt/kvm/kvm_main.c      |  5 +++++
 3 files changed, 30 insertions(+)

Jason

Reviewed-by on v9 4/6
#19 lore raw

Re: [PATCH v9 4/6] KVM: arm64: New function to determine hardware cache management support

In reply to [email protected]
Quoted 22 lines · On 21.06.25 06:21, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

VM_PFNMAP VMA's are allowed to contain PTE's which point to physical addresses that does not have a struct page and may not be in the kernel direct map.

However ARM64 KVM relies on a simple conversion from physaddr to a kernel virtual address when it does cache maintenance as the CMO instructions work on virtual addresses. This simple approach does not work for physical addresses from VM_PFNMAP since those addresses may not have a kernel virtual address, or it may be difficult to find it.

Fortunately if the ARM64 CPU has two features, S2FWB and CACHE DIC, then KVM no longer needs to do cache flushing and NOP's all the CMOs. This has the effect of no longer requiring a KVA for addresses mapped into the S2.

Add a new function, kvm_arch_supports_cacheable_pfnmap(), to report this capability. From a core prespective it means the arch can accept a cachable VM_PFNMAP as a memslot. From an ARM64 perspective it means that no KVA is required.

Quoted 6 lines
CC Jason Gunthorpe <[email protected]>
CC David Hildenbrand <[email protected]>
CC Donald Dutile <[email protected]>

---

Signature · 3 lines
Cheers,

David / dhildenb
#20 lore raw

PATCH v9 5/6 KVM: arm64: Allow cacheable stage 2 mapping using VMA flags

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

Today KVM forces the memory to either NORMAL or DEVICE_nGnRE based on pfn_is_map_memory (which tracks whether the device memory is in the kernel map) and ignores the per-VMA flags that indicates the memory attributes. The KVM code is thus restrictive and allows only for the memory that is added to the kernel to be marked as cacheable.

The device memory such as on the Grace Hopper/Blackwell systems is interchangeable with DDR memory and retains properties such as cacheability, unaligned accesses, atomics and handling of executable faults. This requires the device memory to be mapped as NORMAL in stage-2.

Given that the GPU device memory is not added to the kernel (but is rather VMA mapped through remap_pfn_range() in nvgrace-gpu module which sets VM_PFNMAP), pfn_is_map_memory() is false and thus KVM prevents such memory to be mapped Normal cacheable. The patch aims to solve this use case.

Note when FWB is not enabled, the kernel expects to trivially do cache management by flushing the memory by linearly converting a kvm_pte to phys_addr to a KVA, see kvm_flush_dcache_to_poc(). The cache management thus relies on memory being mapped. Moreover ARM64_HAS_CACHE_DIC CPU cap allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. These two capabilities are thus a requirement of the cacheable PFNMAP feature. Make use of kvm_arch_supports_cacheable_pfnmap() to check them.

A cachebility check is made by consulting the VMA pgprot value. If the pgprot mapping type is cacheable, it is safe to be mapped S2 cacheable as the KVM S2 will have the same Normal memory type as the VMA has in the S1 and KVM has no additional responsibility for safety. Checking pgprot as NORMAL is thus a KVM sanity check.

It is possible to have COW VM_PFNMAP when doing a MAP_PRIVATE /dev/mem mapping on systems that allow such mapping. Add check for COW VM_PFNMAP and refuse such mapping.

No additional checks for MTE are needed as kvm_arch_prepare_memory_region() already tests it at an early stage during memslot creation. There would not even be a fault if the memslot is not created.

---
 arch/arm64/kvm/mmu.c | 61 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 50 insertions(+), 11 deletions(-)
arch/arm64/kvm/mmu.c +50 -11
index d8d2eb8a409e..ddcbbcc8a9ae 100644
@@ -1681,18 +1681,53 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (is_error_noslot_pfn(pfn))
 		return -EFAULT;
 
+	/*
+	 * Check if this is non-struct page memory PFN, and cannot support
+	 * CMOs. It could potentially be unsafe to access as cachable.
+	 */
 	if (vm_flags & (VM_PFNMAP | VM_MIXEDMAP) && !pfn_is_map_memory(pfn)) {
 		/*
-		 * If the page was identified as device early by looking at
-		 * the VMA flags, vma_pagesize is already representing the
-		 * largest quantity we can map.  If instead it was mapped
-		 * via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
-		 * and must not be upgraded.
-		 *
-		 * In both cases, we don't let transparent_hugepage_adjust()
-		 * change things at the last minute.
+		 * COW VM_PFNMAP is possible when doing a MAP_PRIVATE
+		 * /dev/mem mapping on systems that allow such mapping.
+		 * Reject such case.
 		 */
-		s2_force_noncacheable = true;
+		if (is_cow_mapping(vm_flags))
+			return -EINVAL;
+
+		if (is_vma_cacheable) {
+			/*
+			 * Whilst the VMA owner expects cacheable mapping to this
+			 * PFN, hardware also has to support the FWB and CACHE DIC
+			 * features.
+			 *
+			 * ARM64 KVM relies on kernel VA mapping to the PFN to
+			 * perform cache maintenance as the CMO instructions work on
+			 * virtual addresses. VM_PFNMAP region are not necessarily
+			 * mapped to a KVA and hence the presence of hardware features
+			 * S2FWB and CACHE DIC are mandatory for cache maintenance.
+			 *
+			 * Check if the hardware supports it before allowing the VMA
+			 * owner request for cacheable mapping.
+			 */
+			if (!kvm_arch_supports_cacheable_pfnmap())
+				return -EFAULT;
+
+			/* Cannot degrade cachable to non cachable */
+			if (s2_force_noncacheable)
+				return -EINVAL;
+		} else {
+			/*
+			 * If the page was identified as device early by looking at
+			 * the VMA flags, vma_pagesize is already representing the
+			 * largest quantity we can map.  If instead it was mapped
+			 * via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
+			 * and must not be upgraded.
+			 *
+			 * In both cases, we don't let transparent_hugepage_adjust()
+			 * change things at the last minute.
+			 */
+			s2_force_noncacheable = true;
+		}
 	} else if (logging_active && !write_fault) {
 		/*
 		 * Only actually map the page as writable if this was a write
@@ -2269,8 +2304,12 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
 				break;
 			}
 
-			/* Cacheable PFNMAP is not allowed */
-			if (kvm_vma_is_cacheable(vma)) {
+			/*
+			 * Cacheable PFNMAP is allowed only if the hardware
+			 * supports it.
+			 */
+			if (kvm_vma_is_cacheable(vma) &&
+			    !kvm_arch_supports_cacheable_pfnmap()) {
 				ret = -EINVAL;
 				break;
 			}
Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v9 5/6] KVM: arm64: Allow cacheable stage 2 mapping using VMA flags

Quoted 17 lines · On Sat, Jun 21, 2025 at 04:21:10AM +0000, [email protected] wrote:
arch/arm64/kvm/mmu.c +4 -5
@@ -1681,18 +1681,53 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (is_error_noslot_pfn(pfn))
 		return -EFAULT;
 
+	/*
+	 * Check if this is non-struct page memory PFN, and cannot support
+	 * CMOs. It could potentially be unsafe to access as cachable.
+	 */
 	if (vm_flags & (VM_PFNMAP | VM_MIXEDMAP) && !pfn_is_map_memory(pfn)) {
 		/*
-		 * If the page was identified as device early by looking at
-		 * the VMA flags, vma_pagesize is already representing the
-		 * largest quantity we can map.  If instead it was mapped
-		 * via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
-		 * and must not be upgraded.

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1695

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -4
-		 *
-		 * In both cases, we don't let transparent_hugepage_adjust()
-		 * change things at the last minute.
+		 * COW VM_PFNMAP is possible when doing a MAP_PRIVATE
+		 * /dev/mem mapping on systems that allow such mapping.
+		 * Reject such case.
 		 */
-		s2_force_noncacheable = true;
+		if (is_cow_mapping(vm_flags))
+			return -EINVAL;

I still would like an explanation why we need to block this.

COW PFNMAP is like MIXEDMAP, you end up with a VMA where there is a mixture of MMIO and normal pages. Arguably you are supposed to use vm_normal_page() not pfn_is_map_memory(), but that seems difficult for KVM.

Given we exclude the cachable case with the pfn_is_map_memory() we know this is the non-struct page memory already, so why do we need to block the COW?

I think the basic rule we are going for is that within the VMA the non-normal/special PTE have to follow the vma->vm_pgprot while the normal pages have to be cachable.

So if we find a normal page (ie pfn_is_map_memory()) then we know it is cachable and s2_force_noncacheable = false. Otherwise we use the vm_pgprot to decide if the special PTE is cachable.

David can you think of any reason to have this is_cow_mapping() test?

Quoted 11 lines
arch/arm64/kvm/mmu.c +11 -0
+		if (is_vma_cacheable) {
+			/*
+			 * Whilst the VMA owner expects cacheable mapping to this
+			 * PFN, hardware also has to support the FWB and CACHE DIC
+			 * features.
+			 *
+			 * ARM64 KVM relies on kernel VA mapping to the PFN to
+			 * perform cache maintenance as the CMO instructions work on
+			 * virtual addresses. VM_PFNMAP region are not necessarily
+			 * mapped to a KVA and hence the presence of hardware features
+			 * S2FWB and CACHE DIC are mandatory for cache maintenance.

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1717

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+			 *
+			 * Check if the hardware supports it before allowing the VMA
+			 * owner request for cacheable mapping.
+			 */
+			if (!kvm_arch_supports_cacheable_pfnmap())
+				return -EFAULT;
+
+			/* Cannot degrade cachable to non cachable */
+			if (s2_force_noncacheable)
+				return -EINVAL;

What am I missing? After the whole series is applied this is the first reference to s2_force_noncacheable after it is initialized to false. So this can't happen?

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1730

Quoted 13 lines
arch/arm64/kvm/mmu.c +13 -0
+		} else {
+			/*
+			 * If the page was identified as device early by looking at
+			 * the VMA flags, vma_pagesize is already representing the
+			 * largest quantity we can map.  If instead it was mapped
+			 * via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
+			 * and must not be upgraded.
+			 *
+			 * In both cases, we don't let transparent_hugepage_adjust()
+			 * change things at the last minute.
+			 */
+			s2_force_noncacheable = true;
+		}

Then this logic that immediately follows:

        if (is_vma_cacheable && s2_force_noncacheable)
                return -EINVAL;

Doesn't make alot of sense either, the only cases that set s2_force_noncacheable=true are the else block of 'if (is_vma_cacheable)' so this is dead code too.

Seems like this still needs some cleanup to remove these impossible conditions. The logic make sense to me otherwise though.

Jason

#22 lore raw

Re: [PATCH v9 5/6] KVM: arm64: Allow cacheable stage 2 mapping using VMA flags

Quoted 44 lines · Jason Gunthorpe
Quoted 17 lines · On Sat, Jun 21, 2025 at 04:21:10AM +0000, [email protected] wrote:
arch/arm64/kvm/mmu.c +4 -5
@@ -1681,18 +1681,53 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (is_error_noslot_pfn(pfn))
  		return -EFAULT;
 
+	/*
+	 * Check if this is non-struct page memory PFN, and cannot support
+	 * CMOs. It could potentially be unsafe to access as cachable.
+	 */
  	if (vm_flags & (VM_PFNMAP | VM_MIXEDMAP) && !pfn_is_map_memory(pfn)) {
  		/*
-		 * If the page was identified as device early by looking at
-		 * the VMA flags, vma_pagesize is already representing the
-		 * largest quantity we can map.  If instead it was mapped
-		 * via __kvm_faultin_pfn(), vma_pagesize is set to PAGE_SIZE
-		 * and must not be upgraded.

arch/arm64/kvm/mmu.c static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1695

Quoted 10 lines
arch/arm64/kvm/mmu.c +5 -4
-		 *
-		 * In both cases, we don't let transparent_hugepage_adjust()
-		 * change things at the last minute.
+		 * COW VM_PFNMAP is possible when doing a MAP_PRIVATE
+		 * /dev/mem mapping on systems that allow such mapping.
+		 * Reject such case.
  		 */
-		s2_force_noncacheable = true;
+		if (is_cow_mapping(vm_flags))
+			return -EINVAL;

I still would like an explanation why we need to block this.

COW PFNMAP is like MIXEDMAP, you end up with a VMA where there is a mixture of MMIO and normal pages. Arguably you are supposed to use vm_normal_page() not pfn_is_map_memory(), but that seems difficult for KVM.

Given we exclude the cachable case with the pfn_is_map_memory() we know this is the non-struct page memory already, so why do we need to block the COW?

I think the basic rule we are going for is that within the VMA the non-normal/special PTE have to follow the vma->vm_pgprot while the normal pages have to be cachable.

Quoted 5 lines

So if we find a normal page (ie pfn_is_map_memory()) then we know it is cachable and s2_force_noncacheable = false. Otherwise we use the vm_pgprot to decide if the special PTE is cachable.

David can you think of any reason to have this is_cow_mapping() test?

I think with that reasoning, it should be fine to drop it.

I think, the COW test made sense when we were talking about limiting it to VM_PFNMAP only and simplifying by dropping other checks. Then, it would have identified that something is certainly not "normal" memory.

Signature · 3 lines
Cheers,

David / dhildenb
#23 lore raw

Re: [PATCH v9 5/6] KVM: arm64: Allow cacheable stage 2 mapping using VMA flags

Thank you Jason and David to review this and other patches in the series! Comments inline.

Quoted 27 lines
Quoted 27 lines
Quoted 7 lines

+             * COW VM_PFNMAP is possible when doing a MAP_PRIVATE +             * /dev/mem mapping on systems that allow such mapping. +             * Reject such case.
               */
-            s2_force_noncacheable = true;
+            if (is_cow_mapping(vm_flags))
+                    return -EINVAL;

I still would like an explanation why we need to block this.

COW PFNMAP is like MIXEDMAP, you end up with a VMA where there is a mixture of MMIO and normal pages. Arguably you are supposed to use vm_normal_page() not pfn_is_map_memory(), but that seems difficult for KVM.

Given we exclude the cachable case with the pfn_is_map_memory() we know this is the non-struct page memory already, so why do we need to block the COW?

I think the basic rule we are going for is that within the VMA the non-normal/special PTE have to follow the vma->vm_pgprot while the normal pages have to be cachable.

So if we find a normal page (ie pfn_is_map_memory()) then we know it is cachable and s2_force_noncacheable = false. Otherwise we use the vm_pgprot to decide if the special PTE is cachable.

Quoted 8 lines
Quoted 1 line

David can you think of any reason to have this is_cow_mapping() test?

I think with that reasoning, it should be fine to drop it.

I think, the COW test made sense when we were talking about limiting it to VM_PFNMAP only and simplifying by dropping other checks. Then, it would have identified that something is certainly not "normal" memory.

Ack, I'll drop the check in the next version.

Quoted 8 lines

Then this logic that immediately follows:

       if (is_vma_cacheable && s2_force_noncacheable)
               return -EINVAL;

Doesn't make alot of sense either, the only cases that set s2_force_noncacheable=true are the else block of 'if (is_vma_cacheable)' so this is dead code too.

I had left it there so that that condition doesn't get violated in the future. But perhaps it doesn't make much sense to keep it. I'll remove in the next version.

Quoted 2 lines

Seems like this still needs some cleanup to remove these impossible conditions. The logic make sense to me otherwise though.

Sure, I'll fix that in the next version.

#24 lore raw

PATCH v9 6/6 KVM: arm64: Expose new KVM cap for cacheable PFNMAP

In reply to [email protected]

From: Ankit Agrawal <[email protected]>

Introduce a new KVM capability to expose to the userspace whether cacheable mapping of PFNMAP is supported.

The ability to safely do the cacheable mapping of PFNMAP is contingent on S2FWB and ARM64_HAS_CACHE_DIC. S2FWB allows KVM to avoid flushing the D cache, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. The cap would be false if those requirements are missing and is checked by making use of kvm_arch_supports_cacheable_pfnmap.

This capability would allow userspace to discover the support. It could for instance be used by userspace to prevent live-migration across FWB and non-FWB hosts.

CC Catalin Marinas <[email protected]>
CC Jason Gunthorpe <[email protected]>
CC Oliver Upton <[email protected]>
CC David Hildenbrand <[email protected]>
---
 Documentation/virt/kvm/api.rst | 13 ++++++++++++-
 arch/arm64/kvm/arm.c           |  7 +++++++
 include/uapi/linux/kvm.h       |  1 +
 3 files changed, 20 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +12 -1
index 1bd2d42e6424..615cdbdd505f 100644
@@ -8528,7 +8528,7 @@ ENOSYS for the others.
 When enabled, KVM will exit to userspace with KVM_EXIT_SYSTEM_EVENT of
 type KVM_SYSTEM_EVENT_SUSPEND to process the guest suspend request.
 
-7.37 KVM_CAP_ARM_WRITABLE_IMP_ID_REGS
+7.42 KVM_CAP_ARM_WRITABLE_IMP_ID_REGS
 -------------------------------------
 
 :Architectures: arm64
@@ -8557,6 +8557,17 @@ given VM.
 When this capability is enabled, KVM resets the VCPU when setting
 MP_STATE_INIT_RECEIVED through IOCTL.  The original MP_STATE is preserved.
 
+7.43 KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED
+-------------------------------------------
+
+:Architectures: arm64
+:Target: VM
+:Parameters: None
+
+This capability indicate to the userspace whether a PFNMAP memory region
+can be safely mapped as cacheable. This relies on the presence of
+force write back (FWB) feature support on the hardware.
+
 8. Other capabilities.
 ======================
 
arch/arm64/kvm/arm.c +7 -0
index de2b4e9c9f9f..9fb8901dcd86 100644
@@ -408,6 +408,13 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES:
 		r = BIT(0);
 		break;
+	case KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED:
+		if (!kvm)
+			r = -EINVAL;
+		else
+			r = kvm_arch_supports_cacheable_pfnmap();
+		break;
+
 	default:
 		r = 0;
 	}
include/uapi/linux/kvm.h +1 -0
index d00b85cb168c..ed9a46875a49 100644
@@ -934,6 +934,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_ARM_EL2 240
 #define KVM_CAP_ARM_EL2_E2H0 241
 #define KVM_CAP_RISCV_MP_STATE_RESET 242
+#define KVM_CAP_ARM_CACHEABLE_PFNMAP_SUPPORTED 243
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
Signature · 1 line
2.34.1
Reviewed-by on v9 6/6
#25 lore raw

Re: [PATCH v9 6/6] KVM: arm64: Expose new KVM cap for cacheable PFNMAP

Quoted 22 lines · On Sat, Jun 21, 2025 at 04:21:11AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Introduce a new KVM capability to expose to the userspace whether cacheable mapping of PFNMAP is supported.

The ability to safely do the cacheable mapping of PFNMAP is contingent on S2FWB and ARM64_HAS_CACHE_DIC. S2FWB allows KVM to avoid flushing the D cache, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. The cap would be false if those requirements are missing and is checked by making use of kvm_arch_supports_cacheable_pfnmap.

This capability would allow userspace to discover the support. It could for instance be used by userspace to prevent live-migration across FWB and non-FWB hosts.

CC Catalin Marinas <[email protected]>
CC Jason Gunthorpe <[email protected]>
CC Oliver Upton <[email protected]>
CC David Hildenbrand <[email protected]>
Quoted 5 lines
---
 Documentation/virt/kvm/api.rst | 13 ++++++++++++-
 arch/arm64/kvm/arm.c           |  7 +++++++
 include/uapi/linux/kvm.h       |  1 +
 3 files changed, 20 insertions(+), 1 deletion(-)

I don't know if any VMM will ever use this, but it looks OK

Jason

#26 lore raw

Re: [PATCH v9 6/6] KVM: arm64: Expose new KVM cap for cacheable PFNMAP

Quoted 29 lines · Jason Gunthorpe
Quoted 22 lines · On Sat, Jun 21, 2025 at 04:21:11AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Introduce a new KVM capability to expose to the userspace whether cacheable mapping of PFNMAP is supported.

The ability to safely do the cacheable mapping of PFNMAP is contingent on S2FWB and ARM64_HAS_CACHE_DIC. S2FWB allows KVM to avoid flushing the D cache, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. The cap would be false if those requirements are missing and is checked by making use of kvm_arch_supports_cacheable_pfnmap.

This capability would allow userspace to discover the support. It could for instance be used by userspace to prevent live-migration across FWB and non-FWB hosts.

CC Catalin Marinas <[email protected]>
CC Jason Gunthorpe <[email protected]>
CC Oliver Upton <[email protected]>
CC David Hildenbrand <[email protected]>
Quoted 5 lines
---
  Documentation/virt/kvm/api.rst | 13 ++++++++++++-
  arch/arm64/kvm/arm.c           |  7 +++++++
  include/uapi/linux/kvm.h       |  1 +
  3 files changed, 20 insertions(+), 1 deletion(-)
Quoted 1 line

I don't know if any VMM will ever use this, but it looks OK

So, should we defer it to the point where we actually have a use case?

I mean, patch #4 could be simplified by modifying arm64 code in patch #5 only. No need for a common kvm_arch function etc.

Signature · 3 lines
Cheers,

David / dhildenb
#27 lore raw

Re: [PATCH v9 6/6] KVM: arm64: Expose new KVM cap for cacheable PFNMAP

Quoted 30 lines · David Hildenbrand
Quoted 29 lines · Jason Gunthorpe
Quoted 27 lines · On Sat, Jun 21, 2025 at 04:21:11AM +0000, [email protected] wrote:

From: Ankit Agrawal <[email protected]>

Introduce a new KVM capability to expose to the userspace whether cacheable mapping of PFNMAP is supported.

The ability to safely do the cacheable mapping of PFNMAP is contingent on S2FWB and ARM64_HAS_CACHE_DIC. S2FWB allows KVM to avoid flushing the D cache, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. The cap would be false if those requirements are missing and is checked by making use of kvm_arch_supports_cacheable_pfnmap.

This capability would allow userspace to discover the support. It could for instance be used by userspace to prevent live-migration across FWB and non-FWB hosts.

CC Catalin Marinas <[email protected]>
CC Jason Gunthorpe <[email protected]>
CC Oliver Upton <[email protected]>
CC David Hildenbrand <[email protected]>
---
  Documentation/virt/kvm/api.rst | 13 ++++++++++++-
  arch/arm64/kvm/arm.c           |  7 +++++++
  include/uapi/linux/kvm.h       |  1 +
  3 files changed, 20 insertions(+), 1 deletion(-)
Quoted 6 lines
Quoted 1 line

I don't know if any VMM will ever use this, but it looks OK

So, should we defer it to the point where we actually have a use case?

I mean, patch #4 could be simplified by modifying arm64 code in patch #5 only. No need for a common kvm_arch function etc.

IDK, I think Marc and Oliver are right it makes sense to have it, I just don't really see how a VMM would make use of it..

Jason

#28 lore raw

Re: [PATCH v9 6/6] KVM: arm64: Expose new KVM cap for cacheable PFNMAP

Thanks Jason and David for reviewing the patch!

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

I don't know if any VMM will ever use this, but it looks OK

So, should we defer it to the point where we actually have a use case?

Quoted 7 lines
Quoted 2 lines

I mean, patch #4 could be simplified by modifying arm64 code in patch #5 only. No need for a common kvm_arch function etc.

IDK, I think Marc and Oliver are right it makes sense to have it, I just don't really see how a VMM would make use of it..

Jason

Right, it was also pointed out that the VMM could make use of it for early termination during VM migration between incompatible systems (i.e. across systems with and without FWB and CACHE DIC).

#29 lore raw

Re: [PATCH v9 6/6] KVM: arm64: Expose new KVM cap for cacheable PFNMAP

Quoted 10 lines · Ankit Agrawal

Thanks Jason and David for reviewing the patch!

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

I don't know if any VMM will ever use this, but it looks OK

So, should we defer it to the point where we actually have a use case?

I mean, patch #4 could be simplified by modifying arm64 code in patch #5 only. No need for a common kvm_arch function etc.

Quoted 8 lines
Quoted 4 lines

IDK, I think Marc and Oliver are right it makes sense to have it, I just don't really see how a VMM would make use of it..

Jason

Right, it was also pointed out that the VMM could make use of it for early termination during VM migration between incompatible systems (i.e. across systems with and without FWB and CACHE DIC).

In practice it isn't that useful.

This is all linked to VFIO so the receiving VMM and system must already have VFIO load and must already have the VMM told to use the coherent VFIO mapping.

It would be a serious misconfiguration to setup a VMM as a migration target when the configuration can't even boot normally - because memslot creation would fail for the VFIO coherent memory.

Regardless, in such a case the memslot creation should fail and the migration will abort.

Jason

#30 lore raw
In reply to [email protected]
Quoted 44 lines

Grace based platforms such as Grace Hopper/Blackwell Superchips have CPU accessible cache coherent GPU memory. The GPU device memory is essentially a DDR memory and retains properties such as cacheability, unaligned accesses, atomics and handling of executable faults. This requires the device memory to be mapped as NORMAL in stage-2.

Today KVM forces the memory to either NORMAL or DEVICE_nGnRE depending on whether the memory region is added to the kernel. The KVM code is thus restrictive and prevents device memory that is not added to the kernel to be marked as cacheable. The patch aims to solve this.

A cachebility check is made by consulting the VMA pgprot value. If the pgprot mapping type is cacheable, it is considered safe to be mapped cacheable as the KVM S2 will have the same Normal memory type as the VMA has in the S1 and KVM has no additional responsibility for safety.

Note when FWB (Force Write Back) is not enabled, the kernel expects to trivially do cache management by flushing the memory by linearly converting a kvm_pte to phys_addr to a KVA. The cache management thus relies on memory being mapped. Since the GPU device memory is not kernel mapped, exit when the FWB is not supported. Similarly, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. So the cacheable PFNMAP is made contingent on these two hardware features.

The ability to safely do the cacheable mapping of PFNMAP is exposed through a KVM capability for userspace consumption.

The changes are heavily influenced by the discussions among maintainers Marc Zyngier and Oliver Upton besides Jason Gunthorpe, Catalin Marinas, David Hildenbrand, Sean Christopherson [1]. Many thanks for their valuable suggestions.

Applied over next-20250610 and tested on the Grace Blackwell platform by booting up VM, loading NVIDIA module [2] and running nvidia-smi in the VM.

To run CUDA workloads, there is a dependency on the IOMMUFD and the Nested Page Table patches being worked on separately by Nicolin Chen. ([email protected]). NVIDIA has provided git repositories which includes all the requisite kernel [3] and Qemu [4] patches in case one wants to try.

Quoted 4 lines

v8 -> v9
1. Included MIXEDMAP to also be considered for cacheable mapping. (Jason Gunthorpe).
2. Minor text nits (Jason Gunthorpe).

A humble reminder for the review.

#31 lore raw
In reply to [email protected]
Quoted 44 lines

Grace based platforms such as Grace Hopper/Blackwell Superchips have CPU accessible cache coherent GPU memory. The GPU device memory is essentially a DDR memory and retains properties such as cacheability, unaligned accesses, atomics and handling of executable faults. This requires the device memory to be mapped as NORMAL in stage-2.

Today KVM forces the memory to either NORMAL or DEVICE_nGnRE depending on whether the memory region is added to the kernel. The KVM code is thus restrictive and prevents device memory that is not added to the kernel to be marked as cacheable. The patch aims to solve this.

A cachebility check is made by consulting the VMA pgprot value. If the pgprot mapping type is cacheable, it is considered safe to be mapped cacheable as the KVM S2 will have the same Normal memory type as the VMA has in the S1 and KVM has no additional responsibility for safety.

Note when FWB (Force Write Back) is not enabled, the kernel expects to trivially do cache management by flushing the memory by linearly converting a kvm_pte to phys_addr to a KVA. The cache management thus relies on memory being mapped. Since the GPU device memory is not kernel mapped, exit when the FWB is not supported. Similarly, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. So the cacheable PFNMAP is made contingent on these two hardware features.

The ability to safely do the cacheable mapping of PFNMAP is exposed through a KVM capability for userspace consumption.

The changes are heavily influenced by the discussions among maintainers Marc Zyngier and Oliver Upton besides Jason Gunthorpe, Catalin Marinas, David Hildenbrand, Sean Christopherson [1]. Many thanks for their valuable suggestions.

Applied over next-20250610 and tested on the Grace Blackwell platform by booting up VM, loading NVIDIA module [2] and running nvidia-smi in the VM.

To run CUDA workloads, there is a dependency on the IOMMUFD and the Nested Page Table patches being worked on separately by Nicolin Chen. ([email protected]). NVIDIA has provided git repositories which includes all the requisite kernel [3] and Qemu [4] patches in case one wants to try.

Quoted 4 lines

v8 -> v9
1. Included MIXEDMAP to also be considered for cacheable mapping. (Jason Gunthorpe).
2. Minor text nits (Jason Gunthorpe).

Humble reminder for review.

Tested-by
#32 lore raw
Quoted 44 lines · Ankit Agrawal
Quoted 44 lines

Grace based platforms such as Grace Hopper/Blackwell Superchips have CPU accessible cache coherent GPU memory. The GPU device memory is essentially a DDR memory and retains properties such as cacheability, unaligned accesses, atomics and handling of executable faults. This requires the device memory to be mapped as NORMAL in stage-2.

Today KVM forces the memory to either NORMAL or DEVICE_nGnRE depending on whether the memory region is added to the kernel. The KVM code is thus restrictive and prevents device memory that is not added to the kernel to be marked as cacheable. The patch aims to solve this.

A cachebility check is made by consulting the VMA pgprot value. If the pgprot mapping type is cacheable, it is considered safe to be mapped cacheable as the KVM S2 will have the same Normal memory type as the VMA has in the S1 and KVM has no additional responsibility for safety.

Note when FWB (Force Write Back) is not enabled, the kernel expects to trivially do cache management by flushing the memory by linearly converting a kvm_pte to phys_addr to a KVA. The cache management thus relies on memory being mapped. Since the GPU device memory is not kernel mapped, exit when the FWB is not supported. Similarly, ARM64_HAS_CACHE_DIC allows KVM to avoid flushing the icache and turns icache_inval_pou() into a NOP. So the cacheable PFNMAP is made contingent on these two hardware features.

The ability to safely do the cacheable mapping of PFNMAP is exposed through a KVM capability for userspace consumption.

The changes are heavily influenced by the discussions among maintainers Marc Zyngier and Oliver Upton besides Jason Gunthorpe, Catalin Marinas, David Hildenbrand, Sean Christopherson [1]. Many thanks for their valuable suggestions.

Applied over next-20250610 and tested on the Grace Blackwell platform by booting up VM, loading NVIDIA module [2] and running nvidia-smi in the VM.

To run CUDA workloads, there is a dependency on the IOMMUFD and the Nested Page Table patches being worked on separately by Nicolin Chen. ([email protected]). NVIDIA has provided git repositories which includes all the requisite kernel [3] and Qemu [4] patches in case one wants to try.

Quoted 7 lines
Quoted 4 lines

v8 -> v9
1. Included MIXEDMAP to also be considered for cacheable mapping. (Jason Gunthorpe).
2. Minor text nits (Jason Gunthorpe).

Humble reminder for review.

Apologies for the delay, I had some issues getting a Grace-Hopper to test on, and a VM that needed to be adjusted(bigger file system) to run the 12.9.1 CUDA install script.

Anyhow, able to assign a G-H GPU to a VM under qemu-kvm, and in the guest, successfully perform an 'nvidia-smi'. Previously, without this patch series in the host, the nvida-smi command would fail and hang the guest. (qemu-kvm was qemu-10.0 -based)

If anyone wants more details, or want more tests run, feel free to ask; I can probably keep the system for another day or two, but I'll have to give it up by this Friday.