Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>
To: Jizhou Tong <fqwqf@fqwqf.xyz>,
	vkoul@kernel.org, yung-chuan.liao@linux.intel.com
Cc: rf@opensource.cirrus.com, ckeepax@opensource.cirrus.com,
	srini@kernel.org, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] soundwire: protect the slave IRQ mapping during alert dispatch
Date: Sun, 22 Nov 2026 10:19:49 +0100	[thread overview]
Message-ID: <a527bf36-80d2-43d7-ab36-93f51e39fd02@linux.dev> (raw)
In-Reply-To: <010601a20218c3b5-523b454f-b8b0-4259-9b20-524c1d435465-000000@ap-northeast-1.amazonses.com>

On 11/22/26 08:11, Jizhou Tong wrote:
> 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.

Maybe I am thick and didn't get the problem, but if any error occurred
in sdw_irq_create_mapping(), a simple way to prevent the driver probe
from happening would be to change the prototype and return an error
code, e.g.

	/* Create IRQ mapping now so the driver can get it in probe() */
	ret = sdw_irq_create_mapping(slave);
	if (!ret)
		return ret;

	ret = drv->probe(slave, id);

> 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.

not following either. Presumably on a release, the interrupts are masked
in the codec driver prior to cleanups at the bus level. In what case
would an interrupt handler start *after* the resources are released?

And last the 'irq_lock' is confusing, existing ASoC/SoundWire codec
drivers already use a 'disable_irq_lock' - stored as a private resource
to avoid polluting the sdw_slave struct.

> 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
> 


      reply	other threads:[~2026-11-22  9:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a527bf36-80d2-43d7-ab36-93f51e39fd02@linux.dev \
    --to=pierre-louis.bossart@linux.dev \
    --cc=ckeepax@opensource.cirrus.com \
    --cc=fqwqf@fqwqf.xyz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=rf@opensource.cirrus.com \
    --cc=srini@kernel.org \
    --cc=vkoul@kernel.org \
    --cc=yung-chuan.liao@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox