All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Harendra Gautam" <harendra.gautam@oss.qualcomm.com>
Cc: robh@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v5 07/10] ASoC: qcom: Add QAIF PCM operations
Date: Mon, 07 Sep 2026 12:38:36 +0000	[thread overview]
Message-ID: <20260907123837.40E4B1F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260907-b4-qaif-shikra-audio-v5-7-6d8ad9ba5c9f@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Dangling pointers left in persistent substream->dma_buffer on close and error paths, leading to a Double-Free memory corruption.
- [Medium] Large order-5 contiguous DMA buffer allocation in open() callback is highly prone to failure due to memory fragmentation.
--

commit e38dec114f51cf2a17be9cb465b247a4bd24cc25
Author: Harendra Gautam <harendra.gautam@oss.qualcomm.com>

ASoC: qcom: Add QAIF PCM operations

This commit adds ALSA SoC PCM platform driver callbacks for Qualcomm Audio 
Interface (QAIF) devices. It provides open, close, hw_params, prepare, and 
pointer operations, and handles DMA buffer allocation and mmap/copy support 
for playback and capture streams.

> diff --git a/sound/soc/qcom/qaif-platform.c b/sound/soc/qcom/qaif-platform.c
> new file mode 100644
> index 0000000000000..a64bbd7f6fe56
> --- /dev/null
> +++ b/sound/soc/qcom/qaif-platform.c
[ ... ]
> +static int qaif_platform_pcmops_open(struct snd_soc_component *component,
> +				     struct snd_pcm_substream *substream)
> +{
> +	struct snd_pcm_runtime *runtime = substream->runtime;
> +	struct snd_soc_pcm_runtime *soc_runtime = snd_soc_substream_to_rtd(substream);
> +	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_runtime, 0);
> +	struct qaif_drv_data *drvdata = snd_soc_component_get_drvdata(component);
> +	const struct qaif_variant *v = drvdata->variant;
> +	unsigned int dai_id = cpu_dai->driver->id;
> +	struct snd_dma_buffer *buf = &substream->dma_buffer;
> +	struct qaif_dma_mem_info *dma_mem_info;
> +	struct qaif_pcm_data *data;
> +	int ret, stream_dma_idx, dma_reg_idx, dir = substream->stream;
[ ... ]
> +	dma_mem_info = kzalloc(sizeof(*dma_mem_info), GFP_KERNEL);
> +	if (!dma_mem_info) {
> +		ret = -ENOMEM;
> +		goto err_free_data;
> +	}
> +	dma_mem_info->alloc_size = qaif_platform_hardware.buffer_bytes_max;
> +	dma_mem_info->vaddr = dma_alloc_coherent(component->dev,
> +						 dma_mem_info->alloc_size,
> +						 &dma_mem_info->dma_addr,
> +						 GFP_KERNEL);

[Severity: Medium]
Is it safe to perform a large contiguous allocation dynamically during the 
stream open callback? 

On a system that has been running for some time, memory fragmentation might 
cause this order-5 (120 KB) dma_alloc_coherent() call to fail with -ENOMEM, 
which would prevent the stream from opening. ALSA patterns generally avoid 
this by pre-allocating DMA buffers during driver probe via 
snd_pcm_set_managed_buffer_all().

> +	if (!dma_mem_info->vaddr) {
> +		ret = -ENOMEM;
> +		goto err_free_mem_info;
> +	}
[ ... ]
> +	buf->bytes = qaif_platform_hardware.buffer_bytes_max;
> +	buf->addr = dma_mem_info->dma_addr;
> +	buf->area = (unsigned char *)dma_mem_info->vaddr;
[ ... ]
> +	snd_pcm_set_runtime_buffer(substream, buf);
[ ... ]
> +static int qaif_platform_pcmops_close(struct snd_soc_component *component,
> +				      struct snd_pcm_substream *substream)
> +{
> +	struct snd_soc_pcm_runtime *soc_runtime = snd_soc_substream_to_rtd(substream);
> +	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(soc_runtime, 0);
> +	struct qaif_drv_data *drvdata = snd_soc_component_get_drvdata(component);
> +	const struct qaif_variant *v = drvdata->variant;
> +	struct qaif_pcm_data *data = substream->runtime->private_data;
> +	struct qaif_dma_mem_info *dma_mem_info;
> +	unsigned int dai_id = cpu_dai->driver->id;
[ ... ]
> +	if (dma_mem_info) {
> +		dma_free_coherent(component->dev, dma_mem_info->alloc_size,
> +				  dma_mem_info->vaddr, dma_mem_info->dma_addr);
> +		kfree(dma_mem_info);
> +	}
> +
> +	snd_pcm_set_runtime_buffer(substream, NULL);

[Severity: High]
Does this leave a dangling pointer in the persistent substream->dma_buffer 
structure that could result in a double-free memory corruption?

The dynamically allocated memory is assigned to the persistent 
substream->dma_buffer (via the local buf pointer) during 
qaif_platform_pcmops_open().

While snd_pcm_set_runtime_buffer(substream, NULL) correctly clears the 
pointers in the runtime structure, it leaves substream->dma_buffer.area 
pointing to the memory that was just freed here by dma_free_coherent(). 

When the ALSA PCM device is later unregistered (e.g. during driver unbind 
or module unload), the ALSA core calls snd_pcm_lib_preallocate_free_for_all(), 
which sees that dma_buffer.area != NULL and attempts to free it again using 
snd_dma_free_pages(). 

Should the persistent buffer pointers be explicitly cleared here to avoid 
the double-free?

> +	substream->runtime->private_data = NULL;
> +
> +	kfree(data);
> +	return 0;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260907-b4-qaif-shikra-audio-v5-0-6d8ad9ba5c9f@oss.qualcomm.com?part=7

  reply	other threads:[~2026-09-07 12:38 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-07 12:09 [PATCH v5 00/10] ASoC: qcom: Add QAIF driver for Shikra audio platform Harendra Gautam
2026-09-07 12:09 ` [PATCH v5 01/10] ASoC: dt-bindings: qcom,shikra-qaif-cpu: Add binding Harendra Gautam
2026-09-07 12:20   ` sashiko-bot
2026-09-08 19:49   ` Mark Brown
2026-09-09  6:26     ` Harendra Gautam
2026-09-07 12:09 ` [PATCH v5 02/10] ASoC: qcom: Add QAIF shared data structures and variant interface Harendra Gautam
2026-09-07 12:09 ` [PATCH v5 03/10] ASoC: qcom: Add QAIF hardware register map Harendra Gautam
2026-09-07 12:23   ` sashiko-bot
2026-09-07 12:09 ` [PATCH v5 04/10] ASoC: qcom: Add QAIF CPU DAI ops, regmap, DT parsing and platform init Harendra Gautam
2026-09-07 12:25   ` sashiko-bot
2026-09-07 12:09 ` [PATCH v5 05/10] ASoC: soc-core: Add snd_soc_of_xlate_dai_name() generic helper Harendra Gautam
2026-09-07 12:09 ` [PATCH v5 06/10] ASoC: qcom: Switch lpass-cpu and qaif-cpu to snd_soc_of_xlate_dai_name() Harendra Gautam
2026-09-07 12:09 ` [PATCH v5 07/10] ASoC: qcom: Add QAIF PCM operations Harendra Gautam
2026-09-07 12:38   ` sashiko-bot [this message]
2026-09-07 12:09 ` [PATCH v5 08/10] ASoC: qcom: Add QAIF IRQ handling, suspend/resume and platform register Harendra Gautam
2026-09-07 12:36   ` sashiko-bot
2026-09-07 12:09 ` [PATCH v5 09/10] ASoC: qcom: Add Shikra QAIF support Harendra Gautam
2026-09-07 12:42   ` sashiko-bot
2026-09-07 12:09 ` [PATCH v5 10/10] MAINTAINERS: Add Qualcomm QAIF driver entry Harendra Gautam
2026-09-08 19:54 ` [PATCH v5 00/10] ASoC: qcom: Add QAIF driver for Shikra audio platform Mark Brown
2026-09-09  6:33   ` Harendra Gautam

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=20260907123837.40E4B1F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=harendra.gautam@oss.qualcomm.com \
    --cc=robh@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.