From: sashiko-bot@kernel.org
To: "Charlie Jenkins" <thecharlesjenkins@gmail.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v3 10/17] KVM: device: Add test device
Date: Wed, 08 Jul 2026 03:48:46 +0000 [thread overview]
Message-ID: <20260708034846.BDD9C1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260707-riscv_insn_table-v3-10-3f8e5f75ba97@gmail.com>
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] `kvm_io_bus_unregister_dev` is called without holding `kvm->slots_lock`, which can lead to a lockdep splat and data races.
- [High] Passing a `struct kvm_device *` to a function expecting `struct kvm_io_device *` causes silent unregistration failure and a Use-After-Free.
- [Medium] Off-by-one bounds check in `mmio_read` and `mmio_write` incorrectly rejects valid memory accesses at the upper bound of the device.
- [Medium] Missing `.release` callback allows userspace to leak memory and exhaust IO bus slots.
- [Medium] Concurrent lockless reads and writes to the shared `mmio_test->cache` buffer result in data races.
- [Low] Undeclared variables `ret` and `kvm` in `mmio_test_create()` cause hard build failures.
--
--- Patch [10]: [PATCH v3 10/17] KVM: device: Add test device ---
commit 245981a653b22b71de31531543fae6c504801423
Author: Charlie Jenkins <thecharlesjenkins@gmail.com>
KVM: device: Add test device
Create a KVM test device to help verify mmio reads and write emulation.
This is a simple device that will store the data in a buffer on writes
and echo back that stored data on a read.
[ ... ]
> diff --git a/virt/kvm/mmio_test.c b/virt/kvm/mmio_test.c
> new file mode 100644
> index 0000000000000..bd5f21a43ba87
> --- /dev/null
> +++ b/virt/kvm/mmio_test.c
> @@ -0,0 +1,104 @@
[ ... ]
> +static int mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
> + gpa_t addr, int len, void *val)
> +{
> + struct mmio_test *mmio_test = kvm_to_mmio_test_dev(dev);
> +
> + if ((addr - mmio_test->start + len) >= mmio_test->size)
> + return -1;
[Severity: Medium]
Does this bounds check incorrectly reject valid memory accesses?
If an MMIO read ends exactly at the size boundary, the offset plus length
would equal the size, causing the greater-than-or-equal check to fail.
Should this be a strictly greater-than check instead?
> +
> + /* Write back cached value */
> + memcpy(val, &mmio_test->cache[(addr - mmio_test->start)], len);
[Severity: Medium]
Can concurrent lockless reads and writes to the shared cache buffer result
in data races?
Multiple vCPUs performing concurrent MMIO reads or writes to the same
physical address might cause torn reads or corrupted states.
> + return 0;
> +}
> +
> +static int mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
> + gpa_t addr, int len, const void *val)
> +{
> + struct mmio_test *mmio_test = kvm_to_mmio_test_dev(dev);
> +
> + if ((addr - mmio_test->start + len) >= mmio_test->size)
> + return -1;
[Severity: Medium]
Does this bounds check improperly reject legitimate writes?
Similar to the read path, a write ending exactly at the boundary evaluates
to true for the greater-than-or-equal check and is rejected.
> +
> + /* Cache value */
> + memcpy(&mmio_test->cache[(addr - mmio_test->start)], val, len);
[Severity: Medium]
Are writes to the shared cache susceptible to data races from concurrent
vCPUs accessing the same device region?
> + return 0;
> +}
> +
[ ... ]
> +static int mmio_test_create(struct kvm_device *dev, u32 type)
> +{
> + struct mmio_test *mmio_test;
> +
> + mmio_test = kzalloc_obj(struct mmio_test, GFP_KERNEL);
> + if (!mmio_test)
> + return -ENOMEM;
> +
> + mmio_test->start = 0x20000000;
> + mmio_test->size = 16;
> +
> + dev->private = mmio_test;
> +
> + kvm_iodevice_init(&mmio_test->dev, &mmio_ops);
> + mutex_lock(&kvm->slots_lock);
> + ret = kvm_io_bus_register_dev(dev->kvm, KVM_MMIO_BUS, mmio_test->start,
> + mmio_test->size, &mmio_test->dev);
[Severity: Low]
Will this cause build failures due to undeclared variables?
It looks like kvm and ret are not declared in the scope of mmio_test_create.
Should this be dev->kvm->slots_lock and a local int ret declaration?
> + mutex_unlock(&kvm->slots_lock);
> +
> + if (ret < 0)
> + kfree(mmio_test);
> +
> + return ret;
> +}
> +
> +static void mmio_test_destroy(struct kvm_device *dev)
> +{
> + struct mmio_test *mmio_test = kvm_to_mmio_test_dev(dev);
[Severity: High]
Does passing a struct kvm_device pointer to kvm_to_mmio_test_dev lead to
silent unregistration failures and a use-after-free?
The kvm_to_mmio_test_dev function uses container_of expecting a
struct kvm_io_device pointer, but dev here is a struct kvm_device. This
calculates an invalid pointer base.
> +
> + kvm_io_bus_unregister_dev(dev->kvm, KVM_MMIO_BUS, &mmio_test->dev);
[Severity: High]
Is kvm_io_bus_unregister_dev being called without holding the required
kvm->slots_lock?
Modifying the KVM IO bus without the required lock can corrupt the shared
bus array or cause lost RCU pointer updates.
> + kfree(dev->private);
> + kfree(dev);
> +}
> +
> +struct kvm_device_ops kvm_riscv_mmio_test_device_ops = {
> + .name = "kvm-riscv-mmio_test",
> + .create = mmio_test_create,
> + .destroy = mmio_test_destroy,
> +};
[Severity: Medium]
Is it intentional to omit the .release callback here?
Without it, userspace might leak memory and exhaust IO bus slots by repeatedly
creating and closing test devices using the KVM_CREATE_DEVICE ioctl.
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260707-riscv_insn_table-v3-0-3f8e5f75ba97@gmail.com?part=10
next prev parent reply other threads:[~2026-07-08 3:48 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 [this message]
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
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=20260708034846.BDD9C1F000E9@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