diff mbox series

[RFC] ASoC: mediatek: mt8192-mt6359: fix device_node leak

Message ID 20211214040028.2992627-1-tzungbi@google.com (mailing list archive)
State Accepted
Commit 4e28491a7a198c668437f2be8a91a76aa52f20eb
Headers show
Series [RFC] ASoC: mediatek: mt8192-mt6359: fix device_node leak | expand

Commit Message

Tzung-Bi Shih Dec. 14, 2021, 4 a.m. UTC
The of_parse_phandle() document:
    >>> Use of_node_put() on it when done.

The driver didn't call of_node_put().  Fixes the leak.

Signed-off-by: Tzung-Bi Shih <tzungbi@google.com>
---
Hi maintainers,

Need your comments.  The patch is one of the partial fixes that I think
it makes the most sense.

Option 1. Machine driver makes sure the object is valid until registered

This patch adopts the option.  It needs snd_soc_register_card() to call
of_node_get() somewhere to hold the reference count of of_node.  However,
I failed to find similar logic in soc-core.c.

Option 2. Machine driver borrows the reference count

This is what [1] adopts.  Decreasing the reference count in device's
remove() to make sure the object is valid for whole sound card's lifecycle.

[1]: https://elixir.bootlin.com/linux/v5.16-rc5/source/sound/soc/mediatek/mt8195/mt8195-mt6359-rt1019-rt5682.c#L1065


In fact, in my test environment, CONFIG_OF_DYNAMIC is not set.  The
implementation is empty[2].

Per [3]:
    >>> Hardly any platforms need this

[2]: https://elixir.bootlin.com/linux/v5.16-rc5/source/include/linux/of.h#L125
[3]: https://elixir.bootlin.com/linux/v5.16-rc5/source/drivers/of/Kconfig#L55

I am not sure if it is worthy to find somewhere to hold the reference count
in soc-core.c but I think option 1 makes it more clear to drivers.

 sound/soc/mediatek/mt8192/mt8192-mt6359-rt1015-rt5682.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Mark Brown Dec. 23, 2021, 7:39 p.m. UTC | #1
On Tue, 14 Dec 2021 12:00:28 +0800, Tzung-Bi Shih wrote:
> The of_parse_phandle() document:
>     >>> Use of_node_put() on it when done.
> 
> The driver didn't call of_node_put().  Fixes the leak.
> 
> 

Applied to

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

Thanks!

[1/1] ASoC: mediatek: mt8192-mt6359: fix device_node leak
      commit: 4e28491a7a198c668437f2be8a91a76aa52f20eb

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
Tzung-Bi Shih Dec. 24, 2021, 6:21 a.m. UTC | #2
On Tue, Dec 14, 2021 at 12:00 PM Tzung-Bi Shih <tzungbi@google.com> wrote:
> Option 1. Machine driver makes sure the object is valid until registered
>
> This patch adopts the option.  It needs snd_soc_register_card() to call
> of_node_get() somewhere to hold the reference count of of_node.  However,
> I failed to find similar logic in soc-core.c.
>
> Option 2. Machine driver borrows the reference count
>
> This is what [1] adopts.  Decreasing the reference count in device's
> remove() to make sure the object is valid for whole sound card's lifecycle.
>
> [1]: https://elixir.bootlin.com/linux/v5.16-rc5/source/sound/soc/mediatek/mt8195/mt8195-mt6359-rt1019-rt5682.c#L1065

I guess I have found the answer to my original questions.  The of_node
in snd_soc_dai_link_component in snd_soc_dai_link is mainly for
matching the component[4].  snd_soc_component itself should hold the
reference count.

[4]: https://elixir.bootlin.com/linux/v5.16-rc6/source/sound/soc/soc-core.c#L749

In summary:
- ASoC doesn't need to hold the device_node reference counts.
- Device nodes can be released after components have bound.
diff mbox series

Patch

diff --git a/sound/soc/mediatek/mt8192/mt8192-mt6359-rt1015-rt5682.c b/sound/soc/mediatek/mt8192/mt8192-mt6359-rt1015-rt5682.c
index a606133951b7..24a5d0adec1b 100644
--- a/sound/soc/mediatek/mt8192/mt8192-mt6359-rt1015-rt5682.c
+++ b/sound/soc/mediatek/mt8192/mt8192-mt6359-rt1015-rt5682.c
@@ -1172,7 +1172,11 @@  static int mt8192_mt6359_dev_probe(struct platform_device *pdev)
 		return ret;
 	}
 
-	return devm_snd_soc_register_card(&pdev->dev, card);
+	ret = devm_snd_soc_register_card(&pdev->dev, card);
+
+	of_node_put(platform_node);
+	of_node_put(hdmi_codec);
+	return ret;
 }
 
 #ifdef CONFIG_OF