Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Bahadır Hamza Öztürk" <bahadirhamza2@gmail.com>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ASoC: amd: acp: add DMIC capture switch with mic mute LED support
Date: Fri, 04 Dec 2026 20:23:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-250617@kernel.org> (raw)
In-Reply-To: <20261204200115.10636-1-bahadirhamza2@gmail.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] ASoC: amd: acp: NULL deref in acp_pcm_probe() before
  dev_set_drvdata()
- [Medium] ASoC: amd: acp: mmap readers bypass DMIC soft mute while mute
  LED is on
- [Medium] ASoC: amd: acp: stale mute_pos leaks audio after prepare or
  resume
- [Medium] ASoC: amd: acp: modulo mute_pos misses full-buffer wraparound
  in pointer
- [Medium] ASoC: amd: acp: missing snd_ctl_led_request() and SND_CTL_LED
  Kconfig select
- [Medium] ASoC: amd: acp: unmuting in acp_dmic_switch_put() leaks
  pending muted audio
- [Medium] ASoC: amd: acp: unconditional MIC_LED flag breaks mic mute
  LED state
--

commit c6c200b5312901dd7e1038bc5492d1be0b4d966a
Author: Bahadır Hamza Öztürk <bahadirhamza2@gmail.com>

ASoC: amd: acp: add DMIC capture switch with mic mute LED support

This commit adds a "DMIC Capture Switch" mixer control with
SNDRV_CTL_ELEM_ACCESS_MIC_LED to the ACP PCM component when a PDM
controller is present, and implements muting in software by zeroing
newly captured samples in the .pointer callback.

> diff --git a/sound/soc/amd/acp/acp-platform.c b/sound/soc/amd/acp/acp-platform.c
> index f2c8853775ad3..2163e233e6cba 100644
> --- a/sound/soc/amd/acp/acp-platform.c
> +++ b/sound/soc/amd/acp/acp-platform.c
> @@ -268,10 +268,33 @@ static int acp_dma_hw_params(struct snd_soc_component *component,
>  	/* Configure ACP DMA block with params */
>  	config_pte_for_stream(chip, stream);
>  	config_acp_dma(chip, stream, size);
> +	stream->mute_pos = 0;

[Severity: Medium]
Can stream->mute_pos go stale across prepare or system resume and leak
unmuted audio?

stream->mute_pos is only reset here in acp_dma_hw_params(), not in
acp_dmic_prepare(), acp_dmic_dai_trigger(), or restore_acp_pdm_params().
After system suspend/resume (where acp_reset() zeroes the hardware byte
counter) or STOP/XRUN recovery, DMA restarts at offset 0 while
stream->mute_pos still holds its previous non-zero offset.  When
acp_dma_pointer() next observes pos >= stream->mute_pos while muted,
acp_dmic_soft_mute() only zeroes [start, pos) and skips [0, start),
delivering live audio to userspace while the mute LED is on.

Additionally, unlike acp_i2s_trigger(), acp_dmic_dai_trigger() never
snapshots stream->bytescount on start, so reopening a stream without an
ACP reset can begin at a non-zero ring offset that does not match
mute_pos = 0.

>  
>  	return 0;
>  }
>  
> +/*
> + * The ACP PDM block has no mute control, so mute the DMIC in software:
> + * every captured byte between the last handled position and the
> + * current hardware position is cleared before user space can see it.
> + * This is called from the .pointer callback, which runs before any
> + * hw_ptr update, so it covers both period interrupts and mmap readers
> + * that query the position in between.
> + */
> +static void acp_dmic_soft_mute(struct snd_pcm_substream *substream,
> +			       struct acp_stream *stream, u32 pos, u32 buffersize)
> +{
> +	unsigned char *area = substream->runtime->dma_area;
> +	u32 start = stream->mute_pos;
> +
> +	if (start <= pos) {
> +		memset(area + start, 0, pos - start);
> +	} else {
> +		memset(area + start, 0, buffersize - start);
> +		memset(area, 0, pos);
> +	}
> +}

[Severity: Medium]
Can mmap capture readers still read live microphone audio while muted?

Both acp_pcm_hardware_capture and acp6x_pcm_hardware_capture advertise
SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID, mapping
substream->runtime->dma_area directly into userspace.  Because PDM DMA
continues writing live samples into those pages and acp_dmic_soft_mute()
only zeroes them retroactively in acp_dma_pointer(), an mmap reader
inspecting the buffer ahead of hw_ptr can capture unmuted audio while the
hardware mic mute LED stays lit.

Scrubbing up to 64 KB with memset() here also runs in hardirq atomic
context while holding both chip->acp_lock (from acp_irq_handler()) and
the PCM substream spinlock.

[ ... ]

> @@ -291,6 +314,13 @@ static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component,
>  
>  	pos = do_div(bytescount, buffersize);
>  
> +	if (stream->dai_id == DMIC_INSTANCE &&
> +	    substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
> +		if (READ_ONCE(chip->dmic_mute))
> +			acp_dmic_soft_mute(substream, stream, pos, buffersize);
> +		stream->mute_pos = pos;
> +	}

[Severity: Medium]
Will a full-buffer wraparound between .pointer() calls bypass the soft
mute?

do_div(bytescount, buffersize) discards the unwrapped byte progression
before calling acp_dmic_soft_mute().  With a 2-period buffer (as small as
2048 bytes) under a delayed or coalesced interrupt, or under
no_period_wakeup, DMA can advance by buffersize bytes so pos equals
stream->mute_pos.  acp_dmic_soft_mute() then clears zero bytes while
snd_pcm_update_hw_ptr0() detects the wrap via jiffies and advances hw_ptr
by runtime->buffer_size, exposing the full unmuted buffer to userspace.

Tracking the unwrapped 64-bit byte count before do_div() and clearing the
whole ring buffer whenever the delta reaches buffersize (or moves
backwards) would avoid this.

[ ... ]

> +static int acp_dmic_switch_put(struct snd_kcontrol *kcontrol,
> +			       struct snd_ctl_elem_value *ucontrol)
> +{
> +	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
> +	struct acp_chip_info *chip = dev_get_drvdata(component->dev->parent);
> +	bool mute = !ucontrol->value.integer.value[0];
> +
> +	if (mute == READ_ONCE(chip->dmic_mute))
> +		return 0;
> +
> +	WRITE_ONCE(chip->dmic_mute, mute);
> +	return 1;
> +}

[Severity: Medium]
Does clearing chip->dmic_mute here leak audio that was captured while
muted?

While muted, PDM DMA keeps writing live samples ahead of
stream->mute_pos.  Clearing chip->dmic_mute without first zeroing the
pending [stream->mute_pos, current_hw_pos) range causes the next
acp_dma_pointer() call to skip acp_dmic_soft_mute() and advance hw_ptr
over up to a full period of audio recorded while the mute LED was on.

> +
> +static const struct snd_kcontrol_new acp_dmic_controls[] = {
> +	{
> +		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
> +		.name	= "DMIC Capture Switch",
> +		.access	= SNDRV_CTL_ELEM_ACCESS_READWRITE |
> +			  SNDRV_CTL_ELEM_ACCESS_MIC_LED,
> +		.info	= snd_ctl_boolean_mono_info,
> +		.get	= acp_dmic_switch_get,
> +		.put	= acp_dmic_switch_put,
> +	},
> +};

[Severity: Medium]
Does setting SNDRV_CTL_ELEM_ACCESS_MIC_LED unconditionally on this
control regress mic mute LED handling on other ACP platforms?

Because chip->dmic_mute defaults to false (switch on), adding this
control with SNDRV_CTL_ELEM_ACCESS_MIC_LED registers an always-on entry
in the global snd_ctl_leds mic list in sound/core/control_led.c.  On ACP
7.0/7.1/7.2 laptops, snd_amd_acp_acpi_find_config() defaults to
FLAG_AMD_LEGACY_ONLY_DMIC and registers acp-pdm-mach as a second card
alongside snd_hda_intel; since existing userspace does not toggle the new
DMIC switch on acp-pdm-mach, snd_ctl_led_set_state() keeps the mic mute
LED permanently off even when HDA capture is muted.

On legacy I2S+DMIC cards such as acp3x-es83xx, none of the codec capture
switches or headset mic pin switches carry SNDRV_CTL_ELEM_ACCESS_MIC_LED,
so muting DMIC lights the mic mute LED even while the analog headset mic
is live.

> +
> +static int acp_pcm_probe(struct snd_soc_component *component)
> +{
> +	struct acp_chip_info *chip = dev_get_drvdata(component->dev->parent);
> +
> +	if (!chip->is_pdm_dev)
> +		return 0;

[Severity: High]
Can dev_get_drvdata(component->dev->parent) return NULL here and crash
during probe?

In acp_pci_probe(), both create_acp_platform_devs() and
acp_machine_select() run before dev_set_drvdata(&pci->dev, chip):

sound/soc/amd/acp/acp-pci.c:acp_pci_probe() {
	...
	acp_machine_select(chip);

	INIT_LIST_HEAD(&chip->stream_list);
	spin_lock_init(&chip->acp_lock);
skip_pdev_creation:
	dev_set_drvdata(&pci->dev, chip);
	...
}

When the platform, DMIC codec, and machine drivers are built in or
already loaded, acp_machine_select() synchronously calls
devm_snd_soc_register_card() -> snd_soc_component_probe() ->
acp_pcm_probe(), causing a NULL pointer dereference on chip->is_pdm_dev.
Even if acp_pcm_probe() avoided parent drvdata, snd_card_register() ->
snd_ctl_led_register() synchronously invokes acp_dmic_switch_get() for
SNDRV_CTL_ELEM_ACCESS_MIC_LED controls, which also dereferences
dev_get_drvdata(component->dev->parent).

Setting dev_set_drvdata(&pci->dev, chip) in acp_pci_probe() before
creating the child platform devices would avoid both crashes.

> +
> +	return snd_soc_add_component_controls(component, acp_dmic_controls,
> +					      ARRAY_SIZE(acp_dmic_controls));
> +}

[Severity: Medium]
Should acp_pcm_probe() call snd_ctl_led_request(), and should
SND_SOC_AMD_ACP_PCM select SND_CTL_LED?

Adding a control with SNDRV_CTL_ELEM_ACCESS_MIC_LED does not auto-load
snd-ctl-led on its own, so when CONFIG_SND_CTL_LED=m and no HDA or SOF
driver requests the layer, the mic mute LED stays inactive.  In addition,
SND_CTL_LED in sound/core/Kconfig has no prompt, so AMD ACP ASoC-only
configurations without CONFIG_SND_HDA_GENERIC_LEDS cannot enable it at
all unless SND_SOC_AMD_ACP_PCM selects it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204200115.10636-1-bahadirhamza2@gmail.com?part=1

      reply	other threads:[~2026-12-04 20:23 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:01 [PATCH] ASoC: amd: acp: add DMIC capture switch with mic mute LED support Bahadır Hamza Öztürk
2026-12-04 20:23 ` 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-250617@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bahadirhamza2@gmail.com \
    --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