[PATCH 00/11] ASoC: Simplify code with cleanup.h
Allocate the memory with scoped/cleanup.h to reduce error handling (simpler error paths) and make the code a bit smaller.
Best regards, Krzysztof
--- Krzysztof Kozlowski (11): ASoC: codecs: audio-iio-aux: Simplify audio_iio_aux_add_dapms() with cleanup.h ASoC: codecs: audio-iio-aux: Simplify audio_iio_aux_probe() with cleanup.h ASoC: codecs: wcd9335: Simplify with cleanup.h ASoC: codecs: wcd934x: Simplify with cleanup.h ASoC: simple-card-utils: Simplify with cleanup.h ASoC: audio-graph-card: Use cleanup.h instead of devm_kfree() ASoC: audio-graph-card2: Use cleanup.h instead of devm_kfree() ASoC: simple-card: Use cleanup.h instead of devm_kfree() ASoC: ops: Simplify with cleanup.h ASoC: dapm: Simplify dapm_cnew_widget() with cleanup.h ASoC: dapm: Simplify snd_soc_dai_link_event_pre_pmu() with cleanup.h
sound/soc/codecs/audio-iio-aux.c | 83 ++++++++++++----------------------- sound/soc/codecs/wcd9335.c | 22 ++++------ sound/soc/codecs/wcd934x.c | 22 ++++------ sound/soc/generic/audio-graph-card.c | 5 +-- sound/soc/generic/audio-graph-card2.c | 5 +-- sound/soc/generic/simple-card-utils.c | 13 +++--- sound/soc/generic/simple-card.c | 5 +-- sound/soc/soc-dapm.c | 47 ++++++++------------ sound/soc/soc-ops.c | 26 +++++------ 9 files changed, 87 insertions(+), 141 deletions(-) --- base-commit: cf601827c51b0496f6bb8a1865f1bf69ebd246b7 change-id: 20240703-asoc-cleanup-h-ef9cbfb67b66
Best regards,
Allocate the memory with scoped/cleanup.h in audio_iio_aux_add_dapms() to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/codecs/audio-iio-aux.c | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-)
diff --git a/sound/soc/codecs/audio-iio-aux.c b/sound/soc/codecs/audio-iio-aux.c index 1e8e1effc2af..3969ee45f41e 100644 --- a/sound/soc/codecs/audio-iio-aux.c +++ b/sound/soc/codecs/audio-iio-aux.c @@ -6,6 +6,7 @@ // // Author: Herve Codina herve.codina@bootlin.com
+#include <linux/cleanup.h> #include <linux/iio/consumer.h> #include <linux/minmax.h> #include <linux/mod_devicetable.h> @@ -131,33 +132,27 @@ static int audio_iio_aux_add_dapms(struct snd_soc_component *component, struct audio_iio_aux_chan *chan) { struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); - char *output_name; - char *input_name; - char *pga_name; int ret;
- input_name = kasprintf(GFP_KERNEL, "%s IN", chan->name); + /* Allocated names are not needed afterwards (duplicated in ASoC internals) */ + char *input_name __free(kfree) = kasprintf(GFP_KERNEL, "%s IN", chan->name); if (!input_name) return -ENOMEM;
- output_name = kasprintf(GFP_KERNEL, "%s OUT", chan->name); - if (!output_name) { - ret = -ENOMEM; - goto out_free_input_name; - } + char *output_name __free(kfree) = kasprintf(GFP_KERNEL, "%s OUT", chan->name); + if (!output_name) + return -ENOMEM;
- pga_name = kasprintf(GFP_KERNEL, "%s PGA", chan->name); - if (!pga_name) { - ret = -ENOMEM; - goto out_free_output_name; - } + char *pga_name __free(kfree) = kasprintf(GFP_KERNEL, "%s PGA", chan->name); + if (!pga_name) + return -ENOMEM;
widgets[0] = SND_SOC_DAPM_INPUT(input_name); widgets[1] = SND_SOC_DAPM_OUTPUT(output_name); widgets[2] = SND_SOC_DAPM_PGA(pga_name, SND_SOC_NOPM, 0, 0, NULL, 0); ret = snd_soc_dapm_new_controls(dapm, widgets, 3); if (ret) - goto out_free_pga_name; + return ret;
routes[0].sink = pga_name; routes[0].control = NULL; @@ -165,17 +160,8 @@ static int audio_iio_aux_add_dapms(struct snd_soc_component *component, routes[1].sink = output_name; routes[1].control = NULL; routes[1].source = pga_name; - ret = snd_soc_dapm_add_routes(dapm, routes, 2);
- /* Allocated names are no more needed (duplicated in ASoC internals) */ - -out_free_pga_name: - kfree(pga_name); -out_free_output_name: - kfree(output_name); -out_free_input_name: - kfree(input_name); - return ret; + return snd_soc_dapm_add_routes(dapm, routes, 2); }
static int audio_iio_aux_component_probe(struct snd_soc_component *component)
Allocate the memory with scoped/cleanup.h in audio_iio_aux_probe() to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/codecs/audio-iio-aux.c | 47 +++++++++++++++------------------------- 1 file changed, 18 insertions(+), 29 deletions(-)
diff --git a/sound/soc/codecs/audio-iio-aux.c b/sound/soc/codecs/audio-iio-aux.c index 3969ee45f41e..588e48044c13 100644 --- a/sound/soc/codecs/audio-iio-aux.c +++ b/sound/soc/codecs/audio-iio-aux.c @@ -230,8 +230,6 @@ static int audio_iio_aux_probe(struct platform_device *pdev) struct audio_iio_aux_chan *iio_aux_chan; struct device *dev = &pdev->dev; struct audio_iio_aux *iio_aux; - const char **names; - u32 *invert_ranges; int count; int ret; int i; @@ -248,22 +246,22 @@ static int audio_iio_aux_probe(struct platform_device *pdev)
iio_aux->num_chans = count;
- names = kcalloc(iio_aux->num_chans, sizeof(*names), GFP_KERNEL); + const char **names __free(kfree) = kcalloc(iio_aux->num_chans, + sizeof(*names), + GFP_KERNEL); if (!names) return -ENOMEM;
- invert_ranges = kcalloc(iio_aux->num_chans, sizeof(*invert_ranges), GFP_KERNEL); - if (!invert_ranges) { - ret = -ENOMEM; - goto out_free_names; - } + u32 *invert_ranges __free(kfree) = kcalloc(iio_aux->num_chans, + sizeof(*invert_ranges), + GFP_KERNEL); + if (!invert_ranges) + return -ENOMEM;
ret = device_property_read_string_array(dev, "io-channel-names", names, iio_aux->num_chans); - if (ret < 0) { - dev_err_probe(dev, ret, "failed to read io-channel-names\n"); - goto out_free_invert_ranges; - } + if (ret < 0) + return dev_err_probe(dev, ret, "failed to read io-channel-names\n");
/* * snd-control-invert-range is optional and can contain fewer items @@ -274,10 +272,8 @@ static int audio_iio_aux_probe(struct platform_device *pdev) count = min_t(unsigned int, count, iio_aux->num_chans); ret = device_property_read_u32_array(dev, "snd-control-invert-range", invert_ranges, count); - if (ret < 0) { - dev_err_probe(dev, ret, "failed to read snd-control-invert-range\n"); - goto out_free_invert_ranges; - } + if (ret < 0) + return dev_err_probe(dev, ret, "failed to read snd-control-invert-range\n"); }
for (i = 0; i < iio_aux->num_chans; i++) { @@ -286,23 +282,16 @@ static int audio_iio_aux_probe(struct platform_device *pdev) iio_aux_chan->is_invert_range = invert_ranges[i];
iio_aux_chan->iio_chan = devm_iio_channel_get(dev, iio_aux_chan->name); - if (IS_ERR(iio_aux_chan->iio_chan)) { - ret = PTR_ERR(iio_aux_chan->iio_chan); - dev_err_probe(dev, ret, "get IIO channel '%s' failed\n", - iio_aux_chan->name); - goto out_free_invert_ranges; - } + if (IS_ERR(iio_aux_chan->iio_chan)) + return dev_err_probe(dev, PTR_ERR(iio_aux_chan->iio_chan), + "get IIO channel '%s' failed\n", + iio_aux_chan->name); }
platform_set_drvdata(pdev, iio_aux);
- ret = devm_snd_soc_register_component(dev, &audio_iio_aux_component_driver, - NULL, 0); -out_free_invert_ranges: - kfree(invert_ranges); -out_free_names: - kfree(names); - return ret; + return devm_snd_soc_register_component(dev, &audio_iio_aux_component_driver, + NULL, 0); }
static const struct of_device_id audio_iio_aux_ids[] = {
Allocate the memory with scoped/cleanup.h to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/codecs/wcd9335.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
diff --git a/sound/soc/codecs/wcd9335.c b/sound/soc/codecs/wcd9335.c index 1a20131e2a60..373a31ddccb2 100644 --- a/sound/soc/codecs/wcd9335.c +++ b/sound/soc/codecs/wcd9335.c @@ -5,6 +5,7 @@ #include <linux/module.h> #include <linux/init.h> #include <linux/platform_device.h> +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/wait.h> #include <linux/bitops.h> @@ -2714,25 +2715,23 @@ static int wcd9335_codec_enable_dec(struct snd_soc_dapm_widget *w, struct snd_soc_component *comp = snd_soc_dapm_to_component(w->dapm); unsigned int decimator; char *dec_adc_mux_name = NULL; - char *widget_name = NULL; - char *wname; + char *widget_name; int ret = 0, amic_n; u16 tx_vol_ctl_reg, pwr_level_reg = 0, dec_cfg_reg, hpf_gate_reg; u16 tx_gain_ctl_reg; char *dec; u8 hpf_coff_freq;
- widget_name = kmemdup_nul(w->name, 15, GFP_KERNEL); - if (!widget_name) + char *wname __free(kfree) = kmemdup_nul(w->name, 15, GFP_KERNEL); + if (!wname) return -ENOMEM;
- wname = widget_name; + widget_name = wname; dec_adc_mux_name = strsep(&widget_name, " "); if (!dec_adc_mux_name) { dev_err(comp->dev, "%s: Invalid decimator = %s\n", __func__, w->name); - ret = -EINVAL; - goto out; + return -EINVAL; } dec_adc_mux_name = widget_name;
@@ -2740,16 +2739,14 @@ static int wcd9335_codec_enable_dec(struct snd_soc_dapm_widget *w, if (!dec) { dev_err(comp->dev, "%s: decimator index not found\n", __func__); - ret = -EINVAL; - goto out; + return -EINVAL; }
ret = kstrtouint(dec, 10, &decimator); if (ret < 0) { dev_err(comp->dev, "%s: Invalid decimator = %s\n", __func__, wname); - ret = -EINVAL; - goto out; + return -EINVAL; }
tx_vol_ctl_reg = WCD9335_CDC_TX0_TX_PATH_CTL + 16 * decimator; @@ -2836,8 +2833,7 @@ static int wcd9335_codec_enable_dec(struct snd_soc_dapm_widget *w, snd_soc_component_update_bits(comp, tx_vol_ctl_reg, 0x10, 0x00); break; } -out: - kfree(wname); + return ret; }
Allocate the memory with scoped/cleanup.h to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/codecs/wcd934x.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
diff --git a/sound/soc/codecs/wcd934x.c b/sound/soc/codecs/wcd934x.c index 6c65b46e2dc9..291d0c80a6fc 100644 --- a/sound/soc/codecs/wcd934x.c +++ b/sound/soc/codecs/wcd934x.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2019, Linaro Limited
+#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/clk-provider.h> #include <linux/interrupt.h> @@ -4973,25 +4974,23 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w, struct snd_soc_component *comp = snd_soc_dapm_to_component(w->dapm); unsigned int decimator; char *dec_adc_mux_name = NULL; - char *widget_name = NULL; - char *wname; + char *widget_name; int ret = 0, amic_n; u16 tx_vol_ctl_reg, pwr_level_reg = 0, dec_cfg_reg, hpf_gate_reg; u16 tx_gain_ctl_reg; char *dec; u8 hpf_coff_freq;
- widget_name = kstrndup(w->name, 15, GFP_KERNEL); - if (!widget_name) + char *wname __free(kfree) = kstrndup(w->name, 15, GFP_KERNEL); + if (!wname) return -ENOMEM;
- wname = widget_name; + widget_name = wname; dec_adc_mux_name = strsep(&widget_name, " "); if (!dec_adc_mux_name) { dev_err(comp->dev, "%s: Invalid decimator = %s\n", __func__, w->name); - ret = -EINVAL; - goto out; + return -EINVAL; } dec_adc_mux_name = widget_name;
@@ -4999,16 +4998,14 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w, if (!dec) { dev_err(comp->dev, "%s: decimator index not found\n", __func__); - ret = -EINVAL; - goto out; + return -EINVAL; }
ret = kstrtouint(dec, 10, &decimator); if (ret < 0) { dev_err(comp->dev, "%s: Invalid decimator = %s\n", __func__, wname); - ret = -EINVAL; - goto out; + return -EINVAL; }
tx_vol_ctl_reg = WCD934X_CDC_TX0_TX_PATH_CTL + 16 * decimator; @@ -5101,8 +5098,7 @@ static int wcd934x_codec_enable_dec(struct snd_soc_dapm_widget *w, WCD934X_DEC_PWR_LVL_DF); break; } -out: - kfree(wname); + return ret; }
Allocate the memory with scoped/cleanup.h to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/generic/simple-card-utils.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/sound/soc/generic/simple-card-utils.c b/sound/soc/generic/simple-card-utils.c index a18de86b3c88..fedae7f6f70c 100644 --- a/sound/soc/generic/simple-card-utils.c +++ b/sound/soc/generic/simple-card-utils.c @@ -5,6 +5,7 @@ // Copyright (c) 2016 Kuninori Morimoto kuninori.morimoto.gx@renesas.com
#include <dt-bindings/sound/audio-graph.h> +#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/gpio/consumer.h> #include <linux/module.h> @@ -135,8 +136,8 @@ EXPORT_SYMBOL_GPL(simple_util_parse_daifmt); int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np, struct simple_util_dai *dai) { - u32 *array_values, *p; int n, i, ret; + u32 *p;
if (!of_property_read_bool(np, "dai-tdm-slot-width-map")) return 0; @@ -151,14 +152,15 @@ int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np, if (!dai->tdm_width_map) return -ENOMEM;
- array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL); + u32 *array_values __free(kfree) = kcalloc(n, sizeof(*array_values), + GFP_KERNEL); if (!array_values) return -ENOMEM;
ret = of_property_read_u32_array(np, "dai-tdm-slot-width-map", array_values, n); if (ret < 0) { dev_err(dev, "Could not read dai-tdm-slot-width-map: %d\n", ret); - goto out; + return ret; }
p = array_values; @@ -169,11 +171,8 @@ int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np, }
dai->n_tdm_widths = i; - ret = 0; -out: - kfree(array_values);
- return ret; + return 0; } EXPORT_SYMBOL_GPL(simple_util_parse_tdm_width_map);
Hi Krzysztof
Allocate the memory with scoped/cleanup.h to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org
(snip)
@@ -151,14 +152,15 @@ int simple_util_parse_tdm_width_map(struct device *dev, struct device_node *np, if (!dai->tdm_width_map) return -ENOMEM;
- array_values = kcalloc(n, sizeof(*array_values), GFP_KERNEL);
- u32 *array_values __free(kfree) = kcalloc(n, sizeof(*array_values),
GFP_KERNEL);
We can use 100 char, it can be 1 line.
Acked-by: Kuninori Morimoto kuninori.morimoto.gx@renesas.com
Thank you for your help !!
Best regards --- Kuninori Morimoto
Allocate the memory with scoped/cleanup.h, instead of devm interface, to make the code more obvious that memory is not used outside this scope.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/generic/audio-graph-card.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/sound/soc/generic/audio-graph-card.c b/sound/soc/generic/audio-graph-card.c index acf7d92d21e6..3425fbbcbd7e 100644 --- a/sound/soc/generic/audio-graph-card.c +++ b/sound/soc/generic/audio-graph-card.c @@ -7,6 +7,7 @@ // // based on ${LINUX}/sound/soc/generic/simple-card.c
+#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/device.h> #include <linux/gpio/consumer.h> @@ -573,10 +574,9 @@ static int graph_get_dais_count(struct simple_util_priv *priv, int audio_graph_parse_of(struct simple_util_priv *priv, struct device *dev) { struct snd_soc_card *card = simple_priv_to_card(priv); - struct link_info *li; int ret;
- li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL); + struct link_info *li __free(kfree) = kzalloc(sizeof(*li), GFP_KERNEL); if (!li) return -ENOMEM;
@@ -628,7 +628,6 @@ int audio_graph_parse_of(struct simple_util_priv *priv, struct device *dev) if (ret < 0) goto err;
- devm_kfree(dev, li); return 0;
err:
Hi
Allocate the memory with scoped/cleanup.h, instead of devm interface, to make the code more obvious that memory is not used outside this scope.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org
Acked-by: Kuninori Morimoto kuninori.morimoto.gx@renesas.com
Thank you for your help !!
Best regards --- Kuninori Morimoto
Allocate the memory with scoped/cleanup.h, instead of devm interface, to make the code more obvious that memory is not used outside this scope.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/generic/audio-graph-card2.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/sound/soc/generic/audio-graph-card2.c b/sound/soc/generic/audio-graph-card2.c index 93d9d045587b..56f7f946882e 100644 --- a/sound/soc/generic/audio-graph-card2.c +++ b/sound/soc/generic/audio-graph-card2.c @@ -1350,10 +1350,9 @@ int audio_graph2_parse_of(struct simple_util_priv *priv, struct device *dev, struct graph2_custom_hooks *hooks) { struct snd_soc_card *card = simple_priv_to_card(priv); - struct link_info *li; int ret;
- li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL); + struct link_info *li __free(kfree) = kzalloc(sizeof(*li), GFP_KERNEL); if (!li) return -ENOMEM;
@@ -1417,8 +1416,6 @@ int audio_graph2_parse_of(struct simple_util_priv *priv, struct device *dev,
ret = devm_snd_soc_register_card(dev, card); err: - devm_kfree(dev, li); - if (ret < 0) dev_err_probe(dev, ret, "parse error\n");
Hi
Allocate the memory with scoped/cleanup.h, instead of devm interface, to make the code more obvious that memory is not used outside this scope.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org
Acked-by: Kuninori Morimoto kuninori.morimoto.gx@renesas.com
Thank you for your help !!
Best regards --- Kuninori Morimoto
Allocate the memory with scoped/cleanup.h, instead of devm interface, to make the code more obvious that memory is not used outside this scope.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/generic/simple-card.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/sound/soc/generic/simple-card.c b/sound/soc/generic/simple-card.c index edbb6322e9be..d2588f1ea54e 100644 --- a/sound/soc/generic/simple-card.c +++ b/sound/soc/generic/simple-card.c @@ -5,6 +5,7 @@ // Copyright (C) 2012 Renesas Solutions Corp. // Kuninori Morimoto kuninori.morimoto.gx@renesas.com
+#include <linux/cleanup.h> #include <linux/clk.h> #include <linux/device.h> #include <linux/module.h> @@ -727,7 +728,6 @@ static int simple_probe(struct platform_device *pdev) struct device *dev = &pdev->dev; struct device_node *np = dev->of_node; struct snd_soc_card *card; - struct link_info *li; int ret;
/* Allocate the private data and the DAI link array */ @@ -741,7 +741,7 @@ static int simple_probe(struct platform_device *pdev) card->probe = simple_soc_probe; card->driver_name = "simple-card";
- li = devm_kzalloc(dev, sizeof(*li), GFP_KERNEL); + struct link_info *li __free(kfree) = kzalloc(sizeof(*li), GFP_KERNEL); if (!li) return -ENOMEM;
@@ -818,7 +818,6 @@ static int simple_probe(struct platform_device *pdev) if (ret < 0) goto err;
- devm_kfree(dev, li); return 0; err: simple_util_clean_reference(card);
Hi
Allocate the memory with scoped/cleanup.h, instead of devm interface, to make the code more obvious that memory is not used outside this scope.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org
Acked-by: Kuninori Morimoto kuninori.morimoto.gx@renesas.com
Thank you for your help !!
Best regards --- Kuninori Morimoto
Allocate the memory with scoped/cleanup.h to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/soc-ops.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-)
diff --git a/sound/soc/soc-ops.c b/sound/soc/soc-ops.c index b27e89ff6a16..19928f098d8d 100644 --- a/sound/soc/soc-ops.c +++ b/sound/soc/soc-ops.c @@ -11,6 +11,7 @@ // with code, comments and ideas from :- // Richard Purdie richard@openedhand.com
+#include <linux/cleanup.h> #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/init.h> @@ -727,14 +728,14 @@ int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, struct soc_bytes *params = (void *)kcontrol->private_value; int ret, len; unsigned int val, mask; - void *data;
if (!component->regmap || !params->num_regs) return -EINVAL;
len = params->num_regs * component->val_bytes;
- data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA); + void *data __free(kfree) = kmemdup(ucontrol->value.bytes.data, len, + GFP_KERNEL | GFP_DMA); if (!data) return -ENOMEM;
@@ -746,7 +747,7 @@ int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, if (params->mask) { ret = regmap_read(component->regmap, params->base, &val); if (ret != 0) - goto out; + return ret;
val &= params->mask;
@@ -760,14 +761,14 @@ int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, ret = regmap_parse_val(component->regmap, &mask, &mask); if (ret != 0) - goto out; + return ret;
((u16 *)data)[0] &= mask;
ret = regmap_parse_val(component->regmap, &val, &val); if (ret != 0) - goto out; + return ret;
((u16 *)data)[0] |= val; break; @@ -776,30 +777,23 @@ int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, ret = regmap_parse_val(component->regmap, &mask, &mask); if (ret != 0) - goto out; + return ret;
((u32 *)data)[0] &= mask;
ret = regmap_parse_val(component->regmap, &val, &val); if (ret != 0) - goto out; + return ret;
((u32 *)data)[0] |= val; break; default: - ret = -EINVAL; - goto out; + return -EINVAL; } }
- ret = regmap_raw_write(component->regmap, params->base, - data, len); - -out: - kfree(data); - - return ret; + return regmap_raw_write(component->regmap, params->base, data, len); } EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
Allocate the memory with scoped/cleanup.h in dapm_cnew_widget() to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/soc-dapm.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/sound/soc/soc-dapm.c b/sound/soc/soc-dapm.c index 16dad4a45443..236aa3c8eea1 100644 --- a/sound/soc/soc-dapm.c +++ b/sound/soc/soc-dapm.c @@ -20,6 +20,7 @@ #include <linux/module.h> #include <linux/init.h> #include <linux/async.h> +#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/pm.h> #include <linux/bitops.h> @@ -323,9 +324,9 @@ static inline struct snd_soc_dapm_widget *dapm_cnew_widget( const struct snd_soc_dapm_widget *_widget, const char *prefix) { - struct snd_soc_dapm_widget *w; - - w = kmemdup(_widget, sizeof(*_widget), GFP_KERNEL); + struct snd_soc_dapm_widget *w __free(kfree) = kmemdup(_widget, + sizeof(*_widget), + GFP_KERNEL); if (!w) return NULL;
@@ -333,20 +334,18 @@ static inline struct snd_soc_dapm_widget *dapm_cnew_widget( w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, _widget->name); else w->name = kstrdup_const(_widget->name, GFP_KERNEL); - if (!w->name) { - kfree(w); + if (!w->name) return NULL; - }
if (_widget->sname) { w->sname = kstrdup_const(_widget->sname, GFP_KERNEL); if (!w->sname) { kfree_const(w->name); - kfree(w); return NULL; } } - return w; + + return_ptr(w); }
struct dapm_kcontrol_data {
Allocate the memory with scoped/cleanup.h in snd_soc_dai_link_event_pre_pmu() to reduce error handling (less error paths) and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- sound/soc/soc-dapm.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)
diff --git a/sound/soc/soc-dapm.c b/sound/soc/soc-dapm.c index 236aa3c8eea1..fa6f19d26c78 100644 --- a/sound/soc/soc-dapm.c +++ b/sound/soc/soc-dapm.c @@ -3882,11 +3882,10 @@ 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 = snd_soc_substream_to_rtd(substream); - struct snd_pcm_hw_params *params = NULL; const struct snd_soc_pcm_stream *config = NULL; struct snd_pcm_runtime *runtime = NULL; unsigned int fmt; - int ret = 0; + int ret;
/* * NOTE @@ -3897,15 +3896,14 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w, * stuff that increases stack usage. * So, we use kzalloc()/kfree() for params in this function. */ - params = kzalloc(sizeof(*params), GFP_KERNEL); + struct snd_pcm_hw_params *params __free(kfree) = kzalloc(sizeof(*params), + GFP_KERNEL); if (!params) return -ENOMEM;
runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); - if (!runtime) { - ret = -ENOMEM; - goto out; - } + if (!runtime) + return -ENOMEM;
substream->runtime = runtime;
@@ -3915,7 +3913,7 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
ret = snd_soc_dai_startup(source, substream); if (ret < 0) - goto out; + return ret;
snd_soc_dai_activate(source, substream->stream); } @@ -3926,7 +3924,7 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
ret = snd_soc_dai_startup(sink, substream); if (ret < 0) - goto out; + return ret;
snd_soc_dai_activate(sink, substream->stream); } @@ -3941,16 +3939,14 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w, config = rtd->dai_link->c2c_params + rtd->c2c_params_select; if (!config) { dev_err(w->dapm->dev, "ASoC: link config missing\n"); - ret = -EINVAL; - goto out; + return -EINVAL; }
/* Be a little careful as we don't want to overflow the mask array */ if (!config->formats) { dev_warn(w->dapm->dev, "ASoC: Invalid format was specified\n");
- ret = -EINVAL; - goto out; + return -EINVAL; }
fmt = ffs(config->formats) - 1; @@ -3971,7 +3967,7 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
ret = snd_soc_dai_hw_params(source, substream, params); if (ret < 0) - goto out; + return ret;
dapm_update_dai_unlocked(substream, params, source); } @@ -3982,7 +3978,7 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w,
ret = snd_soc_dai_hw_params(sink, substream, params); if (ret < 0) - goto out; + return ret;
dapm_update_dai_unlocked(substream, params, sink); } @@ -3992,11 +3988,7 @@ snd_soc_dai_link_event_pre_pmu(struct snd_soc_dapm_widget *w, runtime->channels = params_channels(params); runtime->rate = params_rate(params);
-out: - /* see above NOTE */ - kfree(params); - - return ret; + return 0; }
static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
On Wed, 03 Jul 2024 14:10:54 +0200, Krzysztof Kozlowski wrote:
Allocate the memory with scoped/cleanup.h to reduce error handling (simpler error paths) and make the code a bit smaller.
Best regards, Krzysztof
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next
Thanks!
[01/11] ASoC: codecs: audio-iio-aux: Simplify audio_iio_aux_add_dapms() with cleanup.h commit: 408e49381750ca22fc584a37636f5035d2cd4c25 [02/11] ASoC: codecs: audio-iio-aux: Simplify audio_iio_aux_probe() with cleanup.h commit: f9cbfb66127bfc2a47dece3dfcdab2b79ab06c50 [03/11] ASoC: codecs: wcd9335: Simplify with cleanup.h commit: 6344ab5d0826640799e0c054ed4c0846b3f87ccb [04/11] ASoC: codecs: wcd934x: Simplify with cleanup.h commit: 56d426f5525d1ad919e20663ad01a58238652df7 [05/11] ASoC: simple-card-utils: Simplify with cleanup.h commit: 6440e7b2a058c50a05ebcc58f35693c50522fc1a [06/11] ASoC: audio-graph-card: Use cleanup.h instead of devm_kfree() commit: 5725c16af2678d334de0bcb85b42cfa50b32e04c [07/11] ASoC: audio-graph-card2: Use cleanup.h instead of devm_kfree() commit: b39f7713ece62b2b0a3cfad7a75a0eb0ab71aa4e [08/11] ASoC: simple-card: Use cleanup.h instead of devm_kfree() commit: 7d996c8a5fea700e816379e57f4983e2611519a0 [09/11] ASoC: ops: Simplify with cleanup.h commit: 1a7b846818210cbdf8994bfee1340c09342a5b3b [10/11] ASoC: dapm: Simplify dapm_cnew_widget() with cleanup.h commit: 5b3cc85673bcc7bb961a3a6fa229cbc4fe0441ac [11/11] ASoC: dapm: Simplify snd_soc_dai_link_event_pre_pmu() with cleanup.h commit: 522133d4401010d936b0588a5a975c2c965cb88e
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
participants (3)
-
Krzysztof Kozlowski
-
Kuninori Morimoto
-
Mark Brown