mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: yolezz <yolezz.secret@gmail.com>
To: mario.limonciello@amd.com, yazen.ghannam@amd.com
Cc: x86@kernel.org, linux-kernel@vger.kernel.org,
	yolezz <yolezz.secret@gmail.com>
Subject: [PATCH v2] x86/amd/node: Release reserved config regions on init error
Date: Tue, 15 Sep 2026 19:23:55 +0200	[thread overview]
Message-ID: <20260915172355.104902-1-yolezz.secret@gmail.com> (raw)
In-Reply-To: <6bd2cad5-65ef-4f3d-a146-d91f120c86ad@amd.com>

In amd_smn_init(), if pci_request_config_region_exclusive() fails or
if kzalloc_objs() fails to allocate memory for amd_roots, the already
reserved PCI config regions are left allocated.

Use a __free() cleanup helper to automatically release all reserved PCI
config space regions on error exit paths.

Fixes: 83518453074d ("x86/amd_node: Add SMN offsets to exclusive region access")
Signed-off-by: yolezz <yolezz.secret@gmail.com>
---
v2:
 - Use a __free() cleanup helper instead of manual error rollback, as suggested by Mario.

 arch/x86/kernel/amd_node.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/amd_node.c b/arch/x86/kernel/amd_node.c
index 6a96d888b5a7..20451f0ad8c7 100644
--- a/arch/x86/kernel/amd_node.c
+++ b/arch/x86/kernel/amd_node.c
@@ -239,15 +239,22 @@ static struct pci_dev *get_next_root(struct pci_dev *root)
 	return root;
 }
 
-static void amd_smn_release_config_regions(u16 num_roots)
+static void amd_smn_release_config_regions(u16 *num_roots)
 {
 	struct pci_dev *root __free(pci_dev_put) = NULL;
 
-	/* Release the PCI config space for each root device. */
-	while (num_roots-- && (root = get_next_root(root)))
+	if (!num_roots)
+		return;
+
+	while (*num_roots && (root = get_next_root(root))) {
 		pci_release_config_region(root, 0, PCI_CFG_SPACE_SIZE);
+		(*num_roots)--;
+	}
 }
 
+DEFINE_FREE(amd_smn_release_config_regions, u16 *,
+	    amd_smn_release_config_regions(_T));
+
 static bool enable_dfs;
 
 static int __init amd_smn_enable_dfs(char *str)
@@ -259,9 +266,11 @@ __setup("amd_smn_debugfs_enable", amd_smn_enable_dfs);
 
 static int __init amd_smn_init(void)
 {
-	u16 count, num_roots, roots_per_node, node, num_nodes;
+	u16 count, num_roots = 0, roots_per_node, node, num_nodes;
 	struct pci_dev *root __free(pci_dev_put) = NULL;
 
+	u16 *config_regions __free(amd_smn_release_config_regions) = NULL;
+
 	if (!cpu_feature_enabled(X86_FEATURE_ZEN))
 		return 0;
 
@@ -270,7 +279,8 @@ static int __init amd_smn_init(void)
 	if (amd_roots)
 		return 0;
 
-	num_roots = 0;
+	config_regions = &num_roots;
+
 	while ((root = get_next_root(root))) {
 		pci_dbg(root, "Reserving PCI config space\n");
 
@@ -282,7 +292,6 @@ static int __init amd_smn_init(void)
 		 */
 		if (!pci_request_config_region_exclusive(root, 0, PCI_CFG_SPACE_SIZE, NULL)) {
 			pci_err(root, "Failed to reserve config space\n");
-			amd_smn_release_config_regions(num_roots);
 			return -EEXIST;
 		}
 
@@ -297,7 +306,6 @@ static int __init amd_smn_init(void)
 	num_nodes = amd_num_nodes();
 	amd_roots = kzalloc_objs(*amd_roots, num_nodes);
 	if (!amd_roots) {
-		amd_smn_release_config_regions(num_roots);
 		return -ENOMEM;
 	}
 
@@ -327,6 +335,7 @@ static int __init amd_smn_init(void)
 		debugfs_create_file("value",	0600, debugfs_dir, NULL, &smn_value_fops);
 	}
 
+	config_regions = NULL;
 	return 0;
 }
 
-- 
2.53.0


  reply	other threads:[~2026-09-15 17:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-15 16:22 [PATCH] " yolezz
2026-09-15 16:35 ` Mario Limonciello
2026-09-15 17:23   ` yolezz [this message]
2026-09-15 17:32     ` [PATCH v2] " Mario Limonciello
2026-09-15 17:36   ` [PATCH v3] " yolezz
2026-09-15 17:42     ` Mario Limonciello
2026-09-15 18:05   ` [PATCH v4] " yolezz
2026-09-15 19:07     ` Mario Limonciello
2026-09-15 20:54     ` Borislav Petkov
2026-09-15 22:20     ` Yazen Ghannam
     [not found]       ` <CABLVixVbsR1Au=ReUUR2tpzhy0+5L8rm6VAAkrwJKR1ahR1sag@mail.gmail.com>
2026-09-16 15:11         ` Borislav Petkov

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=20260915172355.104902-1-yolezz.secret@gmail.com \
    --to=yolezz.secret@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=x86@kernel.org \
    --cc=yazen.ghannam@amd.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

all inboxes | Powered by JetHome®