Skip to contentSkip to conversation

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

PATCH Superseded Inochi Amaoto 2 participants 8 messages Jul 6 – Jul 29

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover RISC-V: KVM: Cleanup SBI extension status when disable

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.

Change from v1:
- https://lore.kernel.org/kvm/[email protected]
patch 2:
1. Check support callback existed before using it.

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 v2 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
maintainer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] RISC-V: KVM: Add SBI extension validate callback

Quoted 9 lines · Inochi Amaoto

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.

LGTM.

Thanks,
Anup

Quoted 75 lines
---
 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 -1
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);
+       }
+}
--

2.55.0

#3 lore raw

PATCH v2 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..0aa1f9a57c90 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 || 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
maintainer Reviewed-by on v2 2/3
#4 lore raw

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

Quoted 5 lines · Inochi Amaoto

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

LGTM.

Thanks,
Anup

Quoted 50 lines
---
 arch/riscv/kvm/vcpu_sbi_fwft.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
arch/riscv/kvm/vcpu_sbi_fwft.c +25 -1
index bc514ae6521d..0aa1f9a57c90 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 || 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,
--

2.55.0

#5 lore raw

PATCH v2 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
maintainer
#6 lore raw

Re: [PATCH v2 3/3] RISC-V: KVM: Reset the SBI extension when disable it

Quoted 6 lines · 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

s/extenion/extension/

Quoted 20 lines

a clear context.

---
 arch/riscv/kvm/vcpu_sbi.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/riscv/kvm/vcpu_sbi.c +1 -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;

arch/riscv/kvm/vcpu_sbi.c static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu, · line 238

Quoted 9 lines
arch/riscv/kvm/vcpu_sbi.c +6 -0
@@ -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;

Let's not blindly set "csr_dirty" when any SBI extension is disabled, instead we should set "csr_dirtry" in kvm_sbi_ext_fwft_reset() (just like we do in kvm_sbi_ext_fwft_set_reg()).

Quoted 9 lines
arch/riscv/kvm/vcpu_sbi.c +3 -1
+               }
+       }
+
        scontext->ext_status[sext->ext_idx] = (reg_val) ?
                        KVM_RISCV_SBI_EXT_STATUS_ENABLED :
                        KVM_RISCV_SBI_EXT_STATUS_DISABLED;
--

2.55.0

Regards,
Anup

#7 lore raw

Re: [PATCH v2 3/3] RISC-V: KVM: Reset the SBI extension when disable it

Quoted 33 lines · Anup Patel
Quoted 6 lines · 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

s/extenion/extension/

Quoted 23 lines

a clear context.

---
 arch/riscv/kvm/vcpu_sbi.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/riscv/kvm/vcpu_sbi.c +1 -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;
 
Quoted 11 lines

arch/riscv/kvm/vcpu_sbi.c static int riscv_vcpu_set_sbi_ext_single(struct kvm_vcpu *vcpu, · line 238

Quoted 6 lines
arch/riscv/kvm/vcpu_sbi.c +6 -0
+       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;

Let's not blindly set "csr_dirty" when any SBI extension is disabled, instead we should set "csr_dirtry" in kvm_sbi_ext_fwft_reset() (just like we do in kvm_sbi_ext_fwft_set_reg()).

Good, I will move this line to the kvm_sbi_ext_fwft_reset() in the new version.

Quoted 10 lines
Quoted 9 lines
arch/riscv/kvm/vcpu_sbi.c +3 -1
+               }
+       }
+
        scontext->ext_status[sext->ext_idx] = (reg_val) ?
                        KVM_RISCV_SBI_EXT_STATUS_ENABLED :
                        KVM_RISCV_SBI_EXT_STATUS_DISABLED;
--

2.55.0

Quoted 3 lines

Regards,
Anup

Regards,
Inochi