Skip to contentSkip to conversation

ASoC: soc-dapm.c: random cleanup

PATCH In review Kuninori Morimoto 2 participants 9 messages Sep 5, 2022 – Sep 7, 2022

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

cover ASoC: soc-dapm.c: random cleanup

Hi Mark

These are cleanup patches for soc-dapm.c. Each patches are not related, very random cleanup.

Kuninori Morimoto (4):
ASoC: soc-dapm.c: don't use kzalloc() for param on snd_soc_dai_link_event_pre_pmu()
ASoC: soc-dapm.c: don't use WARN_ON() at snd_soc_dai_link_event_pre_pmu()
ASoC: soc-dapm.c: tidyup snd_soc_dai_link_event_pre_pmu()
ASoC: soc-dapm.c: fixup snd_soc_dapm_new_control_unlocked() error handling

 sound/soc/soc-dapm.c | 77 +++++++++++++++++++-------------------------
 1 file changed, 33 insertions(+), 44 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 ASoC: soc-dapm.c: don't use kzalloc() for param on snd_soc_dai_link_event_pre_pmu()

From: Kuninori Morimoto <[email protected]>

Current snd_soc_dai_link_event_pre_pmu() is using kzalloc() / kfree() for "params", but it is fixed size, and not used on private data. It is used for setup rtd at end of this function, just a local variable. We don't need to use kzalloc() / kfree() for it.
This patch replace it as local variable.

---
 sound/soc/soc-dapm.c | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)
sound/soc/soc-dapm.c +15 -23
index 73b8bd452ca7..7d4e4068f870 100644
@@ -3837,15 +3837,13 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
 	struct snd_soc_dapm_path *path;
 	struct snd_soc_dai *source, *sink;
 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
-	struct snd_pcm_hw_params *params = NULL;
+	struct snd_pcm_hw_params params;
 	const struct snd_soc_pcm_stream *config = NULL;
 	struct snd_pcm_runtime *runtime = NULL;
 	unsigned int fmt;
 	int ret = 0;
 
-	params = kzalloc(sizeof(*params), GFP_KERNEL);
-	if (!params)
-		return -ENOMEM;
+	memset(&params, 0, sizeof(params));
 
 	runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
 	if (!runtime) {
@@ -3902,45 +3900,39 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
 		goto out;
 	}
 
-	snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
-	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
-		config->rate_min;
-	hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
-		config->rate_max;
-	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
-		= config->channels_min;
-	hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
-		= config->channels_max;
+	snd_mask_set(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
+	hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min	= config->rate_min;
+	hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max	= config->rate_max;
+	hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->min	= config->channels_min;
+	hw_param_interval(&params, SNDRV_PCM_HW_PARAM_CHANNELS)->max	= config->channels_max;
 
 	substream->stream = SNDRV_PCM_STREAM_CAPTURE;
 	snd_soc_dapm_widget_for_each_source_path(w, path) {
 		source = path->source->priv;
 
-		ret = snd_soc_dai_hw_params(source, substream, params);
+		ret = snd_soc_dai_hw_params(source, substream, &params);
 		if (ret < 0)
 			goto out;
 
-		dapm_update_dai_unlocked(substream, params, source);
+		dapm_update_dai_unlocked(substream, &params, source);
 	}
 
 	substream->stream = SNDRV_PCM_STREAM_PLAYBACK;
 	snd_soc_dapm_widget_for_each_sink_path(w, path) {
 		sink = path->sink->priv;
 
-		ret = snd_soc_dai_hw_params(sink, substream, params);
+		ret = snd_soc_dai_hw_params(sink, substream, &params);
 		if (ret < 0)
 			goto out;
 
-		dapm_update_dai_unlocked(substream, params, sink);
+		dapm_update_dai_unlocked(substream, &params, sink);
 	}
 
-	runtime->format = params_format(params);
-	runtime->subformat = params_subformat(params);
-	runtime->channels = params_channels(params);
-	runtime->rate = params_rate(params);
-
+	runtime->format		= params_format(&params);
+	runtime->subformat	= params_subformat(&params);
+	runtime->channels	= params_channels(&params);
+	runtime->rate		= params_rate(&params);
 out:
-	kfree(params);
 	return ret;
 }
 
Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH 1/4] ASoC: soc-dapm.c: don't use kzalloc() for param on snd_soc_dai_link_event_pre_pmu()

Quoted 7 lines · Kuninori Morimoto

From: Kuninori Morimoto <[email protected]>

Current snd_soc_dai_link_event_pre_pmu() is using kzalloc() / kfree() for "params", but it is fixed size, and not used on private data. It is used for setup rtd at end of this function, just a local variable. We don't need to use kzalloc() / kfree() for it.
This patch replace it as local variable.

The reason we're allocating it dynamically is that it's quite large (608 bytes on arm64) and is starting to get a bit excessive for allocation on the stack, especially when you're building with some of the KASAN type stuff that increases stack usage.

#3 lore raw

Re: [PATCH 1/4] ASoC: soc-dapm.c: don't use kzalloc() for param on snd_soc_dai_link_event_pre_pmu()

Hi Mark

Quoted 10 lines
Quoted 5 lines

Current snd_soc_dai_link_event_pre_pmu() is using kzalloc() / kfree() for "params", but it is fixed size, and not used on private data. It is used for setup rtd at end of this function, just a local variable. We don't need to use kzalloc() / kfree() for it.
This patch replace it as local variable.

The reason we're allocating it dynamically is that it's quite large (608 bytes on arm64) and is starting to get a bit excessive for allocation on the stack, especially when you're building with some of the KASAN type stuff that increases stack usage.

Oh, I see.
Thank you for clearing the reason.
I think it is very nice to have such comment on the code.

Thank you for your help !!

Best regards
---
Kuninori Morimoto

#4 lore raw

PATCH 2/4 ASoC: soc-dapm.c: don't use WARN_ON() at snd_soc_dai_link_event_pre_pmu()

In reply to Kuninori Morimoto

From: Kuninori Morimoto <[email protected]>

Current snd_soc_dai_link_event_pre_pmu() is checking "config". It is using dev_err() (A) if it was NULL, so we don't need to use WARN_ON() (B) to check it, it is over-kill. This patch removes it.

(B) if (WARN_ON(!config)) {

(A)		dev_err(...);
		...
	}
---
 sound/soc/soc-dapm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/soc-dapm.c +1 -1
index 7d4e4068f870..bc7d64b570b4 100644
@@ -3883,7 +3883,7 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
 	 * necessary
 	 */
 	config = rtd->dai_link->params + rtd->params_select;
-	if (WARN_ON(!config)) {
+	if (!config) {
 		dev_err(w->dapm->dev, "ASoC: link config missing\n");
 		ret = -EINVAL;
 		goto out;
Signature · 1 line
2.25.1
#5 lore raw

PATCH 3/4 ASoC: soc-dapm.c: tidyup snd_soc_dai_link_event_pre_pmu()

In reply to Kuninori Morimoto

From: Kuninori Morimoto <[email protected]>

snd_soc_dai_link_event_pre_pmu() is using if/else for config->formats check, but "else" case is for just error.
Unnecessary if/else is not good for readable code. this patch checks if config->formats was zero and call "goto out" in such case.

[not readable]

	if (config->formats) {
(A)		fmt = ...
	} else {
		...
(B)		goto err;
	}

[readable]

	if (!config->formats) {
		...
(B)		goto err;
	}

(A) fmt = ...

Moreover, we don't need to indicate config->formats value in error message, because it is zero.

=>	if (config->formats) {
		...
	} else {
		dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
=>			 config->formats);
		...
	}
---
 sound/soc/soc-dapm.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
sound/soc/soc-dapm.c +4 -6
index bc7d64b570b4..e8c813586b53 100644
@@ -3890,16 +3890,14 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
 	}
 
 	/* Be a little careful as we don't want to overflow the mask array */
-	if (config->formats) {
-		fmt = ffs(config->formats) - 1;
-	} else {
-		dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
-			 config->formats);
-
+	if (!config->formats) {
+		dev_warn(w->dapm->dev, "ASoC: Invalid format was specified\n");
 		ret = -EINVAL;
 		goto out;
 	}
 
+	fmt = ffs(config->formats) - 1;
+
 	snd_mask_set(hw_param_mask(&params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
 	hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->min	= config->rate_min;
 	hw_param_interval(&params, SNDRV_PCM_HW_PARAM_RATE)->max	= config->rate_max;
Signature · 1 line
2.25.1
maintainer
#6 lore raw

Re: [PATCH 3/4] ASoC: soc-dapm.c: tidyup snd_soc_dai_link_event_pre_pmu()

Quoted 6 lines · Kuninori Morimoto

From: Kuninori Morimoto <[email protected]>

snd_soc_dai_link_event_pre_pmu() is using if/else for config->formats check, but "else" case is for just error.
Unnecessary if/else is not good for readable code. this patch checks if config->formats was zero and call "goto out" in such case.

This isn't applying for me, I suspect it depends on patch 1 though I didn't check properly.

#7 lore raw

PATCH 4/4 ASoC: soc-dapm.c: fixup snd_soc_dapm_new_control_unlocked() error handling

In reply to Kuninori Morimoto

From: Kuninori Morimoto <[email protected]>

Current snd_soc_dapm_new_control_unlocked() error handling is wrong. It is using "goto request_failed" (A), but error message is using "w->name" (B) which is not yet created in such timing.

	snd_soc_dapm_new_control_unlocked(xxx)
	{
		...
		switch (w->id) {
		case xxx:
			...
			if (IS_ERR(...)) {
				ret = PTR_ERR(...);
(A)				goto request_failed;
			}
		...
		}

		prefix = soc_dapm_prefix(...);
		if (prefix)
(B)			w->name = kasprintf(...);
		else
(B)			w->name = kstrdup_const(...);
		...

(A) request_failed:

		if (ret != -EPROBE_DEFER)

(B) dev_err(..., w->name, ...);

		return ...;
	}

we can create "w->name" at beginning of this function. In such case, we need to call kfree_const(w->name) at error case. This patch do these.

---
 sound/soc/soc-dapm.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
sound/soc/soc-dapm.c +13 -14
index e8c813586b53..928e3bfe7457 100644
@@ -3630,10 +3630,18 @@ snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
 	enum snd_soc_dapm_direction dir;
 	struct snd_soc_dapm_widget *w;
 	const char *prefix;
-	int ret;
+	int ret = -ENOMEM;
 
 	if ((w = dapm_cnew_widget(widget)) == NULL)
-		return ERR_PTR(-ENOMEM);
+		goto cnew_failed;
+
+	prefix = soc_dapm_prefix(dapm);
+	if (prefix)
+		w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
+	else
+		w->name = kstrdup_const(widget->name, GFP_KERNEL);
+	if (!w->name)
+		goto name_failed;
 
 	switch (w->id) {
 	case snd_soc_dapm_regulator_supply:
@@ -3672,17 +3680,6 @@ snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
 		break;
 	}
 
-	prefix = soc_dapm_prefix(dapm);
-	if (prefix)
-		w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
-	else
-		w->name = kstrdup_const(widget->name, GFP_KERNEL);
-	if (w->name == NULL) {
-		kfree_const(w->sname);
-		kfree(w);
-		return ERR_PTR(-ENOMEM);
-	}
-
 	switch (w->id) {
 	case snd_soc_dapm_mic:
 		w->is_ep = SND_SOC_DAPM_EP_SOURCE;
@@ -3770,9 +3767,11 @@ snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
 	if (ret != -EPROBE_DEFER)
 		dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
 			w->name, ret);
-
+	kfree_const(w->name);
+name_failed:
 	kfree_const(w->sname);
 	kfree(w);
+cnew_failed:
 	return ERR_PTR(ret);
 }
 
Signature · 1 line
2.25.1
maintainer
#8 lore raw

Re: (subset) [PATCH 0/4] ASoC: soc-dapm.c: random cleanup

In reply to Kuninori Morimoto
Quoted 10 lines · Kuninori Morimoto

These are cleanup patches for soc-dapm.c. Each patches are not related, very random cleanup.

Kuninori Morimoto (4):
ASoC: soc-dapm.c: don't use kzalloc() for param on snd_soc_dai_link_event_pre_pmu()
ASoC: soc-dapm.c: don't use WARN_ON() at snd_soc_dai_link_event_pre_pmu()
ASoC: soc-dapm.c: tidyup snd_soc_dai_link_event_pre_pmu()
ASoC: soc-dapm.c: fixup snd_soc_dapm_new_control_unlocked() error handling

[...]

Applied to

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

Thanks!

[2/4] ASoC: soc-dapm.c: don't use WARN_ON() at snd_soc_dai_link_event_pre_pmu()

      commit: 427de091a7112aa8eaf2f689e95c0dbca5ea6543
[3/4] ASoC: soc-dapm.c: tidyup snd_soc_dai_link_event_pre_pmu()
      (no commit info)
[4/4] ASoC: soc-dapm.c: fixup snd_soc_dapm_new_control_unlocked() error handling
      commit: 3caac759681eeb31ac80e3cc14b972680c8bde54

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