Skip to contentSkip to conversation

Support the FEAT_HDBSS introduced in Armv9.5

PATCH Superseded Tian Zheng 5 participants 44 messages Feb 25 – Jul 7

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tian Zheng

author
#0 lore raw

v3 cover Support the FEAT_HDBSS introduced in Armv9.5

This series of patches add support to the Hardware Dirty state tracking Structure(HDBSS) feature, which is introduced by the ARM architecture in the DDI0601(ID121123) version.

The HDBSS feature is an extension to the architecture that enhances tracking translation table descriptors' dirty state, identified as FEAT_HDBSS. This feature utilizes hardware assistance to achieve dirty page tracking, aiming to significantly reduce the overhead of scanning for dirty pages.

The purpose of this feature is to make the execution overhead of live migration lower to both the guest and the host, compared to existing approaches (write-protect or search stage 2 tables).

After these patches, users(such as qemu) can use the KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl to enable or disable the HDBSS feature before and after the live migration.

v2:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

v2->v3 changes:
- Remove the ARM64_HDBSS configuration option and ensure this feature is only enabled in VHE mode.
- Move HDBSS-related variables to the arch-independent portion of the kvm structure.
- Remove error messages during HDBSS enable/disable operations
- Change HDBSS buffer flushing from handle_exit to vcpu_put, check_vcpu_requests, and kvm_handle_guest_abort.
- Add fault handling for HDBSS including buffer full, external abort, and general protection fault (GPF).
- Add support for a 4KB HDBSS buffer size, mapped to the value 0b0000.
- Add a second argument to the ioctl to turn HDBSS on or off.

Tian Zheng (1):
KVM: arm64: Document HDBSS ioctl

eillon (4):
arm64/sysreg: Add HDBSS related register information
KVM: arm64: Add support to set the DBM attr during memory abort
KVM: arm64: Add support for FEAT_HDBSS
KVM: arm64: Enable HDBSS support and handle HDBSSF events

 Documentation/virt/kvm/api.rst       |  16 +++++
 arch/arm64/include/asm/cpufeature.h  |   5 ++
 arch/arm64/include/asm/esr.h         |   7 ++
 arch/arm64/include/asm/kvm_host.h    |  17 +++++
 arch/arm64/include/asm/kvm_mmu.h     |   1 +
 arch/arm64/include/asm/kvm_pgtable.h |   4 ++
 arch/arm64/include/asm/sysreg.h      |  11 +++
 arch/arm64/kernel/cpufeature.c       |  12 ++++
 arch/arm64/kvm/arm.c                 | 102 +++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/pgtable.c         |   6 ++
 arch/arm64/kvm/hyp/vhe/switch.c      |  19 +++++
 arch/arm64/kvm/mmu.c                 |  70 ++++++++++++++++++
 arch/arm64/kvm/reset.c               |   3 +
 arch/arm64/tools/cpucaps             |   1 +
 arch/arm64/tools/sysreg              |  29 ++++++++
 include/uapi/linux/kvm.h             |   1 +
 tools/include/uapi/linux/kvm.h       |   1 +
 17 files changed, 305 insertions(+)

--
2.33.0

author
#1 lore raw

PATCH v3 1/5 arm64/sysreg: Add HDBSS related register information

From: eillon <[email protected]>

The ARM architecture added the HDBSS feature and descriptions of related registers (HDBSSBR/HDBSSPROD) in the DDI0601(ID121123) version, add them to Linux.

---
 arch/arm64/include/asm/esr.h |  2 ++
 arch/arm64/tools/sysreg      | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
arch/arm64/include/asm/esr.h +2 -0
index 7e86d400864e..81c17320a588 100644
@@ -160,6 +160,8 @@
 #define ESR_ELx_CM 		(UL(1) << ESR_ELx_CM_SHIFT)
 
 /* ISS2 field definitions for Data Aborts */
+#define ESR_ELx_HDBSSF_SHIFT	(11)
+#define ESR_ELx_HDBSSF		(UL(1) << ESR_ELx_HDBSSF_SHIFT)
 #define ESR_ELx_TnD_SHIFT	(10)
 #define ESR_ELx_TnD 		(UL(1) << ESR_ELx_TnD_SHIFT)
 #define ESR_ELx_TagAccess_SHIFT	(9)
arch/arm64/tools/sysreg +29 -0
index 9d1c21108057..e166ab322de2 100644
@@ -4528,6 +4528,35 @@ Sysreg	GCSPR_EL2	3	4	2	5	1
 Fields	GCSPR_ELx
 EndSysreg
 
+Sysreg	HDBSSBR_EL2	3	4	2	3	2
+Res0	63:56
+Field	55:12	BADDR
+Res0	11:4
+Enum	3:0	SZ
+	0b0000	4KB
+	0b0001	8KB
+	0b0010	16KB
+	0b0011	32KB
+	0b0100	64KB
+	0b0101	128KB
+	0b0110	256KB
+	0b0111	512KB
+	0b1000	1MB
+	0b1001	2MB
+EndEnum
+EndSysreg
+
+Sysreg	HDBSSPROD_EL2	3	4	2	3	3
+Res0	63:32
+Enum	31:26	FSC
+	0b000000	OK
+	0b010000	ExternalAbort
+	0b101000	GPF
+EndEnum
+Res0	25:19
+Field	18:0	INDEX
+EndSysreg
+
 Sysreg	DACR32_EL2	3	4	3	0	0
 Res0	63:32
 Field	31:30	D15

--
2.33.0

author
#2 lore raw

PATCH v3 2/5 KVM: arm64: Add support to set the DBM attr during memory abort

In reply to Tian Zheng

From: eillon <[email protected]>

This patch adds support to set the DBM (Dirty Bit Modifier) attribute in S2 PTE during user_mem_abort(). This bit, introduced in ARMv8.1, enables hardware to automatically promote write-clean pages to write-dirty. This prevents the guest from being trapped in EL2 due to missing write permissions.

---
 arch/arm64/include/asm/kvm_pgtable.h | 4 ++++
 arch/arm64/kvm/hyp/pgtable.c         | 6 ++++++
 2 files changed, 10 insertions(+)
arch/arm64/include/asm/kvm_pgtable.h +4 -0
index c201168f2857..d0f280972a7a 100644
@@ -93,6 +93,8 @@ typedef u64 kvm_pte_t;
 
 #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 | \
@@ -248,6 +250,7 @@ enum kvm_pgtable_stage2_flags {
  * @KVM_PGTABLE_PROT_R:		Read permission.
  * @KVM_PGTABLE_PROT_DEVICE:	Device attributes.
  * @KVM_PGTABLE_PROT_NORMAL_NC:	Normal noncacheable attributes.
+ * @KVM_PGTABLE_PROT_DBM:	Dirty bit management attribute.
  * @KVM_PGTABLE_PROT_SW0:	Software bit 0.
  * @KVM_PGTABLE_PROT_SW1:	Software bit 1.
  * @KVM_PGTABLE_PROT_SW2:	Software bit 2.
@@ -263,6 +266,7 @@ enum kvm_pgtable_prot {
 
 	KVM_PGTABLE_PROT_DEVICE			= BIT(4),
 	KVM_PGTABLE_PROT_NORMAL_NC		= BIT(5),
+	KVM_PGTABLE_PROT_DBM			= BIT(6),
 
 	KVM_PGTABLE_PROT_SW0			= BIT(55),
 	KVM_PGTABLE_PROT_SW1			= BIT(56),
arch/arm64/kvm/hyp/pgtable.c +6 -0
index 0e4ddd28ef5d..5b4c46d8dc74 100644
@@ -739,6 +739,9 @@ static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot p
 	if (prot & KVM_PGTABLE_PROT_W)
 		attr |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
 
+	if (prot & KVM_PGTABLE_PROT_DBM)
+		attr |= KVM_PTE_LEAF_ATTR_HI_S2_DBM;
+
 	if (!kvm_lpa2_is_enabled())
 		attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh);
 
@@ -1361,6 +1364,9 @@ int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr,
 	if (prot & KVM_PGTABLE_PROT_W)
 		set |= KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W;
 
+	if (prot & KVM_PGTABLE_PROT_DBM)
+		set |= KVM_PTE_LEAF_ATTR_HI_S2_DBM;
+
 	ret = stage2_set_xn_attr(prot, &xn);
 	if (ret)
 		return ret;

--
2.33.0

author
#3 lore raw

PATCH v3 3/5 KVM: arm64: Add support for FEAT_HDBSS

In reply to Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
 arch/arm64/include/asm/cpufeature.h |  5 +++++
 arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
 arch/arm64/tools/cpucaps            |  1 +
 include/uapi/linux/kvm.h            |  1 +
 tools/include/uapi/linux/kvm.h      |  1 +
 5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
 	return cpus_have_final_cap(ARM64_HAFT);
 }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
 static __always_inline bool system_supports_mpam(void)
 {
 	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
 	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
 }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
 bool cpu_supports_bbml2_noabort(void)
 {
 	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
 	},
 #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
 	{
 		.desc = "CRC32 instructions",
 		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
 HAS_VIRT_HOST_EXTN
 HAS_WFXT
 HAS_XNX
+HAS_HDBSS
 HAFT
 HW_DBM
 KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -0
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_GUEST_MEMFD_FLAGS 244
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;

--
2.33.0

#4 lore raw

Re: [PATCH v3 3/5] KVM: arm64: Add support for FEAT_HDBSS

Quoted 109 lines · Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
 arch/arm64/include/asm/cpufeature.h |  5 +++++
 arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
 arch/arm64/tools/cpucaps            |  1 +
 include/uapi/linux/kvm.h            |  1 +
 tools/include/uapi/linux/kvm.h      |  1 +
 5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
 	return cpus_have_final_cap(ARM64_HAFT);
 }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
 static __always_inline bool system_supports_mpam(void)
 {
 	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
 	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
 }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
 bool cpu_supports_bbml2_noabort(void)
 {
 	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
 	},
 #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
 	{
 		.desc = "CRC32 instructions",
 		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
 HAS_VIRT_HOST_EXTN
 HAS_WFXT
 HAS_XNX
+HAS_HDBSS
 HAFT
 HW_DBM
 KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -1
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_GUEST_MEMFD_FLAGS 244
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
--
Quoted 2 lines

2.33.0

Instead of having these architecture specific capability, I wonder if we can add a generic capability like "KVM_CAP_HW_DIRTY_STATE", so other architecture supports similar things can reuse this capability,

For this generic thing I suggest, the getter returns the max support entry count (or the buffer size) it supports like the dirty ring capability. And the setter just let the architecture set the parameters based on the user request.

This should do no harm to this implement, as everything still depends on the architecture behavior, and leave room for other architecture to reuse this.

Regards,
Inochi

maintainer
#5 lore raw

Re: [PATCH v3 3/5] KVM: arm64: Add support for FEAT_HDBSS

Quoted 108 lines · Inochi Amaoto
Quoted 106 lines · Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
 arch/arm64/include/asm/cpufeature.h |  5 +++++
 arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
 arch/arm64/tools/cpucaps            |  1 +
 include/uapi/linux/kvm.h            |  1 +
 tools/include/uapi/linux/kvm.h      |  1 +
 5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
 	return cpus_have_final_cap(ARM64_HAFT);
 }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
 static __always_inline bool system_supports_mpam(void)
 {
 	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
 	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
 }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
 bool cpu_supports_bbml2_noabort(void)
 {
 	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
 	},
 #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
 	{
 		.desc = "CRC32 instructions",
 		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
 HAS_VIRT_HOST_EXTN
 HAS_WFXT
 HAS_XNX
+HAS_HDBSS
 HAFT
 HW_DBM
 KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -0
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_GUEST_MEMFD_FLAGS 244
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
Quoted 9 lines
Quoted 5 lines
tools/include/uapi/linux/kvm.h +0 -1
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
--

2.33.0

Instead of having these architecture specific capability, I wonder if we can add a generic capability like "KVM_CAP_HW_DIRTY_STATE", so other architecture supports similar things can reuse this capability,

What of the existing stuff doing the same thing? x86's PML, to start with?

Quoted 4 lines

For this generic thing I suggest, the getter returns the max support entry count (or the buffer size) it supports like the dirty ring capability. And the setter just let the architecture set the parameters based on the user request.

This looks wrong on a number of levels.

- If you want something generic, there is the existing dirty log/bitmap. How this stuff is populated is none of the user's business (trapping write accesses, dirty bit collection from the PTs, or HW-generated log), and we don't need an extra feature for it. Performance will obviously suck, but that's what you pay for something abstracted and cross-architecture.

- If you want something architecture specific, then it can't be generic, by definition. You get the raw speed and compatibility with other arch-specific extensions.

Quoted 3 lines

This should do no harm to this implement, as everything still depends on the architecture behavior, and leave room for other architecture to reuse this.

Again, the generic framework exists, you just have to implement the backend you want.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#6 lore raw

Re: [PATCH v3 3/5] KVM: arm64: Add support for FEAT_HDBSS

Quoted 120 lines · Marc Zyngier
Quoted 108 lines · Inochi Amaoto
Quoted 106 lines · Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
 arch/arm64/include/asm/cpufeature.h |  5 +++++
 arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
 arch/arm64/tools/cpucaps            |  1 +
 include/uapi/linux/kvm.h            |  1 +
 tools/include/uapi/linux/kvm.h      |  1 +
 5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
 	return cpus_have_final_cap(ARM64_HAFT);
 }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
 static __always_inline bool system_supports_mpam(void)
 {
 	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
 	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
 }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
 bool cpu_supports_bbml2_noabort(void)
 {
 	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
 	},
 #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
 	{
 		.desc = "CRC32 instructions",
 		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
 HAS_VIRT_HOST_EXTN
 HAS_WFXT
 HAS_XNX
+HAS_HDBSS
 HAFT
 HW_DBM
 KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
 #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -0
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_GUEST_MEMFD_FLAGS 244
 #define KVM_CAP_ARM_SEA_TO_USER 245
 #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
Quoted 9 lines
Quoted 5 lines
tools/include/uapi/linux/kvm.h +0 -1
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
--

2.33.0

Instead of having these architecture specific capability, I wonder if we can add a generic capability like "KVM_CAP_HW_DIRTY_STATE", so other architecture supports similar things can reuse this capability,

Quoted 3 lines

What of the existing stuff doing the same thing? x86's PML, to start with?

In fact I think the HDBSS is the first one with non-fixed size. Although there is a in process RISC-V extension for it, there will be a long story to make it ratified.

Quoted 14 lines
Quoted 4 lines

For this generic thing I suggest, the getter returns the max support entry count (or the buffer size) it supports like the dirty ring capability. And the setter just let the architecture set the parameters based on the user request.

This looks wrong on a number of levels.

- If you want something generic, there is the existing dirty log/bitmap. How this stuff is populated is none of the user's business (trapping write accesses, dirty bit collection from the PTs, or HW-generated log), and we don't need an extra feature for it. Performance will obviously suck, but that's what you pay for something abstracted and cross-architecture.

Quoted 4 lines

- If you want something architecture specific, then it can't be generic, by definition. You get the raw speed and compatibility with other arch-specific extensions.

OK, I agree, it is better to keep this thing arch-specific. Doing a generic thing does not benefit too much, I have made a mistake on it. Thanks for your kindly explanation.

Quoted 11 lines
Quoted 3 lines

This should do no harm to this implement, as everything still depends on the architecture behavior, and leave room for other architecture to reuse this.

Again, the generic framework exists, you just have to implement the backend you want.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.

Regards,
Inochi

author
#7 lore raw

Re: [PATCH v3 3/5] KVM: arm64: Add support for FEAT_HDBSS

Quoted 137 lines · Inochi Amaoto
Quoted 116 lines · Marc Zyngier
Quoted 106 lines · Inochi Amaoto
Quoted 105 lines · Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
  arch/arm64/include/asm/cpufeature.h |  5 +++++
  arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
  arch/arm64/tools/cpucaps            |  1 +
  include/uapi/linux/kvm.h            |  1 +
  tools/include/uapi/linux/kvm.h      |  1 +
  5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
  	return cpus_have_final_cap(ARM64_HAFT);
  }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
  static __always_inline bool system_supports_mpam(void)
  {
  	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
  	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
  }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
  bool cpu_supports_bbml2_noabort(void)
  {
  	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
  		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
  	},
  #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
  	{
  		.desc = "CRC32 instructions",
  		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
  HAS_VIRT_HOST_EXTN
  HAS_WFXT
  HAS_XNX
+HAS_HDBSS
  HAFT
  HW_DBM
  KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_ARM_SEA_TO_USER 245
  #define KVM_CAP_S390_USER_OPEREXEC 246
  #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -0
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_GUEST_MEMFD_FLAGS 244
  #define KVM_CAP_ARM_SEA_TO_USER 245
  #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
Quoted 8 lines
Quoted 5 lines
tools/include/uapi/linux/kvm.h +0 -1
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
--

2.33.0

Instead of having these architecture specific capability, I wonder if we can add a generic capability like "KVM_CAP_HW_DIRTY_STATE", so other architecture supports similar things can reuse this capability,

Quoted 3 lines

What of the existing stuff doing the same thing? x86's PML, to start with?

In fact I think the HDBSS is the first one with non-fixed size. Although there is a in process RISC-V extension for it, there will be a long story to make it ratified.

Quoted 13 lines
Quoted 4 lines

For this generic thing I suggest, the getter returns the max support entry count (or the buffer size) it supports like the dirty ring capability. And the setter just let the architecture set the parameters based on the user request.

This looks wrong on a number of levels.

- If you want something generic, there is the existing dirty log/bitmap. How this stuff is populated is none of the user's business (trapping write accesses, dirty bit collection from the PTs, or HW-generated log), and we don't need an extra feature for it. Performance will obviously suck, but that's what you pay for something abstracted and cross-architecture.

Quoted 7 lines
Quoted 4 lines

- If you want something architecture specific, then it can't be generic, by definition. You get the raw speed and compatibility with other arch-specific extensions.

OK, I agree, it is better to keep this thing arch-specific. Doing a generic thing does not benefit too much, I have made a mistake on it. Thanks for your kindly explanation.

Awesome. Thanks for the review.

I agree with Marc—keeping this ARM-specific is the right approach. Also, in v4 we're removing the ioctl interface entirely. HDBSS will be auto-enabled during migration setup and auto-disabled when migration completes, so the capability naming issue becomes moot.

I plan to post v4 with the updated approach soon.

Quoted 12 lines
Quoted 10 lines
Quoted 3 lines

This should do no harm to this implement, as everything still depends on the architecture behavior, and leave room for other architecture to reuse this.

Again, the generic framework exists, you just have to implement the backend you want.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.

Regards,
Inochi

#8 lore raw

Re: [PATCH v3 3/5] KVM: arm64: Add support for FEAT_HDBSS

Quoted 148 lines · Tian Zheng
Quoted 137 lines · Inochi Amaoto
Quoted 116 lines · Marc Zyngier
Quoted 106 lines · Inochi Amaoto
Quoted 105 lines · Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
  arch/arm64/include/asm/cpufeature.h |  5 +++++
  arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
  arch/arm64/tools/cpucaps            |  1 +
  include/uapi/linux/kvm.h            |  1 +
  tools/include/uapi/linux/kvm.h      |  1 +
  5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
  	return cpus_have_final_cap(ARM64_HAFT);
  }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
  static __always_inline bool system_supports_mpam(void)
  {
  	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
  	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
  }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
  bool cpu_supports_bbml2_noabort(void)
  {
  	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
  		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
  	},
  #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
  	{
  		.desc = "CRC32 instructions",
  		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
  HAS_VIRT_HOST_EXTN
  HAS_WFXT
  HAS_XNX
+HAS_HDBSS
  HAFT
  HW_DBM
  KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_ARM_SEA_TO_USER 245
  #define KVM_CAP_S390_USER_OPEREXEC 246
  #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -0
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
  #define KVM_CAP_GUEST_MEMFD_FLAGS 244
  #define KVM_CAP_ARM_SEA_TO_USER 245
  #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
Quoted 8 lines
Quoted 5 lines
tools/include/uapi/linux/kvm.h +0 -1
  struct kvm_irq_routing_irqchip {
  	__u32 irqchip;
--

2.33.0

Instead of having these architecture specific capability, I wonder if we can add a generic capability like "KVM_CAP_HW_DIRTY_STATE", so other architecture supports similar things can reuse this capability,

Quoted 3 lines

What of the existing stuff doing the same thing? x86's PML, to start with?

In fact I think the HDBSS is the first one with non-fixed size. Although there is a in process RISC-V extension for it, there will be a long story to make it ratified.

Quoted 13 lines
Quoted 4 lines

For this generic thing I suggest, the getter returns the max support entry count (or the buffer size) it supports like the dirty ring capability. And the setter just let the architecture set the parameters based on the user request.

This looks wrong on a number of levels.

- If you want something generic, there is the existing dirty log/bitmap. How this stuff is populated is none of the user's business (trapping write accesses, dirty bit collection from the PTs, or HW-generated log), and we don't need an extra feature for it. Performance will obviously suck, but that's what you pay for something abstracted and cross-architecture.

Quoted 7 lines
Quoted 4 lines

- If you want something architecture specific, then it can't be generic, by definition. You get the raw speed and compatibility with other arch-specific extensions.

OK, I agree, it is better to keep this thing arch-specific. Doing a generic thing does not benefit too much, I have made a mistake on it. Thanks for your kindly explanation.

Quoted 8 lines

Awesome. Thanks for the review.

I agree with Marc—keeping this ARM-specific is the right approach. Also, in v4 we're removing the ioctl interface entirely. HDBSS will be auto-enabled during migration setup and auto-disabled when migration completes, so the capability naming issue becomes moot.

I plan to post v4 with the updated approach soon.

Hi Tian,

Any updates in the v4?
Please let me know if you want help in any kind, in regards to this patchset.

Thanks!
Leo

author
#9 lore raw

Re: [PATCH v3 3/5] KVM: arm64: Add support for FEAT_HDBSS

Quoted 154 lines · Leonardo Bras
Quoted 145 lines · Tian Zheng
Quoted 136 lines · Inochi Amaoto
Quoted 115 lines · Marc Zyngier
Quoted 105 lines · Inochi Amaoto
Quoted 104 lines · Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
   arch/arm64/include/asm/cpufeature.h |  5 +++++
   arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
   arch/arm64/tools/cpucaps            |  1 +
   include/uapi/linux/kvm.h            |  1 +
   tools/include/uapi/linux/kvm.h      |  1 +
   5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
   	return cpus_have_final_cap(ARM64_HAFT);
   }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
   static __always_inline bool system_supports_mpam(void)
   {
   	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
   	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
   }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
   bool cpu_supports_bbml2_noabort(void)
   {
   	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
   		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
   	},
   #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
   	{
   		.desc = "CRC32 instructions",
   		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
   HAS_VIRT_HOST_EXTN
   HAS_WFXT
   HAS_XNX
+HAS_HDBSS
   HAFT
   HW_DBM
   KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
   #define KVM_CAP_ARM_SEA_TO_USER 245
   #define KVM_CAP_S390_USER_OPEREXEC 246
   #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
   struct kvm_irq_routing_irqchip {
   	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -0
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
   #define KVM_CAP_GUEST_MEMFD_FLAGS 244
   #define KVM_CAP_ARM_SEA_TO_USER 245
   #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
Quoted 8 lines
Quoted 5 lines
tools/include/uapi/linux/kvm.h +0 -1
   struct kvm_irq_routing_irqchip {
   	__u32 irqchip;
--

2.33.0

Instead of having these architecture specific capability, I wonder if we can add a generic capability like "KVM_CAP_HW_DIRTY_STATE", so other architecture supports similar things can reuse this capability,

Quoted 3 lines

What of the existing stuff doing the same thing? x86's PML, to start with?

In fact I think the HDBSS is the first one with non-fixed size. Although there is a in process RISC-V extension for it, there will be a long story to make it ratified.

Quoted 13 lines
Quoted 4 lines

For this generic thing I suggest, the getter returns the max support entry count (or the buffer size) it supports like the dirty ring capability. And the setter just let the architecture set the parameters based on the user request.

This looks wrong on a number of levels.

- If you want something generic, there is the existing dirty log/bitmap. How this stuff is populated is none of the user's business (trapping write accesses, dirty bit collection from the PTs, or HW-generated log), and we don't need an extra feature for it. Performance will obviously suck, but that's what you pay for something abstracted and cross-architecture.

Quoted 7 lines
Quoted 4 lines

- If you want something architecture specific, then it can't be generic, by definition. You get the raw speed and compatibility with other arch-specific extensions.

OK, I agree, it is better to keep this thing arch-specific. Doing a generic thing does not benefit too much, I have made a mistake on it. Thanks for your kindly explanation.

Quoted 8 lines

Awesome. Thanks for the review.

I agree with Marc—keeping this ARM-specific is the right approach. Also, in v4 we're removing the ioctl interface entirely. HDBSS will be auto-enabled during migration setup and auto-disabled when migration completes, so the capability naming issue becomes moot.

I plan to post v4 with the updated approach soon.

Quoted 8 lines

Hi Tian,

Any updates in the v4?
Please let me know if you want help in any kind, in regards to this patchset.

Thanks!
Leo

Hi Leo,

Thanks for the follow-up. I've finished polishing v4 and am doing one last internal review.

In this version, I've also aligned with the latest HACDBS v2 implementation you shared—e.g., auto-inserting KVM_PGTABLE_S2_DBM into pgt->flags during kvm_init_stage2_mmu.

One note: dirty ring mode is not yet supported in v4—the series currently focuses on the dirty bitmap mode. I'll explicitly call this out in the commit messages (including that KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE and related dirty ring components are omitted) to avoid any confusion during review.

I plan to send v4 out later this week.

Thanks,
Tian

#10 lore raw

Re: [PATCH v3 3/5] KVM: arm64: Add support for FEAT_HDBSS

Quoted 182 lines · Tian Zheng
Quoted 154 lines · Leonardo Bras
Quoted 145 lines · Tian Zheng
Quoted 136 lines · Inochi Amaoto
Quoted 115 lines · Marc Zyngier
Quoted 105 lines · Inochi Amaoto
Quoted 104 lines · Tian Zheng

From: eillon <[email protected]>

Armv9.5 introduces the Hardware Dirty Bit State Structure (HDBSS) feature, indicated by ID_AA64MMFR1_EL1.HAFDBS == 0b0100. A CPU capability is added to notify the user of the feature.

Add KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl and basic framework for ARM64 HDBSS support. Since the HDBSS buffer size is configurable and cannot be determined at KVM initialization, an IOCTL interface is required.

Actually exposing the new capability to user space happens in a later patch.

---
   arch/arm64/include/asm/cpufeature.h |  5 +++++
   arch/arm64/kernel/cpufeature.c      | 12 ++++++++++++
   arch/arm64/tools/cpucaps            |  1 +
   include/uapi/linux/kvm.h            |  1 +
   tools/include/uapi/linux/kvm.h      |  1 +
   5 files changed, 20 insertions(+)
arch/arm64/include/asm/cpufeature.h +5 -0
index 4de51f8d92cb..dcc2e2cad5ad 100644
@@ -856,6 +856,11 @@ static inline bool system_supports_haft(void)
   	return cpus_have_final_cap(ARM64_HAFT);
   }
 
+static inline bool system_supports_hdbss(void)
+{
+	return cpus_have_final_cap(ARM64_HAS_HDBSS);
+}
+
   static __always_inline bool system_supports_mpam(void)
   {
   	return alternative_has_cap_unlikely(ARM64_MPAM);
arch/arm64/kernel/cpufeature.c +12 -0
index c31f8e17732a..348b0afffc3e 100644
@@ -2124,6 +2124,11 @@ static bool hvhe_possible(const struct arm64_cpu_capabilities *entry,
   	return arm64_test_sw_feature_override(ARM64_SW_FEATURE_OVERRIDE_HVHE);
   }
 
+static bool has_vhe_hdbss(const struct arm64_cpu_capabilities *entry, int cope)
+{
+	return is_kernel_in_hyp_mode() && has_cpuid_feature(entry, cope);
+}
+
   bool cpu_supports_bbml2_noabort(void)
   {
   	/*
@@ -2759,6 +2764,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
   		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HAFT)
   	},
   #endif
+	{
+		.desc = "Hardware Dirty state tracking structure (HDBSS)",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HDBSS,
+		.matches = has_vhe_hdbss,
+		ARM64_CPUID_FIELDS(ID_AA64MMFR1_EL1, HAFDBS, HDBSS)
+	},
   	{
   		.desc = "CRC32 instructions",
   		.capability = ARM64_HAS_CRC32,
arch/arm64/tools/cpucaps +1 -0
index 7261553b644b..f6ece5b85532 100644
@@ -68,6 +68,7 @@ HAS_VA52
   HAS_VIRT_HOST_EXTN
   HAS_WFXT
   HAS_XNX
+HAS_HDBSS
   HAFT
   HW_DBM
   KVM_HVHE
include/uapi/linux/kvm.h +1 -0
index 65500f5db379..15ee42cdbd51 100644
@@ -985,6 +985,7 @@ struct kvm_enable_cap {
   #define KVM_CAP_ARM_SEA_TO_USER 245
   #define KVM_CAP_S390_USER_OPEREXEC 246
   #define KVM_CAP_S390_KEYOP 247
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
   struct kvm_irq_routing_irqchip {
   	__u32 irqchip;
tools/include/uapi/linux/kvm.h +1 -0
index dddb781b0507..93e0a1e14dc7 100644
@@ -974,6 +974,7 @@ struct kvm_enable_cap {
   #define KVM_CAP_GUEST_MEMFD_FLAGS 244
   #define KVM_CAP_ARM_SEA_TO_USER 245
   #define KVM_CAP_S390_USER_OPEREXEC 246
+#define KVM_CAP_ARM_HW_DIRTY_STATE_TRACK 248
 
Quoted 8 lines
Quoted 5 lines
tools/include/uapi/linux/kvm.h +0 -1
   struct kvm_irq_routing_irqchip {
   	__u32 irqchip;
--

2.33.0

Instead of having these architecture specific capability, I wonder if we can add a generic capability like "KVM_CAP_HW_DIRTY_STATE", so other architecture supports similar things can reuse this capability,

Quoted 3 lines

What of the existing stuff doing the same thing? x86's PML, to start with?

In fact I think the HDBSS is the first one with non-fixed size. Although there is a in process RISC-V extension for it, there will be a long story to make it ratified.

Quoted 13 lines
Quoted 4 lines

For this generic thing I suggest, the getter returns the max support entry count (or the buffer size) it supports like the dirty ring capability. And the setter just let the architecture set the parameters based on the user request.

This looks wrong on a number of levels.

- If you want something generic, there is the existing dirty log/bitmap. How this stuff is populated is none of the user's business (trapping write accesses, dirty bit collection from the PTs, or HW-generated log), and we don't need an extra feature for it. Performance will obviously suck, but that's what you pay for something abstracted and cross-architecture.

Quoted 7 lines
Quoted 4 lines

- If you want something architecture specific, then it can't be generic, by definition. You get the raw speed and compatibility with other arch-specific extensions.

OK, I agree, it is better to keep this thing arch-specific. Doing a generic thing does not benefit too much, I have made a mistake on it. Thanks for your kindly explanation.

Quoted 8 lines

Awesome. Thanks for the review.

I agree with Marc—keeping this ARM-specific is the right approach. Also, in v4 we're removing the ioctl interface entirely. HDBSS will be auto-enabled during migration setup and auto-disabled when migration completes, so the capability naming issue becomes moot.

I plan to post v4 with the updated approach soon.

Quoted 8 lines

Hi Tian,

Any updates in the v4?
Please let me know if you want help in any kind, in regards to this patchset.

Thanks!
Leo

Hi Leo,

Thanks for the follow-up. I've finished polishing v4 and am doing one last internal review.

In this version, I've also aligned with the latest HACDBS v2 implementation you shared—e.g.,
auto-inserting KVM_PGTABLE_S2_DBM into pgt->flags during kvm_init_stage2_mmu.

One note: dirty ring mode is not yet supported in v4—the series currently focuses on the
dirty bitmap mode. I'll explicitly call this out in the commit messages (including that
KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE and related dirty ring components are omitted) to avoid
any confusion during review.

Quoted 4 lines

I plan to send v4 out later this week.

Thanks,
Tian

Hi Tian,

Awesome! Thanks for letting me know!
Leo

author
#11 lore raw

PATCH v3 4/5 KVM: arm64: Enable HDBSS support and handle HDBSSF events

In reply to Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
 arch/arm64/include/asm/esr.h      |   5 ++
 arch/arm64/include/asm/kvm_host.h |  17 +++++
 arch/arm64/include/asm/kvm_mmu.h  |   1 +
 arch/arm64/include/asm/sysreg.h   |  11 ++++
 arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
 arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
 arch/arm64/kvm/reset.c            |   3 +
 8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
 #ifndef __ASSEMBLER__
 #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
 static inline unsigned long esr_brk_comment(unsigned long esr)
 {
 	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
 #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
 #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
 /*
  * Mode of operation configurable with kvm-arm.mode early param.
  * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
 u32 __attribute_const__ kvm_target_cpu(void);
 void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
 struct kvm_hyp_memcache {
 	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
 };
 
 struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
 struct vncr_tlb;
 
 struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
 	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
 	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
 };
 
 /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
 int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
 phys_addr_t kvm_mmu_get_httbr(void);
 phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
 #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
 					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
 /*
  * Definitions for GICv5 instructions
  */
arch/arm64/kvm/arm.c +102 -0

133 lines not on this page · raw message

arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
 	local_irq_restore(flags);
 }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 {
 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 	__vcpu_load_switch_sysregs(vcpu);
 	__vcpu_load_activate_traps(vcpu);
 	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
 }
 
 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
 {
+	kvm_flush_hdbss_buffer(vcpu);
 	__vcpu_put_deactivate_traps(vcpu);
 	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +70 -0

88 lines not on this page · raw message

arch/arm64/kvm/reset.c +3 -0
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
 	kfree(vcpu->arch.vncr_tlb);
 	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)

--
2.33.0

#12 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Hi Tian, eillon,

Quoted 4 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

I wonder if it would not be better just to use the feature if available, instead of needing to have userspace enabling it.

Quoted 5 lines

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

That way we have to actually take a fault for every write you do after migration starts.

What if, instead, we put the DBM bit in every memory faulted as writable (or better, due to a write fault), so from the beginning of the VM we know if memory is RO, WC (writable-clean) or WD (writable-dirty).

On top of that, we don't actually have to take faults when migration starts, as HDBSS is tracking it all.

Quoted 2 lines

KVM does not scan S2 page tables to consume DBM.

You mean dirty pages?
If so, it will actually use dirty-log or dirty-rings to track the dirty pages, and not scan S2 on every iteration.

Quoted 123 lines

Instead, when HDBSS is
enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
 arch/arm64/include/asm/esr.h      |   5 ++
 arch/arm64/include/asm/kvm_host.h |  17 +++++
 arch/arm64/include/asm/kvm_mmu.h  |   1 +
 arch/arm64/include/asm/sysreg.h   |  11 ++++
 arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
 arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
 arch/arm64/kvm/reset.c            |   3 +
 8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
 #ifndef __ASSEMBLER__
 #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
 static inline unsigned long esr_brk_comment(unsigned long esr)
 {
 	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
 #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
 #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
 /*
  * Mode of operation configurable with kvm-arm.mode early param.
  * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
 u32 __attribute_const__ kvm_target_cpu(void);
 void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
 struct kvm_hyp_memcache {
 	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
 };
 
 struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
 struct vncr_tlb;
 
 struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
 	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
 	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
 };
 
 /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
 int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
 phys_addr_t kvm_mmu_get_httbr(void);
 phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +1 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
 #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
 					       GCS_CAP_VALID_TOKEN)
+

arch/arm64/include/asm/sysreg.h line 1052

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +10 -0
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+

Do we actually need the GENMASK above? Could not we use just the HDBSSBR_EL2_BADDR_MASK?

If the base address received in alloc_pages is not properly aligned, we might end up writing in some different memory region that we allocated on.

If you want to actually make sure the mem region is aligned, check just after it's allocated, instead of silently masking it at this moment.

In any case, I wonder if we actually need above defines, as it looks they could easily be replaced by what they do.

Quoted 96 lines
arch/arm64/include/asm/sysreg.h +0 -0
 /*
  * Definitions for GICv5 instructions
  */
arch/arm64/kvm/arm.c +81 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
 }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 			    struct kvm_enable_cap *cap)
 {

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 270

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
 		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;

If we prefer using a ioctl, I wonder if it would not be better to have a arch-generic option that enables hw dirty-bit tracking, and all archs could use it to implement their versions when available.

I guess any VMM would have a much easier time doing it once, than for every arch they support.

Quoted 192 lines
arch/arm64/kvm/arm.c +16 -0
 	default:
 		break;
 	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 			r = kvm_supports_cacheable_pfnmap();
 		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
 	default:
 		r = 0;
 	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
 		if (kvm_dirty_ring_check_request(vcpu))
 			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
 		check_nested_vcpu_requests(vcpu);
 	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
 {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
 }
 
 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
 	local_irq_restore(flags);
 }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 {
 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 	__vcpu_load_switch_sysregs(vcpu);
 	__vcpu_load_activate_traps(vcpu);
 	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
 }
 
 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
 {
+	kvm_flush_hdbss_buffer(vcpu);
 	__vcpu_put_deactivate_traps(vcpu);
 	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +70 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
 	if (exec_fault)
 		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
 /**
  * kvm_handle_guest_abort - handles all 2nd stage aborts
  * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
 	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
 	if (esr_fsc_is_translation_fault(esr)) {
 		/* Beyond sanitised PARange (which is the IPA limit) */
 		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
 	kfree(vcpu->arch.vncr_tlb);
 	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--
Quoted 2 lines

2.33.0

Thx,
Leo

author
#13 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 11 lines · Leonardo Bras

Hi Tian, eillon,

Quoted 4 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

I wonder if it would not be better just to use the feature if available, instead of needing to have userspace enabling it.

I agree. If we decide to make HDBSS automatically enabled, then userspace would no longer need an explicit ioctl to turn it on. In that case, the only userspace‑visible control we may still need is a parameter to specify the HDBSS buffer size, with the kernel providing a reasonable default (for example, 4 KB).

Under such a model, the workflow could be simplified to:
1. HDBSS is automatically enabled during KVM_SET_USER_MEMORY_REGION if the feature is available.
2. HDBSS is automatically disabled when the source VM stops.

Quoted 9 lines
Quoted 5 lines

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

That way we have to actually take a fault for every write you do after migration starts.

Quoted 7 lines

What if, instead, we put the DBM bit in every memory faulted as writable (or better, due to a write fault), so from the beginning of the VM we know if memory is RO, WC (writable-clean) or WD (writable-dirty).

On top of that, we don't actually have to take faults when migration starts, as HDBSS is tracking it all.

That makes sense. Pre‑setting DBM on writable mappings would avoid taking write faults after migration starts, I will optimize this in the next version.

Quoted 7 lines
Quoted 2 lines

KVM does not scan S2 page tables to consume DBM.

You mean dirty pages?
If so, it will actually use dirty-log or dirty-rings to track the dirty pages, and not scan S2 on every iteration.

Sorry for the confusion — what I meant is that if I only added the DBM bit, then relying on DBM for dirty‑page tracking would require scanning the S2 page tables to find which PTEs have DBM set, and then updating the dirty bitmap. That would obviously be expensive.

However, in the current patch series, DBM is used together with HDBSS. With HDBSS enabled, the hardware directly tracks the writable‑clean -> writable‑dirty transitions and push it to HDBSS buffer, so we no longer need to walk the S2 page tables at all. This is the main reason why combining HDBSS with DBM provides a meaningful optimization.

I will clarify this more clearly in the next version.

Quoted 137 lines
Quoted 123 lines

Instead, when HDBSS is
enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
  arch/arm64/include/asm/esr.h      |   5 ++
  arch/arm64/include/asm/kvm_host.h |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h  |   1 +
  arch/arm64/include/asm/sysreg.h   |  11 ++++
  arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
  arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
  arch/arm64/kvm/reset.c            |   3 +
  8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
  #ifndef __ASSEMBLER__
  #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
  static inline unsigned long esr_brk_comment(unsigned long esr)
  {
  	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
  #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
  #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  				     KVM_DIRTY_LOG_INITIALLY_SET)
 
  #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
  /*
   * Mode of operation configurable with kvm-arm.mode early param.
   * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
  u32 __attribute_const__ kvm_target_cpu(void);
  void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
  struct kvm_hyp_memcache {
  	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
  	 * the associated pKVM instance in the hypervisor.
  	 */
  	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
  };
 
  struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
  	bool		reset;
  };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
  struct vncr_tlb;
 
  struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
  	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
  	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
  };
 
  /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
  int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
  int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
  phys_addr_t kvm_mmu_get_httbr(void);
  phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +1 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
  #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
  					       GCS_CAP_VALID_TOKEN)
+

arch/arm64/include/asm/sysreg.h line 1052

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +10 -0
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+

Do we actually need the GENMASK above? Could not we use just the HDBSSBR_EL2_BADDR_MASK?

Quoted 10 lines

If the base address received in alloc_pages is not properly aligned, we might end up writing in some different memory region that we allocated on.

If you want to actually make sure the mem region is aligned, check just after it's allocated, instead of silently masking it at this moment.

In any case, I wonder if we actually need above defines, as it looks they could easily be replaced by what they do.

You're right, I will replace it with HDBSSBR_EL2_BADDR_MASK, and I will add an explicit check to ensure that the physical address returned by alloc_pages() is properly aligned.

I agree that some of them may not be necessary once the alignment is validated. I will review them and simplify the definitions where appropriate.

Quoted 109 lines
Quoted 96 lines
arch/arm64/include/asm/sysreg.h +0 -0
  /*
   * Definitions for GICv5 instructions
   */
arch/arm64/kvm/arm.c +81 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
  int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  			    struct kvm_enable_cap *cap)
  {

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 270

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		r = 0;
  		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
  		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;

If we prefer using a ioctl, I wonder if it would not be better to have a arch-generic option that enables hw dirty-bit tracking, and all archs could use it to implement their versions when available.

Quoted 3 lines

I guess any VMM would have a much easier time doing it once, than for every arch they support.

I think that even if we eventually decide to enable HDBSS by default, userspace will still need an ioctl to configure the HDBSS buffer size. So an interface is required anyway.

Also, it makes sense to expose this as an arch‑generic capability rather than an ARM‑specific one. I will rename the ioctl to something like KVM_CAP_HW_DIRTY_STATE_TRACK, and each architecture can implement its own hardware‑assisted dirty tracking when available.

I will update the interface in the next version.

Quoted 198 lines
Quoted 192 lines
arch/arm64/kvm/arm.c +16 -0
  	default:
  		break;
  	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  			r = kvm_supports_cacheable_pfnmap();
  		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
  	default:
  		r = 0;
  	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
  		if (kvm_dirty_ring_check_request(vcpu))
  			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
  		check_nested_vcpu_requests(vcpu);
  	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
  void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
  }
 
  static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
  	local_irq_restore(flags);
  }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
  void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  {
  	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  	__vcpu_load_switch_sysregs(vcpu);
  	__vcpu_load_activate_traps(vcpu);
  	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
  }
 
  void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
  {
+	kvm_flush_hdbss_buffer(vcpu);
  	__vcpu_put_deactivate_traps(vcpu);
  	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +70 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (writable)
  		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
  	if (exec_fault)
  		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
  	return 0;
  }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
  /**
   * kvm_handle_guest_abort - handles all 2nd stage aborts
   * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
  	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
  	if (esr_fsc_is_translation_fault(esr)) {
  		/* Beyond sanitised PARange (which is the IPA limit) */
  		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
  	kfree(vcpu->arch.vncr_tlb);
  	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
  }
 
  static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--
Quoted 2 lines

2.33.0

Thx,
Leo

#14 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 21 lines · Tian Zheng
Quoted 11 lines · Leonardo Bras

Hi Tian, eillon,

Quoted 4 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

I wonder if it would not be better just to use the feature if available, instead of needing to have userspace enabling it.

I agree. If we decide to make HDBSS automatically enabled, then userspace would no longer need an explicit ioctl to turn it on. In that case, the only userspace‑visible control we may still need is a parameter to specify the HDBSS buffer size, with the kernel providing a reasonable default (for example, 4 KB).

Quoted 5 lines

Under such a model, the workflow could be simplified to:
1. HDBSS is automatically enabled during KVM_SET_USER_MEMORY_REGION if the feature is available.
2. HDBSS is automatically disabled when the source VM stops.

I suggest we allocate the buffers and enable HDBSS during the first step of live migration, this way we don't need o have this memory usage during the lifetime of the VM, and we turn on HDBSS only when needed.

Quoted 34 lines
Quoted 9 lines
Quoted 5 lines

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

That way we have to actually take a fault for every write you do after migration starts.

Quoted 7 lines

What if, instead, we put the DBM bit in every memory faulted as writable (or better, due to a write fault), so from the beginning of the VM we know if memory is RO, WC (writable-clean) or WD (writable-dirty).

On top of that, we don't actually have to take faults when migration starts, as HDBSS is tracking it all.

That makes sense. Pre‑setting DBM on writable mappings would avoid taking write faults after migration starts, I will optimize this in the next version.

Quoted 7 lines
Quoted 2 lines

KVM does not scan S2 page tables to consume DBM.

You mean dirty pages?
If so, it will actually use dirty-log or dirty-rings to track the dirty pages, and not scan S2 on every iteration.

Sorry for the confusion — what I meant is that if I only added the DBM bit, then relying on DBM for dirty‑page tracking would require scanning the S2 page tables to find which PTEs have DBM set, and then updating the dirty bitmap. That would obviously be expensive.

Quoted 7 lines

However, in the current patch series, DBM is used together with HDBSS. With HDBSS enabled, the hardware directly tracks the writable‑clean -> writable‑dirty transitions and push it to HDBSS buffer, so we no longer need to walk the S2 page tables at all. This is the main reason why combining HDBSS with DBM provides a meaningful optimization.

I will clarify this more clearly in the next version.

Got it! Thanks for making it clear!
I would just mention that you are transfering the recorded data to the dirty log, then.

Quoted 144 lines
Quoted 144 lines
Quoted 124 lines

Instead, when HDBSS is
enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
  arch/arm64/include/asm/esr.h      |   5 ++
  arch/arm64/include/asm/kvm_host.h |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h  |   1 +
  arch/arm64/include/asm/sysreg.h   |  11 ++++
  arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
  arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
  arch/arm64/kvm/reset.c            |   3 +
  8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
  #ifndef __ASSEMBLER__
  #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
  static inline unsigned long esr_brk_comment(unsigned long esr)
  {
  	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
  #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
  #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  				     KVM_DIRTY_LOG_INITIALLY_SET)
 
  #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
  /*
   * Mode of operation configurable with kvm-arm.mode early param.
   * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
  u32 __attribute_const__ kvm_target_cpu(void);
  void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
  struct kvm_hyp_memcache {
  	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
  	 * the associated pKVM instance in the hypervisor.
  	 */
  	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
  };
 
  struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
  	bool		reset;
  };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
  struct vncr_tlb;
 
  struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
  	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
  	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
  };
 
  /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
  int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
  int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
  phys_addr_t kvm_mmu_get_httbr(void);
  phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +1 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
  #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
  					       GCS_CAP_VALID_TOKEN)
+

arch/arm64/include/asm/sysreg.h line 1052

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +10 -0
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+

Do we actually need the GENMASK above? Could not we use just the HDBSSBR_EL2_BADDR_MASK?

If the base address received in alloc_pages is not properly aligned, we might end up writing in some different memory region that we allocated on.

If you want to actually make sure the mem region is aligned, check just after it's allocated, instead of silently masking it at this moment.

Quoted 8 lines
Quoted 4 lines

In any case, I wonder if we actually need above defines, as it looks they could easily be replaced by what they do.

You're right, I will replace it with HDBSSBR_EL2_BADDR_MASK, and I will add an explicit check to ensure that the physical address returned by alloc_pages() is properly aligned.

I recommend checking if the used function have any garantees in respect to alignment, so that maybe we may not actually need to check.

Quoted 4 lines

I agree that some of them may not be necessary once the alignment is validated. I will review them and simplify the definitions where appropriate.

Thanks!

Quoted 110 lines
Quoted 110 lines
Quoted 97 lines
arch/arm64/include/asm/sysreg.h +0 -0
  /*
   * Definitions for GICv5 instructions
   */
arch/arm64/kvm/arm.c +81 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
  int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  			    struct kvm_enable_cap *cap)
  {

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 270

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		r = 0;
  		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
  		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;

If we prefer using a ioctl, I wonder if it would not be better to have a arch-generic option that enables hw dirty-bit tracking, and all archs could use it to implement their versions when available.

Quoted 7 lines
Quoted 3 lines

I guess any VMM would have a much easier time doing it once, than for every arch they support.

I think that even if we eventually decide to enable HDBSS by default, userspace will still need an ioctl to configure the HDBSS buffer size. So an interface is required anyway.

That's a valid argument. But we could as well have those configured based on VM memory size, or other parameter from the VM.
Since it could be allocated just during the migration, we may have some flexibility on size.

But sure, we could have a default value and let user (optionally) configure the hdbss percpu bufsize.

Quoted 5 lines

Also, it makes sense to expose this as an arch‑generic capability rather than an ARM‑specific one. I will rename the ioctl to something like KVM_CAP_HW_DIRTY_STATE_TRACK, and each architecture can implement its own hardware‑assisted dirty tracking when available.

I wonder if we need a new capability for this, at all. Couldn't we only use the feature when available?

Quoted 3 lines

I will update the interface in the next version.

Thanks!
Leo

Quoted 199 lines
Quoted 198 lines
Quoted 192 lines
arch/arm64/kvm/arm.c +16 -0
  	default:
  		break;
  	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  			r = kvm_supports_cacheable_pfnmap();
  		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
  	default:
  		r = 0;
  	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
  		if (kvm_dirty_ring_check_request(vcpu))
  			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
  		check_nested_vcpu_requests(vcpu);
  	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
  void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
  }
 
  static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
  	local_irq_restore(flags);
  }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
  void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  {
  	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  	__vcpu_load_switch_sysregs(vcpu);
  	__vcpu_load_activate_traps(vcpu);
  	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
  }
 
  void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
  {
+	kvm_flush_hdbss_buffer(vcpu);
  	__vcpu_put_deactivate_traps(vcpu);
  	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +70 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (writable)
  		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
  	if (exec_fault)
  		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
  	return 0;
  }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
  /**
   * kvm_handle_guest_abort - handles all 2nd stage aborts
   * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
  	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
  	if (esr_fsc_is_translation_fault(esr)) {
  		/* Beyond sanitised PARange (which is the IPA limit) */
  		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
  	kfree(vcpu->arch.vncr_tlb);
  	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
  }
 
  static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--
Quoted 2 lines

2.33.0

Thx,
Leo

author
#15 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Hi Leo,

Quoted 19 lines · Leonardo Bras
Quoted 18 lines · Tian Zheng
Quoted 10 lines · Leonardo Bras

Hi Tian, eillon,

Quoted 4 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

I wonder if it would not be better just to use the feature if available, instead of needing to have userspace enabling it.

I agree. If we decide to make HDBSS automatically enabled, then userspace would no longer need an explicit ioctl to turn it on. In that case, the only userspace‑visible control we may still need is a parameter to specify the HDBSS buffer size, with the kernel providing a reasonable default (for example, 4 KB).

Quoted 8 lines
Quoted 5 lines

Under such a model, the workflow could be simplified to:
1. HDBSS is automatically enabled during KVM_SET_USER_MEMORY_REGION if the feature is available.
2. HDBSS is automatically disabled when the source VM stops.

I suggest we allocate the buffers and enable HDBSS during the first step of live migration, this way we don't need o have this memory usage during the lifetime of the VM, and we turn on HDBSS only when needed.

Yes, we also think that enabling this feature and allocating the buffers during the

first step of live migration is the right approach.

Quoted 35 lines
Quoted 35 lines
Quoted 8 lines
Quoted 5 lines

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

That way we have to actually take a fault for every write you do after migration starts.

Quoted 7 lines

What if, instead, we put the DBM bit in every memory faulted as writable (or better, due to a write fault), so from the beginning of the VM we know if memory is RO, WC (writable-clean) or WD (writable-dirty).

On top of that, we don't actually have to take faults when migration starts, as HDBSS is tracking it all.

That makes sense. Pre‑setting DBM on writable mappings would avoid taking write faults after migration starts, I will optimize this in the next version.

Quoted 5 lines
Quoted 1 line

KVM does not scan S2 page tables to consume DBM.

You mean dirty pages?
If so, it will actually use dirty-log or dirty-rings to track the dirty pages, and not scan S2 on every iteration.

Sorry for the confusion — what I meant is that if I only added the DBM bit, then relying on DBM for dirty‑page tracking would require scanning the S2 page tables to find which PTEs have DBM set, and then updating the dirty bitmap. That would obviously be expensive.

However, in the current patch series, DBM is used together with HDBSS. With HDBSS enabled, the hardware directly tracks the writable‑clean -> writable‑dirty transitions and push it to HDBSS buffer, so we no longer need to walk the S2 page tables at all. This is the main reason why combining HDBSS with DBM provides a meaningful optimization.

Quoted 4 lines
Quoted 1 line

I will clarify this more clearly in the next version.

Got it! Thanks for making it clear!
I would just mention that you are transfering the recorded data to the dirty log, then.

You're welcome! I'm glad the explanation is clear now.

Quoted 146 lines
Quoted 146 lines
Quoted 146 lines
Quoted 123 lines

Instead, when HDBSS is
enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
   arch/arm64/include/asm/esr.h      |   5 ++
   arch/arm64/include/asm/kvm_host.h |  17 +++++
   arch/arm64/include/asm/kvm_mmu.h  |   1 +
   arch/arm64/include/asm/sysreg.h   |  11 ++++
   arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
   arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
   arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
   arch/arm64/kvm/reset.c            |   3 +
   8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
   #ifndef __ASSEMBLER__
   #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
   static inline unsigned long esr_brk_comment(unsigned long esr)
   {
   	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
   #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
   #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
   				     KVM_DIRTY_LOG_INITIALLY_SET)
 
   #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
   /*
    * Mode of operation configurable with kvm-arm.mode early param.
    * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
   u32 __attribute_const__ kvm_target_cpu(void);
   void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
   void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
   struct kvm_hyp_memcache {
   	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
   	 * the associated pKVM instance in the hypervisor.
   	 */
   	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
   };
 
   struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
   	bool		reset;
   };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
   struct vncr_tlb;
 
   struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
   	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
   	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
   };
 
   /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
   int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
   int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
   phys_addr_t kvm_mmu_get_httbr(void);
   phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +1 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
   #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
   					       GCS_CAP_VALID_TOKEN)
+

arch/arm64/include/asm/sysreg.h line 1052

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +10 -0
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+

Do we actually need the GENMASK above? Could not we use just the HDBSSBR_EL2_BADDR_MASK?

If the base address received in alloc_pages is not properly aligned, we might end up writing in some different memory region that we allocated on.

If you want to actually make sure the mem region is aligned, check just after it's allocated, instead of silently masking it at this moment.

In any case, I wonder if we actually need above defines, as it looks they could easily be replaced by what they do.

Quoted 5 lines
Quoted 3 lines

You're right, I will replace it with HDBSSBR_EL2_BADDR_MASK, and I will add an explicit check to ensure that the physical address returned by alloc_pages() is properly aligned.

I recommend checking if the used function have any garantees in respect to alignment, so that maybe we may not actually need to check.

Ok, I will check that and confirm whether the function provides any alignment

guarantees.

Quoted 118 lines
Quoted 3 lines

I agree that some of them may not be necessary once the alignment is validated. I will review them and simplify the definitions where appropriate.

Thanks!

Quoted 108 lines
Quoted 108 lines
Quoted 96 lines
arch/arm64/include/asm/sysreg.h +0 -0
   /*
    * Definitions for GICv5 instructions
    */
arch/arm64/kvm/arm.c +81 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
   	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
   }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
   int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   			    struct kvm_enable_cap *cap)
   {

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 270

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   		r = 0;
   		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
   		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;

If we prefer using a ioctl, I wonder if it would not be better to have a arch-generic option that enables hw dirty-bit tracking, and all archs could use it to implement their versions when available.

Quoted 6 lines
Quoted 3 lines

I guess any VMM would have a much easier time doing it once, than for every arch they support.

I think that even if we eventually decide to enable HDBSS by default, userspace will still need an ioctl to configure the HDBSS buffer size. So an interface is required anyway.

Quoted 7 lines

That's a valid argument. But we could as well have those configured based on VM memory size, or other parameter from the VM.
Since it could be allocated just during the migration, we may have some flexibility on size.

But sure, we could have a default value and let user (optionally) configure the hdbss percpu bufsize.

That's a good idea! We can automatically determine an appropriate buffer size

when this feature is enabled during the first step of live migration, and then we

can remove the ioctl interface.

I will update this in the next version.

Quoted 7 lines
Quoted 4 lines

Also, it makes sense to expose this as an arch‑generic capability rather than an ARM‑specific one. I will rename the ioctl to something like KVM_CAP_HW_DIRTY_STATE_TRACK, and each architecture can implement its own hardware‑assisted dirty tracking when available.

I wonder if we need a new capability for this, at all. Couldn't we only use the feature when available?

If we decide to enable this feature entirely inside KVM, we could remove

this interface.

Quoted 202 lines
Quoted 2 lines

I will update the interface in the next version.

Thanks!
Leo

Quoted 197 lines
Quoted 197 lines
Quoted 192 lines
arch/arm64/kvm/arm.c +16 -0
   	default:
   		break;
   	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
   			r = kvm_supports_cacheable_pfnmap();
   		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
   	default:
   		r = 0;
   	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
   		if (kvm_dirty_ring_check_request(vcpu))
   			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
   		check_nested_vcpu_requests(vcpu);
   	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
   void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
   {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
   }
 
   static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
   	local_irq_restore(flags);
   }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
   void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   {
   	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   	__vcpu_load_switch_sysregs(vcpu);
   	__vcpu_load_activate_traps(vcpu);
   	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
   }
 
   void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
   {
+	kvm_flush_hdbss_buffer(vcpu);
   	__vcpu_put_deactivate_traps(vcpu);
   	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +70 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
   	if (writable)
   		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
   	if (exec_fault)
   		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
   	return 0;
   }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
   /**
    * kvm_handle_guest_abort - handles all 2nd stage aborts
    * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
   	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
   	if (esr_fsc_is_translation_fault(esr)) {
   		/* Beyond sanitised PARange (which is the IPA limit) */
   		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
   	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
   	kfree(vcpu->arch.vncr_tlb);
   	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
   }
 
   static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--
Quoted 2 lines

2.33.0

Thx,
Leo

Thx,

Tian

#16 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 233 lines · Tian Zheng

Hi Leo,

Quoted 19 lines · Leonardo Bras
Quoted 18 lines · Tian Zheng
Quoted 10 lines · Leonardo Bras

Hi Tian, eillon,

Quoted 4 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

I wonder if it would not be better just to use the feature if available, instead of needing to have userspace enabling it.

I agree. If we decide to make HDBSS automatically enabled, then userspace would no longer need an explicit ioctl to turn it on. In that case, the only userspace‑visible control we may still need is a parameter to specify the HDBSS buffer size, with the kernel providing a reasonable default (for example, 4 KB).

Quoted 8 lines
Quoted 5 lines

Under such a model, the workflow could be simplified to:
1. HDBSS is automatically enabled during KVM_SET_USER_MEMORY_REGION if the feature is available.
2. HDBSS is automatically disabled when the source VM stops.

I suggest we allocate the buffers and enable HDBSS during the first step of live migration, this way we don't need o have this memory usage during the lifetime of the VM, and we turn on HDBSS only when needed.

Yes, we also think that enabling this feature and allocating the buffers during the

first step of live migration is the right approach.

Quoted 35 lines
Quoted 35 lines
Quoted 8 lines
Quoted 5 lines

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

That way we have to actually take a fault for every write you do after migration starts.

Quoted 7 lines

What if, instead, we put the DBM bit in every memory faulted as writable (or better, due to a write fault), so from the beginning of the VM we know if memory is RO, WC (writable-clean) or WD (writable-dirty).

On top of that, we don't actually have to take faults when migration starts, as HDBSS is tracking it all.

That makes sense. Pre‑setting DBM on writable mappings would avoid taking write faults after migration starts, I will optimize this in the next version.

Quoted 5 lines
Quoted 1 line

KVM does not scan S2 page tables to consume DBM.

You mean dirty pages?
If so, it will actually use dirty-log or dirty-rings to track the dirty pages, and not scan S2 on every iteration.

Sorry for the confusion — what I meant is that if I only added the DBM bit, then relying on DBM for dirty‑page tracking would require scanning the S2 page tables to find which PTEs have DBM set, and then updating the dirty bitmap. That would obviously be expensive.

However, in the current patch series, DBM is used together with HDBSS. With HDBSS enabled, the hardware directly tracks the writable‑clean -> writable‑dirty transitions and push it to HDBSS buffer, so we no longer need to walk the S2 page tables at all. This is the main reason why combining HDBSS with DBM provides a meaningful optimization.

Quoted 4 lines
Quoted 1 line

I will clarify this more clearly in the next version.

Got it! Thanks for making it clear!
I would just mention that you are transfering the recorded data to the dirty log, then.

You're welcome! I'm glad the explanation is clear now.

Quoted 149 lines
Quoted 149 lines
Quoted 136 lines
Quoted 123 lines

Instead, when HDBSS is
enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
   arch/arm64/include/asm/esr.h      |   5 ++
   arch/arm64/include/asm/kvm_host.h |  17 +++++
   arch/arm64/include/asm/kvm_mmu.h  |   1 +
   arch/arm64/include/asm/sysreg.h   |  11 ++++
   arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
   arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
   arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
   arch/arm64/kvm/reset.c            |   3 +
   8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
   #ifndef __ASSEMBLER__
   #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
   static inline unsigned long esr_brk_comment(unsigned long esr)
   {
   	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
   #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
   #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
   				     KVM_DIRTY_LOG_INITIALLY_SET)
 
   #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
   /*
    * Mode of operation configurable with kvm-arm.mode early param.
    * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
   u32 __attribute_const__ kvm_target_cpu(void);
   void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
   void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
   struct kvm_hyp_memcache {
   	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
   	 * the associated pKVM instance in the hypervisor.
   	 */
   	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
   };
 
   struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
   	bool		reset;
   };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
   struct vncr_tlb;
 
   struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
   	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
   	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
   };
 
   /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
   int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
   int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
   phys_addr_t kvm_mmu_get_httbr(void);
   phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +1 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
   #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
   					       GCS_CAP_VALID_TOKEN)
+

arch/arm64/include/asm/sysreg.h line 1052

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +10 -0
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+

Do we actually need the GENMASK above? Could not we use just the HDBSSBR_EL2_BADDR_MASK?

Quoted 10 lines

If the base address received in alloc_pages is not properly aligned, we might end up writing in some different memory region that we allocated on.

If you want to actually make sure the mem region is aligned, check just after it's allocated, instead of silently masking it at this moment.

In any case, I wonder if we actually need above defines, as it looks they could easily be replaced by what they do.

You're right, I will replace it with HDBSSBR_EL2_BADDR_MASK, and I will add an explicit check to ensure that the physical address returned by alloc_pages() is properly aligned.

Quoted 10 lines
Quoted 2 lines

I recommend checking if the used function have any garantees in respect to alignment, so that maybe we may not actually need to check.

Ok, I will check that and confirm whether the function provides any alignment

guarantees.

See below comment

Quoted 57 lines
Quoted 57 lines
Quoted 3 lines

I agree that some of them may not be necessary once the alignment is validated. I will review them and simplify the definitions where appropriate.

Thanks!

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
arch/arm64/include/asm/sysreg.h +0 -0
   /*
    * Definitions for GICv5 instructions
    */
arch/arm64/kvm/arm.c +42 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
   	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
   }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 177

Quoted 8 lines
arch/arm64/kvm/arm.c +8 -0
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);

/**
* alloc_pages - Allocate pages.
* @gfp: GFP flags.
* @order: Power of two of number of pages to allocate.
*
* Allocate 1 << @order contiguous pages. The physical address of the
* first page is naturally aligned (eg an order-3 allocation will be aligned
* to a multiple of 8 * PAGE_SIZE bytes). The NUMA policy of the current
* process is honoured when in process context.
*
* Context: Can be called from any context, providing the appropriate GFP
* flags are used.
* Return: The page on success or NULL if allocation fails.
*/

It looks like we are safe from the aspect of alignment, according to the documentation on alloc_pages.

I would rename the variable 'size' here, it could be misleading, even though the ioctl docs state that it's the order.

Quoted 78 lines
Quoted 53 lines
Quoted 47 lines
Quoted 47 lines
Quoted 35 lines
arch/arm64/kvm/arm.c +31 -0
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
   int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   			    struct kvm_enable_cap *cap)
   {

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 270

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   		r = 0;
   		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
   		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;

If we prefer using a ioctl, I wonder if it would not be better to have a arch-generic option that enables hw dirty-bit tracking, and all archs could use it to implement their versions when available.

Quoted 6 lines
Quoted 3 lines

I guess any VMM would have a much easier time doing it once, than for every arch they support.

I think that even if we eventually decide to enable HDBSS by default, userspace will still need an ioctl to configure the HDBSS buffer size. So an interface is required anyway.

Quoted 7 lines

That's a valid argument. But we could as well have those configured based on VM memory size, or other parameter from the VM.
Since it could be allocated just during the migration, we may have some flexibility on size.

But sure, we could have a default value and let user (optionally) configure the hdbss percpu bufsize.

That's a good idea! We can automatically determine an appropriate buffer size

when this feature is enabled during the first step of live migration, and then we

can remove the ioctl interface.

I will update this in the next version.

Quoted 4 lines
Quoted 4 lines

Also, it makes sense to expose this as an arch‑generic capability rather than an ARM‑specific one. I will rename the ioctl to something like KVM_CAP_HW_DIRTY_STATE_TRACK, and each architecture can implement its own hardware‑assisted dirty tracking when available.

Quoted 8 lines
Quoted 2 lines

I wonder if we need a new capability for this, at all. Couldn't we only use the feature when available?

If we decide to enable this feature entirely inside KVM, we could remove

this interface.

I think that's the best option.

Thanks!
Leo

Quoted 209 lines
Quoted 203 lines
Quoted 3 lines

I will update the interface in the next version.

Thanks!
Leo

Quoted 197 lines
Quoted 197 lines
Quoted 192 lines
arch/arm64/kvm/arm.c +16 -0
   	default:
   		break;
   	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
   			r = kvm_supports_cacheable_pfnmap();
   		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
   	default:
   		r = 0;
   	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
   		if (kvm_dirty_ring_check_request(vcpu))
   			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
   		check_nested_vcpu_requests(vcpu);
   	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
   void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
   {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
   }
 
   static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
   	local_irq_restore(flags);
   }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
   void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   {
   	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   	__vcpu_load_switch_sysregs(vcpu);
   	__vcpu_load_activate_traps(vcpu);
   	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
   }
 
   void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
   {
+	kvm_flush_hdbss_buffer(vcpu);
   	__vcpu_put_deactivate_traps(vcpu);
   	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +70 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
   	if (writable)
   		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
   	if (exec_fault)
   		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
   	return 0;
   }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2p;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
   /**
    * kvm_handle_guest_abort - handles all 2nd stage aborts
    * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
   	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
   	if (esr_fsc_is_translation_fault(esr)) {
   		/* Beyond sanitised PARange (which is the IPA limit) */
   		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
   	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
   	kfree(vcpu->arch.vncr_tlb);
   	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
   }
 
   static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--
Quoted 2 lines

2.33.0

Thx,
Leo

Thx,

Tian

author
#17 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Hi Leo,

Quoted 321 lines · Leonardo Bras
Quoted 229 lines · Tian Zheng

Hi Leo,

Quoted 18 lines · Leonardo Bras
Quoted 17 lines · Tian Zheng
Quoted 10 lines · Leonardo Bras

Hi Tian, eillon,

Quoted 4 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

I wonder if it would not be better just to use the feature if available, instead of needing to have userspace enabling it.

I agree. If we decide to make HDBSS automatically enabled, then userspace would no longer need an explicit ioctl to turn it on. In that case, the only userspace‑visible control we may still need is a parameter to specify the HDBSS buffer size, with the kernel providing a reasonable default (for example, 4 KB).

Quoted 8 lines
Quoted 5 lines

Under such a model, the workflow could be simplified to:
1. HDBSS is automatically enabled during KVM_SET_USER_MEMORY_REGION if the feature is available.
2. HDBSS is automatically disabled when the source VM stops.

I suggest we allocate the buffers and enable HDBSS during the first step of live migration, this way we don't need o have this memory usage during the lifetime of the VM, and we turn on HDBSS only when needed.

Yes, we also think that enabling this feature and allocating the buffers during the

first step of live migration is the right approach.

Quoted 35 lines
Quoted 35 lines
Quoted 8 lines
Quoted 5 lines

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

That way we have to actually take a fault for every write you do after migration starts.

Quoted 7 lines

What if, instead, we put the DBM bit in every memory faulted as writable (or better, due to a write fault), so from the beginning of the VM we know if memory is RO, WC (writable-clean) or WD (writable-dirty).

On top of that, we don't actually have to take faults when migration starts, as HDBSS is tracking it all.

That makes sense. Pre‑setting DBM on writable mappings would avoid taking write faults after migration starts, I will optimize this in the next version.

Quoted 5 lines
Quoted 1 line

KVM does not scan S2 page tables to consume DBM.

You mean dirty pages?
If so, it will actually use dirty-log or dirty-rings to track the dirty pages, and not scan S2 on every iteration.

Sorry for the confusion — what I meant is that if I only added the DBM bit, then relying on DBM for dirty‑page tracking would require scanning the S2 page tables to find which PTEs have DBM set, and then updating the dirty bitmap. That would obviously be expensive.

However, in the current patch series, DBM is used together with HDBSS. With HDBSS enabled, the hardware directly tracks the writable‑clean -> writable‑dirty transitions and push it to HDBSS buffer, so we no longer need to walk the S2 page tables at all. This is the main reason why combining HDBSS with DBM provides a meaningful optimization.

Quoted 4 lines
Quoted 1 line

I will clarify this more clearly in the next version.

Got it! Thanks for making it clear!
I would just mention that you are transfering the recorded data to the dirty log, then.

You're welcome! I'm glad the explanation is clear now.

Quoted 149 lines
Quoted 149 lines
Quoted 136 lines
Quoted 123 lines

Instead, when HDBSS is
enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
    arch/arm64/include/asm/esr.h      |   5 ++
    arch/arm64/include/asm/kvm_host.h |  17 +++++
    arch/arm64/include/asm/kvm_mmu.h  |   1 +
    arch/arm64/include/asm/sysreg.h   |  11 ++++
    arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
    arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
    arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
    arch/arm64/kvm/reset.c            |   3 +
    8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
    #ifndef __ASSEMBLER__
    #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
    static inline unsigned long esr_brk_comment(unsigned long esr)
    {
    	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
    #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
    #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
    				     KVM_DIRTY_LOG_INITIALLY_SET)
 
    #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
    /*
     * Mode of operation configurable with kvm-arm.mode early param.
     * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
    u32 __attribute_const__ kvm_target_cpu(void);
    void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
    void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
    struct kvm_hyp_memcache {
    	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
    	 * the associated pKVM instance in the hypervisor.
    	 */
    	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
    };
 
    struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
    	bool		reset;
    };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
    struct vncr_tlb;
 
    struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
    	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
    	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
    };
 
    /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
    int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
    int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
    phys_addr_t kvm_mmu_get_httbr(void);
    phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +1 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
    #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
    					       GCS_CAP_VALID_TOKEN)
+

arch/arm64/include/asm/sysreg.h line 1052

Quoted 10 lines
arch/arm64/include/asm/sysreg.h +10 -0
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+

Do we actually need the GENMASK above? Could not we use just the HDBSSBR_EL2_BADDR_MASK?

Quoted 10 lines

If the base address received in alloc_pages is not properly aligned, we might end up writing in some different memory region that we allocated on.

If you want to actually make sure the mem region is aligned, check just after it's allocated, instead of silently masking it at this moment.

In any case, I wonder if we actually need above defines, as it looks they could easily be replaced by what they do.

You're right, I will replace it with HDBSSBR_EL2_BADDR_MASK, and I will add an explicit check to ensure that the physical address returned by alloc_pages() is properly aligned.

Quoted 9 lines
Quoted 2 lines

I recommend checking if the used function have any garantees in respect to alignment, so that maybe we may not actually need to check.

Ok, I will check that and confirm whether the function provides any alignment

guarantees.

See below comment

Quoted 57 lines
Quoted 57 lines
Quoted 3 lines

I agree that some of them may not be necessary once the alignment is validated. I will review them and simplify the definitions where appropriate.

Thanks!

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
arch/arm64/include/asm/sysreg.h +0 -0
    /*
     * Definitions for GICv5 instructions
     */
arch/arm64/kvm/arm.c +42 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
    	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
    }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/arm64/kvm/arm.c int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) · line 177

Quoted 8 lines
arch/arm64/kvm/arm.c +8 -0
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);

/**
* alloc_pages - Allocate pages.
* @gfp: GFP flags.
* @order: Power of two of number of pages to allocate.
*
* Allocate 1 << @order contiguous pages. The physical address of the
* first page is naturally aligned (eg an order-3 allocation will be aligned
* to a multiple of 8 * PAGE_SIZE bytes). The NUMA policy of the current
* process is honoured when in process context.
*
* Context: Can be called from any context, providing the appropriate GFP
* flags are used.
* Return: The page on success or NULL if allocation fails.
*/

Quoted 5 lines

It looks like we are safe from the aspect of alignment, according to the documentation on alloc_pages.

I would rename the variable 'size' here, it could be misleading, even though the ioctl docs state that it's the order.

Sure, I will rename it from size to order. Thx!

Quoted 80 lines
Quoted 80 lines
Quoted 53 lines
Quoted 47 lines
Quoted 47 lines
Quoted 35 lines
arch/arm64/kvm/arm.c +31 -0
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
    int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
    			    struct kvm_enable_cap *cap)
    {

arch/arm64/kvm/arm.c int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · line 270

Quoted 8 lines
arch/arm64/kvm/arm.c +5 -0
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
    		r = 0;
    		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
    		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;

If we prefer using a ioctl, I wonder if it would not be better to have a arch-generic option that enables hw dirty-bit tracking, and all archs could use it to implement their versions when available.

Quoted 6 lines
Quoted 3 lines

I guess any VMM would have a much easier time doing it once, than for every arch they support.

I think that even if we eventually decide to enable HDBSS by default, userspace will still need an ioctl to configure the HDBSS buffer size. So an interface is required anyway.

Quoted 7 lines

That's a valid argument. But we could as well have those configured based on VM memory size, or other parameter from the VM.
Since it could be allocated just during the migration, we may have some flexibility on size.

But sure, we could have a default value and let user (optionally) configure the hdbss percpu bufsize.

That's a good idea! We can automatically determine an appropriate buffer size

when this feature is enabled during the first step of live migration, and then we

can remove the ioctl interface.

I will update this in the next version.

Quoted 6 lines
Quoted 4 lines

Also, it makes sense to expose this as an arch‑generic capability rather than an ARM‑specific one. I will rename the ioctl to something like KVM_CAP_HW_DIRTY_STATE_TRACK, and each architecture can implement its own hardware‑assisted dirty tracking when available.

I wonder if we need a new capability for this, at all. Couldn't we only use the feature when available?

Quoted 216 lines
Quoted 4 lines

If we decide to enable this feature entirely inside KVM, we could remove

this interface.

I think that's the best option.

Thanks!
Leo

Quoted 207 lines
Quoted 202 lines
Quoted 2 lines

I will update the interface in the next version.

Thanks!
Leo

Quoted 197 lines
Quoted 197 lines
Quoted 192 lines
arch/arm64/kvm/arm.c +16 -0
    	default:
    		break;
    	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
    			r = kvm_supports_cacheable_pfnmap();
    		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
    	default:
    		r = 0;
    	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
    		if (kvm_dirty_ring_check_request(vcpu))
    			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
    		check_nested_vcpu_requests(vcpu);
    	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
    void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
    {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
    }
 
    static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
    	local_irq_restore(flags);
    }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
    void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
    {
    	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
    	__vcpu_load_switch_sysregs(vcpu);
    	__vcpu_load_activate_traps(vcpu);
    	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
    }
 
    void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
    {
+	kvm_flush_hdbss_buffer(vcpu);
    	__vcpu_put_deactivate_traps(vcpu);
    	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +70 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
    	if (writable)
    		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
    	if (exec_fault)
    		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
    	return 0;
    }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2p;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
    /**
     * kvm_handle_guest_abort - handles all 2nd stage aborts
     * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
    	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
    	if (esr_fsc_is_translation_fault(esr)) {
    		/* Beyond sanitised PARange (which is the IPA limit) */
    		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
    	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
    	kfree(vcpu->arch.vncr_tlb);
    	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
    }
 
    static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--
Quoted 2 lines

2.33.0

Thx,
Leo

Thx,

Tian

Thx,

Tian

#18 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

In reply to Tian Zheng

Hi Tian,

Few extra notes/questions below

Quoted 91 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
 arch/arm64/include/asm/esr.h      |   5 ++
 arch/arm64/include/asm/kvm_host.h |  17 +++++
 arch/arm64/include/asm/kvm_mmu.h  |   1 +
 arch/arm64/include/asm/sysreg.h   |  11 ++++
 arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
 arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
 arch/arm64/kvm/reset.c            |   3 +
 8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
 #ifndef __ASSEMBLER__
 #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
 static inline unsigned long esr_brk_comment(unsigned long esr)
 {
 	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +8 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
 #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
 #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
 /*
  * Mode of operation configurable with kvm-arm.mode early param.
  * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
 u32 __attribute_const__ kvm_target_cpu(void);
 void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
 struct kvm_hyp_memcache {
 	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
 };
 
 struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 832

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+

IIUC this is used once both on enable/disable and massively on vcpu_put/get.

What if we actually save just HDBSSBR_EL2 and HDBSSPROD_EL2 instead? That way we avoid having masking operations in put/get as well as any possible error we may have formatting those.

The cost is doing those operations once for enable and once for disable, which should be fine.

Quoted 162 lines
arch/arm64/include/asm/kvm_host.h +3 -0
 struct vncr_tlb;
 
 struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
 	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
 	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
 };
 
 /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
 int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
 phys_addr_t kvm_mmu_get_httbr(void);
 phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
 #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
 					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
 /*
  * Definitions for GICv5 instructions
  */
arch/arm64/kvm/arm.c +89 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
 }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 			    struct kvm_enable_cap *cap)
 {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
 		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
 	default:
 		break;
 	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 			r = kvm_supports_cacheable_pfnmap();
 		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
 	default:
 		r = 0;
 	}

arch/arm64/kvm/arm.c static int check_vcpu_requests(struct kvm_vcpu *vcpu) · line 1213

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
 		if (kvm_dirty_ring_check_request(vcpu))
 			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);

I am curious on why we need a flush-hdbss request,
Don't we have the flush function happening every time we run vcpu_put?

Oh, I see, you want to check if there is anything needed inside the inner loop of vcpu_run, without having to vcpu_put. I think it makes sense.

But instead of having this on guest entry, does not it make more sense to have it in guest exit? This way we flush every time (if needed) we exit the guest, and instead of having a vcpu request, we just require a vcpu kick and it should flush if needed.

Maybe have vcpu_put just save the registers, and add a the flush before handle_exit.

What do you think?

Quoted 103 lines
arch/arm64/kvm/arm.c +11 -0
+
 		check_nested_vcpu_requests(vcpu);
 	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
 {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
 }
 
 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
 	local_irq_restore(flags);
 }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 {
 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 	__vcpu_load_switch_sysregs(vcpu);
 	__vcpu_load_activate_traps(vcpu);
 	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
 }
 
 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
 {
+	kvm_flush_hdbss_buffer(vcpu);
 	__vcpu_put_deactivate_traps(vcpu);
 	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
 	if (exec_fault)
 		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

This will mark a page dirty for both dirty_bitmap or dirty_ring, depending on what is in use.

Out of plain curiosity, have you planned / tested for the dirty-ring as well, or just for dirty-bitmap?

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2090

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;

Humm, flushing in a fault handler means hanging there, in IRQ context, for a while.

Since we already deal with this on guest_exit (vcpu_put IIUC), why not just return in a way the vcpu has to exit the inner loop and let it flush there instead?

Thanks!
Leo

Quoted 42 lines
arch/arm64/kvm/mmu.c +15 -0
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
 /**
  * kvm_handle_guest_abort - handles all 2nd stage aborts
  * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
 	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
 	if (esr_fsc_is_translation_fault(esr)) {
 		/* Beyond sanitised PARange (which is the IPA limit) */
 		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
 	kfree(vcpu->arch.vncr_tlb);
 	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

author
#19 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Hi Leo,

Quoted 108 lines · Leonardo Bras

Hi Tian,

Few extra notes/questions below

Quoted 91 lines · Tian Zheng

From: eillon<[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
  arch/arm64/include/asm/esr.h      |   5 ++
  arch/arm64/include/asm/kvm_host.h |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h  |   1 +
  arch/arm64/include/asm/sysreg.h   |  11 ++++
  arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
  arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
  arch/arm64/kvm/reset.c            |   3 +
  8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
  #ifndef __ASSEMBLER__
  #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
  static inline unsigned long esr_brk_comment(unsigned long esr)
  {
  	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +8 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
  #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
  #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  				     KVM_DIRTY_LOG_INITIALLY_SET)
 
  #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
  /*
   * Mode of operation configurable with kvm-arm.mode early param.
   * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
  u32 __attribute_const__ kvm_target_cpu(void);
  void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
  struct kvm_hyp_memcache {
  	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
  	 * the associated pKVM instance in the hypervisor.
  	 */
  	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
  };
 
  struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 832

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
  	bool		reset;
  };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+

IIUC this is used once both on enable/disable and massively on vcpu_put/get.

Quoted 6 lines

What if we actually save just HDBSSBR_EL2 and HDBSSPROD_EL2 instead? That way we avoid having masking operations in put/get as well as any possible error we may have formatting those.

The cost is doing those operations once for enable and once for disable, which should be fine.

Thanks for the suggestion. I actually started with storing the raw system register

values, as you proposed.

However, after discussing it with Oliver Upton in v1, we felt that keeping the base address,

size, and index as separate fields makes the state easier to understand.

Discussion link:https://lore.kernel.org/linux-arm-kernel/[email protected]/ <https://lore.kernel.org/linux-arm-kernel/[email protected]/>

That's why I ended up changing the storage approach in the end.

Quoted 178 lines
Quoted 162 lines
arch/arm64/include/asm/kvm_host.h +3 -0
  struct vncr_tlb;
 
  struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
  	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
  	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
  };
 
  /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
  int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
  int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
  phys_addr_t kvm_mmu_get_httbr(void);
  phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
  #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
  					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
  /*
   * Definitions for GICv5 instructions
   */
arch/arm64/kvm/arm.c +89 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
  int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  			    struct kvm_enable_cap *cap)
  {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		r = 0;
  		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
  		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
  	default:
  		break;
  	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  			r = kvm_supports_cacheable_pfnmap();
  		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
  	default:
  		r = 0;
  	}

arch/arm64/kvm/arm.c static int check_vcpu_requests(struct kvm_vcpu *vcpu) · line 1213

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
  		if (kvm_dirty_ring_check_request(vcpu))
  			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);

I am curious on why we need a flush-hdbss request,
Don't we have the flush function happening every time we run vcpu_put?

Oh, I see, you want to check if there is anything needed inside the inner loop of vcpu_run, without having to vcpu_put. I think it makes sense.

But instead of having this on guest entry, does not it make more sense to have it in guest exit? This way we flush every time (if needed) we exit the guest, and instead of having a vcpu request, we just require a vcpu kick and it should flush if needed.

Quoted 4 lines

Maybe have vcpu_put just save the registers, and add a the flush before handle_exit.

What do you think?

Thank you for the feedback.

Indeed, in the initial version (v1), I placed the flush operation inside handle_exit and

used a vcpu_kick in kvm_arch_sync_dirty_log to trigger the flush of the HDBSS buffer.

However, during the review, Marc pointed out that calling this function on every exit

event is too frequent if it's not always needed.

Discussion link: _https://lore.kernel.org/linux-arm-kernel/[email protected]/_

I agreed with his assessment. Therefore, in the current version, I've separated the flush

operation into more specific and less frequent points:

1. In vcpu_put

2. During dirty log synchronization, by kicking the vCPU to trigger a request that flushes

on its next exit.

3. When handling a specific HDBSSF event.

This ensures the flush happens only when necessary, avoiding the overhead of doing it

on every guest exit.

Quoted 116 lines
Quoted 103 lines
arch/arm64/kvm/arm.c +11 -0
+
  		check_nested_vcpu_requests(vcpu);
  	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
  void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
  }
 
  static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
  	local_irq_restore(flags);
  }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
  void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  {
  	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  	__vcpu_load_switch_sysregs(vcpu);
  	__vcpu_load_activate_traps(vcpu);
  	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
  }
 
  void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
  {
+	kvm_flush_hdbss_buffer(vcpu);
  	__vcpu_put_deactivate_traps(vcpu);
  	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (writable)
  		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
  	if (exec_fault)
  		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
  	return 0;
  }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

This will mark a page dirty for both dirty_bitmap or dirty_ring, depending on what is in use.

Quoted 2 lines

Out of plain curiosity, have you planned / tested for the dirty-ring as well, or just for dirty-bitmap?

Currently, I have only tested this with dirty-bitmap mode.

I will test and ensure the HDBSS feature works correctly with dirty-ring in the next version.

Quoted 23 lines
Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2090

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;

Humm, flushing in a fault handler means hanging there, in IRQ context, for a while.

Quoted 6 lines

Since we already deal with this on guest_exit (vcpu_put IIUC), why not just return in a way the vcpu has to exit the inner loop and let it flush there instead?

Thanks!
Leo

Thanks for the feedback.

If we flush on every guest exit (by moving the flush before handle_exit, then we can

indeed drop the flush from the fault handler and from vcpu_put.

However, given Marc's earlier concern about not imposing this overhead on all vCPUs,

I'd rather avoid flushing on every exit.

My current plan is to set a request bit in kvm_handle_hdbss_fault (via kvm_make_request),

and move the actual flush to the normal exit path, where it can execute in a safe context.

This also allows us to remove the flush from the fault handler entirely.

Does that approach sound reasonable to you?

Quoted 38 lines
Quoted 38 lines
arch/arm64/kvm/mmu.c +15 -0
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
  /**
   * kvm_handle_guest_abort - handles all 2nd stage aborts
   * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
  	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
  	if (esr_fsc_is_translation_fault(esr)) {
  		/* Beyond sanitised PARange (which is the IPA limit) */
  		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -0
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
  	kfree(vcpu->arch.vncr_tlb);
  	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
  }
 
Quoted 3 lines
Quoted 3 lines
arch/arm64/kvm/reset.c +0 -1
  static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

Thanks!

Tian

#20 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 117 lines · Tian Zheng

Hi Leo,

Quoted 114 lines · Leonardo Bras

Hi Tian,

Few extra notes/questions below

Quoted 91 lines · Tian Zheng

From: eillon<[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
  arch/arm64/include/asm/esr.h      |   5 ++
  arch/arm64/include/asm/kvm_host.h |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h  |   1 +
  arch/arm64/include/asm/sysreg.h   |  11 ++++
  arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
  arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
  arch/arm64/kvm/reset.c            |   3 +
  8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
  #ifndef __ASSEMBLER__
  #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
  static inline unsigned long esr_brk_comment(unsigned long esr)
  {
  	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +8 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
  #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
  #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  				     KVM_DIRTY_LOG_INITIALLY_SET)
 
  #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
  /*
   * Mode of operation configurable with kvm-arm.mode early param.
   * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
  u32 __attribute_const__ kvm_target_cpu(void);
  void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
  struct kvm_hyp_memcache {
  	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
  	 * the associated pKVM instance in the hypervisor.
  	 */
  	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
  };
 
  struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 832

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
  	bool		reset;
  };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+

IIUC this is used once both on enable/disable and massively on vcpu_put/get.

What if we actually save just HDBSSBR_EL2 and HDBSSPROD_EL2 instead? That way we avoid having masking operations in put/get as well as any possible error we may have formatting those.

The cost is doing those operations once for enable and once for disable, which should be fine.

Hi Tian,

Quoted 14 lines

Thanks for the suggestion. I actually started with storing the raw system register

values, as you proposed.

However, after discussing it with Oliver Upton in v1, we felt that keeping the base address,

size, and index as separate fields makes the state easier to understand.

Quoted 8 lines

Discussion

<https://lore.kernel.org/linux-arm-kernel/[email protected]/>

That's why I ended up changing the storage approach in the end.

Humm, FWIW I disagree with the above argument.
I would argue that vcpu_put should save the registers, and not actually know what they are about or how are they formatted at this point.

The responsibility of understanding it's fields and usage value should be in the code that actually uses it.

IIUC on kvm_vcpu_put_vhe and kvm_vcpu_load_vhe there are calls to other functions than only save the register as it is.

Quoted 218 lines
Quoted 178 lines
Quoted 162 lines
arch/arm64/include/asm/kvm_host.h +3 -0
  struct vncr_tlb;
 
  struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
  	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
  	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
  };
 
  /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
  int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
  int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
  phys_addr_t kvm_mmu_get_httbr(void);
  phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
  #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
  					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
  /*
   * Definitions for GICv5 instructions
   */
arch/arm64/kvm/arm.c +89 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
  int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  			    struct kvm_enable_cap *cap)
  {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		r = 0;
  		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
  		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
  	default:
  		break;
  	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  			r = kvm_supports_cacheable_pfnmap();
  		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
  	default:
  		r = 0;
  	}

arch/arm64/kvm/arm.c static int check_vcpu_requests(struct kvm_vcpu *vcpu) · line 1213

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
  		if (kvm_dirty_ring_check_request(vcpu))
  			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);

I am curious on why we need a flush-hdbss request,
Don't we have the flush function happening every time we run vcpu_put?

Oh, I see, you want to check if there is anything needed inside the inner loop of vcpu_run, without having to vcpu_put. I think it makes sense.

But instead of having this on guest entry, does not it make more sense to have it in guest exit? This way we flush every time (if needed) we exit the guest, and instead of having a vcpu request, we just require a vcpu kick and it should flush if needed.

Quoted 4 lines

Maybe have vcpu_put just save the registers, and add a the flush before handle_exit.

What do you think?

Thank you for the feedback.

Indeed, in the initial version (v1), I placed the flush operation inside handle_exit and

used a vcpu_kick in kvm_arch_sync_dirty_log to trigger the flush of the HDBSS buffer.

However, during the review, Marc pointed out that calling this function on every exit

event is too frequent if it's not always needed.

Discussion link:
_https://lore.kernel.org/linux-arm-kernel/[email protected]/_

I agreed with his assessment. Therefore, in the current version, I've separated the flush

operation into more specific and less frequent points:

1. In vcpu_put

2. During dirty log synchronization, by kicking the vCPU to trigger a request that flushes

on its next exit.

Quoted 8 lines

3. When handling a specific HDBSSF event.

This ensures the flush happens only when necessary, avoiding the overhead of doing it

on every guest exit.

Fair enough, calling it every time you go in the inner loop may be too much, even with a check to make sure it needs to run.

Having it as a request means you may do that sometimes without leaving the inner loop. That could be useful if you want to use it with the IRQ handler to deal with full buffer, or any error, as well as dealing with a regular request in the 2nd case.

While I agree it's needed to run before leaving guest context (i.e leaving the inner loop), I am not really sure vcpu_put is the best place to put the flushing. I may be wrong, but for me it looks more like of a place to save registers and context, as well as dropping refcounts or something like that. I would not expect a flush happening in vcpu_put, if I was reading the code.

Would it be too bad if we had it into a call before vcpu_put, at kvm_arch_vcpu_ioctl_run()?

Quoted 120 lines
Quoted 116 lines
Quoted 103 lines
arch/arm64/kvm/arm.c +11 -0
+
  		check_nested_vcpu_requests(vcpu);
  	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
  void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
  }
 
  static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
  	local_irq_restore(flags);
  }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
  void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  {
  	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  	__vcpu_load_switch_sysregs(vcpu);
  	__vcpu_load_activate_traps(vcpu);
  	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
  }
 
  void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
  {
+	kvm_flush_hdbss_buffer(vcpu);
  	__vcpu_put_deactivate_traps(vcpu);
  	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (writable)
  		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
  	if (exec_fault)
  		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
  	return 0;
  }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

This will mark a page dirty for both dirty_bitmap or dirty_ring, depending on what is in use.

Quoted 2 lines

Out of plain curiosity, have you planned / tested for the dirty-ring as well, or just for dirty-bitmap?

Quoted 7 lines

Currently, I have only tested this with dirty-bitmap mode.

I will test and ensure the HDBSS feature works correctly with dirty-ring in the next version.

Thanks!

Quoted 49 lines
Quoted 23 lines
Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2090

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;

Humm, flushing in a fault handler means hanging there, in IRQ context, for a while.

Quoted 6 lines

Since we already deal with this on guest_exit (vcpu_put IIUC), why not just return in a way the vcpu has to exit the inner loop and let it flush there instead?

Thanks!
Leo

Thanks for the feedback.

If we flush on every guest exit (by moving the flush before handle_exit, then we can

indeed drop the flush from the fault handler and from vcpu_put.

However, given Marc's earlier concern about not imposing this overhead on all vCPUs,

I'd rather avoid flushing on every exit.

My current plan is to set a request bit in kvm_handle_hdbss_fault (via kvm_make_request),

Quoted 9 lines

and move the actual flush to the normal exit path, where it can execute in a safe context.

This also allows us to remove the flush from the fault handler entirely.

Does that approach sound reasonable to you?

Yes, I think it looks much better, as the fault will cause guest to exit, and it can run the flush on it's way back in.

Thanks!
Leo

Quoted 46 lines
Quoted 38 lines
Quoted 38 lines
arch/arm64/kvm/mmu.c +15 -0
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
  /**
   * kvm_handle_guest_abort - handles all 2nd stage aborts
   * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
  	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
  	if (esr_fsc_is_translation_fault(esr)) {
  		/* Beyond sanitised PARange (which is the IPA limit) */
  		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -0
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
  	kfree(vcpu->arch.vncr_tlb);
  	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
  }
 
Quoted 3 lines
Quoted 3 lines
arch/arm64/kvm/reset.c +0 -1
  static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

Thanks!

Tian

author
#21 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 145 lines · Leonardo Bras
Quoted 117 lines · Tian Zheng

Hi Leo,

Quoted 114 lines · Leonardo Bras

Hi Tian,

Few extra notes/questions below

Quoted 91 lines · Tian Zheng

From: eillon<[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
   arch/arm64/include/asm/esr.h      |   5 ++
   arch/arm64/include/asm/kvm_host.h |  17 +++++
   arch/arm64/include/asm/kvm_mmu.h  |   1 +
   arch/arm64/include/asm/sysreg.h   |  11 ++++
   arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
   arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
   arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
   arch/arm64/kvm/reset.c            |   3 +
   8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
   #ifndef __ASSEMBLER__
   #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
   static inline unsigned long esr_brk_comment(unsigned long esr)
   {
   	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +8 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
   #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
   #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
   				     KVM_DIRTY_LOG_INITIALLY_SET)
 
   #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
   /*
    * Mode of operation configurable with kvm-arm.mode early param.
    * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
   u32 __attribute_const__ kvm_target_cpu(void);
   void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
   void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
   struct kvm_hyp_memcache {
   	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
   	 * the associated pKVM instance in the hypervisor.
   	 */
   	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
   };
 
   struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 832

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
   	bool		reset;
   };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+

IIUC this is used once both on enable/disable and massively on vcpu_put/get.

What if we actually save just HDBSSBR_EL2 and HDBSSPROD_EL2 instead? That way we avoid having masking operations in put/get as well as any possible error we may have formatting those.

The cost is doing those operations once for enable and once for disable, which should be fine.

Hi Tian,

Quoted 13 lines

Thanks for the suggestion. I actually started with storing the raw system register

values, as you proposed.

However, after discussing it with Oliver Upton in v1, we felt that keeping the base address,

size, and index as separate fields makes the state easier to understand.

Quoted 8 lines

Discussion

<https://lore.kernel.org/linux-arm-kernel/[email protected]/>

That's why I ended up changing the storage approach in the end.

Humm, FWIW I disagree with the above argument.
I would argue that vcpu_put should save the registers, and not actually know what they are about or how are they formatted at this point.

Quoted 5 lines

The responsibility of understanding it's fields and usage value should be in the code that actually uses it.

IIUC on kvm_vcpu_put_vhe and kvm_vcpu_load_vhe there are calls to other functions than only save the register as it is.

ok, thx! I'll update the struct to store only the raw register values.

Quoted 240 lines
Quoted 217 lines
Quoted 178 lines
Quoted 162 lines
arch/arm64/include/asm/kvm_host.h +3 -0
   struct vncr_tlb;
 
   struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
   	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
   	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
   };
 
   /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
   int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
   int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
   phys_addr_t kvm_mmu_get_httbr(void);
   phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
   #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
   					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
   /*
    * Definitions for GICv5 instructions
    */
arch/arm64/kvm/arm.c +89 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
   	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
   }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
   int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   			    struct kvm_enable_cap *cap)
   {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   		r = 0;
   		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
   		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
   	default:
   		break;
   	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
   			r = kvm_supports_cacheable_pfnmap();
   		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
   	default:
   		r = 0;
   	}

arch/arm64/kvm/arm.c static int check_vcpu_requests(struct kvm_vcpu *vcpu) · line 1213

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
   		if (kvm_dirty_ring_check_request(vcpu))
   			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);

I am curious on why we need a flush-hdbss request,
Don't we have the flush function happening every time we run vcpu_put?

Oh, I see, you want to check if there is anything needed inside the inner loop of vcpu_run, without having to vcpu_put. I think it makes sense.

But instead of having this on guest entry, does not it make more sense to have it in guest exit? This way we flush every time (if needed) we exit the guest, and instead of having a vcpu request, we just require a vcpu kick and it should flush if needed.

Quoted 4 lines

Maybe have vcpu_put just save the registers, and add a the flush before handle_exit.

What do you think?

Thank you for the feedback.

Indeed, in the initial version (v1), I placed the flush operation inside handle_exit and

used a vcpu_kick in kvm_arch_sync_dirty_log to trigger the flush of the HDBSS buffer.

However, during the review, Marc pointed out that calling this function on every exit

event is too frequent if it's not always needed.

Discussion link:
_https://lore.kernel.org/linux-arm-kernel/[email protected]/_

I agreed with his assessment. Therefore, in the current version, I've separated the flush

operation into more specific and less frequent points:

1. In vcpu_put

2. During dirty log synchronization, by kicking the vCPU to trigger a request that flushes

on its next exit.

Quoted 8 lines

3. When handling a specific HDBSSF event.

This ensures the flush happens only when necessary, avoiding the overhead of doing it

on every guest exit.

Fair enough, calling it every time you go in the inner loop may be too much, even with a check to make sure it needs to run.

Having it as a request means you may do that sometimes without leaving the inner loop. That could be useful if you want to use it with the IRQ handler to deal with full buffer, or any error, as well as dealing with a regular request in the 2nd case.

While I agree it's needed to run before leaving guest context (i.e leaving the inner loop), I am not really sure vcpu_put is the best place to put the flushing. I may be wrong, but for me it looks more like of a place to save registers and context, as well as dropping refcounts or something like that. I would not expect a flush happening in vcpu_put, if I was reading the code.

Quoted 3 lines

Would it be too bad if we had it into a call before vcpu_put, at kvm_arch_vcpu_ioctl_run()?

Thanks for the clarification. After looking again at the code paths, I agree that

kvm_vcpu_put_vhe() and kvm_arch_vcpu_put() are really meant to be pure save/restore

paths, so embedding HDBSS flushing there isn't ideal.

My remaining concern is that kvm_arch_vcpu_ioctl_run() doesn't cover the case where

the vCPU is scheduled out. In that case we still leave guest context, but we don't return

through the run loop, so a flush placed only in ioctl_run() wouldn't run.

Any suggestions on where this should hook in? Would introducing a small arch‑specific

"guest exit" helper, invoked fromm kvm_arch_vcpu_put(), be acceptable?

Thanks!

Tian

Quoted 237 lines
Quoted 117 lines
Quoted 117 lines
Quoted 104 lines
arch/arm64/kvm/arm.c +11 -0
+
   		check_nested_vcpu_requests(vcpu);
   	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
   void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
   {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
   }
 
   static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
   	local_irq_restore(flags);
   }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
   void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   {
   	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   	__vcpu_load_switch_sysregs(vcpu);
   	__vcpu_load_activate_traps(vcpu);
   	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
   }
 
   void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
   {
+	kvm_flush_hdbss_buffer(vcpu);
   	__vcpu_put_deactivate_traps(vcpu);
   	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
   	if (writable)
   		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
   	if (exec_fault)
   		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
   	return 0;
   }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

This will mark a page dirty for both dirty_bitmap or dirty_ring, depending on what is in use.

Quoted 10 lines
Quoted 2 lines

Out of plain curiosity, have you planned / tested for the dirty-ring as well, or just for dirty-bitmap?

Currently, I have only tested this with dirty-bitmap mode.

I will test and ensure the HDBSS feature works correctly with dirty-ring in the next version.

Thanks!

Quoted 48 lines
Quoted 23 lines
Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2090

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;

Humm, flushing in a fault handler means hanging there, in IRQ context, for a while.

Quoted 6 lines

Since we already deal with this on guest_exit (vcpu_put IIUC), why not just return in a way the vcpu has to exit the inner loop and let it flush there instead?

Thanks!
Leo

Thanks for the feedback.

If we flush on every guest exit (by moving the flush before handle_exit, then we can

indeed drop the flush from the fault handler and from vcpu_put.

However, given Marc's earlier concern about not imposing this overhead on all vCPUs,

I'd rather avoid flushing on every exit.

My current plan is to set a request bit in kvm_handle_hdbss_fault (via kvm_make_request),

Quoted 9 lines

and move the actual flush to the normal exit path, where it can execute in a safe context.

This also allows us to remove the flush from the fault handler entirely.

Does that approach sound reasonable to you?

Yes, I think it looks much better, as the fault will cause guest to exit, and it can run the flush on it's way back in.

Thanks!
Leo

Quoted 45 lines
Quoted 38 lines
Quoted 38 lines
arch/arm64/kvm/mmu.c +15 -0
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
   /**
    * kvm_handle_guest_abort - handles all 2nd stage aborts
    * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
   	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
   	if (esr_fsc_is_translation_fault(esr)) {
   		/* Beyond sanitised PARange (which is the IPA limit) */
   		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -0
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
   	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
   	kfree(vcpu->arch.vncr_tlb);
   	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
   }
 
Quoted 3 lines
Quoted 3 lines
arch/arm64/kvm/reset.c +0 -1
   static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

Thanks!

Tian

#22 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 147 lines · Tian Zheng
Quoted 145 lines · Leonardo Bras
Quoted 117 lines · Tian Zheng

Hi Leo,

Quoted 114 lines · Leonardo Bras

Hi Tian,

Few extra notes/questions below

Quoted 91 lines · Tian Zheng

From: eillon<[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
   arch/arm64/include/asm/esr.h      |   5 ++
   arch/arm64/include/asm/kvm_host.h |  17 +++++
   arch/arm64/include/asm/kvm_mmu.h  |   1 +
   arch/arm64/include/asm/sysreg.h   |  11 ++++
   arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
   arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
   arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
   arch/arm64/kvm/reset.c            |   3 +
   8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
   #ifndef __ASSEMBLER__
   #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
   static inline unsigned long esr_brk_comment(unsigned long esr)
   {
   	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +8 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
   #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
   #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
   				     KVM_DIRTY_LOG_INITIALLY_SET)
 
   #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
   /*
    * Mode of operation configurable with kvm-arm.mode early param.
    * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
   u32 __attribute_const__ kvm_target_cpu(void);
   void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
   void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
   struct kvm_hyp_memcache {
   	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
   	 * the associated pKVM instance in the hypervisor.
   	 */
   	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
   };
 
   struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 832

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
   	bool		reset;
   };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+

IIUC this is used once both on enable/disable and massively on vcpu_put/get.

What if we actually save just HDBSSBR_EL2 and HDBSSPROD_EL2 instead? That way we avoid having masking operations in put/get as well as any possible error we may have formatting those.

The cost is doing those operations once for enable and once for disable, which should be fine.

Hi Tian,

Quoted 13 lines

Thanks for the suggestion. I actually started with storing the raw system register

values, as you proposed.

However, after discussing it with Oliver Upton in v1, we felt that keeping the base address,

size, and index as separate fields makes the state easier to understand.

Quoted 8 lines

Discussion

<https://lore.kernel.org/linux-arm-kernel/[email protected]/>

That's why I ended up changing the storage approach in the end.

Humm, FWIW I disagree with the above argument.
I would argue that vcpu_put should save the registers, and not actually know what they are about or how are they formatted at this point.

Quoted 8 lines
Quoted 5 lines

The responsibility of understanding it's fields and usage value should be in the code that actually uses it.

IIUC on kvm_vcpu_put_vhe and kvm_vcpu_load_vhe there are calls to other functions than only save the register as it is.

ok, thx! I'll update the struct to store only the raw register values.

Awesome :)

Quoted 258 lines
Quoted 242 lines
Quoted 219 lines
Quoted 180 lines
Quoted 164 lines
arch/arm64/include/asm/kvm_host.h +3 -0
   struct vncr_tlb;
 
   struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
   	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
   	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
   };
 
   /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
   int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
   int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
   phys_addr_t kvm_mmu_get_httbr(void);
   phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
   #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
   					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
   /*
    * Definitions for GICv5 instructions
    */
arch/arm64/kvm/arm.c +89 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
   	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
   }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
   int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   			    struct kvm_enable_cap *cap)
   {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   		r = 0;
   		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
   		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
   	default:
   		break;
   	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
   			r = kvm_supports_cacheable_pfnmap();
   		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
   	default:
   		r = 0;
   	}

arch/arm64/kvm/arm.c static int check_vcpu_requests(struct kvm_vcpu *vcpu) · line 1213

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
   		if (kvm_dirty_ring_check_request(vcpu))
   			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);

I am curious on why we need a flush-hdbss request,
Don't we have the flush function happening every time we run vcpu_put?

Oh, I see, you want to check if there is anything needed inside the inner loop of vcpu_run, without having to vcpu_put. I think it makes sense.

But instead of having this on guest entry, does not it make more sense to have it in guest exit? This way we flush every time (if needed) we exit the guest, and instead of having a vcpu request, we just require a vcpu kick and it should flush if needed.

Quoted 4 lines

Maybe have vcpu_put just save the registers, and add a the flush before handle_exit.

What do you think?

Thank you for the feedback.

Indeed, in the initial version (v1), I placed the flush operation inside handle_exit and

used a vcpu_kick in kvm_arch_sync_dirty_log to trigger the flush of the HDBSS buffer.

However, during the review, Marc pointed out that calling this function on every exit

event is too frequent if it's not always needed.

Discussion link:
_https://lore.kernel.org/linux-arm-kernel/[email protected]/_

I agreed with his assessment. Therefore, in the current version, I've separated the flush

operation into more specific and less frequent points:

1. In vcpu_put

2. During dirty log synchronization, by kicking the vCPU to trigger a request that flushes

on its next exit.

Quoted 8 lines

3. When handling a specific HDBSSF event.

This ensures the flush happens only when necessary, avoiding the overhead of doing it

on every guest exit.

Fair enough, calling it every time you go in the inner loop may be too much, even with a check to make sure it needs to run.

Having it as a request means you may do that sometimes without leaving the inner loop. That could be useful if you want to use it with the IRQ handler to deal with full buffer, or any error, as well as dealing with a regular request in the 2nd case.

While I agree it's needed to run before leaving guest context (i.e leaving the inner loop), I am not really sure vcpu_put is the best place to put the flushing. I may be wrong, but for me it looks more like of a place to save registers and context, as well as dropping refcounts or something like that. I would not expect a flush happening in vcpu_put, if I was reading the code.

Quoted 3 lines

Would it be too bad if we had it into a call before vcpu_put, at kvm_arch_vcpu_ioctl_run()?

Thanks for the clarification. After looking again at the code paths, I agree that

kvm_vcpu_put_vhe() and kvm_arch_vcpu_put() are really meant to be pure save/restore

paths, so embedding HDBSS flushing there isn't ideal.

My remaining concern is that kvm_arch_vcpu_ioctl_run() doesn't cover the case where

Quoted 7 lines

the vCPU is scheduled out. In that case we still leave guest context, but we don't return

through the run loop, so a flush placed only in ioctl_run() wouldn't run.

Any suggestions on where this should hook in?

You mention that on vcpu_put it works, right? maybe it's worth to track down which vcpu_put users would make sense to flush before it's calling.

I found that vcpu_put is called only in the vcpu_run, but it's arch-specific version is called in:

kvm_debug_handle_oslar : put and load in sequence, flush shouldnt be needed kvm_emulate_nested_eret : same as above, not sure if nested will be supported kvm_inject_nested : same as above
kvm_reset_vcpu : put and load in sequence, not needed [1] kvm_sched_out : that's ran on sched-out, that makes sense for us vcpu_put : called only by vcpu_run, where we already planned to use

Which brings us other benefit of not having that in vcpu_put: the flush could be happening on functions that should originally ran fast, and having it outside vcpu_put allows us to decide if we need it.

So, having the flush happening in kvm_arch_vcpu_ioctl_run() and kvm_sched_out() should be enough, on top of the per-request you mentioned before.

[1]: the vcpu_reset: not sure how often this does happen, and if it would be interesting flushing here as well. It seems to be called on init, so not the case where would be something to flush, and from a case where it's restoring the registers. So I think it should be safe to not flush here, so it should be a question of 'maybe being interesting', which I am not sure.

Quoted 5 lines

Would introducing a small
arch‑specific

"guest exit" helper, invoked fromm kvm_arch_vcpu_put(), be acceptable?

IIUC that would be the same as the previous one: we would have the flush happening inside a function that is supposed to save registers.

Thanks!
Leo

Quoted 243 lines

Thanks!

Tian

Quoted 237 lines
Quoted 117 lines
Quoted 117 lines
Quoted 104 lines
arch/arm64/kvm/arm.c +11 -0
+
   		check_nested_vcpu_requests(vcpu);
   	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
   void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
   {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
   }
 
   static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
   	local_irq_restore(flags);
   }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
   void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   {
   	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   	__vcpu_load_switch_sysregs(vcpu);
   	__vcpu_load_activate_traps(vcpu);
   	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
   }
 
   void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
   {
+	kvm_flush_hdbss_buffer(vcpu);
   	__vcpu_put_deactivate_traps(vcpu);
   	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
   	if (writable)
   		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
   	if (exec_fault)
   		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
   	return 0;
   }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

This will mark a page dirty for both dirty_bitmap or dirty_ring, depending on what is in use.

Quoted 10 lines
Quoted 2 lines

Out of plain curiosity, have you planned / tested for the dirty-ring as well, or just for dirty-bitmap?

Currently, I have only tested this with dirty-bitmap mode.

I will test and ensure the HDBSS feature works correctly with dirty-ring in the next version.

Thanks!

Quoted 48 lines
Quoted 23 lines
Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2090

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;

Humm, flushing in a fault handler means hanging there, in IRQ context, for a while.

Quoted 6 lines

Since we already deal with this on guest_exit (vcpu_put IIUC), why not just return in a way the vcpu has to exit the inner loop and let it flush there instead?

Thanks!
Leo

Thanks for the feedback.

If we flush on every guest exit (by moving the flush before handle_exit, then we can

indeed drop the flush from the fault handler and from vcpu_put.

However, given Marc's earlier concern about not imposing this overhead on all vCPUs,

I'd rather avoid flushing on every exit.

My current plan is to set a request bit in kvm_handle_hdbss_fault (via kvm_make_request),

Quoted 9 lines

and move the actual flush to the normal exit path, where it can execute in a safe context.

This also allows us to remove the flush from the fault handler entirely.

Does that approach sound reasonable to you?

Yes, I think it looks much better, as the fault will cause guest to exit, and it can run the flush on it's way back in.

Thanks!
Leo

Quoted 45 lines
Quoted 38 lines
Quoted 38 lines
arch/arm64/kvm/mmu.c +15 -0
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
   /**
    * kvm_handle_guest_abort - handles all 2nd stage aborts
    * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
   	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
   	if (esr_fsc_is_translation_fault(esr)) {
   		/* Beyond sanitised PARange (which is the IPA limit) */
   		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -0
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
   	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
   	kfree(vcpu->arch.vncr_tlb);
   	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
   }
 
Quoted 3 lines
Quoted 3 lines
arch/arm64/kvm/reset.c +0 -1
   static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

Thanks!

Tian

author
#23 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 438 lines · Leonardo Bras
Quoted 145 lines · Tian Zheng
Quoted 144 lines · Leonardo Bras
Quoted 117 lines · Tian Zheng

Hi Leo,

Quoted 114 lines · Leonardo Bras

Hi Tian,

Few extra notes/questions below

Quoted 91 lines · Tian Zheng

From: eillon<[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
    arch/arm64/include/asm/esr.h      |   5 ++
    arch/arm64/include/asm/kvm_host.h |  17 +++++
    arch/arm64/include/asm/kvm_mmu.h  |   1 +
    arch/arm64/include/asm/sysreg.h   |  11 ++++
    arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
    arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
    arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
    arch/arm64/kvm/reset.c            |   3 +
    8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
    #ifndef __ASSEMBLER__
    #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
    static inline unsigned long esr_brk_comment(unsigned long esr)
    {
    	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +8 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
    #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
    #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
    				     KVM_DIRTY_LOG_INITIALLY_SET)
 
    #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
    /*
     * Mode of operation configurable with kvm-arm.mode early param.
     * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
    u32 __attribute_const__ kvm_target_cpu(void);
    void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
    void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
    struct kvm_hyp_memcache {
    	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
    	 * the associated pKVM instance in the hypervisor.
    	 */
    	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
    };
 
    struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 832

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
    	bool		reset;
    };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+

IIUC this is used once both on enable/disable and massively on vcpu_put/get.

What if we actually save just HDBSSBR_EL2 and HDBSSPROD_EL2 instead? That way we avoid having masking operations in put/get as well as any possible error we may have formatting those.

The cost is doing those operations once for enable and once for disable, which should be fine.

Hi Tian,

Quoted 12 lines

Thanks for the suggestion. I actually started with storing the raw system register

values, as you proposed.

However, after discussing it with Oliver Upton in v1, we felt that keeping the base address,

size, and index as separate fields makes the state easier to understand.

Quoted 8 lines

Discussion

<https://lore.kernel.org/linux-arm-kernel/[email protected]/>

That's why I ended up changing the storage approach in the end.

Humm, FWIW I disagree with the above argument.
I would argue that vcpu_put should save the registers, and not actually know what they are about or how are they formatted at this point.

Quoted 7 lines
Quoted 5 lines

The responsibility of understanding it's fields and usage value should be in the code that actually uses it.

IIUC on kvm_vcpu_put_vhe and kvm_vcpu_load_vhe there are calls to other functions than only save the register as it is.

ok, thx! I'll update the struct to store only the raw register values.

Awesome :)

Quoted 255 lines
Quoted 240 lines
Quoted 217 lines
Quoted 179 lines
Quoted 163 lines
arch/arm64/include/asm/kvm_host.h +3 -0
    struct vncr_tlb;
 
    struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
    	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
    	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
    };
 
    /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
    int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
    int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
    phys_addr_t kvm_mmu_get_httbr(void);
    phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
    #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
    					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
    /*
     * Definitions for GICv5 instructions
     */
arch/arm64/kvm/arm.c +89 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
    	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
    }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
    int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
    			    struct kvm_enable_cap *cap)
    {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
    		r = 0;
    		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
    		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
    	default:
    		break;
    	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
    			r = kvm_supports_cacheable_pfnmap();
    		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
    	default:
    		r = 0;
    	}

arch/arm64/kvm/arm.c static int check_vcpu_requests(struct kvm_vcpu *vcpu) · line 1213

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
    		if (kvm_dirty_ring_check_request(vcpu))
    			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);

I am curious on why we need a flush-hdbss request,
Don't we have the flush function happening every time we run vcpu_put?

Oh, I see, you want to check if there is anything needed inside the inner loop of vcpu_run, without having to vcpu_put. I think it makes sense.

But instead of having this on guest entry, does not it make more sense to have it in guest exit? This way we flush every time (if needed) we exit the guest, and instead of having a vcpu request, we just require a vcpu kick and it should flush if needed.

Quoted 4 lines

Maybe have vcpu_put just save the registers, and add a the flush before handle_exit.

What do you think?

Thank you for the feedback.

Indeed, in the initial version (v1), I placed the flush operation inside handle_exit and

used a vcpu_kick in kvm_arch_sync_dirty_log to trigger the flush of the HDBSS buffer.

However, during the review, Marc pointed out that calling this function on every exit

event is too frequent if it's not always needed.

Discussion link:
_https://lore.kernel.org/linux-arm-kernel/[email protected]/_

I agreed with his assessment. Therefore, in the current version, I've separated the flush

operation into more specific and less frequent points:

1. In vcpu_put

2. During dirty log synchronization, by kicking the vCPU to trigger a request that flushes

on its next exit.

Quoted 8 lines

3. When handling a specific HDBSSF event.

This ensures the flush happens only when necessary, avoiding the overhead of doing it

on every guest exit.

Fair enough, calling it every time you go in the inner loop may be too much, even with a check to make sure it needs to run.

Having it as a request means you may do that sometimes without leaving the inner loop. That could be useful if you want to use it with the IRQ handler to deal with full buffer, or any error, as well as dealing with a regular request in the 2nd case.

While I agree it's needed to run before leaving guest context (i.e leaving the inner loop), I am not really sure vcpu_put is the best place to put the flushing. I may be wrong, but for me it looks more like of a place to save registers and context, as well as dropping refcounts or something like that. I would not expect a flush happening in vcpu_put, if I was reading the code.

Quoted 3 lines

Would it be too bad if we had it into a call before vcpu_put, at kvm_arch_vcpu_ioctl_run()?

Thanks for the clarification. After looking again at the code paths, I agree that

kvm_vcpu_put_vhe() and kvm_arch_vcpu_put() are really meant to be pure save/restore

paths, so embedding HDBSS flushing there isn't ideal.

My remaining concern is that kvm_arch_vcpu_ioctl_run() doesn't cover the case where

Quoted 7 lines

the vCPU is scheduled out. In that case we still leave guest context, but we don't return

through the run loop, so a flush placed only in ioctl_run() wouldn't run.

Any suggestions on where this should hook in?

You mention that on vcpu_put it works, right? maybe it's worth to track down which vcpu_put users would make sense to flush before it's calling.

I found that vcpu_put is called only in the vcpu_run, but it's arch-specific version is called in:

kvm_debug_handle_oslar : put and load in sequence, flush shouldnt be needed kvm_emulate_nested_eret : same as above, not sure if nested will be supported kvm_inject_nested : same as above
kvm_reset_vcpu : put and load in sequence, not needed [1] kvm_sched_out : that's ran on sched-out, that makes sense for us vcpu_put : called only by vcpu_run, where we already planned to use

Which brings us other benefit of not having that in vcpu_put: the flush could be happening on functions that should originally ran fast, and having it outside vcpu_put allows us to decide if we need it.

So, having the flush happening in kvm_arch_vcpu_ioctl_run() and kvm_sched_out() should be enough, on top of the per-request you mentioned before.

Quoted 6 lines

[1]: the vcpu_reset: not sure how often this does happen, and if it would be interesting flushing here as well. It seems to be called on init, so not the case where would be something to flush, and from a case where it's restoring the registers. So I think it should be safe to not flush here, so it should be a question of 'maybe being interesting', which I am not sure.

Got it, makes sense.

HDBSS doesn't apply to nested or other complex cases, so we only need to flush in

kvm_arch_vcpu_ioctl_run() and kvm_sched_out().

I'll update the code accordingly and test to make sure we haven't missed any edge cases.

Thanks!

Tian

Quoted 251 lines
Quoted 5 lines

Would introducing a small
arch‑specific

"guest exit" helper, invoked fromm kvm_arch_vcpu_put(), be acceptable?

IIUC that would be the same as the previous one: we would have the flush happening inside a function that is supposed to save registers.

Thanks!
Leo

Quoted 239 lines

Thanks!

Tian

Quoted 234 lines
Quoted 116 lines
Quoted 116 lines
Quoted 103 lines
arch/arm64/kvm/arm.c +11 -0
+
    		check_nested_vcpu_requests(vcpu);
    	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
    void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
    {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
    }
 
    static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
    	local_irq_restore(flags);
    }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
    void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
    {
    	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
    	__vcpu_load_switch_sysregs(vcpu);
    	__vcpu_load_activate_traps(vcpu);
    	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
    }
 
    void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
    {
+	kvm_flush_hdbss_buffer(vcpu);
    	__vcpu_put_deactivate_traps(vcpu);
    	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
    	if (writable)
    		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
    	if (exec_fault)
    		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
    	return 0;
    }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

This will mark a page dirty for both dirty_bitmap or dirty_ring, depending on what is in use.

Quoted 9 lines
Quoted 2 lines

Out of plain curiosity, have you planned / tested for the dirty-ring as well, or just for dirty-bitmap?

Currently, I have only tested this with dirty-bitmap mode.

I will test and ensure the HDBSS feature works correctly with dirty-ring in the next version.

Thanks!

Quoted 47 lines
Quoted 23 lines
Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2090

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;

Humm, flushing in a fault handler means hanging there, in IRQ context, for a while.

Quoted 6 lines

Since we already deal with this on guest_exit (vcpu_put IIUC), why not just return in a way the vcpu has to exit the inner loop and let it flush there instead?

Thanks!
Leo

Thanks for the feedback.

If we flush on every guest exit (by moving the flush before handle_exit, then we can

indeed drop the flush from the fault handler and from vcpu_put.

However, given Marc's earlier concern about not imposing this overhead on all vCPUs,

I'd rather avoid flushing on every exit.

My current plan is to set a request bit in kvm_handle_hdbss_fault (via kvm_make_request),

Quoted 9 lines

and move the actual flush to the normal exit path, where it can execute in a safe context.

This also allows us to remove the flush from the fault handler entirely.

Does that approach sound reasonable to you?

Yes, I think it looks much better, as the fault will cause guest to exit, and it can run the flush on it's way back in.

Thanks!
Leo

Quoted 45 lines
Quoted 38 lines
Quoted 38 lines
arch/arm64/kvm/mmu.c +15 -0
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
    /**
     * kvm_handle_guest_abort - handles all 2nd stage aborts
     * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
    	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
    	if (esr_fsc_is_translation_fault(esr)) {
    		/* Beyond sanitised PARange (which is the IPA limit) */
    		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -0
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
    	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
    	kfree(vcpu->arch.vncr_tlb);
    	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
    }
 
Quoted 3 lines
Quoted 3 lines
arch/arm64/kvm/reset.c +0 -1
    static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

Thanks!

Tian

#24 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 457 lines · Tian Zheng
Quoted 438 lines · Leonardo Bras
Quoted 145 lines · Tian Zheng
Quoted 144 lines · Leonardo Bras
Quoted 117 lines · Tian Zheng

Hi Leo,

Quoted 114 lines · Leonardo Bras

Hi Tian,

Few extra notes/questions below

Quoted 91 lines · Tian Zheng

From: eillon<[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
    arch/arm64/include/asm/esr.h      |   5 ++
    arch/arm64/include/asm/kvm_host.h |  17 +++++
    arch/arm64/include/asm/kvm_mmu.h  |   1 +
    arch/arm64/include/asm/sysreg.h   |  11 ++++
    arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
    arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
    arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
    arch/arm64/kvm/reset.c            |   3 +
    8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
    #ifndef __ASSEMBLER__
    #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
    static inline unsigned long esr_brk_comment(unsigned long esr)
    {
    	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +8 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
    #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
    #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
    				     KVM_DIRTY_LOG_INITIALLY_SET)
 
    #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
    /*
     * Mode of operation configurable with kvm-arm.mode early param.
     * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
    u32 __attribute_const__ kvm_target_cpu(void);
    void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
    void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
    struct kvm_hyp_memcache {
    	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
    	 * the associated pKVM instance in the hypervisor.
    	 */
    	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
    };
 
    struct kvm_vcpu_fault_info {

arch/arm64/include/asm/kvm_host.h struct vcpu_reset_state { · line 832

Quoted 9 lines
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
    	bool		reset;
    };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+

IIUC this is used once both on enable/disable and massively on vcpu_put/get.

What if we actually save just HDBSSBR_EL2 and HDBSSPROD_EL2 instead? That way we avoid having masking operations in put/get as well as any possible error we may have formatting those.

The cost is doing those operations once for enable and once for disable, which should be fine.

Hi Tian,

Quoted 12 lines

Thanks for the suggestion. I actually started with storing the raw system register

values, as you proposed.

However, after discussing it with Oliver Upton in v1, we felt that keeping the base address,

size, and index as separate fields makes the state easier to understand.

Quoted 8 lines

Discussion

<https://lore.kernel.org/linux-arm-kernel/[email protected]/>

That's why I ended up changing the storage approach in the end.

Humm, FWIW I disagree with the above argument.
I would argue that vcpu_put should save the registers, and not actually know what they are about or how are they formatted at this point.

Quoted 7 lines
Quoted 5 lines

The responsibility of understanding it's fields and usage value should be in the code that actually uses it.

IIUC on kvm_vcpu_put_vhe and kvm_vcpu_load_vhe there are calls to other functions than only save the register as it is.

ok, thx! I'll update the struct to store only the raw register values.

Awesome :)

Quoted 255 lines
Quoted 240 lines
Quoted 217 lines
Quoted 179 lines
Quoted 163 lines
arch/arm64/include/asm/kvm_host.h +3 -0
    struct vncr_tlb;
 
    struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
    	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
    	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
    };
 
    /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
    int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
    int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
    phys_addr_t kvm_mmu_get_httbr(void);
    phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
    #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
    					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
    /*
     * Definitions for GICv5 instructions
     */
arch/arm64/kvm/arm.c +89 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
    	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
    }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
    int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
    			    struct kvm_enable_cap *cap)
    {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
    		r = 0;
    		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
    		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
    	default:
    		break;
    	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
    			r = kvm_supports_cacheable_pfnmap();
    		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
    	default:
    		r = 0;
    	}

arch/arm64/kvm/arm.c static int check_vcpu_requests(struct kvm_vcpu *vcpu) · line 1213

Quoted 5 lines
arch/arm64/kvm/arm.c +2 -0
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
    		if (kvm_dirty_ring_check_request(vcpu))
    			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);

I am curious on why we need a flush-hdbss request,
Don't we have the flush function happening every time we run vcpu_put?

Oh, I see, you want to check if there is anything needed inside the inner loop of vcpu_run, without having to vcpu_put. I think it makes sense.

But instead of having this on guest entry, does not it make more sense to have it in guest exit? This way we flush every time (if needed) we exit the guest, and instead of having a vcpu request, we just require a vcpu kick and it should flush if needed.

Quoted 4 lines

Maybe have vcpu_put just save the registers, and add a the flush before handle_exit.

What do you think?

Thank you for the feedback.

Indeed, in the initial version (v1), I placed the flush operation inside handle_exit and

used a vcpu_kick in kvm_arch_sync_dirty_log to trigger the flush of the HDBSS buffer.

However, during the review, Marc pointed out that calling this function on every exit

event is too frequent if it's not always needed.

Discussion link:
_https://lore.kernel.org/linux-arm-kernel/[email protected]/_

I agreed with his assessment. Therefore, in the current version, I've separated the flush

operation into more specific and less frequent points:

1. In vcpu_put

2. During dirty log synchronization, by kicking the vCPU to trigger a request that flushes

on its next exit.

Quoted 8 lines

3. When handling a specific HDBSSF event.

This ensures the flush happens only when necessary, avoiding the overhead of doing it

on every guest exit.

Fair enough, calling it every time you go in the inner loop may be too much, even with a check to make sure it needs to run.

Having it as a request means you may do that sometimes without leaving the inner loop. That could be useful if you want to use it with the IRQ handler to deal with full buffer, or any error, as well as dealing with a regular request in the 2nd case.

While I agree it's needed to run before leaving guest context (i.e leaving the inner loop), I am not really sure vcpu_put is the best place to put the flushing. I may be wrong, but for me it looks more like of a place to save registers and context, as well as dropping refcounts or something like that. I would not expect a flush happening in vcpu_put, if I was reading the code.

Quoted 3 lines

Would it be too bad if we had it into a call before vcpu_put, at kvm_arch_vcpu_ioctl_run()?

Thanks for the clarification. After looking again at the code paths, I agree that

kvm_vcpu_put_vhe() and kvm_arch_vcpu_put() are really meant to be pure save/restore

paths, so embedding HDBSS flushing there isn't ideal.

My remaining concern is that kvm_arch_vcpu_ioctl_run() doesn't cover the case where

Quoted 7 lines

the vCPU is scheduled out. In that case we still leave guest context, but we don't return

through the run loop, so a flush placed only in ioctl_run() wouldn't run.

Any suggestions on where this should hook in?

You mention that on vcpu_put it works, right? maybe it's worth to track down which vcpu_put users would make sense to flush before it's calling.

I found that vcpu_put is called only in the vcpu_run, but it's arch-specific version is called in:

kvm_debug_handle_oslar : put and load in sequence, flush shouldnt be needed kvm_emulate_nested_eret : same as above, not sure if nested will be supported kvm_inject_nested : same as above
kvm_reset_vcpu : put and load in sequence, not needed [1] kvm_sched_out : that's ran on sched-out, that makes sense for us vcpu_put : called only by vcpu_run, where we already planned to use

Which brings us other benefit of not having that in vcpu_put: the flush could be happening on functions that should originally ran fast, and having it outside vcpu_put allows us to decide if we need it.

So, having the flush happening in kvm_arch_vcpu_ioctl_run() and kvm_sched_out() should be enough, on top of the per-request you mentioned before.

Quoted 6 lines

[1]: the vcpu_reset: not sure how often this does happen, and if it would be interesting flushing here as well. It seems to be called on init, so not the case where would be something to flush, and from a case where it's restoring the registers. So I think it should be safe to not flush here, so it should be a question of 'maybe being interesting', which I am not sure.

Got it, makes sense.

HDBSS doesn't apply to nested or other complex cases, so we only need to flush in

kvm_arch_vcpu_ioctl_run() and kvm_sched_out().

Quoted 8 lines

I'll update the code accordingly and test to make sure we haven't missed any edge cases.

Thanks!

Tian

Awesome! Thanks!
Leo

Quoted 251 lines
Quoted 251 lines
Quoted 5 lines

Would introducing a small
arch‑specific

"guest exit" helper, invoked fromm kvm_arch_vcpu_put(), be acceptable?

IIUC that would be the same as the previous one: we would have the flush happening inside a function that is supposed to save registers.

Thanks!
Leo

Quoted 239 lines

Thanks!

Tian

Quoted 234 lines
Quoted 116 lines
Quoted 116 lines
Quoted 103 lines
arch/arm64/kvm/arm.c +11 -0
+
    		check_nested_vcpu_requests(vcpu);
    	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
    void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
    {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
    }
 
    static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +19 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
    	local_irq_restore(flags);
    }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+
    void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
    {
    	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
    	__vcpu_load_switch_sysregs(vcpu);
    	__vcpu_load_activate_traps(vcpu);
    	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
    }
 
    void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
    {
+	kvm_flush_hdbss_buffer(vcpu);
    	__vcpu_put_deactivate_traps(vcpu);
    	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
    	if (writable)
    		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
    	if (exec_fault)
    		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
    	return 0;
    }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

This will mark a page dirty for both dirty_bitmap or dirty_ring, depending on what is in use.

Quoted 9 lines
Quoted 2 lines

Out of plain curiosity, have you planned / tested for the dirty-ring as well, or just for dirty-bitmap?

Currently, I have only tested this with dirty-bitmap mode.

I will test and ensure the HDBSS feature works correctly with dirty-ring in the next version.

Thanks!

Quoted 47 lines
Quoted 23 lines
Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2090

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;

Humm, flushing in a fault handler means hanging there, in IRQ context, for a while.

Quoted 6 lines

Since we already deal with this on guest_exit (vcpu_put IIUC), why not just return in a way the vcpu has to exit the inner loop and let it flush there instead?

Thanks!
Leo

Thanks for the feedback.

If we flush on every guest exit (by moving the flush before handle_exit, then we can

indeed drop the flush from the fault handler and from vcpu_put.

However, given Marc's earlier concern about not imposing this overhead on all vCPUs,

I'd rather avoid flushing on every exit.

My current plan is to set a request bit in kvm_handle_hdbss_fault (via kvm_make_request),

Quoted 9 lines

and move the actual flush to the normal exit path, where it can execute in a safe context.

This also allows us to remove the flush from the fault handler entirely.

Does that approach sound reasonable to you?

Yes, I think it looks much better, as the fault will cause guest to exit, and it can run the flush on it's way back in.

Thanks!
Leo

Quoted 45 lines
Quoted 38 lines
Quoted 38 lines
arch/arm64/kvm/mmu.c +15 -0
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
    /**
     * kvm_handle_guest_abort - handles all 2nd stage aborts
     * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
    	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
    	if (esr_fsc_is_translation_fault(esr)) {
    		/* Beyond sanitised PARange (which is the IPA limit) */
    		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -0
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
    	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
    	kfree(vcpu->arch.vncr_tlb);
    	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
    }
 
Quoted 3 lines
Quoted 3 lines
arch/arm64/kvm/reset.c +0 -1
    static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

Thanks!

Tian

#25 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

In reply to Tian Zheng

Hello Tian,

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

(see below)

Quoted 304 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
 arch/arm64/include/asm/esr.h      |   5 ++
 arch/arm64/include/asm/kvm_host.h |  17 +++++
 arch/arm64/include/asm/kvm_mmu.h  |   1 +
 arch/arm64/include/asm/sysreg.h   |  11 ++++
 arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
 arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
 arch/arm64/kvm/reset.c            |   3 +
 8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
 #ifndef __ASSEMBLER__
 #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
 static inline unsigned long esr_brk_comment(unsigned long esr)
 {
 	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
 #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
 #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
 /*
  * Mode of operation configurable with kvm-arm.mode early param.
  * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
 u32 __attribute_const__ kvm_target_cpu(void);
 void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
 struct kvm_hyp_memcache {
 	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
 };
 
 struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
 struct vncr_tlb;
 
 struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
 	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
 	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
 };
 
 /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
 int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
 phys_addr_t kvm_mmu_get_httbr(void);
 phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
 #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
 					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
 /*
  * Definitions for GICv5 instructions]
  */
arch/arm64/kvm/arm.c +102 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
 }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 			    struct kvm_enable_cap *cap)
 {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
 		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
 	default:
 		break;
 	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 			r = kvm_supports_cacheable_pfnmap();
 		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
 	default:
 		r = 0;
 	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
 		if (kvm_dirty_ring_check_request(vcpu))
 			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
 		check_nested_vcpu_requests(vcpu);
 	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
 {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
 }
 
 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +7 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
 	local_irq_restore(flags);
 }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;

arch/arm64/kvm/hyp/vhe/switch.c static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) · line 232

Quoted 10 lines
arch/arm64/kvm/hyp/vhe/switch.c +10 -0
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+

I see in the code below you trust that the tracking will happen with PAGE_SIZE granularity (you track with PAGE_SHIFT).

That may be a problem when we have guest memory backed by hugepages or transparent huge pages.

When we are using HDBSS, there is no fault happening, so we have no way of doing on-demand block splitting, so we need to make use of eager block splitting, _before_ we start to track anything, or else we may have different-sized pages in the HDBSS buffer, which is harder to deal with.

Suggestion: do the eager splitting before we enable HDBSS.

For this to happen, we have to enable the EAGER_SPLIT_CHUNK_SIZE capability, which can only be enabled when all memslots are empty.

I suggest doing that at kvm_init_stage2_mmu(), and checking if HDBSS is in which case we set mmu->split_page_chunk_size to PAGESIZE.

I will send a patch you can put before this one to make sure it works :)

Thanks!
Leo

Quoted 56 lines
arch/arm64/kvm/hyp/vhe/switch.c +2 -0
 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 {
 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 	__vcpu_load_switch_sysregs(vcpu);
 	__vcpu_load_activate_traps(vcpu);
 	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
 }
 
 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
 {
+	kvm_flush_hdbss_buffer(vcpu);
 	__vcpu_put_deactivate_traps(vcpu);
 	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
 	if (exec_fault)
 		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

Here ^

Quoted 62 lines
arch/arm64/kvm/mmu.c +35 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
 /**
  * kvm_handle_guest_abort - handles all 2nd stage aborts
  * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
 	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
 	if (esr_fsc_is_translation_fault(esr)) {
 		/* Beyond sanitised PARange (which is the IPA limit) */
 		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
 	kfree(vcpu->arch.vncr_tlb);
 	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

#26 lore raw

PATCH 1/1 arm64/kvm: Enable eager hugepage splitting if HDBSS is available

FEAT_HDBSS speeds up guest memory dirty tracking by avoiding a page fault and saving the entry in a tracking structure.

That may be a problem when we have guest memory backed by hugepages or transparent huge pages, as it's not possible to do on-demand hugepage splitting, relying only on eager hugepage splitting.

So, at stage2 initialization, enable eager hugepage splitting with chunk = PAGE_SIZE if the system supports HDBSS.

---
 arch/arm64/kvm/mmu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmu.c +6 -2
index 070a01e53fcb..bdfa72b7c073 100644
@@ -993,22 +993,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 
 	mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
 	if (!mmu->last_vcpu_ran) {
 		err = -ENOMEM;
 		goto out_destroy_pgtable;
 	}
 
 	for_each_possible_cpu(cpu)
 		*per_cpu_ptr(mmu->last_vcpu_ran, cpu) = -1;
 
-	 /* The eager page splitting is disabled by default */
-	mmu->split_page_chunk_size = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
+	 /* The eager page splitting is disabled by default if system has no HDBSS */
+	if (system_supports_hacdbs())
+		mmu->split_page_chunk_size = PAGE_SIZE;
+	else
+		mmu->split_page_chunk_size = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
+
 	mmu->split_page_cache.gfp_zero = __GFP_ZERO;
 
 	mmu->pgd_phys = __pa(pgt->pgd);
 
 	if (kvm_is_nested_s2_mmu(kvm, mmu))
 		kvm_init_nested_s2_mmu(mmu);
 
 	return 0;
 
 out_destroy_pgtable:
Signature · 1 line
2.53.0
author
#27 lore raw

Re: [PATCH] arm64/kvm: Enable eager hugepage splitting if HDBSS is available

Quoted 38 lines · Leonardo Bras

FEAT_HDBSS speeds up guest memory dirty tracking by avoiding a page fault and saving the entry in a tracking structure.

That may be a problem when we have guest memory backed by hugepages or transparent huge pages, as it's not possible to do on-demand hugepage splitting, relying only on eager hugepage splitting.

So, at stage2 initialization, enable eager hugepage splitting with chunk = PAGE_SIZE if the system supports HDBSS.

---
  arch/arm64/kvm/mmu.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmu.c +6 -2
index 070a01e53fcb..bdfa72b7c073 100644
@@ -993,22 +993,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
 
  	mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
  	if (!mmu->last_vcpu_ran) {
  		err = -ENOMEM;
  		goto out_destroy_pgtable;
  	}
 
  	for_each_possible_cpu(cpu)
  		*per_cpu_ptr(mmu->last_vcpu_ran, cpu) = -1;
 
-	 /* The eager page splitting is disabled by default */
-	mmu->split_page_chunk_size = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
+	 /* The eager page splitting is disabled by default if system has no HDBSS */
+	if (system_supports_hacdbs())
+		mmu->split_page_chunk_size = PAGE_SIZE;
+	else
+		mmu->split_page_chunk_size = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
+

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1018

Quoted 10 lines
arch/arm64/kvm/mmu.c +0 -0
  	mmu->split_page_cache.gfp_zero = __GFP_ZERO;
 
  	mmu->pgd_phys = __pa(pgt->pgd);
 
  	if (kvm_is_nested_s2_mmu(kvm, mmu))
  		kvm_init_nested_s2_mmu(mmu);
 
  	return 0;
 
  out_destroy_pgtable:

Thanks again for sending this patch. I'll integrate it into the next version and run some tests.

#28 lore raw

Re: [PATCH] arm64/kvm: Enable eager hugepage splitting if HDBSS is available

Quoted 39 lines · Tian Zheng
Quoted 37 lines · Leonardo Bras

FEAT_HDBSS speeds up guest memory dirty tracking by avoiding a page fault and saving the entry in a tracking structure.

That may be a problem when we have guest memory backed by hugepages or transparent huge pages, as it's not possible to do on-demand hugepage splitting, relying only on eager hugepage splitting.

So, at stage2 initialization, enable eager hugepage splitting with chunk = PAGE_SIZE if the system supports HDBSS.

---
  arch/arm64/kvm/mmu.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
arch/arm64/kvm/mmu.c +6 -2
index 070a01e53fcb..bdfa72b7c073 100644
@@ -993,22 +993,26 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t
  	mmu->last_vcpu_ran = alloc_percpu(typeof(*mmu->last_vcpu_ran));
  	if (!mmu->last_vcpu_ran) {
  		err = -ENOMEM;
  		goto out_destroy_pgtable;
  	}
  	for_each_possible_cpu(cpu)
  		*per_cpu_ptr(mmu->last_vcpu_ran, cpu) = -1;
-	 /* The eager page splitting is disabled by default */
-	mmu->split_page_chunk_size = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
+	 /* The eager page splitting is disabled by default if system has no HDBSS */
+	if (system_supports_hacdbs())
+		mmu->split_page_chunk_size = PAGE_SIZE;
+	else
+		mmu->split_page_chunk_size = KVM_ARM_EAGER_SPLIT_CHUNK_SIZE_DEFAULT;
+
  	mmu->split_page_cache.gfp_zero = __GFP_ZERO;
  	mmu->pgd_phys = __pa(pgt->pgd);
Quoted 10 lines

arch/arm64/kvm/mmu.c int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t · line 1018

Quoted 4 lines
arch/arm64/kvm/mmu.c +0 -0
  	if (kvm_is_nested_s2_mmu(kvm, mmu))
  		kvm_init_nested_s2_mmu(mmu);
  	return 0;
  out_destroy_pgtable:

Thanks again for sending this patch. I'll integrate it into the next version and run some tests.

Awesome, thanks!
Leo

#29 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

In reply to Leonardo Bras
Quoted 319 lines · Leonardo Bras

Hello Tian,

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

(see below)

Quoted 309 lines · Tian Zheng

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
 arch/arm64/include/asm/esr.h      |   5 ++
 arch/arm64/include/asm/kvm_host.h |  17 +++++
 arch/arm64/include/asm/kvm_mmu.h  |   1 +
 arch/arm64/include/asm/sysreg.h   |  11 ++++
 arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
 arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
 arch/arm64/kvm/reset.c            |   3 +
 8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
 #ifndef __ASSEMBLER__
 #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
 static inline unsigned long esr_brk_comment(unsigned long esr)
 {
 	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
 #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
 #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
 				     KVM_DIRTY_LOG_INITIALLY_SET)
 
 #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
 /*
  * Mode of operation configurable with kvm-arm.mode early param.
  * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
 u32 __attribute_const__ kvm_target_cpu(void);
 void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
 void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
 struct kvm_hyp_memcache {
 	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
 	 * the associated pKVM instance in the hypervisor.
 	 */
 	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
 };
 
 struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
 	bool		reset;
 };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
 struct vncr_tlb;
 
 struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
 	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
 	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
 };
 
 /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
 int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
 int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
 phys_addr_t kvm_mmu_get_httbr(void);
 phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
 #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
 					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
 /*
  * Definitions for GICv5 instructions]
  */
arch/arm64/kvm/arm.c +102 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
 	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
 }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
 int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 			    struct kvm_enable_cap *cap)
 {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = 0;
 		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
 		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
 	default:
 		break;
 	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 			r = kvm_supports_cacheable_pfnmap();
 		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
 	default:
 		r = 0;
 	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
 		if (kvm_dirty_ring_check_request(vcpu))
 			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
 		check_nested_vcpu_requests(vcpu);
 	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
 {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
 }
 
 static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +12 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
 	local_irq_restore(flags);
 }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
Quoted 8 lines

arch/arm64/kvm/hyp/vhe/switch.c static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) · line 232

Quoted 5 lines
arch/arm64/kvm/hyp/vhe/switch.c +5 -0
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+

I see in the code below you trust that the tracking will happen with PAGE_SIZE granularity (you track with PAGE_SHIFT).

Oh, that was misleading :\
The mentioned routine is not wrong AFAICS, but without the patch I sent, if the VMM does not manually set eager splitting, and is using some sort of hugepages (even transparent, without noticing) it could end up sending just PAGE_SIZE instead of the whole hugepage size, which breaks migration.

Does that make sense?

Thanks!
Leo

Quoted 154 lines

That may be a problem when we have guest memory backed by hugepages or transparent huge pages.

When we are using HDBSS, there is no fault happening, so we have no way of doing on-demand block splitting, so we need to make use of eager block splitting, _before_ we start to track anything, or else we may have different-sized pages in the HDBSS buffer, which is harder to deal with.

Suggestion: do the eager splitting before we enable HDBSS.

For this to happen, we have to enable the EAGER_SPLIT_CHUNK_SIZE capability, which can only be enabled when all memslots are empty.

I suggest doing that at kvm_init_stage2_mmu(), and checking if HDBSS is in which case we set mmu->split_page_chunk_size to PAGESIZE.

I will send a patch you can put before this one to make sure it works :)

Thanks!
Leo

Quoted 56 lines
arch/arm64/kvm/hyp/vhe/switch.c +2 -0
 void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 {
 	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
 	__vcpu_load_switch_sysregs(vcpu);
 	__vcpu_load_activate_traps(vcpu);
 	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
 }
 
 void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
 {
+	kvm_flush_hdbss_buffer(vcpu);
 	__vcpu_put_deactivate_traps(vcpu);
 	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +25 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	if (writable)
 		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
 	if (exec_fault)
 		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

Here ^

Quoted 62 lines
arch/arm64/kvm/mmu.c +35 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
 /**
  * kvm_handle_guest_abort - handles all 2nd stage aborts
  * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
 	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
 	if (esr_fsc_is_translation_fault(esr)) {
 		/* Beyond sanitised PARange (which is the IPA limit) */
 		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
 	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
 	kfree(vcpu->arch.vncr_tlb);
 	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
 }
 
 static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

author
#30 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

In reply to Leonardo Bras
Quoted 8 lines · Leonardo Bras

Hello Tian,

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

(see below)

Of course! Happy to exchange ideas and learn together.

Quoted 332 lines · Tian Zheng
Quoted 304 lines

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
  arch/arm64/include/asm/esr.h      |   5 ++
  arch/arm64/include/asm/kvm_host.h |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h  |   1 +
  arch/arm64/include/asm/sysreg.h   |  11 ++++
  arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
  arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
  arch/arm64/kvm/reset.c            |   3 +
  8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
  #ifndef __ASSEMBLER__
  #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
  static inline unsigned long esr_brk_comment(unsigned long esr)
  {
  	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
  #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
  #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  				     KVM_DIRTY_LOG_INITIALLY_SET)
 
  #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
  /*
   * Mode of operation configurable with kvm-arm.mode early param.
   * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
  u32 __attribute_const__ kvm_target_cpu(void);
  void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
  struct kvm_hyp_memcache {
  	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
  	 * the associated pKVM instance in the hypervisor.
  	 */
  	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
  };
 
  struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
  	bool		reset;
  };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
  struct vncr_tlb;
 
  struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
  	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
  	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
  };
 
  /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
  int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
  int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
  phys_addr_t kvm_mmu_get_httbr(void);
  phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
  #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
  					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
  /*
   * Definitions for GICv5 instructions]
   */
arch/arm64/kvm/arm.c +102 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
  int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  			    struct kvm_enable_cap *cap)
  {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		r = 0;
  		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
  		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
  	default:
  		break;
  	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  			r = kvm_supports_cacheable_pfnmap();
  		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
  	default:
  		r = 0;
  	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
  		if (kvm_dirty_ring_check_request(vcpu))
  			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
  		check_nested_vcpu_requests(vcpu);
  	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
  void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
  }
 
  static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +7 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
  	local_irq_restore(flags);
  }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;

arch/arm64/kvm/hyp/vhe/switch.c static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) · line 232

Quoted 10 lines
arch/arm64/kvm/hyp/vhe/switch.c +10 -0
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+

I see in the code below you trust that the tracking will happen with PAGE_SIZE granularity (you track with PAGE_SHIFT).

That may be a problem when we have guest memory backed by hugepages or transparent huge pages.

When we are using HDBSS, there is no fault happening, so we have no way of doing on-demand block splitting, so we need to make use of eager block splitting, _before_ we start to track anything, or else we may have different-sized pages in the HDBSS buffer, which is harder to deal with.

Suggestion: do the eager splitting before we enable HDBSS.

For this to happen, we have to enable the EAGER_SPLIT_CHUNK_SIZE capability, which can only be enabled when all memslots are empty.

Quoted 7 lines

I suggest doing that at kvm_init_stage2_mmu(), and checking if HDBSS is in which case we set mmu->split_page_chunk_size to PAGESIZE.

I will send a patch you can put before this one to make sure it works :)

Thanks!
Leo

Hi Leo,

Thanks for the helpful suggestion. I had previously traced the hugepage-splitting path

during live migration and found that when migration starts, enabling dirty logging

triggers the splitting path. I also tested HDBSS with traditional hugepages and haven't

observed any issues yet.

However, your concern is valid — there may be cases not covered, especially when the

VMM uses transparent hugepages. I'll integrate your patch into the next version and

run some tests.

For reference, here's the path I traced:

```

- userspace, e.g., QEMU

kvm_log_start
+-> kvm_section_update_flags
    +-> kvm_slot_update_flags
        |
        | // For each memory region, QEMU issues a KVM_SET_USER_MEMORY_REGION ioctl.
        | // Before issuing it, flags are updated to include KVM_MEM_LOG_DIRTY_PAGES.
        +-> kvm_mem_flags
        +-> kvm_set_user_memory_region   // ioctl that enables dirty logging on the memslot

- KVM

KVM_SET_USER_MEMORY_REGION
+-> kvm_vm_ioctl_set_memory_region
    +-> kvm_set_memory_region / __kvm_set_memory_region
        +-> kvm_set_memslot
            +-> kvm_commit_memory_region
                +-> kvm_arch_commit_memory_region
                    +-> kvm_mmu_split_memory_region
                        // Splits Stage-2 hugepages/contiguous mappings into 4KB PTEs.
                        +-> kvm_mmu_split_huge_pages
                            +-> kvm_pgtable_stage2_split

```

Thanks again for the detailed explanation and for sending the patch.

Quoted 59 lines
Quoted 59 lines
arch/arm64/kvm/hyp/vhe/switch.c +2 -0
  void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  {
  	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  	__vcpu_load_switch_sysregs(vcpu);
  	__vcpu_load_activate_traps(vcpu);
  	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
  }
 
  void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
  {
+	kvm_flush_hdbss_buffer(vcpu);
  	__vcpu_put_deactivate_traps(vcpu);
  	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +28 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (writable)
  		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
  	if (exec_fault)
  		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
  	return 0;
  }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
Quoted 8 lines

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 7 lines
arch/arm64/kvm/mmu.c +7 -0
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

Here ^

Thanks!

Tian

Quoted 63 lines
Quoted 63 lines
arch/arm64/kvm/mmu.c +35 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
  /**
   * kvm_handle_guest_abort - handles all 2nd stage aborts
   * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
  	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
  	if (esr_fsc_is_translation_fault(esr)) {
  		/* Beyond sanitised PARange (which is the IPA limit) */
  		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
  	kfree(vcpu->arch.vncr_tlb);
  	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
  }
 
  static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

#31 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 9 lines · Tian Zheng
Quoted 7 lines · Leonardo Bras

Hello Tian,

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

Quoted 4 lines
Quoted 1 line

(see below)

Of course! Happy to exchange ideas and learn together.

:)

Quoted 386 lines
Quoted 334 lines · Tian Zheng
Quoted 304 lines

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
  arch/arm64/include/asm/esr.h      |   5 ++
  arch/arm64/include/asm/kvm_host.h |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h  |   1 +
  arch/arm64/include/asm/sysreg.h   |  11 ++++
  arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
  arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
  arch/arm64/kvm/reset.c            |   3 +
  8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
  #ifndef __ASSEMBLER__
  #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
  static inline unsigned long esr_brk_comment(unsigned long esr)
  {
  	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
  #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
  #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
  				     KVM_DIRTY_LOG_INITIALLY_SET)
 
  #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
  /*
   * Mode of operation configurable with kvm-arm.mode early param.
   * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
  u32 __attribute_const__ kvm_target_cpu(void);
  void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
  void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
  struct kvm_hyp_memcache {
  	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
  	 * the associated pKVM instance in the hypervisor.
  	 */
  	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
  };
 
  struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
  	bool		reset;
  };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
  struct vncr_tlb;
 
  struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
  	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
  	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
  };
 
  /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
  int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
  int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
  phys_addr_t kvm_mmu_get_httbr(void);
  phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
  #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
  					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
  /*
   * Definitions for GICv5 instructions]
   */
arch/arm64/kvm/arm.c +102 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
  	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
  }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
  int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  			    struct kvm_enable_cap *cap)
  {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
  		r = 0;
  		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
  		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
  	default:
  		break;
  	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
  			r = kvm_supports_cacheable_pfnmap();
  		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
  	default:
  		r = 0;
  	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
  		if (kvm_dirty_ring_check_request(vcpu))
  			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
  		check_nested_vcpu_requests(vcpu);
  	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
  void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
  }
 
  static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +7 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
  	local_irq_restore(flags);
  }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;

arch/arm64/kvm/hyp/vhe/switch.c static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) · line 232

Quoted 10 lines
arch/arm64/kvm/hyp/vhe/switch.c +10 -0
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+

I see in the code below you trust that the tracking will happen with PAGE_SIZE granularity (you track with PAGE_SHIFT).

That may be a problem when we have guest memory backed by hugepages or transparent huge pages.

When we are using HDBSS, there is no fault happening, so we have no way of doing on-demand block splitting, so we need to make use of eager block splitting, _before_ we start to track anything, or else we may have different-sized pages in the HDBSS buffer, which is harder to deal with.

Suggestion: do the eager splitting before we enable HDBSS.

For this to happen, we have to enable the EAGER_SPLIT_CHUNK_SIZE capability, which can only be enabled when all memslots are empty.

Quoted 7 lines

I suggest doing that at kvm_init_stage2_mmu(), and checking if HDBSS is in which case we set mmu->split_page_chunk_size to PAGESIZE.

I will send a patch you can put before this one to make sure it works :)

Thanks!
Leo

Hi Leo,

Thanks for the helpful suggestion. I had previously traced the hugepage-splitting path

during live migration and found that when migration starts, enabling dirty logging

triggers the splitting path. I also tested HDBSS with traditional hugepages and haven't

observed any issues yet.

However, your concern is valid — there may be cases not covered, especially when the

VMM uses transparent hugepages. I'll integrate your patch into the next version and

run some tests.

For reference, here's the path I traced:

```

- userspace, e.g., QEMU

kvm_log_start

+-> kvm_section_update_flags
    +-> kvm_slot_update_flags
        |
        | // For each memory region, QEMU issues a
KVM_SET_USER_MEMORY_REGION ioctl.
        | // Before issuing it, flags are updated to include
KVM_MEM_LOG_DIRTY_PAGES.
        +-> kvm_mem_flags
        +-> kvm_set_user_memory_region   // ioctl that enables dirty logging

on the memslot

- KVM

KVM_SET_USER_MEMORY_REGION

Quoted 8 lines
+-> kvm_vm_ioctl_set_memory_region
    +-> kvm_set_memory_region / __kvm_set_memory_region
        +-> kvm_set_memslot
            +-> kvm_commit_memory_region
                +-> kvm_arch_commit_memory_region
                    +-> kvm_mmu_split_memory_region

                        // Splits Stage-2 hugepages/contiguous mappings into 4KB PTEs.

Right, except on a case we have dirty_log_manual_protect and init_set, when it returns before splitting pages:

```
if (kvm_dirty_log_manual_protect_and_init_set(kvm))
	return;
```

IIUC, that's desired to avoid holding the lock for a long time while it cleans every page in the beginning, and instead do it in a per dirty-page basis. I guess it may benefit guests with very little dirty pages, as it does not have to split/dirty everything at the start.
(Its a pain for my HACDBS routines, though)

Quoted 1 line

                        +-> kvm_mmu_split_huge_pages

Other important point here:
You can see in this function it skips splitting if chunk_size == 0. This value is set by a capability that configures EAGER_SPLIT, meaning splitting before the guest have write faults, which is nice as the write-fault is faster.

Two points in this capability:
- It's optional, if it's not set, only on-demand splitting (on fault) will happen, and since HDBSS removes the write-fault, we have no splitting
- It can be set to any valid block size, not only 4K, nor PAGE_SIZE, it can be set to PMD_SIZE, PUD_SIZE, and so on, which will depend on the PAGE_SIZE the kernel was compiled to.

That's only some points to keep in mind :)

		if (kvm_dirty_log_manual_protect_and_init_set(kvm))
			return;
Quoted 6 lines

                            +-> kvm_pgtable_stage2_split

```

Thanks again for the detailed explanation and for sending the patch.

Thank you for the collaboration on this!
Leo

Quoted 136 lines
Quoted 59 lines
Quoted 59 lines
arch/arm64/kvm/hyp/vhe/switch.c +2 -0
  void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  {
  	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
  	__vcpu_load_switch_sysregs(vcpu);
  	__vcpu_load_activate_traps(vcpu);
  	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
  }
 
  void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
  {
+	kvm_flush_hdbss_buffer(vcpu);
  	__vcpu_put_deactivate_traps(vcpu);
  	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +28 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	if (writable)
  		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
  	if (exec_fault)
  		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
  	return 0;
  }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
Quoted 8 lines

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 7 lines
arch/arm64/kvm/mmu.c +7 -0
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

Here ^

Thanks!

Tian

Quoted 63 lines
Quoted 63 lines
arch/arm64/kvm/mmu.c +35 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
  /**
   * kvm_handle_guest_abort - handles all 2nd stage aborts
   * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
  	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
  	if (esr_fsc_is_translation_fault(esr)) {
  		/* Beyond sanitised PARange (which is the IPA limit) */
  		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
  	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
  	kfree(vcpu->arch.vncr_tlb);
  	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
  }
 
  static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

author
#32 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 438 lines · Leonardo Bras
Quoted 11 lines · Tian Zheng
Quoted 8 lines · Leonardo Bras

Hello Tian,

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

(see below)

Of course! Happy to exchange ideas and learn together.

:)

Quoted 383 lines
Quoted 332 lines · Tian Zheng
Quoted 304 lines

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
   arch/arm64/include/asm/esr.h      |   5 ++
   arch/arm64/include/asm/kvm_host.h |  17 +++++
   arch/arm64/include/asm/kvm_mmu.h  |   1 +
   arch/arm64/include/asm/sysreg.h   |  11 ++++
   arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
   arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
   arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
   arch/arm64/kvm/reset.c            |   3 +
   8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
   #ifndef __ASSEMBLER__
   #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
   static inline unsigned long esr_brk_comment(unsigned long esr)
   {
   	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
   #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
   #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
   				     KVM_DIRTY_LOG_INITIALLY_SET)
 
   #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
   /*
    * Mode of operation configurable with kvm-arm.mode early param.
    * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
   u32 __attribute_const__ kvm_target_cpu(void);
   void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
   void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
   struct kvm_hyp_memcache {
   	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
   	 * the associated pKVM instance in the hypervisor.
   	 */
   	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
   };
 
   struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
   	bool		reset;
   };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
   struct vncr_tlb;
 
   struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
   	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
   	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
   };
 
   /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
   int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
   int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
   phys_addr_t kvm_mmu_get_httbr(void);
   phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
   #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
   					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
   /*
    * Definitions for GICv5 instructions]
    */
arch/arm64/kvm/arm.c +102 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
   	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
   }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
   int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   			    struct kvm_enable_cap *cap)
   {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   		r = 0;
   		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
   		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
   	default:
   		break;
   	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
   			r = kvm_supports_cacheable_pfnmap();
   		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
   	default:
   		r = 0;
   	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
   		if (kvm_dirty_ring_check_request(vcpu))
   			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
   		check_nested_vcpu_requests(vcpu);
   	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
   void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
   {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
   }
 
   static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +7 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
   	local_irq_restore(flags);
   }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;

arch/arm64/kvm/hyp/vhe/switch.c static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) · line 232

Quoted 10 lines
arch/arm64/kvm/hyp/vhe/switch.c +10 -0
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+

I see in the code below you trust that the tracking will happen with PAGE_SIZE granularity (you track with PAGE_SHIFT).

That may be a problem when we have guest memory backed by hugepages or transparent huge pages.

When we are using HDBSS, there is no fault happening, so we have no way of doing on-demand block splitting, so we need to make use of eager block splitting, _before_ we start to track anything, or else we may have different-sized pages in the HDBSS buffer, which is harder to deal with.

Suggestion: do the eager splitting before we enable HDBSS.

For this to happen, we have to enable the EAGER_SPLIT_CHUNK_SIZE capability, which can only be enabled when all memslots are empty.

Quoted 7 lines

I suggest doing that at kvm_init_stage2_mmu(), and checking if HDBSS is in which case we set mmu->split_page_chunk_size to PAGESIZE.

I will send a patch you can put before this one to make sure it works :)

Thanks!
Leo

Hi Leo,

Thanks for the helpful suggestion. I had previously traced the hugepage-splitting path

during live migration and found that when migration starts, enabling dirty logging

triggers the splitting path. I also tested HDBSS with traditional hugepages and haven't

observed any issues yet.

However, your concern is valid — there may be cases not covered, especially when the

VMM uses transparent hugepages. I'll integrate your patch into the next version and

run some tests.

For reference, here's the path I traced:

```

- userspace, e.g., QEMU

kvm_log_start

+-> kvm_section_update_flags
     +-> kvm_slot_update_flags
         |
         | // For each memory region, QEMU issues a
KVM_SET_USER_MEMORY_REGION ioctl.
         | // Before issuing it, flags are updated to include
KVM_MEM_LOG_DIRTY_PAGES.
         +-> kvm_mem_flags
         +-> kvm_set_user_memory_region   // ioctl that enables dirty logging

on the memslot

- KVM

KVM_SET_USER_MEMORY_REGION

Quoted 8 lines
+-> kvm_vm_ioctl_set_memory_region
     +-> kvm_set_memory_region / __kvm_set_memory_region
         +-> kvm_set_memslot
             +-> kvm_commit_memory_region
                 +-> kvm_arch_commit_memory_region
                     +-> kvm_mmu_split_memory_region

                        // Splits Stage-2 hugepages/contiguous mappings into
4KB PTEs.

Right, except on a case we have dirty_log_manual_protect and init_set, when it returns before splitting pages:

```
if (kvm_dirty_log_manual_protect_and_init_set(kvm))
	return;
```

IIUC, that's desired to avoid holding the lock for a long time while it cleans every page in the beginning, and instead do it in a per dirty-page basis. I guess it may benefit guests with very little dirty pages, as it does not have to split/dirty everything at the start.
(Its a pain for my HACDBS routines, though)

Quoted 1 line

                        +-> kvm_mmu_split_huge_pages

Other important point here:
You can see in this function it skips splitting if chunk_size == 0. This value is set by a capability that configures EAGER_SPLIT, meaning splitting before the guest have write faults, which is nice as the write-fault is faster.

Two points in this capability:
- It's optional, if it's not set, only on-demand splitting (on fault) will happen, and since HDBSS removes the write-fault, we have no splitting
- It can be set to any valid block size, not only 4K, nor PAGE_SIZE, it can be set to PMD_SIZE, PUD_SIZE, and so on, which will depend on the PAGE_SIZE the kernel was compiled to.

That's only some points to keep in mind :)

		if (kvm_dirty_log_manual_protect_and_init_set(kvm))
			return;
Quoted 8 lines
Quoted 6 lines

                            +-> kvm_pgtable_stage2_split

```

Thanks again for the detailed explanation and for sending the patch.

Thank you for the collaboration on this!
Leo

Thanks for the detailed explanation — very helpful. My earlier tests missed cases like lazy splitting

and manual‑protect mode, and your patch addresses them perfectly.

I'll adopt it in the next version and test the corner cases you mentioned.

Tian

Quoted 134 lines
Quoted 134 lines
Quoted 59 lines
Quoted 59 lines
arch/arm64/kvm/hyp/vhe/switch.c +2 -0
   void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   {
   	host_data_ptr(host_ctxt)->__hyp_running_vcpu = vcpu;
@@ -220,10 +237,12 @@ void kvm_vcpu_load_vhe(struct kvm_vcpu *vcpu)
   	__vcpu_load_switch_sysregs(vcpu);
   	__vcpu_load_activate_traps(vcpu);
   	__load_stage2(vcpu->arch.hw_mmu, vcpu->arch.hw_mmu->arch);
+	__load_hdbss(vcpu);
   }
 
   void kvm_vcpu_put_vhe(struct kvm_vcpu *vcpu)
   {
+	kvm_flush_hdbss_buffer(vcpu);
   	__vcpu_put_deactivate_traps(vcpu);
   	__vcpu_put_switch_sysregs(vcpu);
 
arch/arm64/kvm/mmu.c +28 -0
index 070a01e53fcb..42b0710a16ce 100644
@@ -1896,6 +1896,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
   	if (writable)
   		prot |= KVM_PGTABLE_PROT_W;
 
+	if (writable && kvm->arch.enable_hdbss && logging_active)
+		prot |= KVM_PGTABLE_PROT_DBM;
+
   	if (exec_fault)
   		prot |= KVM_PGTABLE_PROT_X;
 
@@ -2033,6 +2036,70 @@ int kvm_handle_guest_sea(struct kvm_vcpu *vcpu)
   	return 0;
   }
 
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu)
+{
+	int idx, curr_idx;
+	u64 br_el2;
+	u64 *hdbss_buf;
+	struct kvm *kvm = vcpu->kvm;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
+
+	curr_idx = HDBSSPROD_IDX(read_sysreg_s(SYS_HDBSSPROD_EL2));
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+
+	/* Do nothing if HDBSS buffer is empty or br_el2 is NULL */
+	if (curr_idx == 0 || br_el2 == 0)
+		return;
+
+	hdbss_buf = page_address(phys_to_page(vcpu->arch.hdbss.base_phys));
+	if (!hdbss_buf)
+		return;
+
+	guard(write_lock_irqsave)(&vcpu->kvm->mmu_lock);
+	for (idx = 0; idx < curr_idx; idx++) {
+		u64 gpa;
+
Quoted 8 lines

arch/arm64/kvm/mmu.c int kvm_handle_guest_sea(struct kvm_vcpu *vcpu) · line 2070

Quoted 7 lines
arch/arm64/kvm/mmu.c +7 -0
+		gpa = hdbss_buf[idx];
+		if (!(gpa & HDBSS_ENTRY_VALID))
+			continue;
+
+		gpa &= HDBSS_ENTRY_IPA;
+		kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
+	}

Here ^

Thanks!

Tian

Quoted 62 lines
Quoted 62 lines
arch/arm64/kvm/mmu.c +35 -0
+
+	/* reset HDBSS index */
+	write_sysreg_s(0, SYS_HDBSSPROD_EL2);
+	vcpu->arch.hdbss.next_index = 0;
+	isb();
+}
+
+static int kvm_handle_hdbss_fault(struct kvm_vcpu *vcpu)
+{
+	u64 prod;
+	u64 fsc;
+
+	prod = read_sysreg_s(SYS_HDBSSPROD_EL2);
+	fsc = FIELD_GET(HDBSSPROD_EL2_FSC_MASK, prod);
+
+	switch (fsc) {
+	case HDBSSPROD_EL2_FSC_OK:
+		/* Buffer full, which is reported as permission fault. */
+		kvm_flush_hdbss_buffer(vcpu);
+		return 1;
+	case HDBSSPROD_EL2_FSC_ExternalAbort:
+	case HDBSSPROD_EL2_FSC_GPF:
+		return -EFAULT;
+	default:
+		/* Unknown fault. */
+		WARN_ONCE(1,
+				"Unexpected HDBSS fault type, FSC: 0x%llx (prod=0x%llx, vcpu=%d)\n",
+				fsc, prod, vcpu->vcpu_id);
+		return -EFAULT;
+	}
+}
+
   /**
    * kvm_handle_guest_abort - handles all 2nd stage aborts
    * @vcpu:	the VCPU pointer
@@ -2071,6 +2138,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 
   	is_iabt = kvm_vcpu_trap_is_iabt(vcpu);
 
+	if (esr_iss2_is_hdbssf(esr))
+		return kvm_handle_hdbss_fault(vcpu);
+
   	if (esr_fsc_is_translation_fault(esr)) {
   		/* Beyond sanitised PARange (which is the IPA limit) */
   		if (fault_ipa >= BIT_ULL(get_kvm_ipa_limit())) {
arch/arm64/kvm/reset.c +3 -1
index 959532422d3a..c03a4b310b53 100644
@@ -161,6 +161,9 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu)
   	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
   	kfree(vcpu->arch.vncr_tlb);
   	kfree(vcpu->arch.ccsidr);
+
+	if (vcpu->kvm->arch.enable_hdbss)
+		kvm_arm_vcpu_free_hdbss(vcpu);
   }
 
   static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
--

2.33.0

#33 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 449 lines · Tian Zheng
Quoted 437 lines · Leonardo Bras
Quoted 11 lines · Tian Zheng
Quoted 8 lines · Leonardo Bras

Hello Tian,

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

(see below)

Of course! Happy to exchange ideas and learn together.

:)

Quoted 383 lines
Quoted 332 lines · Tian Zheng
Quoted 304 lines

From: eillon <[email protected]>

HDBSS is enabled via an ioctl from userspace (e.g. QEMU) at the start of migration. This feature is only supported in VHE mode.

Initially, S2 PTEs doesn't contain the DBM attribute. During migration, write faults are handled by user_mem_abort, which relaxes permissions and adds the DBM bit when HDBSS is active. Once DBM is set, subsequent writes no longer trap, as the hardware automatically transitions the page from writable-clean to writable-dirty.

KVM does not scan S2 page tables to consume DBM. Instead, when HDBSS is enabled, the hardware observes the clean->dirty transition and records the corresponding page into the HDBSS buffer.

During sync_dirty_log, KVM kicks all vCPUs to force VM-Exit, ensuring that check_vcpu_requests flushes the HDBSS buffer and propagates the accumulated dirty information into the userspace-visible dirty bitmap.

Add fault handling for HDBSS including buffer full, external abort, and

general protection fault (GPF).
---
   arch/arm64/include/asm/esr.h      |   5 ++
   arch/arm64/include/asm/kvm_host.h |  17 +++++
   arch/arm64/include/asm/kvm_mmu.h  |   1 +
   arch/arm64/include/asm/sysreg.h   |  11 ++++
   arch/arm64/kvm/arm.c              | 102 ++++++++++++++++++++++++++++++
   arch/arm64/kvm/hyp/vhe/switch.c   |  19 ++++++
   arch/arm64/kvm/mmu.c              |  70 ++++++++++++++++++++
   arch/arm64/kvm/reset.c            |   3 +
   8 files changed, 228 insertions(+)
arch/arm64/include/asm/esr.h +5 -0
index 81c17320a588..2e6b679b5908 100644
@@ -437,6 +437,11 @@
   #ifndef __ASSEMBLER__
   #include <asm/types.h>
 
+static inline bool esr_iss2_is_hdbssf(unsigned long esr)
+{
+	return ESR_ELx_ISS2(esr) & ESR_ELx_HDBSSF;
+}
+
   static inline unsigned long esr_brk_comment(unsigned long esr)
   {
   	return esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
arch/arm64/include/asm/kvm_host.h +17 -0
index 5d5a3bbdb95e..57ee6b53e061 100644
@@ -55,12 +55,17 @@
   #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_FLUSH_HDBSS			KVM_ARCH_REQ(12)
 
   #define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
   				     KVM_DIRTY_LOG_INITIALLY_SET)
 
   #define KVM_HAVE_MMU_RWLOCK
 
+/* HDBSS entry field definitions */
+#define HDBSS_ENTRY_VALID BIT(0)
+#define HDBSS_ENTRY_IPA GENMASK_ULL(55, 12)
+
   /*
    * Mode of operation configurable with kvm-arm.mode early param.
    * See Documentation/admin-guide/kernel-parameters.txt for more information.
@@ -84,6 +89,7 @@ int __init kvm_arm_init_sve(void);
   u32 __attribute_const__ kvm_target_cpu(void);
   void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
   void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu);
 
   struct kvm_hyp_memcache {
   	phys_addr_t head;
@@ -405,6 +411,8 @@ struct kvm_arch {
   	 * the associated pKVM instance in the hypervisor.
   	 */
   	struct kvm_protected_vm pkvm;
+
+	bool enable_hdbss;
   };
 
   struct kvm_vcpu_fault_info {
@@ -816,6 +824,12 @@ struct vcpu_reset_state {
   	bool		reset;
   };
 
+struct vcpu_hdbss_state {
+	phys_addr_t base_phys;
+	u32 size;
+	u32 next_index;
+};
+
   struct vncr_tlb;
 
   struct kvm_vcpu_arch {
@@ -920,6 +934,9 @@ struct kvm_vcpu_arch {
 
   	/* Per-vcpu TLB for VNCR_EL2 -- NULL when !NV */
   	struct vncr_tlb	*vncr_tlb;
+
+	/* HDBSS registers info */
+	struct vcpu_hdbss_state hdbss;
   };
 
   /*
arch/arm64/include/asm/kvm_mmu.h +1 -0
index d968aca0461a..3fea8cfe8869 100644
@@ -183,6 +183,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,
 
   int kvm_handle_guest_sea(struct kvm_vcpu *vcpu);
   int kvm_handle_guest_abort(struct kvm_vcpu *vcpu);
+void kvm_flush_hdbss_buffer(struct kvm_vcpu *vcpu);
 
   phys_addr_t kvm_mmu_get_httbr(void);
   phys_addr_t kvm_get_idmap_vector(void);
arch/arm64/include/asm/sysreg.h +11 -0
index f4436ecc630c..d11f4d0dd4e7 100644
@@ -1039,6 +1039,17 @@
 
   #define GCS_CAP(x)	((((unsigned long)x) & GCS_CAP_ADDR_MASK) | \
   					       GCS_CAP_VALID_TOKEN)
+
+/*
+ * Definitions for the HDBSS feature
+ */
+#define HDBSS_MAX_SIZE		HDBSSBR_EL2_SZ_2MB
+
+#define HDBSSBR_EL2(baddr, sz)	(((baddr) & GENMASK(55, 12 + sz)) | \
+				 FIELD_PREP(HDBSSBR_EL2_SZ_MASK, sz))
+
+#define HDBSSPROD_IDX(prod)	FIELD_GET(HDBSSPROD_EL2_INDEX_MASK, prod)
+
   /*
    * Definitions for GICv5 instructions]
    */
arch/arm64/kvm/arm.c +102 -0
index 29f0326f7e00..d64da05e25c4 100644
@@ -125,6 +125,87 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
   	return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
   }
 
+void kvm_arm_vcpu_free_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct page *hdbss_pg;
+
+	hdbss_pg = phys_to_page(vcpu->arch.hdbss.base_phys);
+	if (hdbss_pg)
+		__free_pages(hdbss_pg, vcpu->arch.hdbss.size);
+
+	vcpu->arch.hdbss.size = 0;
+}
+
+static int kvm_cap_arm_enable_hdbss(struct kvm *kvm,
+				    struct kvm_enable_cap *cap)
+{
+	unsigned long i;
+	struct kvm_vcpu *vcpu;
+	struct page *hdbss_pg = NULL;
+	__u64 size = cap->args[0];
+	bool enable = cap->args[1] ? true : false;
+
+	if (!system_supports_hdbss())
+		return -EINVAL;
+
+	if (size > HDBSS_MAX_SIZE)
+		return -EINVAL;
+
+	if (!enable && !kvm->arch.enable_hdbss) /* Already Off */
+		return 0;
+
+	if (enable && kvm->arch.enable_hdbss) /* Already On, can't set size */
+		return -EINVAL;
+
+	if (!enable) { /* Turn it off */
+		kvm->arch.mmu.vtcr &= ~(VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA);
+
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			/* Kick vcpus to flush hdbss buffer. */
+			kvm_vcpu_kick(vcpu);
+
+			kvm_arm_vcpu_free_hdbss(vcpu);
+		}
+
+		kvm->arch.enable_hdbss = false;
+
+		return 0;
+	}
+
+	/* Turn it on */
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		hdbss_pg = alloc_pages(GFP_KERNEL_ACCOUNT, size);
+		if (!hdbss_pg)
+			goto error_alloc;
+
+		vcpu->arch.hdbss = (struct vcpu_hdbss_state) {
+			.base_phys = page_to_phys(hdbss_pg),
+			.size = size,
+			.next_index = 0,
+		};
+	}
+
+	kvm->arch.enable_hdbss = true;
+	kvm->arch.mmu.vtcr |= VTCR_EL2_HD | VTCR_EL2_HDBSS | VTCR_EL2_HA;
+
+	/*
+	 * We should kick vcpus out of guest mode here to load new
+	 * vtcr value to vtcr_el2 register when re-enter guest mode.
+	 */
+	kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_vcpu_kick(vcpu);
+
+	return 0;
+
+error_alloc:
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (vcpu->arch.hdbss.base_phys)
+			kvm_arm_vcpu_free_hdbss(vcpu);
+	}
+
+	return -ENOMEM;
+}
+
   int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   			    struct kvm_enable_cap *cap)
   {
@@ -182,6 +263,11 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
   		r = 0;
   		set_bit(KVM_ARCH_FLAG_EXIT_SEA, &kvm->arch.flags);
   		break;
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		mutex_lock(&kvm->lock);
+		r = kvm_cap_arm_enable_hdbss(kvm, cap);
+		mutex_unlock(&kvm->lock);
+		break;
   	default:
   		break;
   	}
@@ -471,6 +557,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
   			r = kvm_supports_cacheable_pfnmap();
   		break;
 
+	case KVM_CAP_ARM_HW_DIRTY_STATE_TRACK:
+		r = system_supports_hdbss();
+		break;
   	default:
   		r = 0;
   	}
@@ -1120,6 +1209,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu)
   		if (kvm_dirty_ring_check_request(vcpu))
   			return 0;
 
+		if (kvm_check_request(KVM_REQ_FLUSH_HDBSS, vcpu))
+			kvm_flush_hdbss_buffer(vcpu);
+
   		check_nested_vcpu_requests(vcpu);
   	}
 
@@ -1898,7 +1990,17 @@ long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
 
   void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
   {
+	/*
+	 * Flush all CPUs' dirty log buffers to the dirty_bitmap.  Called
+	 * before reporting dirty_bitmap to userspace. Send a request with
+	 * KVM_REQUEST_WAIT to flush buffer synchronously.
+	 */
+	struct kvm_vcpu *vcpu;
+
+	if (!kvm->arch.enable_hdbss)
+		return;
 
+	kvm_make_all_cpus_request(kvm, KVM_REQ_FLUSH_HDBSS);
   }
 
   static int kvm_vm_ioctl_set_device_addr(struct kvm *kvm,
arch/arm64/kvm/hyp/vhe/switch.c +7 -0
index 9db3f11a4754..600cbc4f8ae9 100644
@@ -213,6 +213,23 @@ static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu)
   	local_irq_restore(flags);
   }
 
+static void __load_hdbss(struct kvm_vcpu *vcpu)
+{
+	struct kvm *kvm = vcpu->kvm;
+	u64 br_el2, prod_el2;
+
+	if (!kvm->arch.enable_hdbss)
+		return;

arch/arm64/kvm/hyp/vhe/switch.c static void __vcpu_put_deactivate_traps(struct kvm_vcpu *vcpu) · line 232

Quoted 10 lines
arch/arm64/kvm/hyp/vhe/switch.c +10 -0
+
+	br_el2 = HDBSSBR_EL2(vcpu->arch.hdbss.base_phys, vcpu->arch.hdbss.size);
+	prod_el2 = vcpu->arch.hdbss.next_index;
+
+	write_sysreg_s(br_el2, SYS_HDBSSBR_EL2);
+	write_sysreg_s(prod_el2, SYS_HDBSSPROD_EL2);
+
+	isb();
+}
+

I see in the code below you trust that the tracking will happen with PAGE_SIZE granularity (you track with PAGE_SHIFT).

That may be a problem when we have guest memory backed by hugepages or transparent huge pages.

When we are using HDBSS, there is no fault happening, so we have no way of doing on-demand block splitting, so we need to make use of eager block splitting, _before_ we start to track anything, or else we may have different-sized pages in the HDBSS buffer, which is harder to deal with.

Suggestion: do the eager splitting before we enable HDBSS.

For this to happen, we have to enable the EAGER_SPLIT_CHUNK_SIZE capability, which can only be enabled when all memslots are empty.

Quoted 7 lines

I suggest doing that at kvm_init_stage2_mmu(), and checking if HDBSS is in which case we set mmu->split_page_chunk_size to PAGESIZE.

I will send a patch you can put before this one to make sure it works :)

Thanks!
Leo

Hi Leo,

Thanks for the helpful suggestion. I had previously traced the hugepage-splitting path

during live migration and found that when migration starts, enabling dirty logging

triggers the splitting path. I also tested HDBSS with traditional hugepages and haven't

observed any issues yet.

However, your concern is valid — there may be cases not covered, especially when the

VMM uses transparent hugepages. I'll integrate your patch into the next version and

run some tests.

For reference, here's the path I traced:

```

- userspace, e.g., QEMU

kvm_log_start

+-> kvm_section_update_flags
     +-> kvm_slot_update_flags
         |
         | // For each memory region, QEMU issues a
KVM_SET_USER_MEMORY_REGION ioctl.
         | // Before issuing it, flags are updated to include
KVM_MEM_LOG_DIRTY_PAGES.
         +-> kvm_mem_flags
         +-> kvm_set_user_memory_region   // ioctl that enables dirty logging

on the memslot

- KVM

KVM_SET_USER_MEMORY_REGION

Quoted 8 lines
+-> kvm_vm_ioctl_set_memory_region
     +-> kvm_set_memory_region / __kvm_set_memory_region
         +-> kvm_set_memslot
             +-> kvm_commit_memory_region
                 +-> kvm_arch_commit_memory_region
                     +-> kvm_mmu_split_memory_region

                        // Splits Stage-2 hugepages/contiguous mappings into
4KB PTEs.

Right, except on a case we have dirty_log_manual_protect and init_set, when it returns before splitting pages:

```
if (kvm_dirty_log_manual_protect_and_init_set(kvm))
	return;
```

IIUC, that's desired to avoid holding the lock for a long time while it cleans every page in the beginning, and instead do it in a per dirty-page basis. I guess it may benefit guests with very little dirty pages, as it does not have to split/dirty everything at the start.
(Its a pain for my HACDBS routines, though)

Quoted 1 line

                        +-> kvm_mmu_split_huge_pages

Other important point here:
You can see in this function it skips splitting if chunk_size == 0. This value is set by a capability that configures EAGER_SPLIT, meaning splitting before the guest have write faults, which is nice as the write-fault is faster.

Two points in this capability:
- It's optional, if it's not set, only on-demand splitting (on fault) will happen, and since HDBSS removes the write-fault, we have no splitting
- It can be set to any valid block size, not only 4K, nor PAGE_SIZE, it can be set to PMD_SIZE, PUD_SIZE, and so on, which will depend on the PAGE_SIZE the kernel was compiled to.
That's only some points to keep in mind :)

		if (kvm_dirty_log_manual_protect_and_init_set(kvm))
			return;
Quoted 8 lines
Quoted 6 lines

                            +-> kvm_pgtable_stage2_split

```

Thanks again for the detailed explanation and for sending the patch.

Thank you for the collaboration on this!
Leo

Quoted 6 lines

Thanks for the detailed explanation — very helpful. My earlier tests missed cases like lazy splitting

and manual‑protect mode, and your patch addresses them perfectly.

I'll adopt it in the next version and test the corner cases you mentioned.

Awesome, thanks!
Leo

#34 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 12 lines · Leonardo Bras
Quoted 11 lines · Tian Zheng
Quoted 9 lines · Leonardo Bras
Quoted 8 lines · Tian Zheng
Quoted 7 lines · Leonardo Bras

Hello Tian,

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

(see below)

Of course! Happy to exchange ideas and learn together.

:)

Hello Tian,

On the above, HACDBS depends on HACDBSIRQ which can be announced by either device-tree or ACPI.

Do you think it's ok for it to be ACPI only, for now?

Thanks!
Leo

author
#35 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 17 lines · Leonardo Bras
Quoted 6 lines · Leonardo Bras
Quoted 5 lines · Tian Zheng
Quoted 4 lines · Leonardo Bras
Quoted 3 lines · Tian Zheng
Quoted 2 lines · Leonardo Bras

Hello Tian,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

(see below)

Of course! Happy to exchange ideas and learn together.

:)

Hello Tian,

Quoted 7 lines

On the above, HACDBS depends on HACDBSIRQ which can be announced by either device-tree or ACPI.

Do you think it's ok for it to be ACPI only, for now?

Thanks!
Leo

Hi Leo,

No problem with ACPI-only for HACDBSIRQ for now.

Feel free to let me know if you have any questions while working on HACDBS on top of HDBSS.

Tian

#36 lore raw

Re: [PATCH v3 4/5] KVM: arm64: Enable HDBSS support and handle HDBSSF events

Quoted 22 lines · Tian Zheng
Quoted 20 lines · Leonardo Bras
Quoted 6 lines · Leonardo Bras
Quoted 5 lines · Tian Zheng
Quoted 4 lines · Leonardo Bras
Quoted 3 lines · Tian Zheng
Quoted 2 lines · Leonardo Bras

Hello Tian,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines

I am currently working on HACDBS enablement(which will be rebased on top of this patchset) and due to the fact HACDBS and HDBSS are kind of complementary I will sometimes come with some questions for issues I have faced myself on that part. :)

(see below)

Of course! Happy to exchange ideas and learn together.

:)

Hello Tian,

On the above, HACDBS depends on HACDBSIRQ which can be announced by either device-tree or ACPI.

Quoted 8 lines
Quoted 4 lines

Do you think it's ok for it to be ACPI only, for now?

Thanks!
Leo

Hi Leo,

No problem with ACPI-only for HACDBSIRQ for now.

Awesome!
I got it all working on my side, so I will try to send a freestanding patch with the required needs from HDBSS :)

Quoted 4 lines

Feel free to let me know if you have any questions while working on HACDBS on top of HDBSS.

Will do:)

Thanks!
Leo

Quoted 2 lines

Tian

author
#37 lore raw

PATCH v3 5/5 KVM: arm64: Document HDBSS ioctl

In reply to Tian Zheng

A new ioctl (KVM_CAP_ARM_HW_DIRTY_STATE_TRACK) provides a mechanism for userspace to configure the HDBSS buffer size during live migration, enabling hardware-assisted dirty page tracking.

---
 Documentation/virt/kvm/api.rst | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
Documentation/virt/kvm/api.rst +16 -0
index fc5736839edd..2b5531d40d02 100644
@@ -8896,6 +8896,22 @@ helpful if user space wants to emulate instructions which are not
 This capability can be enabled dynamically even if VCPUs were already
 created and are running.
 
+7.47 KVM_CAP_ARM_HW_DIRTY_STATE_TRACK
+----------------------------
+
+:Architectures: arm64
+:Type: VM
+:Parameters: args[0] is the allocation order determining HDBSS buffer size
+             args[1] is 0 to disable, 1 to enable HDBSS
+:Returns: 0 on success, negative value on failure
+
+Enables hardware-assisted dirty page tracking via the Hardware Dirty State
+Tracking Structure (HDBSS).
+
+When live migration is initiated, userspace can enable this feature by
+setting KVM_CAP_ARM_HW_DIRTY_STATE_TRACK through IOCTL. KVM will allocate
+per-vCPU HDBSS buffers.
+
 8. Other capabilities.
 ======================
 

--
2.33.0

#38 lore raw
In reply to Tian Zheng
Quoted 62 lines · Tian Zheng

This series of patches add support to the Hardware Dirty state tracking Structure(HDBSS) feature, which is introduced by the ARM architecture in the DDI0601(ID121123) version.

The HDBSS feature is an extension to the architecture that enhances tracking translation table descriptors' dirty state, identified as FEAT_HDBSS. This feature utilizes hardware assistance to achieve dirty page tracking, aiming to significantly reduce the overhead of scanning for dirty pages.

The purpose of this feature is to make the execution overhead of live migration lower to both the guest and the host, compared to existing approaches (write-protect or search stage 2 tables).

After these patches, users(such as qemu) can use the KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl to enable or disable the HDBSS feature before and after the live migration.

v2:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

v2->v3 changes:
- Remove the ARM64_HDBSS configuration option and ensure this feature is only enabled in VHE mode.
- Move HDBSS-related variables to the arch-independent portion of the kvm structure.
- Remove error messages during HDBSS enable/disable operations
- Change HDBSS buffer flushing from handle_exit to vcpu_put, check_vcpu_requests, and kvm_handle_guest_abort.
- Add fault handling for HDBSS including buffer full, external abort, and general protection fault (GPF).
- Add support for a 4KB HDBSS buffer size, mapped to the value 0b0000.
- Add a second argument to the ioctl to turn HDBSS on or off.

Tian Zheng (1):
KVM: arm64: Document HDBSS ioctl

eillon (4):
arm64/sysreg: Add HDBSS related register information
KVM: arm64: Add support to set the DBM attr during memory abort
KVM: arm64: Add support for FEAT_HDBSS
KVM: arm64: Enable HDBSS support and handle HDBSSF events

 Documentation/virt/kvm/api.rst       |  16 +++++
 arch/arm64/include/asm/cpufeature.h  |   5 ++
 arch/arm64/include/asm/esr.h         |   7 ++
 arch/arm64/include/asm/kvm_host.h    |  17 +++++
 arch/arm64/include/asm/kvm_mmu.h     |   1 +
 arch/arm64/include/asm/kvm_pgtable.h |   4 ++
 arch/arm64/include/asm/sysreg.h      |  11 +++
 arch/arm64/kernel/cpufeature.c       |  12 ++++
 arch/arm64/kvm/arm.c                 | 102 +++++++++++++++++++++++++++
 arch/arm64/kvm/hyp/pgtable.c         |   6 ++
 arch/arm64/kvm/hyp/vhe/switch.c      |  19 +++++
 arch/arm64/kvm/mmu.c                 |  70 ++++++++++++++++++
 arch/arm64/kvm/reset.c               |   3 +
 arch/arm64/tools/cpucaps             |   1 +
 arch/arm64/tools/sysreg              |  29 ++++++++
 include/uapi/linux/kvm.h             |   1 +
 tools/include/uapi/linux/kvm.h       |   1 +
 17 files changed, 305 insertions(+)
Quoted 3 lines

--
2.33.0

Hi Tian,

I was thinking: maybe instead of putting the HDBSS (and HACDBS) stuff across a bunch of KVM files, we should try to focus them all on a single arch/arm64/kvm/dirty_bit.c file (plus a header such as arch/arm64/include/asm/kvm_dirty_bit.h).

What is your opinion on that?

Thanks!
Leo

author
#39 lore raw
Quoted 74 lines · Leonardo Bras
Quoted 62 lines · Tian Zheng

This series of patches add support to the Hardware Dirty state tracking Structure(HDBSS) feature, which is introduced by the ARM architecture in the DDI0601(ID121123) version.

The HDBSS feature is an extension to the architecture that enhances tracking translation table descriptors' dirty state, identified as FEAT_HDBSS. This feature utilizes hardware assistance to achieve dirty page tracking, aiming to significantly reduce the overhead of scanning for dirty pages.

The purpose of this feature is to make the execution overhead of live migration lower to both the guest and the host, compared to existing approaches (write-protect or search stage 2 tables).

After these patches, users(such as qemu) can use the KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl to enable or disable the HDBSS feature before and after the live migration.

v2:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

v2->v3 changes:
- Remove the ARM64_HDBSS configuration option and ensure this feature is only enabled in VHE mode.
- Move HDBSS-related variables to the arch-independent portion of the kvm structure.
- Remove error messages during HDBSS enable/disable operations
- Change HDBSS buffer flushing from handle_exit to vcpu_put, check_vcpu_requests, and kvm_handle_guest_abort.
- Add fault handling for HDBSS including buffer full, external abort, and general protection fault (GPF).
- Add support for a 4KB HDBSS buffer size, mapped to the value 0b0000.
- Add a second argument to the ioctl to turn HDBSS on or off.

Tian Zheng (1):
KVM: arm64: Document HDBSS ioctl

eillon (4):
arm64/sysreg: Add HDBSS related register information
KVM: arm64: Add support to set the DBM attr during memory abort
KVM: arm64: Add support for FEAT_HDBSS
KVM: arm64: Enable HDBSS support and handle HDBSSF events

  Documentation/virt/kvm/api.rst       |  16 +++++
  arch/arm64/include/asm/cpufeature.h  |   5 ++
  arch/arm64/include/asm/esr.h         |   7 ++
  arch/arm64/include/asm/kvm_host.h    |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h     |   1 +
  arch/arm64/include/asm/kvm_pgtable.h |   4 ++
  arch/arm64/include/asm/sysreg.h      |  11 +++
  arch/arm64/kernel/cpufeature.c       |  12 ++++
  arch/arm64/kvm/arm.c                 | 102 +++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/pgtable.c         |   6 ++
  arch/arm64/kvm/hyp/vhe/switch.c      |  19 +++++
  arch/arm64/kvm/mmu.c                 |  70 ++++++++++++++++++
  arch/arm64/kvm/reset.c               |   3 +
  arch/arm64/tools/cpucaps             |   1 +
  arch/arm64/tools/sysreg              |  29 ++++++++
  include/uapi/linux/kvm.h             |   1 +
  tools/include/uapi/linux/kvm.h       |   1 +
  17 files changed, 305 insertions(+)
Quoted 3 lines

--
2.33.0

Hi Tian,

I was thinking: maybe instead of putting the HDBSS (and HACDBS) stuff across a bunch of KVM files, we should try to focus them all on a single arch/arm64/kvm/dirty_bit.c file (plus a header such as arch/arm64/include/asm/kvm_dirty_bit.h).

Quoted 4 lines

What is your opinion on that?

Thanks!
Leo

Sorry for the late reply. Yes, I had the same thought before. In the next version, I will

move all the HDBSS-related content into the same file, such as arch/arm64/kvm/dirty_bit.c

and arch/arm64/include/asm/kvm_dirty_bit.h.

Tian

Quoted 2 lines
#40 lore raw
Quoted 85 lines · Tian Zheng
Quoted 74 lines · Leonardo Bras
Quoted 62 lines · Tian Zheng

This series of patches add support to the Hardware Dirty state tracking Structure(HDBSS) feature, which is introduced by the ARM architecture in the DDI0601(ID121123) version.

The HDBSS feature is an extension to the architecture that enhances tracking translation table descriptors' dirty state, identified as FEAT_HDBSS. This feature utilizes hardware assistance to achieve dirty page tracking, aiming to significantly reduce the overhead of scanning for dirty pages.

The purpose of this feature is to make the execution overhead of live migration lower to both the guest and the host, compared to existing approaches (write-protect or search stage 2 tables).

After these patches, users(such as qemu) can use the KVM_CAP_ARM_HW_DIRTY_STATE_TRACK ioctl to enable or disable the HDBSS feature before and after the live migration.

v2:
https://lore.kernel.org/linux-arm-kernel/[email protected]/

v2->v3 changes:
- Remove the ARM64_HDBSS configuration option and ensure this feature is only enabled in VHE mode.
- Move HDBSS-related variables to the arch-independent portion of the kvm structure.
- Remove error messages during HDBSS enable/disable operations
- Change HDBSS buffer flushing from handle_exit to vcpu_put, check_vcpu_requests, and kvm_handle_guest_abort.
- Add fault handling for HDBSS including buffer full, external abort, and general protection fault (GPF).
- Add support for a 4KB HDBSS buffer size, mapped to the value 0b0000.
- Add a second argument to the ioctl to turn HDBSS on or off.

Tian Zheng (1):
KVM: arm64: Document HDBSS ioctl

eillon (4):
arm64/sysreg: Add HDBSS related register information
KVM: arm64: Add support to set the DBM attr during memory abort
KVM: arm64: Add support for FEAT_HDBSS
KVM: arm64: Enable HDBSS support and handle HDBSSF events

  Documentation/virt/kvm/api.rst       |  16 +++++
  arch/arm64/include/asm/cpufeature.h  |   5 ++
  arch/arm64/include/asm/esr.h         |   7 ++
  arch/arm64/include/asm/kvm_host.h    |  17 +++++
  arch/arm64/include/asm/kvm_mmu.h     |   1 +
  arch/arm64/include/asm/kvm_pgtable.h |   4 ++
  arch/arm64/include/asm/sysreg.h      |  11 +++
  arch/arm64/kernel/cpufeature.c       |  12 ++++
  arch/arm64/kvm/arm.c                 | 102 +++++++++++++++++++++++++++
  arch/arm64/kvm/hyp/pgtable.c         |   6 ++
  arch/arm64/kvm/hyp/vhe/switch.c      |  19 +++++
  arch/arm64/kvm/mmu.c                 |  70 ++++++++++++++++++
  arch/arm64/kvm/reset.c               |   3 +
  arch/arm64/tools/cpucaps             |   1 +
  arch/arm64/tools/sysreg              |  29 ++++++++
  include/uapi/linux/kvm.h             |   1 +
  tools/include/uapi/linux/kvm.h       |   1 +
  17 files changed, 305 insertions(+)
Quoted 3 lines

--
2.33.0

Hi Tian,

I was thinking: maybe instead of putting the HDBSS (and HACDBS) stuff across a bunch of KVM files, we should try to focus them all on a single arch/arm64/kvm/dirty_bit.c file (plus a header such as arch/arm64/include/asm/kvm_dirty_bit.h).

Quoted 4 lines

What is your opinion on that?

Thanks!
Leo

Sorry for the late reply. Yes, I had the same thought before. In the next version, I will

Quoted 11 lines

move all the HDBSS-related content into the same file, such as arch/arm64/kvm/dirty_bit.c

and arch/arm64/include/asm/kvm_dirty_bit.h.

Tian

Quoted 2 lines

Awesome! Then I will work my HACDBS enablement that way as well.

Thanks!
Leo

maintainer
#41 lore raw
In reply to Tian Zheng
Quoted 11 lines · Tian Zheng

This series of patches add support to the Hardware Dirty state tracking Structure(HDBSS) feature, which is introduced by the ARM architecture in the DDI0601(ID121123) version.

The HDBSS feature is an extension to the architecture that enhances tracking translation table descriptors' dirty state, identified as FEAT_HDBSS. This feature utilizes hardware assistance to achieve dirty page tracking, aiming to significantly reduce the overhead of scanning for dirty pages.

[...]

Applied sysreg definitions to arm64 (for-next/sysregs), thanks!

[1/5] arm64/sysreg: Add HDBSS related register information

      https://git.kernel.org/arm64/c/72f7be0c2e30

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev
#42 lore raw
Quoted 11 lines · Will Deacon
Quoted 10 lines · Tian Zheng

This series of patches add support to the Hardware Dirty state tracking Structure(HDBSS) feature, which is introduced by the ARM architecture in the DDI0601(ID121123) version.

The HDBSS feature is an extension to the architecture that enhances tracking translation table descriptors' dirty state, identified as FEAT_HDBSS. This feature utilizes hardware assistance to achieve dirty page tracking, aiming to significantly reduce the overhead of scanning for dirty pages.

Quoted 7 lines
Quoted 1 line

[...]

Applied sysreg definitions to arm64 (for-next/sysregs), thanks!

[1/5] arm64/sysreg: Add HDBSS related register information

      https://git.kernel.org/arm64/c/72f7be0c2e30

Thanks!
Leo

Quoted 7 lines

Cheers,

Signature · 5 lines
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev
author
#43 lore raw
In reply to Will Deacon
Quoted 11 lines · Will Deacon
Quoted 10 lines · Tian Zheng

This series of patches add support to the Hardware Dirty state tracking Structure(HDBSS) feature, which is introduced by the ARM architecture in the DDI0601(ID121123) version.

The HDBSS feature is an extension to the architecture that enhances tracking translation table descriptors' dirty state, identified as FEAT_HDBSS. This feature utilizes hardware assistance to achieve dirty page tracking, aiming to significantly reduce the overhead of scanning for dirty pages.

Quoted 8 lines
Quoted 1 line

[...]

Applied sysreg definitions to arm64 (for-next/sysregs), thanks!

[1/5] arm64/sysreg: Add HDBSS related register information

       https://git.kernel.org/arm64/c/72f7be0c2e30

Cheers,

Thanks!
Tian