From e93900a97b927d2913c97b794e32cb56a3dcbccb Mon Sep 17 00:00:00 2001 From: Vijendar Mukunda Date: Thu, 3 Dec 2026 15:14:53 +0530 Subject: [PATCH] soundwire: Fix race between handle_nested_irq() and irq_dispose_mapping() sdw_irq_dispose_mapping() and sdw_handle_slave_alerts() can run concurrently with no serialization, leading to a NULL pointer dereference: CPU0 CPU1 device_release_driver_internal() sdw_bus_remove() slave->probed = false device_unbind_cleanup() sdw_irq_dispose_mapping() irq_dispose_mapping(slave->irq) delete_irq_desc() sdw_handle_slave_alerts() handle_nested_irq(slave->irq) desc = irq_to_desc(irq) raw_spin_lock_irq(&desc->lock) <- NULL pointer dereference After commit ccb7bb13c00b ("soundwire: Move handle_nested_irq outside of sdw_dev_lock"), handle_nested_irq(slave->irq) is called before the sdw_dev_lock / slave->probed check and therefore has no protection against concurrent irq_dispose_mapping(). sdw_dev_lock cannot be held for the full duration of handle_nested_irq() as that would reintroduce the deadlock that commit ccb7bb13c00b ("soundwire: Move handle_nested_irq outside of sdw_dev_lock") was fixing. Add a dedicated per-slave irq_lock mutex to serialize the two critical sections. handle_nested_irq() holds irq_lock for its entire duration, preventing irq_dispose_mapping() from proceeding until the handler completes. sdw_irq_dispose_mapping() acquires irq_lock to clear slave->irq atomically, then calls irq_dispose_mapping() after releasing the lock. Once slave->irq is zeroed under irq_lock, no new handle_nested_irq() calls can start, and any call already in progress will complete before irq_dispose_mapping() is reached. slave->irq is explicitly cleared to 0 before irq_dispose_mapping() is called, so any alert arriving after dispose safely skips handle_nested_irq() without reaching a freed descriptor. irq_lock is separate from sdw_dev_lock and is not visible to peripheral drivers, so there is no deadlock risk from the IRQ handler calling back into SoundWire. Fixes: ccb7bb13c00b ("soundwire: Move handle_nested_irq outside of sdw_dev_lock") Signed-off-by: Vijendar Mukunda --- drivers/soundwire/bus.c | 8 ++++++-- drivers/soundwire/irq.c | 9 ++++++++- drivers/soundwire/slave.c | 2 ++ include/linux/soundwire/sdw.h | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/soundwire/bus.c b/drivers/soundwire/bus.c index 79ae8a4babf97f..16fd218b30d0e5 100644 --- a/drivers/soundwire/bus.c +++ b/drivers/soundwire/bus.c @@ -1788,8 +1788,12 @@ 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) { + mutex_lock(&slave->irq_lock); + if (slave->irq) + handle_nested_irq(slave->irq); + mutex_unlock(&slave->irq_lock); + } mutex_lock(&slave->sdw_dev_lock); diff --git a/drivers/soundwire/irq.c b/drivers/soundwire/irq.c index f18be37efef8e0..b618253a856ac1 100644 --- a/drivers/soundwire/irq.c +++ b/drivers/soundwire/irq.c @@ -49,8 +49,15 @@ void sdw_irq_delete(struct sdw_bus *bus) static void sdw_irq_dispose_mapping(void *data) { struct sdw_slave *slave = data; + int irq; - irq_dispose_mapping(slave->irq); + mutex_lock(&slave->irq_lock); + irq = slave->irq; + slave->irq = 0; + mutex_unlock(&slave->irq_lock); + + if (irq) + irq_dispose_mapping(irq); } void sdw_irq_create_mapping(struct sdw_slave *slave) diff --git a/drivers/soundwire/slave.c b/drivers/soundwire/slave.c index a7d2938b031d0f..f56f4efc1a8973 100644 --- a/drivers/soundwire/slave.c +++ b/drivers/soundwire/slave.c @@ -15,6 +15,7 @@ static void sdw_slave_release(struct device *dev) of_node_put(slave->dev.of_node); mutex_destroy(&slave->sdw_dev_lock); + mutex_destroy(&slave->irq_lock); kfree(slave); } @@ -65,6 +66,7 @@ int sdw_slave_add(struct sdw_bus *bus, slave->probed = false; slave->first_interrupt_done = false; mutex_init(&slave->sdw_dev_lock); + mutex_init(&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 9e71ae68e9d927..f9ff19f0f4e75e 100644 --- a/include/linux/soundwire/sdw.h +++ b/include/linux/soundwire/sdw.h @@ -698,6 +698,7 @@ struct sdw_slave { bool first_interrupt_done; bool is_mockup_device; struct mutex sdw_dev_lock; /* protect callbacks/remove races */ + struct mutex irq_lock; /* serialize handle_nested_irq and irq_dispose_mapping */ struct sdca_device_data sdca_data; };