Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Mark Brown <broonie@kernel.org>
Cc: linux-sound@vger.kernel.org, Liam Girdwood <lgirdwood@gmail.com>,
	Peter Ujfalusi <peter.ujfalusi@linux.intel.com>,
	Bard Liao <yung-chuan.liao@linux.intel.com>,
	Daniel Baluta <daniel.baluta@nxp.com>,
	Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>,
	Vijendar Mukunda <Vijendar.Mukunda@amd.com>
Subject: [PATCH 31/31] ASoC: SOF: Use auto-cleanup for firmware loading
Date: Wed,  5 Aug 2026 15:52:32 +0200	[thread overview]
Message-ID: <20260805135247.670693-32-tiwai@suse.de> (raw)
In-Reply-To: <20260805135247.670693-1-tiwai@suse.de>

Simplify the code to manage the firmware loading with __free(firmware)
and __free(kfree) auto-cleanups for the firmware data and the temporary
string or array.

Only the code refactoring, no functional changes.

Cc: Liam Girdwood <lgirdwood@gmail.com>
Cc: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
Cc: Bard Liao <yung-chuan.liao@linux.intel.com>
Cc: Daniel Baluta <daniel.baluta@nxp.com>
Cc: Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>
Cc: Vijendar Mukunda <Vijendar.Mukunda@amd.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
 sound/soc/sof/fw-file-profile.c | 19 +++++++------------
 sound/soc/sof/topology.c        | 20 ++++++--------------
 2 files changed, 13 insertions(+), 26 deletions(-)

diff --git a/sound/soc/sof/fw-file-profile.c b/sound/soc/sof/fw-file-profile.c
index 76bde2e0be1d..fcd57f04ca36 100644
--- a/sound/soc/sof/fw-file-profile.c
+++ b/sound/soc/sof/fw-file-profile.c
@@ -16,20 +16,19 @@ static int sof_test_firmware_file(struct device *dev,
 				  enum sof_ipc_type *ipc_type_to_adjust)
 {
 	enum sof_ipc_type fw_ipc_type;
-	const struct firmware *fw;
-	const char *fw_filename;
 	const u32 *magic;
 	int ret;
 
-	fw_filename = kasprintf(GFP_KERNEL, "%s/%s", profile->fw_path,
-				profile->fw_name);
+	const char *fw_filename __free(kfree) =
+		kasprintf(GFP_KERNEL, "%s/%s", profile->fw_path,
+			  profile->fw_name);
 	if (!fw_filename)
 		return -ENOMEM;
 
+	const struct firmware *fw __free(firmware) = NULL;
 	ret = firmware_request_nowarn(&fw, fw_filename, dev);
 	if (ret < 0) {
 		dev_dbg(dev, "Failed to open firmware file: %s\n", fw_filename);
-		kfree(fw_filename);
 		return ret;
 	}
 
@@ -44,8 +43,7 @@ static int sof_test_firmware_file(struct device *dev,
 		break;
 	default:
 		dev_err(dev, "Invalid firmware magic: %#x\n", *magic);
-		ret = -EINVAL;
-		goto out;
+		return -EINVAL;
 	}
 
 	if (ipc_type_to_adjust) {
@@ -54,13 +52,10 @@ static int sof_test_firmware_file(struct device *dev,
 		dev_err(dev,
 			"ipc type mismatch between %s and expected: %d vs %d\n",
 			fw_filename, fw_ipc_type, profile->ipc_type);
-		ret = -EINVAL;
+		return -EINVAL;
 	}
-out:
-	release_firmware(fw);
-	kfree(fw_filename);
 
-	return ret;
+	return 0;
 }
 
 static int sof_test_topology_file(struct device *dev,
diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c
index 6fd69ba11c41..820513bb2577 100644
--- a/sound/soc/sof/topology.c
+++ b/sound/soc/sof/topology.c
@@ -2506,13 +2506,12 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
 	struct snd_sof_pdata *sof_pdata = sdev->pdata;
 	const char *tplg_filename_prefix = sof_pdata->tplg_filename_prefix;
-	const struct firmware *fw;
-	const char **tplg_files;
 	int tplg_cnt = 0;
 	int ret;
 	int i;
 
-	tplg_files = kcalloc(scomp->card->num_links, sizeof(char *), GFP_KERNEL);
+	const char **tplg_files __free(kfree) =
+		kcalloc(scomp->card->num_links, sizeof(char *), GFP_KERNEL);
 	if (!tplg_files)
 		return -ENOMEM;
 
@@ -2538,10 +2537,8 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 								       tplg_filename_prefix,
 								       &tplg_files,
 								       no_fallback);
-		if (tplg_cnt < 0) {
-			kfree(tplg_files);
+		if (tplg_cnt < 0)
 			return tplg_cnt;
-		}
 	}
 
 	/*
@@ -2552,8 +2549,6 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 		if (strstr(file, "dummy")) {
 			dev_err(scomp->dev,
 				"Function topology is required, please upgrade sof-firmware\n");
-
-			kfree(tplg_files);
 			return -EINVAL;
 		}
 		tplg_files[0] = file;
@@ -2568,6 +2563,7 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 		if (tplg_files[0] != file)
 			dev_info(scomp->dev, "loading topology %d: %s\n", i, tplg_files[i]);
 
+		const struct firmware *fw __free(firmware) = NULL;
 		ret = request_firmware(&fw, tplg_files[i], scomp->dev);
 		if (ret < 0) {
 			/*
@@ -2586,8 +2582,6 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 		else
 			ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
 
-		release_firmware(fw);
-
 		if (ret < 0) {
 			dev_err(scomp->dev, "tplg %s component load failed %d\n",
 				tplg_files[i], ret);
@@ -2606,6 +2600,8 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 			goto out;
 		}
 		dev_info(scomp->dev, "loading feature topology %d: %s\n", i, feature_topology);
+
+		const struct firmware *fw __free(firmware) = NULL;
 		ret = request_firmware(&fw, feature_topology, scomp->dev);
 		if (ret < 0) {
 			/*
@@ -2630,8 +2626,6 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 		else
 			ret = snd_soc_tplg_component_load(scomp, &sof_tplg_ops, fw);
 
-		release_firmware(fw);
-
 		if (ret < 0) {
 			dev_err(scomp->dev, "feature tplg %s component load failed %d\n",
 				feature_topologies[i], ret);
@@ -2650,8 +2644,6 @@ int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file)
 	if (ret >= 0 && sdev->led_present)
 		ret = snd_ctl_led_request();
 
-	kfree(tplg_files);
-
 	return ret;
 }
 EXPORT_SYMBOL(snd_sof_load_topology);
-- 
2.55.0


  parent reply	other threads:[~2026-08-05 13:53 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-05 13:52 [PATCH 00/31] ASoC: Use auto-cleanup for firmware loading Takashi Iwai
2026-08-05 13:52 ` [PATCH 01/31] ASoC: aw87390: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 02/31] ASoC: aw88081: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 03/31] ASoC: aw88166: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 04/31] ASoC: aw88261: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 05/31] ASoC: aw88395: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 06/31] ASoC: aw88399: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 07/31] ASoC: fs-amp-lib: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 08/31] ASoC: hdac_hda: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 09/31] ASoC: max98390: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 10/31] ASoC: ntpfw: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 11/31] ASoC: pcm6240: " Takashi Iwai
2026-08-05 16:47   ` Herve Codina
2026-08-06  6:16     ` Takashi Iwai
2026-08-05 13:52 ` [PATCH 12/31] ASoC: peb2466: " Takashi Iwai
2026-08-05 16:50   ` Herve Codina
2026-08-05 13:52 ` [PATCH 13/31] ASoC: rt1320-sdw: " Takashi Iwai
2026-08-05 22:56   ` Mark Brown
2026-08-06  7:37     ` Takashi Iwai
2026-08-06 11:52       ` Mark Brown
2026-08-06 13:48         ` Takashi Iwai
2026-08-05 13:52 ` [PATCH 14/31] ASoC: rt5575: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 15/31] ASoC: rt5677: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 16/31] ASoC: rt722-sdca: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 17/31] ASoC: sigmadsp: se " Takashi Iwai
2026-08-05 16:08   ` Nuno Sá
2026-08-05 13:52 ` [PATCH 18/31] ASoC: sma1307: Use " Takashi Iwai
2026-08-05 13:52 ` [PATCH 19/31] ASoC: tas2781: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 20/31] ASoC: tas5805m: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 21/31] ASoC: tlv320aic31xx: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 22/31] ASoC: wm0010: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 23/31] ASoC: wm2000: " Takashi Iwai
2026-08-05 23:16   ` Mark Brown
2026-08-06  7:38     ` Takashi Iwai
2026-08-05 13:52 ` [PATCH 24/31] ASoC: zl38060: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 25/31] ASoC: fsl: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 26/31] ASoC: Intel: avs: " Takashi Iwai
2026-08-05 14:48   ` Cezary Rojewski
2026-08-05 13:52 ` [PATCH 27/31] ASoC: Intel: catpt: " Takashi Iwai
2026-08-05 14:48   ` Cezary Rojewski
2026-08-05 13:52 ` [PATCH 28/31] ASoC: qcom: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 29/31] ASoC: renesas: " Takashi Iwai
2026-08-05 13:52 ` [PATCH 30/31] ASoC: SDCA: " Takashi Iwai
2026-08-05 14:34   ` Charles Keepax
2026-08-05 13:52 ` Takashi Iwai [this message]
2026-08-06 11:56   ` [PATCH 31/31] ASoC: SOF: " Péter Ujfalusi
2026-08-06 12:06     ` Takashi Iwai
2026-08-06 13:04       ` Péter Ujfalusi
2026-08-06 13:18         ` Takashi Iwai

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=20260805135247.670693-32-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=Vijendar.Mukunda@amd.com \
    --cc=broonie@kernel.org \
    --cc=daniel.baluta@nxp.com \
    --cc=lgirdwood@gmail.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=peter.ujfalusi@linux.intel.com \
    --cc=pierre-louis.bossart@linux.dev \
    --cc=yung-chuan.liao@linux.intel.com \
    /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