All of lore.kernel.org
 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 13/24] ASoC: qcom: audioreach: add speaker protection calibration trigger infrastructure
Date: Fri, 04 Dec 2026 13:25:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-249862@kernel.org> (raw)
In-Reply-To: <20261204113101.1141644-14-srinivas.kandagatla@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] ASoC: qcom: audioreach: argument mismatch breaks compilation
- [High] ASoC: qcom: audioreach: AB-BA deadlock on sp_vi_r0_lock and
  watchdog
- [High] ASoC: qcom: q6apm: sp_cal_watchdog UAF due to early shutdown in
  apm_remove()
- [Medium] ASoC: qcom: audioreach: races across sp_operation_mode,
  armed, and watchdog
- [Medium] ASoC: qcom: audioreach: empty VI event falsely completes
  calibration
- [Medium] ASoC: qcom: audioreach: missing VI disarm on error and
  watchdog desync
- [Medium] ASoC: qcom: audioreach: audioreach_sp_shutdown() leaves
  calibration stuck
- [Medium] ASoC: qcom: audioreach: split sp_operation_mode reads during
  graph setup
--

commit 32fbe7bee8c50fa2ff9735485256480782651b1a
Author: Ravi Hothi <ravi.hothi@oss.qualcomm.com>

ASoC: qcom: audioreach: add speaker protection calibration trigger infrastructure

This commit introduces a module-global sp_operation_mode state and an
edge-triggered calibration API guarded by a 60-second delayed work watchdog,
automatically returning the mode to Normal when VI calibration reaches a
terminal state.

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

[ ... ]

> @@ -1456,6 +1459,65 @@ static int audioreach_gain_set(struct q6apm_graph *graph,

[ ... ]

> +static void audioreach_sp_cal_watchdog_fn(struct work_struct *work)
> +{
> +	if (cmpxchg(&sp_operation_mode, PARAM_ID_SP_VI_OP_MODE_CALIBRATION,
> +		    PARAM_ID_SP_VI_OP_MODE_NORMAL) !=
> +	    PARAM_ID_SP_VI_OP_MODE_CALIBRATION)
> +		return;
> +
> +	audioreach_sp_disarm_vi();
> +	pr_info("audioreach: SP calibration watchdog fired, back to Normal\n");
> +}
> +
> +/* Finish before writing the mode so a trigger racing in cannot be clobbered */
> +static void audioreach_sp_finish_calibration(void)
> +{
> +	cancel_delayed_work_sync(&sp_cal_watchdog);
> +	WRITE_ONCE(sp_operation_mode, PARAM_ID_SP_VI_OP_MODE_NORMAL);
> +}
> +
> +/* Release the delayed work from the module on unload so the timer cannot fire */
> +void audioreach_sp_shutdown(void)
> +{
> +	cancel_delayed_work_sync(&sp_cal_watchdog);
> +}
> +EXPORT_SYMBOL_GPL(audioreach_sp_shutdown);

[Severity: Medium]
Can audioreach_sp_shutdown() leave sp_operation_mode wedged in Calibration
across an ADSP subsystem restart?

On an ADSP crash/restart, apr_pd_status() invokes apr_remove_device() ->
apm_remove() and later re-probes via apm_probe() while snd-q6apm.ko stays
loaded.  Because audioreach_sp_shutdown() cancels sp_cal_watchdog without
resetting sp_operation_mode to Normal or clearing sp_vi_r0.armed, no timer
remains to revert the mode, leaving subsequent calibration triggers failing
with -EBUSY and normal playback configuring Calibration mode until reboot or
module reload.

> +
> +int audioreach_trigger_sp_calibration(void)
> +{
> +	if (cmpxchg(&sp_operation_mode, PARAM_ID_SP_VI_OP_MODE_NORMAL,
> +		    PARAM_ID_SP_VI_OP_MODE_CALIBRATION) !=
> +	    PARAM_ID_SP_VI_OP_MODE_NORMAL)
> +		return -EBUSY;
> +
> +	schedule_delayed_work(&sp_cal_watchdog,
> +			      SP_CAL_WATCHDOG_SECS * HZ);
> +	return 0;
> +}

[Severity: Medium]
Can lockless updates to sp_operation_mode race with sp_vi_r0.armed and
sp_cal_watchdog, causing a stale watchdog or completion to corrupt a new
calibration run?

CPU0                              CPU1
----                              ----
audioreach_sp_cal_watchdog_fn()
  cmpxchg(&sp_operation_mode,
          CALIBRATION, NORMAL)
                                  audioreach_trigger_sp_calibration()
                                    cmpxchg(..., NORMAL, CALIBRATION)
                                    schedule_delayed_work(...)
                                  audioreach_arm_vi_calibration()
                                    sp_vi_r0.armed = true
  audioreach_sp_disarm_vi()
    sp_vi_r0.armed = false  <-- disarms the new run

Because sp_operation_mode is mutated via cmpxchg/WRITE_ONCE outside
sp_vi_r0_lock, an expiring watchdog can disarm a newly triggered run so its
events are silently dropped.

Two related races share the same cause: if audioreach_sp_finish_calibration()
runs between cmpxchg() and schedule_delayed_work() in
audioreach_trigger_sp_calibration(), it leaves an orphaned watchdog pending
that will fire early on a later run (since schedule_delayed_work() does not
re-arm a pending timer).  Likewise, audioreach_sp_finish_calibration()
unconditionally cancels sp_cal_watchdog and writes Normal without verifying
run ownership, which can clobber a newly triggered run if the watchdog
already expired or if audioreach_speaker_protection_vi() armed VI from a
stale mode snapshot.  Protecting sp_operation_mode, sp_vi_r0.armed, and
watchdog scheduling under a shared lock or generation counter would close
these races.

[ ... ]

> @@ -1506,10 +1568,6 @@ void audioreach_vi_calibration_event(struct device *dev,
>  	unsigned int i;
>  	u32 bit;
>  
> -	/* A payload with no entries gives no new information */
> -	if (!num_ch)
> -		return;
> -
>  	guard(mutex)(&sp_vi_r0_lock);

[Severity: Medium]
Why was the zero-channel guard removed here?

q6apm_vi_calibration_event() does not reject num_ch == 0, and in
audioreach_vi_calibration_event() both decisive and success start as true.
An empty calibration event therefore skips the channel loop and falls through
to record SP_VI_CALI_SUCCESS with num_ch = 0, wiping cached R0 values,
disarming sp_vi_r0, and prematurely returning sp_operation_mode to Normal.

[ ... ]

> @@ -1566,14 +1624,16 @@ void audioreach_vi_calibration_event(struct device *dev,
>  	}
>  
>  	dev_info(dev, "VI calibration complete: %s\n", success ? "success" : "failed");
> +
> +	/* Edge-triggered: the mode auto-returns to Normal on terminal state. */
> +	audioreach_sp_finish_calibration();
>  }

[Severity: High]
Does calling audioreach_sp_finish_calibration() while holding sp_vi_r0_lock
create an ABBA deadlock with sp_cal_watchdog?

CPU0                              CPU1
----                              ----
lock(&sp_vi_r0_lock)
                                  lock(&sp_cal_watchdog)
lock(&sp_cal_watchdog)
                                  lock(&sp_vi_r0_lock)

 *** DEADLOCK ***

CPU0: audioreach_vi_calibration_event() -> audioreach_sp_finish_calibration()
      -> cancel_delayed_work_sync(&sp_cal_watchdog)
CPU1: audioreach_sp_cal_watchdog_fn() -> audioreach_sp_disarm_vi()

audioreach_sp_finish_calibration() calls cancel_delayed_work_sync() before
clearing sp_operation_mode, so an overlapping watchdog succeeds in cmpxchg()
and blocks on sp_vi_r0_lock.  Because audioreach_vi_calibration_event() runs
on the single-threaded qcom_apr_rx workqueue, this permanently stalls all
QDSP6 GPR/APR IPC and triggers a lockdep inversion warning on every terminal
calibration event.  Note that commit "ASoC: qcom: audioreach: send cached
R0/T0 to the VI module in normal mode" later avoids this deadlock by dropping
audioreach_sp_disarm_vi() and switching to cancel_delayed_work(), which
reintroduces races with a newly triggered run.

>  
>  static int audioreach_speaker_protection(struct q6apm_graph *graph,
> -					 const struct audioreach_module *module,
> -					 uint32_t operation_mode)
> +					 const struct audioreach_module *module)
>  {
>  	return audioreach_send_u32_param(graph, module, PARAM_ID_SP_OP_MODE,
> -					 operation_mode);
> +					 READ_ONCE(sp_operation_mode));
>  }

[Severity: Medium]
Can reading sp_operation_mode separately for MODULE_ID_SPEAKER_PROTECTION
and MODULE_ID_SPEAKER_PROTECTION_VI configure the two modules in mismatched
modes?

q6apm_graph_media_format_pcm() calls audioreach_set_media_format() for each
module in the graph across sleeping q6apm_send_cmd_sync() IPCs without
snapshotting sp_operation_mode once per graph setup.  A calibration trigger
or watchdog expiry between the two modules can put SP in Normal and VI in
Calibration (failing with VI_CALIBRATION_STATE_INCORRECT_OP_MODE), or SP in
Calibration while VI stays in Normal without arming or registering events.

[ ... ]

> @@ -1640,11 +1701,13 @@ static int audioreach_speaker_protection_vi(struct q6apm_graph *graph,
>  		return -EINVAL;
>  	}
>  
> -	audioreach_arm_vi_calibration();
> +	if (mode == PARAM_ID_SP_VI_OP_MODE_CALIBRATION) {
> +		audioreach_arm_vi_calibration();
>  
> -	rc = audioreach_register_events(graph, module);
> -	if (rc)
> -		return rc;
> +		rc = audioreach_register_events(graph, module);
> +		if (rc)
> +			return rc;
> +	}

[Severity: Medium]
Does returning an error after audioreach_arm_vi_calibration() leave
sp_vi_r0.armed set?

If audioreach_register_events(), audioreach_alloc_apm_cmd_pkt(), or
q6apm_send_cmd_sync() fails, sp_vi_r0.armed stays true (allowing subsequent
DSP events to wipe cached R0 values) while sp_operation_mode stays in
Calibration until the watchdog fires.  On watchdog timeout,
audioreach_sp_disarm_vi() also leaves sp_vi_r0.state stuck in
SP_VI_CALI_RUNNING, and commit "ASoC: qcom: audioreach: send cached R0/T0 to
the VI module in normal mode" later drops audioreach_sp_disarm_vi()
altogether, leaving sp_vi_r0.armed set as well.

[ ... ]

> @@ -1760,7 +1823,7 @@ int audioreach_set_media_format(struct q6apm_graph *graph,
>  		break;
>  	case MODULE_ID_SPEAKER_PROTECTION:
>  		rc = audioreach_speaker_protection(graph, module,
> -						   PARAM_ID_SP_OP_MODE_CALIBRATION);
> +						   READ_ONCE(sp_operation_mode));
>  		if (!rc)
>  			rc = audioreach_module_enable(graph, module, true);

[Severity: High]
Does this commit compile?

audioreach_speaker_protection() was changed to take 2 arguments, but this
call site still passes 3; conversely, audioreach_speaker_protection_vi() was
changed to take a 4th mode argument, but the MODULE_ID_SPEAKER_PROTECTION_VI
case right below still passes only 3.  This breaks builds whenever
CONFIG_SND_SOC_QDSP6_APM is enabled and breaks git bisectability across the
series (later commits in the series also mismatch these signatures before
reconciling them).

> diff --git a/sound/soc/qcom/qdsp6/q6apm.c b/sound/soc/qcom/qdsp6/q6apm.c
> index c410e2f6e6a3f..6494eb80f0897 100644
> --- a/sound/soc/qcom/qdsp6/q6apm.c
> +++ b/sound/soc/qcom/qdsp6/q6apm.c
> @@ -950,6 +950,8 @@ static int apm_probe(gpr_device_t *gdev)
>  
>  static void apm_remove(gpr_device_t *gdev)
>  {
> +	/* Release the SP calibration watchdog before the module goes away */
> +	audioreach_sp_shutdown();
>  	g_apm = NULL;
>  	of_platform_depopulate(&gdev->dev);
>  	snd_soc_unregister_component(&gdev->dev);

[Severity: High]
Can sp_cal_watchdog be re-scheduled after audioreach_sp_shutdown() and fire
after snd-q6apm.ko is unloaded?

CPU0                              CPU1
----                              ----
apm_remove()
  audioreach_sp_shutdown()
    cancel_delayed_work_sync(...)
                                  audioreach_trigger_sp_calibration()
                                    schedule_delayed_work(&sp_cal_watchdog)
  of_platform_depopulate(...)
  snd_soc_unregister_component(...)
snd-q6apm.ko unloaded
                                  audioreach_sp_cal_watchdog_fn()
                                    <-- use-after-free in freed module

Because audioreach_sp_shutdown() runs before of_platform_depopulate() and
snd_soc_unregister_component(), the kcontrols remain active and can arm the
60-second watchdog again before teardown completes.  The error path in
apm_probe() when of_platform_populate() fails also calls
snd_soc_unregister_component() without calling audioreach_sp_shutdown().
Calling audioreach_sp_shutdown() after both teardown calls (and keeping it
rather than dropping it in commit "ASoC: qcom: audioreach: send cached R0/T0
to the VI module in normal mode") would prevent the use-after-free.

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

  reply	other threads:[~2026-12-04 13:25 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 [this message]
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
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-249862@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.