[PATCH RFC 1/2] irqchip/ls-scfg-msi: refactor allocation to bitmap_find_free_region()

From: Alexander Wilhelm

Date: Thu Jul 16 2026 - 06:35:55 EST


Replace the manual find_first_zero_bit()/__set_bit() pair with
bitmap_find_free_region(), and __clear_bit() with bitmap_release_region().

For the current single-MSI case (guarded by WARN_ON(nr_irqs != 1)) with
order 0 this is functionally equivalent: bitmap_find_free_region() walks
the bitmap in single-bit units and internally does the test-and-set
atomically under the same lock. On failure it returns -ENOMEM which is now
propagated directly, replacing the previous -ENOSPC.

While at it, release the region on iommu_dma_prepare_msi() failure instead
of leaking the allocated bit, loop over nr_irqs when calling
irq_domain_set_info(), and range-check pos + nr_irqs in the free path. All
three are no-ops for nr_irqs = 1 but prepare the driver for a subsequent
MSI_FLAG_MULTI_PCI_MSI enable without further changes to the allocation
bookkeeping.

Assisted-by: Copilot:claude-opus-4.7
Signed-off-by: Alexander Wilhelm <alexander.wilhelm@xxxxxxxxxxxx>
---
drivers/irqchip/irq-ls-scfg-msi.c | 36 +++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 15 deletions(-)

diff --git a/drivers/irqchip/irq-ls-scfg-msi.c b/drivers/irqchip/irq-ls-scfg-msi.c
index 4910f364e568..bd5833ef621a 100644
--- a/drivers/irqchip/irq-ls-scfg-msi.c
+++ b/drivers/irqchip/irq-ls-scfg-msi.c
@@ -141,28 +141,32 @@ static int ls_scfg_msi_domain_irq_alloc(struct irq_domain *domain,
{
msi_alloc_info_t *info = args;
struct ls_scfg_msi *msi_data = domain->host_data;
- int pos, err = 0;
+ int order = get_count_order(nr_irqs);
+ int pos, err;
+ unsigned int i;

WARN_ON(nr_irqs != 1);

spin_lock(&msi_data->lock);
- pos = find_first_zero_bit(msi_data->used, msi_data->irqs_num);
- if (pos < msi_data->irqs_num)
- __set_bit(pos, msi_data->used);
- else
- err = -ENOSPC;
+ pos = bitmap_find_free_region(msi_data->used, msi_data->irqs_num,
+ order);
spin_unlock(&msi_data->lock);

- if (err)
- return err;
+ if (pos < 0)
+ return pos;

err = iommu_dma_prepare_msi(info->desc, msi_data->msiir_addr);
- if (err)
+ if (err) {
+ spin_lock(&msi_data->lock);
+ bitmap_release_region(msi_data->used, pos, order);
+ spin_unlock(&msi_data->lock);
return err;
+ }

- irq_domain_set_info(domain, virq, pos,
- &ls_scfg_msi_parent_chip, msi_data,
- handle_simple_irq, NULL, NULL);
+ for (i = 0; i < nr_irqs; i++)
+ irq_domain_set_info(domain, virq + i, pos + i,
+ &ls_scfg_msi_parent_chip, msi_data,
+ handle_simple_irq, NULL, NULL);

return 0;
}
@@ -172,16 +176,18 @@ static void ls_scfg_msi_domain_irq_free(struct irq_domain *domain,
{
struct irq_data *d = irq_domain_get_irq_data(domain, virq);
struct ls_scfg_msi *msi_data = irq_data_get_irq_chip_data(d);
+ int order = get_count_order(nr_irqs);
int pos;

pos = d->hwirq;
- if (pos < 0 || pos >= msi_data->irqs_num) {
- pr_err("failed to teardown msi. Invalid hwirq %d\n", pos);
+ if (pos < 0 || pos + nr_irqs > msi_data->irqs_num) {
+ pr_err("failed to teardown msi. Invalid hwirq %d nr %u\n",
+ pos, nr_irqs);
return;
}

spin_lock(&msi_data->lock);
- __clear_bit(pos, msi_data->used);
+ bitmap_release_region(msi_data->used, pos, order);
spin_unlock(&msi_data->lock);
}


--
2.43.0