Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Joseph Telaak" <joseph@telaak.dev>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot
Date: Fri, 04 Dec 2026 03:41:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-249065@kernel.org> (raw)
In-Reply-To: <20261203-omap-mcbsp-tdm-slot-v4-2-08985f4284f9@telaak.dev>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ASoC: omap-mcbsp: data race in omap_mcbsp_slot_rate_refine()
- [Medium] ASoC: omap-mcbsp: sysclk state corrupted on error in
  set_dai_sysclk()
- [Medium] ASoC: omap-mcbsp: stale sysclk_id and in_freq left in
  omap_mcbsp_remove()
- [Medium] ASoC: omap-mcbsp: set_tdm_slot() latches dynamic slots and
  blocks post-hw_free
--

commit 730349fbef80ded0ca4e7f899480736b5797d744
Author: Joseph Telaak <joseph@telaak.dev>

ASoC: ti: omap-mcbsp: Implement set_tdm_slot

This commit implements the set_tdm_slot() DAI op for omap-mcbsp so generic
cards such as simple-audio-card can configure a frame of slots x slot_width
bit clocks and let hw_params() compute the SRG divider per sample rate.

> diff --git a/sound/soc/ti/omap-mcbsp.c b/sound/soc/ti/omap-mcbsp.c
> index 6e0c7d7359f49..0f3925622106b 100644
> --- a/sound/soc/ti/omap-mcbsp.c
> +++ b/sound/soc/ti/omap-mcbsp.c
> @@ -759,12 +759,242 @@ static int omap_mcbsp_hwrule_min_buffersize(struct snd_pcm_hw_params *params,
>  	return snd_interval_refine(buffer_size, &frames);
>  }

[ ... ]

> +static int omap_mcbsp_slot_rate_refine(struct snd_pcm_hw_params *params,
> +				       struct omap_mcbsp *mcbsp, int stream)
> +{
> +	struct snd_interval *rate = hw_param_interval(params,
> +						      SNDRV_PCM_HW_PARAM_RATE);
> +	unsigned int list[ARRAY_SIZE(omap_mcbsp_slot_rates)];
> +	unsigned int n;
> +
> +	/* Slots cleared since open: no rates to compute */
> +	if (!omap_mcbsp_slots_on(mcbsp))
> +		return 0;
> +	/* Both directions share the sample rate generator */
> +	if (omap_mcbsp_slot_other(mcbsp, stream))
> +		return snd_interval_list(rate, 1, &mcbsp->slot_rate, 0);
> +	/* A sysclk a card sets in hw_params() is checked there */
> +	if (!mcbsp->sysclk_static || !omap_mcbsp_slot_sysclk(mcbsp))
> +		return 0;
> +
> +	n = omap_mcbsp_slot_rate_list(mcbsp, list);
> +	return snd_interval_list(rate, n, list, 0);
> +}

[Severity: Medium]
Can omap_mcbsp_slot_rate_refine() race with concurrent hw_params(),
hw_free(), or set_tdm_slot() calls on the opposite direction?

snd_pcm_hw_refine_with_lock() only holds the target substream's
runtime->buffer_mutex, while soc_pcm_hw_params() and soc_pcm_hw_free() hold
rtd->card->pcm_mutex.  Neither path takes mcbsp->lock, so on SMP OMAP4/5
systems the two directions access the shared slot and clock fields without
synchronization:

CPU0 (capture hw_refine)          CPU1 (playback hw_params)
snd_pcm_hw_refine_with_lock()     soc_pcm_hw_params()
  lock(&runtime->buffer_mutex)      lock(&card->pcm_mutex)
  omap_mcbsp_slot_rate_refine()     omap_mcbsp_dai_hw_params()
    if (omap_mcbsp_slot_other())
    snd_interval_list(..., &mcbsp->slot_rate, 0)
      min(..., list[0])               mcbsp->slot_rate = new_rate;
      max(..., list[0])  <-- data race / mismatched min & max

snd_interval_list() dereferences list[0] three times (snd_interval_test(),
min(), and max()), so a concurrent single-stream rate change overwriting
mcbsp->slot_rate can leave list_range.min > list_range.max and fail
refinement with -EINVAL.  Without ordering between mcbsp->slot_rate and
mcbsp->slot_streams, CPU0 can also see the new slot_streams bit with a stale
or zero slot_rate.

Similarly, concurrent set_tdm_slot() or set_dai_sysclk() updates can mutate
slots, slot_width, or in_freq across omap_mcbsp_slot_rate_refine() and
omap_mcbsp_slot_div(), which reads mcbsp->in_freq three times.  Protecting
these fields with mcbsp->lock and snapshotting slot_rate into a local
variable before calling snd_interval_list() would avoid the race.

[ ... ]

> @@ -1184,6 +1449,67 @@ static int omap_mcbsp_dai_set_clkdiv(struct snd_soc_dai *cpu_dai,
>  	return 0;
>  }

[ ... ]

> +	/* The other direction may already run with the current slots */
> +	if (mcbsp->configured) {
> +		if (slots == mcbsp->slots && slot_width == mcbsp->slot_width &&
> +		    tx == mcbsp->tx_slots && rx == mcbsp->rx_slots)
> +			return 0;
> +		return -EBUSY;
> +	}
> +
> +	/* Back to the set_clkdiv() divider */
> +	if (mcbsp->slot_width && !slot_width) {
> +		mcbsp->cfg_regs.srgr1 &= ~CLKGDV(0xff);
> +		mcbsp->cfg_regs.srgr1 |=
> +			CLKGDV((mcbsp->clk_div ? mcbsp->clk_div : 1) - 1);
> +	}
> +
> +	mcbsp->slots = slots;
> +	mcbsp->slot_width = slot_width;
> +	mcbsp->tx_slots = tx;
> +	mcbsp->rx_slots = rx;

[Severity: Medium]
Will dynamic per-stream set_tdm_slot() callers get locked into the first
stream's width and hit -EBUSY after hw_free()?

audio-graph-card2 with dai-tdm-slot-width-map calls snd_soc_dai_set_tdm_slot()
per stream from simple_util_hw_params() -> simple_set_tdm().  Unlike
set_dai_sysclk(), which tracks sysclk_static, set_tdm_slot() does not
distinguish init-time slots from dynamic ones and neither hw_free() nor
shutdown() clears them, so on later opens omap_mcbsp_slot_constraints() pins
CHANNELS and SAMPLE_BITS to the first stream's width and rejects other
formats before simple_set_tdm() can run.

In addition, omap_mcbsp_dai_hw_free() clears mcbsp->slot_streams while
mcbsp->configured stays set until shutdown().  Reconfiguring an open stream
via hw_free() -> hw_params() with a different slot width then fails with
-EBUSY here because this checks mcbsp->configured instead of
mcbsp->active || mcbsp->slot_streams.

> @@ -1192,13 +1518,19 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
>  	struct omap_mcbsp_reg_cfg *regs = &mcbsp->cfg_regs;
>  	int err = 0;
>  
> -	if (mcbsp->active) {
> +	/* With slots, a configured stream holds the sysclk until hw_free() */
> +	if (mcbsp->active || mcbsp->slot_streams) {
>  		if (freq == mcbsp->in_freq)
>  			return 0;
>  		else
>  			return -EBUSY;
>  	}
>  
> +	/* Set outside a stream, it holds for the next open */
> +	if (!snd_soc_dai_active(cpu_dai))
> +		mcbsp->sysclk_static = true;
> +	else if (freq != mcbsp->in_freq)
> +		mcbsp->sysclk_static = false;
>  	mcbsp->in_freq = freq;
>  	regs->srgr2 &= ~CLKSM;
>  	regs->pcr0 &= ~SCLKME;
> @@ -1243,6 +1575,8 @@ static int omap_mcbsp_dai_set_dai_sysclk(struct snd_soc_dai *cpu_dai,
>  	default:
>  		err = -ENODEV;
>  	}
> +	if (!err)
> +		mcbsp->sysclk_id = clk_id;
>  
>  	return err;
>  }

[Severity: Medium]
Does omap_mcbsp_dai_set_dai_sysclk() leave sysclk_static, in_freq, and
cfg_regs out of sync with sysclk_id when setting the clock fails?

mcbsp->sysclk_static, mcbsp->in_freq, and the CLKSM/SCLKME bits in cfg_regs
are overwritten before switch (clk_id) runs, while only mcbsp->sysclk_id is
gated on !err.  If omap2_mcbsp_set_clks_src() fails or clk_id is unsupported,
later TDM slot checks evaluate the old sysclk_id against the rejected
frequency and sysclk_static flag.

Also, the early return when mcbsp->active || mcbsp->slot_streams is set only
checks freq == mcbsp->in_freq, so requesting a different clk_id at the same
frequency silently returns 0 instead of -EBUSY.

> @@ -1266,6 +1600,13 @@ static int omap_mcbsp_remove(struct snd_soc_dai *dai)
>  
>  	pm_runtime_disable(mcbsp->dev);
>  
> +	/* The slots belong to the card that set them */
> +	mcbsp->slots = 0;
> +	mcbsp->slot_width = 0;
> +	mcbsp->tx_slots = 0;
> +	mcbsp->rx_slots = 0;
> +	mcbsp->sysclk_static = false;
> +
>  	return 0;
>  }

[Severity: Medium]
Should omap_mcbsp_remove() also reset mcbsp->sysclk_id and mcbsp->in_freq?

struct omap_mcbsp persists across card unbind and rebind.  If a prior card
left OMAP_MCBSP_SYSCLK_CLKX_EXT or OMAP_MCBSP_SYSCLK_CLKR_EXT in sysclk_id,
omap_mcbsp_slots_on() returns false for a newly bound card and silently
disables TDM slot mode; a stale in_freq with OMAP_MCBSP_SYSCLK_CLKS_EXT
likewise bypasses the missing-sysclk and fclk checks in
omap_mcbsp_slot_params_check().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-omap-mcbsp-tdm-slot-v4-0-08985f4284f9@telaak.dev?part=2

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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  3:13 [PATCH v4 0/2] ASoC: ti: omap-mcbsp: TDM slots for generic cards Joseph Telaak
2026-12-04  3:13 ` [PATCH v4 1/2] ASoC: ti: omap-mcbsp: Use all 8 bits of CLKGDV Joseph Telaak
2026-12-04  3:13 ` [PATCH v4 2/2] ASoC: ti: omap-mcbsp: Implement set_tdm_slot Joseph Telaak
2026-12-04  3:41   ` sashiko-bot [this message]

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-249065@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=joseph@telaak.dev \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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