Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Srinivas Kandagatla" <srinivas.kandagatla@oss.qualcomm.com>
Cc: linux-sound@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v5 18/24] ASoC: qcom: audioreach: add live speaker temperature (T0) readback
Date: Fri, 04 Dec 2026 14:04:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-249925@kernel.org> (raw)
In-Reply-To: <20261204113101.1141644-19-srinivas.kandagatla@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] ASoC: qcom: audioreach: deadlock holding sp_vi_r0t0_cfg_lock
  across GPR IPC
- [High] ASoC: qcom: audioreach: thermal read in calibration callback
  can deadlock
- [High] ASoC: qcom: audioreach: uncancelled sp_cal_watchdog work on
  unload
- [High] ASoC: qcom: audioreach: sp_cal_watchdog not cancelled on module
  unload
- [High] ASoC: qcom: audioreach: audioreach_vi_calibration_event()
  caches 0 degC T0
- [Medium] ASoC: qcom: audioreach: TOCTOU race on sp_operation_mode in
  VI setup
- [Medium] ASoC: qcom: audioreach: missing calibration teardown on VI
  setup errors
- [Medium] ASoC: qcom: audioreach: T0 read with PA active and missing
  count check
- [Medium] ASoC: qcom: audioreach: watchdog race in
  audioreach_sp_finish_calibration()
- [Low] ASoC: qcom: audioreach: unchecked T0 range and s16 cast on
  calibration
- [Low] ASoC: qcom: audioreach: commit omits regressive VI and lock
  changes
- [Low] ASoC: qcom: audioreach: zero-channel VI event treated as
  calibration success
--

commit 56bf9ae64c86f85671795a9786fef3749d76fe65
Author: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>

> ASoC: qcom: audioreach: add live speaker temperature (T0) readback
>
> A calibration needs each speaker's die temperature when R0 is measured,
> so that the R0/T0 pair given back to the DSP is consistent.
>
> Let the machine driver register the speakers' thermal zones in VI
> channel order with audioreach_sp_register_speaker_map(), and expose
> their temperatures in Q6 format as a read-only "Speaker Protection T0"
> control. Record T0 with R0 when a calibration completes. A slot that
> cannot be read reports 0.
>
> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@oss.qualcomm.com>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202611231947.lq10qE2P-lkp@intel.com/

[Severity: Low]
Does the commit message omit several unrelated changes to
audioreach_speaker_protection_vi() and audioreach_set_media_format()?

Beyond adding T0 readback, this patch drops the sp_vi_r0t0_cfg snapshot so
sp_vi_r0t0_cfg_lock is held across q6apm_send_cmd_sync(), deletes error
cleanup after audioreach_register_events(), switches back from local mode
to sp_operation_mode, and fixes the mismatched 4-argument call site left
by commit "ASoC: qcom: audioreach: send cached R0/T0 to the VI module in
normal mode".  Those look like an accidental rebase or conflict
resolution.

> diff --git a/sound/soc/qcom/qdsp6/audioreach.c b/sound/soc/qcom/qdsp6/audioreach.c
> index d1f6cd2b66354..cf6c686f88bdd 100644
> --- a/sound/soc/qcom/qdsp6/audioreach.c
> +++ b/sound/soc/qcom/qdsp6/audioreach.c

[ ... ]

> +int audioreach_get_sp_vi_t0(long *vals, unsigned int count)
> +{
> +	unsigned int i, n;
> +	bool any_failed = false;
> +
> +	guard(mutex)(&sp_speaker_map_lock);
> +
> +	n = min(count, sp_speaker_map.num);
> +	for (i = 0; i < n; i++) {
> +		int mdeg;
> +
> +		if (sp_speaker_map.tz[i] &&
> +		    !thermal_zone_get_temp(sp_speaker_map.tz[i], &mdeg)) {
> +			vals[i] = div_s64((s64)mdeg * 64, 1000);
> +		} else {
> +			vals[i] = 0;
> +			any_failed = true;
> +		}
> +	}
> +	for (; i < count; i++)
> +		vals[i] = 0;
> +
> +	if (any_failed)
> +		return -EAGAIN;
> +
> +	return n;
> +}

[Severity: High]
Can calling audioreach_get_sp_vi_t0() from audioreach_vi_calibration_event()
deadlock the single-threaded qcom_apr_rx workqueue?

When a WSA884x PA is off and the SoundWire controller or WSA macro is
runtime-suspended, thermal_zone_get_temp() resumes the hardware and sends
a synchronous Q6PRM clock request whose response is processed on the same
qcom_apr_rx workqueue:

qcom_apr_rx (single-threaded wq)
  graph_callback()
    audioreach_vi_calibration_event()
      audioreach_get_sp_vi_t0()
        thermal_zone_get_temp()
          wsa884x_get_temp()
            pm_runtime_resume_and_get()
              swrm_runtime_resume()
                clk_prepare_enable(ctrl->hclk)
                  wsa_macro_enable_clocks()
                    q6prm_send_cmd_sync()
                      wait_event_timeout(&prm->wait)  <-- waits for
                                                          prm_callback()
                                                          on this same wq

A concurrent userspace read of "Speaker Protection T0" or thermal sysfs
holding sp_speaker_map_lock, tz->lock, or wsa884x->sp_lock inside
q6prm_send_cmd_sync() also stalls qcom_apr_rx for the 5-second timeout,
blocking all GPR responses and PCM callbacks.  Sampling T0 outside the
GPR RX workqueue avoids this.

[ ... ]

> @@ -1704,12 +1769,28 @@ void audioreach_vi_calibration_event(struct device *dev,
>  	if (success) {
> -		guard(mutex)(&sp_vi_r0t0_cfg_lock);
> -		sp_vi_r0t0_cfg.num_ch = num_ch;
> -		for (i = 0; i < num_ch; i++)
> -			sp_vi_r0t0_cfg.r0_cali_q24[i] = cali->cali_param[i].r0_cali_q24;
> +		int t0_rc = audioreach_get_sp_vi_t0(t0_live, num_ch);
> +
> +		if (t0_rc < 0) {
> +			dev_warn(dev,
> +				 "VI calibration: T0 unreadable, not caching R0/T0 (%d)\n",
> +				 t0_rc);
> +			success = false;
> +		} else {

[Severity: High]
Can this still cache 0 degC for T0 when fewer than num_ch speaker thermal
zones are registered?

audioreach_get_sp_vi_t0() zero-fills slots past sp_speaker_map.num without
setting any_failed and returns n >= 0, so the t0_rc < 0 check passes
whenever sp_speaker_map.num < num_ch (for example on machine drivers that
do not call audioreach_sp_register_speaker_map()).  Storing 0 degC
alongside a room-temperature R0 makes the DSP underestimate voice-coil
temperature by 25 to 40 degC, defeating thermal limiting.

Note that later commit "ASoC: qcom: audioreach: add speaker protection
calibration state control" also removes the any_failed/-EAGAIN return and
the t0_rc < 0 check altogether, which causes unreadable zones to cache
0 degC as well.  Requiring num_ch > 0 and t0_rc == (int)num_ch (and
keeping the -EAGAIN check) prevents caching invalid T0 values.

[Severity: Medium]
Can audioreach_get_sp_vi_t0() read live hardware temperature here while
the speaker PA is active?

audioreach_vi_calibration_event() runs during active playback when
wsa884x->pa_on is true, and wsa884x_get_temp() cannot sample TADC while
the PA is on: it returns -EAGAIN on a fresh boot before any off-state
read, or returns an arbitrarily stale cached temperature from earlier.
Priming T0 before DAPM enables the PA (for instance during DAI prepare in
audioreach_speaker_protection_vi()) would pair R0 with a fresh reading.

Note that later commit "ASoC: qcom: audioreach: add speaker protection
calibration state control" also drops the -EAGAIN check, turning the
cold-boot failure into silently cached 0 degC values.

[Severity: Low]
What happens if the DSP sends a calibration event with num_ch == 0?

q6apm_vi_calibration_event() allows cali->num_ch == 0 or a payload with no
cali_param entries to reach audioreach_vi_calibration_event() with
num_ch == 0.  With zero iterations, decisive and success stay true and
audioreach_get_sp_vi_t0(t0_live, 0) returns 0, overwriting
sp_vi_r0t0_cfg.num_ch with 0 and wiping any previously cached R0/T0.

> +			guard(mutex)(&sp_vi_r0t0_cfg_lock);
> +			sp_vi_r0t0_cfg.num_ch = num_ch;
> +			for (i = 0; i < num_ch; i++) {
> +				sp_vi_r0t0_cfg.r0_cali_q24[i] = cali->cali_param[i].r0_cali_q24;
> +				sp_vi_r0t0_cfg.t0_cali_q6[i] = (s16)t0_live[i];
> +			}
> +		}
>  	}

[Severity: Low]
Should t0_live[i] be range-checked against [SP_VI_T0_MIN_Q6,
SP_VI_T0_MAX_Q6] before narrowing to s16?

WSA884x thermal zones can legitimately report up to 150 degC, which
exceeds SP_VI_T0_MAX_Q6 (80 degC), so caching it unchecked sends an
out-of-range T0 to the DSP and causes subsequent restore via
audioreach_set_sp_vi_r0t0() to fail with -ERANGE.  Out-of-range thermal
readings outside s16 bounds would also silently wrap on the cast.

[ ... ]

> @@ -1794,11 +1875,8 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
>  		audioreach_arm_vi_calibration();
>  
>  		rc = audioreach_register_events(graph, module);
> -		if (rc) {
> -			audioreach_sp_finish_calibration();
> -			audioreach_sp_disarm_vi();
> +		if (rc)
>  			return rc;
> -		}
>  	}

[Severity: Medium]
Does removing the cleanup here leave calibration armed and stuck in
Calibration mode when audioreach_register_events(), packet allocation, or
q6apm_send_cmd_sync() fails?

sp_operation_mode stays in PARAM_ID_SP_VI_OP_MODE_CALIBRATION for up to
60 seconds (causing retries to return -EBUSY and intervening playback to
configure Calibration mode), and sp_vi_r0.armed remains true indefinitely
because audioreach_sp_cal_watchdog_fn() never clears it.  Even with
audioreach_vi_calibration_abort() added later in the series, returning
early when no DSP events have arrived still leaves sp_operation_mode stuck
in Calibration mode.

[Severity: Medium]
Can audioreach_sp_finish_calibration() race with a concurrent
audioreach_trigger_sp_calibration() and cancel the new run's watchdog?

audioreach_sp_finish_calibration() writes PARAM_ID_SP_VI_OP_MODE_NORMAL
before calling cancel_delayed_work(&sp_cal_watchdog):

CPU0                               CPU1
audioreach_sp_finish_calibration()
  WRITE_ONCE(sp_operation_mode,
             NORMAL)
                                   audioreach_trigger_sp_calibration()
                                     cmpxchg(..., NORMAL, CALIBRATION)
                                     schedule_delayed_work(&sp_cal_watchdog)
  cancel_delayed_work(
      &sp_cal_watchdog)  <-- cancels CPU1's watchdog

An already-running audioreach_sp_cal_watchdog_fn() can also abort CPU1's
new calibration run back to Normal because cancel_delayed_work() does not
wait for it.  Calling cancel_delayed_work_sync(&sp_cal_watchdog) before
resetting sp_operation_mode closes both windows.

[ ... ]

> @@ -1862,26 +1940,23 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
>  	if (rc)
>  		return rc;
>  
> -	if (mode == PARAM_ID_SP_VI_OP_MODE_NORMAL) {
> +	if (sp_operation_mode == PARAM_ID_SP_VI_OP_MODE_NORMAL) {

[Severity: Medium]
Does re-reading sp_operation_mode here introduce a data race and a TOCTOU
mismatch with the local mode already sent to the DSP?

sp_operation_mode is updated concurrently via cmpxchg() and WRITE_ONCE(),
and q6apm_send_cmd_sync() sleeps just above.  If calibration is triggered
while q6apm_send_cmd_sync() sleeps, a Normal-mode VI module skips
PARAM_ID_SP_TH_VI_R0T0_CFG; conversely, if calibration finishes or times
out in that window, Normal-mode R0/T0 config is sent to a Calibration-mode
module.  Checking the snapshotted local mode avoids both.

>  		struct apm_module_sp_th_vi_r0t0_cfg *r0t0_cfg;
> -		struct sp_vi_r0t0_cfg snap;
>  		int r0t0_sz;
>  
> -		/* Snapshot the cache so q6apm_send_cmd_sync runs with no locks held */
> -		scoped_guard(mutex, &sp_vi_r0t0_cfg_lock)
> -			snap = sp_vi_r0t0_cfg;
> +		guard(mutex)(&sp_vi_r0t0_cfg_lock);

[Severity: High]
Can holding sp_vi_r0t0_cfg_lock across q6apm_send_cmd_sync() deadlock the
single-threaded qcom_apr_rx workqueue?

audioreach_vi_calibration_event() runs on qcom_apr_rx and acquires
sp_vi_r0t0_cfg_lock, while q6apm_send_cmd_sync() waits for apm_callback()
on the same workqueue:

CPU0 (DAI prepare)                 qcom_apr_rx (single-threaded wq)
------------------                 --------------------------------
audioreach_speaker_protection_vi()
  lock(&sp_vi_r0t0_cfg_lock)
  q6apm_send_cmd_sync()
    wait_event_timeout(&apm->wait)
                                   graph_callback()
                                     audioreach_vi_calibration_event()
                                       lock(&sp_vi_r0t0_cfg_lock)  <-- blocks
                                   apm_callback()  <-- cannot run to wake
                                                       &apm->wait

This stalls all GPR responses and PCM callbacks for the 5-second command
timeout and causes speaker protection activation to fail with -ETIMEDOUT.
Keeping the stack snapshot under scoped_guard(mutex, &sp_vi_r0t0_cfg_lock)
avoids holding the lock across IPC.

[ ... ]

> diff --git a/sound/soc/qcom/qdsp6/audioreach.h b/sound/soc/qcom/qdsp6/audioreach.h
> index 780d3df717d0e..e60e9c6561223 100644
> --- a/sound/soc/qcom/qdsp6/audioreach.h
> +++ b/sound/soc/qcom/qdsp6/audioreach.h
> @@ -1136,6 +1137,10 @@ u32 audioreach_get_sp_operation_mode(void);
>  int audioreach_trigger_sp_calibration(void);
>  void audioreach_get_sp_vi_r0t0(long *vals, unsigned int count);
>  int audioreach_set_sp_vi_r0t0(struct device *dev, const long *vals, unsigned int count);
> +int audioreach_sp_register_speaker_map(struct thermal_zone_device * const *tzs,
> +				       unsigned int num);
> +int audioreach_get_sp_vi_t0(long *vals, unsigned int count);
> +int audioreach_add_sp_telemetry_controls(struct snd_soc_component *component);
>  void audioreach_vi_calibration_event(struct device *dev,
>  				     const struct event_id_vi_per_spkr_calibration *cali,
>  				     u32 num_ch);

[Severity: High]
Is sp_cal_watchdog left pending on module unload?

audioreach_trigger_sp_calibration() schedules the 60-second module-global
sp_cal_watchdog delayed work, and audioreach.h still declares
audioreach_sp_shutdown(), but its implementation and call in apm_remove()
were dropped by commit "ASoC: qcom: audioreach: send cached R0/T0 to the
VI module in normal mode".  Unloading snd-q6apm while the timer is pending
leads to a use-after-free when audioreach_sp_cal_watchdog_fn() runs in
freed module memory.

[Severity: High]
Can unloading snd-q6apm within 60 seconds of a calibration trigger crash
the kernel when sp_cal_watchdog fires?

apm_remove() unregisters the component without calling
cancel_delayed_work_sync(&sp_cal_watchdog), leaving the timer queued on
system_wq after module text and static data are freed.  Restoring
audioreach_sp_shutdown() and calling it in apm_remove() after component
and child teardown prevents both the use-after-free and re-arming from
userspace kcontrol writes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204113101.1141644-1-srinivas.kandagatla@oss.qualcomm.com?part=18

  reply	other threads:[~2026-12-04 14:04 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 11:30 [PATCH v5 00/24] ASoC: qcom: AudioReach Active speaker protection for WSA88xx Srinivas Kandagatla
2026-12-04 11:30 ` [PATCH v5 01/24] ASoC: soc-core: bump MAX_DEFAULT_CH_MAP_SIZE to 18 Srinivas Kandagatla
2026-12-04 11:30 ` [PATCH v5 02/24] ASoC: soc-pcm: add multi_direction_codecs link flag Srinivas Kandagatla
2026-12-04 12:01   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 03/24] ASoC: soc-dai: export snd_soc_dai_stream_valid Srinivas Kandagatla
2026-12-04 11:30 ` [PATCH v5 04/24] ASoC: qcom: sdw: add per-direction stream runtimes for multi_direction links Srinivas Kandagatla
2026-12-04 12:12   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 05/24] ASoC: dt-bindings: qcom,wsa8840: allow one sound DAI cell Srinivas Kandagatla
2026-12-04 11:30 ` [PATCH v5 06/24] ASoC: codecs: wsa884x: add VI sense capture DAI Srinivas Kandagatla
2026-12-04 12:38   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 07/24] ASoC: codecs: wsa883x: " Srinivas Kandagatla
2026-12-04 12:33   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 08/24] ASoC: codecs: wsa884x: register the die temperature as a thermal zone Srinivas Kandagatla
2026-12-04 13:34   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 09/24] ASoC: qcom: audioreach: add control link support to graphs Srinivas Kandagatla
2026-12-04 13:35   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 10/24] ASoC: qcom: audioreach: run VI module in calibration mode with per-speaker events Srinivas Kandagatla
2026-12-04 13:32   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 11/24] ASoC: qcom: q6apm-dai: add S32_LE format support Srinivas Kandagatla
2026-12-04 11:30 ` [PATCH v5 12/24] ASoC: qcom: audioreach: cache VI calibration results per speaker Srinivas Kandagatla
2026-12-04 13:32   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 13/24] ASoC: qcom: audioreach: add speaker protection calibration trigger infrastructure Srinivas Kandagatla
2026-12-04 13:25   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 14/24] ASoC: qcom: audioreach: send cached R0/T0 to the VI module in normal mode Srinivas Kandagatla
2026-12-04 13:43   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 15/24] ASoC: qcom: audioreach: send static cfg blobs to speaker protection modules Srinivas Kandagatla
2026-12-04 13:50   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 16/24] ASoC: qcom: audioreach: add speaker protection mixer controls Srinivas Kandagatla
2026-12-04 14:01   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 17/24] ASoC: qcom: audioreach: pin SPEAKER_PROTECTION_VI media format Srinivas Kandagatla
2026-12-04 13:56   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 18/24] ASoC: qcom: audioreach: add live speaker temperature (T0) readback Srinivas Kandagatla
2026-12-04 14:04   ` sashiko-bot [this message]
2026-12-04 11:30 ` [PATCH v5 19/24] ASoC: qcom: audioreach: add speaker protection calibration state control Srinivas Kandagatla
2026-12-04 13:59   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 20/24] ASoC: qcom: x1e80100: carry WSA VI feedback on the WSA Playback BE link Srinivas Kandagatla
2026-12-04 14:11   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 21/24] ASoC: qcom: x1e80100: register WSA speaker thermal zones for speaker protection Srinivas Kandagatla
2026-12-04 14:16   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 22/24] arm64: dts: qcom: glymur-crd: add WSA speaker protection VI feedback Srinivas Kandagatla
2026-12-04 14:18   ` sashiko-bot
2026-12-04 11:30 ` [PATCH v5 23/24] arm64: dts: qcom: mahua-t14s: " Srinivas Kandagatla
2026-12-04 14:16   ` sashiko-bot
2026-12-04 11:31 ` [PATCH v5 24/24] arm64: dts: qcom: x1e78100-t14s: " Srinivas Kandagatla
2026-12-04 14:24   ` sashiko-bot

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=sashiko-outbox-249925@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=srinivas.kandagatla@oss.qualcomm.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