[PATCH 0/9] soundwire: 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 (9): soundwire: amd: simplify return path in hw_params soundwire: amd: simplify with cleanup.h soundwire: amd_init: simplify with cleanup.h soundwire: intel: simplify return path in hw_params soundwire: intel: simplify with cleanup.h soundwire: intel_ace2x: simplify return path in hw_params soundwire: intel_ace2x: simplify with cleanup.h soundwire: cadence: simplify with cleanup.h soundwire: debugfs: simplify with cleanup.h
drivers/soundwire/amd_init.c | 7 +++---- drivers/soundwire/amd_manager.c | 13 +++++-------- drivers/soundwire/cadence_master.c | 5 ++--- drivers/soundwire/debugfs.c | 7 ++----- drivers/soundwire/intel.c | 25 +++++++++---------------- drivers/soundwire/intel_ace2x.c | 22 ++++++++-------------- 6 files changed, 29 insertions(+), 50 deletions(-) --- base-commit: 246cf92bafedeea46cb16964d16478d6b68f8e5a change-id: 20240703-soundwire-cleanup-h-a29941fb3dd4
Best regards,
Remove unused error path (label+goto) to make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/amd_manager.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/drivers/soundwire/amd_manager.c b/drivers/soundwire/amd_manager.c index 795e223f7e5c..4470fda83c5c 100644 --- a/drivers/soundwire/amd_manager.c +++ b/drivers/soundwire/amd_manager.c @@ -627,10 +627,8 @@ static int amd_sdw_hw_params(struct snd_pcm_substream *substream,
/* Port configuration */ pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL); - if (!pconfig) { - ret = -ENOMEM; - goto error; - } + if (!pconfig) + return -ENOMEM;
pconfig->num = dai->id; pconfig->ch_mask = (1 << ch) - 1; @@ -640,7 +638,7 @@ static int amd_sdw_hw_params(struct snd_pcm_substream *substream, dev_err(amd_manager->dev, "add manager to stream failed:%d\n", ret);
kfree(pconfig); -error: + return ret; }
Allocate the memory with scoped/cleanup.h to reduce error handling and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/amd_manager.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/drivers/soundwire/amd_manager.c b/drivers/soundwire/amd_manager.c index 4470fda83c5c..0d01849c3586 100644 --- a/drivers/soundwire/amd_manager.c +++ b/drivers/soundwire/amd_manager.c @@ -6,6 +6,7 @@ */
#include <linux/completion.h> +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/io.h> #include <linux/jiffies.h> @@ -603,7 +604,6 @@ static int amd_sdw_hw_params(struct snd_pcm_substream *substream, struct amd_sdw_manager *amd_manager = snd_soc_dai_get_drvdata(dai); struct sdw_amd_dai_runtime *dai_runtime; struct sdw_stream_config sconfig; - struct sdw_port_config *pconfig; int ch, dir; int ret;
@@ -626,7 +626,8 @@ static int amd_sdw_hw_params(struct snd_pcm_substream *substream, sconfig.bps = snd_pcm_format_width(params_format(params));
/* Port configuration */ - pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL); + struct sdw_port_config *pconfig __free(kfree) = kzalloc(sizeof(*pconfig), + GFP_KERNEL); if (!pconfig) return -ENOMEM;
@@ -637,8 +638,6 @@ static int amd_sdw_hw_params(struct snd_pcm_substream *substream, if (ret) dev_err(amd_manager->dev, "add manager to stream failed:%d\n", ret);
- kfree(pconfig); - return ret; }
Allocate the memory with scoped/cleanup.h to reduce error handling and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/amd_init.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/drivers/soundwire/amd_init.c b/drivers/soundwire/amd_init.c index 4cd26f3a21f5..db040f435059 100644 --- a/drivers/soundwire/amd_init.c +++ b/drivers/soundwire/amd_init.c @@ -8,6 +8,7 @@ */
#include <linux/acpi.h> +#include <linux/cleanup.h> #include <linux/export.h> #include <linux/io.h> #include <linux/module.h> @@ -69,7 +70,6 @@ static struct sdw_amd_ctx *sdw_amd_probe_controller(struct sdw_amd_res *res) { struct sdw_amd_ctx *ctx; struct acpi_device *adev; - struct resource *sdw_res; struct acp_sdw_pdata sdw_pdata[2]; struct platform_device_info pdevinfo[2]; u32 link_mask; @@ -104,7 +104,8 @@ static struct sdw_amd_ctx *sdw_amd_probe_controller(struct sdw_amd_res *res)
ctx->count = count; ctx->link_mask = res->link_mask; - sdw_res = kzalloc(sizeof(*sdw_res), GFP_KERNEL); + struct resource *sdw_res __free(kfree) = kzalloc(sizeof(*sdw_res), + GFP_KERNEL); if (!sdw_res) { kfree(ctx); return NULL; @@ -132,7 +133,6 @@ static struct sdw_amd_ctx *sdw_amd_probe_controller(struct sdw_amd_res *res) if (IS_ERR(ctx->pdev[index])) goto err; } - kfree(sdw_res); return ctx; err: while (index--) { @@ -142,7 +142,6 @@ static struct sdw_amd_ctx *sdw_amd_probe_controller(struct sdw_amd_res *res) platform_device_unregister(ctx->pdev[index]); }
- kfree(sdw_res); kfree(ctx); return NULL; }
Remove unused error path (label+goto) to make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/intel.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/drivers/soundwire/intel.c b/drivers/soundwire/intel.c index 01e1a0f3ec39..b4449095b423 100644 --- a/drivers/soundwire/intel.c +++ b/drivers/soundwire/intel.c @@ -743,10 +743,8 @@ static int intel_hw_params(struct snd_pcm_substream *substream,
pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
- if (!pdi) { - ret = -EINVAL; - goto error; - } + if (!pdi) + return -EINVAL;
/* do run-time configurations for SHIM, ALH and PDI/PORT */ intel_pdi_shim_configure(sdw, pdi); @@ -763,7 +761,7 @@ static int intel_hw_params(struct snd_pcm_substream *substream, sdw->instance, pdi->intel_alh_id); if (ret) - goto error; + return ret;
sconfig.direction = dir; sconfig.ch_count = ch; @@ -774,10 +772,8 @@ static int intel_hw_params(struct snd_pcm_substream *substream,
/* Port configuration */ pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL); - if (!pconfig) { - ret = -ENOMEM; - goto error; - } + if (!pconfig) + return -ENOMEM;
pconfig->num = pdi->num; pconfig->ch_mask = (1 << ch) - 1; @@ -788,7 +784,7 @@ static int intel_hw_params(struct snd_pcm_substream *substream, dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
kfree(pconfig); -error: + return ret; }
Allocate the memory with scoped/cleanup.h to reduce error handling and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/intel.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/drivers/soundwire/intel.c b/drivers/soundwire/intel.c index b4449095b423..421da0f86fad 100644 --- a/drivers/soundwire/intel.c +++ b/drivers/soundwire/intel.c @@ -6,6 +6,7 @@ */
#include <linux/acpi.h> +#include <linux/cleanup.h> #include <linux/debugfs.h> #include <linux/delay.h> #include <linux/io.h> @@ -73,12 +74,11 @@ static int intel_reg_show(struct seq_file *s_file, void *data) struct sdw_intel *sdw = s_file->private; void __iomem *s = sdw->link_res->shim; void __iomem *a = sdw->link_res->alh; - char *buf; ssize_t ret; int i, j; unsigned int links, reg;
- buf = kzalloc(RD_BUF, GFP_KERNEL); + char *buf __free(kfree) = kzalloc(RD_BUF, GFP_KERNEL); if (!buf) return -ENOMEM;
@@ -129,7 +129,6 @@ static int intel_reg_show(struct seq_file *s_file, void *data) ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
seq_printf(s_file, "%s", buf); - kfree(buf);
return 0; } @@ -727,7 +726,6 @@ static int intel_hw_params(struct snd_pcm_substream *substream, struct sdw_cdns_dai_runtime *dai_runtime; struct sdw_cdns_pdi *pdi; struct sdw_stream_config sconfig; - struct sdw_port_config *pconfig; int ch, dir; int ret;
@@ -771,7 +769,8 @@ static int intel_hw_params(struct snd_pcm_substream *substream, sconfig.bps = snd_pcm_format_width(params_format(params));
/* Port configuration */ - pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL); + struct sdw_port_config *pconfig __free(kfree) = kzalloc(sizeof(*pconfig), + GFP_KERNEL); if (!pconfig) return -ENOMEM;
@@ -783,8 +782,6 @@ static int intel_hw_params(struct snd_pcm_substream *substream, if (ret) dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
- kfree(pconfig); - return ret; }
Remove unused error path (label+goto) to make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/intel_ace2x.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
diff --git a/drivers/soundwire/intel_ace2x.c b/drivers/soundwire/intel_ace2x.c index 4f78b7f99e44..f0008ff84ab4 100644 --- a/drivers/soundwire/intel_ace2x.c +++ b/drivers/soundwire/intel_ace2x.c @@ -325,11 +325,8 @@ static int intel_hw_params(struct snd_pcm_substream *substream, dir = SDW_DATA_DIR_TX;
pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id); - - if (!pdi) { - ret = -EINVAL; - goto error; - } + if (!pdi) + return -EINVAL;
/* use same definitions for alh_id as previous generations */ pdi->intel_alh_id = (sdw->instance * 16) + pdi->num + 3; @@ -350,7 +347,7 @@ static int intel_hw_params(struct snd_pcm_substream *substream, sdw->instance, pdi->intel_alh_id); if (ret) - goto error; + return ret;
sconfig.direction = dir; sconfig.ch_count = ch; @@ -361,10 +358,8 @@ static int intel_hw_params(struct snd_pcm_substream *substream,
/* Port configuration */ pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL); - if (!pconfig) { - ret = -ENOMEM; - goto error; - } + if (!pconfig) + return -ENOMEM;
pconfig->num = pdi->num; pconfig->ch_mask = (1 << ch) - 1; @@ -375,7 +370,7 @@ static int intel_hw_params(struct snd_pcm_substream *substream, dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
kfree(pconfig); -error: + return ret; }
Allocate the memory with scoped/cleanup.h to reduce error handling and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/intel_ace2x.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/drivers/soundwire/intel_ace2x.c b/drivers/soundwire/intel_ace2x.c index f0008ff84ab4..781fe0aefa68 100644 --- a/drivers/soundwire/intel_ace2x.c +++ b/drivers/soundwire/intel_ace2x.c @@ -6,6 +6,7 @@ */
#include <linux/acpi.h> +#include <linux/cleanup.h> #include <linux/device.h> #include <linux/soundwire/sdw_registers.h> #include <linux/soundwire/sdw.h> @@ -310,7 +311,6 @@ static int intel_hw_params(struct snd_pcm_substream *substream, struct sdw_cdns_dai_runtime *dai_runtime; struct sdw_cdns_pdi *pdi; struct sdw_stream_config sconfig; - struct sdw_port_config *pconfig; int ch, dir; int ret;
@@ -357,7 +357,8 @@ static int intel_hw_params(struct snd_pcm_substream *substream, sconfig.bps = snd_pcm_format_width(params_format(params));
/* Port configuration */ - pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL); + struct sdw_port_config *pconfig __free(kfree) = kzalloc(sizeof(*pconfig), + GFP_KERNEL); if (!pconfig) return -ENOMEM;
@@ -369,8 +370,6 @@ static int intel_hw_params(struct snd_pcm_substream *substream, if (ret) dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
- kfree(pconfig); - return ret; }
Allocate the memory with scoped/cleanup.h to reduce error handling and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/cadence_master.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/soundwire/cadence_master.c b/drivers/soundwire/cadence_master.c index 74da99034dab..e0683a5975d1 100644 --- a/drivers/soundwire/cadence_master.c +++ b/drivers/soundwire/cadence_master.c @@ -6,6 +6,7 @@ * Used by Master driver */
+#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/device.h> #include <linux/debugfs.h> @@ -323,12 +324,11 @@ static ssize_t cdns_sprintf(struct sdw_cdns *cdns, static int cdns_reg_show(struct seq_file *s, void *data) { struct sdw_cdns *cdns = s->private; - char *buf; ssize_t ret; int num_ports; int i, j;
- buf = kzalloc(RD_BUF, GFP_KERNEL); + char *buf __free(kfree) = kzalloc(RD_BUF, GFP_KERNEL); if (!buf) return -ENOMEM;
@@ -389,7 +389,6 @@ static int cdns_reg_show(struct seq_file *s, void *data) ret += cdns_sprintf(cdns, buf, ret, CDNS_PDI_CONFIG(i));
seq_printf(s, "%s", buf); - kfree(buf);
return 0; }
Allocate the memory with scoped/cleanup.h to reduce error handling and make the code a bit simpler.
Signed-off-by: Krzysztof Kozlowski krzysztof.kozlowski@linaro.org --- drivers/soundwire/debugfs.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/drivers/soundwire/debugfs.c b/drivers/soundwire/debugfs.c index 6d253d69871d..c30f571934ee 100644 --- a/drivers/soundwire/debugfs.c +++ b/drivers/soundwire/debugfs.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only // Copyright(c) 2017-2019 Intel Corporation.
+#include <linux/cleanup.h> #include <linux/device.h> #include <linux/debugfs.h> #include <linux/firmware.h> @@ -49,18 +50,16 @@ static ssize_t sdw_sprintf(struct sdw_slave *slave, static int sdw_slave_reg_show(struct seq_file *s_file, void *data) { struct sdw_slave *slave = s_file->private; - char *buf; ssize_t ret; int i, j;
- buf = kzalloc(RD_BUF, GFP_KERNEL); + char *buf __free(kfree) = kzalloc(RD_BUF, GFP_KERNEL); if (!buf) return -ENOMEM;
ret = pm_runtime_get_sync(&slave->dev); if (ret < 0 && ret != -EACCES) { pm_runtime_put_noidle(&slave->dev); - kfree(buf); return ret; }
@@ -132,8 +131,6 @@ static int sdw_slave_reg_show(struct seq_file *s_file, void *data) pm_runtime_mark_last_busy(&slave->dev); pm_runtime_put(&slave->dev);
- kfree(buf); - return 0; } DEFINE_SHOW_ATTRIBUTE(sdw_slave_reg);
On Wed, 03 Jul 2024 12:15:52 +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, thanks!
[1/9] soundwire: amd: simplify return path in hw_params commit: 89cc1354d388ba8c8f8b41095736202a83591497 [2/9] soundwire: amd: simplify with cleanup.h commit: 02611eeec5893c17ad85769007ecfb5cbe7a5987 [3/9] soundwire: amd_init: simplify with cleanup.h commit: 1f93cb229b0e2d78233690c9ca65715e1f798803 [4/9] soundwire: intel: simplify return path in hw_params commit: ba874a8c2f895d898bbaf67f9e952425aff1557d [5/9] soundwire: intel: simplify with cleanup.h commit: e4fcf153d91809aefa6860d285e747fd7dd9e61c [6/9] soundwire: intel_ace2x: simplify return path in hw_params commit: 13814ed162687be08e34762040cfc2e58831219d [7/9] soundwire: intel_ace2x: simplify with cleanup.h commit: 3dce65898e0911aa76a0a321540b78e9218b9a6a [8/9] soundwire: cadence: simplify with cleanup.h commit: b72d4af98cae2f74dc8061befcc3c0c2a174894f [9/9] soundwire: debugfs: simplify with cleanup.h commit: fdd3d14ca3c8c5269174f10d33d6181173cbd0b4
Best regards,
participants (2)
-
Krzysztof Kozlowski
-
Vinod Koul