git: d0f67f97579f - main - smp_topo(): make it idempotent

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 09 May 2023 15:30:16 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=d0f67f97579f7b2febf29264b1874a95056dec06

commit d0f67f97579f7b2febf29264b1874a95056dec06
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-05-07 18:37:42 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-05-09 15:30:07 +0000

    smp_topo(): make it idempotent
    
    If more than one call to the function occurs, it currently allocates the
    same amount from the group[] array, eventually leading to the memory
    corruption.
    
    Noted and reviewed by:  markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D39981
---
 sys/kern/subr_smp.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/sys/kern/subr_smp.c b/sys/kern/subr_smp.c
index df82d948afb0..5a9aeb5ab04a 100644
--- a/sys/kern/subr_smp.c
+++ b/sys/kern/subr_smp.c
@@ -645,7 +645,14 @@ struct cpu_group *
 smp_topo(void)
 {
 	char cpusetbuf[CPUSETBUFSIZ], cpusetbuf2[CPUSETBUFSIZ];
-	struct cpu_group *top;
+	static struct cpu_group *top = NULL;
+
+	/*
+	 * The first call to smp_topo() is guaranteed to occur
+	 * during the kernel boot while we are still single-threaded.
+	 */
+	if (top != NULL)
+		return (top);
 
 	/*
 	 * Check for a fake topology request for debugging purposes.