Skip to contentSkip to conversation

KVM: arm64: Record whether pKVM stage 2 mapping is cacheable

PATCH In review Bradley Morgan 4 participants 14 messages Jul 1 – Jul 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 KVM: arm64: Record whether pKVM stage 2 mapping is cacheable

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by the generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

---
Changes since V3:
- addressed some review :)

 arch/arm64/kvm/pkvm.c | 51 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 10 deletions(-)
arch/arm64/kvm/pkvm.c +41 -10
index 053e4f733e4b..6d1cad890c7e 100644
@@ -302,9 +302,32 @@ static u64 __pkvm_mapping_start(struct pkvm_mapping *m)
 	return m->gfn * PAGE_SIZE;
 }
 
+#define PKVM_MAPPING_NR_PAGES_MASK     GENMASK_ULL(47, 0)
+#define PKVM_MAPPING_NC                        BIT_ULL(48)
+
+static u64 pkvm_mapping_nr_pages(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+}
+
+static bool pkvm_mapping_is_nc(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NC;
+}
+
+static void pkvm_mapping_set_nr_pages(struct pkvm_mapping *m, u64 nr_pages,
+				      bool nc)
+{
+	WARN_ON_ONCE(nr_pages & ~PKVM_MAPPING_NR_PAGES_MASK);
+
+	m->nr_pages = nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+	if (nc)
+		m->nr_pages |= PKVM_MAPPING_NC;
+}
+
 static u64 __pkvm_mapping_end(struct pkvm_mapping *m)
 {
-	return (m->gfn + m->nr_pages) * PAGE_SIZE - 1;
+	return (m->gfn + pkvm_mapping_nr_pages(m)) * PAGE_SIZE - 1;
 }
 
 INTERVAL_TREE_DEFINE(struct pkvm_mapping, node, u64, __subtree_last,
@@ -350,7 +373,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
 			continue;
 
 		page = pfn_to_page(mapping->pfn);
-		WARN_ON_ONCE(mapping->nr_pages != 1);
+	       WARN_ON_ONCE(pkvm_mapping_nr_pages(mapping) != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
 		account_locked_vm(current->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -369,7 +392,7 @@ static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64
 
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+				       pkvm_mapping_nr_pages(mapping));
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -448,7 +471,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 		 * permission faults are handled in the relax_perms() path.
 		 */
 		if (mapping) {
-			if (size == (mapping->nr_pages * PAGE_SIZE))
+			if (size == (pkvm_mapping_nr_pages(mapping) * PAGE_SIZE))
 				return -EAGAIN;
 
 			/*
@@ -472,7 +495,9 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 	swap(mapping, cache->mapping);
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
-	mapping->nr_pages = size / PAGE_SIZE;
+	pkvm_mapping_set_nr_pages(mapping, size / PAGE_SIZE,
+				  (prot & (KVM_PGTABLE_PROT_DEVICE |
+					   KVM_PGTABLE_PROT_NORMAL_NC)));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +528,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+				       pkvm_mapping_nr_pages(mapping));
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,9 +542,13 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (pkvm_mapping_is_nc(mapping))
+			continue;
+
 		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
+					  PAGE_SIZE * pkvm_mapping_nr_pages(mapping));
+	}
 
 	return 0;
 }
@@ -536,8 +565,10 @@ bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64
 
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest,
+					   handle, mapping->gfn,
+					   pkvm_mapping_nr_pages(mapping),
+					   mkold);
 
 	return young;
 }
Signature · 1 line
2.53.0
maintainer
#1 lore raw

+ Vincent, Leo

Quoted 5 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by the generic stage 2 walker.

Quoted 8 lines

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

---
Changes since V3:
- addressed some review :)

This isn't a change log. If you want to be taken seriously, I'd suggest you start by following the process. You are otherwise wasting people's time. Again.

You also failed to Cc people who have provided feedback on previous versions. That's not right.

Quoted 9 lines
 arch/arm64/kvm/pkvm.c | 51 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 10 deletions(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..6d1cad890c7e 100644

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct pkvm_mapping *m) · line 311

Quoted 10 lines
arch/arm64/kvm/pkvm.c +7 -0
@@ -302,9 +302,32 @@ static u64 __pkvm_mapping_start(struct pkvm_mapping *m)
 	return m->gfn * PAGE_SIZE;
 }
 
+#define PKVM_MAPPING_NR_PAGES_MASK     GENMASK_ULL(47, 0)
+#define PKVM_MAPPING_NC                        BIT_ULL(48)
+
+static u64 pkvm_mapping_nr_pages(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+}

No. You've been pointed to the correct data structure (an anonymous structure containing bit fields). Please consider taking the review comments into account.

This would avoid most of the churn in this patch, and make it easy to backport. Something like the untested hack below.

	M.
arch/arm64/include/asm/kvm_pkvm.h +4 -1
index 74fedd9c5ff02..cdddc9e3a11f5 100644
@@ -195,7 +195,10 @@ struct pkvm_mapping {
 	struct rb_node node;
 	u64 gfn;
 	u64 pfn;
-	u64 nr_pages;
+	struct {
+		unsigned long nr_pages:48;
+		unsigned int  nc:1;
+	};
 	u64 __subtree_last;	/* Internal member for interval tree */
 };
 
arch/arm64/kvm/pkvm.c +9 -7
index 428723b1b0f5c..5932b93bded58 100644
@@ -369,7 +369,7 @@ static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64
 
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -473,6 +473,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
 	mapping->nr_pages = size / PAGE_SIZE;
+	mapping->nc = !!(prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +504,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,10 +518,11 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
-
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (!mapping->nc)
+			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
+						  PAGE_SIZE * mapping->nr_pages);
+	}
 	return 0;
 }
 
@@ -537,7 +539,7 @@ bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
 		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+					   (u64)mapping->nr_pages, mkold);
 
 	return young;
 }
Signature · 1 line
Without deviation from the norm, progress is not possible.
#2 lore raw
Quoted 18 lines · Marc Zyngier

+ Vincent, Leo

Quoted 5 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by the generic stage 2 walker.

Quoted 8 lines

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

---
Changes since V3:
- addressed some review :)

Quoted 3 lines

This isn't a change log. If you want to be taken seriously, I'd suggest you start by following the process. You are otherwise wasting people's time. Again.

Agree... the process has a reason: the changelog here helps the reviewer having an overview of what actually changed between versions, and that makes reviewing much easier, and costs very little to the contributor.

Also, the 1 week waiting time is really important, as mentioned before, as it allows more reviewers and maintainers to give feedback: different people, from different companies and lifestyles have different schedules for dev/rev, but in general it happens weekly. So waiting for a week is really recommended, as it tends to avoid people re-reading 2+ versions of the same patchset, and allows revs time to discuss the suggestions in the same thread.

(I know the waiting can be really frustating, and that sending a vN+1 fast seems to show that you are interested in it, but it really does not help)

Quoted 3 lines

You also failed to Cc people who have provided feedback on previous versions. That's not right.

(Bradley: usually you want as many people as possible to review your stuff, so CC'ing previous reviewers is actually good for you)

Quoted 13 lines
Quoted 13 lines

 arch/arm64/kvm/pkvm.c | 51 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 10 deletions(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..6d1cad890c7e 100644
@@ -302,9 +302,32 @@ static u64 __pkvm_mapping_start(struct pkvm_mapping *m)
 	return m->gfn * PAGE_SIZE;
 }
 
Quoted 11 lines

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct pkvm_mapping *m) · line 311

Quoted 7 lines
arch/arm64/kvm/pkvm.c +7 -0
+#define PKVM_MAPPING_NR_PAGES_MASK     GENMASK_ULL(47, 0)
+#define PKVM_MAPPING_NC                        BIT_ULL(48)
+
+static u64 pkvm_mapping_nr_pages(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+}

No. You've been pointed to the correct data structure (an anonymous structure containing bit fields). Please consider taking the review comments into account.

(and if you do not agree with the suggestion, discuss it in the same thread. Although as Marc shows below, it becames much simpler like that)

Thanks!
Leo

Quoted 80 lines

This would avoid most of the churn in this patch, and make it easy to backport. Something like the untested hack below.

	M.
arch/arm64/include/asm/kvm_pkvm.h +4 -1
index 74fedd9c5ff02..cdddc9e3a11f5 100644
@@ -195,7 +195,10 @@ struct pkvm_mapping {
 	struct rb_node node;
 	u64 gfn;
 	u64 pfn;
-	u64 nr_pages;
+	struct {
+		unsigned long nr_pages:48;
+		unsigned int  nc:1;
+	};
 	u64 __subtree_last;	/* Internal member for interval tree */
 };
 
arch/arm64/kvm/pkvm.c +9 -7
index 428723b1b0f5c..5932b93bded58 100644
@@ -369,7 +369,7 @@ static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64
 
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -473,6 +473,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
 	mapping->nr_pages = size / PAGE_SIZE;
+	mapping->nc = !!(prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +504,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,10 +518,11 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
-
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (!mapping->nc)
+			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
+						  PAGE_SIZE * mapping->nr_pages);
+	}
 	return 0;
 }
 
@@ -537,7 +539,7 @@ bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
 		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+					   (u64)mapping->nr_pages, mkold);
 
 	return young;
 }
Signature · 2 lines
-- 
Without deviation from the norm, progress is not possible.
#3 lore raw
Quoted 36 lines · Leonardo Bras
Quoted 7 lines · Marc Zyngier

+ Vincent, Leo

Quoted 3 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by

the

Quoted 11 lines
Quoted 10 lines

generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

---
Changes since V3:
- addressed some review :)

Quoted 3 lines

This isn't a change log. If you want to be taken seriously, I'd suggest you start by following the process. You are otherwise wasting people's time. Again.

Agree... the process has a reason: the changelog here helps the reviewer having an overview of what actually changed between versions, and that makes reviewing much easier, and costs very little to the contributor.

Also, the 1 week waiting time is really important, as mentioned before, as it allows more reviewers and maintainers to give feedback: different people, from different companies and lifestyles have different schedules for dev/rev, but in general it happens weekly. So waiting for a week is really recommended, as it tends to avoid people re-reading 2+ versions of the same patchset, and allows revs time to discuss the suggestions in the same thread.

Quoted 2 lines

(I know the waiting can be really frustating, and that sending a vN+1 fast seems to show that you are interested in it, but it really does not help)

Well, it's fair, let me give you reasoning on why I do quick Rerolls.

So, if I do something wrong, e.g: checkpatch, I don't want to wait a week, because I would get grilled for a said checkpatch failure.

In this case, I was just excited /shrug.

Quoted 7 lines
Quoted 3 lines

You also failed to Cc people who have provided feedback on previous versions. That's not right.

(Bradley: usually you want as many people as possible to review your stuff,
so CC'ing previous reviewers is actually good for you)

Vincent not being CCed was dumb of me, no idea how I didn't get him on get maintainers.

For you, I didn't want to annoy you with another patch, since maintainers tend to be stressed and annoyed. And I didn't wanna add on to the pain.

Quoted 27 lines
Quoted 3 lines
Quoted 3 lines

 arch/arm64/kvm/pkvm.c | 51

++++++++++++++++++++++++++++++++++---------

Quoted 7 lines

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct

Quoted 7 lines
 1 file changed, 41 insertions(+), 10 deletions(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..6d1cad890c7e 100644
@@ -302,9 +302,32 @@ static u64 __pkvm_mapping_start(struct

pkvm_mapping *m)

Quoted 14 lines

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct · line 311

Quoted 10 lines
arch/arm64/kvm/pkvm.c +7 -0
 	return m->gfn * PAGE_SIZE;
 }
 
+#define PKVM_MAPPING_NR_PAGES_MASK     GENMASK_ULL(47, 0)
+#define PKVM_MAPPING_NC                        BIT_ULL(48)
+
+static u64 pkvm_mapping_nr_pages(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+}

No. You've been pointed to the correct data structure (an anonymous structure containing bit fields). Please consider taking the review comments into account.

Quoted 6 lines

(and if you do not agree with the suggestion, discuss it in the same thread. Although as Marc shows below, it becames much simpler like that)

Thanks!
Leo

I'll test it. If it's good. I'll do

Suggested-by? Or co-developed by?

I'll hold the patch on for a week.

Thanks you lot for taking my bull crap.

Quoted 81 lines

Review

Quoted 7 lines

This would avoid most of the churn in this patch, and make it easy to backport. Something like the untested hack below.

	M.
(quoted diff) +0 -0

b/arch/arm64/include/asm/kvm_pkvm.h

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_unshare(struct

Quoted 20 lines

index 74fedd9c5ff02..cdddc9e3a11f5 100644

arch/arm64/include/asm/kvm_pkvm.h +4 -1
@@ -195,7 +195,10 @@ struct pkvm_mapping {
 	struct rb_node node;
 	u64 gfn;
 	u64 pfn;
-	u64 nr_pages;
+	struct {
+		unsigned long nr_pages:48;
+		unsigned int  nc:1;
+	};
 	u64 __subtree_last;	/* Internal member for interval tree */
 };
 
arch/arm64/kvm/pkvm.c +0 -0
index 428723b1b0f5c..5932b93bded58 100644
@@ -369,7 +369,7 @@ static int __pkvm_pgtable_stage2_unshare(struct

kvm_pgtable *pgt, u64 start, u64

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -473,6 +473,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

u64 addr, u64 size,

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable

Quoted 8 lines
arch/arm64/kvm/pkvm.c +1 -0
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
 	mapping->nr_pages = size / PAGE_SIZE;
+	mapping->nc = !!(prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +504,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable

*pgt, u64 addr, u64 size)

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_flush(struct kvm_pgtable

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,10 +518,11 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable

*pgt, u64 addr, u64 size)

arch/arm64/kvm/pkvm.c bool pkvm_pgtable_stage2_test_clear_young(struct

Quoted 16 lines
arch/arm64/kvm/pkvm.c +5 -4
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
-
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (!mapping->nc)
+			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
+						  PAGE_SIZE * mapping->nr_pages);
+	}
 	return 0;
 }
 
@@ -537,7 +539,7 @@ bool pkvm_pgtable_stage2_test_clear_young(struct

kvm_pgtable *pgt, u64 addr, u64

Quoted 6 lines
arch/arm64/kvm/pkvm.c +1 -1
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
 		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+					   (u64)mapping->nr_pages, mkold);
 
Quoted 6 lines
Quoted 5 lines
arch/arm64/kvm/pkvm.c +0 -0
 	return young;
 }
Signature · 2 lines
-- 
Without deviation from the norm, progress is not possible.

Thanks!

#4 lore raw
Quoted 38 lines · Bradley Morgan
Quoted 36 lines · Leonardo Bras
Quoted 7 lines · Marc Zyngier

+ Vincent, Leo

Quoted 3 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by

the

Quoted 11 lines
Quoted 10 lines

generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

---
Changes since V3:
- addressed some review :)

Quoted 3 lines

This isn't a change log. If you want to be taken seriously, I'd suggest you start by following the process. You are otherwise wasting people's time. Again.

Agree... the process has a reason: the changelog here helps the reviewer having an overview of what actually changed between versions, and that makes reviewing much easier, and costs very little to the contributor.

Also, the 1 week waiting time is really important, as mentioned before, as it allows more reviewers and maintainers to give feedback: different people, from different companies and lifestyles have different schedules for dev/rev, but in general it happens weekly. So waiting for a week is really recommended, as it tends to avoid people re-reading 2+ versions of the same patchset, and allows revs time to discuss the suggestions in the same thread.

Quoted 8 lines
Quoted 2 lines

(I know the waiting can be really frustating, and that sending a vN+1 fast seems to show that you are interested in it, but it really does not help)

Well, it's fair, let me give you reasoning on why I do quick Rerolls.

So, if I do something wrong, e.g: checkpatch, I don't want to wait a week, because I would get grilled for a said checkpatch failure.

Fair, but people would not avoid reviewing your new version due to a checkpath/etc issue. It's common to just report/ignore that and go on with the code that matters.

Also, checkpath is a good example of what you can run in your code before sending, so it does not become a respin.

Quoted 2 lines

In this case, I was just excited /shrug.

It's okay. Been there as well :)

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

You also failed to Cc people who have provided feedback on previous versions. That's not right.

(Bradley: usually you want as many people as possible to review your stuff,
so CC'ing previous reviewers is actually good for you)

Quoted 6 lines

Vincent not being CCed was dumb of me, no idea how I didn't get him on get maintainers.

For you, I didn't want to annoy you with another patch, since maintainers tend to be stressed and annoyed. And I didn't wanna add on to the pain.

Don't worry on this. Previous reviewers can just ignore the e-mail if they don't find it interesting anymore.

Quoted 30 lines
Quoted 30 lines
Quoted 3 lines
Quoted 3 lines

 arch/arm64/kvm/pkvm.c | 51

++++++++++++++++++++++++++++++++++---------

Quoted 7 lines

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct

Quoted 7 lines
 1 file changed, 41 insertions(+), 10 deletions(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..6d1cad890c7e 100644
@@ -302,9 +302,32 @@ static u64 __pkvm_mapping_start(struct

pkvm_mapping *m)

Quoted 14 lines

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct · line 311

Quoted 10 lines
arch/arm64/kvm/pkvm.c +7 -0
 	return m->gfn * PAGE_SIZE;
 }
 
+#define PKVM_MAPPING_NR_PAGES_MASK     GENMASK_ULL(47, 0)
+#define PKVM_MAPPING_NC                        BIT_ULL(48)
+
+static u64 pkvm_mapping_nr_pages(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+}

No. You've been pointed to the correct data structure (an anonymous structure containing bit fields). Please consider taking the review comments into account.

(and if you do not agree with the suggestion, discuss it in the same thread. Although as Marc shows below, it becames much simpler like that)

Quoted 7 lines
Quoted 3 lines

Thanks!
Leo

I'll test it. If it's good. I'll do

Suggested-by? Or co-developed by?

I would say none. It's just a change suggestion to your patch, made by a reviewer.

IIRC 'Suggested-by' is used if someone suggested the idea for the patch, such as "ah, you should do this thing before", etc. It's both for giving credit and showing people the idea for that was not yours, for multiple reasons.

Quoted 3 lines

I'll hold the patch on for a week.

Good!
If you get too excited, I suggest reading other people's patches. Everybody makes mistakes, and the solutions / suggestions are really interesting :)

Quoted 3 lines

Thanks you lot for taking my bull crap.

It's not bad, the process just goes like that for everybody. It's just slower than people expect :)

We get slightly better over time, but still make mistakes. (And try our best not to make the same mistakes again :)

Thanks for contributing!
Leo

Quoted 89 lines
Quoted 81 lines

Review

Quoted 7 lines

This would avoid most of the churn in this patch, and make it easy to backport. Something like the untested hack below.

	M.
(quoted diff) +0 -0

b/arch/arm64/include/asm/kvm_pkvm.h

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_unshare(struct

Quoted 20 lines

index 74fedd9c5ff02..cdddc9e3a11f5 100644

arch/arm64/include/asm/kvm_pkvm.h +4 -1
@@ -195,7 +195,10 @@ struct pkvm_mapping {
 	struct rb_node node;
 	u64 gfn;
 	u64 pfn;
-	u64 nr_pages;
+	struct {
+		unsigned long nr_pages:48;
+		unsigned int  nc:1;
+	};
 	u64 __subtree_last;	/* Internal member for interval tree */
 };
 
arch/arm64/kvm/pkvm.c +0 -0
index 428723b1b0f5c..5932b93bded58 100644
@@ -369,7 +369,7 @@ static int __pkvm_pgtable_stage2_unshare(struct

kvm_pgtable *pgt, u64 start, u64

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -473,6 +473,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

u64 addr, u64 size,

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable

Quoted 8 lines
arch/arm64/kvm/pkvm.c +1 -0
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
 	mapping->nr_pages = size / PAGE_SIZE;
+	mapping->nc = !!(prot & (KVM_PGTABLE_PROT_DEVICE | KVM_PGTABLE_PROT_NORMAL_NC));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +504,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable

*pgt, u64 addr, u64 size)

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_flush(struct kvm_pgtable

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+					(u64)mapping->nr_pages);
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,10 +518,11 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable

*pgt, u64 addr, u64 size)

arch/arm64/kvm/pkvm.c bool pkvm_pgtable_stage2_test_clear_young(struct

Quoted 16 lines
arch/arm64/kvm/pkvm.c +5 -4
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
-
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (!mapping->nc)
+			__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
+						  PAGE_SIZE * mapping->nr_pages);
+	}
 	return 0;
 }
 
@@ -537,7 +539,7 @@ bool pkvm_pgtable_stage2_test_clear_young(struct

kvm_pgtable *pgt, u64 addr, u64

Quoted 6 lines
arch/arm64/kvm/pkvm.c +1 -1
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
 		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+					   (u64)mapping->nr_pages, mkold);
 
Quoted 6 lines
Quoted 5 lines
arch/arm64/kvm/pkvm.c +0 -0
 	return young;
 }
Signature · 2 lines
-- 
Without deviation from the norm, progress is not possible.

Thanks!

maintainer
#5 lore raw
In reply to Bradley Morgan
Quoted 4 lines · Bradley Morgan

I'll test it. If it's good. I'll do

Suggested-by? Or co-developed by?

Sb if you want, definitely not Cdb. More importantly, I expect to you to follow the process described at [1]. Nothing there is optional.

Thanks,

	M.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst

Signature · 1 line
Without deviation from the norm, progress is not possible.
#6 lore raw
Quoted 10 lines · Marc Zyngier
Quoted 4 lines · Bradley Morgan

I'll test it. If it's good. I'll do

Suggested-by? Or co-developed by?

Sb if you want, definitely not Cdb. More importantly, I expect to you to follow the process described at [1]. Nothing there is optional.

Quoted 8 lines

Thanks,

	M.

[1]
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst

Hey, I've reviewed your code, LGTM, in a week (or when you tell me discussion
is over)

I'll do V5! :)

I'll see if there is any modifications I can do to shorten it, (of course if it fixes the bug)

Thanks maz for writing the fixed patch.

Thanks!

#7 lore raw
In reply to Bradley Morgan
Quoted 8 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by the generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Is Fixes tag required? If I am reading correctly, Arm ARM says this:

"For VA-based cache maintenance instructions, the instruction operates on the caches regardless of the memory type and cacheability attributes marked for the memory address in the VMSA translation table entries. This means that the effects of the cache maintenance instructions can apply regardless of:
Whether the address accessed:
Is Normal memory or Device memory.
Has the Cacheable attribute or the Non-cacheable attribute."

So nothing goes wrong if we do dcache clean for non-cacheable memory.

Quoted 122 lines

---
Changes since V3:
- addressed some review :)

 arch/arm64/kvm/pkvm.c | 51 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 10 deletions(-)
arch/arm64/kvm/pkvm.c +41 -10
index 053e4f733e4b..6d1cad890c7e 100644
@@ -302,9 +302,32 @@ static u64 __pkvm_mapping_start(struct pkvm_mapping *m)
 	return m->gfn * PAGE_SIZE;
 }
 
+#define PKVM_MAPPING_NR_PAGES_MASK     GENMASK_ULL(47, 0)
+#define PKVM_MAPPING_NC                        BIT_ULL(48)
+
+static u64 pkvm_mapping_nr_pages(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+}
+
+static bool pkvm_mapping_is_nc(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NC;
+}
+
+static void pkvm_mapping_set_nr_pages(struct pkvm_mapping *m, u64 nr_pages,
+				      bool nc)
+{
+	WARN_ON_ONCE(nr_pages & ~PKVM_MAPPING_NR_PAGES_MASK);
+
+	m->nr_pages = nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+	if (nc)
+		m->nr_pages |= PKVM_MAPPING_NC;
+}
+
 static u64 __pkvm_mapping_end(struct pkvm_mapping *m)
 {
-	return (m->gfn + m->nr_pages) * PAGE_SIZE - 1;
+	return (m->gfn + pkvm_mapping_nr_pages(m)) * PAGE_SIZE - 1;
 }
 
 INTERVAL_TREE_DEFINE(struct pkvm_mapping, node, u64, __subtree_last,
@@ -350,7 +373,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
 			continue;
 
 		page = pfn_to_page(mapping->pfn);
-		WARN_ON_ONCE(mapping->nr_pages != 1);
+	       WARN_ON_ONCE(pkvm_mapping_nr_pages(mapping) != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
 		account_locked_vm(current->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -369,7 +392,7 @@ static int __pkvm_pgtable_stage2_unshare(struct kvm_pgtable *pgt, u64 start, u64
 
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+				       pkvm_mapping_nr_pages(mapping));
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -448,7 +471,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 		 * permission faults are handled in the relax_perms() path.
 		 */
 		if (mapping) {
-			if (size == (mapping->nr_pages * PAGE_SIZE))
+			if (size == (pkvm_mapping_nr_pages(mapping) * PAGE_SIZE))
 				return -EAGAIN;
 
 			/*
@@ -472,7 +495,9 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
 	swap(mapping, cache->mapping);
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
-	mapping->nr_pages = size / PAGE_SIZE;
+	pkvm_mapping_set_nr_pages(mapping, size / PAGE_SIZE,
+				  (prot & (KVM_PGTABLE_PROT_DEVICE |
+					   KVM_PGTABLE_PROT_NORMAL_NC)));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +528,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+				       pkvm_mapping_nr_pages(mapping));
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,9 +542,13 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (pkvm_mapping_is_nc(mapping))
+			continue;
+
 		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
+					  PAGE_SIZE * pkvm_mapping_nr_pages(mapping));
+	}
 
 	return 0;
 }
@@ -536,8 +565,10 @@ bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64
 
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest,
+					   handle, mapping->gfn,
+					   pkvm_mapping_nr_pages(mapping),
+					   mkold);
 
 	return young;
 }
#8 lore raw
Quoted 24 lines · Dev Jain
Quoted 8 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by the generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Is Fixes tag required? If I am reading correctly, Arm ARM says this:

"For VA-based cache maintenance instructions, the instruction operates on the
caches regardless of the memory type and cacheability attributes marked for
the memory address in the VMSA translation table entries. This means that the effects of the cache maintenance instructions can apply regardless of:
Whether the address accessed:
Is Normal memory or Device memory.
Has the Cacheable attribute or the Non-cacheable attribute."

Quoted 1 line

So nothing goes wrong if we do dcache clean for non-cacheable memory.

hmmm. Makes sense, fair enough, in V5 I'll also remove the fixes tag

Quoted 129 lines

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct pkvm_mapping

Quoted 13 lines

---
Changes since V3:
- addressed some review :)

 arch/arm64/kvm/pkvm.c | 51 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 10 deletions(-)
arch/arm64/kvm/pkvm.c +0 -0
index 053e4f733e4b..6d1cad890c7e 100644
@@ -302,9 +302,32 @@ static u64 __pkvm_mapping_start(struct pkvm_mapping

*m)

Quoted 7 lines
arch/arm64/kvm/pkvm.c +4 -0
 	return m->gfn * PAGE_SIZE;
 }
 
+#define PKVM_MAPPING_NR_PAGES_MASK     GENMASK_ULL(47, 0)
+#define PKVM_MAPPING_NC                        BIT_ULL(48)
+
+static u64 pkvm_mapping_nr_pages(struct pkvm_mapping *m)

arch/arm64/kvm/pkvm.c static u64 __pkvm_mapping_start(struct pkvm_mapping *m)

Quoted 10 lines
arch/arm64/kvm/pkvm.c +10 -0
+{
+	return m->nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+}
+
+static bool pkvm_mapping_is_nc(struct pkvm_mapping *m)
+{
+	return m->nr_pages & PKVM_MAPPING_NC;
+}
+
+static void pkvm_mapping_set_nr_pages(struct pkvm_mapping *m, u64

nr_pages,

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_reclaim(struct

Quoted 17 lines
arch/arm64/kvm/pkvm.c +10 -1
+				      bool nc)
+{
+	WARN_ON_ONCE(nr_pages & ~PKVM_MAPPING_NR_PAGES_MASK);
+
+	m->nr_pages = nr_pages & PKVM_MAPPING_NR_PAGES_MASK;
+	if (nc)
+		m->nr_pages |= PKVM_MAPPING_NC;
+}
+
 static u64 __pkvm_mapping_end(struct pkvm_mapping *m)
 {
-	return (m->gfn + m->nr_pages) * PAGE_SIZE - 1;
+	return (m->gfn + pkvm_mapping_nr_pages(m)) * PAGE_SIZE - 1;
 }
 
 INTERVAL_TREE_DEFINE(struct pkvm_mapping, node, u64, __subtree_last,
@@ -350,7 +373,7 @@ static int __pkvm_pgtable_stage2_reclaim(struct

kvm_pgtable *pgt, u64 start, u64

arch/arm64/kvm/pkvm.c static int __pkvm_pgtable_stage2_unshare(struct

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 			continue;
 
 		page = pfn_to_page(mapping->pfn);
-		WARN_ON_ONCE(mapping->nr_pages != 1);
+	       WARN_ON_ONCE(pkvm_mapping_nr_pages(mapping) != 1);
 		unpin_user_pages_dirty_lock(&page, 1, true);
 		account_locked_vm(current->mm, 1, false);
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -369,7 +392,7 @@ static int __pkvm_pgtable_stage2_unshare(struct

kvm_pgtable *pgt, u64 start, u64

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 	for_each_mapping_in_range_safe(pgt, start, end, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_unshare_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+				       pkvm_mapping_nr_pages(mapping));
 		if (WARN_ON(ret))
 			return ret;
 		pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
@@ -448,7 +471,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

u64 addr, u64 size,

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 		 * permission faults are handled in the relax_perms() path.
 		 */
 		if (mapping) {
-			if (size == (mapping->nr_pages * PAGE_SIZE))
+			if (size == (pkvm_mapping_nr_pages(mapping) * PAGE_SIZE))
 				return -EAGAIN;
 
 			/*
@@ -472,7 +495,9 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt,

u64 addr, u64 size,

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable

Quoted 11 lines
arch/arm64/kvm/pkvm.c +3 -1
 	swap(mapping, cache->mapping);
 	mapping->gfn = gfn;
 	mapping->pfn = pfn;
-	mapping->nr_pages = size / PAGE_SIZE;
+	pkvm_mapping_set_nr_pages(mapping, size / PAGE_SIZE,
+				  (prot & (KVM_PGTABLE_PROT_DEVICE |
+					   KVM_PGTABLE_PROT_NORMAL_NC)));
 	pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
 
 	return ret;
@@ -503,7 +528,7 @@ int pkvm_pgtable_stage2_wrprotect(struct kvm_pgtable

*pgt, u64 addr, u64 size)

arch/arm64/kvm/pkvm.c int pkvm_pgtable_stage2_flush(struct kvm_pgtable

Quoted 9 lines
arch/arm64/kvm/pkvm.c +1 -1
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
 		ret = kvm_call_hyp_nvhe(__pkvm_host_wrprotect_guest, handle, mapping->gfn,
-					mapping->nr_pages);
+				       pkvm_mapping_nr_pages(mapping));
 		if (WARN_ON(ret))
 			break;
 	}
@@ -517,9 +542,13 @@ int pkvm_pgtable_stage2_flush(struct kvm_pgtable

*pgt, u64 addr, u64 size)

arch/arm64/kvm/pkvm.c bool pkvm_pgtable_stage2_test_clear_young(struct

Quoted 16 lines
arch/arm64/kvm/pkvm.c +6 -2
 	struct pkvm_mapping *mapping;
 
 	lockdep_assert_held(&kvm->mmu_lock);
-	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
+	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping) {
+		if (pkvm_mapping_is_nc(mapping))
+			continue;
+
 		__clean_dcache_guest_page(pfn_to_kaddr(mapping->pfn),
-					  PAGE_SIZE * mapping->nr_pages);
+					  PAGE_SIZE * pkvm_mapping_nr_pages(mapping));
+	}
 
 	return 0;
 }
@@ -536,8 +565,10 @@ bool pkvm_pgtable_stage2_test_clear_young(struct

kvm_pgtable *pgt, u64 addr, u64

Quoted 10 lines
arch/arm64/kvm/pkvm.c +4 -2
 	lockdep_assert_held(&kvm->mmu_lock);
 	for_each_mapping_in_range_safe(pgt, addr, addr + size, mapping)
-		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest, handle, mapping->gfn,
-					   mapping->nr_pages, mkold);
+		young |= kvm_call_hyp_nvhe(__pkvm_host_test_clear_young_guest,
+					   handle, mapping->gfn,
+					   pkvm_mapping_nr_pages(mapping),
+					   mkold);
 
Quoted 4 lines

arch/arm64/kvm/pkvm.c bool pkvm_pgtable_stage2_test_clear_young(struct kvm_pgtable *pgt, u64 addr, u64 · line 574

Quoted 2 lines
arch/arm64/kvm/pkvm.c +0 -0
 	return young;
 }

Thanks!

maintainer
#9 lore raw
In reply to Dev Jain
Quoted 23 lines · Dev Jain
Quoted 8 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by the generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Is Fixes tag required? If I am reading correctly, Arm ARM says this:

"For VA-based cache maintenance instructions, the instruction operates on the caches regardless of the memory type and cacheability attributes marked for the memory address in the VMSA translation table entries. This means that the effects of the cache maintenance instructions can apply regardless of:
Whether the address accessed:
Is Normal memory or Device memory.
Has the Cacheable attribute or the Non-cacheable attribute."

Quoted 2 lines

So nothing goes wrong if we do dcache clean for non-cacheable memory.

Two things:

- having to perform CMOs for something that is not *expected* to be cacheable is both pointless and a contradiction of the intent

- what you quote is about the nature of the *mapping*, and not the memory that is being mapped. Cleaning a dirty cache line on an unsuspecting MMIO endpoint is never going to end nicely. Just have a try.

My reading of all this is that a fix indeed is required, and therefore a Fixes tag *must* be present.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#10 lore raw
Quoted 43 lines · Marc Zyngier
Quoted 6 lines · Dev Jain
Quoted 2 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by

the

Quoted 10 lines
Quoted 6 lines

generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Is Fixes tag required? If I am reading correctly, Arm ARM says this:

"For VA-based cache maintenance instructions, the instruction operates

on the

Quoted 1 line

caches regardless of the memory type and cacheability attributes marked

for

Quoted 1 line

the memory address in the VMSA translation table entries. This means

that

Quoted 1 line

the effects of the cache maintenance instructions can apply regardless

of:

Quoted 6 lines

Whether the address accessed:
Is Normal memory or Device memory.
Has the Cacheable attribute or the Non-cacheable attribute."

So nothing goes wrong if we do dcache clean for non-cacheable memory.

Two things:

- having to perform CMOs for something that is not *expected* to be cacheable is both pointless and a contradiction of the intent

- what you quote is about the nature of the *mapping*, and not the memory that is being mapped. Cleaning a dirty cache line on an unsuspecting MMIO endpoint is never going to end nicely. Just have a try.

Quoted 6 lines

My reading of all this is that a fix indeed is required, and therefore a Fixes tag *must* be present.

	M.

Well, fair enough.

It's kind of two things.

1: A fix
2: a optimization

I say it's a fix, because it's not fun, no.

And I Also say it's a optimization because wasting cycles isn't a good idea, trust me.

Thanks!

maintainer
#11 lore raw
Quoted 57 lines · Bradley Morgan
Quoted 43 lines · Marc Zyngier
Quoted 6 lines · Dev Jain
Quoted 2 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by

the

Quoted 10 lines
Quoted 6 lines

generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Is Fixes tag required? If I am reading correctly, Arm ARM says this:

"For VA-based cache maintenance instructions, the instruction operates

on the

Quoted 1 line

caches regardless of the memory type and cacheability attributes marked

for

Quoted 1 line

the memory address in the VMSA translation table entries. This means

that

Quoted 1 line

the effects of the cache maintenance instructions can apply regardless

of:

Quoted 6 lines

Whether the address accessed:
Is Normal memory or Device memory.
Has the Cacheable attribute or the Non-cacheable attribute."

So nothing goes wrong if we do dcache clean for non-cacheable memory.

Two things:

- having to perform CMOs for something that is not *expected* to be cacheable is both pointless and a contradiction of the intent

- what you quote is about the nature of the *mapping*, and not the memory that is being mapped. Cleaning a dirty cache line on an unsuspecting MMIO endpoint is never going to end nicely. Just have a try.

Quoted 6 lines

My reading of all this is that a fix indeed is required, and therefore a Fixes tag *must* be present.

	M.

Well, fair enough.

It's kind of two things.

Quoted 7 lines

1: A fix
2: a optimization

I say it's a fix, because it's not fun, no.

And I Also say it's a optimization because wasting cycles isn't a good idea, trust me.

This is getting tiresome. Really. And when it comes to wasting cycles, you strike me as the expert here.

It's not an optimisation at all. If anything, the test is going to cost cycles because there is statistically never a case that you map a device with upstream pKVM.

If you call this an optimisation, I'll ask for numbers *proving* this is actually one. Do you really want to get down that road?

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
#12 lore raw
Quoted 76 lines · Marc Zyngier
Quoted 2 lines · Bradley Morgan

On July 5, 2026 8:27:34 PM GMT+01:00, Marc Zyngier <[email protected]>

Quoted 7 lines · wrote:
Quoted 7 lines
Quoted 5 lines · Dev Jain
Quoted 1 line · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush

path

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

uses that list directly, so it lost the PTE attribute check done by

the

Quoted 3 lines
Quoted 3 lines

generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance

for

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Is Fixes tag required? If I am reading correctly, Arm ARM says this:

"For VA-based cache maintenance instructions, the instruction

operates

Quoted 2 lines
Quoted 2 lines

on the

Quoted 1 line

caches regardless of the memory type and cacheability attributes

marked

Quoted 4 lines
Quoted 4 lines

for

Quoted 1 line

the memory address in the VMSA translation table entries. This means

that

Quoted 1 line

the effects of the cache maintenance instructions can apply

regardless

Quoted 30 lines
Quoted 18 lines

of:

Quoted 6 lines

Whether the address accessed:
Is Normal memory or Device memory.
Has the Cacheable attribute or the Non-cacheable attribute."

So nothing goes wrong if we do dcache clean for non-cacheable memory.

Two things:

- having to perform CMOs for something that is not *expected* to be cacheable is both pointless and a contradiction of the intent

- what you quote is about the nature of the *mapping*, and not the memory that is being mapped. Cleaning a dirty cache line on an unsuspecting MMIO endpoint is never going to end nicely. Just have a try.

Quoted 6 lines

My reading of all this is that a fix indeed is required, and therefore a Fixes tag *must* be present.

	M.

Well, fair enough.

It's kind of two things.

Quoted 7 lines

1: A fix
2: a optimization

I say it's a fix, because it's not fun, no.

And I Also say it's a optimization because wasting cycles isn't a good idea, trust me.

This is getting tiresome. Really. And when it comes to wasting cycles, you strike me as the expert here.

Quoted 6 lines

It's not an optimisation at all. If anything, the test is going to cost cycles because there is statistically never a case that you map a device with upstream pKVM.

If you call this an optimisation, I'll ask for numbers *proving* this is actually one. Do you really want to get down that road?

yeah, lesson learned..

before I embarrass myself more, I'll only respond if I have to...

sorry.

Quoted 3 lines
	M.

Thanks!

#13 lore raw
In reply to Marc Zyngier
Quoted 33 lines · Marc Zyngier
Quoted 23 lines · Dev Jain
Quoted 8 lines · Bradley Morgan

pKVM keeps its own mapping list for stage 2 operations. Its flush path uses that list directly, so it lost the PTE attribute check done by the generic stage 2 walker.

Record whether a mapping is cacheable and skip cache maintenance for mappings that are not cacheable.

Fixes e912efed485a ("KVM: arm64: Introduce the EL1 pKVM MMU")

Is Fixes tag required? If I am reading correctly, Arm ARM says this:

"For VA-based cache maintenance instructions, the instruction operates on the caches regardless of the memory type and cacheability attributes marked for the memory address in the VMSA translation table entries. This means that the effects of the cache maintenance instructions can apply regardless of:
Whether the address accessed:
Is Normal memory or Device memory.
Has the Cacheable attribute or the Non-cacheable attribute."

Quoted 2 lines

So nothing goes wrong if we do dcache clean for non-cacheable memory.

Two things:

- having to perform CMOs for something that is not *expected* to be cacheable is both pointless and a contradiction of the intent

Quoted 7 lines

- what you quote is about the nature of the *mapping*, and not the memory that is being mapped. Cleaning a dirty cache line on an unsuspecting MMIO endpoint is never going to end nicely. Just have a try.

My reading of all this is that a fix indeed is required, and therefore a Fixes tag *must* be present.

Thanks for explaining.

Quoted 3 lines
	M.