Skip to contentSkip to conversation

Allow SoundWire devices to communicate during remove

PATCH Superseded Charles Keepax 4 participants 20 messages Sep 25 – Oct 9

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Allow SoundWire devices to communicate during remove

Currently on Intel systems SoundWire drivers can't communicate with the device during driver removal. This is primarily because the IRQs are disabled before the driver remove callback is run. The result of this is such transactions timeout causing a) a lot of errors in the log and b) driver remove to take a very long time.

This issue affects cs42l43 and cs42l45, primarily due to both using regmap IRQ.

soundwire_intel soundwire_intel.link.0: IO transfer timed out, cmd 3 device 6 addr 5d len 1 soundwire sdw-master-0-0: trf on Slave 6 failed:-110 write addr 5d count 0 sdca_class sdw:0:0:01fa:4245:01: Failed to sync masks in 5d

As regmap IRQ is torn down it will mask the interrupts that are removed. However, there are many valid reasons a driver might want communicate with the device during removal, others would include disabling jack detection, putting the device into the lowest possible power state to save power, etc.

This patch set attempts to address this problem trying to locate the reason interrupts are disabled, fixing that and then leaving the IRQs enabled for the remove callback.

Thanks,
Charles

Changes since v1:
- Add a new helper to destroy the children on the bus separately, this allows the IRQs to be disabled for final cleanup.
- Split device_unregister into device_del and put_device.

Changes since v1:
- Add a new helper to destroy the children on the bus separately, this allows the IRQs to be disabled for final cleanup.
- Split device_unregister into device_del and put_device.

Charles Keepax (3):
soundwire: bus: Don't unassign dev_num before unregistering device
soundwire: bus: Expose a helper to remove devices from the bus
soundwire: intel_auxdevice: Don't disable IRQs before removing
children

 drivers/soundwire/bus.c             | 45 +++++++++++++++++++++--------
 drivers/soundwire/intel.h           |  1 +
 drivers/soundwire/intel_auxdevice.c |  7 ++++-
 drivers/soundwire/intel_init.c      | 21 ++++++++++++++
 include/linux/soundwire/sdw.h       |  1 +
 include/linux/soundwire/sdw_intel.h |  1 +
 6 files changed, 63 insertions(+), 13 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v2 1/3 soundwire: bus: Don't unassign dev_num before unregistering device

Don't mark dev_num as unassigned until after the driver remove has been called. The driver may want to communicate with the device as part of the driver remove operation, so the dev_num should remain assigned until that has completed. This requires moving from device_unregister() to manually calling device_del() and device_put() to ensure that the slave struct isn't freed too early.

---

Changes since v1:
- Split device_unregister into device_del and put_device

 drivers/soundwire/bus.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/soundwire/bus.c +4 -2
index aeaae5a57c89d..1488b6540844c 100644
@@ -175,8 +175,9 @@ static int sdw_delete_slave(struct device *dev, void *data)
 
 	sdw_slave_debugfs_exit(slave);
 
-	mutex_lock(&bus->bus_lock);
+	device_del(dev);
 
+	mutex_lock(&bus->bus_lock);
 	if (slave->dev_num) { /* clear dev_num if assigned */
 		clear_bit(slave->dev_num, bus->assigned);
 		if (bus->ops && bus->ops->put_device_num)
@@ -185,7 +186,8 @@ static int sdw_delete_slave(struct device *dev, void *data)
 	list_del_init(&slave->node);
 	mutex_unlock(&bus->bus_lock);
 
-	device_unregister(dev);
+	put_device(dev);
+
 	return 0;
 }
 
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [PATCH v2 1/3] soundwire: bus: Don't unassign dev_num before unregistering device

Quoted 32 lines · Charles Keepax

Don't mark dev_num as unassigned until after the driver remove has been called. The driver may want to communicate with the device as part of the driver remove operation, so the dev_num should remain assigned until that has completed. This requires moving from device_unregister() to manually calling device_del() and device_put() to ensure that the slave struct isn't freed too early.

---

Changes since v1:
- Split device_unregister into device_del and put_device

 drivers/soundwire/bus.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/soundwire/bus.c +2 -1
index aeaae5a57c89d..1488b6540844c 100644
@@ -175,8 +175,9 @@ static int sdw_delete_slave(struct device *dev, void *data)
 
 	sdw_slave_debugfs_exit(slave);
 
-	mutex_lock(&bus->bus_lock);
+	device_del(dev);
 
+	mutex_lock(&bus->bus_lock);
 	if (slave->dev_num) { /* clear dev_num if assigned */
 		clear_bit(slave->dev_num, bus->assigned);
 		if (bus->ops && bus->ops->put_device_num)

drivers/soundwire/bus.c static int sdw_delete_slave(struct device *dev, void *data) · line 189

Quoted 5 lines
drivers/soundwire/bus.c +1 -1
@@ -185,7 +186,8 @@ static int sdw_delete_slave(struct device *dev, void *data)
 	list_del_init(&slave->node);
 	mutex_unlock(&bus->bus_lock);
 
-	device_unregister(dev);
+	put_device(dev);

if we are doing this I would add code comments on why we split. We invoke device_register so the expectation is that we always call device_unregister. I understand this is being done but this needs to be well documented here

Signature · 1 line
~Vinod
#3 lore raw

Re: [PATCH v2 1/3] soundwire: bus: Don't unassign dev_num before unregistering device

Quoted 36 lines · Vinod Koul
Quoted 35 lines · Charles Keepax

Don't mark dev_num as unassigned until after the driver remove has been called. The driver may want to communicate with the device as part of the driver remove operation, so the dev_num should remain assigned until that has completed. This requires moving from device_unregister() to manually calling device_del() and device_put() to ensure that the slave struct isn't freed too early.

---

Changes since v1:
- Split device_unregister into device_del and put_device

 drivers/soundwire/bus.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/soundwire/bus.c +2 -1
index aeaae5a57c89d..1488b6540844c 100644
@@ -175,8 +175,9 @@ static int sdw_delete_slave(struct device *dev, void *data)
 
 	sdw_slave_debugfs_exit(slave);
 
-	mutex_lock(&bus->bus_lock);
+	device_del(dev);
 
+	mutex_lock(&bus->bus_lock);
 	if (slave->dev_num) { /* clear dev_num if assigned */
 		clear_bit(slave->dev_num, bus->assigned);
 		if (bus->ops && bus->ops->put_device_num)
@@ -185,7 +186,8 @@ static int sdw_delete_slave(struct device *dev, void *data)
 	list_del_init(&slave->node);
 	mutex_unlock(&bus->bus_lock);
 
Quoted 7 lines

drivers/soundwire/bus.c static int sdw_delete_slave(struct device *dev, void *data) · line 189

Quoted 2 lines
drivers/soundwire/bus.c +1 -1
-	device_unregister(dev);
+	put_device(dev);

if we are doing this I would add code comments on why we split. We invoke device_register so the expectation is that we always call device_unregister. I understand this is being done but this needs to be well documented here

Yeah that is a good idea I will update.

Thanks,
Charles

#4 lore raw

PATCH v2 2/3 soundwire: bus: Expose a helper to remove devices from the bus

In reply to Charles Keepax

Some SoundWire controllers may have features such as IRQs that are needed to support peripheral operation, it is desirable to continue to support these whilst peripheral drivers are removed. However, the current sdw_bus_master_delete() monolithically removes the peripherals and destroys the controller giving no chance for the controller driver to perform clean up after the slaves are removed, but before it is itself destroyed.

Split out a separate helper function that only removes the peripherals, this will allow drivers that require this to sequence things appropriately. The functionality of the current helper is left as is, if the peripherals have already been removed the call to do this is a no-op which allows drivers that don't need this functionality to remain untouched.

---

New since v1.

 drivers/soundwire/bus.c       | 43 +++++++++++++++++++++++++----------
 include/linux/soundwire/sdw.h |  1 +
 2 files changed, 32 insertions(+), 12 deletions(-)
drivers/soundwire/bus.c +31 -12
index 1488b6540844c..4c4e69a58fbef 100644
@@ -169,7 +169,6 @@ EXPORT_SYMBOL(sdw_bus_master_add);
 static int sdw_delete_slave(struct device *dev, void *data)
 {
 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
-	struct sdw_bus *bus = slave->bus;
 
 	pm_runtime_disable(dev);
 
@@ -177,18 +176,37 @@ static int sdw_delete_slave(struct device *dev, void *data)
 
 	device_del(dev);
 
-	mutex_lock(&bus->bus_lock);
-	if (slave->dev_num) { /* clear dev_num if assigned */
-		clear_bit(slave->dev_num, bus->assigned);
-		if (bus->ops && bus->ops->put_device_num)
-			bus->ops->put_device_num(bus, slave);
-	}
-	list_del_init(&slave->node);
-	mutex_unlock(&bus->bus_lock);
+	return 0;
+}
 
-	put_device(dev);
+/**
+ * sdw_bus_slaves_delete() - delete all peripherals on a bus
+ * @bus: bus with peripherals to be deleted
+ *
+ * Delete the child devices.
+ */
+void sdw_bus_slaves_delete(struct sdw_bus *bus)
+{
+	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
+}
+EXPORT_SYMBOL(sdw_bus_slaves_delete);
 
-	return 0;
+static void sdw_bus_slaves_put(struct sdw_bus *bus)
+{
+	struct sdw_slave *slave, *tmp;
+
+	list_for_each_entry_safe(slave, tmp, &bus->slaves, node) {
+		mutex_lock(&bus->bus_lock);
+		if (slave->dev_num) { /* clear dev_num if assigned */
+			clear_bit(slave->dev_num, bus->assigned);
+			if (bus->ops && bus->ops->put_device_num)
+				bus->ops->put_device_num(bus, slave);
+		}
+		list_del_init(&slave->node);
+		mutex_unlock(&bus->bus_lock);
+
+		put_device(&slave->dev);
+	}
 }
 
 /**
@@ -199,7 +217,8 @@ static int sdw_delete_slave(struct device *dev, void *data)
  */
 void sdw_bus_master_delete(struct sdw_bus *bus)
 {
-	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
+	sdw_bus_slaves_delete(bus);
+	sdw_bus_slaves_put(bus);
 
 	sdw_irq_delete(bus);
 
include/linux/soundwire/sdw.h +1 -0
index f710e5932b4b2..df9ee56493b17 100644
@@ -899,6 +899,7 @@ struct sdw_master_ops {
 
 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
 		       struct fwnode_handle *fwnode);
+void sdw_bus_slaves_delete(struct sdw_bus *bus);
 void sdw_bus_master_delete(struct sdw_bus *bus);
 
 void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay);
Signature · 1 line
2.47.3
#5 lore raw

PATCH v2 3/3 soundwire: intel_auxdevice: Don't disable IRQs before removing children

In reply to Charles Keepax

Currently the auxiliary device for the link disables IRQs before it calls sdw_bus_master_delete(). This has the side effect that none of the devices on the link can access their own registers whilst their remove functions run, because the IRQs are required for bus transactions to function.

There appear to be two things that currently block leaving the IRQs enabled during peripheral removal. Firstly, the IRQ handler iterates through a linked list of all the links, once a link is removed the memory pointed at by this linked list is freed, but not removed from the linked_list. Secondly, the potential that an IRQ runs after the controller itself has been destroyed.

For the first problem add a list_del() for the linked list item, note whilst the list itself is contained in the intel_init portion of the code, the list remove needs to be attached to the auxiliary device for the link, since that owns the memory that the list points at. Locking is also required to ensure the IRQ handler runs either before or after any additions/removals from the list. A new lock is added for this, the shim_lock is used to gate access to the shared registers so doesn't feel a super obvious fit for managing the list.

For the second problem utilise the newly added helper that allows destroying the peripherals separately, allowing the auxiliary device to disable IRQs before running its own cleanup.

---

Changes since v1:
- Use new sdw_bus_slaves_delete() helper, which allows us to disable IRQs before destroying the master completely.

 drivers/soundwire/intel.h           |  1 +
 drivers/soundwire/intel_auxdevice.c |  7 ++++++-
 drivers/soundwire/intel_init.c      | 21 +++++++++++++++++++++
 include/linux/soundwire/sdw_intel.h |  1 +
 4 files changed, 29 insertions(+), 1 deletion(-)
drivers/soundwire/intel.h +1 -0
index 7a2e7e73ad632..ec82ac8d54adb 100644
@@ -47,6 +47,7 @@ struct sdw_intel_link_res {
 	u32 link_mask;
 	struct sdw_cdns *cdns;
 	struct list_head list;
+	struct mutex *link_lock; /* lock protecting list */
 	struct hdac_bus *hbus;
 };
 
drivers/soundwire/intel_auxdevice.c +6 -1
index 901a71262094f..1439cef43462d 100644
@@ -508,8 +508,13 @@ static void intel_link_remove(struct auxiliary_device *auxdev)
 	if (!bus->prop.hw_disabled) {
 		sdw_intel_debugfs_exit(sdw);
 		cancel_delayed_work_sync(&cdns->attach_dwork);
-		sdw_cdns_enable_interrupt(cdns, false);
 	}
+
+	sdw_bus_slaves_delete(bus);
+
+	if (!bus->prop.hw_disabled)
+		sdw_cdns_enable_interrupt(cdns, false);
+
 	sdw_bus_master_delete(bus);
 }
 
drivers/soundwire/intel_init.c +21 -0
index ad48d67fa9358..a7437cd420288 100644
@@ -28,6 +28,15 @@ static void intel_link_dev_release(struct device *dev)
 	kfree(ldev);
 }
 
+static void intel_link_list_del(void *data)
+{
+	struct sdw_intel_link_res *link = data;
+
+	mutex_lock(link->link_lock);
+	list_del(&link->list);
+	mutex_unlock(link->link_lock);
+}
+
 /* alloc, init and add link devices */
 static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *res,
 							  struct sdw_intel_ctx *ctx,
@@ -79,6 +88,7 @@ static struct sdw_intel_link_dev *intel_link_dev_register(struct sdw_intel_res *
 		link->shim_lock = res->eml_lock;
 		link->mic_privacy = res->mic_privacy;
 	}
+	link->link_lock = &ctx->link_lock;
 
 	link->ops = res->ops;
 	link->dev = res->dev;
@@ -145,8 +155,10 @@ irqreturn_t sdw_intel_thread(int irq, void *dev_id)
 	struct sdw_intel_ctx *ctx = dev_id;
 	struct sdw_intel_link_res *link;
 
+	mutex_lock(&ctx->link_lock);
 	list_for_each_entry(link, &ctx->link_list, list)
 		sdw_cdns_irq(irq, link->cdns);
+	mutex_unlock(&ctx->link_lock);
 
 	return IRQ_HANDLED;
 }
@@ -165,6 +177,7 @@ static struct sdw_intel_ctx
 	u32 link_mask;
 	int num_slaves = 0;
 	int count;
+	int ret;
 	int i;
 
 	if (!res)
@@ -210,6 +223,7 @@ static struct sdw_intel_ctx
 	ctx->link_mask = res->link_mask;
 	ctx->handle = res->handle;
 	mutex_init(&ctx->shim_lock);
+	mutex_init(&ctx->link_lock);
 
 	link_mask = ctx->link_mask;
 
@@ -246,7 +260,14 @@ static struct sdw_intel_ctx
 			i++;
 			goto err;
 		}
+
+		mutex_lock(&ctx->link_lock);
 		list_add_tail(&link->list, &ctx->link_list);
+		mutex_unlock(&ctx->link_lock);
+		ret = devm_add_action_or_reset(&ldev->auxdev.dev, intel_link_list_del, link);
+		if (ret)
+			goto err;
+
 		bus = &link->cdns->bus;
 		/* Calculate number of slaves */
 		list_for_each(node, &bus->slaves)
include/linux/soundwire/sdw_intel.h +1 -0
index 9710f2dc04e29..7495d35ed2fc3 100644
@@ -307,6 +307,7 @@ struct sdw_intel_ctx {
 	acpi_handle handle;
 	struct sdw_intel_link_dev **ldev;
 	struct list_head link_list;
+	struct mutex link_lock; /* lock protecting link_list */
 	struct mutex shim_lock; /* lock for access to shared SHIM registers */
 	u32 shim_mask;
 	u32 shim_base;
Signature · 1 line
2.47.3
#6 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 9 lines
drivers/soundwire/intel_auxdevice.c +0 -0
index 901a71262094f..1439cef43462d 100644
@@ -508,8 +508,13 @@ static void intel_link_remove(struct auxiliary_device *auxdev)
 	if (!bus->prop.hw_disabled) {
 		sdw_intel_debugfs_exit(sdw);
 		cancel_delayed_work_sync(&cdns->attach_dwork);

drivers/soundwire/intel_auxdevice.c static void intel_link_remove(struct auxiliary_device *auxdev) · line 519

Quoted 10 lines
drivers/soundwire/intel_auxdevice.c +6 -1
-		sdw_cdns_enable_interrupt(cdns, false);
 	}
+
+	sdw_bus_slaves_delete(bus);
+
+	if (!bus->prop.hw_disabled)
+		sdw_cdns_enable_interrupt(cdns, false);
+
 	sdw_bus_master_delete(bus);
 }

sorry, not following - this sequence seems to rely on *two* calls to sdw_bus_slaves_delete(), is this intentional or I am missing something?

void sdw_bus_master_delete(struct sdw_bus *bus)
{
- device_for_each_child(bus->dev, NULL, sdw_delete_slave);
+ sdw_bus_slaves_delete(bus); <<< this would be the second call?
+ sdw_bus_slaves_put(bus);

#7 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 26 lines · Pierre-Louis Bossart
Quoted 9 lines
drivers/soundwire/intel_auxdevice.c +0 -0
index 901a71262094f..1439cef43462d 100644
@@ -508,8 +508,13 @@ static void intel_link_remove(struct auxiliary_device *auxdev)
 	if (!bus->prop.hw_disabled) {
 		sdw_intel_debugfs_exit(sdw);
 		cancel_delayed_work_sync(&cdns->attach_dwork);

drivers/soundwire/intel_auxdevice.c static void intel_link_remove(struct auxiliary_device *auxdev) · line 519

Quoted 10 lines
drivers/soundwire/intel_auxdevice.c +6 -1
-		sdw_cdns_enable_interrupt(cdns, false);
 	}
+
+	sdw_bus_slaves_delete(bus);
+
+	if (!bus->prop.hw_disabled)
+		sdw_cdns_enable_interrupt(cdns, false);
+
 	sdw_bus_master_delete(bus);
 }

sorry, not following - this sequence seems to rely on *two* calls to sdw_bus_slaves_delete(), is this intentional or I am missing something?

void sdw_bus_master_delete(struct sdw_bus *bus)
{

Review

Quoted 3 lines
(quoted diff) +2 -1
-	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
+	sdw_bus_slaves_delete(bus); <<< this would be the second call?
+	sdw_bus_slaves_put(bus);

Yeah seemed the simplest way to not have to change any existing drivers, the second call is a complete no-op if the first was done.

Thanks,
Charles

#8 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 25 lines · Charles Keepax
Quoted 24 lines · Pierre-Louis Bossart
Quoted 9 lines
drivers/soundwire/intel_auxdevice.c +0 -0
index 901a71262094f..1439cef43462d 100644
@@ -508,8 +508,13 @@ static void intel_link_remove(struct auxiliary_device *auxdev)
 	if (!bus->prop.hw_disabled) {
 		sdw_intel_debugfs_exit(sdw);
 		cancel_delayed_work_sync(&cdns->attach_dwork);

drivers/soundwire/intel_auxdevice.c static void intel_link_remove(struct auxiliary_device *auxdev) · line 519

Quoted 10 lines
drivers/soundwire/intel_auxdevice.c +6 -1
-		sdw_cdns_enable_interrupt(cdns, false);
 	}
+
+	sdw_bus_slaves_delete(bus);
+
+	if (!bus->prop.hw_disabled)
+		sdw_cdns_enable_interrupt(cdns, false);
+
 	sdw_bus_master_delete(bus);
 }

sorry, not following - this sequence seems to rely on *two* calls to sdw_bus_slaves_delete(), is this intentional or I am missing something?

Quoted 9 lines

Review

Quoted 5 lines

void sdw_bus_master_delete(struct sdw_bus *bus)
{

(quoted diff) +2 -1
-	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
+	sdw_bus_slaves_delete(bus); <<< this would be the second call?
+	sdw_bus_slaves_put(bus);

Yeah seemed the simplest way to not have to change any existing drivers, the second call is a complete no-op if the first was done.

humm, that seems to work but I get this layering violation after-taste, and I wonder if this works for AMD?

AMD use a platform device instead of an auxiliary one, but overall this looks like the same problem of disabling interrupts before the peripheral driver is removed, no?

static void amd_sdw_manager_remove(struct platform_device *pdev)

{
	struct amd_sdw_manager *amd_manager = dev_get_drvdata(&pdev->dev);
	int ret;

	pm_runtime_disable(&pdev->dev);
	cancel_work_sync(&amd_manager->amd_sdw_work);
	amd_disable_sdw_interrupts(amd_manager); <<< SAME PROBLEM ??
	sdw_bus_master_delete(&amd_manager->bus);

If this is indeed the same problem then the AMD driver should use the same solution...

#9 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 12 lines · Pierre-Louis Bossart
Quoted 10 lines · Charles Keepax

Review

Quoted 5 lines · Pierre-Louis Bossart

void sdw_bus_master_delete(struct sdw_bus *bus)
{

(quoted diff) +2 -1
-	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
+	sdw_bus_slaves_delete(bus); <<< this would be the second call?
+	sdw_bus_slaves_put(bus);

Yeah seemed the simplest way to not have to change any existing drivers, the second call is a complete no-op if the first was done.

Quoted 2 lines

humm, that seems to work but I get this layering violation after-taste, and I wonder if this works for AMD?

Nothing should have changed from AMD's perspective. I do think perhaps a slightly better fix could be done moving significant amounts of the IRQ handling over to the IRQ framework, however that is a much bigger piece of work and more scary from a regressions point of view due to it being a much bigger refactoring.

Quoted 14 lines

AMD use a platform device instead of an auxiliary one, but overall this looks like the same problem of disabling interrupts before the peripheral driver is removed, no?

static void amd_sdw_manager_remove(struct platform_device *pdev)

{
	struct amd_sdw_manager *amd_manager = dev_get_drvdata(&pdev->dev);
	int ret;

	pm_runtime_disable(&pdev->dev);
	cancel_work_sync(&amd_manager->amd_sdw_work);
	amd_disable_sdw_interrupts(amd_manager); <<< SAME PROBLEM ??
	sdw_bus_master_delete(&amd_manager->bus);
Quoted 2 lines

If this is indeed the same problem then the AMD driver should use the same solution...

I am not super familiar with the AMD stuff, so take this with a massive pinch of salt but it depends on if that IRQ is actually involved in SoundWire command transfers.

A quick look through amd_sdw_irq_thread() and amd_sdw_xfer_msg() looks to me like the IRQ is not involved there. So I believe the AMD host doesn't have the same problem as the Intel one. Although there is perhaps debate if SoundWire alerts should still function during remove, which would be broken on AMD, but I am not aware of any devices that need that right now.

Thanks,
Charles

#10 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 44 lines · Charles Keepax
Quoted 12 lines · Pierre-Louis Bossart
Quoted 10 lines · Charles Keepax

Review

Quoted 5 lines · Pierre-Louis Bossart

void sdw_bus_master_delete(struct sdw_bus *bus)
{

(quoted diff) +2 -1
-	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
+	sdw_bus_slaves_delete(bus); <<< this would be the second call?
+	sdw_bus_slaves_put(bus);

Yeah seemed the simplest way to not have to change any existing drivers, the second call is a complete no-op if the first was done.

Quoted 2 lines

humm, that seems to work but I get this layering violation after-taste, and I wonder if this works for AMD?

Nothing should have changed from AMD's perspective. I do think perhaps a slightly better fix could be done moving significant amounts of the IRQ handling over to the IRQ framework, however that is a much bigger piece of work and more scary from a regressions point of view due to it being a much bigger refactoring.

Quoted 14 lines

AMD use a platform device instead of an auxiliary one, but overall this looks like the same problem of disabling interrupts before the peripheral driver is removed, no?

static void amd_sdw_manager_remove(struct platform_device *pdev)

{
	struct amd_sdw_manager *amd_manager = dev_get_drvdata(&pdev->dev);
	int ret;

	pm_runtime_disable(&pdev->dev);
	cancel_work_sync(&amd_manager->amd_sdw_work);
	amd_disable_sdw_interrupts(amd_manager); <<< SAME PROBLEM ??
	sdw_bus_master_delete(&amd_manager->bus);
Quoted 2 lines

If this is indeed the same problem then the AMD driver should use the same solution...

I am not super familiar with the AMD stuff, so take this with a massive pinch of salt but it depends on if that IRQ is actually involved in SoundWire command transfers.

Quoted 6 lines

A quick look through amd_sdw_irq_thread() and amd_sdw_xfer_msg() looks to me like the IRQ is not involved there. So I believe the AMD host doesn't have the same problem as the Intel one. Although there is perhaps debate if SoundWire alerts should still function during remove, which would be broken on AMD, but I am not aware of any devices that need that right now.

I think you're right, the AMD messenging relies on polling, not on interrupts, so they are 'safe'. I don't see anything that would prevent communication with the peripheral.

For the alerts, I guess it's the same on remove, clock stop and system suspend. When the manager made the decision to remove/stop the clock/power-down, the transition has to happen. Alerts can only be handled 'later'.

#11 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

In reply to Charles Keepax
Quoted 22 lines · Charles Keepax

Currently the auxiliary device for the link disables IRQs before it calls sdw_bus_master_delete(). This has the side effect that none of the devices on the link can access their own registers whilst their remove functions run, because the IRQs are required for bus transactions to function.

There appear to be two things that currently block leaving the IRQs enabled during peripheral removal. Firstly, the IRQ handler iterates through a linked list of all the links, once a link is removed the memory pointed at by this linked list is freed, but not removed from the linked_list. Secondly, the potential that an IRQ runs after the controller itself has been destroyed.

For the first problem add a list_del() for the linked list item, note whilst the list itself is contained in the intel_init portion of the code, the list remove needs to be attached to the auxiliary device for the link, since that owns the memory that the list points at. Locking is also required to ensure the IRQ handler runs either before or after any additions/removals from the list. A new lock is added for this, the shim_lock is used to gate access to the shared registers so doesn't feel a super obvious fit for managing the list.

Quoted 3 lines

For the second problem utilise the newly added helper that allows destroying the peripherals separately, allowing the auxiliary device to disable IRQs before running its own cleanup.

I ran into a use-after-free on Samsung Galaxy Book6 (Panther Lake) the other day. I was going to send a patch adding RCU, then I saw your patch already added a mutex:

sof-audio-pci-intel-ptl 0000:00:1f.3: SOF firmware and/or topology file not found.

  Oops: general protection fault, kernel NULL pointer dereference 0x3c0: 0000 [#1] SMP NOPTI
  RIP: 0010:sdw_cdns_irq+0x9/0x1f0 [soundwire_cadence]
  Call Trace:
   sdw_intel_thread+0x2d/0x50 [soundwire_intel]
   hda_dsp_interrupt_thread+0x97/0x320 [snd_sof_intel_hda_generic]
   irq_thread_fn+0x23/0x60
   irq_thread+0xc7/0x190

I would add 'Fixes: 4a98a6b2fa75 ("soundwire: intel/cadence: merge Soundwire interrupt handlers/threads")' maybe

drivers/soundwire/intel_init.c irqreturn_t sdw_intel_thread(int irq, void *dev_id) · line 164

Quoted 11 lines
drivers/soundwire/intel_init.c +2 -0
@@ -145,8 +155,10 @@ irqreturn_t sdw_intel_thread(int irq, void *dev_id)
 	struct sdw_intel_ctx *ctx = dev_id;
 	struct sdw_intel_link_res *link;
 
+	mutex_lock(&ctx->link_lock);
 	list_for_each_entry(link, &ctx->link_list, list)
 		sdw_cdns_irq(irq, link->cdns);
+	mutex_unlock(&ctx->link_lock);
 
 	return IRQ_HANDLED;
 }

I don't understand the code very well, but isn't there a second UAF with the ctx object getting freed? kfree(ctx) in sdw_intel_exit() runs well before the IRQ is unregistered.

-- Richard

#12 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 14 lines · Richard Patel

On Fri, Sep 25, 2026 at 04:42:16PM +0100, Charles Keepax wrote:
I ran into a use-after-free on Samsung Galaxy Book6 (Panther Lake) the other day. I was going to send a patch adding RCU, then I saw your patch already added a mutex:

sof-audio-pci-intel-ptl 0000:00:1f.3: SOF firmware and/or topology file not found.

  Oops: general protection fault, kernel NULL pointer dereference 0x3c0: 0000 [#1] SMP NOPTI
  RIP: 0010:sdw_cdns_irq+0x9/0x1f0 [soundwire_cadence]
  Call Trace:
   sdw_intel_thread+0x2d/0x50 [soundwire_intel]
   hda_dsp_interrupt_thread+0x97/0x320 [snd_sof_intel_hda_generic]
   irq_thread_fn+0x23/0x60
   irq_thread+0xc7/0x190
Quoted 1 line

I would add 'Fixes: 4a98a6b2fa75 ("soundwire: intel/cadence: merge Soundwire interrupt handlers/threads")' maybe

I can probably add a fixes here, will have a look.

Quoted 10 lines
Quoted 10 lines
drivers/soundwire/intel_init.c +2 -0
@@ -145,8 +155,10 @@ irqreturn_t sdw_intel_thread(int irq, void *dev_id)
 	struct sdw_intel_ctx *ctx = dev_id;
 	struct sdw_intel_link_res *link;
 
+	mutex_lock(&ctx->link_lock);
 	list_for_each_entry(link, &ctx->link_list, list)
 		sdw_cdns_irq(irq, link->cdns);
+	mutex_unlock(&ctx->link_lock);
 
Quoted 6 lines

drivers/soundwire/intel_init.c irqreturn_t sdw_intel_thread(int irq, void *dev_id) · line 164

Quoted 2 lines
drivers/soundwire/intel_init.c +0 -0
 	return IRQ_HANDLED;
 }

I don't understand the code very well, but isn't there a second UAF with the ctx object getting freed? kfree(ctx) in sdw_intel_exit() runs well before the IRQ is unregistered.

This situation is unfortunately fairly complex, the IRQ is shared between many different functions and only the SoundWire function relies on ctx. I will do some more poking, but I believe the free order is such that the soundwire stuff is shutdown before ctx is freed. I am not 100% certain if that will prevent the SoundWire IRQ path from getting called, although I would like to believe it does :-)

Thanks,
Charles

#13 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 6 lines · Charles Keepax
Quoted 4 lines · Richard Patel

On Fri, Sep 25, 2026 at 04:42:16PM +0100, Charles Keepax wrote:
I don't understand the code very well, but isn't there a second UAF with the ctx object getting freed? kfree(ctx) in sdw_intel_exit() runs well before the IRQ is unregistered.

Quoted 7 lines

This situation is unfortunately fairly complex, the IRQ is shared between many different functions and only the SoundWire function relies on ctx. I will do some more poking, but I believe the free order is such that the soundwire stuff is shutdown before ctx is freed. I am not 100% certain if that will prevent the SoundWire IRQ path from getting called, although I would like to believe it does :-)

Hmm... ok so poking this a little more looks like I do see just see these freed in hard the wrong order so we should probably fix that up too. Thanks for spotting that I will have a bit of a think.

Thanks,
Charles

#14 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 15 lines · Charles Keepax
Quoted 6 lines · Charles Keepax
Quoted 4 lines · Richard Patel

On Fri, Sep 25, 2026 at 04:42:16PM +0100, Charles Keepax wrote:
I don't understand the code very well, but isn't there a second UAF with the ctx object getting freed? kfree(ctx) in sdw_intel_exit() runs well before the IRQ is unregistered.

Quoted 7 lines

This situation is unfortunately fairly complex, the IRQ is shared between many different functions and only the SoundWire function relies on ctx. I will do some more poking, but I believe the free order is such that the soundwire stuff is shutdown before ctx is freed. I am not 100% certain if that will prevent the SoundWire IRQ path from getting called, although I would like to believe it does :-)

Quoted 4 lines

Hmm... ok so poking this a little more looks like I do see just see these freed in hard the wrong order so we should probably fix that up too. Thanks for spotting that I will have a bit of a think.

Ok found some time to look at this properly I think this is all fine. sdw_intel_exit() first calls sdw_intel_cleanup() which will eventually call sdw_cdns_enable_interrupt(..., false), which should disable the SoundWire IRQs. Then sdw_intel_exit() frees the ctx, whilst at that point whilst the IRQ is still registered one should no longer be able to see soundwire IRQs, so you shouldn't get a dereferencing of ctx.

Thanks,
Charles

#15 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 21 lines · Charles Keepax
Quoted 15 lines · Charles Keepax
Quoted 6 lines · Charles Keepax
Quoted 4 lines · Richard Patel

On Fri, Sep 25, 2026 at 04:42:16PM +0100, Charles Keepax wrote:
I don't understand the code very well, but isn't there a second UAF with the ctx object getting freed? kfree(ctx) in sdw_intel_exit() runs well before the IRQ is unregistered.

Quoted 7 lines

This situation is unfortunately fairly complex, the IRQ is shared between many different functions and only the SoundWire function relies on ctx. I will do some more poking, but I believe the free order is such that the soundwire stuff is shutdown before ctx is freed. I am not 100% certain if that will prevent the SoundWire IRQ path from getting called, although I would like to believe it does :-)

Quoted 4 lines

Hmm... ok so poking this a little more looks like I do see just see these freed in hard the wrong order so we should probably fix that up too. Thanks for spotting that I will have a bit of a think.

Quoted 7 lines

Ok found some time to look at this properly I think this is all fine. sdw_intel_exit() first calls sdw_intel_cleanup() which will eventually call sdw_cdns_enable_interrupt(..., false), which should disable the SoundWire IRQs. Then sdw_intel_exit() frees the ctx, whilst at that point whilst the IRQ is still registered one should no longer be able to see soundwire IRQs, so you shouldn't get a dereferencing of ctx.

On my Galaxy Book6, I was able to get a ctx UAF with your v2 patch set by adding a sleep.

  BUG: unable to handle page fault for address: fffffffffffffff8
  Oops: Oops: 0000 [#1] SMP NOPTI
  CPU: 5 UID: 0 PID: 34491 Comm: irq/165-AudioDS Kdump: loaded Tainted: G           OE       7.3.0-rc6-ibt+ #2 PREEMPT(lazy) 
  RIP: 0010:sdw_intel_thread+0x3c/0x70 [soundwire_intel]
  Call Trace:
   <TASK>
   hda_dsp_interrupt_thread+0x97/0x320 [snd_sof_intel_hda_generic]
   irq_thread_fn+0x23/0x60
   irq_thread+0xc7/0x190
   kthread+0xe5/0x120
   </TASK>

--Richard

---

drivers/soundwire/intel_init.c +3 -0
index a7437cd42028..66a08dad7281 100644
@@ -155,6 +155,8 @@ irqreturn_t sdw_intel_thread(int irq, void *dev_id)
 	struct sdw_intel_ctx *ctx = dev_id;
 	struct sdw_intel_link_res *link;
 
+	msleep(5000);
+
 	mutex_lock(&ctx->link_lock);
 	list_for_each_entry(link, &ctx->link_list, list)
 		sdw_cdns_irq(irq, link->cdns);
@@ -393,6 +395,7 @@ void sdw_intel_exit(struct sdw_intel_ctx *ctx)
 	sdw_intel_cleanup(ctx);
 	kfree(ctx->peripherals);
 	kfree(ctx->ldev);
+	memset(ctx, 0, sizeof(*ctx));
 	kfree(ctx);
 }
 EXPORT_SYMBOL_NS(sdw_intel_exit, "SOUNDWIRE_INTEL_INIT");
#16 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 45 lines · Richard Patel
Quoted 11 lines · Charles Keepax
Quoted 3 lines · Charles Keepax
Quoted 2 lines · Charles Keepax
Quoted 1 line · Richard Patel

On Fri, Sep 25, 2026 at 04:42:16PM +0100, Charles Keepax wrote:

Ok found some time to look at this properly I think this is all fine. sdw_intel_exit() first calls sdw_intel_cleanup() which will eventually call sdw_cdns_enable_interrupt(..., false), which should disable the SoundWire IRQs. Then sdw_intel_exit() frees the ctx, whilst at that point whilst the IRQ is still registered one should no longer be able to see soundwire IRQs, so you shouldn't get a dereferencing of ctx.

On my Galaxy Book6, I was able to get a ctx UAF with your v2 patch set by adding a sleep.

  BUG: unable to handle page fault for address: fffffffffffffff8
  Oops: Oops: 0000 [#1] SMP NOPTI
  CPU: 5 UID: 0 PID: 34491 Comm: irq/165-AudioDS Kdump: loaded Tainted: G           OE       7.3.0-rc6-ibt+ #2 PREEMPT(lazy)
  RIP: 0010:sdw_intel_thread+0x3c/0x70 [soundwire_intel]
  Call Trace:
   <TASK>
   hda_dsp_interrupt_thread+0x97/0x320 [snd_sof_intel_hda_generic]
   irq_thread_fn+0x23/0x60
   irq_thread+0xc7/0x190
   kthread+0xe5/0x120
   </TASK>

--Richard

---

drivers/soundwire/intel_init.c +2 -0
index a7437cd42028..66a08dad7281 100644
@@ -155,6 +155,8 @@ irqreturn_t sdw_intel_thread(int irq, void *dev_id)
 	struct sdw_intel_ctx *ctx = dev_id;
 	struct sdw_intel_link_res *link;
 
+	msleep(5000);
+
 	mutex_lock(&ctx->link_lock);
 	list_for_each_entry(link, &ctx->link_list, list)
 		sdw_cdns_irq(irq, link->cdns);

drivers/soundwire/intel_init.c void sdw_intel_exit(struct sdw_intel_ctx *ctx) · line 401

Quoted 7 lines
drivers/soundwire/intel_init.c +1 -0
@@ -393,6 +395,7 @@ void sdw_intel_exit(struct sdw_intel_ctx *ctx)
 	sdw_intel_cleanup(ctx);
 	kfree(ctx->peripherals);
 	kfree(ctx->ldev);
+	memset(ctx, 0, sizeof(*ctx));
 	kfree(ctx);
 }
 EXPORT_SYMBOL_NS(sdw_intel_exit, "SOUNDWIRE_INTEL_INIT");

Hmm... yeah, I guess the masking ensures a new IRQ can't come in but nothing ensures a currently running IRQ is synchronised in. Well assuming the masking does actually prevent an IRQ coming in.

That is a little awkward, normally freeing the IRQ would synchronise it but as the "IRQ" here is done as a pile of callbacks that doesn't happen. We could do a manual sync on the IRQ but that feels like a bit of a layering violation, since the actually IRQ is several layers away in another part of the code. We could add some flags/completions such that we can wait for the current IRQ to finish but feels a bit like adding code that shouldn't exist. I think the correct solution is probably to switch the handling over to the IRQ framework.

I am going to go for the theory this is not directly a problem with this series since the problem exists unchanged before and after the series. So lets not block this stuff on it, but I will try to find time to start porting more of the handling over to the IRQ framework, or happy to help review if you would rather take a run at it.

Thanks,
Charles

#17 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 30 lines · Charles Keepax
Quoted 13 lines · Richard Patel
Quoted 11 lines · Charles Keepax
Quoted 3 lines · Charles Keepax
Quoted 2 lines · Charles Keepax
Quoted 1 line · Richard Patel

On Fri, Sep 25, 2026 at 04:42:16PM +0100, Charles Keepax wrote:

Ok found some time to look at this properly I think this is all fine. sdw_intel_exit() first calls sdw_intel_cleanup() which will eventually call sdw_cdns_enable_interrupt(..., false), which should disable the SoundWire IRQs. Then sdw_intel_exit() frees the ctx, whilst at that point whilst the IRQ is still registered one should no longer be able to see soundwire IRQs, so you shouldn't get a dereferencing of ctx.

Quoted 2 lines

On my Galaxy Book6, I was able to get a ctx UAF with your v2 patch set by adding a sleep.

Hmm... yeah, I guess the masking ensures a new IRQ can't come in but nothing ensures a currently running IRQ is synchronised in. Well assuming the masking does actually prevent an IRQ coming in.

That is a little awkward, normally freeing the IRQ would synchronise it but as the "IRQ" here is done as a pile of callbacks that doesn't happen. We could do a manual sync on the IRQ but that feels like a bit of a layering violation, since the actually IRQ is several layers away in another part of the code. We could add some flags/completions such that we can wait for the current IRQ to finish but feels a bit like adding code that shouldn't exist. I think the correct solution is probably to switch the handling over to the IRQ framework.

Quoted 3 lines

I am going to go for the theory this is not directly a problem with this series since the problem exists unchanged before and after the series. So lets not block this stuff on it, but I will

Yep, sounds good :-) Thanks again for the fixes.

Quoted 3 lines

try to find time to start porting more of the handling over to the IRQ framework, or happy to help review if you would rather take a run at it.

I was going to defer kfree(ctx) via RCU, what do you think?

Cheers,
-- Richard

#18 lore raw

Re: [PATCH v2 3/3] soundwire: intel_auxdevice: Don't disable IRQs before removing children

Quoted 35 lines · Richard Patel
Quoted 30 lines · Charles Keepax
Quoted 13 lines · Richard Patel
Quoted 11 lines · Charles Keepax
Quoted 3 lines · Charles Keepax
Quoted 2 lines · Charles Keepax
Quoted 1 line · Richard Patel

On Fri, Sep 25, 2026 at 04:42:16PM +0100, Charles Keepax wrote:

Ok found some time to look at this properly I think this is all fine. sdw_intel_exit() first calls sdw_intel_cleanup() which will eventually call sdw_cdns_enable_interrupt(..., false), which should disable the SoundWire IRQs. Then sdw_intel_exit() frees the ctx, whilst at that point whilst the IRQ is still registered one should no longer be able to see soundwire IRQs, so you shouldn't get a dereferencing of ctx.

Quoted 2 lines

On my Galaxy Book6, I was able to get a ctx UAF with your v2 patch set by adding a sleep.

Hmm... yeah, I guess the masking ensures a new IRQ can't come in but nothing ensures a currently running IRQ is synchronised in. Well assuming the masking does actually prevent an IRQ coming in.

That is a little awkward, normally freeing the IRQ would synchronise it but as the "IRQ" here is done as a pile of callbacks that doesn't happen. We could do a manual sync on the IRQ but that feels like a bit of a layering violation, since the actually IRQ is several layers away in another part of the code. We could add some flags/completions such that we can wait for the current IRQ to finish but feels a bit like adding code that shouldn't exist. I think the correct solution is probably to switch the handling over to the IRQ framework.

Quoted 3 lines

I am going to go for the theory this is not directly a problem with this series since the problem exists unchanged before and after the series. So lets not block this stuff on it, but I will

Quoted 7 lines

Yep, sounds good :-) Thanks again for the fixes.

Quoted 3 lines

try to find time to start porting more of the handling over to the IRQ framework, or happy to help review if you would rather take a run at it.

I was going to defer kfree(ctx) via RCU, what do you think?

My slight concern would be it is tackling the UAF directly, but really the problem here is the IRQ handler is still running after the link device has been destroyed. It seems quite likely you can end up with other problems, which we may have to add other mitigations for later. So doing something to ensure the IRQ thread has completed (or at least the SoundWire part there of) in intel_link_remove after the IRQ is disabled feels more robust.

However, that said if the changes are small and neat then it certainly improves the current situation, so I am not totally against the idea.

Thanks,
Charles

reviewer Reviewed-by on the v2 series
#19 lore raw
In reply to Charles Keepax
Quoted 36 lines · Charles Keepax

Currently on Intel systems SoundWire drivers can't communicate with the device during driver removal. This is primarily because the IRQs are disabled before the driver remove callback is run. The result of this is such transactions timeout causing a) a lot of errors in the log and b) driver remove to take a very long time.

This issue affects cs42l43 and cs42l45, primarily due to both using regmap IRQ.

soundwire_intel soundwire_intel.link.0: IO transfer timed out, cmd 3 device 6 addr 5d len 1 soundwire sdw-master-0-0: trf on Slave 6 failed:-110 write addr 5d count 0 sdca_class sdw:0:0:01fa:4245:01: Failed to sync masks in 5d

As regmap IRQ is torn down it will mask the interrupts that are removed. However, there are many valid reasons a driver might want communicate with the device during removal, others would include disabling jack detection, putting the device into the lowest possible power state to save power, etc.

This patch set attempts to address this problem trying to locate the reason interrupts are disabled, fixing that and then leaving the IRQs enabled for the remove callback.

Thanks,
Charles

Changes since v1:
- Add a new helper to destroy the children on the bus separately, this allows the IRQs to be disabled for final cleanup.
- Split device_unregister into device_del and put_device.

Changes since v1:
- Add a new helper to destroy the children on the bus separately, this allows the IRQs to be disabled for final cleanup.
- Split device_unregister into device_del and put_device.

Quoted 5 lines

Charles Keepax (3):
soundwire: bus: Don't unassign dev_num before unregistering device
soundwire: bus: Expose a helper to remove devices from the bus
soundwire: intel_auxdevice: Don't disable IRQs before removing
children

I've run out of objections and can't think of a better solution, so for the patchset:

Thanks Charles!