Commit 36f5f026 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

Merge tag 'irq-msi-2025-03-23' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull MSI irq updates from Thomas Gleixner:

 - Switch the MSI descriptor locking to guards

 - Replace the broken PCI/TPH implementation, which lacks any form of
   serialization against concurrent modifications with a properly
   serialized mechanism in the PCI/MSI core code

 - Replace the MSI descriptor abuse in the SCSI/UFS Qualcom driver with
   dedicated driver internal storage

* tag 'irq-msi-2025-03-23' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  genirq/msi: Rename msi_[un]lock_descs()
  scsi: ufs: qcom: Remove the MSI descriptor abuse
  PCI/TPH: Replace the broken MSI-X control word update
  PCI/MSI: Provide a sane mechanism for TPH
  PCI: hv: Switch MSI descriptor locking to guard()
  PCI/MSI: Switch to MSI descriptor locking to guard()
  NTB/msi: Switch MSI descriptor locking to lock guard()
  soc: ti: ti_sci_inta_msi: Switch MSI descriptor locking to guard()
  genirq/msi: Use lock guards for MSI descriptor locking
  cleanup: Provide retain_ptr()
  genirq/msi: Make a few functions static
parents 43a7eec0 8327df40
Loading
Loading
Loading
Loading
+8 −14
Original line number Diff line number Diff line
@@ -106,10 +106,10 @@ int ntb_msi_setup_mws(struct ntb_dev *ntb)
	if (!ntb->msi)
		return -EINVAL;

	msi_lock_descs(&ntb->pdev->dev);
	scoped_guard (msi_descs_lock, &ntb->pdev->dev) {
		desc = msi_first_desc(&ntb->pdev->dev, MSI_DESC_ASSOCIATED);
		addr = desc->msg.address_lo + ((uint64_t)desc->msg.address_hi << 32);
	msi_unlock_descs(&ntb->pdev->dev);
	}

	for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) {
		peer_widx = ntb_peer_highest_mw_idx(ntb, peer);
@@ -289,7 +289,7 @@ int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb, irq_handler_t handler,
	if (!ntb->msi)
		return -EINVAL;

	msi_lock_descs(dev);
	guard(msi_descs_lock)(dev);
	msi_for_each_desc(entry, dev, MSI_DESC_ASSOCIATED) {
		if (irq_has_action(entry->irq))
			continue;
@@ -307,17 +307,11 @@ int ntbm_msi_request_threaded_irq(struct ntb_dev *ntb, irq_handler_t handler,
		ret = ntbm_msi_setup_callback(ntb, entry, msi_desc);
		if (ret) {
			devm_free_irq(&ntb->dev, entry->irq, dev_id);
			goto unlock;
			return ret;
		}

		ret = entry->irq;
		goto unlock;
		return entry->irq;
	}
	ret = -ENODEV;

unlock:
	msi_unlock_descs(dev);
	return ret;
	return -ENODEV;
}
EXPORT_SYMBOL(ntbm_msi_request_threaded_irq);

+4 −10
Original line number Diff line number Diff line
@@ -3975,24 +3975,18 @@ static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
{
	struct irq_data *irq_data;
	struct msi_desc *entry;
	int ret = 0;

	if (!pdev->msi_enabled && !pdev->msix_enabled)
		return 0;

	msi_lock_descs(&pdev->dev);
	guard(msi_descs_lock)(&pdev->dev);
	msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
		irq_data = irq_get_irq_data(entry->irq);
		if (WARN_ON_ONCE(!irq_data)) {
			ret = -EINVAL;
			break;
		}

		if (WARN_ON_ONCE(!irq_data))
			return -EINVAL;
		hv_compose_msi_msg(irq_data, &entry->msg);
	}
	msi_unlock_descs(&pdev->dev);

	return ret;
	return 0;
}

/*
+2 −4
Original line number Diff line number Diff line
@@ -53,10 +53,9 @@ void pci_disable_msi(struct pci_dev *dev)
	if (!pci_msi_enabled() || !dev || !dev->msi_enabled)
		return;

	msi_lock_descs(&dev->dev);
	guard(msi_descs_lock)(&dev->dev);
	pci_msi_shutdown(dev);
	pci_free_msi_irqs(dev);
	msi_unlock_descs(&dev->dev);
}
EXPORT_SYMBOL(pci_disable_msi);

@@ -196,10 +195,9 @@ void pci_disable_msix(struct pci_dev *dev)
	if (!pci_msi_enabled() || !dev || !dev->msix_enabled)
		return;

	msi_lock_descs(&dev->dev);
	guard(msi_descs_lock)(&dev->dev);
	pci_msix_shutdown(dev);
	pci_free_msi_irqs(dev);
	msi_unlock_descs(&dev->dev);
}
EXPORT_SYMBOL(pci_disable_msix);

+107 −60
Original line number Diff line number Diff line
@@ -336,41 +336,11 @@ static int msi_verify_entries(struct pci_dev *dev)
	return !entry ? 0 : -EIO;
}

/**
 * msi_capability_init - configure device's MSI capability structure
 * @dev: pointer to the pci_dev data structure of MSI device function
 * @nvec: number of interrupts to allocate
 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
 *
 * Setup the MSI capability structure of the device with the requested
 * number of interrupts.  A return value of zero indicates the successful
 * setup of an entry with the new MSI IRQ.  A negative return value indicates
 * an error, and a positive return value indicates the number of interrupts
 * which could have been allocated.
 */
static int msi_capability_init(struct pci_dev *dev, int nvec,
			       struct irq_affinity *affd)
static int __msi_capability_init(struct pci_dev *dev, int nvec, struct irq_affinity_desc *masks)
{
	struct irq_affinity_desc *masks = NULL;
	int ret = msi_setup_msi_desc(dev, nvec, masks);
	struct msi_desc *entry, desc;
	int ret;

	/* Reject multi-MSI early on irq domain enabled architectures */
	if (nvec > 1 && !pci_msi_domain_supports(dev, MSI_FLAG_MULTI_PCI_MSI, ALLOW_LEGACY))
		return 1;

	/*
	 * Disable MSI during setup in the hardware, but mark it enabled
	 * so that setup code can evaluate it.
	 */
	pci_msi_set_enable(dev, 0);
	dev->msi_enabled = 1;

	if (affd)
		masks = irq_create_affinity_masks(nvec, affd);

	msi_lock_descs(&dev->dev);
	ret = msi_setup_msi_desc(dev, nvec, masks);
	if (ret)
		goto fail;

@@ -399,19 +369,48 @@ static int msi_capability_init(struct pci_dev *dev, int nvec,

	pcibios_free_irq(dev);
	dev->irq = entry->irq;
	goto unlock;

	return 0;
err:
	pci_msi_unmask(&desc, msi_multi_mask(&desc));
	pci_free_msi_irqs(dev);
fail:
	dev->msi_enabled = 0;
unlock:
	msi_unlock_descs(&dev->dev);
	kfree(masks);
	return ret;
}

/**
 * msi_capability_init - configure device's MSI capability structure
 * @dev: pointer to the pci_dev data structure of MSI device function
 * @nvec: number of interrupts to allocate
 * @affd: description of automatic IRQ affinity assignments (may be %NULL)
 *
 * Setup the MSI capability structure of the device with the requested
 * number of interrupts.  A return value of zero indicates the successful
 * setup of an entry with the new MSI IRQ.  A negative return value indicates
 * an error, and a positive return value indicates the number of interrupts
 * which could have been allocated.
 */
static int msi_capability_init(struct pci_dev *dev, int nvec,
			       struct irq_affinity *affd)
{
	/* Reject multi-MSI early on irq domain enabled architectures */
	if (nvec > 1 && !pci_msi_domain_supports(dev, MSI_FLAG_MULTI_PCI_MSI, ALLOW_LEGACY))
		return 1;

	/*
	 * Disable MSI during setup in the hardware, but mark it enabled
	 * so that setup code can evaluate it.
	 */
	pci_msi_set_enable(dev, 0);
	dev->msi_enabled = 1;

	struct irq_affinity_desc *masks __free(kfree) =
		affd ? irq_create_affinity_masks(nvec, affd) : NULL;

	guard(msi_descs_lock)(&dev->dev);
	return __msi_capability_init(dev, nvec, masks);
}

int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec,
			   struct irq_affinity *affd)
{
@@ -666,40 +665,41 @@ static void msix_mask_all(void __iomem *base, int tsize)
		writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL);
}

static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
				 int nvec, struct irq_affinity *affd)
static int __msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
				   int nvec, struct irq_affinity_desc *masks)
{
	struct irq_affinity_desc *masks = NULL;
	int ret;

	if (affd)
		masks = irq_create_affinity_masks(nvec, affd);
	int ret = msix_setup_msi_descs(dev, entries, nvec, masks);

	msi_lock_descs(&dev->dev);
	ret = msix_setup_msi_descs(dev, entries, nvec, masks);
	if (ret)
		goto out_free;
		goto fail;

	ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
	if (ret)
		goto out_free;
		goto fail;

	/* Check if all MSI entries honor device restrictions */
	ret = msi_verify_entries(dev);
	if (ret)
		goto out_free;
		goto fail;

	msix_update_entries(dev, entries);
	goto out_unlock;
	return 0;

out_free:
fail:
	pci_free_msi_irqs(dev);
out_unlock:
	msi_unlock_descs(&dev->dev);
	kfree(masks);
	return ret;
}

static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
				 int nvec, struct irq_affinity *affd)
{
	struct irq_affinity_desc *masks __free(kfree) =
		affd ? irq_create_affinity_masks(nvec, affd) : NULL;

	guard(msi_descs_lock)(&dev->dev);
	return __msix_setup_interrupts(dev, entries, nvec, masks);
}

/**
 * msix_capability_init - configure device's MSI-X capability
 * @dev: pointer to the pci_dev data structure of MSI-X device function
@@ -871,13 +871,13 @@ void __pci_restore_msix_state(struct pci_dev *dev)

	write_msg = arch_restore_msi_irqs(dev);

	msi_lock_descs(&dev->dev);
	scoped_guard (msi_descs_lock, &dev->dev) {
		msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) {
			if (write_msg)
				__pci_write_msi_msg(entry, &entry->msg);
			pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl);
		}
	msi_unlock_descs(&dev->dev);
	}

	pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0);
}
@@ -916,6 +916,53 @@ void pci_free_msi_irqs(struct pci_dev *dev)
	}
}

#ifdef CONFIG_PCIE_TPH
/**
 * pci_msix_write_tph_tag - Update the TPH tag for a given MSI-X vector
 * @pdev:	The PCIe device to update
 * @index:	The MSI-X index to update
 * @tag:	The tag to write
 *
 * Returns: 0 on success, error code on failure
 */
int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag)
{
	struct msi_desc *msi_desc;
	struct irq_desc *irq_desc;
	unsigned int virq;

	if (!pdev->msix_enabled)
		return -ENXIO;

	guard(msi_descs_lock)(&pdev->dev);
	virq = msi_get_virq(&pdev->dev, index);
	if (!virq)
		return -ENXIO;
	/*
	 * This is a horrible hack, but short of implementing a PCI
	 * specific interrupt chip callback and a huge pile of
	 * infrastructure, this is the minor nuissance. It provides the
	 * protection against concurrent operations on this entry and keeps
	 * the control word cache in sync.
	 */
	irq_desc = irq_to_desc(virq);
	if (!irq_desc)
		return -ENXIO;

	guard(raw_spinlock_irq)(&irq_desc->lock);
	msi_desc = irq_data_get_msi_desc(&irq_desc->irq_data);
	if (!msi_desc || msi_desc->pci.msi_attrib.is_virtual)
		return -ENXIO;

	msi_desc->pci.msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_ST;
	msi_desc->pci.msix_ctrl |= FIELD_PREP(PCI_MSIX_ENTRY_CTRL_ST, tag);
	pci_msix_write_vector_ctrl(msi_desc, msi_desc->pci.msix_ctrl);
	/* Flush the write */
	readl(pci_msix_desc_addr(msi_desc));
	return 0;
}
#endif

/* Misc. infrastructure */

struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc)
+9 −0
Original line number Diff line number Diff line
@@ -989,6 +989,15 @@ int pcim_request_region_exclusive(struct pci_dev *pdev, int bar,
				  const char *name);
void pcim_release_region(struct pci_dev *pdev, int bar);

#ifdef CONFIG_PCI_MSI
int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag);
#else
static inline int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag)
{
	return -ENODEV;
}
#endif

/*
 * Config Address for PCI Configuration Mechanism #1
 *
Loading