Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Collect guest/host statistics during the redirected traps
@ 2024-12-24 21:04 Atish Patra
  2024-12-24 21:04 ` [PATCH v2 1/3] RISC-V: KVM: Redirect instruction access fault trap to guest Atish Patra
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Atish Patra @ 2024-12-24 21:04 UTC (permalink / raw)
  To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt
  Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra, Quan Zhou

As discussed in the patch[1], this series adds the host statistics for
traps that are redirected to the guest. Since there are 1-1 mapping for
firmware counters as well, this series enables those so that the guest
can collect information about these exits via perf if required.

I have included the patch[1] as well in this series as it has not been
applied and there will be likely conflicts while merging both.

Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
Changes in v2:
- Improved commit messages in PATCH3. 
- Added RB tags.
- Link to v1: https://lore.kernel.org/r/20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@rivosinc.com

---
Atish Patra (2):
      RISC-V: KVM: Update firmware counters for various events
      RISC-V: KVM: Add new exit statstics for redirected traps

Quan Zhou (1):
      RISC-V: KVM: Redirect instruction access fault trap to guest

 arch/riscv/include/asm/kvm_host.h |  5 +++++
 arch/riscv/kvm/vcpu.c             |  7 ++++++-
 arch/riscv/kvm/vcpu_exit.c        | 37 +++++++++++++++++++++++++++++++++----
 3 files changed, 44 insertions(+), 5 deletions(-)
---
base-commit: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4
change-id: 20241212-kvm_guest_stat-bc469665b410
--
Regards,
Atish patra


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/3] RISC-V: KVM: Redirect instruction access fault trap to guest
  2024-12-24 21:04 [PATCH v2 0/3] Collect guest/host statistics during the redirected traps Atish Patra
@ 2024-12-24 21:04 ` Atish Patra
  2024-12-24 21:04 ` [PATCH v2 2/3] RISC-V: KVM: Update firmware counters for various events Atish Patra
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2024-12-24 21:04 UTC (permalink / raw)
  To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt
  Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra, Quan Zhou

From: Quan Zhou <zhouquan@iscas.ac.cn>

The M-mode redirects an unhandled instruction access
fault trap back to S-mode when not delegating it to
VS-mode(hedeleg). However, KVM running in HS-mode
terminates the VS-mode software when back from M-mode.

The KVM should redirect the trap back to VS-mode, and
let VS-mode trap handler decide the next step.

Signed-off-by: Quan Zhou <zhouquan@iscas.ac.cn>
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
 arch/riscv/kvm/vcpu_exit.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c
index fa98e5c024b2..c9f8b2094554 100644
--- a/arch/riscv/kvm/vcpu_exit.c
+++ b/arch/riscv/kvm/vcpu_exit.c
@@ -187,6 +187,7 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	case EXC_STORE_MISALIGNED:
 	case EXC_LOAD_ACCESS:
 	case EXC_STORE_ACCESS:
+	case EXC_INST_ACCESS:
 		if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
 			kvm_riscv_vcpu_trap_redirect(vcpu, trap);
 			ret = 1;

-- 
2.34.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/3] RISC-V: KVM: Update firmware counters for various events
  2024-12-24 21:04 [PATCH v2 0/3] Collect guest/host statistics during the redirected traps Atish Patra
  2024-12-24 21:04 ` [PATCH v2 1/3] RISC-V: KVM: Redirect instruction access fault trap to guest Atish Patra
@ 2024-12-24 21:04 ` Atish Patra
  2024-12-24 21:04 ` [PATCH v2 3/3] RISC-V: KVM: Add new exit statstics for redirected traps Atish Patra
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2024-12-24 21:04 UTC (permalink / raw)
  To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt
  Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra

SBI PMU specification defines few firmware counters which can be
used by the guests to collect the statstics about various traps
occurred in the host.

Update these counters whenever a corresponding trap is taken

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
 arch/riscv/kvm/vcpu_exit.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c
index c9f8b2094554..acdcd619797e 100644
--- a/arch/riscv/kvm/vcpu_exit.c
+++ b/arch/riscv/kvm/vcpu_exit.c
@@ -165,6 +165,17 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu,
 	vcpu->arch.guest_context.sstatus |= SR_SPP;
 }
 
+static inline int vcpu_redirect(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap)
+{
+	int ret = -EFAULT;
+
+	if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
+		kvm_riscv_vcpu_trap_redirect(vcpu, trap);
+		ret = 1;
+	}
+	return ret;
+}
+
 /*
  * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
  * proper exit to userspace.
@@ -183,15 +194,27 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	run->exit_reason = KVM_EXIT_UNKNOWN;
 	switch (trap->scause) {
 	case EXC_INST_ILLEGAL:
+		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ILLEGAL_INSN);
+		ret = vcpu_redirect(vcpu, trap);
+		break;
 	case EXC_LOAD_MISALIGNED:
+		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_LOAD);
+		ret = vcpu_redirect(vcpu, trap);
+		break;
 	case EXC_STORE_MISALIGNED:
+		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_STORE);
+		ret = vcpu_redirect(vcpu, trap);
+		break;
 	case EXC_LOAD_ACCESS:
+		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_LOAD);
+		ret = vcpu_redirect(vcpu, trap);
+		break;
 	case EXC_STORE_ACCESS:
+		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_STORE);
+		ret = vcpu_redirect(vcpu, trap);
+		break;
 	case EXC_INST_ACCESS:
-		if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) {
-			kvm_riscv_vcpu_trap_redirect(vcpu, trap);
-			ret = 1;
-		}
+		ret = vcpu_redirect(vcpu, trap);
 		break;
 	case EXC_VIRTUAL_INST_FAULT:
 		if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV)

-- 
2.34.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/3] RISC-V: KVM: Add new exit statstics for redirected traps
  2024-12-24 21:04 [PATCH v2 0/3] Collect guest/host statistics during the redirected traps Atish Patra
  2024-12-24 21:04 ` [PATCH v2 1/3] RISC-V: KVM: Redirect instruction access fault trap to guest Atish Patra
  2024-12-24 21:04 ` [PATCH v2 2/3] RISC-V: KVM: Update firmware counters for various events Atish Patra
@ 2024-12-24 21:04 ` Atish Patra
  2024-12-26  5:44 ` [PATCH v2 0/3] Collect guest/host statistics during the " Anup Patel
  2025-02-03 19:15 ` patchwork-bot+linux-riscv
  4 siblings, 0 replies; 6+ messages in thread
From: Atish Patra @ 2024-12-24 21:04 UTC (permalink / raw)
  To: Anup Patel, Atish Patra, Paul Walmsley, Palmer Dabbelt
  Cc: kvm, kvm-riscv, linux-riscv, linux-kernel, Atish Patra

Currently, kvm doesn't delegate the few traps such as misaligned
load/store, illegal instruction and load/store access faults because it
is not expected to occur in the guest very frequently. Thus, kvm gets a
chance to act upon it or collect statistics about it before redirecting
the traps to the guest.

Collect both guest and host visible statistics during the traps.
Enable them so that both guest and host can collect the stats about
them if required.

Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Atish Patra <atishp@rivosinc.com>
---
 arch/riscv/include/asm/kvm_host.h | 5 +++++
 arch/riscv/kvm/vcpu.c             | 7 ++++++-
 arch/riscv/kvm/vcpu_exit.c        | 5 +++++
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h
index 35eab6e0f4ae..cc33e35cd628 100644
--- a/arch/riscv/include/asm/kvm_host.h
+++ b/arch/riscv/include/asm/kvm_host.h
@@ -87,6 +87,11 @@ struct kvm_vcpu_stat {
 	u64 csr_exit_kernel;
 	u64 signal_exits;
 	u64 exits;
+	u64 instr_illegal_exits;
+	u64 load_misaligned_exits;
+	u64 store_misaligned_exits;
+	u64 load_access_exits;
+	u64 store_access_exits;
 };
 
 struct kvm_arch_memory_slot {
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index e048dcc6e65e..60d684c76c58 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -34,7 +34,12 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
 	STATS_DESC_COUNTER(VCPU, csr_exit_user),
 	STATS_DESC_COUNTER(VCPU, csr_exit_kernel),
 	STATS_DESC_COUNTER(VCPU, signal_exits),
-	STATS_DESC_COUNTER(VCPU, exits)
+	STATS_DESC_COUNTER(VCPU, exits),
+	STATS_DESC_COUNTER(VCPU, instr_illegal_exits),
+	STATS_DESC_COUNTER(VCPU, load_misaligned_exits),
+	STATS_DESC_COUNTER(VCPU, store_misaligned_exits),
+	STATS_DESC_COUNTER(VCPU, load_access_exits),
+	STATS_DESC_COUNTER(VCPU, store_access_exits),
 };
 
 const struct kvm_stats_header kvm_vcpu_stats_header = {
diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c
index acdcd619797e..6e0c18412795 100644
--- a/arch/riscv/kvm/vcpu_exit.c
+++ b/arch/riscv/kvm/vcpu_exit.c
@@ -195,22 +195,27 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
 	switch (trap->scause) {
 	case EXC_INST_ILLEGAL:
 		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ILLEGAL_INSN);
+		vcpu->stat.instr_illegal_exits++;
 		ret = vcpu_redirect(vcpu, trap);
 		break;
 	case EXC_LOAD_MISALIGNED:
 		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_LOAD);
+		vcpu->stat.load_misaligned_exits++;
 		ret = vcpu_redirect(vcpu, trap);
 		break;
 	case EXC_STORE_MISALIGNED:
 		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_STORE);
+		vcpu->stat.store_misaligned_exits++;
 		ret = vcpu_redirect(vcpu, trap);
 		break;
 	case EXC_LOAD_ACCESS:
 		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_LOAD);
+		vcpu->stat.load_access_exits++;
 		ret = vcpu_redirect(vcpu, trap);
 		break;
 	case EXC_STORE_ACCESS:
 		kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_STORE);
+		vcpu->stat.store_access_exits++;
 		ret = vcpu_redirect(vcpu, trap);
 		break;
 	case EXC_INST_ACCESS:

-- 
2.34.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/3] Collect guest/host statistics during the redirected traps
  2024-12-24 21:04 [PATCH v2 0/3] Collect guest/host statistics during the redirected traps Atish Patra
                   ` (2 preceding siblings ...)
  2024-12-24 21:04 ` [PATCH v2 3/3] RISC-V: KVM: Add new exit statstics for redirected traps Atish Patra
@ 2024-12-26  5:44 ` Anup Patel
  2025-02-03 19:15 ` patchwork-bot+linux-riscv
  4 siblings, 0 replies; 6+ messages in thread
From: Anup Patel @ 2024-12-26  5:44 UTC (permalink / raw)
  To: Atish Patra
  Cc: Atish Patra, Paul Walmsley, Palmer Dabbelt, kvm, kvm-riscv,
	linux-riscv, linux-kernel, Quan Zhou

On Wed, Dec 25, 2024 at 2:34 AM Atish Patra <atishp@rivosinc.com> wrote:
>
> As discussed in the patch[1], this series adds the host statistics for
> traps that are redirected to the guest. Since there are 1-1 mapping for
> firmware counters as well, this series enables those so that the guest
> can collect information about these exits via perf if required.
>
> I have included the patch[1] as well in this series as it has not been
> applied and there will be likely conflicts while merging both.
>
> Signed-off-by: Atish Patra <atishp@rivosinc.com>
> ---
> Changes in v2:
> - Improved commit messages in PATCH3.
> - Added RB tags.
> - Link to v1: https://lore.kernel.org/r/20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@rivosinc.com
>
> ---
> Atish Patra (2):
>       RISC-V: KVM: Update firmware counters for various events
>       RISC-V: KVM: Add new exit statstics for redirected traps
>
> Quan Zhou (1):
>       RISC-V: KVM: Redirect instruction access fault trap to guest

Queued this series for Linux-6.14.

Thanks,
Anup

>
>  arch/riscv/include/asm/kvm_host.h |  5 +++++
>  arch/riscv/kvm/vcpu.c             |  7 ++++++-
>  arch/riscv/kvm/vcpu_exit.c        | 37 +++++++++++++++++++++++++++++++++----
>  3 files changed, 44 insertions(+), 5 deletions(-)
> ---
> base-commit: fac04efc5c793dccbd07e2d59af9f90b7fc0dca4
> change-id: 20241212-kvm_guest_stat-bc469665b410
> --
> Regards,
> Atish patra
>

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 0/3] Collect guest/host statistics during the redirected traps
  2024-12-24 21:04 [PATCH v2 0/3] Collect guest/host statistics during the redirected traps Atish Patra
                   ` (3 preceding siblings ...)
  2024-12-26  5:44 ` [PATCH v2 0/3] Collect guest/host statistics during the " Anup Patel
@ 2025-02-03 19:15 ` patchwork-bot+linux-riscv
  4 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+linux-riscv @ 2025-02-03 19:15 UTC (permalink / raw)
  To: Atish Kumar Patra
  Cc: linux-riscv, anup, atishp, paul.walmsley, palmer, kvm, kvm-riscv,
	linux-kernel, zhouquan

Hello:

This series was applied to riscv/linux.git (fixes)
by Anup Patel <anup@brainfault.org>:

On Tue, 24 Dec 2024 13:04:52 -0800 you wrote:
> As discussed in the patch[1], this series adds the host statistics for
> traps that are redirected to the guest. Since there are 1-1 mapping for
> firmware counters as well, this series enables those so that the guest
> can collect information about these exits via perf if required.
> 
> I have included the patch[1] as well in this series as it has not been
> applied and there will be likely conflicts while merging both.
> 
> [...]

Here is the summary with links:
  - [v2,1/3] RISC-V: KVM: Redirect instruction access fault trap to guest
    https://git.kernel.org/riscv/c/51c58956732b
  - [v2,2/3] RISC-V: KVM: Update firmware counters for various events
    https://git.kernel.org/riscv/c/2f15b5eaff79
  - [v2,3/3] RISC-V: KVM: Add new exit statstics for redirected traps
    https://git.kernel.org/riscv/c/af79caa83f6a

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-02-03 19:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-24 21:04 [PATCH v2 0/3] Collect guest/host statistics during the redirected traps Atish Patra
2024-12-24 21:04 ` [PATCH v2 1/3] RISC-V: KVM: Redirect instruction access fault trap to guest Atish Patra
2024-12-24 21:04 ` [PATCH v2 2/3] RISC-V: KVM: Update firmware counters for various events Atish Patra
2024-12-24 21:04 ` [PATCH v2 3/3] RISC-V: KVM: Add new exit statstics for redirected traps Atish Patra
2024-12-26  5:44 ` [PATCH v2 0/3] Collect guest/host statistics during the " Anup Patel
2025-02-03 19:15 ` patchwork-bot+linux-riscv

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox