From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796026726; b=0UA7eGqyDM+YWJbIBlHEAZKpAhsF9mxRMnX9NHvQbCnbczdRAZmjintiYPnKPks1mpRJ qfMg36nREQLM9UTy035/bP45naOk3iPgYl/E2hSyIjzSzp5XKjfKdrJo8BXwAuv7UDTFd tqD3KlNF8J38m8STIJ0cF8KKjnnbS0Ot6tRVurNHxJSqriEOacjbfQqHuXpJTUhKSegib APg5j1EZQ/tkpzoZmt1w3v7g8Dky4gijEMwVYA8JsDLH8WKqblCmK6Q1NtQw3gFShsC/X spgSkIv1cKh/cLgcblK2fTflPxtpjSpBsEond9Omd4bfzMKxxvaC2FbwHqd/mUm3pbw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796026726; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=HxEFtoUKyqPlpvbEQh8jzoAdALRj0j2CFg621nrgoi0=; b=3laBAKZ60ZtqD7cifwTO5xIRfib7omQRCk1Ja04uSUWkbrfG93+VpBku3rTJiMX9sX5Y jges0qSS/uNHI3DVhHzpCI+CoTGYfsNatzmx9mui9bcC5PP/tbrB5Niy9VEfga79xp/wN rV9sgaMu0Ro9tIXlYqCQroRt3nSCexfZUTgP0N3ksvsCUdXBgN9yyYlwhD4EsD8qA2K8w QsjR7ZetkKrwKE+jYCnDCaINvAyr+Pr011ofAP377hF6QV5uOKIOhxmO4R1SsW/Uhi7gK 9lRwsm8/WbdesQBIAhhQjwyVnco4cn9+jK84vt5jQ2VkuyFtcNSKV5WjjfhJJFkoTiQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=a8WN3Vrh; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDA3F606A31C for ; Mon, 30 Nov 2026 08:18:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=a8WN3Vrh Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F6981F000C1; Mon, 30 Nov 2026 08:18:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796026725; bh=HxEFtoUKyqPlpvbEQh8jzoAdALRj0j2CFg621nrgoi0=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=a8WN3VrhuarsJoHv9ZRGWXoc3AQXc/R92fF+GQNSDflj4sP/a6L75Jf+/fgv4h3dz pLlcMHH8WuuCFBhiOq6J9BXLD+vb5NytRsASW1k3+8qtqAQfghp428UudeOtk4WoVz vKW2sv+cUt0aduKUhlabL93L4MLq2WP6tswYGmtQMSA70nipyjr9IYLbAqNbN6l2YS YwVGZqf3SjiEFNIBnky4iplY8OoIpIqqQA3yhnFgcRrDnUuJ1Z4k0aqlcEKMphZn3h fmA8AN+6mKjcUTCEOE1rSLtVc4HVkoCDedsJNMlz2opuEXyCje+WjsK/uEQhe3zVtQ ND7iVJmchIAIA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v2 1/3] ASoC: qcom: q6apm: Wait for SPF to be ready before registering Reply-To: sashiko-reviews@lists.linux.dev To: "Shawn Guo" Cc: linux-sound@vger.kernel.org In-Reply-To: <20261130075333.43354-2-shengchao.guo@oss.qualcomm.com> References: <20261130075333.43354-1-shengchao.guo@oss.qualcomm.com> <20261130075333.43354-2-shengchao.guo@oss.qualcomm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 08:18:44 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 > 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 [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; > } > =20 > +/* > + * Right after the DSP boots, the SPF drops commands without replying. P= oll > + * 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 =3D jiffies + msecs_to_jiffies(Q6APM_SPF_READY_MS= ); > + gpr_device_t *gdev =3D apm->gdev; > + > + do { > + struct gpr_pkt *pkt __free(kfree) =3D > + audioreach_alloc_apm_cmd_pkt(0, APM_CMD_GET_SPF_STATE, 0); > + long rc; > + > + if (IS_ERR(pkt)) > + return false; > + > + pkt->hdr.dest_domain =3D audioreach_gpr_dest_domain(gdev); > + > + mutex_lock(&apm->lock); > + apm->result.opcode =3D 0; > + apm->state =3D 0; > + rc =3D gpr_send_pkt(gdev, pkt); > + if (rc >=3D 0) > + rc =3D wait_event_timeout(apm->wait, > + apm->result.opcode =3D=3D 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 =3D hdr->opcode; apm->result.status =3D 0; /* First word of result it state */ apm->state =3D 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 =3D 0; apm_callback() apm->result.opcode =3D APM_CMD_RSP_GET_SPF_STATE; apm->state =3D 1; apm->state =3D 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 =3D=3D APM_CMD_RSP_GET_SPF_STATE in its condition check before apm->state =3D 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 =3D apm; > =20 > - 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 =3D 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 =3D 0 under apm->lock. Setting g_apm after q6apm_wait_spf_ready() returns would keep q6apm_is_adsp_ready() returning false until polling finishes. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261130075333.4335= 4-1-shengchao.guo@oss.qualcomm.com?part=3D1