Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Charlie Jenkins" <thecharlesjenkins@gmail.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v3 12/17] riscv: kvm: Use generated instruction headers for mmio emulation
Date: Wed, 08 Jul 2026 04:01:31 +0000	[thread overview]
Message-ID: <20260708040131.94C7F1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707-riscv_insn_table-v3-12-3f8e5f75ba97@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Signed left shift leading to Undefined Behavior in `kvm_riscv_vcpu_mmio_return`.
- [High] Incorrect register extraction for RISC-V compressed load and store instructions in KVM MMIO emulation leads to guest state corruption.

Pre-existing issues:
- [High] The `mmio_test_destroy` function calls `kvm_io_bus_unregister_dev` without holding the required `kvm->slots_lock`.
--

--- Patch [12]: [PATCH v3 12/17] riscv: kvm: Use generated instruction headers for mmio emulation ---
commit 8d80090faef41d1000ad8ee985dcdaa3df7c12d3
Author: Charlie Jenkins <thecharlesjenkins@gmail.com>

riscv: kvm: Use generated instruction headers for mmio emulation

Migrate the mmio emulation code to use the generated instruction headers
instead of the hand-written instruction composition functions.

> diff --git a/arch/riscv/kvm/vcpu_insn.c b/arch/riscv/kvm/vcpu_insn.c
> index a1898ab8bbe85..78adc28308193 100644
> --- a/arch/riscv/kvm/vcpu_insn.c
> +++ b/arch/riscv/kvm/vcpu_insn.c

[ ... ]

> -	} else if ((insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD) {
> +	} else if (riscv_insn_is_c_ld(insn)) {
>  		len = 8;
>  		shift = 8 * (sizeof(ulong) - len);
> -		insn = RVC_RS2S(insn) << SH_RD;
> -	} else if ((insn & INSN_MASK_C_LDSP) == INSN_MATCH_C_LDSP &&
> -		   ((insn >> SH_RD) & 0x1f)) {
> +		rd = riscv_insn_c_ld_extract_xd(insn);

[Severity: High]
Does kvm_riscv_vcpu_mmio_load miss the +8 register offset for compressed
instructions?

The old code used RVC_RS2S which mapped the 3-bit compressed register field
to architectural registers x8-x15. Without adding this offset, won't this
corrupt guest state by targeting registers x0-x7 instead?

[ ... ]

> -	} else if ((insn & INSN_MASK_C_SD) == INSN_MATCH_C_SD) {
> +	} else if (riscv_insn_is_c_sd(insn)) {
>  		len = 8;
> -		data64 = GET_RS2S(insn, &vcpu->arch.guest_context);
> -	} else if ((insn & INSN_MASK_C_SDSP) == INSN_MATCH_C_SDSP &&
> -		   ((insn >> SH_RD) & 0x1f)) {
> +		rs2 = riscv_insn_c_sd_extract_xs2(insn);

[Severity: High]
Similarly here in kvm_riscv_vcpu_mmio_store, does riscv_insn_c_sd_extract_xs2
also need the +8 offset added to map to the correct source register?

[ ... ]

> +		*((ulong *)(&vcpu->arch.guest_context) + vcpu->arch.mmio_decode.rd) =
> +			(long)data << shift >> shift;

[Severity: High]
Can this introduce undefined behavior in kvm_riscv_vcpu_mmio_return from a
signed left shift?

If a guest reads a value with the high bit set (like 0xFF for a 1-byte read),
casting data to long before the shift evaluates to 255 << 56. This
overflows the maximum positive value for a 64-bit signed integer.

The previous code safely did:
    (long)((ulong)data << shift) >> shift
    
Could we keep the unsigned cast before the left shift?

>  	}
>  
>  done:

> diff --git a/virt/kvm/mmio_test.c b/virt/kvm/mmio_test.c
> index bd5f21a43ba87..b3fec6988333f 100644
> --- a/virt/kvm/mmio_test.c
> +++ b/virt/kvm/mmio_test.c

[ ... ]

>  static void mmio_test_destroy(struct kvm_device *dev)
>  {
> -	struct mmio_test *mmio_test = kvm_to_mmio_test_dev(dev);
> -
> -	kvm_io_bus_unregister_dev(dev->kvm, KVM_MMIO_BUS, &mmio_test->dev);
> +	kvm_io_bus_unregister_dev(dev->kvm, KVM_MMIO_BUS, &((struct mmio_test *)dev->private)->dev);

[Severity: High]
This is a pre-existing issue, but does mmio_test_destroy need to hold
dev->kvm->slots_lock around this call?

kvm_io_bus_unregister_dev has a lockdep_assert_held for kvm->slots_lock.
Since mmio_test_destroy is called during kvm_destroy_devices without
holding this lock, won't this trigger a kernel lockdep splat when
userspace closes the VM?

>  	kfree(dev->private);
>  	kfree(dev);
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260707-riscv_insn_table-v3-0-3f8e5f75ba97@gmail.com?part=12

  reply	other threads:[~2026-07-08  4:01 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08  3:34 [PATCH v3 00/17] riscv: Generate riscv instruction functions Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 01/17] riscv: Introduce instruction table generation Charlie Jenkins
2026-07-08  3:50   ` sashiko-bot
2026-07-09  6:23     ` Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 02/17] riscv: alternatives: Use generated instruction headers for patching code Charlie Jenkins
2026-07-08  3:45   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 03/17] riscv: kgdb: Use generated instruction headers Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 04/17] riscv: Add kprobes instruction simulation KUnit Charlie Jenkins
2026-07-08  3:51   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 05/17] riscv: kprobes: Use generated instruction headers Charlie Jenkins
2026-07-08  3:52   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 06/17] riscv: cfi: " Charlie Jenkins
2026-07-08  3:34 ` [PATCH v3 07/17] riscv: Maintain epc on misaligned emulation error Charlie Jenkins
2026-07-08  3:55   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 08/17] riscv: Use generated instruction headers for misaligned loads/stores Charlie Jenkins
2026-07-08  3:49   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 09/17] riscv: kvm: Use generated instruction headers for csr code Charlie Jenkins
2026-07-08  3:48   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 10/17] KVM: device: Add test device Charlie Jenkins
2026-07-08  3:48   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 11/17] KVM: riscv: selftests: Add mmio test Charlie Jenkins
2026-07-08  3:59   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 12/17] riscv: kvm: Use generated instruction headers for mmio emulation Charlie Jenkins
2026-07-08  4:01   ` sashiko-bot [this message]
2026-07-08  3:34 ` [PATCH v3 13/17] riscv: kvm: Add emulated test csr Charlie Jenkins
2026-07-08  4:00   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 14/17] KVM: riscv: selftests: Add csr emulation test Charlie Jenkins
2026-07-08  3:58   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 15/17] riscv: kvm: Use generated instruction headers for csr emulation Charlie Jenkins
2026-07-08  4:04   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 16/17] riscv: kexec: Use generated instruction headers for kexec relocations Charlie Jenkins
2026-07-08  4:01   ` sashiko-bot
2026-07-08  3:34 ` [PATCH v3 17/17] riscv: Remove unused instruction headers Charlie Jenkins
2026-07-08  4:09   ` sashiko-bot
2026-07-09  6:26 ` [syzbot ci] Re: riscv: Generate riscv instruction functions syzbot ci

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=20260708040131.94C7F1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thecharlesjenkins@gmail.com \
    /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