Skip to contentSkip to conversation

ASoC: meson: axg-tdm-formatter: Use guard() for mutex locks

PATCH In review [email protected] 4 participants 9 messages Jun 10 – Jun 12

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: meson: axg-tdm-formatter: Use guard() for mutex locks

From: bui duc phuc <[email protected]>

Clean up the code using guard() for mutex locks.
Merely code refactoring, and no behavior change.

---
 sound/soc/meson/axg-tdm-formatter.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
sound/soc/meson/axg-tdm-formatter.c +8 -14
index f451e4dce442..a6ba401104d5 100644
@@ -157,20 +157,19 @@ static int axg_tdm_formatter_attach(struct axg_tdm_formatter *formatter)
 	struct axg_tdm_stream *ts = formatter->stream;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 
 	/* Catch up if the stream is already running when we attach */
 	if (ts->ready) {
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {
 			pr_err("failed to enable formatter\n");
-			goto out;
+			return ret;
 		}
 	}
 
 	list_add_tail(&formatter->list, &ts->formatter_list);
-out:
-	mutex_unlock(&ts->lock);
+
 	return ret;
 }
 
@@ -178,9 +177,8 @@ static void axg_tdm_formatter_dettach(struct axg_tdm_formatter *formatter)
 {
 	struct axg_tdm_stream *ts = formatter->stream;
 
-	mutex_lock(&ts->lock);
-	list_del(&formatter->list);
-	mutex_unlock(&ts->lock);
+	scoped_guard(mutex, &ts->lock)
+		list_del(&formatter->list);
 
 	axg_tdm_formatter_disable(formatter);
 }
@@ -330,7 +328,7 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 	struct axg_tdm_formatter *formatter;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 	ts->ready = true;
 
 	/* Start all the formatters attached to the stream */
@@ -338,12 +336,10 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {
 			pr_err("failed to start tdm stream\n");
-			goto out;
+			return ret;
 		}
 	}
 
-out:
-	mutex_unlock(&ts->lock);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(axg_tdm_stream_start);
@@ -352,15 +348,13 @@ void axg_tdm_stream_stop(struct axg_tdm_stream *ts)
 {
 	struct axg_tdm_formatter *formatter;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 	ts->ready = false;
 
 	/* Stop all the formatters attached to the stream */
 	list_for_each_entry(formatter, &ts->formatter_list, list) {
 		axg_tdm_formatter_disable(formatter);
 	}
-
-	mutex_unlock(&ts->lock);
 }
 EXPORT_SYMBOL_GPL(axg_tdm_stream_stop);
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] If axg_tdm_formatter_attach() fails to enable the formatter (e.g. when trying to catch up to an already running stream), it returns an error and skips list_add_tail(). However, the DAPM framework will log the error but still mark the widget as powered up. Later, during teardown or routing changes, DAPM will call axg_tdm_formatter_dettach(), which blindly executes list_del(&formatter->list). Since the list head is zero-initialized by devm_kzalloc and was never added to the list, list_del() will dereference NULL pointers, causing a kernel crash.
- [High] The return value of axg_tdm_stream_start() is completely ignored in axg_tdm_iface_trigger(). If the stream fails to start (for example, due to a failure in axg_tdm_formatter_enable()), axg_tdm_stream_start() correctly returns an error code. However, axg_tdm_iface_trigger() drops this error and unconditionally returns 0.
--

commit d9e23c7e7ade1061d83943c614a45fe952dec33d
Author: bui duc phuc <[email protected]>

ASoC: meson: axg-tdm-formatter: Use guard() for mutex locks

Clean up the code using guard() for mutex locks.
Merely code refactoring, and no behavior change.

Quoted 16 lines
sound/soc/meson/axg-tdm-formatter.c +1 -2
@@ -157,20 +157,19 @@ static int axg_tdm_formatter_attach(struct axg_tdm_formatter *formatter)
 	struct axg_tdm_stream *ts = formatter->stream;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 
 	/* Catch up if the stream is already running when we attach */
 	if (ts->ready) {
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {
 			pr_err("failed to enable formatter\n");
-			goto out;

sound/soc/meson/axg-tdm-formatter.c static int axg_tdm_formatter_attach(struct axg_tdm_formatter *formatter) · line 174

Quoted 10 lines
sound/soc/meson/axg-tdm-formatter.c +2 -2
+			return ret;
 		}
 	}
 
 	list_add_tail(&formatter->list, &ts->formatter_list);
-out:
-	mutex_unlock(&ts->lock);
+
 	return ret;
 }

[Severity: High]
This is a pre-existing issue, but if axg_tdm_formatter_enable() fails, this error path returns early and skips list_add_tail().

If the DAPM framework logs the error but still marks the widget as powered up, it might later call axg_tdm_formatter_dettach() during teardown or routing changes.

Since axg_tdm_formatter_dettach() blindly executes:

    scoped_guard(mutex, &ts->lock)
        list_del(&formatter->list);

Does this result in a NULL pointer dereference because the list head is zero-initialized and was never actually added to the list?

[ ... ]

Quoted 12 lines
sound/soc/meson/axg-tdm-formatter.c +1 -1
@@ -330,7 +328,7 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 	struct axg_tdm_formatter *formatter;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 	ts->ready = true;
 
 	/* Start all the formatters attached to the stream */
@@ -338,12 +336,10 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {

sound/soc/meson/axg-tdm-formatter.c int axg_tdm_stream_start(struct axg_tdm_stream *ts) · line 344

Quoted 10 lines
sound/soc/meson/axg-tdm-formatter.c +1 -3
 			pr_err("failed to start tdm stream\n");
-			goto out;
+			return ret;
 		}
 	}
 
-out:
-	mutex_unlock(&ts->lock);
 	return ret;
 }

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for the return value of axg_tdm_stream_start() to be lost?

Looking at axg_tdm_iface_trigger() in sound/soc/meson/axg-tdm-interface.c:

    switch (cmd) {
    case SNDRV_PCM_TRIGGER_START:
    case SNDRV_PCM_TRIGGER_RESUME:
    case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
        axg_tdm_stream_start(ts);
        break;

If axg_tdm_stream_start() returns an error code here, the error isn't propagated back to ALSA. Does this mask the initialization failure and cause the framework to silently fail, leaving applications to hang while waiting for period elapsed events?

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

_______________________________________________
linux-amlogic mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-amlogic
maintainer
#2 lore raw
Quoted 46 lines · On mer. 10 juin 2026 at 10:32, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] If axg_tdm_formatter_attach() fails to enable the formatter (e.g. when trying to catch up to an already running stream), it returns an error and skips list_add_tail(). However, the DAPM framework will log the error but still mark the widget as powered up. Later, during teardown or routing changes, DAPM will call axg_tdm_formatter_dettach(), which blindly executes list_del(&formatter->list). Since the list head is zero-initialized by devm_kzalloc and was never added to the list, list_del() will dereference NULL pointers, causing a kernel crash.
- [High] The return value of axg_tdm_stream_start() is completely ignored in axg_tdm_iface_trigger(). If the stream fails to start (for example, due to a failure in axg_tdm_formatter_enable()), axg_tdm_stream_start() correctly returns an error code. However, axg_tdm_iface_trigger() drops this error and unconditionally returns 0.
--

commit d9e23c7e7ade1061d83943c614a45fe952dec33d
Author: bui duc phuc <[email protected]>

ASoC: meson: axg-tdm-formatter: Use guard() for mutex locks

Clean up the code using guard() for mutex locks.
Merely code refactoring, and no behavior change.

Quoted 20 lines
sound/soc/meson/axg-tdm-formatter.c +2 -2
@@ -157,20 +157,19 @@ static int axg_tdm_formatter_attach(struct axg_tdm_formatter *formatter)
 	struct axg_tdm_stream *ts = formatter->stream;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 
 	/* Catch up if the stream is already running when we attach */
 	if (ts->ready) {
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {
 			pr_err("failed to enable formatter\n");
-			goto out;
+			return ret;
 		}
 	}
 
Quoted 10 lines

sound/soc/meson/axg-tdm-formatter.c static int axg_tdm_formatter_attach(struct axg_tdm_formatter *formatter) · line 174

Quoted 6 lines
sound/soc/meson/axg-tdm-formatter.c +1 -2
 	list_add_tail(&formatter->list, &ts->formatter_list);
-out:
-	mutex_unlock(&ts->lock);
+
 	return ret;
 }

[Severity: High]
This is a pre-existing issue, but if axg_tdm_formatter_enable() fails, this error path returns early and skips list_add_tail().

This is path the very few have tested since it only happens when someone is playing around the routing kcontrol on a running stream. Still it is supposed to work.

Quoted 4 lines

If the DAPM framework logs the error but still marks the widget as powered up, it might later call axg_tdm_formatter_dettach() during teardown or routing changes.

Ok but the IF is important here.

A widget that failed PRE_PMU event should not receive a PRE_PMD one. so it makes to unwind anything on error.

Quoted 46 lines

Since axg_tdm_formatter_dettach() blindly executes:

    scoped_guard(mutex, &ts->lock)
        list_del(&formatter->list);

Does this result in a NULL pointer dereference because the list head is zero-initialized and was never actually added to the list?

[ ... ]

Quoted 12 lines
sound/soc/meson/axg-tdm-formatter.c +1 -1
@@ -330,7 +328,7 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 	struct axg_tdm_formatter *formatter;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 	ts->ready = true;
 
 	/* Start all the formatters attached to the stream */
@@ -338,12 +336,10 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {

sound/soc/meson/axg-tdm-formatter.c int axg_tdm_stream_start(struct axg_tdm_stream *ts) · line 344

Quoted 10 lines
sound/soc/meson/axg-tdm-formatter.c +1 -3
 			pr_err("failed to start tdm stream\n");
-			goto out;
+			return ret;
 		}
 	}
 
-out:
-	mutex_unlock(&ts->lock);
 	return ret;
 }

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for the return value of axg_tdm_stream_start() to be lost?

Looking at axg_tdm_iface_trigger() in sound/soc/meson/axg-tdm-interface.c:

    switch (cmd) {
    case SNDRV_PCM_TRIGGER_START:
    case SNDRV_PCM_TRIGGER_RESUME:
    case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
        axg_tdm_stream_start(ts);
        break;
Quoted 4 lines

If axg_tdm_stream_start() returns an error code here, the error isn't propagated back to ALSA. Does this mask the initialization failure and cause the framework to silently fail, leaving applications to hang while waiting for period elapsed events?

2 things here:

1) This is a DPCM backend so no "period elapsed events" here. This would happen on the FIFO frontends

2) The return code is a grey area here. There could be multiple formatters attached, this is mostly why the error code is ignored, so a working backend would continue to operate if another fail. That's the idea at least.

Signature · 1 line
Jerome
maintainer
#3 lore raw
In reply to [email protected]
Quoted 4 lines · On mer. 10 juin 2026 at 17:21, [email protected] wrote:

From: bui duc phuc <[email protected]>

Clean up the code using guard() for mutex locks.
Merely code refactoring, and no behavior change.

I suppose it is OK but it does not seem to really clean anything and make the code easier to follow in that instance, from my perspective at least.

If there is policy to systematically use guard() whenever possible then OK, otherwise it seems unnecessary.

Quoted 85 lines
---
 sound/soc/meson/axg-tdm-formatter.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
sound/soc/meson/axg-tdm-formatter.c +8 -14
index f451e4dce442..a6ba401104d5 100644
@@ -157,20 +157,19 @@ static int axg_tdm_formatter_attach(struct axg_tdm_formatter *formatter)
 	struct axg_tdm_stream *ts = formatter->stream;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 
 	/* Catch up if the stream is already running when we attach */
 	if (ts->ready) {
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {
 			pr_err("failed to enable formatter\n");
-			goto out;
+			return ret;
 		}
 	}
 
 	list_add_tail(&formatter->list, &ts->formatter_list);
-out:
-	mutex_unlock(&ts->lock);
+
 	return ret;
 }
 
@@ -178,9 +177,8 @@ static void axg_tdm_formatter_dettach(struct axg_tdm_formatter *formatter)
 {
 	struct axg_tdm_stream *ts = formatter->stream;
 
-	mutex_lock(&ts->lock);
-	list_del(&formatter->list);
-	mutex_unlock(&ts->lock);
+	scoped_guard(mutex, &ts->lock)
+		list_del(&formatter->list);
 
 	axg_tdm_formatter_disable(formatter);
 }
@@ -330,7 +328,7 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 	struct axg_tdm_formatter *formatter;
 	int ret = 0;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 	ts->ready = true;
 
 	/* Start all the formatters attached to the stream */
@@ -338,12 +336,10 @@ int axg_tdm_stream_start(struct axg_tdm_stream *ts)
 		ret = axg_tdm_formatter_enable(formatter);
 		if (ret) {
 			pr_err("failed to start tdm stream\n");
-			goto out;
+			return ret;
 		}
 	}
 
-out:
-	mutex_unlock(&ts->lock);
 	return ret;
 }
 EXPORT_SYMBOL_GPL(axg_tdm_stream_start);
@@ -352,15 +348,13 @@ void axg_tdm_stream_stop(struct axg_tdm_stream *ts)
 {
 	struct axg_tdm_formatter *formatter;
 
-	mutex_lock(&ts->lock);
+	guard(mutex)(&ts->lock);
 	ts->ready = false;
 
 	/* Stop all the formatters attached to the stream */
 	list_for_each_entry(formatter, &ts->formatter_list, list) {
 		axg_tdm_formatter_disable(formatter);
 	}
-
-	mutex_unlock(&ts->lock);
 }
 EXPORT_SYMBOL_GPL(axg_tdm_stream_stop);
Signature · 1 line
Jerome
maintainer
#4 lore raw
Quoted 11 lines · Jerome Brunet
Quoted 2 lines · On mer. 10 juin 2026 at 17:21, [email protected] wrote:

Clean up the code using guard() for mutex locks.
Merely code refactoring, and no behavior change.

I suppose it is OK but it does not seem to really clean anything and make the code easier to follow in that instance, from my perspective at least.

If there is policy to systematically use guard() whenever possible then OK, otherwise it seems unnecessary.

I don't know about policy but there's definitely people who are keen on converting things to this pattern.

#5 lore raw
In reply to Jerome Brunet

Hi Jerome,

Thank you for your feedback,

Quoted 8 lines

I suppose it is OK but it does not seem to really clean anything and make the code easier to follow in that instance, from my perspective at least.

If there is policy to systematically use guard() whenever possible then OK, otherwise it seems unnecessary.

I have noticed that guard() has been adopted in several subsystems. Since this appears to be the only place in the Meson ASoC code currently using mutex_lock()/mutex_unlock(), I converted it for consistency with the newer style.

Going forward, should new Meson ASoC code use guard(), or should it continue using the traditional mutex_lock()/mutex_unlock() pattern?

Best regards,
Phuc

maintainer Reviewed-by
#6 lore raw
Quoted 13 lines · Bui Duc Phuc

Hi Jerome,

Thank you for your feedback,

Quoted 8 lines

I suppose it is OK but it does not seem to really clean anything and make the code easier to follow in that instance, from my perspective at least.

If there is policy to systematically use guard() whenever possible then OK, otherwise it seems unnecessary.

Quoted 7 lines

I have noticed that guard() has been adopted in several subsystems. Since this appears to be the only place in the Meson ASoC code currently using mutex_lock()/mutex_unlock(), I converted it for consistency with the newer style.

Going forward, should new Meson ASoC code use guard(), or should it continue using the traditional mutex_lock()/mutex_unlock() pattern?

Can't say if there is such policy either. IMO it should be more a case-by-case thing

The code is not better or worse with the change but you went through the trouble of doing so, if Mark is fine with it, let's have it

Quoted 3 lines

Best regards,
Phuc

Signature · 1 line
Jerome
#7 lore raw

Hi Jerome,

Thank you for the review and for the Reviewed-by tag.

Quoted 6 lines

The code is not better or worse with the change but you went through the trouble of doing so, if Mark is fine with it, let's have it

Regarding the point about whether the change makes the code better or worse, I have sent an additional patch to address related cleanup in the same area:

https://lore.kernel.org/all/[email protected]/

Would appreciate your review on that as well.

While looking into the Meson ASoC probe paths,
I noticed a few consistency issues that seem to appear across multiple drivers:

1. Mixed usage of dev_err_probe() and dev_err() in probe error paths.

2. Inconsistent error message formatting: some messages start with lowercase ("failed to init..."), while others start with uppercase ("Failed
to register...").

3. In aiu_probe(), error paths for IRQ retrieval do not provide additional context,
making it harder to identify which resource failed:

   aiu->i2s.irq = platform_get_irq_byname(pdev, "i2s");
   if (aiu->i2s.irq < 0)
           return aiu->i2s.irq;

   aiu->spdif.irq = platform_get_irq_byname(pdev, "spdif");
   if (aiu->spdif.irq < 0)
           return aiu->spdif.irq;

4 ......

Do you think addressing these issues would improve readability and maintainability of the Meson codebase over time?
Or is it generally preferred to leave these kinds of consistency improvements untouched unless a functional bug is being fixed?

Best regards,
Phuc

maintainer
#8 lore raw
In reply to [email protected]
Quoted 1 line · On Wed, 10 Jun 2026 17:21:53 +0700, [email protected] wrote:

ASoC: meson: axg-tdm-formatter: Use guard() for mutex locks

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.2

Thanks!

[1/1] ASoC: meson: axg-tdm-formatter: Use guard() for mutex locks

      https://git.kernel.org/broonie/sound/c/a68bbd4ac1b1

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark