From: Alexey Dobriyan <adobriyan@gmail.com>
To: tglx@linutronix.de, peterz@infradead.org
Cc: adobriyan@gmail.com, yury.norov@gmail.com,
linux@rasmusvillemoes.dk, akpm@linux-foundation.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] cpu: memoise number of possible cpus
Date: Thu, 18 Apr 2024 07:19:27 +0300 [thread overview]
Message-ID: <20240418041927.3903-1-adobriyan@gmail.com> (raw)
cpu_possible_mask is fixed after boot, so it makes sense
to calculate number of possible cpus to
a) make num_possible_cpus() faster (distros ship with _large_ NR_CPUS),
b) unscrew codegen elsewhere replacing function call
with simple memory load.
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
include/linux/cpumask.h | 3 ++-
init/main.c | 3 +++
kernel/cpu.c | 3 +++
3 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index 04536a29f10f..a98843ca6131 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -1106,7 +1106,8 @@ static __always_inline unsigned int num_online_cpus(void)
{
return raw_atomic_read(&__num_online_cpus);
}
-#define num_possible_cpus() cpumask_weight(cpu_possible_mask)
+extern unsigned int num_possible_cpus;
+#define num_possible_cpus() num_possible_cpus
#define num_present_cpus() cpumask_weight(cpu_present_mask)
#define num_active_cpus() cpumask_weight(cpu_active_mask)
diff --git a/init/main.c b/init/main.c
index 881f6230ee59..fe0291b44d78 100644
--- a/init/main.c
+++ b/init/main.c
@@ -904,6 +904,9 @@ void start_kernel(void)
setup_boot_config();
setup_command_line(command_line);
setup_nr_cpu_ids();
+#if NR_CPUS > 1
+ num_possible_cpus = cpumask_weight(cpu_possible_mask);
+#endif
setup_per_cpu_areas();
smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */
boot_cpu_hotplug_init();
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 07ad53b7f119..4a75f95fec82 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -3106,6 +3106,9 @@ EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
EXPORT_SYMBOL(cpu_all_bits);
+unsigned int num_possible_cpus __ro_after_init = 1;
+EXPORT_SYMBOL(num_possible_cpus);
+
#ifdef CONFIG_INIT_ALL_POSSIBLE
struct cpumask __cpu_possible_mask __ro_after_init
= {CPU_BITS_ALL};
--
2.43.2
next reply other threads:[~2024-04-18 4:19 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-18 4:19 Alexey Dobriyan [this message]
2024-04-18 20:26 ` Andrew Morton
2024-04-18 20:27 ` Andrew Morton
2024-04-19 5:32 ` Thomas Gleixner
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=20240418041927.3903-1-adobriyan@gmail.com \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=yury.norov@gmail.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
Powered by JetHome