Skip to contentSkip to conversation

genirq/msi: Spring cleaning

PATCH Applied Thomas Gleixner 11 participants 33 messages Mar 9, 2025 – Mar 13, 2025

Subsystems (12)

v1 of 4 Applied Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover genirq/msi: Spring cleaning

While converting the MSI descriptor locking to a lock guard() I stumbled over various abuse of MSI descriptors (again).

The following series cleans up the offending code and converts the MSI descriptor locking over to lock guard().

The series applies on Linus tree and is also available from git:

git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git irq/msi

Thanks,

tglx

---
 drivers/ntb/msi.c                   |   22 +----
 drivers/pci/controller/pci-hyperv.c |   14 ---
 drivers/pci/msi/api.c               |    6 -
 drivers/pci/msi/msi.c               |   77 ++++++++++++++----
 drivers/pci/pci.h                   |    9 ++
 drivers/pci/tph.c                   |   44 ----------
 drivers/soc/ti/ti_sci_inta_msi.c    |   10 --
 drivers/ufs/host/ufs-qcom.c         |   75 +++++++++---------
 include/linux/msi.h                 |   12 +-
 kernel/irq/msi.c                    |  150 ++++++++++++------------------------
 10 files changed, 181 insertions(+), 238 deletions(-)
#1 lore raw

PATCH 1/10 genirq/msi: Make a few functions static

None of these functions are used outside of the MSI core.

---
 include/linux/msi.h |    5 -----
 kernel/irq/msi.c    |   40 +++++++---------------------------------
 2 files changed, 7 insertions(+), 38 deletions(-)
include/linux/msi.h +0 -5
@@ -81,7 +81,6 @@ struct device_attribute;
 struct irq_domain;
 struct irq_affinity_desc;
 
-void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
 #ifdef CONFIG_GENERIC_MSI_IRQ
 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
 #else
@@ -603,8 +602,6 @@ void msi_remove_device_irq_domain(struct
 bool msi_match_device_irq_domain(struct device *dev, unsigned int domid,
 				 enum irq_domain_bus_token bus_token);
 
-int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last);
 int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
 				unsigned int first, unsigned int last);
 int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs);
@@ -613,8 +610,6 @@ struct msi_map msi_domain_alloc_irq_at(s
 				       const struct irq_affinity_desc *affdesc,
 				       union msi_instance_cookie *cookie);
 
-void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last);
 void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
 				unsigned int first, unsigned int last);
 void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid);
kernel/irq/msi.c +7 -33
@@ -270,16 +270,11 @@ static int msi_domain_add_simple_msi_des
 	return ret;
 }
 
-void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
-{
-	*msg = entry->msg;
-}
-
 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
 {
 	struct msi_desc *entry = irq_get_msi_desc(irq);
 
-	__get_cached_msi_msg(entry, msg);
+	*msg = entry->msg;
 }
 EXPORT_SYMBOL_GPL(get_cached_msi_msg);
 
@@ -1352,21 +1347,17 @@ static int msi_domain_alloc_locked(struc
 }
 
 /**
- * msi_domain_alloc_irqs_range_locked - Allocate interrupts from a MSI interrupt domain
+ * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain
  * @dev:	Pointer to device struct of the device for which the interrupts
  *		are allocated
  * @domid:	Id of the interrupt domain to operate on
  * @first:	First index to allocate (inclusive)
  * @last:	Last index to allocate (inclusive)
  *
- * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
- * pair. Use this for MSI irqdomains which implement their own descriptor
- * allocation/free.
- *
  * Return: %0 on success or an error code.
  */
-int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last)
+int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
+				unsigned int first, unsigned int last)
 {
 	struct msi_ctrl ctrl = {
 		.domid	= domid,
@@ -1374,27 +1365,10 @@ int msi_domain_alloc_irqs_range_locked(s
 		.last	= last,
 		.nirqs	= last + 1 - first,
 	};
-
-	return msi_domain_alloc_locked(dev, &ctrl);
-}
-
-/**
- * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain
- * @dev:	Pointer to device struct of the device for which the interrupts
- *		are allocated
- * @domid:	Id of the interrupt domain to operate on
- * @first:	First index to allocate (inclusive)
- * @last:	Last index to allocate (inclusive)
- *
- * Return: %0 on success or an error code.
- */
-int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
-				unsigned int first, unsigned int last)
-{
 	int ret;
 
 	msi_lock_descs(dev);
-	ret = msi_domain_alloc_irqs_range_locked(dev, domid, first, last);
+	ret = msi_domain_alloc_locked(dev, &ctrl);
 	msi_unlock_descs(dev);
 	return ret;
 }
@@ -1618,8 +1592,8 @@ static void msi_domain_free_locked(struc
  * @first:	First index to free (inclusive)
  * @last:	Last index to free (inclusive)
  */
-void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last)
+static void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
+					      unsigned int first, unsigned int last)
 {
 	struct msi_ctrl ctrl = {
 		.domid	= domid,
#3 lore raw

[tip: irq/msi] genirq/msi: Make a few functions static

In reply to Thomas Gleixner
The following commit has been merged into the irq/msi branch of tip:

The following commit has been merged into the irq/msi branch of tip:

Commit-ID:     537625233537179cb2e8293b2c0dc9c989363f41
Gitweb:        https://git.kernel.org/tip/537625233537179cb2e8293b2c0dc9c989363f41
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Sun, 09 Mar 2025 09:41:42 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Thu, 13 Mar 2025 13:35:33 +01:00

genirq/msi: Make a few functions static

None of these functions are used outside of the MSI core.

---
 include/linux/msi.h |  5 -----
 kernel/irq/msi.c    | 40 +++++++---------------------------------
 2 files changed, 7 insertions(+), 38 deletions(-)
include/linux/msi.h +0 -5
index b10093c..df2dd63 100644
@@ -81,7 +81,6 @@ struct device_attribute;
 struct irq_domain;
 struct irq_affinity_desc;
 
-void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg);
 #ifdef CONFIG_GENERIC_MSI_IRQ
 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg);
 #else
@@ -603,8 +602,6 @@ void msi_remove_device_irq_domain(struct device *dev, unsigned int domid);
 bool msi_match_device_irq_domain(struct device *dev, unsigned int domid,
 				 enum irq_domain_bus_token bus_token);
 
-int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last);
 int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
 				unsigned int first, unsigned int last);
 int msi_domain_alloc_irqs_all_locked(struct device *dev, unsigned int domid, int nirqs);
@@ -613,8 +610,6 @@ struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, u
 				       const struct irq_affinity_desc *affdesc,
 				       union msi_instance_cookie *cookie);
 
-void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last);
 void msi_domain_free_irqs_range(struct device *dev, unsigned int domid,
 				unsigned int first, unsigned int last);
 void msi_domain_free_irqs_all_locked(struct device *dev, unsigned int domid);
kernel/irq/msi.c +7 -33
index fa92882..57e6421 100644
@@ -270,16 +270,11 @@ fail:
 	return ret;
 }
 
-void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
-{
-	*msg = entry->msg;
-}
-
 void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
 {
 	struct msi_desc *entry = irq_get_msi_desc(irq);
 
-	__get_cached_msi_msg(entry, msg);
+	*msg = entry->msg;
 }
 EXPORT_SYMBOL_GPL(get_cached_msi_msg);
 
@@ -1352,21 +1347,17 @@ static int msi_domain_alloc_locked(struct device *dev, struct msi_ctrl *ctrl)
 }
 
 /**
- * msi_domain_alloc_irqs_range_locked - Allocate interrupts from a MSI interrupt domain
+ * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain
  * @dev:	Pointer to device struct of the device for which the interrupts
  *		are allocated
  * @domid:	Id of the interrupt domain to operate on
  * @first:	First index to allocate (inclusive)
  * @last:	Last index to allocate (inclusive)
  *
- * Must be invoked from within a msi_lock_descs() / msi_unlock_descs()
- * pair. Use this for MSI irqdomains which implement their own descriptor
- * allocation/free.
- *
  * Return: %0 on success or an error code.
  */
-int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last)
+int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
+				unsigned int first, unsigned int last)
 {
 	struct msi_ctrl ctrl = {
 		.domid	= domid,
@@ -1374,27 +1365,10 @@ int msi_domain_alloc_irqs_range_locked(struct device *dev, unsigned int domid,
 		.last	= last,
 		.nirqs	= last + 1 - first,
 	};
-
-	return msi_domain_alloc_locked(dev, &ctrl);
-}
-
-/**
- * msi_domain_alloc_irqs_range - Allocate interrupts from a MSI interrupt domain
- * @dev:	Pointer to device struct of the device for which the interrupts
- *		are allocated
- * @domid:	Id of the interrupt domain to operate on
- * @first:	First index to allocate (inclusive)
- * @last:	Last index to allocate (inclusive)
- *
- * Return: %0 on success or an error code.
- */
-int msi_domain_alloc_irqs_range(struct device *dev, unsigned int domid,
-				unsigned int first, unsigned int last)
-{
 	int ret;
 
 	msi_lock_descs(dev);
-	ret = msi_domain_alloc_irqs_range_locked(dev, domid, first, last);
+	ret = msi_domain_alloc_locked(dev, &ctrl);
 	msi_unlock_descs(dev);
 	return ret;
 }
@@ -1618,8 +1592,8 @@ static void msi_domain_free_locked(struct device *dev, struct msi_ctrl *ctrl)
  * @first:	First index to free (inclusive)
  * @last:	Last index to free (inclusive)
  */
-void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
-				       unsigned int first, unsigned int last)
+static void msi_domain_free_irqs_range_locked(struct device *dev, unsigned int domid,
+					      unsigned int first, unsigned int last)
 {
 	struct msi_ctrl ctrl = {
 		.domid	= domid,
#4 lore raw

PATCH 2/10 genirq/msi: Use lock guards for MSI descriptor locking

In reply to Thomas Gleixner

Provide a lock guard for MSI descriptor locking and update the core code accordingly.

No functional change intended.

---
 include/linux/msi.h |    3 +
 kernel/irq/msi.c    |  104 +++++++++++++++++++---------------------------------
 2 files changed, 42 insertions(+), 65 deletions(-)
include/linux/msi.h +3 -0
@@ -227,6 +227,9 @@ int msi_setup_device_data(struct device
 void msi_lock_descs(struct device *dev);
 void msi_unlock_descs(struct device *dev);
 
+DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, msi_lock_descs(_T->lock),
+		    msi_unlock_descs(_T->lock));
+
 struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
 				       enum msi_desc_filter filter);
 
kernel/irq/msi.c +39 -65

198 lines not on this page · raw message

Reviewed-by on v1 2/10
#5 lore raw

Re: [patch 02/10] genirq/msi: Use lock guards for MSI descriptor locking

Quoted 6 lines · On Sun, 9 Mar 2025 09:41:44 +0100 (CET) Thomas Gleixner <[email protected]>…

Provide a lock guard for MSI descriptor locking and update the core code accordingly.

No functional change intended.

One case in here made me go back to the helpful docs Dan wrote after early confusion on how to use this stuff. We might want to consider updating as you've found a case that I at least didn't consider when reviewing those. +CC Dan.

Anyhow, this seems fine to me.

kernel/irq/msi.c bool msi_create_device_irq_domain(struct · line 1032

Quoted 8 lines
kernel/irq/msi.c +0 -0
@@ -443,7 +443,6 @@ EXPORT_SYMBOL_GPL(msi_next_desc);
 
 
@@ -1037,25 +1032,23 @@ bool msi_create_device_irq_domain(struct
 	if (msi_setup_device_data(dev))

Hmm. We might want to make the docs in cleanup.h more nuanced. They specifically say to not mix goto and auto cleanup, but in the case of scoped_guard() unlikely almost any other case it should be fine.

Quoted 40 lines
kernel/irq/msi.c +16 -18
 		goto free_fwnode;
 
-	msi_lock_descs(dev);
-
-	if (WARN_ON_ONCE(msi_get_device_domain(dev, domid)))
-		goto fail;
-
-	if (!pops->init_dev_msi_info(dev, parent, parent, &bundle->info))
-		goto fail;
-
-	domain = __msi_create_irq_domain(fwnode, &bundle->info, IRQ_DOMAIN_FLAG_MSI_DEVICE, parent);
-	if (!domain)
-		goto fail;
-
-	domain->dev = dev;
-	dev->msi.data->__domains[domid].domain = domain;
-	msi_unlock_descs(dev);
-	return true;
+	scoped_guard(msi_descs_lock, dev) {
+		if (WARN_ON_ONCE(msi_get_device_domain(dev, domid)))
+			goto free_fwnode;
+
+		if (!pops->init_dev_msi_info(dev, parent, parent, &bundle->info))
+			goto free_fwnode;
+
+		domain = __msi_create_irq_domain(fwnode, &bundle->info, IRQ_DOMAIN_FLAG_MSI_DEVICE,
+						 parent);
+		if (!domain)
+			goto free_fwnode;
+
+		domain->dev = dev;
+		dev->msi.data->__domains[domid].domain = domain;
+		return true;
+	}
 
-fail:
-	msi_unlock_descs(dev);
 free_fwnode:
 	irq_domain_free_fwnode(fwnalloced);
 free_bundle:
#6 lore raw

Re: [patch 02/10] genirq/msi: Use lock guards for MSI descriptor locking

Quoted 4 lines · Jonathan Cameron
Quoted 1 line · On Sun, 9 Mar 2025 09:41:44 +0100 (CET) Thomas Gleixner <[email protected]>…
Quoted 9 lines

Review bool msi_create_device_irq_domain(struct · line 1032

Quoted 2 lines
(quoted diff) +0 -0
@@ -1037,25 +1032,23 @@ bool msi_create_device_irq_domain(struct
 	if (msi_setup_device_data(dev))

Hmm. We might want to make the docs in cleanup.h more nuanced. They specifically say to not mix goto and auto cleanup, but in the case of scoped_guard() unlikely almost any other case it should be fine.

Quoted 1 line
(quoted diff) +0 -0
 		goto free_fwnode;

I got rid of the gotos. It requires __free() for the two allocations.

Thanks,

tglx
---

include/linux/cleanup.h +2 -0
@@ -216,6 +216,8 @@ const volatile void * __must_check_fn(co
 
 #define return_ptr(p)	return no_free_ptr(p)
 
+#define retain_ptr(p)				\
+	__get_and_null(p, NULL)
 
 /*
  * DEFINE_CLASS(name, type, exit, init, init_args...):
include/linux/irqdomain.h +2 -0
@@ -281,6 +281,8 @@ static inline struct fwnode_handle *irq_
 
 void irq_domain_free_fwnode(struct fwnode_handle *fwnode);
 
+DEFINE_FREE(irq_domain_free_fwnode, struct fwnode_handle *, if (_T) irq_domain_free_fwnode(_T))
+
 struct irq_domain_chip_generic_info;
 
 /**
include/linux/msi.h +3 -0
@@ -227,6 +227,9 @@ int msi_setup_device_data(struct device
 void msi_lock_descs(struct device *dev);
 void msi_unlock_descs(struct device *dev);
 
+DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, msi_lock_descs(_T->lock),
+		    msi_unlock_descs(_T->lock));
+
 struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
 				       enum msi_desc_filter filter);
 
kernel/irq/msi.c +40 -69

231 lines not on this page · raw message

#7 lore raw

Re: [patch 02/10] genirq/msi: Use lock guards for MSI descriptor locking

Quoted 13 lines · On Tue, 11 Mar 2025 22:26:52 +0100 Thomas Gleixner <[email protected]> wrote:
Quoted 12 lines · Jonathan Cameron

kernel/irq/msi.c bool msi_create_device_irq_domain(struct · line 1032

Quoted 3 lines · On Sun, 9 Mar 2025 09:41:44 +0100 (CET) Thomas Gleixner <[email protected]>…
kernel/irq/msi.c +0 -0
@@ -1037,25 +1032,23 @@ bool msi_create_device_irq_domain(struct
 	if (msi_setup_device_data(dev))  

Hmm. We might want to make the docs in cleanup.h more nuanced. They specifically say to not mix goto and auto cleanup, but in the case of scoped_guard() unlikely almost any other case it should be fine.

Quoted 3 lines
Quoted 1 line

Review bool msi_create_device_irq_domain(struct · line 1033

Quoted 1 line
(quoted diff) +0 -0
 		goto free_fwnode;  

I got rid of the gotos. It requires __free() for the two allocations.

That works.

include/linux/cleanup.h const volatile void * __must_check_fn(co · line 220

Quoted 13 lines

Thanks,

tglx
---

include/linux/cleanup.h +2 -0
@@ -216,6 +216,8 @@ const volatile void * __must_check_fn(co
 
 #define return_ptr(p)	return no_free_ptr(p)
 
+#define retain_ptr(p)				\
+	__get_and_null(p, NULL)

Single line?

This sort of thing got discussed in the past though I doubt I can find the thread. There was some push back but maybe now is it's time!

Probably worth shouting about it a bit to attract attention. Maybe a separate patch.

Jonathan

#8 lore raw

Re: [patch 02/10] genirq/msi: Use lock guards for MSI descriptor locking

Quoted 9 lines · Jonathan Cameron

include/linux/cleanup.h const volatile void * __must_check_fn(co · line 220

Quoted 5 lines · On Tue, 11 Mar 2025 22:26:52 +0100 Thomas Gleixner <[email protected]> wrote:

#define return_ptr(p) return no_free_ptr(p)

include/linux/cleanup.h +2 -0
+#define retain_ptr(p)				\
+	__get_and_null(p, NULL)

Single line?

Quoted 5 lines

This sort of thing got discussed in the past though I doubt I can find the thread. There was some push back but maybe now is it's time!

Probably worth shouting about it a bit to attract attention. Maybe a separate patch.

Yes of course.

#9 lore raw

PATCH 3/10 soc: ti: ti_sci_inta_msi: Switch MSI descriptor locking to guard()

In reply to Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

Cc Nishanth Menon <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Santosh Shilimkar <[email protected]>
---
 drivers/soc/ti/ti_sci_inta_msi.c |   10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/soc/ti/ti_sci_inta_msi.c +3 -7
@@ -103,19 +103,15 @@ int ti_sci_inta_msi_domain_alloc_irqs(st
 	if (ret)
 		return ret;
 
-	msi_lock_descs(dev);
+	guard(msi_descs_lock)(dev);
 	nvec = ti_sci_inta_msi_alloc_descs(dev, res);
-	if (nvec <= 0) {
-		ret = nvec;
-		goto unlock;
-	}
+	if (nvec <= 0)
+		return nvec;
 
 	/* Use alloc ALL as it's unclear whether there are gaps in the indices */
 	ret = msi_domain_alloc_irqs_all_locked(dev, MSI_DEFAULT_DOMAIN, nvec);
 	if (ret)
 		dev_err(dev, "Failed to allocate IRQs %d\n", ret);
-unlock:
-	msi_unlock_descs(dev);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_alloc_irqs);
Reviewed-by on v1 3/10
#10 lore raw

Re: [patch 03/10] soc: ti: ti_sci_inta_msi: Switch MSI descriptor locking to guard()

Quoted 8 lines · On Sun, 9 Mar 2025 09:41:46 +0100 (CET) Thomas Gleixner <[email protected]>…

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

Cc Nishanth Menon <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Santosh Shilimkar <[email protected]>
maintainer Tested-by on v1 3/10
#11 lore raw

Re: [patch 03/10] soc: ti: ti_sci_inta_msi: Switch MSI descriptor locking to guard()

In reply to Thomas Gleixner
Quoted 28 lines · Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

Cc Nishanth Menon <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Santosh Shilimkar <[email protected]>
---
 drivers/soc/ti/ti_sci_inta_msi.c |   10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/soc/ti/ti_sci_inta_msi.c +3 -5
@@ -103,19 +103,15 @@ int ti_sci_inta_msi_domain_alloc_irqs(st
 	if (ret)
 		return ret;
 
-	msi_lock_descs(dev);
+	guard(msi_descs_lock)(dev);
 	nvec = ti_sci_inta_msi_alloc_descs(dev, res);
-	if (nvec <= 0) {
-		ret = nvec;
-		goto unlock;
-	}
+	if (nvec <= 0)
+		return nvec;

drivers/soc/ti/ti_sci_inta_msi.c int ti_sci_inta_msi_domain_alloc_irqs(st · line 117

Quoted 10 lines
drivers/soc/ti/ti_sci_inta_msi.c +0 -2
 
 	/* Use alloc ALL as it's unclear whether there are gaps in the indices */
 	ret = msi_domain_alloc_irqs_all_locked(dev, MSI_DEFAULT_DOMAIN, nvec);
 	if (ret)
 		dev_err(dev, "Failed to allocate IRQs %d\n", ret);
-unlock:
-	msi_unlock_descs(dev);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_alloc_irqs);

Quick test of the series for basic NFS boot (which uses INTR/INTA MSI for Ethernet) on TI K3 platforms against linux-next:
https://gist.github.com/nmenon/26ea6eb530de34808ab04b1958a0b28b

Signature · 3 lines
Regards,
Nishanth Menon
Key (0xDDB5849D1736249D) / Fingerprint: F8A2 8693 54EB 8232 17A3  1A34 DDB5 849D 1736 249D
Reviewed-by on v1 3/10
#12 lore raw

Re: [patch 03/10] soc: ti: ti_sci_inta_msi: Switch MSI descriptor locking to guard()

In reply to Thomas Gleixner
Quoted 11 lines · Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

Cc Nishanth Menon <[email protected]>
Cc Tero Kristo <[email protected]>
Cc Santosh Shilimkar <[email protected]>
---
 drivers/soc/ti/ti_sci_inta_msi.c |   10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
Quoted 28 lines
drivers/soc/ti/ti_sci_inta_msi.c +3 -7
@@ -103,19 +103,15 @@ int ti_sci_inta_msi_domain_alloc_irqs(st
 	if (ret)
 		return ret;
 
-	msi_lock_descs(dev);
+	guard(msi_descs_lock)(dev);
 	nvec = ti_sci_inta_msi_alloc_descs(dev, res);
-	if (nvec <= 0) {
-		ret = nvec;
-		goto unlock;
-	}
+	if (nvec <= 0)
+		return nvec;
 
 	/* Use alloc ALL as it's unclear whether there are gaps in the indices */
 	ret = msi_domain_alloc_irqs_all_locked(dev, MSI_DEFAULT_DOMAIN, nvec);
 	if (ret)
 		dev_err(dev, "Failed to allocate IRQs %d\n", ret);
-unlock:
-	msi_unlock_descs(dev);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_alloc_irqs);
Signature · 3 lines
Best regards,
Dhruva Gole
Texas Instruments Incorporated
#13 lore raw

PATCH 4/10 NTB/msi: Switch MSI descriptor locking to lock guard()

In reply to Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

---
 drivers/ntb/msi.c |   22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/ntb/msi.c +8 -14
@@ -106,10 +106,10 @@ int ntb_msi_setup_mws(struct ntb_dev *nt
 	if (!ntb->msi)
 		return -EINVAL;
 
-	msi_lock_descs(&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);
+	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);
+	}
 
 	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
 	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
 		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);
 
maintainer Acked-by on v1 4/10
#14 lore raw

Re: [patch 04/10] NTB/msi: Switch MSI descriptor locking to lock guard()

Quoted 9 lines · Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

Quoted 52 lines
---
 drivers/ntb/msi.c |   22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/ntb/msi.c +8 -14
@@ -106,10 +106,10 @@ int ntb_msi_setup_mws(struct ntb_dev *nt
 	if (!ntb->msi)
 		return -EINVAL;
 
-	msi_lock_descs(&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);
+	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);
+	}
 
 	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
 	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
 		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);
 
Reviewed-by on v1 4/10
#15 lore raw

Re: [patch 04/10] NTB/msi: Switch MSI descriptor locking to lock guard()

In reply to Thomas Gleixner
Quoted 9 lines · Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

Looks really nice to me, thanks.

Reviewed-by on v1 4/10
#16 lore raw

Re: [patch 04/10] NTB/msi: Switch MSI descriptor locking to lock guard()

In reply to Thomas Gleixner
Quoted 9 lines · On Sun, 9 Mar 2025 09:41:48 +0100 (CET) Thomas Gleixner <[email protected]>…

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

#17 lore raw

PATCH 5/10 PCI/MSI: Switch to MSI descriptor locking to guard()

In reply to Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

---
 drivers/pci/msi/api.c |    6 ++----
 drivers/pci/msi/msi.c |   30 ++++++++++++------------------
 2 files changed, 14 insertions(+), 22 deletions(-)
drivers/pci/msi/api.c +2 -4
@@ -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 *de
 	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);
 
drivers/pci/msi/msi.c +12 -18
@@ -351,7 +351,7 @@ static int msi_verify_entries(struct pci
 static int msi_capability_init(struct pci_dev *dev, int nvec,
 			       struct irq_affinity *affd)
 {
-	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity_desc *masks __free(kfree) = NULL;
 	struct msi_desc *entry, desc;
 	int ret;
 
@@ -369,7 +369,7 @@ static int msi_capability_init(struct pc
 	if (affd)
 		masks = irq_create_affinity_masks(nvec, affd);
 
-	msi_lock_descs(&dev->dev);
+	guard(msi_descs_lock)(&dev->dev);
 	ret = msi_setup_msi_desc(dev, nvec, masks);
 	if (ret)
 		goto fail;
@@ -399,16 +399,13 @@ static int msi_capability_init(struct pc
 
 	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;
 }
 
@@ -669,13 +666,13 @@ static void msix_mask_all(void __iomem *
 static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
 				 int nvec, struct irq_affinity *affd)
 {
-	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity_desc *masks __free(kfree) = NULL;
 	int ret;
 
 	if (affd)
 		masks = irq_create_affinity_masks(nvec, affd);
 
-	msi_lock_descs(&dev->dev);
+	guard(msi_descs_lock)(&dev->dev);
 	ret = msix_setup_msi_descs(dev, entries, nvec, masks);
 	if (ret)
 		goto out_free;
@@ -690,13 +687,10 @@ static int msix_setup_interrupts(struct
 		goto out_free;
 
 	msix_update_entries(dev, entries);
-	goto out_unlock;
+	return 0;
 
 out_free:
 	pci_free_msi_irqs(dev);
-out_unlock:
-	msi_unlock_descs(&dev->dev);
-	kfree(masks);
 	return ret;
 }
 
@@ -871,13 +865,13 @@ void __pci_restore_msix_state(struct pci
 
 	write_msg = arch_restore_msi_irqs(dev);
 
-	msi_lock_descs(&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);
+	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);
 }
#18 lore raw

Re: [patch 05/10] PCI/MSI: Switch to MSI descriptor locking to guard()

Quoted 7 lines · On Sun, 9 Mar 2025 09:41:49 +0100 (CET) Thomas Gleixner <[email protected]>…

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

This one runs into some of the stuff that the docs say to not do. I don't there there are bugs in here as such but it gets harder to reason about and fragile in the long run. Easy enough to avoid though - see inline.

Jonathan

drivers/pci/msi/msi.c static int msi_verify_entries(struct pci · line 354

Quoted 8 lines
drivers/pci/msi/msi.c +1 -1
@@ -351,7 +351,7 @@ static int msi_verify_entries(struct pci
 static int msi_capability_init(struct pci_dev *dev, int nvec,
 			       struct irq_affinity *affd)
 {
-	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity_desc *masks __free(kfree) = NULL;

This is a pattern that the cleanup.h docs talk about that Linus really didn't like in some of the early patches because of wanting constructors and destructors together.

Maybe use an inline definition as

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

drivers/pci/msi/msi.c static int msi_capability_init(struct pc · line 375

Quoted 12 lines
drivers/pci/msi/msi.c +1 -1
 	struct msi_desc *entry, desc;
 	int ret;
 
@@ -369,7 +369,7 @@ static int msi_capability_init(struct pc
 	if (affd)
 		masks = irq_create_affinity_masks(nvec, affd);
 
-	msi_lock_descs(&dev->dev);
+	guard(msi_descs_lock)(&dev->dev);
 	ret = msi_setup_msi_desc(dev, nvec, masks);
 	if (ret)
 		goto fail;

This runs against the advice in cleanup.h. It is probably correct and doesn't hit the undefined paths that motivated that guidance, but still maybe worth avoiding the mix of cleanup and goto handling.

Easiest way being to factor out the code after guard to a helper.

Quoted 19 lines
drivers/pci/msi/msi.c +1 -4
@@ -399,16 +399,13 @@ static int msi_capability_init(struct pc
 
 	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;
 }
 

drivers/pci/msi/msi.c static void msix_mask_all(void __iomem * · line 669

Quoted 5 lines
drivers/pci/msi/msi.c +1 -1
@@ -669,13 +666,13 @@ static void msix_mask_all(void __iomem *
 static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
 				 int nvec, struct irq_affinity *affd)
 {
-	struct irq_affinity_desc *masks = NULL;
+	struct irq_affinity_desc *masks __free(kfree) = NULL;

Similar to above.

Quoted 11 lines
drivers/pci/msi/msi.c +1 -1
 	int ret;
 
 	if (affd)
 		masks = irq_create_affinity_masks(nvec, affd);
 
-	msi_lock_descs(&dev->dev);
+	guard(msi_descs_lock)(&dev->dev);
 	ret = msix_setup_msi_descs(dev, entries, nvec, masks);
 	if (ret)
 		goto out_free;

drivers/pci/msi/msi.c static int msix_setup_interrupts(struct · line 692

Quoted 7 lines
drivers/pci/msi/msi.c +1 -1
@@ -690,13 +687,10 @@ static int msix_setup_interrupts(struct
 		goto out_free;
 
 	msix_update_entries(dev, entries);
-	goto out_unlock;
+	return 0;
 
 out_free:

Here as well.

Quoted 30 lines
drivers/pci/msi/msi.c +6 -9
 	pci_free_msi_irqs(dev);
-out_unlock:
-	msi_unlock_descs(&dev->dev);
-	kfree(masks);
 	return ret;
 }
 
@@ -871,13 +865,13 @@ void __pci_restore_msix_state(struct pci
 
 	write_msg = arch_restore_msi_irqs(dev);
 
-	msi_lock_descs(&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);
+	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);
 }
#19 lore raw

Re: [patch 05/10] PCI/MSI: Switch to MSI descriptor locking to guard()

Quoted 7 lines · Jonathan Cameron

On Sun, 9 Mar 2025 09:41:49 +0100 (CET)
Thomas Gleixner <[email protected]> wrote:

This one runs into some of the stuff that the docs say to not do. I don't there there are bugs in here as such but it gets harder to reason about and fragile in the long run. Easy enough to avoid though - see inline.

Right. Updated variant below.

Thanks,

tglx
---

drivers/pci/msi/api.c +2 -4
@@ -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 *de
 	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);
 
drivers/pci/msi/msi.c +60 -61

170 lines not on this page · raw message

#20 lore raw

Re: [patch 05/10] PCI/MSI: Switch to MSI descriptor locking to guard()

Quoted 10 lines · On Tue, 11 Mar 2025 22:45:44 +0100 Thomas Gleixner <[email protected]> wrote:
Quoted 7 lines · Jonathan Cameron

On Sun, 9 Mar 2025 09:41:49 +0100 (CET)
Thomas Gleixner <[email protected]> wrote:

This one runs into some of the stuff that the docs say to not do. I don't there there are bugs in here as such but it gets harder to reason about and fragile in the long run. Easy enough to avoid though - see inline.

Right. Updated variant below.

LGTM

Thanks,

Jonathan

#21 lore raw

PATCH 6/10 PCI: hv: Switch MSI descriptor locking to guard()

In reply to Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

---
 drivers/pci/controller/pci-hyperv.c |   14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/pci/controller/pci-hyperv.c +4 -10
@@ -3976,24 +3976,18 @@ static int hv_pci_restore_msi_msg(struct
 {
 	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;
 }
 
 /*
maintainer Acked-by on v1 6/10
#22 lore raw

Re: [patch 06/10] PCI: hv: Switch MSI descriptor locking to guard()

Quoted 10 lines · Thomas Gleixner

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

Quoted 36 lines
---
 drivers/pci/controller/pci-hyperv.c |   14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/pci/controller/pci-hyperv.c +4 -10
@@ -3976,24 +3976,18 @@ static int hv_pci_restore_msi_msg(struct
 {
 	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;
 }
 
 /*
Reviewed-by on v1 6/10
#23 lore raw

RE: [patch 06/10] PCI: hv: Switch MSI descriptor locking to guard()

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]> Sent: Sunday, March 9, 2025 12:42 AM

Quoted 35 lines

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

---
 drivers/pci/controller/pci-hyperv.c |   14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/pci/controller/pci-hyperv.c +1 -7
@@ -3976,24 +3976,18 @@ static int hv_pci_restore_msi_msg(struct
 {
 	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;
-		}
-

drivers/pci/controller/pci-hyperv.c static int hv_pci_restore_msi_msg(struct · line 3992

Quoted 10 lines
drivers/pci/controller/pci-hyperv.c +3 -3
+		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;
 }
 
Reviewed-by on v1 6/10
#24 lore raw

Re: [patch 06/10] PCI: hv: Switch MSI descriptor locking to guard()

In reply to Thomas Gleixner
Quoted 10 lines · On Sun, 9 Mar 2025 09:41:51 +0100 (CET) Thomas Gleixner <[email protected]>…

Convert the code to use the new guard(msi_descs_lock).

No functional change intended.

#25 lore raw

PATCH 7/10 PCI/MSI: Provide a sane mechanism for TPH

In reply to Thomas Gleixner

The PCI/TPH driver fiddles with the MSI-X control word of an active interrupt completely unserialized against concurrent operations issued from the interrupt core. It also brings the PCI/MSI-X internal cached control word out of sync.

Provide a function, which has the required serialization and keeps the control word cache in sync.

Unfortunately this requires to look up and lock the interrupt descriptor, which should be only done in the interrupt core code. But confining this particular oddity in the PCI/MSI core is the lesser of all evil. A interrupt core implementation would require a larger pile of infrastructure and indirections for dubious value.

---
 drivers/pci/msi/msi.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/pci/pci.h     |    9 +++++++++
 2 files changed, 56 insertions(+)
drivers/pci/msi/msi.c +47 -0
@@ -910,6 +910,53 @@ void pci_free_msi_irqs(struct pci_dev *d
 	}
 }
 
+#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)
drivers/pci/pci.h +9 -0
@@ -989,6 +989,15 @@ int pcim_request_region_exclusive(struct
 				  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
  *
#26 lore raw

PATCH 8/10 PCI/TPH: Replace the broken MSI-X control word update

In reply to Thomas Gleixner

The driver walks the MSI descriptors to test whether a descriptor exists for a given index. That's just abuse of the MSI internals.

The same test can be done with a single function call by looking up whether there is a Linux interrupt number assigned at the index.

What's worse is that the function is completely unserialized against modifications of the MSI-X control by operations issued from the interrupt core. It also brings the PCI/MSI-X internal cached control word out of sync.

Remove the trainwreck and invoke the function provided by the PCI/MSI core to update it.

---
 drivers/pci/tph.c |   44 +-------------------------------------------
 1 file changed, 1 insertion(+), 43 deletions(-)
drivers/pci/tph.c +1 -43
@@ -204,48 +204,6 @@ static u8 get_rp_completer_type(struct p
 	return FIELD_GET(PCI_EXP_DEVCAP2_TPH_COMP_MASK, reg);
 }
 
-/* Write ST to MSI-X vector control reg - Return 0 if OK, otherwise -errno */
-static int write_tag_to_msix(struct pci_dev *pdev, int msix_idx, u16 tag)
-{
-#ifdef CONFIG_PCI_MSI
-	struct msi_desc *msi_desc = NULL;
-	void __iomem *vec_ctrl;
-	u32 val;
-	int err = 0;
-
-	msi_lock_descs(&pdev->dev);
-
-	/* Find the msi_desc entry with matching msix_idx */
-	msi_for_each_desc(msi_desc, &pdev->dev, MSI_DESC_ASSOCIATED) {
-		if (msi_desc->msi_index == msix_idx)
-			break;
-	}
-
-	if (!msi_desc) {
-		err = -ENXIO;
-		goto err_out;
-	}
-
-	/* Get the vector control register (offset 0xc) pointed by msix_idx */
-	vec_ctrl = pdev->msix_base + msix_idx * PCI_MSIX_ENTRY_SIZE;
-	vec_ctrl += PCI_MSIX_ENTRY_VECTOR_CTRL;
-
-	val = readl(vec_ctrl);
-	val &= ~PCI_MSIX_ENTRY_CTRL_ST;
-	val |= FIELD_PREP(PCI_MSIX_ENTRY_CTRL_ST, tag);
-	writel(val, vec_ctrl);
-
-	/* Read back to flush the update */
-	val = readl(vec_ctrl);
-
-err_out:
-	msi_unlock_descs(&pdev->dev);
-	return err;
-#else
-	return -ENODEV;
-#endif
-}
-
 /* Write tag to ST table - Return 0 if OK, otherwise -errno */
 static int write_tag_to_st_table(struct pci_dev *pdev, int index, u16 tag)
 {
@@ -346,7 +304,7 @@ int pcie_tph_set_st_entry(struct pci_dev
 
 	switch (loc) {
 	case PCI_TPH_LOC_MSIX:
-		err = write_tag_to_msix(pdev, index, tag);
+		err = pci_msix_write_tph_tag(pdev, index, tag);
 		break;
 	case PCI_TPH_LOC_CAP:
 		err = write_tag_to_st_table(pdev, index, tag);
#27 lore raw

PATCH 9/10 scsi: ufs: qcom: Remove the MSI descriptor abuse

In reply to Thomas Gleixner

The driver abuses the MSI descriptors for internal purposes. Aside of core code and MSI providers nothing has to care about their existence. They have been encapsulated with a lot of effort because this kind of abuse caused all sorts of issues including a maintainability nightmare.

Rewrite the code so it uses dedicated storage to hand the required information to the interrupt handler.

No functional change intended.

Cc Manivannan Sadhasivam <[email protected]>
Cc "James E.J. Bottomley" <[email protected]>
Cc "Martin K. Petersen" <[email protected]>
---
 drivers/ufs/host/ufs-qcom.c |   77 ++++++++++++++++++++++----------------------
 1 file changed, 40 insertions(+), 37 deletions(-)
drivers/ufs/host/ufs-qcom.c +40 -37
@@ -1782,15 +1782,19 @@ static void ufs_qcom_write_msi_msg(struc
 	ufshcd_mcq_config_esi(hba, msg);
 }
 
+struct ufs_qcom_irq {
+	unsigned int		irq;
+	unsigned int		idx;
+	struct ufs_hba		*hba;
+};
+
 static irqreturn_t ufs_qcom_mcq_esi_handler(int irq, void *data)
 {
-	struct msi_desc *desc = data;
-	struct device *dev = msi_desc_to_dev(desc);
-	struct ufs_hba *hba = dev_get_drvdata(dev);
-	u32 id = desc->msi_index;
-	struct ufs_hw_queue *hwq = &hba->uhq[id];
+	struct ufs_qcom_irq *qi = data;
+	struct ufs_hba *hba = qi->hba;
+	struct ufs_hw_queue *hwq = &hba->uhq[qi->idx];
 
-	ufshcd_mcq_write_cqis(hba, 0x1, id);
+	ufshcd_mcq_write_cqis(hba, 0x1, qi->idx);
 	ufshcd_mcq_poll_cqe_lock(hba, hwq);
 
 	return IRQ_HANDLED;
@@ -1799,8 +1803,7 @@ static irqreturn_t ufs_qcom_mcq_esi_hand
 static int ufs_qcom_config_esi(struct ufs_hba *hba)
 {
 	struct ufs_qcom_host *host = ufshcd_get_variant(hba);
-	struct msi_desc *desc;
-	struct msi_desc *failed_desc = NULL;
+	struct ufs_qcom_irq *qi;
 	int nr_irqs, ret;
 
 	if (host->esi_enabled)
@@ -1811,47 +1814,47 @@ static int ufs_qcom_config_esi(struct uf
 	 * 2. Poll queues do not need ESI.
 	 */
 	nr_irqs = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL];
+	qi = devm_kcalloc(hba->dev, nr_irqs, sizeof(*qi), GFP_KERNEL);
+	if (qi)
+		return -ENOMEM;
+
 	ret = platform_device_msi_init_and_alloc_irqs(hba->dev, nr_irqs,
 						      ufs_qcom_write_msi_msg);
 	if (ret) {
 		dev_err(hba->dev, "Failed to request Platform MSI %d\n", ret);
-		return ret;
+		goto cleanup;
 	}
 
-	msi_lock_descs(hba->dev);
-	msi_for_each_desc(desc, hba->dev, MSI_DESC_ALL) {
-		ret = devm_request_irq(hba->dev, desc->irq,
-				       ufs_qcom_mcq_esi_handler,
-				       IRQF_SHARED, "qcom-mcq-esi", desc);
+	for (int idx = 0; idx < nr_irqs; idx++) {
+		qi[idx].irq = msi_get_virq(hba->dev, idx);
+		qi[idx].idx = idx;
+		qi[idx].hba = hba;
+
+		ret = devm_request_irq(hba->dev, qi[idx].irq, ufs_qcom_mcq_esi_handler,
+				       IRQF_SHARED, "qcom-mcq-esi", qi + idx);
 		if (ret) {
 			dev_err(hba->dev, "%s: Fail to request IRQ for %d, err = %d\n",
-				__func__, desc->irq, ret);
-			failed_desc = desc;
-			break;
+				__func__, qi[idx].irq, ret);
+			qi[idx].irq = 0;
+			goto cleanup;
 		}
 	}
-	msi_unlock_descs(hba->dev);
 
-	if (ret) {
-		/* Rewind */
-		msi_lock_descs(hba->dev);
-		msi_for_each_desc(desc, hba->dev, MSI_DESC_ALL) {
-			if (desc == failed_desc)
-				break;
-			devm_free_irq(hba->dev, desc->irq, hba);
-		}
-		msi_unlock_descs(hba->dev);
-		platform_device_msi_free_irqs_all(hba->dev);
-	} else {
-		if (host->hw_ver.major == 6 && host->hw_ver.minor == 0 &&
-		    host->hw_ver.step == 0)
-			ufshcd_rmwl(hba, ESI_VEC_MASK,
-				    FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1),
-				    REG_UFS_CFG3);
-		ufshcd_mcq_enable_esi(hba);
-		host->esi_enabled = true;
+	if (host->hw_ver.major == 6 && host->hw_ver.minor == 0 &&
+	    host->hw_ver.step == 0) {
+		ufshcd_rmwl(hba, ESI_VEC_MASK,
+			    FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1),
+			    REG_UFS_CFG3);
 	}
-
+	ufshcd_mcq_enable_esi(hba);
+	host->esi_enabled = true;
+	return 0;
+
+cleanup:
+	for (int idx = 0; qi[idx].irq; idx++)
+		devm_free_irq(hba->dev, qi[idx].irq, hba);
+	platform_device_msi_free_irqs_all(hba->dev);
+	devm_kfree(hba->dev, qi);
 	return ret;
 }
 
#28 lore raw

PATCH 10/10 genirq/msi: Rename msi_[un]lock_descs()

In reply to Thomas Gleixner

Now that all abuse is gone and the legit users are converted to guard(msi_descs_lock), rename the lock functions and document them as internal.

No functional chance.

---
 include/linux/msi.h |    8 ++++----
 kernel/irq/msi.c    |   16 ++++++++++------
 2 files changed, 14 insertions(+), 10 deletions(-)
include/linux/msi.h +4 -4
@@ -224,11 +224,11 @@ struct msi_dev_domain {
 
 int msi_setup_device_data(struct device *dev);
 
-void msi_lock_descs(struct device *dev);
-void msi_unlock_descs(struct device *dev);
+void __msi_lock_descs(struct device *dev);
+void __msi_unlock_descs(struct device *dev);
 
-DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, msi_lock_descs(_T->lock),
-		    msi_unlock_descs(_T->lock));
+DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, __msi_lock_descs(_T->lock),
+		    __msi_unlock_descs(_T->lock));
 
 struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid,
 				       enum msi_desc_filter filter);
kernel/irq/msi.c +10 -6
@@ -337,26 +337,30 @@ int msi_setup_device_data(struct device
 }
 
 /**
- * msi_lock_descs - Lock the MSI descriptor storage of a device
+ * __msi_lock_descs - Lock the MSI descriptor storage of a device
  * @dev:	Device to operate on
+ *
+ * Internal function for guard(msi_descs_lock). Don't use in code.
  */
-void msi_lock_descs(struct device *dev)
+void __msi_lock_descs(struct device *dev)
 {
 	mutex_lock(&dev->msi.data->mutex);
 }
-EXPORT_SYMBOL_GPL(msi_lock_descs);
+EXPORT_SYMBOL_GPL(__msi_lock_descs);
 
 /**
- * msi_unlock_descs - Unlock the MSI descriptor storage of a device
+ * __msi_unlock_descs - Unlock the MSI descriptor storage of a device
  * @dev:	Device to operate on
+ *
+ * Internal function for guard(msi_descs_lock). Don't use in code.
  */
-void msi_unlock_descs(struct device *dev)
+void __msi_unlock_descs(struct device *dev)
 {
 	/* Invalidate the index which was cached by the iterator */
 	dev->msi.data->__iter_idx = MSI_XA_MAX_INDEX;
 	mutex_unlock(&dev->msi.data->mutex);
 }
-EXPORT_SYMBOL_GPL(msi_unlock_descs);
+EXPORT_SYMBOL_GPL(__msi_unlock_descs);
 
 static struct msi_desc *msi_find_desc(struct msi_device_data *md, unsigned int domid,
 				      enum msi_desc_filter filter)
#29 lore raw

Re: [patch 10/10] genirq/msi: Rename msi_[un]lock_descs()

Quoted 5 lines · Thomas Gleixner

Now that all abuse is gone and the legit users are converted to guard(msi_descs_lock), rename the lock functions and document them as internal.

No functional chance.

s/chance/change?

Quoted 2 lines
Reviewed-by on v1 10/10
#30 lore raw

Re: [patch 10/10] genirq/msi: Rename msi_[un]lock_descs()

In reply to Thomas Gleixner
Quoted 7 lines · On Sun, 9 Mar 2025 09:41:58 +0100 (CET) Thomas Gleixner <[email protected]>…

Now that all abuse is gone and the legit users are converted to guard(msi_descs_lock), rename the lock functions and document them as internal.

No functional chance.

Sensible.

Acked-by on the v1 series
#31 lore raw
In reply to Thomas Gleixner
Quoted 13 lines · Thomas Gleixner

While converting the MSI descriptor locking to a lock guard() I stumbled over various abuse of MSI descriptors (again).

The following series cleans up the offending code and converts the MSI descriptor locking over to lock guard().

The series applies on Linus tree and is also available from git:

git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git irq/msi

Thanks,

tglx

Quoted 12 lines
---
 drivers/ntb/msi.c                   |   22 +----
 drivers/pci/controller/pci-hyperv.c |   14 ---
 drivers/pci/msi/api.c               |    6 -
 drivers/pci/msi/msi.c               |   77 ++++++++++++++----
 drivers/pci/pci.h                   |    9 ++
 drivers/pci/tph.c                   |   44 ----------
 drivers/soc/ti/ti_sci_inta_msi.c    |   10 --
 drivers/ufs/host/ufs-qcom.c         |   75 +++++++++---------
 include/linux/msi.h                 |   12 +-
 kernel/irq/msi.c                    |  150 ++++++++++++------------------------
 10 files changed, 181 insertions(+), 238 deletions(-)

For the drivers/pci/ parts:

I assume you'll merge this somewhere, let me know if otherwise.