mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH] soundwire: protect the slave IRQ mapping during alert dispatch
@ 2026-11-22  7:11 Jizhou Tong
  2026-11-22  9:19 ` Pierre-Louis Bossart
  0 siblings, 1 reply; 2+ messages in thread
From: Jizhou Tong @ 2026-11-22  7:11 UTC (permalink / raw)
  To: vkoul, yung-chuan.liao
  Cc: pierre-louis.bossart, rf, ckeepax, srini, linux-sound, linux-kernel

sdw_irq_create_mapping() publishes slave->irq before it registers the
devres action. If devm_add_action_or_reset() fails, it disposes the
mapping but leaves its number in slave->irq. sdw_bus_probe() then
calls the peripheral driver's probe() with that stale number.

Clearing the number on release alone also leaves a window for the alert
handler to pick up the old IRQ before its mapping is disposed. Keep the
mapping private until cleanup is registered. On teardown, take a
per-slave write lock to drain active dispatches and clear the number
before disposing the mapping. Hold the read side across the nested IRQ
handler, which runs in thread context and can sleep.

The lock is independent of sdw_dev_lock so nested handlers continue to
run outside the peripheral callback lock. Peripherals that can operate
without an IRQ can still probe if mapping setup fails. Return early on
a zero mapping so there is no cleanup action to register for IRQ 0.

Fixes: a5fef9baa87f ("soundwire: bus: Move irq mapping cleanup into devres")
Link: https://lore.kernel.org/r/d7761226-31b0-4d0f-bc56-9c4d5e42bfba@opensource.cirrus.com
Signed-off-by: Jizhou Tong <fqwqf@fqwqf.xyz>
---
This came up while moving WCD938x to SoundWire domain IRQs. Richard
previously pointed out the stale IRQ on release.

The rwsem lets overlapping alerts run and keeps per-slave state small.
Reusing sdw_dev_lock would bring it into nested handlers. A per-slave
SRCU domain would also have to drain readers during teardown. On this
base, mapping setup precedes the peripheral probe and sdw_dev_lock.
Unbind still holds the device mutex while draining readers. Do any
domain IRQ handlers depend on work that needs that same device mutex?

Built soundwire-bus.o with ARCH=arm64 W=1. In a four-vCPU arm64 guest
with PROVE_LOCKING and DEBUG_ATOMIC_SLEEP enabled, an emulated peripheral
ran three rounds of 200 driver-core bind/unbind cycles under concurrent
alert dispatch. Removal started with an active handler in 514 of the 600
cycles. To exercise lock ordering against sdw_dev_lock, the test handler
acquired that lock and slept while holding it. Lockdep remained enabled
and reported no warnings in any run.
The physical codec and runtime PM paths still need coverage.

 drivers/soundwire/bus.c       |  4 ++--
 drivers/soundwire/irq.c       | 36 +++++++++++++++++++++++++++++++----
 drivers/soundwire/irq.h       |  5 +++++
 drivers/soundwire/slave.c     |  1 +
 include/linux/soundwire/sdw.h |  3 +++
 5 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c
index aeaae5a57c89d8af05cadd6fe55d44856600e781..0bc0ad9e9934e790c27526ff4c736e8d5a2bebaf 100644
--- a/drivers/soundwire/bus.c
+++ b/drivers/soundwire/bus.c
@@ -1781,8 +1781,8 @@ static int sdw_handle_slave_alerts(struct sdw_slave *slave)
 
 		/* Update the Slave driver */
 		if (slave_notify) {
-			if (slave->prop.use_domain_irq && slave->irq)
-				handle_nested_irq(slave->irq);
+			if (slave->prop.use_domain_irq)
+				sdw_irq_handle(slave);
 
 			mutex_lock(&slave->sdw_dev_lock);
 
diff --git a/drivers/soundwire/irq.c b/drivers/soundwire/irq.c
index f18be37efef8e0ef63bad159cc0d9ebb619a89bf..1f13e1093d94863012fb966b555682826f051cb6 100644
--- a/drivers/soundwire/irq.c
+++ b/drivers/soundwire/irq.c
@@ -2,10 +2,13 @@
 // Copyright (C) 2023 Cirrus Logic, Inc. and
 //                    Cirrus Logic International Semiconductor Ltd.
 
+#include <linux/cleanup.h>
 #include <linux/device.h>
 #include <linux/fwnode.h>
+#include <linux/interrupt.h>
 #include <linux/irq.h>
 #include <linux/irqdomain.h>
+#include <linux/rwsem.h>
 #include <linux/soundwire/sdw.h>
 #include "irq.h"
 
@@ -49,15 +52,40 @@ void sdw_irq_delete(struct sdw_bus *bus)
 static void sdw_irq_dispose_mapping(void *data)
 {
 	struct sdw_slave *slave = data;
+	unsigned int irq;
 
-	irq_dispose_mapping(slave->irq);
+	scoped_guard(rwsem_write, &slave->irq_lock) {
+		irq = slave->irq;
+		slave->irq = 0;
+	}
+
+	irq_dispose_mapping(irq);
 }
 
 void sdw_irq_create_mapping(struct sdw_slave *slave)
 {
-	slave->irq = irq_create_mapping(slave->bus->domain, slave->index);
-	if (!slave->irq)
+	unsigned int irq;
+
+	irq = irq_create_mapping(slave->bus->domain, slave->index);
+	if (!irq) {
 		dev_warn(&slave->dev, "Failed to map IRQ\n");
+		return;
+	}
+
+	if (devm_add_action(&slave->dev, sdw_irq_dispose_mapping, slave)) {
+		irq_dispose_mapping(irq);
+		dev_warn(&slave->dev, "Failed to register IRQ mapping cleanup\n");
+		return;
+	}
+
+	guard(rwsem_write)(&slave->irq_lock);
+	slave->irq = irq;
+}
+
+void sdw_irq_handle(struct sdw_slave *slave)
+{
+	guard(rwsem_read)(&slave->irq_lock);
 
-	devm_add_action_or_reset(&slave->dev, sdw_irq_dispose_mapping, slave);
+	if (slave->irq)
+		handle_nested_irq(slave->irq);
 }
diff --git a/drivers/soundwire/irq.h b/drivers/soundwire/irq.h
index 86e2318409dab8b8137df45586029a6312ee61ff..93e949ec10ca37abea6f0fb8f88e4f016e83259a 100644
--- a/drivers/soundwire/irq.h
+++ b/drivers/soundwire/irq.h
@@ -16,6 +16,7 @@ int sdw_irq_create(struct sdw_bus *bus,
 		   struct fwnode_handle *fwnode);
 void sdw_irq_delete(struct sdw_bus *bus);
 void sdw_irq_create_mapping(struct sdw_slave *slave);
+void sdw_irq_handle(struct sdw_slave *slave);
 
 #else /* CONFIG_IRQ_DOMAIN */
 
@@ -33,6 +34,10 @@ static inline void sdw_irq_create_mapping(struct sdw_slave *slave)
 {
 }
 
+static inline void sdw_irq_handle(struct sdw_slave *slave)
+{
+}
+
 #endif /* CONFIG_IRQ_DOMAIN */
 
 #endif /* __SDW_IRQ_H */
diff --git a/drivers/soundwire/slave.c b/drivers/soundwire/slave.c
index a7d2938b031d0f2d32986e93ffb2bd85e7d73520..ffe21bb04ff70e71af681b26cc475ea65c188c68 100644
--- a/drivers/soundwire/slave.c
+++ b/drivers/soundwire/slave.c
@@ -65,6 +65,7 @@ int sdw_slave_add(struct sdw_bus *bus,
 	slave->probed = false;
 	slave->first_interrupt_done = false;
 	mutex_init(&slave->sdw_dev_lock);
+	init_rwsem(&slave->irq_lock);
 
 	for (i = 0; i < SDW_MAX_PORTS; i++)
 		init_completion(&slave->port_ready[i]);
diff --git a/include/linux/soundwire/sdw.h b/include/linux/soundwire/sdw.h
index f710e5932b4b23c025579b3c8346fe4b5e5f2624..d8c8eae7aed67ae02a140656432a561b28876575 100644
--- a/include/linux/soundwire/sdw.h
+++ b/include/linux/soundwire/sdw.h
@@ -15,6 +15,7 @@
 #include <linux/lockdep_types.h>
 #include <linux/device-id/sdw.h>
 #include <linux/mutex.h>
+#include <linux/rwsem.h>
 #include <linux/types.h>
 #include <sound/sdca.h>
 
@@ -643,6 +644,7 @@ struct sdw_slave_ops {
  * @id: MIPI device ID
  * @dev: Linux device
  * @index: internal ID for this slave
+ * @irq_lock: protects the IRQ mapping during nested interrupt handling
  * @irq: IRQ number
  * @status: Status reported by the Slave
  * @bus: Bus handle
@@ -676,6 +678,7 @@ struct sdw_slave {
 	struct sdw_slave_id id;
 	struct device dev;
 	int index;
+	struct rw_semaphore irq_lock;
 	int irq;
 	enum sdw_slave_status status;
 	struct sdw_bus *bus;

base-commit: afd88e7ad78170040612f608203734156b8a9eb1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-22  9:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22  7:11 [RFC PATCH] soundwire: protect the slave IRQ mapping during alert dispatch Jizhou Tong
2026-11-22  9:19 ` Pierre-Louis Bossart

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®