Skip to contentSkip to conversation

RISC-V: KVM: Cleanup SBI extension status when disble

PATCH Superseded Inochi Amaoto 2 participants 5 messages Jul 1

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover RISC-V: KVM: Cleanup SBI extension status when disble

KVM SBI FWFT extension has multiple internal state can be programmed by the userspace. However, when the FWFT feature or the FWFT extension is disabled, the internal state will be kept as it is, which may cause some side effect for the vCPU, cleanup these state when a SBI extension or FWFT feature is disabled.

Inochi Amaoto (3):
RISC-V: KVM: Add SBI extension validate callback
RISC-V: KVM: Add SBI FWFT validation support
RISC-V: KVM: Reset the SBI extension when disable it

 arch/riscv/include/asm/kvm_vcpu_sbi.h |  4 ++++
 arch/riscv/kvm/vcpu_config.c          |  2 ++
 arch/riscv/kvm/vcpu_sbi.c             | 33 +++++++++++++++++++++++++++
 arch/riscv/kvm/vcpu_sbi_fwft.c        | 25 ++++++++++++++++++++
 4 files changed, 64 insertions(+)

--
2.55.0

#1 lore raw

PATCH 1/3 RISC-V: KVM: Add SBI extension validate callback

When user disable FWFT extension after setting any value of the FWFT feature, the state of vCPU will be broken since the value of disable FWFT feature is still functional.

Add the generic SBI extension validate callback so the FWFT extension can fix its parameters before the first run.

---
 arch/riscv/include/asm/kvm_vcpu_sbi.h |  4 ++++
 arch/riscv/kvm/vcpu_config.c          |  2 ++
 arch/riscv/kvm/vcpu_sbi.c             | 23 +++++++++++++++++++++++
 3 files changed, 29 insertions(+)
arch/riscv/include/asm/kvm_vcpu_sbi.h +4 -0
index c1a7e3b40d9c..f01a2860c751 100644
@@ -60,6 +60,9 @@ struct kvm_vcpu_sbi_extension {
 
 	void (*reset)(struct kvm_vcpu *vcpu);
 
+	/* Allow the extension to correct its parameters before the first run */
+	void (*validate)(struct kvm_vcpu *vcpu);
+
 	unsigned long state_reg_subtype;
 	unsigned long (*get_state_reg_count)(struct kvm_vcpu *vcpu);
 	int (*get_state_reg_id)(struct kvm_vcpu *vcpu, int index, u64 *reg_id);
@@ -93,6 +96,7 @@ int kvm_riscv_vcpu_sbi_ecall(struct kvm_vcpu *vcpu, struct kvm_run *run);
 void kvm_riscv_vcpu_sbi_init(struct kvm_vcpu *vcpu);
 void kvm_riscv_vcpu_sbi_deinit(struct kvm_vcpu *vcpu);
 void kvm_riscv_vcpu_sbi_reset(struct kvm_vcpu *vcpu);
+void kvm_riscv_vcpu_sbi_validate(struct kvm_vcpu *vcpu);
 
 #ifdef CONFIG_RISCV_SBI_V01
 extern const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_v01;
arch/riscv/kvm/vcpu_config.c +2 -0
index 238418fed2b9..b68aa830aaf5 100644
@@ -69,6 +69,8 @@ void kvm_riscv_vcpu_config_ran_once(struct kvm_vcpu *vcpu)
 
 	if (vcpu->guest_debug)
 		cfg->hedeleg &= ~BIT(EXC_BREAKPOINT);
+
+	kvm_riscv_vcpu_sbi_validate(vcpu);
 }
 
 void kvm_riscv_vcpu_config_load(struct kvm_vcpu *vcpu)
arch/riscv/kvm/vcpu_sbi.c +23 -0
index 46ab7b989432..b737e9a7a12a 100644
@@ -723,3 +723,26 @@ void kvm_riscv_vcpu_sbi_reset(struct kvm_vcpu *vcpu)
 		ext->reset(vcpu);
 	}
 }
+
+void kvm_riscv_vcpu_sbi_validate(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context;
+	const struct kvm_riscv_sbi_extension_entry *entry;
+	const struct kvm_vcpu_sbi_extension *ext;
+	int idx, i;
+
+	for (i = 0; i < ARRAY_SIZE(sbi_ext); i++) {
+		entry = &sbi_ext[i];
+		ext = entry->ext_ptr;
+		idx = entry->ext_idx;
+
+		if (idx < 0 || idx >= ARRAY_SIZE(scontext->ext_status))
+			continue;
+
+		if (scontext->ext_status[idx] != KVM_RISCV_SBI_EXT_STATUS_ENABLED ||
+		    !ext->validate)
+			continue;
+
+		ext->validate(vcpu);
+	}
+}
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/3 RISC-V: KVM: Add SBI FWFT validation support

In reply to Inochi Amaoto

Since the KVM SBI has extension parameters validation support, implement it for the SBI FWFT support.

---
 arch/riscv/kvm/vcpu_sbi_fwft.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
arch/riscv/kvm/vcpu_sbi_fwft.c +25 -0
index bc514ae6521d..74eab72b2dd9 100644
@@ -604,6 +604,30 @@ static void kvm_sbi_ext_fwft_reset(struct kvm_vcpu *vcpu)
 	}
 }
 
+static void kvm_sbi_ext_fwft_validate(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sbi_fwft *fwft = vcpu_to_fwft(vcpu);
+	const struct kvm_sbi_fwft_feature *feature;
+	struct kvm_sbi_fwft_config *conf;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(features); i++) {
+		feature = &features[i];
+		conf = &fwft->configs[i];
+		if (!conf->supported)
+			continue;
+
+		if (feature->supported(vcpu))
+			continue;
+
+		conf->enabled = false;
+		conf->flags = 0;
+
+		if (feature->reset)
+			feature->reset(vcpu);
+	}
+}
+
 static unsigned long kvm_sbi_ext_fwft_get_reg_count(struct kvm_vcpu *vcpu)
 {
 	unsigned long max_reg_count = sizeof(struct kvm_riscv_sbi_fwft) / sizeof(unsigned long);
@@ -756,6 +780,7 @@ const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_fwft = {
 	.init = kvm_sbi_ext_fwft_init,
 	.deinit = kvm_sbi_ext_fwft_deinit,
 	.reset = kvm_sbi_ext_fwft_reset,
+	.validate = kvm_sbi_ext_fwft_validate,
 	.state_reg_subtype = KVM_REG_RISCV_SBI_FWFT,
 	.get_state_reg_count = kvm_sbi_ext_fwft_get_reg_count,
 	.get_state_reg_id = kvm_sbi_ext_fwft_get_reg_id,
Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH 2/3] RISC-V: KVM: Add SBI FWFT validation support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missing NULL pointer check on the explicitly optional `feature->supported` callback leads to a…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing NULL pointer check on the explicitly optional feature->supported callback leads to a potential kernel panic. --

commit 0a44921a7fcd1601c393c31f9c36440ef03d03b8
Author: Inochi Amaoto <[email protected]>

RISC-V: KVM: Add SBI FWFT validation support

This commit adds SBI extension parameters validation for the SBI FWFT support in KVM, bringing it in line with other SBI extensions.

Quoted 4 lines
arch/riscv/kvm/vcpu_sbi_fwft.c +0 -0
index ab39ac464ffd8..14701a130fd56 100644

[ ... ]

Quoted 8 lines
arch/riscv/kvm/vcpu_sbi_fwft.c +4 -0
@@ -420,6 +420,30 @@ static void kvm_sbi_ext_fwft_reset(struct kvm_vcpu *vcpu)
 	}
 }
 
+static void kvm_sbi_ext_fwft_validate(struct kvm_vcpu *vcpu)
+{
+	struct kvm_sbi_fwft *fwft = vcpu_to_fwft(vcpu);
+	const struct kvm_sbi_fwft_feature *feature;

arch/riscv/kvm/vcpu_sbi_fwft.c static void kvm_sbi_ext_fwft_reset(struct kvm_vcpu *vcpu) · line 436

Quoted 10 lines
arch/riscv/kvm/vcpu_sbi_fwft.c +10 -0
+	struct kvm_sbi_fwft_config *conf;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(features); i++) {
+		feature = &features[i];
+		conf = &fwft->configs[i];
+		if (!conf->supported)
+			continue;
+
+		if (feature->supported(vcpu))

[Severity: Medium]
The struct kvm_sbi_fwft_feature documentation defines the supported callback as explicitly optional. If a future feature is added that omits this callback, could this lead to a null pointer dereference here since it is invoked without prior verification?

Other functions in this file, such as kvm_sbi_ext_fwft_get_reg_count, verify the callback exists by checking if (feature->supported) before calling it. Would it be safer to include a similar check in kvm_sbi_ext_fwft_validate?

Quoted 9 lines
arch/riscv/kvm/vcpu_sbi_fwft.c +9 -0
+			continue;
+
+		conf->enabled = false;
+		conf->flags = 0;
+
+		if (feature->reset)
+			feature->reset(vcpu);
+	}
+}

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

PATCH 3/3 RISC-V: KVM: Reset the SBI extension when disable it

In reply to Inochi Amaoto

The SBI extension validation callback can only fix the parameter if the extension is enabled. However, if the extension is disabled after modifty some parameters, the state of the extension is still broken.

Reset the extension when the extenion is disabled so it can have a clear context.

---
 arch/riscv/kvm/vcpu_sbi.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/riscv/kvm/vcpu_sbi.c +10 -0
index b737e9a7a12a..3eb7b7c57059 100644
@@ -221,6 +221,7 @@ static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu,
 {
 	struct kvm_vcpu_sbi_context *scontext = &vcpu->arch.sbi_context;
 	const struct kvm_riscv_sbi_extension_entry *sext;
+	const struct kvm_vcpu_sbi_extension *ext;
 
 	if (reg_val != 1 && reg_val != 0)
 		return -EINVAL;
@@ -229,6 +230,15 @@ static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu,
 	if (!sext || scontext->ext_status[sext->ext_idx] == KVM_RISCV_SBI_EXT_STATUS_UNAVAILABLE)
 		return -ENOENT;
 
+	ext = sext->ext_ptr;
+
+	if (!reg_val && scontext->ext_status[sext->ext_idx] == KVM_RISCV_SBI_EXT_STATUS_ENABLED) {
+		if (ext->reset) {
+			ext->reset(vcpu);
+			vcpu->arch.csr_dirty = true;
+		}
+	}
+
 	scontext->ext_status[sext->ext_idx] = (reg_val) ?
 			KVM_RISCV_SBI_EXT_STATUS_ENABLED :
 			KVM_RISCV_SBI_EXT_STATUS_DISABLED;
Signature · 1 line
2.55.0