Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Charlie Jenkins via B4 Relay"
	<devnull+thecharlesjenkins.gmail.com@kernel.org>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v2 12/16] riscv: kvm: Add emulated test csr
Date: Mon, 22 Jun 2026 04:23:25 +0000	[thread overview]
Message-ID: <20260622042326.46FFF1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260621-riscv_insn_table-v2-12-7f0810ee2274@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] The `vsscratch` test CSR emulation handler is structurally unreachable, rendering the feature dead code because accesses to VS-level CSRs from VS-mode do not trap to KVM.
- [High] The emulated test CSR unconditionally introduces a silently-on guest-visible feature and unmigratable state, violating KVM API invariants.
- [High] The patch hijacks the architectural `vsscratch` CSR, backing it with disconnected software state and causing split-brain register semantics.
- [Medium] The emulated test CSR state is not cleared during VCPU reset, leaking state across warm resets.
--

commit 783cfef4904ebb8469e4e71c63a55936a9c8e148
Author: Charlie Jenkins <thecharlesjenkins@gmail.com>

riscv: kvm: Add emulated test csr

This commit adds support to emulate the vsscratch CSR when
CONFIG_RISCV_KVM_TEST_CSR is set. It introduces a new kvm_test_csr
struct and hooks into the CSR instruction handling to reflect changes
when the vsscratch register is accessed.

> diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h
> index 75b0a951c1bc6..4d30d30ebefc7 100644
> --- a/arch/riscv/include/asm/kvm_host.h
> +++ b/arch/riscv/include/asm/kvm_host.h
[ ... ]
> @@ -265,6 +271,10 @@ struct kvm_vcpu_arch {
>  		gpa_t shmem;
>  		u64 last_steal;
>  	} sta;
> +
> +#ifdef CONFIG_RISCV_KVM_TEST_CSR
> +	struct kvm_test_csr test_csr;
> +#endif
>  };

[Severity: High]
Does this new guest-visible state need to be explicitly enabled by userspace
with a runtime capability check rather than just a compile-time config? 

Also, since this state isn't exposed via the KVM_GET_ONE_REG or KVM_SET_ONE_REG
API, will it break live migration because it is completely invisible to
userspace?

[Severity: Medium]
Is this test_csr state cleared during a VCPU reset? It looks like it is missing
from the architecture-specific reset path kvm_riscv_vcpu_context_reset(),
which might allow the modified value to leak across warm resets.

> diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
> index 1d8741d022425..c5a70de4a5790 100644
> --- a/arch/riscv/kvm/vcpu_insn.c
> +++ b/arch/riscv/kvm/vcpu_insn.c
[ ... ]
> @@ -112,6 +114,9 @@ static int seed_csr_rmw(struct kvm_vcpu *vcpu, unsigned int csr_num,
>  static const struct csr_func csr_funcs[] = {
>  	KVM_RISCV_VCPU_AIA_CSR_FUNCS
>  	KVM_RISCV_VCPU_HPMCOUNTER_CSR_FUNCS
> +#ifdef CONFIG_RISCV_KVM_TEST_CSR
> +	KVM_RISCV_VCPU_TEST_CSR_FUNCS
> +#endif
>  	{ .base = CSR_SEED, .count = 1, .func = seed_csr_rmw },
>  };

[Severity: High]
Will this CSR emulation handler ever be reached?

KVM's CSR emulation path kvm_riscv_vcpu_virtual_insn() is only invoked for
Virtual Instruction exceptions (EXC_VIRTUAL_INST_FAULT, cause 22). However,
accessing an HS-level CSR like vsscratch from VS-mode natively generates an
Illegal Instruction exception (EXC_INST_ILLEGAL, cause 2).

Since KVM explicitly delegates these exceptions directly to the guest's
VS-mode exception handler, it seems this handler gets bypassed entirely.

> diff --git a/arch/riscv/kvm/vcpu_test_csr.c b/arch/riscv/kvm/vcpu_test_csr.c
> new file mode 100644
> index 0000000000000..b8aa503cdaba7
> --- /dev/null
> +++ b/arch/riscv/kvm/vcpu_test_csr.c
> @@ -0,0 +1,21 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/kvm_host.h>
> +#include <asm/kvm_vcpu_insn.h>
> +#include <asm/kvm_vcpu_test_csr.h>
> +
> +#define vcpu_to_test_csr(vcpu) (&(vcpu)->arch.test_csr)
> +
> +int kvm_riscv_vcpu_test_csr(struct kvm_vcpu *vcpu, unsigned int csr_num,
> +			    unsigned long *val, unsigned long new_val,
> +			    unsigned long wr_mask)
> +{
> +	struct kvm_test_csr *test_csr = vcpu_to_test_csr(vcpu);
> +
> +	*val = test_csr->val;
> +
> +	if (wr_mask)
> +		test_csr->val = (test_csr->val & ~wr_mask) | (new_val & wr_mask);
> +
> +	return KVM_INSN_CONTINUE_NEXT_SEPC;
> +}

[Severity: High]
Does using the architectural vsscratch CSR for this test cause a split-brain
state?

In a RISC-V guest, accessing sscratch implicitly accesses the hardware
vsscratch register, which KVM context-switches via its standard guest context
structures.

By intercepting explicit guest accesses to the vsscratch address and directing
them to this disjoint test_csr.val variable, the guest will see different
values depending on whether it accesses the register implicitly via sscratch
or explicitly via vsscratch. Would it be safer for a test CSR to use a
reserved or custom CSR address?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260621-riscv_insn_table-v2-0-7f0810ee2274@gmail.com?part=12

  reply	other threads:[~2026-06-22  4:23 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-22  4:01 [PATCH v2 00/16] riscv: Generate riscv instruction functions Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 01/16] riscv: Introduce instruction table generation Charlie Jenkins via B4 Relay
2026-06-22  4:13   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 02/16] riscv: alternatives: Use generated instruction headers for patching code Charlie Jenkins via B4 Relay
2026-06-22  4:28   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 03/16] riscv: kgdb: Use generated instruction headers Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 04/16] riscv: Add kprobes instruction simulation KUnit Charlie Jenkins via B4 Relay
2026-06-22  4:19   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 05/16] riscv: kprobes: Use generated instruction headers Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 06/16] riscv: cfi: " Charlie Jenkins via B4 Relay
2026-06-22  4:35   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 07/16] riscv: Use generated instruction headers for misaligned loads/stores Charlie Jenkins via B4 Relay
2026-06-22  4:18   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 08/16] riscv: kvm: Use generated instruction headers for csr code Charlie Jenkins via B4 Relay
2026-06-22  4:18   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 09/16] KVM: device: Add test device Charlie Jenkins via B4 Relay
2026-06-22  4:13   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 10/16] KVM: riscv: selftests: Add mmio test Charlie Jenkins via B4 Relay
2026-06-22  4:18   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 11/16] riscv: kvm: Use generated instruction headers for mmio emulation Charlie Jenkins via B4 Relay
2026-06-22  4:27   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 12/16] riscv: kvm: Add emulated test csr Charlie Jenkins via B4 Relay
2026-06-22  4:23   ` sashiko-bot [this message]
2026-06-22  4:01 ` [PATCH v2 13/16] KVM: riscv: selftests: Add csr emulation test Charlie Jenkins via B4 Relay
2026-06-22  4:24   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 14/16] riscv: kvm: Use generated instruction headers for csr emulation Charlie Jenkins via B4 Relay
2026-06-22  4:30   ` sashiko-bot
2026-06-22  4:01 ` [PATCH v2 15/16] riscv: kexec: Use generated instruction headers for kexec relocations Charlie Jenkins via B4 Relay
2026-06-22  4:01 ` [PATCH v2 16/16] riscv: Remove unused instruction headers Charlie Jenkins via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260622042326.46FFF1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=devnull+thecharlesjenkins.gmail.com@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox