Skip to contentSkip to conversation

Set DBM bit on writable pages

RFC PATCH In review Leonardo Bras 2 participants 10 messages Oct 26 – Nov 25

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

v2 cover Set DBM bit on writable pages

When HAFDBS is enabled (VTCR_EL2.{HA,HD}={1,1}), having a DBM bit allows hardware to promote a non-writable page to writable by setting (S2AP[1]=1), without having to take a fault.

The reason for this patchset is to add the DBM bit when setting-up writable pages, so the code is prepared for future patches that make use of HDBSS/HACDBS to accelerate dirty-tracking. Write-bit detection still happens based only in S2AP[1], so no change in that aspect.

Having DBM set on block entries is undesired, as we benefit from the first fault for splitting it in PAGE_SIZE entries, which is the granularity used to track the dirty pages in KVM. So introduce a new walker that, on top of clearing S2AP[1] for every entry, also clear DBM for block entries.

Also introduce a new VCPU request to reload Stage2, as it will be needed on future HDBSS/HAFDBS patchsets.

Please let me know of what do you think!

Changes since RFCv1:
- Dropped patch #5 on enabling HAFDBS on non-dirty-tracking guests, as it proved to be counter-productive on wrprotect, contrary to expectations
- No more moving writable-bit to DBM. That does not seem to be the intended use. It makes sense and it's easier to convert code to new DBM approach.
- No new PROT_DIRTY, so drop patch #2
- Tried adding a PROT_DBM, but does not seem necessary to fault a page as writable-clean.
- KVM_REQ_RELOAD_STAGE2 now depends on VHE, as it already reloads Stage2 on guest entry otherwise.

Thanks!
Leo

Leonardo Bras (3):
KVM: arm64: pgtables: Set DBM attribute on writable Stage-2 PTEs
KVM: arm64: Introduce a dedicated walker for stage2 write-protect
KVM: arm64: Add KVM_REQ_RELOAD_STAGE2

 arch/arm64/include/asm/kvm_host.h    |  2 ++
 arch/arm64/include/asm/kvm_pgtable.h |  3 +++
 arch/arm64/kvm/arm.c                 | 10 ++++++++++
 arch/arm64/kvm/hyp/pgtable.c         | 30 ++++++++++++++++++++++------
 4 files changed, 39 insertions(+), 6 deletions(-)

base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222

Signature · 1 line
2.56.0
#1 lore raw

RFC PATCH v2 1/3 KVM: arm64: pgtables: Set DBM attribute on writable Stage-2 PTEs

As a preparation to the introduction of HAFDBS-based features, set the DBM bit (along with S2AP[1]) to all writable PTEs. Also unset DBM and S2AP[1] when a PTE is made read-only again.

DBM bit works as following:
- If HAFDBS is disabled, DBM bit is ignored.
- If HAFDBS is enabled, and DBM bit is set, a write to non-writable page can promote it's PTE to writable (S2AP[1]=1) without taking a fault.

Since it's being set on writable and unset on read-only, it should cause no functional change.

---
 arch/arm64/include/asm/kvm_pgtable.h | 3 +++
 arch/arm64/kvm/hyp/pgtable.c         | 5 +++--
 2 files changed, 6 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_pgtable.h +3 -0
index 41a8687938eb..37baa86d6fd8 100644
@@ -91,14 +91,17 @@ typedef u64 kvm_pte_t;
 #define KVM_PTE_LEAF_ATTR_HI_S1_UXN	BIT(54)
 #define KVM_PTE_LEAF_ATTR_HI_S1_PXN	BIT(53)
 
 #define KVM_PTE_LEAF_ATTR_HI_S2_XN	GENMASK(54, 53)
 
+#define KVM_PTE_LEAF_ATTR_HI_S2_DBM	BIT(51)
+
 #define KVM_PTE_LEAF_ATTR_HI_S1_GP	BIT(50)
 
 #define KVM_PTE_LEAF_ATTR_S2_PERMS	(KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
 					 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
+					 KVM_PTE_LEAF_ATTR_HI_S2_DBM | \
 					 KVM_PTE_LEAF_ATTR_HI_S2_XN)
 
 /* pKVM invalid pte encodings */
 #define KVM_INVALID_PTE_TYPE_MASK	GENMASK(63, 60)
 #define KVM_INVALID_PTE_ANNOT_MASK	~(KVM_PTE_VALID | \
arch/arm64/kvm/hyp/pgtable.c +3 -2
index b74dd5ce1efd..8c2320c6a7f1 100644
@@ -730,11 +730,11 @@ static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot p
 
 	if (prot & KVM_PGTABLE_PROT_R)
 		attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
 
 	if (prot & KVM_PGTABLE_PROT_W)
-		attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
+		attr |= KVM_PTE_LEAF_ATTR_HI_S2_DBM | KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
 
 	if (!kvm_lpa2_is_enabled())
 		attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh);
 
 	attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF;
@@ -1286,10 +1286,11 @@ static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr,
 }
 
 int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 {
 	return stage2_update_leaf_attrs(pgt, addr, size, 0,
+					KVM_PTE_LEAF_ATTR_HI_S2_DBM |
 					KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
 					NULL, NULL,
 					KVM_PGTABLE_WALK_IGNORE_EAGAIN);
 }
 
@@ -1366,11 +1367,11 @@ int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
 
 	if (prot & KVM_PGTABLE_PROT_R)
 		set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R;
 
 	if (prot & KVM_PGTABLE_PROT_W)
-		set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
+		set |= KVM_PTE_LEAF_ATTR_HI_S2_DBM | KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
 
 	if (prot & KVM_PGTABLE_PROT_X) {
 		ret = stage2_set_xn_attr(prot, &xn);
 		if (ret)
 			return ret;
Signature · 1 line
2.56.0
#2 lore raw

RFC PATCH v2 2/3 KVM: arm64: Introduce a dedicated walker for stage2 write-protect

In reply to Leonardo Bras

When write-protecting a range, clean the writable bit (S2AP[1]) and keep the DBM bit set for pages, so on HDBSS-enabled systems we can skip the fault for making it writable again.

On block entries clean both the writable bit and the DBM bit, so we fault even on HDBSS-enabled systems, in order to have hugepage splitting.

With disabled HDBSS, there should be no change in faulting behavior.

---
 arch/arm64/kvm/hyp/pgtable.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +22 -5
index 8c2320c6a7f1..837546be5c13 100644
@@ -1283,17 +1283,34 @@ static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr,
 	if (level)
 		*level = data.level;
 	return 0;
 }
 
+static int stage2_wrprotect_walker(const struct kvm_pgtable_visit_ctx *ctx,
+				   enum kvm_pgtable_walk_flags visit)
+{
+	kvm_pte_t new = ctx->old & ~KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
+
+	/* We remove DBM on blocks so they can fault and get split */
+	if (ctx->level < KVM_PGTABLE_LAST_LEVEL)
+		new &= ~KVM_PTE_LEAF_ATTR_HI_S2_DBM;
+
+	if (kvm_pte_valid(ctx->old) && ctx->old != new)
+		WRITE_ONCE(*ctx->ptep, new);
+
+	return 0;
+}
+
+
 int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size)
 {
-	return stage2_update_leaf_attrs(pgt, addr, size, 0,
-					KVM_PTE_LEAF_ATTR_HI_S2_DBM |
-					KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W,
-					NULL, NULL,
-					KVM_PGTABLE_WALK_IGNORE_EAGAIN);
+	struct kvm_pgtable_walker walker = {
+		.cb	= stage2_wrprotect_walker,
+		.flags	= KVM_PGTABLE_WALK_LEAF,
+	};
+
+	return kvm_pgtable_walk(pgt, addr, size, &walker);
 }
 
 void kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr,
 				enum kvm_pgtable_walk_flags flags)
 {
Signature · 1 line
2.56.0
#3 lore raw

RFC PATCH v2 3/3 KVM: arm64: Add KVM_REQ_RELOAD_STAGE2

In reply to Leonardo Bras

Add a vcpu request to exit guest, reload Stage2, and then come back to guest.

This will be used on future patches that enable HDBSS, as they may need to change VTCR bits for enabling/disabling the feature when the vcpus are still running.

On non-VHE systems, just skip this request as Stage2 is being reloaded on every guest entry.

---
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/arm.c              | 10 ++++++++++
 2 files changed, 12 insertions(+)
arch/arm64/include/asm/kvm_host.h +2 -0
index cd9b9d2462f9..86a4d6e50934 100644
@@ -53,10 +53,12 @@
 #define KVM_REQ_RESYNC_PMU_EL0		KVM_ARCH_REQ(7)
 #define KVM_REQ_NESTED_S2_UNMAP		KVM_ARCH_REQ(8)
 #define KVM_REQ_GUEST_HYP_IRQ_PENDING	KVM_ARCH_REQ(9)
 #define KVM_REQ_MAP_L1_VNCR_EL2		KVM_ARCH_REQ(10)
 #define KVM_REQ_VGIC_PROCESS_UPDATE	KVM_ARCH_REQ(11)
+#define KVM_REQ_RELOAD_STAGE2 \
+	KVM_ARCH_REQ_FLAGS(12, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
 #define KVM_HAVE_MMU_RWLOCK
arch/arm64/kvm/arm.c +10 -0
index 0576c2022ef5..d311637e0b13 100644
@@ -1173,10 +1173,20 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
 			return kvm_vcpu_suspend(vcpu);
 
 		if (kvm_dirty_ring_check_request(vcpu))
 			return 0;
 
+		/* Non-vhe systems already reload Stage2 on guest entry */
+		if (kvm_check_request(KVM_REQ_RELOAD_STAGE2, vcpu) &&
+		    has_vhe()) {
+			unsigned long flags;
+
+			local_irq_save(flags);
+			__load_stage2(vcpu->arch.hw_mmu);
+			local_irq_restore(flags);
+		}
+
 		check_nested_vcpu_requests(vcpu);
 	}
 
 	return 1;
 }
Signature · 1 line
2.56.0
maintainer
#4 lore raw
In reply to Leonardo Bras
Quoted 14 lines · Leonardo Bras

When HAFDBS is enabled (VTCR_EL2.{HA,HD}={1,1}), having a DBM bit allows hardware to promote a non-writable page to writable by setting (S2AP[1]=1), without having to take a fault.

The reason for this patchset is to add the DBM bit when setting-up writable pages, so the code is prepared for future patches that make use of HDBSS/HACDBS to accelerate dirty-tracking. Write-bit detection still happens based only in S2AP[1], so no change in that aspect.

Having DBM set on block entries is undesired, as we benefit from the first fault for splitting it in PAGE_SIZE entries, which is the granularity used to track the dirty pages in KVM. So introduce a new walker that, on top of clearing S2AP[1] for every entry, also clear DBM for block entries.

Quoted 4 lines

Also introduce a new VCPU request to reload Stage2, as it will be needed on future HDBSS/HAFDBS patchsets.

Please let me know of what do you think!

I'm pretty sure I shared this feedback before, but patches that enable DBM do not make sense in isolation. We need a single series that contains the DBM enablement as well as the corresponding HDBSS support.

Quoted 5 lines

Changes since RFCv1:
- Dropped patch #5 on enabling HAFDBS on non-dirty-tracking guests, as it proved to be counter-productive on wrprotect, contrary to expectations
- No more moving writable-bit to DBM. That does not seem to be the intended use. It makes sense and it's easier to convert code to new DBM approach.

Sorry, where was this concluded?

The abstract permission flags need to have a WRITE an DIRTY state, which we then map to hardware based on the capabilities of the system.

!FEAT_HAFDBS:

	DIRTY	=> S2AP[1]
	WRITE	=> S2AP[1]

FEAT_HAFDBS:

	DIRTY	=> S2AP[1]
	WRITE	=> DBM

Beyond making the whole HAFDBS easy to reason about, I want this for FEAT_S2PIE which reimagines bit 7 as a literal dirty bit that can be managed by software or hardware.

Thanks,
Oliver

#5 lore raw
Quoted 15 lines · Oliver Upton
Quoted 14 lines · Leonardo Bras

When HAFDBS is enabled (VTCR_EL2.{HA,HD}={1,1}), having a DBM bit allows hardware to promote a non-writable page to writable by setting (S2AP[1]=1), without having to take a fault.

The reason for this patchset is to add the DBM bit when setting-up writable pages, so the code is prepared for future patches that make use of HDBSS/HACDBS to accelerate dirty-tracking. Write-bit detection still happens based only in S2AP[1], so no change in that aspect.

Having DBM set on block entries is undesired, as we benefit from the first fault for splitting it in PAGE_SIZE entries, which is the granularity used to track the dirty pages in KVM. So introduce a new walker that, on top of clearing S2AP[1] for every entry, also clear DBM for block entries.

Quoted 9 lines
Quoted 4 lines

Also introduce a new VCPU request to reload Stage2, as it will be needed on future HDBSS/HAFDBS patchsets.

Please let me know of what do you think!

I'm pretty sure I shared this feedback before, but patches that enable DBM do not make sense in isolation. We need a single series that contains the DBM enablement as well as the corresponding HDBSS support.

Right,
My reasoning is that we can discuss this new version, and make the needed changes, so Tian could pick this for his own patch series for the next version. I do not intend to get this merged by itself.

Next time I will make sure to state this in the cover letter so it's clear.

Quoted 8 lines
Quoted 5 lines

Changes since RFCv1:
- Dropped patch #5 on enabling HAFDBS on non-dirty-tracking guests, as it proved to be counter-productive on wrprotect, contrary to expectations
- No more moving writable-bit to DBM. That does not seem to be the intended use. It makes sense and it's easier to convert code to new DBM approach.

Sorry, where was this concluded?

Sorry, this is my viewpoint after re-reading the documentation and being really pedantic while trying to get a proper answer from the people responsible for keeping/improving it.

Quoted 5 lines

The abstract permission flags need to have a WRITE an DIRTY state, which we then map to hardware based on the capabilities of the system.

!FEAT_HAFDBS:

Quoted 8 lines
	DIRTY	=> S2AP[1]
	WRITE	=> S2AP[1]

FEAT_HAFDBS:

	DIRTY	=> S2AP[1]
	WRITE	=> DBM

I agree that it could work, and my first version was based on that.

But looks weird to me that we have to decide the writable bit based on having a feature enabled.

Take in account that we can only have DBM being the writable bit while HAFDBS is enabled (not only present), and that it is just enabled during dirty-tracking.

It means we would need to convert the whole pagetable between DIRTY=S2AP[1] and DIRTY=DBM every time we enable or disable dirty-tracking.

My suggestion with this series is to have WRITE=DIRTY=S2AP[1] at all times, and have DBM to act as a "can be promoted to writable on write" bit. This way we don't actually need to convert between encodings, and it still works and complies with the architecture.

Quoted 3 lines

Beyond making the whole HAFDBS easy to reason about, I want this for FEAT_S2PIE which reimagines bit 7 as a literal dirty bit that can be managed by software or hardware.

Humm, and would not that work with the above suggested approach? I mean, bit 7 (Dirty, S2AP[1]) will both mean WRITE/DIRTY, but it will be able to get promoted (nonW->W, or clean->dirty) based on the indirect permissions.

What do you think?

Thanks for reviewing,
Leo

#6 lore raw
Quoted 65 lines · Leonardo Bras
Quoted 15 lines · Oliver Upton
Quoted 14 lines · Leonardo Bras

When HAFDBS is enabled (VTCR_EL2.{HA,HD}={1,1}), having a DBM bit allows hardware to promote a non-writable page to writable by setting (S2AP[1]=1), without having to take a fault.

The reason for this patchset is to add the DBM bit when setting-up writable pages, so the code is prepared for future patches that make use of HDBSS/HACDBS to accelerate dirty-tracking. Write-bit detection still happens based only in S2AP[1], so no change in that aspect.

Having DBM set on block entries is undesired, as we benefit from the first fault for splitting it in PAGE_SIZE entries, which is the granularity used to track the dirty pages in KVM. So introduce a new walker that, on top of clearing S2AP[1] for every entry, also clear DBM for block entries.

Quoted 9 lines
Quoted 4 lines

Also introduce a new VCPU request to reload Stage2, as it will be needed on future HDBSS/HAFDBS patchsets.

Please let me know of what do you think!

I'm pretty sure I shared this feedback before, but patches that enable DBM do not make sense in isolation. We need a single series that contains the DBM enablement as well as the corresponding HDBSS support.

Right,
My reasoning is that we can discuss this new version, and make the needed changes, so Tian could pick this for his own patch series for the next version. I do not intend to get this merged by itself.

Next time I will make sure to state this in the cover letter so it's clear.

Quoted 8 lines
Quoted 5 lines

Changes since RFCv1:
- Dropped patch #5 on enabling HAFDBS on non-dirty-tracking guests, as it proved to be counter-productive on wrprotect, contrary to expectations
- No more moving writable-bit to DBM. That does not seem to be the intended use. It makes sense and it's easier to convert code to new DBM approach.

Sorry, where was this concluded?

Sorry, this is my viewpoint after re-reading the documentation and being really pedantic while trying to get a proper answer from the people responsible for keeping/improving it.

Quoted 5 lines

The abstract permission flags need to have a WRITE an DIRTY state, which we then map to hardware based on the capabilities of the system.

!FEAT_HAFDBS:

Quoted 8 lines
	DIRTY	=> S2AP[1]
	WRITE	=> S2AP[1]

FEAT_HAFDBS:

	DIRTY	=> S2AP[1]
	WRITE	=> DBM

I agree that it could work, and my first version was based on that.

But looks weird to me that we have to decide the writable bit based on having a feature enabled.

Quoted 8 lines

Take in account that we can only have DBM being the writable bit while HAFDBS is enabled (not only present), and that it is just enabled during dirty-tracking.

It means we would need to convert the whole pagetable between DIRTY=S2AP[1] and DIRTY=DBM every time we enable or disable dirty-tracking.

s/DIRTY/WRITE/

Quoted 19 lines

My suggestion with this series is to have WRITE=DIRTY=S2AP[1] at all times, and have DBM to act as a "can be promoted to writable on write" bit. This way we don't actually need to convert between encodings, and it still works and complies with the architecture.

Quoted 3 lines

Beyond making the whole HAFDBS easy to reason about, I want this for FEAT_S2PIE which reimagines bit 7 as a literal dirty bit that can be managed by software or hardware.

Humm, and would not that work with the above suggested approach? I mean, bit 7 (Dirty, S2AP[1]) will both mean WRITE/DIRTY, but it will be able to get promoted (nonW->W, or clean->dirty) based on the indirect permissions.

What do you think?

Thanks for reviewing,
Leo

maintainer
#7 lore raw
In reply to Leonardo Bras
Quoted 11 lines · Leonardo Bras
Quoted 4 lines

I'm pretty sure I shared this feedback before, but patches that enable DBM do not make sense in isolation. We need a single series that contains the DBM enablement as well as the corresponding HDBSS support.

Right,
My reasoning is that we can discuss this new version, and make the needed changes, so Tian could pick this for his own patch series for the next version. I do not intend to get this merged by itself.

Next time I will make sure to state this in the cover letter so it's clear.

Can you just work this out off-list? Let's keep the on-list dialogue consolidated to a single series to review.

Quoted 23 lines
Quoted 5 lines

The abstract permission flags need to have a WRITE an DIRTY state, which we then map to hardware based on the capabilities of the system.

!FEAT_HAFDBS:

Quoted 8 lines
	DIRTY	=> S2AP[1]
	WRITE	=> S2AP[1]

FEAT_HAFDBS:

	DIRTY	=> S2AP[1]
	WRITE	=> DBM

I agree that it could work, and my first version was based on that.

But looks weird to me that we have to decide the writable bit based on having a feature enabled.

Take in account that we can only have DBM being the writable bit while HAFDBS is enabled (not only present), and that it is just enabled during dirty-tracking.

Quoted 7 lines

It means we would need to convert the whole pagetable between DIRTY=S2AP[1] and DIRTY=DBM every time we enable or disable dirty-tracking.

My suggestion with this series is to have WRITE=DIRTY=S2AP[1] at all times, and have DBM to act as a "can be promoted to writable on write" bit. This way we don't actually need to convert between encodings, and it still works and complies with the architecture.

There's no such "can be promoted to writable on write" in the indirect permission model. This is why I'm suggesting that we align the abstract permission bits (i.e. KVM_PGTABLE_PROT_*) with the architecture's own abstracted permissions (e.g. uX, pX, RO, RW).

That composes nicely with nested virt as it is unambiguous what permissions we're trying to represent in a shadow stage-2. And in the non-nested case you can just lay down writable-dirty translations for most things and use writable-clean when necessary for HDBSS.

Quoted 8 lines
Quoted 3 lines

Beyond making the whole HAFDBS easy to reason about, I want this for FEAT_S2PIE which reimagines bit 7 as a literal dirty bit that can be managed by software or hardware.

Humm, and would not that work with the above suggested approach? I mean, bit 7 (Dirty, S2AP[1]) will both mean WRITE/DIRTY, but it will be able to get promoted (nonW->W, or clean->dirty) based on the indirect permissions.

There's a bit more though. We need to have an entry in S2PIR_EL2 that encodes write permission and select that with PIIndex. So then we wind up mapping the abstract WRITE permission bit to two fields into the descriptor rather than 1.

#8 lore raw
Quoted 11 lines · Oliver Upton
Quoted 10 lines · Leonardo Bras
Quoted 4 lines

I'm pretty sure I shared this feedback before, but patches that enable DBM do not make sense in isolation. We need a single series that contains the DBM enablement as well as the corresponding HDBSS support.

Right,
My reasoning is that we can discuss this new version, and make the needed changes, so Tian could pick this for his own patch series for the next version. I do not intend to get this merged by itself.

Quoted 4 lines
Quoted 1 line

Next time I will make sure to state this in the cover letter so it's clear.

Can you just work this out off-list? Let's keep the on-list dialogue consolidated to a single series to review.

Sure, will try to reduce this noise in kvmarm.

Quoted 27 lines
Quoted 27 lines
Quoted 6 lines

The abstract permission flags need to have a WRITE an DIRTY state, which we then map to hardware based on the capabilities of the system.

!FEAT_HAFDBS:

Quoted 8 lines
	DIRTY	=> S2AP[1]
	WRITE	=> S2AP[1]

FEAT_HAFDBS:

	DIRTY	=> S2AP[1]
	WRITE	=> DBM

I agree that it could work, and my first version was based on that.

But looks weird to me that we have to decide the writable bit based on having a feature enabled.

Take in account that we can only have DBM being the writable bit while HAFDBS is enabled (not only present), and that it is just enabled during dirty-tracking.

It means we would need to convert the whole pagetable between DIRTY=S2AP[1] and DIRTY=DBM every time we enable or disable dirty-tracking.

Quoted 7 lines
Quoted 4 lines

My suggestion with this series is to have WRITE=DIRTY=S2AP[1] at all times, and have DBM to act as a "can be promoted to writable on write" bit. This way we don't actually need to convert between encodings, and it still works and complies with the architecture.

There's no such "can be promoted to writable on write" in the indirect permission model.

Well, IIUC it's kind of the default behavior, right? Direct - DBM means HW can promote that page RO->RW, with HAFDBS=on S2PIE - All pages can be promoted RO->RW, as long as HAFDBS=on

Quoted 4 lines

This is why I'm suggesting that we align the abstract permission bits (i.e. KVM_PGTABLE_PROT_*) with the architecture's own abstracted permissions (e.g. uX, pX, RO, RW).

I think the approach you suggest makes sense, and it makes it simpler to deal with permission indirection in the future. In order to do like that, while keeping a single encoding for direct permission, I suggest we have:

- KVM_PGTABLE_PROT_W = BIT(2),
+ KVM_PGTABLE_PROT_DIRTY = BIT(2),
...
+ KVM_PGTABLE_PROT_DBM = BIT(6),
...

+#define KVM_PGTABLE_PROT_W (KVM_PGTABLE_PROT_DBM |
+ KVM_PGTABLE_PROT_DIRTY)

(Rationale is: it's writable if it's either dirty, or can be made dirty by hardware on a write)

And then replacing by PROT_DIRTY the uses of PROT_W which were only tracking the fact it's dirty. I tracked just a couple of those.

When detecting the PROT_* based in PTE encoding, we track PROT_DIRTY, and PROT_DBM individually. The same can be done in nested, as we can just ignore the PROT_DBM for shadow pagetables, given we want, IIUC, to fault and update the original pagetables.

That would avoid switching between encodings while toggling HACDBS, and while making use of the DBM without directly associating it with the write permission.

What do you think?

Quoted 14 lines

That composes nicely with nested virt as it is unambiguous what permissions we're trying to represent in a shadow stage-2. And in the non-nested case you can just lay down writable-dirty translations for most things and use writable-clean when necessary for HDBSS.

Quoted 8 lines
Quoted 3 lines

Beyond making the whole HAFDBS easy to reason about, I want this for FEAT_S2PIE which reimagines bit 7 as a literal dirty bit that can be managed by software or hardware.

Humm, and would not that work with the above suggested approach? I mean, bit 7 (Dirty, S2AP[1]) will both mean WRITE/DIRTY, but it will be able to get promoted (nonW->W, or clean->dirty) based on the indirect permissions.

Quoted 4 lines

There's a bit more though. We need to have an entry in S2PIR_EL2 that encodes write permission and select that with PIIndex. So then we wind up mapping the abstract WRITE permission bit to two fields into the descriptor rather than 1.

Makes sense to me now.

Sorry I took long to reply your message, I was reading the docs for S2PIE and thinking on a solution that would make sense for it in the future as well.

I hope it's enough, but if it's not, let me know of any suggestions or requisites.

Thanks!
Leo

#9 lore raw
Quoted 112 lines · Leonardo Bras
Quoted 11 lines · Oliver Upton
Quoted 10 lines · Leonardo Bras
Quoted 4 lines

I'm pretty sure I shared this feedback before, but patches that enable DBM do not make sense in isolation. We need a single series that contains the DBM enablement as well as the corresponding HDBSS support.

Right,
My reasoning is that we can discuss this new version, and make the needed changes, so Tian could pick this for his own patch series for the next version. I do not intend to get this merged by itself.

Quoted 4 lines
Quoted 1 line

Next time I will make sure to state this in the cover letter so it's clear.

Can you just work this out off-list? Let's keep the on-list dialogue consolidated to a single series to review.

Sure, will try to reduce this noise in kvmarm.

Quoted 27 lines
Quoted 27 lines
Quoted 6 lines

The abstract permission flags need to have a WRITE an DIRTY state, which we then map to hardware based on the capabilities of the system.

!FEAT_HAFDBS:

Quoted 8 lines
	DIRTY	=> S2AP[1]
	WRITE	=> S2AP[1]

FEAT_HAFDBS:

	DIRTY	=> S2AP[1]
	WRITE	=> DBM

I agree that it could work, and my first version was based on that.

But looks weird to me that we have to decide the writable bit based on having a feature enabled.

Take in account that we can only have DBM being the writable bit while HAFDBS is enabled (not only present), and that it is just enabled during dirty-tracking.

It means we would need to convert the whole pagetable between DIRTY=S2AP[1] and DIRTY=DBM every time we enable or disable dirty-tracking.

Quoted 7 lines
Quoted 4 lines

My suggestion with this series is to have WRITE=DIRTY=S2AP[1] at all times, and have DBM to act as a "can be promoted to writable on write" bit. This way we don't actually need to convert between encodings, and it still works and complies with the architecture.

There's no such "can be promoted to writable on write" in the indirect permission model.

Well, IIUC it's kind of the default behavior, right? Direct - DBM means HW can promote that page RO->RW, with HAFDBS=on S2PIE - All pages can be promoted RO->RW, as long as HAFDBS=on

Quoted 4 lines

This is why I'm suggesting that we align the abstract permission bits (i.e. KVM_PGTABLE_PROT_*) with the architecture's own abstracted permissions (e.g. uX, pX, RO, RW).

I think the approach you suggest makes sense, and it makes it simpler to deal with permission indirection in the future. In order to do like that, while keeping a single encoding for direct permission, I suggest we have:

(quoted diff) +4 -1
-	KVM_PGTABLE_PROT_W			= BIT(2),
+	KVM_PGTABLE_PROT_DIRTY			= BIT(2),
...
+	KVM_PGTABLE_PROT_DBM			= BIT(6),
...
 
+#define KVM_PGTABLE_PROT_W	(KVM_PGTABLE_PROT_DBM | 
+				 KVM_PGTABLE_PROT_DIRTY)

(Rationale is: it's writable if it's either dirty, or can be made dirty by hardware on a write)

And then replacing by PROT_DIRTY the uses of PROT_W which were only tracking the fact it's dirty. I tracked just a couple of those.

When detecting the PROT_* based in PTE encoding, we track PROT_DIRTY, and PROT_DBM individually. The same can be done in nested, as we can just ignore the PROT_DBM for shadow pagetables, given we want, IIUC, to fault and update the original pagetables.

That would avoid switching between encodings while toggling HACDBS, and while making use of the DBM without directly associating it with the write permission.

What do you think?

Quoted 14 lines

That composes nicely with nested virt as it is unambiguous what permissions we're trying to represent in a shadow stage-2. And in the non-nested case you can just lay down writable-dirty translations for most things and use writable-clean when necessary for HDBSS.

Quoted 8 lines
Quoted 3 lines

Beyond making the whole HAFDBS easy to reason about, I want this for FEAT_S2PIE which reimagines bit 7 as a literal dirty bit that can be managed by software or hardware.

Humm, and would not that work with the above suggested approach? I mean, bit 7 (Dirty, S2AP[1]) will both mean WRITE/DIRTY, but it will be able to get promoted (nonW->W, or clean->dirty) based on the indirect permissions.

Quoted 4 lines

There's a bit more though. We need to have an entry in S2PIR_EL2 that encodes write permission and select that with PIIndex. So then we wind up mapping the abstract WRITE permission bit to two fields into the descriptor rather than 1.

Quoted 9 lines

Makes sense to me now.

Sorry I took long to reply your message, I was reading the docs for S2PIE and thinking on a solution that would make sense for it in the future as well.

I hope it's enough, but if it's not, let me know of any suggestions or requisites.

Friendly ping :)