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

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