All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shawn Guo" <shengchao.guo@oss.qualcomm.com>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH v2 1/3] ASoC: qcom: q6apm: Wait for SPF to be ready before registering
Date: Mon, 30 Nov 2026 08:18:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-240682@kernel.org> (raw)
In-Reply-To: <20261130075333.43354-2-shengchao.guo@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] ASoC: qcom: q6apm: unsynchronized state polling in
  q6apm_wait_spf_ready()
- [Medium] ASoC: qcom: q6apm: g_apm published before
  q6apm_wait_spf_ready() in apm_probe()
- [Low] ASoC: qcom: q6apm: missing Fixes trailer for apm_probe()
  readiness fix
--

commit 783d7c63c014afaa7ef3abe8394a7317a124239c
Author: Shawn Guo <shengchao.guo@oss.qualcomm.com>

> ASoC: qcom: q6apm: Wait for SPF to be ready before registering
>
> apm_probe() sends APM_CMD_GET_SPF_STATE once and ignores the result.
> At cold boot this is harmless: module loading and PDR lookup delay the
> probe by tens of milliseconds after the DSP is up, so SPF is listening
> by the time the command is sent.
>
> After an ADSP restart, everything is already loaded and PDR reports the
> audio PD up at once. The GPR service is added about 0.5 ms after the
> remoteproc reports the DSP up, and SPF drops a command sent that early
> without replying:
>
>   remoteproc remoteproc0: remote processor adsp is now up
>   qcom,apr ...: Adding APR/GPR dev: gprsvc:service:2:1
>   qcom-apm gprsvc:service:2:1: CMD timeout for [1001021] opcode
>
> Once the q6apm component is registered, a sound card whose other
> components are still bound can bind right away, inside apm_probe(), and
> issue APM_CMD_SHARED_MEM_MAP_REGIONS before SPF can handle it. The map
> times out and the card fails to come up:
>
>   qcom-apm gprsvc:service:2:1: CMD timeout for [100100c] opcode
>
> Poll GET_SPF_STATE before registering the component. Simply retrying
> q6apm_get_apm_state() is not enough: it goes through
> audioreach_send_cmd_sync(), whose 5 second timeout makes every dropped
> command stall the probe, and the card with it, for 5 seconds. Instead,
> send the command directly and wait only 100 ms for the reply, for up to
> 5 seconds in total, which is the worst case of the single command sent
> before. A timed out try is retried at once, while a "not ready" reply
> or a send failure backs off for 100 ms first. On Nord, SPF answers
> within 100-200 ms of the restart. If it still does not answer, warn and
> carry on as before rather than failing the probe.
>
> Signed-off-by: Shawn Guo <shengchao.guo@oss.qualcomm.com>

[Severity: Low]
This fixes apm_probe() registering the component before SPF is ready.
Should it include a Fixes: tag?

  Fixes: 5477518b8a0e ("ASoC: qdsp6: audioreach: add q6apm support")

> diff --git a/sound/soc/qcom/qdsp6/q6apm.c b/sound/soc/qcom/qdsp6/q6apm.c
> index 4cb597d737b16..e7ba6bc931421 100644
> --- a/sound/soc/qcom/qdsp6/q6apm.c
> +++ b/sound/soc/qcom/qdsp6/q6apm.c
[ ... ]
> @@ -154,6 +156,47 @@ static int q6apm_get_apm_state(struct q6apm *apm)
>  	return apm->state;
>  }
>  
> +/*
> + * Right after the DSP boots, the SPF drops commands without replying. Poll
> + * GET_SPF_STATE with a short timeout per try instead of the 5 s one of
> + * q6apm_send_cmd_sync(), so a dropped command does not stall the probe.
> + */
> +static bool q6apm_wait_spf_ready(struct q6apm *apm)
> +{
> +	unsigned long timeout = jiffies + msecs_to_jiffies(Q6APM_SPF_READY_MS);
> +	gpr_device_t *gdev = apm->gdev;
> +
> +	do {
> +		struct gpr_pkt *pkt __free(kfree) =
> +			audioreach_alloc_apm_cmd_pkt(0, APM_CMD_GET_SPF_STATE, 0);
> +		long rc;
> +
> +		if (IS_ERR(pkt))
> +			return false;
> +
> +		pkt->hdr.dest_domain = audioreach_gpr_dest_domain(gdev);
> +
> +		mutex_lock(&apm->lock);
> +		apm->result.opcode = 0;
> +		apm->state = 0;
> +		rc = gpr_send_pkt(gdev, pkt);
> +		if (rc >= 0)
> +			rc = wait_event_timeout(apm->wait,
> +					apm->result.opcode == APM_CMD_RSP_GET_SPF_STATE,
> +					msecs_to_jiffies(Q6APM_SPF_POLL_MS));
> +		mutex_unlock(&apm->lock);
> +
> +		if (apm->state)
> +			return true;

[Severity: Medium]
Can q6apm_wait_spf_ready() race with apm_callback() on apm->result.opcode
and apm->state?

apm_callback() does not take apm->lock and writes apm->result.opcode before
apm->state without a memory barrier:

sound/soc/qcom/qdsp6/q6apm.c:apm_callback() {
    ...
	case APM_CMD_RSP_GET_SPF_STATE:
		apm->result.opcode = hdr->opcode;
		apm->result.status = 0;
		/* First word of result it state */
		apm->state = result->opcode;
		wake_up(&apm->wait);
		break;
    ...
}

When a 100 ms poll times out just as a late reply arrives on apr->rxwq,
the next iteration can overwrite the reply or observe the stores out of
order:

CPU0 (apm_probe)                  CPU1 (apr_rxwq)
q6apm_wait_spf_ready()
  wait_event_timeout() -> 0
  ...
  /* next iteration */
  mutex_lock(&apm->lock)
  apm->result.opcode = 0;
                                  apm_callback()
                                    apm->result.opcode =
                                      APM_CMD_RSP_GET_SPF_STATE;
                                    apm->state = 1;
  apm->state = 0;
  gpr_send_pkt(gdev, pkt)
  wait_event_timeout() -> > 0
  mutex_unlock(&apm->lock)
  if (apm->state)                 <-- sees 0, treats ready SPF as not ready

Even within a single iteration, wait_event_timeout() can observe
apm->result.opcode == APM_CMD_RSP_GET_SPF_STATE in its condition check
before apm->state = result->opcode is visible, causing if (apm->state) to
read 0 and sleep for another 100 ms (or fail on the last iteration), while
leaving the newly sent APM_CMD_GET_SPF_STATE command in flight.

> +
> +		/* A timeout has already waited, anything else backs off */
> +		if (rc)
> +			msleep(Q6APM_SPF_POLL_MS);
> +	} while (time_before(jiffies, timeout));
> +
> +	return false;
> +}
> +
>  bool q6apm_is_adsp_ready(void)
>  {
>  	if (g_apm)
> @@ -906,7 +949,12 @@ static int apm_probe(gpr_device_t *gdev)
>  	dev_set_drvdata(dev, apm);
>  	g_apm = apm;
>  
> -	q6apm_get_apm_state(apm);
> +	/*
> +	 * Once the component is registered a card can bind and send commands
> +	 * at once, so wait for the SPF first.
> +	 */
> +	if (!q6apm_wait_spf_ready(apm))
> +		dev_warn(dev, "SPF not ready\n");

[Severity: Medium]
Does assigning g_apm before q6apm_wait_spf_ready() allow a concurrent
q6apm_is_adsp_ready() call to stall q6apm_wait_spf_ready() for 5 seconds?

prm_probe() calls q6apm_is_adsp_ready() -> q6apm_get_apm_state() ->
audioreach_send_cmd_sync(), which takes apm->lock and waits 5 seconds for a
reply.  Because g_apm is already set before q6apm_wait_spf_ready() runs, a
concurrent prm_probe() can grab apm->lock as soon as the first 100 ms poll
releases it:

CPU0 (apm_probe)                  CPU1 (prm_probe)
g_apm = apm;
q6apm_wait_spf_ready()
  mutex_lock(&apm->lock)
  gpr_send_pkt() /* dropped */
  wait_event_timeout(..., 100 ms)
  mutex_unlock(&apm->lock)
                                  q6apm_is_adsp_ready()
                                    q6apm_get_apm_state(g_apm)
                                      audioreach_send_cmd_sync()
                                        mutex_lock(&apm->lock)
                                        gpr_send_pkt() /* dropped */
                                        wait_event_timeout(..., 5 * HZ)
  /* next iteration */
  mutex_lock(&apm->lock)          <-- blocked for 5 s, timeout expires

When CPU1 finally releases apm->lock, time_before(jiffies, timeout) is
false and q6apm_wait_spf_ready() fails with "SPF not ready".  In addition,
both q6apm_get_apm_state() and q6apm_wait_spf_ready() read apm->state
outside apm->lock while q6apm_wait_spf_ready() resets apm->state = 0 under
apm->lock.  Setting g_apm after q6apm_wait_spf_ready() returns would keep
q6apm_is_adsp_ready() returning false until polling finishes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130075333.43354-1-shengchao.guo@oss.qualcomm.com?part=1

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

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  7:53 [PATCH v2 0/3] ASoC: qcom: qdsp6: Fix audio after an ADSP restart Shawn Guo
2026-11-30  7:53 ` [PATCH v2 1/3] ASoC: qcom: q6apm: Wait for SPF to be ready before registering Shawn Guo
2026-11-30  8:18   ` sashiko-bot [this message]
2026-11-30  7:53 ` [PATCH v2 2/3] ASoC: qcom: q6dsp-lpass-clocks: Restore clocks after a DSP restart Shawn Guo
2026-11-30  7:53 ` [PATCH v2 3/3] ASoC: qcom: q6prm: Keep the clock controller across DSP restarts Shawn Guo
2026-11-30  8:06   ` sashiko-bot
2026-12-01 10:55 ` [PATCH v2 0/3] ASoC: qcom: qdsp6: Fix audio after an ADSP restart Ajay Kumar Nandam
2026-12-01 17:34 ` Srinivas Kandagatla

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-240682@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shengchao.guo@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.