Re: [PATCH] PCI: brcmstb: Reserve only the MSI vectors that are handed out

From: Han / 한상우Sangwoo

Date: Tue Dec 01 2026 - 05:43:48 EST


On Mon, Nov 30 2026, Thomas Gleixner wrote:
> Gentle ping. November is almost over. :)

Hi Thomas,

Sorry for the delay. Some time freed up, so I looked into it a bit more.

The KASAN slab-out-of-bounds read still triggers with the Nov 8 patch.
The Nov 8 patch does not touch msi_alloc_desc(). That function still
copies only nvec entries into desc->affinity (mainline
kernel/irq/msi.c:87):

desc->affinity = kmemdup_array(affinity, nvec,
sizeof(*desc->affinity), GFP_KERNEL);

With the Nov 8 patch applied, __msi_domain_alloc_irqs() computes
nvec_aligned and passes it to __irq_domain_alloc_irqs() together with
desc->affinity:

unsigned int nvec_aligned = roundup_pow_of_two(desc->nvec_used);
virq = __irq_domain_alloc_irqs(domain, -1, nvec_aligned,

The affinity array built by irq_create_affinity_masks() now has
roundup_pow_of_two(nvec) entries, but desc->affinity holds only nvec
entries, and the allocation reads nvec_aligned entries from that copy.

A fix against mainline msi_alloc_desc() (the Nov 8 patch does not touch
this function, so it applies on top):

--- a/kernel/irq/msi.c
+++ b/kernel/irq/msi.c
@@ -66,10 +66,7 @@ static int msi_domain_prepare_irqs(struc
* msi_alloc_desc - Allocate an initialized msi_desc
* @dev: Pointer to the device for which this is allocated
* @nvec: The number of vectors used in this entry
- * @affinity: Optional pointer to an affinity mask array size of @nvec
- *
- * If @affinity is not %NULL then an affinity array[@nvec] is allocated
- * and the affinity masks and flags from @affinity are copied.
+ * @affinity: Optional pointer to an affinity mask array
*
* Return: pointer to allocated &msi_desc on success or %NULL on failure
*/
@@ -84,7 +81,8 @@ static struct msi_desc *msi_alloc_desc(s
desc->dev = dev;
desc->nvec_used = nvec;
if (affinity) {
- desc->affinity = kmemdup_array(affinity, nvec,
sizeof(*desc->affinity), GFP_KERNEL);
+ desc->affinity = kmemdup_array(affinity, roundup_pow_of_two(nvec),
+ sizeof(*desc->affinity), GFP_KERNEL);
if (!desc->affinity) {
kfree(desc);
return NULL;

This makes desc->affinity hold roundup_pow_of_two(nvec) entries,
matching what irq_create_affinity_masks() allocates for PCI/MSI with
the Nov 8 patch. It reads roundup_pow_of_two(nvec) entries from the
caller's array, so it relies on the caller passing an array of that
size - which the Nov 8 patch does for PCI/MSI.

I also looked for places where rounding Multi-MSI allocations up could
cause a regression.

With the Nov 8 patch, the MSI core asks the parent domain for the
rounded-up count rather than the requested count. GICv2m reserves
exactly the count it is asked for, so a Multi-MSI device whose vector
count is not a power of two occupies more SPIs than before - 8 instead
of 5 for a device that requests 5. I don't know whether this will turn
into a real issue.

The patch also runs the per-interrupt setup loop over the rounded-up
count, so the interrupts the rounding adds - the ones the driver did not
request - are activated at allocation time alongside the requested ones.
For a device that requests 5 vectors, 8 interrupts are activated instead
of 5. I don't know whether this can become an issue with any real
hardware combination.

Another way to pad the affinity array: instead of adding a totvecs
argument to irq_create_affinity_masks() and updating all its callers,
let msi_alloc_desc() handle the padding itself - allocate
roundup_pow_of_two(nvec) entries, copy the nvec entries from the
caller, and fill the remainder with the default affinity.
irq_create_affinity_masks() keeps its current signature, and
drivers/base/platform.c, drivers/pci/msi/api.c, drivers/pci/msi/msi.c,
include/linux/interrupt.h and kernel/irq/affinity.c stay as in
mainline.

This does not change the two side effects described above.

Sketch against mainline kernel/irq/msi.c, intended on top of the Nov 8
patch with its totvecs changes dropped:

--- a/kernel/irq/msi.c
+++ b/kernel/irq/msi.c
@@ -68,9 +68,6 @@ static int msi_domain_prepare_irqs(struc
* @nvec: The number of vectors used in this entry
* @affinity: Optional pointer to an affinity mask array size of @nvec
*
- * If @affinity is not %NULL then an affinity array[@nvec] is allocated
- * and the affinity masks and flags from @affinity are copied.
- *
* Return: pointer to allocated &msi_desc on success or %NULL on failure
*/
static struct msi_desc *msi_alloc_desc(struct device *dev, int nvec,
@@ -84,11 +81,21 @@ static struct msi_desc *msi_alloc_desc(s
desc->dev = dev;
desc->nvec_used = nvec;
if (affinity) {
- desc->affinity = kmemdup_array(affinity, nvec,
sizeof(*desc->affinity), GFP_KERNEL);
+ unsigned int i, nvec_alloc = roundup_pow_of_two(nvec);
+
+ desc->affinity = kcalloc(nvec_alloc, sizeof(*desc->affinity), GFP_KERNEL);
if (!desc->affinity) {
kfree(desc);
return NULL;
}
+ memcpy(desc->affinity, affinity, nvec * sizeof(*desc->affinity));
+ for (i = nvec; i < nvec_alloc; i++) {
+#ifdef CONFIG_SMP
+ cpumask_copy(&desc->affinity[i].mask, irq_default_affinity);
+#else
+ cpumask_copy(&desc->affinity[i].mask, cpu_possible_mask);
+#endif
+ }
}
return desc;
}

The #ifdef CONFIG_SMP block in the fill loop above would be better
replaced with a helper or the like.

Thanks,
Sangwoo

2026년 11월 30일 (월) 오후 6:44, Thomas Gleixner <tglx@xxxxxxxxxx>님이 작성:
>
> On Tue, Nov 17 2026 at 11:39, Han wrote:
> > Hi Thomas,
> >
> > Thank you for the updated patch.
> >
> > I will test it on the BCM2712 with the same setup as before and report
> > back within November.
>
> Gentle ping. November is almost over. :)