Re: [PATCH] PCI: brcmstb: Reserve only the MSI vectors that are handed out
From: Han / 한상우Sangwoo
Date: Fri Dec 04 2026 - 01:13:33 EST
On Thu, Dec 03 2026 at 00:25, Thomas Gleixner wrote:
> Updated patch with your fix for msi_alloc_desc() folded in is below.
Hi Thomas,
Tested on BCM2712 with rpi-6.12.y (6.12.93) and KASAN, each patch
backported. A test module requests 3 MSI vectors; 5 loads with
PCI_IRQ_AFFINITY and 5 without, on each MSI parent:
KASAN successful loads
patch MSI parent report with affinity without affinity
Nov 8 brcmstb yes 3 of 5 5 of 5
Nov 8 BCM2712 MIP yes 1 of 5 5 of 5
Dec 3 brcmstb no 5 of 5 5 of 5
Dec 3 BCM2712 MIP no 5 of 5 5 of 5
KASAN report: slab-out-of-bounds in __irq_alloc_descs()
Failed loads: pci_alloc_irq_vectors() returned -ENOSPC
Not tested: bulk free on the brcmstb path. brcmstb on this tree still
uses pci_msi_create_irq_domain(), so the new branch in
irq_domain_free_irqs_hierarchy() is not reached.
Tested-by: Sangwoo Han <sangwoo.han@xxxxxxxxxxxxx>
Thanks,
Sangwoo
2026년 12월 3일 (목) 오전 8:25, Thomas Gleixner <tglx@xxxxxxxxxx>님이 작성:
>
> On Tue, Dec 01 2026 at 19:43, Han wrote:
> > On Mon, Nov 30 2026, Thomas Gleixner wrote:
> > 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):
>
> Indeed.
>
> > 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.
>
> Bah. Missed that detail.
>
> > A fix against mainline msi_alloc_desc() (the Nov 8 patch does not touch
> > this function, so it applies on top):
> >
> > 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.
>
> That's correct.
>
> > 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.
>
> That's intentional because there have been issues with quite some
> controllers which require the allocation to be power of two and then
> fall over the free() path freeing them one by one. If the core code
> enforces power of two then all the related workarounds can go away and
> new drivers won't need them.
>
> I agree that this prevents the underlying interrupt chip driver to
> squeeze some other bits into the unused areas.
>
> Your example: GICv2m has 987 vectors available for MSI, so you need a
> boat load of devices to even get near the limit. I seriously doubt that
> any of these gicv2m equipped systems have a chance to hit that.
>
> On vector space constraint platforms like x86 that's a non-issue because
> native unremapped mode does not support multi-MSI and in remapped mode
> the non-requested interrupts are not using a vector because the
> allocation only affects the remap table for that device and that has to
> be sized to the maximum number of MSI vectors the device advertises,
> which is always power of two. The actual CPU vectors which are mapped
> into the redirection tables are only allocated for requested interrupts,
> non-requested interrupts use the catch all spurious vector.
>
> So yes, in theory this might cause a problem on some obscure platform,
> but I prefer that risk over the ever growing warts which are required in
> MSI domain implementations to work around that multi-MSI insanity.
>
> > 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.
>
> The interrupts are disabled and not requested. So unless the device
> raises one of them nothing happens. In fact it's better that way because
> the device MSI enablement is always power of two independed of the
> number of vectors in use.
>
> So if only 3 vectors are required and the device raises the 4th due to
> malfunction the vector will hit a completely unrelated interrupt. If it
> hits an unused vector above the requested MSI ones, then this has zero
> side effects.
>
> > 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.
>
> TBH, that's a hack.
>
> We don't care whether we have to update a few call sites. We do that
> every day.
>
> What we care about is the overall design and correctness. As I said
> above and what the brcmstb driver which started this whole conversation
> got wrong has been papered over in other places as well.
>
> Updated patch with your fix for msi_alloc_desc() folded in is below.
>
> Thanks,
>
> tglx
> ---
> --- a/drivers/base/platform.c
> +++ b/drivers/base/platform.c
> @@ -421,7 +421,7 @@ int devm_platform_get_irqs_affinity(stru
> ptr->irq[i] = irq;
> }
>
> - desc = irq_create_affinity_masks(nvec, affd);
> + desc = irq_create_affinity_masks(nvec, nvec, affd);
> if (!desc) {
> ret = -ENOMEM;
> goto err_free_devres;
> --- a/drivers/pci/msi/api.c
> +++ b/drivers/pci/msi/api.c
> @@ -286,7 +286,7 @@ int pci_alloc_irq_vectors_affinity(struc
> * for the single interrupt case.
> */
> if (affd)
> - irq_create_affinity_masks(1, affd);
> + irq_create_affinity_masks(1, 1, affd);
> pci_intx(dev, 1);
> return 1;
> }
> --- a/drivers/pci/msi/msi.c
> +++ b/drivers/pci/msi/msi.c
> @@ -414,8 +414,14 @@ static int msi_capability_init(struct pc
> */
> pci_msi_set_enable(dev, 0);
>
> + /*
> + * Make sure a MULTI-MSI allocation is power of two even if the
> + * requested number of vectors (@nvec) is not.
> + */
> + unsigned int nvec_aligned = roundup_pow_of_two(nvec);
> +
> struct irq_affinity_desc *masks __free(kfree) =
> - affd ? irq_create_affinity_masks(nvec, affd) : NULL;
> + affd ? irq_create_affinity_masks(nvec, nvec_aligned, affd) : NULL;
>
> guard(msi_descs_lock)(&dev->dev);
> return __msi_capability_init(dev, nvec, masks);
> @@ -704,7 +710,7 @@ static int msix_setup_interrupts(struct
> int nvec, struct irq_affinity *affd)
> {
> struct irq_affinity_desc *masks __free(kfree) =
> - affd ? irq_create_affinity_masks(nvec, affd) : NULL;
> + affd ? irq_create_affinity_masks(nvec, nvec, affd) : NULL;
>
> guard(msi_descs_lock)(&dev->dev);
> return __msix_setup_interrupts(dev, entries, nvec, masks);
> --- a/include/linux/interrupt.h
> +++ b/include/linux/interrupt.h
> @@ -370,7 +370,7 @@ extern int
> irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
>
> struct irq_affinity_desc *
> -irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd);
> +irq_create_affinity_masks(unsigned int nvec, unsigned int totvec, struct irq_affinity *affd);
>
> unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
> const struct irq_affinity *affd);
> @@ -425,7 +425,7 @@ irq_set_affinity_notifier(unsigned int i
> }
>
> static inline struct irq_affinity_desc *
> -irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd)
> +irq_create_affinity_masks(unsigned int nvec, unsigned int totvec, struct irq_affinity *affd)
> {
> return NULL;
> }
> --- a/kernel/irq/affinity.c
> +++ b/kernel/irq/affinity.c
> @@ -17,13 +17,17 @@ static void default_calc_sets(struct irq
>
> /**
> * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
> - * @nvecs: The total number of vectors
> + * @nvecs: The number of active vectors for which affinity spreading is required
> + * @totvecs: The total number of required descriptors to handle the over
> + * allocation for PCI/MSI which requires power of two even if @nvec is
> + * not power of two. The extra descriptors are filled with default
> + * affinity and not relevant for the spreading calcuations.
> * @affd: Description of the affinity requirements
> *
> * Returns the irq_affinity_desc pointer or NULL if allocation failed.
> */
> struct irq_affinity_desc *
> -irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
> +irq_create_affinity_masks(unsigned int nvecs, unsigned int totvecs, struct irq_affinity *affd)
> {
> unsigned int affvecs, curvec, usedvecs, i;
> struct irq_affinity_desc *masks = NULL;
> @@ -56,7 +60,7 @@ irq_create_affinity_masks(unsigned int n
> if (!affvecs)
> return NULL;
>
> - masks = kzalloc_objs(*masks, nvecs);
> + masks = kzalloc_objs(*masks, totvecs);
> if (!masks)
> return NULL;
>
> @@ -90,7 +94,7 @@ irq_create_affinity_masks(unsigned int n
> curvec = affd->pre_vectors + affvecs;
> else
> curvec = affd->pre_vectors + usedvecs;
> - for (; curvec < nvecs; curvec++)
> + for (; curvec < totvecs; curvec++)
> cpumask_copy(&masks[curvec].mask, irq_default_affinity);
>
> /* Mark the managed interrupts */
> --- a/kernel/irq/irqdomain.c
> +++ b/kernel/irq/irqdomain.c
> @@ -1611,6 +1611,13 @@ static void irq_domain_free_irqs_hierarc
> if (!domain->ops->free)
> return;
>
> + /* MSI domains are capable of bulk free. */
> + if (domain->flags & (IRQ_DOMAIN_FLAG_MSI_DEVICE | IRQ_DOMAIN_FLAG_MSI_PARENT)) {
> + if (irq_domain_get_irq_data(domain, irq_base))
> + domain->ops->free(domain, irq_base, nr_irqs);
> + return;
> + }
> +
> for (i = 0; i < nr_irqs; i++) {
> if (irq_domain_get_irq_data(domain, irq_base + i))
> domain->ops->free(domain, irq_base + i, 1);
> --- a/kernel/irq/msi.c
> +++ b/kernel/irq/msi.c
> @@ -150,11 +150,13 @@ static int msi_insert_desc(struct device
> int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
> struct msi_desc *init_desc)
> {
> - struct msi_desc *desc;
> -
> lockdep_assert_held(&dev->msi.data->mutex);
>
> - desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity);
> + /* Make sure a MULTI-MSI allocation is power of two */
> + unsigned int nvec_aligned = roundup_pow_of_two(init_desc->nvec_used);
> +
> + struct msi_desc *desc = msi_alloc_desc(dev, nvec_aligned, init_desc->affinity);
> +
> if (!desc)
> return -ENOMEM;
>
> @@ -1333,20 +1335,28 @@ static int __msi_domain_alloc_irqs(struc
>
> ops->set_desc(&arg, desc);
>
> - virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
> + /* Make sure a MULTI-MSI allocation is power of two */
> + unsigned int nvec_aligned = roundup_pow_of_two(desc->nvec_used);
> +
> + virq = __irq_domain_alloc_irqs(domain, -1, nvec_aligned,
> dev_to_node(dev), &arg, false,
> desc->affinity);
> if (virq < 0)
> return msi_handle_pci_fail(domain, desc, allocated);
>
> - for (i = 0; i < desc->nvec_used; i++) {
> + for (i = 0; i < nvec_aligned; i++) {
> irq_set_msi_desc_off(virq, i, desc);
> irq_debugfs_copy_devname(virq + i, dev);
> ret = msi_init_virq(domain, virq + i, vflags);
> if (ret)
> return ret;
> }
> +
> if (info->flags & MSI_FLAG_DEV_SYSFS) {
> + /*
> + * This only exposes desc->nvec_used and ignores the
> + * overallocated MULTI-MSI ones.
> + */
> ret = msi_sysfs_populate_desc(dev, desc);
> if (ret)
> return ret;
> @@ -1610,13 +1620,15 @@ static void __msi_domain_free_irqs(struc
> continue;
>
> /* Make sure all interrupts are deactivated */
> - for (i = 0; i < desc->nvec_used; i++) {
> + unsigned int nvec_aligned = roundup_pow_of_two(desc->nvec_used);
> +
> + for (i = 0; i < nvec_aligned; i++) {
> irqd = irq_domain_get_irq_data(domain, desc->irq + i);
> if (irqd && irqd_is_activated(irqd))
> irq_domain_deactivate_irq(irqd);
> }
>
> - irq_domain_free_irqs(desc->irq, desc->nvec_used);
> + irq_domain_free_irqs(desc->irq, nvec_aligned);
> if (info->flags & MSI_FLAG_DEV_SYSFS)
> msi_sysfs_remove_desc(dev, desc);
> desc->irq = 0;
2026년 12월 3일 (목) 오전 8:25, Thomas Gleixner <tglx@xxxxxxxxxx>님이 작성:
>
> On Tue, Dec 01 2026 at 19:43, Han wrote:
> > On Mon, Nov 30 2026, Thomas Gleixner wrote:
> > 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):
>
> Indeed.
>
> > 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.
>
> Bah. Missed that detail.
>
> > A fix against mainline msi_alloc_desc() (the Nov 8 patch does not touch
> > this function, so it applies on top):
> >
> > 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.
>
> That's correct.
>
> > 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.
>
> That's intentional because there have been issues with quite some
> controllers which require the allocation to be power of two and then
> fall over the free() path freeing them one by one. If the core code
> enforces power of two then all the related workarounds can go away and
> new drivers won't need them.
>
> I agree that this prevents the underlying interrupt chip driver to
> squeeze some other bits into the unused areas.
>
> Your example: GICv2m has 987 vectors available for MSI, so you need a
> boat load of devices to even get near the limit. I seriously doubt that
> any of these gicv2m equipped systems have a chance to hit that.
>
> On vector space constraint platforms like x86 that's a non-issue because
> native unremapped mode does not support multi-MSI and in remapped mode
> the non-requested interrupts are not using a vector because the
> allocation only affects the remap table for that device and that has to
> be sized to the maximum number of MSI vectors the device advertises,
> which is always power of two. The actual CPU vectors which are mapped
> into the redirection tables are only allocated for requested interrupts,
> non-requested interrupts use the catch all spurious vector.
>
> So yes, in theory this might cause a problem on some obscure platform,
> but I prefer that risk over the ever growing warts which are required in
> MSI domain implementations to work around that multi-MSI insanity.
>
> > 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.
>
> The interrupts are disabled and not requested. So unless the device
> raises one of them nothing happens. In fact it's better that way because
> the device MSI enablement is always power of two independed of the
> number of vectors in use.
>
> So if only 3 vectors are required and the device raises the 4th due to
> malfunction the vector will hit a completely unrelated interrupt. If it
> hits an unused vector above the requested MSI ones, then this has zero
> side effects.
>
> > 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.
>
> TBH, that's a hack.
>
> We don't care whether we have to update a few call sites. We do that
> every day.
>
> What we care about is the overall design and correctness. As I said
> above and what the brcmstb driver which started this whole conversation
> got wrong has been papered over in other places as well.
>
> Updated patch with your fix for msi_alloc_desc() folded in is below.
>
> Thanks,
>
> tglx
> ---
> --- a/drivers/base/platform.c
> +++ b/drivers/base/platform.c
> @@ -421,7 +421,7 @@ int devm_platform_get_irqs_affinity(stru
> ptr->irq[i] = irq;
> }
>
> - desc = irq_create_affinity_masks(nvec, affd);
> + desc = irq_create_affinity_masks(nvec, nvec, affd);
> if (!desc) {
> ret = -ENOMEM;
> goto err_free_devres;
> --- a/drivers/pci/msi/api.c
> +++ b/drivers/pci/msi/api.c
> @@ -286,7 +286,7 @@ int pci_alloc_irq_vectors_affinity(struc
> * for the single interrupt case.
> */
> if (affd)
> - irq_create_affinity_masks(1, affd);
> + irq_create_affinity_masks(1, 1, affd);
> pci_intx(dev, 1);
> return 1;
> }
> --- a/drivers/pci/msi/msi.c
> +++ b/drivers/pci/msi/msi.c
> @@ -414,8 +414,14 @@ static int msi_capability_init(struct pc
> */
> pci_msi_set_enable(dev, 0);
>
> + /*
> + * Make sure a MULTI-MSI allocation is power of two even if the
> + * requested number of vectors (@nvec) is not.
> + */
> + unsigned int nvec_aligned = roundup_pow_of_two(nvec);
> +
> struct irq_affinity_desc *masks __free(kfree) =
> - affd ? irq_create_affinity_masks(nvec, affd) : NULL;
> + affd ? irq_create_affinity_masks(nvec, nvec_aligned, affd) : NULL;
>
> guard(msi_descs_lock)(&dev->dev);
> return __msi_capability_init(dev, nvec, masks);
> @@ -704,7 +710,7 @@ static int msix_setup_interrupts(struct
> int nvec, struct irq_affinity *affd)
> {
> struct irq_affinity_desc *masks __free(kfree) =
> - affd ? irq_create_affinity_masks(nvec, affd) : NULL;
> + affd ? irq_create_affinity_masks(nvec, nvec, affd) : NULL;
>
> guard(msi_descs_lock)(&dev->dev);
> return __msix_setup_interrupts(dev, entries, nvec, masks);
> --- a/include/linux/interrupt.h
> +++ b/include/linux/interrupt.h
> @@ -370,7 +370,7 @@ extern int
> irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
>
> struct irq_affinity_desc *
> -irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd);
> +irq_create_affinity_masks(unsigned int nvec, unsigned int totvec, struct irq_affinity *affd);
>
> unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
> const struct irq_affinity *affd);
> @@ -425,7 +425,7 @@ irq_set_affinity_notifier(unsigned int i
> }
>
> static inline struct irq_affinity_desc *
> -irq_create_affinity_masks(unsigned int nvec, struct irq_affinity *affd)
> +irq_create_affinity_masks(unsigned int nvec, unsigned int totvec, struct irq_affinity *affd)
> {
> return NULL;
> }
> --- a/kernel/irq/affinity.c
> +++ b/kernel/irq/affinity.c
> @@ -17,13 +17,17 @@ static void default_calc_sets(struct irq
>
> /**
> * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
> - * @nvecs: The total number of vectors
> + * @nvecs: The number of active vectors for which affinity spreading is required
> + * @totvecs: The total number of required descriptors to handle the over
> + * allocation for PCI/MSI which requires power of two even if @nvec is
> + * not power of two. The extra descriptors are filled with default
> + * affinity and not relevant for the spreading calcuations.
> * @affd: Description of the affinity requirements
> *
> * Returns the irq_affinity_desc pointer or NULL if allocation failed.
> */
> struct irq_affinity_desc *
> -irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
> +irq_create_affinity_masks(unsigned int nvecs, unsigned int totvecs, struct irq_affinity *affd)
> {
> unsigned int affvecs, curvec, usedvecs, i;
> struct irq_affinity_desc *masks = NULL;
> @@ -56,7 +60,7 @@ irq_create_affinity_masks(unsigned int n
> if (!affvecs)
> return NULL;
>
> - masks = kzalloc_objs(*masks, nvecs);
> + masks = kzalloc_objs(*masks, totvecs);
> if (!masks)
> return NULL;
>
> @@ -90,7 +94,7 @@ irq_create_affinity_masks(unsigned int n
> curvec = affd->pre_vectors + affvecs;
> else
> curvec = affd->pre_vectors + usedvecs;
> - for (; curvec < nvecs; curvec++)
> + for (; curvec < totvecs; curvec++)
> cpumask_copy(&masks[curvec].mask, irq_default_affinity);
>
> /* Mark the managed interrupts */
> --- a/kernel/irq/irqdomain.c
> +++ b/kernel/irq/irqdomain.c
> @@ -1611,6 +1611,13 @@ static void irq_domain_free_irqs_hierarc
> if (!domain->ops->free)
> return;
>
> + /* MSI domains are capable of bulk free. */
> + if (domain->flags & (IRQ_DOMAIN_FLAG_MSI_DEVICE | IRQ_DOMAIN_FLAG_MSI_PARENT)) {
> + if (irq_domain_get_irq_data(domain, irq_base))
> + domain->ops->free(domain, irq_base, nr_irqs);
> + return;
> + }
> +
> for (i = 0; i < nr_irqs; i++) {
> if (irq_domain_get_irq_data(domain, irq_base + i))
> domain->ops->free(domain, irq_base + i, 1);
> --- a/kernel/irq/msi.c
> +++ b/kernel/irq/msi.c
> @@ -150,11 +150,13 @@ static int msi_insert_desc(struct device
> int msi_domain_insert_msi_desc(struct device *dev, unsigned int domid,
> struct msi_desc *init_desc)
> {
> - struct msi_desc *desc;
> -
> lockdep_assert_held(&dev->msi.data->mutex);
>
> - desc = msi_alloc_desc(dev, init_desc->nvec_used, init_desc->affinity);
> + /* Make sure a MULTI-MSI allocation is power of two */
> + unsigned int nvec_aligned = roundup_pow_of_two(init_desc->nvec_used);
> +
> + struct msi_desc *desc = msi_alloc_desc(dev, nvec_aligned, init_desc->affinity);
> +
> if (!desc)
> return -ENOMEM;
>
> @@ -1333,20 +1335,28 @@ static int __msi_domain_alloc_irqs(struc
>
> ops->set_desc(&arg, desc);
>
> - virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
> + /* Make sure a MULTI-MSI allocation is power of two */
> + unsigned int nvec_aligned = roundup_pow_of_two(desc->nvec_used);
> +
> + virq = __irq_domain_alloc_irqs(domain, -1, nvec_aligned,
> dev_to_node(dev), &arg, false,
> desc->affinity);
> if (virq < 0)
> return msi_handle_pci_fail(domain, desc, allocated);
>
> - for (i = 0; i < desc->nvec_used; i++) {
> + for (i = 0; i < nvec_aligned; i++) {
> irq_set_msi_desc_off(virq, i, desc);
> irq_debugfs_copy_devname(virq + i, dev);
> ret = msi_init_virq(domain, virq + i, vflags);
> if (ret)
> return ret;
> }
> +
> if (info->flags & MSI_FLAG_DEV_SYSFS) {
> + /*
> + * This only exposes desc->nvec_used and ignores the
> + * overallocated MULTI-MSI ones.
> + */
> ret = msi_sysfs_populate_desc(dev, desc);
> if (ret)
> return ret;
> @@ -1610,13 +1620,15 @@ static void __msi_domain_free_irqs(struc
> continue;
>
> /* Make sure all interrupts are deactivated */
> - for (i = 0; i < desc->nvec_used; i++) {
> + unsigned int nvec_aligned = roundup_pow_of_two(desc->nvec_used);
> +
> + for (i = 0; i < nvec_aligned; i++) {
> irqd = irq_domain_get_irq_data(domain, desc->irq + i);
> if (irqd && irqd_is_activated(irqd))
> irq_domain_deactivate_irq(irqd);
> }
>
> - irq_domain_free_irqs(desc->irq, desc->nvec_used);
> + irq_domain_free_irqs(desc->irq, nvec_aligned);
> if (info->flags & MSI_FLAG_DEV_SYSFS)
> msi_sysfs_remove_desc(dev, desc);
> desc->irq = 0;