All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: JinRui <jinrui@haiwei.tech>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH] KVM: selftests: riscv: Add lazy V extension enablement for guests
Date: Tue, 21 Jul 2026 10:25:22 +0000	[thread overview]
Message-ID: <20260721102522.AF8081F000E9@smtp.kernel.org> (raw)
In-Reply-To: <4FAAF34C966898C0+20260721101406.305108-1-jinrui@haiwei.tech>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Lazy V extension enablement causes an inescapable infinite exception loop on hosts without the V extension when an illegal instruction is executed.
- [Medium] Reading the live `sstatus` CSR instead of the preserved `regs->status` creates a TOCTOU vulnerability with nested exceptions.
--

--- Patch [1]: [PATCH] KVM: selftests: riscv: Add lazy V extension enablement for guests ---
commit 2d8f4dc253676991c8c0807d4dda2a90b9cca890
Author: jinrui <jinrui@haiwei.tech>

KVM: selftests: riscv: Add lazy V extension enablement for guests

This commit introduces lazy V extension enablement for guests in RISC-V KVM
selftests to handle compiler auto-vectorization. It replaces the default
unexpected trap handler with the full exception vector table and adds a check
in route_exception() to enable V and retry the instruction.

> diff --git a/tools/testing/selftests/kvm/lib/riscv/processor.c b/tools/testing/selftests/kvm/lib/riscv/processor.c
> index ded5429f34483..68577b2f48f4e 100644
> --- a/tools/testing/selftests/kvm/lib/riscv/processor.c
> +++ b/tools/testing/selftests/kvm/lib/riscv/processor.c
[ ... ]
> @@ -432,6 +438,28 @@ void route_exception(struct pt_regs *regs)
>  		ec = 0;
>  	}
>  
> +	/*
> +	 * Handle V (vector) extension lazy enablement before any
> +	 * registered handler. The compiler's default march may include
> +	 * V, and auto-vectorization generates vector instructions that
> +	 * trigger EXC_INST_ILLEGAL when VS (Vector Status) in sstatus
> +	 * is Off. Enable VS to Initial and re-execute the faulting
> +	 * instruction, mimicking what a real OS kernel does.
> +	 *
> +	 * This check runs before any test-registered handler, so tests
> +	 * that install their own EXC_INST_ILLEGAL handler (e.g.
> +	 * sbi_pmu_test) are not affected.
> +	 */
> +	if (!(regs->cause & CAUSE_IRQ_FLAG) && ec == EXC_INST_ILLEGAL) {
> +		unsigned long sstatus;
> +
> +		asm volatile("csrr %0, sstatus" : "=r" (sstatus));

[Severity: Medium]
Does reading the live sstatus CSR here instead of using the preserved
regs->status create an issue with nested exceptions?

If the prologue of route_exception() uses auto-vectorized instructions, it
traps. The nested trap fixes sstatus and returns, leaving the live sstatus.VS
altered to Initial. The outer handler would then read the modified live
sstatus instead of the original exception state in regs->status, skipping V
enablement for the original fault.

> +		if (!(sstatus & SR_VS)) {
> +			regs->status |= SR_VS_INITIAL;
> +			return;

[Severity: Medium]
Can this cause an infinite exception loop on hosts that don't support the V
extension?

If a test deliberately executes an illegal instruction (like sbi_pmu_test) on
hardware without V support, SR_VS is hardwired to 0. The code sets
SR_VS_INITIAL and returns to re-execute, but the instruction faults again with
VS=0, repeating infinitely.

> +		}
> +	}
> +
>  	if (handlers && handlers->exception_handlers[vector][ec])
>  		return handlers->exception_handlers[vector][ec](regs);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/4FAAF34C966898C0+20260721101406.305108-1-jinrui@haiwei.tech?part=1

  reply	other threads:[~2026-07-21 10:25 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-21 10:14 [PATCH] KVM: selftests: riscv: Add lazy V extension enablement for guests JinRui
2026-07-21 10:14 ` JinRui
2026-07-21 10:14 ` JinRui
2026-07-21 10:25 ` sashiko-bot [this message]
2026-07-21 10:57 ` [PATCH v2] " JinRui
2026-07-21 10:57   ` JinRui
2026-07-21 10:57   ` JinRui
2026-07-21 11:06   ` sashiko-bot
2026-07-21 11:24   ` [PATCH v3] " JinRui
2026-07-21 11:24     ` JinRui
2026-07-21 11:24     ` JinRui
2026-07-21 11:41     ` sashiko-bot

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=20260721102522.AF8081F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jinrui@haiwei.tech \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.