mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chen Liu <2508871010@qq.com>
To: linux-sound@vger.kernel.org
Cc: Srinivas Kandagatla <srini@kernel.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>, Jaroslav Kysela <perex@perex.cz>,
	Takashi Iwai <tiwai@suse.com>,
	linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org,
	2508871010@qq.com
Subject: [PATCH v2] ASoC: wcd937x: bound the DT channel mapping to the channel tables
Date: Mon, 16 Nov 2026 16:10:52 +0800	[thread overview]
Message-ID: <tencent_4D458B8B4F511E5B0F06C5C858627990C306@qq.com> (raw)

>From 1121be9da86c87962c365f844af9b13dce9a64e3 Mon Sep 17 00:00:00 2001
From: Chen Liu <2508871010@qq.com>
Date: Mon, 16 Nov 2026 14:16:10 +0800
Subject: [PATCH v2] 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.

Bound the length to the scratch buffer before reading the property and to
the table size before filling it in, so that an over-long mapping is
truncated instead of corrupting the RX table.  Only the upper bound is
involved: a missing property still yields a negative count, so the
existing fallback (the read fails, "Static channel mapping not
specified ..." is printed) keeps working unchanged.

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 the SoundWire RX channel it actually
needs (EN_CHAN = 0x1 for HPH_L) instead of channel 4 (EN_CHAN = 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 the last entry of a conforming mapping is now
ignored instead of written out of bounds (binding update left as a
follow-up).

Fixes: c06c4f7cbea1 ("ASoC: codecs: wcd937x: Add static channel mapping support in wcd937x-sdw")
Signed-off-by: Chen Liu <2508871010@qq.com>
---
v2: this is the first resend of the patch.  v1 reached the archive with a
truncated Subject header ("...to the channel"), because the sending script
did not unfold the RFC822 continuation line of the long Subject.

It also addresses the Sashiko review of v1:

  - The fallback diagnostic is preserved: bound the length with min_t()
    instead of clamp(), so a missing property still yields a negative
    count, of_property_read_u8_array() still fails and
    "Static channel mapping not specified ..." is still printed.  Only the
    upper bound changes, so boards without the property see no difference.
  - Fixes: now points at c06c4f7cbea1 ("ASoC: codecs: wcd937x: Add static
    channel mapping support in wcd937x-sdw"), which added the property
    parsing and made the tables writable, instead of the original
    SoundWire driver import.
  - Author/Signed-off-by: full name instead of a single-word handle.
 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..5ad6b30d6 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 = min_t(int, master_ch_mask_size,
+					    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 = min_t(int, master_ch_mask_size,
+					    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


                 reply	other threads:[~2026-11-16  8:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tencent_4D458B8B4F511E5B0F06C5C858627990C306@qq.com \
    --to=2508871010@qq.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=srini@kernel.org \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®