Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Olivier MOYSAN <olivier.moysan@foss.st.com>
To: <phucduc.bui@gmail.com>, Mark Brown <broonie@kernel.org>
Cc: Liam Girdwood <lgirdwood@gmail.com>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	Arnaud Pouliquen <arnaud.pouliquen@foss.st.com>,
	Maxime Coquelin <mcoquelin.stm32@gmail.com>,
	<linux-sound@vger.kernel.org>,
	<linux-stm32@st-md-mailman.stormreply.com>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>,
	Alexandre Torgue <alexandre.torgue@foss.st.com>
Subject: Re: [PATCH 2/3] ASoC: stm32: spdifrx: Fix refcount on sync failure
Date: Mon, 30 Nov 2026 18:18:00 +0100	[thread overview]
Message-ID: <e8344834-5cfd-453e-af28-5a2bc385e582@foss.st.com> (raw)
In-Reply-To: <20261127092345.12383-3-phucduc.bui@gmail.com>

Hi,

Thanks for your patches.

On 11/27/26 10:23, phucduc.bui@gmail.com wrote:
> From: bui duc phuc <phucduc.bui@gmail.com>
> 
> Roll back the refcount when starting synchronization fails.
> Disable interrupts if no users remain.
> 
> Signed-off-by: bui duc phuc <phucduc.bui@gmail.com>
> ---
>   sound/soc/stm/stm32_spdifrx.c | 8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/sound/soc/stm/stm32_spdifrx.c b/sound/soc/stm/stm32_spdifrx.c
> index 467c3c583a00..ba6c715750aa 100644
> --- a/sound/soc/stm/stm32_spdifrx.c
> +++ b/sound/soc/stm/stm32_spdifrx.c
> @@ -358,9 +358,15 @@ static int stm32_spdifrx_start_sync(struct stm32_spdifrx_data *spdifrx)
>   		cr_mask |= SPDIFRX_CR_SPDIFEN_MASK;
>   		ret = regmap_update_bits(spdifrx->regmap, STM32_SPDIFRX_CR,
>   					 cr_mask, cr);
> -		if (ret < 0)
> +		if (ret < 0) {
>   			dev_err(&spdifrx->pdev->dev,
>   				"Failed to start synchronization\n");
> +			spdifrx->refcount--;
> +			if (!spdifrx->refcount)
> +				regmap_update_bits(spdifrx->regmap,
> +						   STM32_SPDIFRX_IMR,
> +						   SPDIFRX_XIMR_MASK, 0);
> +		}
>   	}
>   
>   	return ret;

stm32_spdifrx_start_sync() is called in both control and data path.

The patch fixes the stm32_spdifrx_get_ctrl_data() path but it seems that 
it can break the stm32_spdifrx_trigger path.

If the trigger START fails (on SPDIFRX_CR update), ALSA calls trigger 
STOP and runs stm32_spdifrx_stop() a second time. The refcount goes to 
-1, so stm32_spdifrx_stop() returns early and the spdifrx is never disabled.

An alternate could be to ensure that stm32_spdifrx_stop() is called in 
stm32_spdifrx_get_ctrl_data() when stm32_spdifrx_start_sync() fails.
Below is the suggested change:

--- a/sound/soc/stm/stm32_spdifrx.c
+++ b/sound/soc/stm/stm32_spdifrx.c
@@ -322,16 +322,17 @@ static int stm32_spdifrx_start_sync(struct 
stm32_spdifrx_data *spdifrx)
  {
         int cr, cr_mask, imr, ret;

+       guard(spinlock_irqsave)(&spdifrx->lock);
+
+       /* Callers always balance refcount with stm32_spdifrx_stop(). */
+       spdifrx->refcount++;
+
         /* Enable IRQs */
         imr = SPDIFRX_IMR_IFEIE | SPDIFRX_IMR_SYNCDIE | SPDIFRX_IMR_PERRIE;
         ret = regmap_update_bits(spdifrx->regmap, STM32_SPDIFRX_IMR, 
imr, imr);
         if (ret)
                 return ret;

-       guard(spinlock_irqsave)(&spdifrx->lock);
-
-       spdifrx->refcount++;
-
         regmap_read(spdifrx->regmap, STM32_SPDIFRX_CR, &cr);

         if (!(cr & SPDIFRX_CR_SPDIFEN_MASK)) {
@@ -480,7 +481,7 @@ static int stm32_spdifrx_get_ctrl_data(struct 
stm32_spdifrx_data *spdifrx)

         ret = stm32_spdifrx_start_sync(spdifrx);
         if (ret < 0)
-               goto end;
+               goto stop;

         if 
(wait_for_completion_interruptible_timeout(&spdifrx->cs_completion,
  
msecs_to_jiffies(100))
@@ -489,6 +490,7 @@ static int stm32_spdifrx_get_ctrl_data(struct 
stm32_spdifrx_data *spdifrx)
                 ret = -EAGAIN;
         }

+stop:
         stm32_spdifrx_stop(spdifrx);
         stm32_spdifrx_dma_ctrl_stop(spdifrx);

I realize that cleanup path needs to be at least re-ordered.
I will think about another patch to fix this.

Regards
Olivier





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

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  9:23 [PATCH 0/3] ASoC: stm32: spdifrx: Fix resource handling and startup phucduc.bui
2026-11-27  9:23 ` [PATCH 1/3] ASoC: stm32: spdifrx: Use devm helpers and drop remove callback phucduc.bui
2026-11-27  9:23 ` [PATCH 2/3] ASoC: stm32: spdifrx: Fix refcount on sync failure phucduc.bui
2026-11-30 17:18   ` Olivier MOYSAN [this message]
2026-11-27  9:23 ` [PATCH 3/3] ASoC: stm32: spdifrx: Enable clock before setting substream phucduc.bui

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=e8344834-5cfd-453e-af28-5a2bc385e582@foss.st.com \
    --to=olivier.moysan@foss.st.com \
    --cc=alexandre.torgue@foss.st.com \
    --cc=arnaud.pouliquen@foss.st.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=perex@perex.cz \
    --cc=phucduc.bui@gmail.com \
    --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