Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ASoC: wcd937x: bound the DT channel mapping to the channel
@ 2026-11-16  7:09 liuchen
  2026-11-16  7:27 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: liuchen @ 2026-11-16  7:09 UTC (permalink / raw)
  To: linux-sound
  Cc: Srinivas Kandagatla, Liam Girdwood, Mark Brown, Jaroslav Kysela,
	Takashi Iwai, linux-arm-msm, linux-kernel, 2508871010

>From 82eb1b88f7971375fbca3c8aedce2da80b189da5 Mon Sep 17 00:00:00 2001
From: liuchen <2508871010@qq.com>
Date: Mon, 16 Nov 2026 14:16:10 +0800
Subject: [PATCH] ASoC: wcd937x: bound the DT channel mapping to the channel
 tables

wcd9370_probe() copies qcom,{tx,rx}-channel-mapping into the per-channel
tables with the length of the DT property as the loop bound.  Nothing
checks that the property describes no more channels than
wcd937x_sdw_tx_ch_info[] (11 entries) and wcd937x_sdw_rx_ch_info[]
(8 entries) hold.

The DT binding requires exactly 12 entries for qcom,tx-channel-mapping
("adc1, adc2, adc3, dmic0, dmic1, mbhc, dmic2, dmic3, dmic4, dmic5,
dmic6, dmic7"), while the driver's table stops at WCD937X_DMIC6, i.e. 11
channels.  Any board that follows the binding therefore writes one entry
past the end of the table; both boards in mainline that use the property
do so (qcm6490-idp.dts and sm7150-xiaomi-davinci.dtsi both spell out 12
values).

Both tables live in .data right next to each other, so the stray write
lands in wcd937x_sdw_rx_ch_info[0].ch_mask.  The 12th value, 4, stores
WCD937X_SWRM_CH_MASK(4) == 0x8 into HPH_L; the codec then asks for
SoundWire RX channel 4 (EN_CHAN = 0x8) while the AFE keeps sending two
channels (active_channels_mask = 0x3).  The CDC DMA never completes a
block, the DSP never reports WRITE_DONE, and playback on the RX codec
DMA path fails with -EIO after the wait_for_avail() timeout.

Clamp the property length to the scratch buffer before reading it and to
the table size before filling it in, so that an over-long mapping is
ignored instead of corrupting the RX table.

The overflow was found while debugging RX codec DMA playback (no
WRITE_DONE from the DSP, playback failing with -EIO) on an SM7150 phone:
a temporary printk showed exactly one entry of the runtime table
differing from .data, which led to the 12th iteration of the loop.
Diagnosis, patch and this changelog were produced with the help of an AI
coding assistant (Claude, Anthropic); the submitter reviewed the change,
built it for arm64 with W=1 and verified on that phone that playback now
completes and that the codec enables SoundWire RX channels 1 and 2
(EN_CHAN = 0x3) instead of channel 4 (0x8).

Not done: qcm6490-idp was not booted, there was no KASAN or reproducer
other than the phone, and the DT binding still advertises 12 TX channels
while the table holds 11, so a conforming mapping now has its last entry
ignored instead of written out of bounds (binding update left as a
follow-up).

Fixes: c99a515ff153 ("ASoC: codecs: wcd937x-sdw: add SoundWire driver")
Signed-off-by: liuchen <2508871010@qq.com>
---
 sound/soc/codecs/wcd937x-sdw.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/sound/soc/codecs/wcd937x-sdw.c b/sound/soc/codecs/wcd937x-sdw.c
index 575495187..baacfef6c 100644
--- a/sound/soc/codecs/wcd937x-sdw.c
+++ b/sound/soc/codecs/wcd937x-sdw.c
@@ -1003,6 +1003,8 @@ static int wcd9370_probe(struct sdw_slave *pdev,
 	if (wcd->is_tx) {
 		master_ch_mask_size = of_property_count_u8_elems(dev->of_node,
 								 "qcom,tx-channel-mapping");
+		master_ch_mask_size = clamp(master_ch_mask_size, 0,
+					    (int)ARRAY_SIZE(master_ch_mask));
 
 		if (master_ch_mask_size)
 			ret = of_property_read_u8_array(dev->of_node, "qcom,tx-channel-mapping",
@@ -1010,6 +1012,8 @@ static int wcd9370_probe(struct sdw_slave *pdev,
 	} else {
 		master_ch_mask_size = of_property_count_u8_elems(dev->of_node,
 								 "qcom,rx-channel-mapping");
+		master_ch_mask_size = clamp(master_ch_mask_size, 0,
+					    (int)ARRAY_SIZE(master_ch_mask));
 
 		if (master_ch_mask_size)
 			ret = of_property_read_u8_array(dev->of_node, "qcom,rx-channel-mapping",
@@ -1023,6 +1027,8 @@ static int wcd9370_probe(struct sdw_slave *pdev,
 		pdev->prop.source_ports = GENMASK(WCD937X_MAX_TX_SWR_PORTS, 0);
 		pdev->prop.src_dpn_prop = wcd937x_dpn_prop;
 		wcd->ch_info = &wcd937x_sdw_tx_ch_info[0];
+		master_ch_mask_size = min_t(int, master_ch_mask_size,
+					    ARRAY_SIZE(wcd937x_sdw_tx_ch_info));
 
 		for (i = 0; i < master_ch_mask_size; i++)
 			wcd->ch_info[i].master_ch_mask = WCD937X_SWRM_CH_MASK(master_ch_mask[i]);
@@ -1040,6 +1046,8 @@ static int wcd9370_probe(struct sdw_slave *pdev,
 		pdev->prop.sink_ports = GENMASK(WCD937X_MAX_SWR_PORTS - 1, 0);
 		pdev->prop.sink_dpn_prop = wcd937x_dpn_prop;
 		wcd->ch_info = &wcd937x_sdw_rx_ch_info[0];
+		master_ch_mask_size = min_t(int, master_ch_mask_size,
+					    ARRAY_SIZE(wcd937x_sdw_rx_ch_info));
 
 		for (i = 0; i < master_ch_mask_size; i++)
 			wcd->ch_info[i].master_ch_mask = WCD937X_SWRM_CH_MASK(master_ch_mask[i]);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ASoC: wcd937x: bound the DT channel mapping to the channel
  2026-11-16  7:09 [PATCH] ASoC: wcd937x: bound the DT channel mapping to the channel liuchen
@ 2026-11-16  7:27 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16  7:27 UTC (permalink / raw)
  To: liuchen, linux-sound

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ASoC: wcd937x: single-word handle used in Signed-off-by trailer
- [Low] ASoC: wcd937x: stale ret check in wcd9370_probe() after clamping
  size
- [Low] ASoC: wcd937x: Fixes trailer cites wrong commit in commit
  message
--

commit e110cdb8f270ccdee8cfffae54f7844e1377eb32
Author: liuchen <2508871010@qq.com>

> ASoC: wcd937x: bound the DT channel mapping to the channel tables
>
> wcd9370_probe() copies qcom,{tx,rx}-channel-mapping into the per-channel
> tables with the length of the DT property as the loop bound.  Nothing
> checks that the property describes no more channels than
> wcd937x_sdw_tx_ch_info[] (11 entries) and wcd937x_sdw_rx_ch_info[]
> (8 entries) hold.
>
> The DT binding requires exactly 12 entries for qcom,tx-channel-mapping
> ("adc1, adc2, adc3, dmic0, dmic1, mbhc, dmic2, dmic3, dmic4, dmic5,
> dmic6, dmic7"), while the driver's table stops at WCD937X_DMIC6, i.e. 11
> channels.  Any board that follows the binding therefore writes one entry
> past the end of the table; both boards in mainline that use the property
> do so (qcm6490-idp.dts and sm7150-xiaomi-davinci.dtsi both spell out 12
> values).
>
> Both tables live in .data right next to each other, so the stray write
> lands in wcd937x_sdw_rx_ch_info[0].ch_mask.  The 12th value, 4, stores
> WCD937X_SWRM_CH_MASK(4) == 0x8 into HPH_L; the codec then asks for
> SoundWire RX channel 4 (EN_CHAN = 0x8) while the AFE keeps sending two
> channels (active_channels_mask = 0x3).  The CDC DMA never completes a
> block, the DSP never reports WRITE_DONE, and playback on the RX codec
> DMA path fails with -EIO after the wait_for_avail() timeout.
>
> Clamp the property length to the scratch buffer before reading it and to
> the table size before filling it in, so that an over-long mapping is
> ignored instead of corrupting the RX table.
>
> The overflow was found while debugging RX codec DMA playback (no
> WRITE_DONE from the DSP, playback failing with -EIO) on an SM7150 phone:
> a temporary printk showed exactly one entry of the runtime table
> differing from .data, which led to the 12th iteration of the loop.
> Diagnosis, patch and this changelog were produced with the help of an AI
> coding assistant (Claude, Anthropic); the submitter reviewed the change,
> built it for arm64 with W=1 and verified on that phone that playback now
> completes and that the codec enables SoundWire RX channels 1 and 2
> (EN_CHAN = 0x3) instead of channel 4 (0x8).
>
> Not done: qcm6490-idp was not booted, there was no KASAN or reproducer
> other than the phone, and the DT binding still advertises 12 TX channels
> while the table holds 11, so a conforming mapping now has its last entry
> ignored instead of written out of bounds (binding update left as a
> follow-up).
>
> Fixes: c99a515ff153 ("ASoC: codecs: wcd937x-sdw: add SoundWire driver")
> Signed-off-by: liuchen <2508871010@qq.com>

[Severity: High]
The Author header and Signed-off-by trailer use a single-word handle
("liuchen") instead of a full real name as required by the Developer's
Certificate of Origin in Documentation/process/submitting-patches.rst.

[Severity: Low]
In commit c99a515ff153 ("ASoC: codecs: wcd937x-sdw: add SoundWire driver"),
wcd937x_sdw_rx_ch_info and wcd937x_sdw_tx_ch_info were const arrays and
wcd9370_probe() did not parse qcom,tx-channel-mapping or
qcom,rx-channel-mapping.  Should the Fixes: tag instead point to the commit
that introduced static channel mapping support:

  Fixes: c06c4f7cbea1 ("ASoC: codecs: wcd937x: Add static channel mapping support in wcd937x-sdw")

> diff --git a/sound/soc/codecs/wcd937x-sdw.c b/sound/soc/codecs/wcd937x-sdw.c
> index 57549518767ce..baacfef6c9cb4 100644
> --- a/sound/soc/codecs/wcd937x-sdw.c
> +++ b/sound/soc/codecs/wcd937x-sdw.c
> @@ -1003,6 +1003,8 @@ static int wcd9370_probe(struct sdw_slave *pdev,
>  	if (wcd->is_tx) {
>  		master_ch_mask_size = of_property_count_u8_elems(dev->of_node,
>  								 "qcom,tx-channel-mapping");
> +		master_ch_mask_size = clamp(master_ch_mask_size, 0,
> +					    (int)ARRAY_SIZE(master_ch_mask));
>  
>  		if (master_ch_mask_size)
>  			ret = of_property_read_u8_array(dev->of_node, "qcom,tx-channel-mapping",
> @@ -1010,6 +1012,8 @@ static int wcd9370_probe(struct sdw_slave *pdev,
>  	} else {
>  		master_ch_mask_size = of_property_count_u8_elems(dev->of_node,
>  								 "qcom,rx-channel-mapping");
> +		master_ch_mask_size = clamp(master_ch_mask_size, 0,
> +					    (int)ARRAY_SIZE(master_ch_mask));
>  
>  		if (master_ch_mask_size)
>  			ret = of_property_read_u8_array(dev->of_node, "qcom,rx-channel-mapping",

[Severity: Low]
Does clamping master_ch_mask_size to 0 before checking
if (master_ch_mask_size) leave ret holding the stale return value from the
earlier port-mapping read when the optional channel-mapping property is
absent?

When qcom,tx-channel-mapping or qcom,rx-channel-mapping is omitted from the
Device Tree, of_property_count_u8_elems() returns a negative error code.
Previously, if (master_ch_mask_size) was true for negative values, so
of_property_read_u8_array() ran and set ret to a negative error code.
Clamping master_ch_mask_size to 0 skips of_property_read_u8_array(), so the
subsequent check in wcd9370_probe():

sound/soc/codecs/wcd937x-sdw.c:wcd9370_probe() {
	...
	if (ret < 0)
		dev_info(dev, "Static channel mapping not specified using device channel maps\n");
}

evaluates the return status of of_property_read_u32_array() for
qcom,tx-port-mapping or qcom,rx-port-mapping instead, suppressing the
fallback message when port mapping succeeded.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_5E8B535138DB2906DF4871E24590DA458A06@qq.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-16  7:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  7:09 [PATCH] ASoC: wcd937x: bound the DT channel mapping to the channel liuchen
2026-11-16  7:27 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox