From d0f67f97579f7b2febf29264b1874a95056dec06 Mon Sep 17 00:00:00 2001 From: Konstantin Belousov Date: Sun, 7 May 2023 21:37:42 +0300 Subject: [PATCH] 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.