From: Oleksii Kurochko <oleksii.kurochko@gmail.com>
To: Baptiste Le Duc <baptiste.le-duc@vates.tech>,
xen-devel@lists.xenproject.org
Cc: "Alistair Francis" <alistair.francis@wdc.com>,
"Connor Davis" <connojdavis@gmail.com>,
"Andrew Cooper" <andrew.cooper3@citrix.com>,
"Anthony PERARD" <anthony.perard@vates.tech>,
"Michal Orzel" <michal.orzel@amd.com>,
"Jan Beulich" <jbeulich@suse.com>,
"Julien Grall" <julien@xen.org>,
"Roger Pau Monné" <roger@xenproject.org>,
"Stefano Stabellini" <sstabellini@kernel.org>
Subject: Re: [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots
Date: Mon, 16 Nov 2026 17:32:05 +0100 [thread overview]
Message-ID: <c9bbf2e0-9334-423a-ace9-9e50ee712986@gmail.com> (raw)
In-Reply-To: <1794842995.8631fc262581453bbf619ec5b2062170.1a1e4faaa300006d3f@vates.tech>
On 11/16/26 4:29 PM, Baptiste Le Duc wrote:
> imsic_init() requires every MMIO register set to be exactly
> IMSIC_HART_SIZE(guest_index_bits) * nr_parent_irqs bytes, i.e. to cover
> the interrupt files of all harts listed in interrupts-extended. This only
> holds when the DT describes a single register set.
>
> When harts are split into groups (riscv,group-index-bits > 0), each group
> has its own register set, which only covers the harts of that group. For
> example, the SpacemiT V100 IMSIC node describes two 2 MiB register sets,
> each holding 64 hart slots of 32 KiB, with 128 parent interrupts in total.
> Xen expects 4 MiB per register set and panics with:
>
> IMSIC MMIO size is incorrect 2097152, expected MMIO size: 4194304
>
> Check instead that a register set does not run past the end of the group it
> starts in, i.e. BIT(hart_index_bits) hart slots. A register set may be
> smaller than that, e.g. when a group holds a non-power-of-two number of
> harts.
>
> Parent interrupt i gets the i-th hart slot of the register sets, taken in
> order. A slot with no hart behind it can be:
> - at the very end of the last register set, e.g. when a DT describes a
> group by its full capacity: harmless, no interrupt is left to take it.
> - anywhere else: every later interrupt is shifted one slot early, onto the
> previous hart's interrupt file.
>
> Both show up as more slots than parent interrupts, so counting can't tell
> them apart. Refusing to boot would reject the valid DTs of the first case,
> so only warn: they keep booting, and broken ones get a hint as to why their
> interrupts are misrouted.
>
> While at it, print the MMIO sizes in hex to match the DT reg property.
>
> Fixes: c9bd8b322ecb ("xen/riscv: imsic_init() implementation")
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Baptiste Le Duc <baptiste.le-duc@vates.tech>
> ---
> Changes in v2:
> - Bound a register set by the end of the group it starts in (offset +
> size), not only by the group size.
> - Warn when the register sets hold more hart slots than parent
> interrupts, as an unused slot before the end of the last register set
> misroutes every following hart.
> - Reuse the group size for the base address mask.
> - Link to v1: https://patch.msgid.link/20261113-fix-imsic-mmio-dt-v1-1-10efb6c28de4@vates.tech
> ---
> xen/arch/riscv/imsic.c | 39 +++++++++++++++++++++++++++++----------
> 1 file changed, 29 insertions(+), 10 deletions(-)
>
> diff --git a/xen/arch/riscv/imsic.c b/xen/arch/riscv/imsic.c
> index 44b8640ea3..48acc02440 100644
> --- a/xen/arch/riscv/imsic.c
> +++ b/xen/arch/riscv/imsic.c
> @@ -381,7 +381,7 @@ void cf_check vcpu_imsic_deinit(struct vcpu *v)
> int __init imsic_init(const struct dt_device_node *node)
> {
> int rc;
> - unsigned long reloff, hartid;
> + unsigned long reloff, hartid, nr_slots = 0;
> unsigned int nr_parent_irqs, index, nr_handlers = 0;
> paddr_t base_addr;
> unsigned int nr_mmios;
> @@ -415,9 +415,11 @@ int __init imsic_init(const struct dt_device_node *node)
> /* Check MMIO register sets */
> for ( unsigned int i = 0; i < nr_mmios; i++ )
> {
> - unsigned int guest_bits = imsic_cfg.guest_index_bits;
> - unsigned long expected_mmio_size =
> - IMSIC_HART_SIZE(guest_bits) * nr_parent_irqs;
> + /* Address space of one group: BIT(hart_index_bits) hart slots. */
> + unsigned long group_size = BIT(imsic_cfg.guest_index_bits +
> + imsic_cfg.hart_index_bits +
> + IMSIC_MMIO_PAGE_SHIFT, UL);
> + unsigned long offset;
>
> rc = dt_device_get_address(node, i, &mmios[i].base_addr,
> &mmios[i].size);
> @@ -429,9 +431,7 @@ int __init imsic_init(const struct dt_device_node *node)
> }
>
> base_addr = mmios[i].base_addr;
> - base_addr &= ~(BIT(guest_bits +
> - imsic_cfg.hart_index_bits +
> - IMSIC_MMIO_PAGE_SHIFT, UL) - 1);
> + base_addr &= ~(group_size - 1);
> base_addr &= ~((BIT(imsic_cfg.group_index_bits, UL) - 1) <<
> imsic_cfg.group_index_shift);
> if ( base_addr != imsic_cfg.base_addr )
> @@ -442,15 +442,34 @@ int __init imsic_init(const struct dt_device_node *node)
> goto imsic_init_err;
> }
>
> - if ( mmios[i].size != expected_mmio_size )
> + /* A register set must not run past the end of its group. */
> + offset = mmios[i].base_addr & (group_size - 1);
> + if ( mmios[i].size + offset > group_size )
This can overflow for a bogus, huge size from the DT and then pass. As
offset is always below group_size,
if ( mmios[i].size > group_size - offset )
can't.
> {
> rc = -EINVAL;
> - printk(XENLOG_ERR "%s: IMSIC MMIO size is incorrect %ld, expected MMIO size: %ld\n",
> - node->name, mmios[i].size, expected_mmio_size);
> + printk(XENLOG_ERR
> + "%s: IMSIC MMIO regset %u (offset %#lx + size %#lx) exceeds group size %#lx\n",
> + node->name, i, offset, mmios[i].size, group_size);
> goto imsic_init_err;
> }
> +
> + /* The last slot may be cut short, see the lookup below. */
> + nr_slots += DIV_ROUND_UP(mmios[i].size,
> + IMSIC_HART_SIZE(imsic_cfg.guest_index_bits));
> }
>
> + /*
> + * Parent irq i is given hart slot i, counting slots across the register
> + * sets in order. A slot with no hart behind it therefore shifts every
> + * later irq one slot early, onto the previous hart's interrupt file.
> + * Counting can't tell that from harmless unused slots at the end, hence
> + * only a warning.
> + */
> + if ( nr_slots > nr_parent_irqs )
> + printk(XENLOG_WARNING
> + "%s: %lu hart slots in IMSIC MMIO regsets for %u parent irqs, unused slots must only be at the end of the last regset\n",
> + node->name, nr_slots, nr_parent_irqs);
> +
With the overflow addressed and fixing a length of the string like (took
from Baptiste response):
printk(XENLOG_WARNING "%s: %lu hart slots for %u parent irqs\n",
node->name, nr_slots, nr_parent_irqs);
Reviewed-by: Oleksii Kurochko <oleksii.kurochko@gmail.com>
~ Oleksii
next prev parent reply other threads:[~2026-11-16 16:32 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 15:29 [PATCH v2] xen/riscv: bound each IMSIC MMIO regset by the per-group hart slots Baptiste Le Duc
2026-11-16 15:36 ` Jan Beulich
2026-11-16 15:45 ` Baptiste Le Duc
2026-11-16 15:47 ` Jan Beulich
2026-11-16 16:32 ` Oleksii Kurochko [this message]
2026-11-16 16:42 ` Baptiste Le Duc
2026-11-16 16:50 ` Jan Beulich
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=c9bbf2e0-9334-423a-ace9-9e50ee712986@gmail.com \
--to=oleksii.kurochko@gmail.com \
--cc=alistair.francis@wdc.com \
--cc=andrew.cooper3@citrix.com \
--cc=anthony.perard@vates.tech \
--cc=baptiste.le-duc@vates.tech \
--cc=connojdavis@gmail.com \
--cc=jbeulich@suse.com \
--cc=julien@xen.org \
--cc=michal.orzel@amd.com \
--cc=roger@xenproject.org \
--cc=sstabellini@kernel.org \
--cc=xen-devel@lists.xenproject.org \
/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.