[PATCH v2 00/21] ASoC: codecs: Fix resource leaks across card bind/unbind

Chancel Liu posted 21 patches 1 day, 6 hours ago
sound/soc/codecs/cpcap.c         |  74 +++++++++++-------
sound/soc/codecs/da7218.c        | 129 ++++++++++++++++---------------
sound/soc/codecs/es8311.c        |  10 +--
sound/soc/codecs/es8316.c        |  14 ++--
sound/soc/codecs/es8323.c        |  15 ++--
sound/soc/codecs/es8328.c        |  13 ++--
sound/soc/codecs/es8389.c        |  36 ++++-----
sound/soc/codecs/max98090.c      |  10 +--
sound/soc/codecs/max98095.c      |  10 +--
sound/soc/codecs/rt1011.c        |  30 +++++--
sound/soc/codecs/rt5514-spi.c    |  37 +++++++--
sound/soc/codecs/rt5514.c        |  23 +++---
sound/soc/codecs/rt5616.c        |  10 +--
sound/soc/codecs/rt5640.c        |  10 +--
sound/soc/codecs/rt5645.c        |  11 ++-
sound/soc/codecs/rt5677-spi.c    |  21 ++++-
sound/soc/codecs/rt5682s.c       |  12 +--
sound/soc/codecs/tlv320aic32x4.c |  94 +++++++++++-----------
sound/soc/codecs/twl4030.c       |  63 ++++++++++-----
sound/soc/codecs/wm8955.c        |  24 +++---
sound/soc/codecs/wm8985.c        |  35 ++++++---
21 files changed, 388 insertions(+), 293 deletions(-)
[PATCH v2 00/21] ASoC: codecs: Fix resource leaks across card bind/unbind
Posted by Chancel Liu 1 day, 6 hours ago
From: Chancel Liu <chancel.liu@nxp.com>

For an ASoC component, component->dev is the underlying bus device
(I2C / SPI / platform). Its devres lifetime is tied to the physical
device's probe/remove, not to the ASoC card's bind/unbind.

A card bind (snd_soc_bind_card()) / unbind (snd_soc_unbind_card())
runs the component .probe / .remove callbacks, but the bus device
itself stays bound across the cycle. Resources requested from the
component .probe (or from a runtime DAI callback) with
devm_*(component->dev, ...), or allocated there and only released when
the bus device is removed, therefore outlive a card unbind and
accumulate one extra copy on every bind/unbind cycle.

Each codec is fixed following one of two principles, chosen by what the
resource actually depends on:

1. Pure hardware resources (clk / regulator / regmap / GPIO / board
   description) only depend on the physical device, not on the ASoC
   component.

   Their acquisition is moved to the bus probe (i2c / spi / platform probe)
   using devm_*(&client->dev, ...), so their devres lifetime correctly
   follows the device and they are not re-requested on every bind.

2. Resources that genuinely depend on the card/component (they are bound
   to the component, or must be torn down when the card is unbound)

   They are kept in the component .probe, but switched from devm to
   plain allocation and released in a paired component .remove:
   memory (kzalloc/kcalloc + kfree), IRQ (request_irq + free_irq),
   delayed work (cancel), wakeup source and mutex.

While working on the card re-bind resource leaks fixed in
https://lore.kernel.org/linux-sound/20260913101531.2787654-1-chancel.liu@oss.nxp.com/

I noticed that the same devm-on-component->dev pattern is present in
many other codecs, leaking resources in the same way.

This series is the result of code analysis only, based on the same
devm-without-remove leak pattern I confirmed by repeated card bind/unbind
with wm8962. The individual fixes have not been tested on hardware, as I
do not have access to these codecs; review of the resource lifetimes
is therefore appreciated.

Changes in v2:
- All patches: prefix the subject with "ASoC: codecs:" (Cezary Rojewski).
- da7218: also move the device tree parsing (da7218_of_to_pdata(), which
  allocates with devm) to the i2c probe (Mark Brown).
- rt5514-spi: keep the original behaviour of continuing probe when the
  IRQ request fails (Mark Brown).
- wm8985: move the "Failed to request supplies" error message into the
  wm8985_get_regulators() helper instead of duplicating it in both the
  i2c and spi probes (Charles Keepax).
- rt5640: acquire the mclk near the start of the i2c probe, before the
  ldo1_en handling and its msleep(400), so a deferred probe does not
  repeat the long hardware delays on every retry. Reported by Sashiko.
- rt5677-spi: serialize the DSP context teardown in the component .remove
  against the exported rt5677_spi_hotword_detected() callback with a
  dedicated lock, to avoid a use-after-free. Reported by Sashiko.
- es8389: drop the now-unused variable "i" in es8389_probe() left behind
  after moving the regulator loop to the i2c probe. Reported by Sashiko.
v1: https://lore.kernel.org/linux-sound/20260921104640.1941554-1-chancel.liu@oss.nxp.com/

Chancel Liu (21):
  ASoC: codecs: cpcap: Fix devm resource leaks across card bind/unbind
  ASoC: codecs: da7218: Fix devm resource leaks across card bind/unbind
  ASoC: codecs: tlv320aic32x4: Fix clock leak from runtime callbacks
  ASoC: codecs: twl4030: Acquire board params and hs_extmute GPIO in the
    platform probe
  ASoC: codecs: es8316: Move mclk acquisition to the i2c probe
  ASoC: codecs: es8323: Move mclk acquisition to the i2c probe
  ASoC: codecs: es8311: Move mclk acquisition to the i2c probe
  ASoC: codecs: es8328: Move clk acquisition to the bus probe
  ASoC: codecs: rt5640: Move mclk acquisition to the i2c probe
  ASoC: codecs: rt5616: Move mclk acquisition to the i2c probe
  ASoC: codecs: rt5514: Move clk acquisition to the i2c probe
  ASoC: codecs: rt5682s: Move mclk acquisition to the i2c probe
  ASoC: codecs: max98090: Move mclk acquisition to the i2c probe
  ASoC: codecs: max98095: Move mclk acquisition to the i2c probe
  ASoC: codecs: wm8985: Move regulator acquisition to the bus probe
  ASoC: codecs: wm8955: Move regulator acquisition to the i2c probe
  ASoC: codecs: es8389: Move regulator and mclk acquisition to the i2c
    probe
  ASoC: codecs: rt5677-spi: Free the DSP context on component remove
  ASoC: codecs: rt1011: Free the bq/drc coefficient arrays on component
    remove
  ASoC: codecs: rt5645: Free the hardware EQ parameters on component
    remove
  ASoC: codecs: rt5514-spi: Free the DSP context on component remove

 sound/soc/codecs/cpcap.c         |  74 +++++++++++-------
 sound/soc/codecs/da7218.c        | 129 ++++++++++++++++---------------
 sound/soc/codecs/es8311.c        |  10 +--
 sound/soc/codecs/es8316.c        |  14 ++--
 sound/soc/codecs/es8323.c        |  15 ++--
 sound/soc/codecs/es8328.c        |  13 ++--
 sound/soc/codecs/es8389.c        |  36 ++++-----
 sound/soc/codecs/max98090.c      |  10 +--
 sound/soc/codecs/max98095.c      |  10 +--
 sound/soc/codecs/rt1011.c        |  30 +++++--
 sound/soc/codecs/rt5514-spi.c    |  37 +++++++--
 sound/soc/codecs/rt5514.c        |  23 +++---
 sound/soc/codecs/rt5616.c        |  10 +--
 sound/soc/codecs/rt5640.c        |  10 +--
 sound/soc/codecs/rt5645.c        |  11 ++-
 sound/soc/codecs/rt5677-spi.c    |  21 ++++-
 sound/soc/codecs/rt5682s.c       |  12 +--
 sound/soc/codecs/tlv320aic32x4.c |  94 +++++++++++-----------
 sound/soc/codecs/twl4030.c       |  63 ++++++++++-----
 sound/soc/codecs/wm8955.c        |  24 +++---
 sound/soc/codecs/wm8985.c        |  35 ++++++---
 21 files changed, 388 insertions(+), 293 deletions(-)

--
2.50.1
Re: [PATCH v2 00/21] ASoC: codecs: Fix resource leaks across card bind/unbind
Posted by Mark Brown 1 day ago
On Wed, 23 Sep 2026 15:12:44 +0900, Chancel Liu wrote:
> ASoC: codecs: Fix resource leaks across card bind/unbind
> 
> From: Chancel Liu <chancel.liu@nxp.com>
> 
> For an ASoC component, component->dev is the underlying bus device
> (I2C / SPI / platform). Its devres lifetime is tied to the physical
> device's probe/remove, not to the ASoC card's bind/unbind.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.4

Thanks!

[01/21] ASoC: codecs: cpcap: Fix devm resource leaks across card bind/unbind
        https://git.kernel.org/broonie/sound/c/3713c74a1b0a
[02/21] ASoC: codecs: da7218: Fix devm resource leaks across card bind/unbind
        https://git.kernel.org/broonie/sound/c/116f1bd60ae3
[03/21] ASoC: codecs: tlv320aic32x4: Fix clock leak from runtime callbacks
        https://git.kernel.org/broonie/sound/c/ef8caf5d2784
[04/21] ASoC: codecs: twl4030: Acquire board params and hs_extmute GPIO in the platform probe
        https://git.kernel.org/broonie/sound/c/99fff20db8de
[05/21] ASoC: codecs: es8316: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/d009170e12ad
[06/21] ASoC: codecs: es8323: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/be9bc47e2569
[07/21] ASoC: codecs: es8311: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/5cc3c8f7264e
[08/21] ASoC: codecs: es8328: Move clk acquisition to the bus probe
        https://git.kernel.org/broonie/sound/c/10a45015665e
[09/21] ASoC: codecs: rt5640: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/e68e66353b8e
[10/21] ASoC: codecs: rt5616: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/b5c9df40ca56
[11/21] ASoC: codecs: rt5514: Move clk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/cc2977e80218
[12/21] ASoC: codecs: rt5682s: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/c580cd416b68
[13/21] ASoC: codecs: max98090: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/bf4379a7195e
[14/21] ASoC: codecs: max98095: Move mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/8635b9033d03
[15/21] ASoC: codecs: wm8985: Move regulator acquisition to the bus probe
        https://git.kernel.org/broonie/sound/c/fe15e876d9ee
[16/21] ASoC: codecs: wm8955: Move regulator acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/07bcfafa4719
[17/21] ASoC: codecs: es8389: Move regulator and mclk acquisition to the i2c probe
        https://git.kernel.org/broonie/sound/c/5ef6e7ff52db
[18/21] ASoC: codecs: rt5677-spi: Free the DSP context on component remove
        https://git.kernel.org/broonie/sound/c/57d1e55c7b5a
[19/21] ASoC: codecs: rt1011: Free the bq/drc coefficient arrays on component remove
        https://git.kernel.org/broonie/sound/c/48f1dfbfe4e5
[20/21] ASoC: codecs: rt5645: Free the hardware EQ parameters on component remove
        https://git.kernel.org/broonie/sound/c/6e7f4c5b30a3
[21/21] ASoC: codecs: rt5514-spi: Free the DSP context on component remove
        https://git.kernel.org/broonie/sound/c/3943b84e063f

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark