2024-05-01 17:51:44

by Krzysztof Kozlowski

[permalink] [raw]
Subject: [PATCH v2] ASoC: Use inline function for type safety in snd_soc_substream_to_rtd()

A common pattern in sound drivers is getting 'struct snd_soc_pcm_runtime'
from 'struct snd_pcm_substream' opaque pointer private_data field with
snd_soc_substream_to_rtd(). However 'private_data' appears in several
other structures as well, including 'struct snd_compr_stream'. The
field might not hold the same type for every structure, although seems
the case at least for 'struct snd_compr_stream', so code can easily make
a mistake by using macro for wrong structure passed as argument.

Switch from macro to inline function, so such mistake will be build-time
detectable.

Signed-off-by: Krzysztof Kozlowski <[email protected]>

---

Changes in v2:
1. Do not open-code underlying macro snd_pcm_substream_chip()
---
include/sound/soc.h | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/include/sound/soc.h b/include/sound/soc.h
index 2a1b6c198547..ab2dd1590db0 100644
--- a/include/sound/soc.h
+++ b/include/sound/soc.h
@@ -1215,8 +1215,12 @@ struct snd_soc_pcm_runtime {
/* see soc_new_pcm_runtime() */
#define snd_soc_rtd_to_cpu(rtd, n) (rtd)->dais[n]
#define snd_soc_rtd_to_codec(rtd, n) (rtd)->dais[n + (rtd)->dai_link->num_cpus]
-#define snd_soc_substream_to_rtd(substream) \
- (struct snd_soc_pcm_runtime *)snd_pcm_substream_chip(substream)
+
+static inline struct snd_soc_pcm_runtime *
+snd_soc_substream_to_rtd(const struct snd_pcm_substream *substream)
+{
+ return snd_pcm_substream_chip(substream);
+}

#define for_each_rtd_components(rtd, i, component) \
for ((i) = 0, component = NULL; \
--
2.43.0



2024-05-05 15:03:17

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH v2] ASoC: Use inline function for type safety in snd_soc_substream_to_rtd()

On Wed, 01 May 2024 19:51:27 +0200, Krzysztof Kozlowski wrote:
> A common pattern in sound drivers is getting 'struct snd_soc_pcm_runtime'
> from 'struct snd_pcm_substream' opaque pointer private_data field with
> snd_soc_substream_to_rtd(). However 'private_data' appears in several
> other structures as well, including 'struct snd_compr_stream'. The
> field might not hold the same type for every structure, although seems
> the case at least for 'struct snd_compr_stream', so code can easily make
> a mistake by using macro for wrong structure passed as argument.
>
> [...]

Applied to

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

Thanks!

[1/1] ASoC: Use inline function for type safety in snd_soc_substream_to_rtd()
commit: 9723cab054d59d770cd1927f92fe315c26cc6a78

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