All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yury Norov <yury.norov@gmail.com>
To: linux-kernel@vger.kernel.org, "Yury Norov" <yury.norov@gmail.com>,
	"James E.J. Bottomley" <jejb@linux.ibm.com>,
	"Martin K. Petersen" <martin.petersen@oracle.com>,
	"Michał Mirosław" <mirq-linux@rere.qmqm.pl>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	"Alexey Klimov" <aklimov@redhat.com>,
	"Amitkumar Karwar" <amitkarwar@gmail.com>,
	"Andi Kleen" <ak@linux.intel.com>, "Andrew Lunn" <andrew@lunn.ch>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Andy Gross" <agross@kernel.org>,
	"Andy Lutomirski" <luto@kernel.org>,
	"Andy Shevchenko" <andy@infradead.org>,
	"Anup Patel" <anup.patel@wdc.com>,
	"Ard Biesheuvel" <ardb@kernel.org>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>
Subject: [PATCH 15/17] kernel/cpu: add num_active_cpu counter
Date: Sat, 18 Dec 2021 13:20:11 -0800	[thread overview]
Message-ID: <20211218212014.1315894-16-yury.norov__41341.6491750226$1639862552$gmane$org@gmail.com> (raw)
In-Reply-To: <20211218212014.1315894-1-yury.norov@gmail.com>

Similarly to the online cpus, the cpu_active_mask is actively used
in the kernel. This patch adds a counter for active cpus, so that
users that call num_active_cpus() would know the result immediately,
instead of calling the bitmap_weight for the mask.

Suggested-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
---
 include/linux/cpumask.h | 26 +++++++++++++++-----------
 kernel/cpu.c            | 15 +++++++++++++++
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index c2a9d15e2cbd..0add872898f8 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -101,6 +101,7 @@ extern struct cpumask __cpu_dying_mask;
 extern atomic_t __num_online_cpus;
 extern atomic_t __num_possible_cpus;
 extern atomic_t __num_present_cpus;
+extern atomic_t __num_active_cpus;
 
 extern cpumask_t cpus_booted_once_mask;
 
@@ -875,17 +876,8 @@ void init_cpu_online(const struct cpumask *src);
 void set_cpu_possible(unsigned int cpu, bool possible);
 void reset_cpu_possible_mask(void);
 void set_cpu_present(unsigned int cpu, bool present);
-
 void set_cpu_online(unsigned int cpu, bool online);
-
-static inline void
-set_cpu_active(unsigned int cpu, bool active)
-{
-	if (active)
-		cpumask_set_cpu(cpu, &__cpu_active_mask);
-	else
-		cpumask_clear_cpu(cpu, &__cpu_active_mask);
-}
+void set_cpu_active(unsigned int cpu, bool active);
 
 static inline void
 set_cpu_dying(unsigned int cpu, bool dying)
@@ -971,7 +963,19 @@ static inline unsigned int num_present_cpus(void)
 {
 	return atomic_read(&__num_present_cpus);
 }
-#define num_active_cpus()	cpumask_weight(cpu_active_mask)
+
+/**
+ * num_active_cpus() - Read the number of active CPUs
+ *
+ * Despite the fact that __num_active_cpus is of type atomic_t, this
+ * interface gives only a momentary snapshot and is not protected against
+ * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
+ * region.
+ */
+static inline unsigned int num_active_cpus(void)
+{
+	return atomic_read(&__num_active_cpus);
+}
 
 static inline bool cpu_online(unsigned int cpu)
 {
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 1f7ea1bdde1a..62b411d88810 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -2603,6 +2603,9 @@ EXPORT_SYMBOL(__num_present_cpus);
 struct cpumask __cpu_active_mask __read_mostly;
 EXPORT_SYMBOL(__cpu_active_mask);
 
+atomic_t __num_active_cpus __read_mostly;
+EXPORT_SYMBOL(__num_active_cpus);
+
 struct cpumask __cpu_dying_mask __read_mostly;
 EXPORT_SYMBOL(__cpu_dying_mask);
 
@@ -2678,6 +2681,18 @@ void set_cpu_present(unsigned int cpu, bool present)
 }
 EXPORT_SYMBOL(set_cpu_present);
 
+void set_cpu_active(unsigned int cpu, bool active)
+{
+	if (active) {
+		if (!cpumask_test_and_set_cpu(cpu, &__cpu_active_mask))
+			atomic_inc(&__num_active_cpus);
+	} else {
+		if (cpumask_test_and_clear_cpu(cpu, &__cpu_active_mask))
+			atomic_dec(&__num_active_cpus);
+	}
+}
+EXPORT_SYMBOL(set_cpu_active);
+
 /*
  * Activate the first processor.
  */
-- 
2.30.2


  parent reply	other threads:[~2021-12-18 21:20 UTC|newest]

Thread overview: 126+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-18 21:19 [PATCH v2 00/17] lib/bitmap: optimize bitmap_weight() usage Yury Norov
2021-12-18 21:19 ` Yury Norov
2021-12-18 21:19 ` Yury Norov
2021-12-18 21:19 ` Yury Norov
2021-12-18 21:19 ` Yury Norov
2021-12-18 21:19 ` [PATCH 01/17] all: don't use bitmap_weight() where possible Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 22:15   ` Michał Mirosław
2021-12-18 22:15     ` Michał Mirosław
2021-12-18 22:15     ` Michał Mirosław
2021-12-18 22:15     ` Michał Mirosław
2021-12-18 22:15     ` Michał Mirosław
2021-12-18 23:28     ` Yury Norov
2021-12-18 23:28       ` Yury Norov
2021-12-18 23:28       ` Yury Norov
2021-12-18 23:28       ` Yury Norov
2021-12-18 23:28       ` Yury Norov
2021-12-18 23:28       ` Yury Norov
2021-12-18 21:19 ` [PATCH 02/17] drivers: rename num_*_cpus variables Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19 ` [PATCH 03/17] fix open-coded for_each_set_bit() Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19   ` Yury Norov
2021-12-18 21:19 ` Yury Norov
2021-12-18 21:20 ` [PATCH 04/17] all: replace bitmap_weight with bitmap_empty where appropriate Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` [PATCH 05/17] all: replace cpumask_weight with cpumask_empty " Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20 ` [PATCH 06/17] all: replace nodes_weight with nodes_empty " Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20 ` [PATCH 07/17] lib/bitmap: add bitmap_weight_{cmp,eq,gt,ge,lt,le} functions Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` [PATCH 07/17] lib/bitmap: add bitmap_weight_{cmp, eq, gt, ge, lt, le} functions Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` [PATCH 08/17] all: replace bitmap_weight with bitmap_weight_{eq,gt,ge,lt,le} where appropriate Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` [PATCH 08/17] all: replace bitmap_weight with bitmap_weight_{eq, gt, ge, lt, le} " Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-20 16:41   ` [PATCH 08/17] all: replace bitmap_weight with bitmap_weight_{eq,gt,ge,lt,le} " Greg Kroah-Hartman
2021-12-20 16:41     ` Greg Kroah-Hartman
2021-12-20 16:41     ` Greg Kroah-Hartman
2021-12-20 16:41     ` Greg Kroah-Hartman
2021-12-20 16:41     ` Greg Kroah-Hartman
2021-12-20 16:41     ` Greg Kroah-Hartman
2021-12-18 21:20 ` [PATCH 09/17] lib/cpumask: add cpumask_weight_{eq,gt,ge,lt,le} Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` [PATCH 10/17] lib/nodemask: add nodemask_weight_{eq,gt,ge,lt,le} Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20 ` [PATCH 11/17] lib/nodemask: add num_node_state_eq() Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` [PATCH 12/17] kernel/cpu.c: fix init_cpu_online Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20 ` [PATCH 13/17] kernel/cpu: add num_possible_cpus counter Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-21 13:15   ` Peter Zijlstra
2021-12-21 13:15     ` Peter Zijlstra
2021-12-21 13:15     ` Peter Zijlstra
2021-12-21 13:15     ` Peter Zijlstra
2021-12-21 13:15     ` Peter Zijlstra
2021-12-21 13:15     ` Peter Zijlstra
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20 ` [PATCH 14/17] kernel/cpu: add num_present_cpu counter Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-21 13:14   ` Peter Zijlstra
2021-12-21 13:14     ` Peter Zijlstra
2021-12-21 13:14     ` Peter Zijlstra
2021-12-21 13:14     ` Peter Zijlstra
2021-12-21 13:14     ` Peter Zijlstra
2021-12-21 13:14     ` Peter Zijlstra
2021-12-18 21:20 ` [PATCH 15/17] kernel/cpu: add num_active_cpu counter Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-21 13:13   ` Peter Zijlstra
2021-12-21 13:13     ` Peter Zijlstra
2021-12-21 13:13     ` Peter Zijlstra
2021-12-21 13:13     ` Peter Zijlstra
2021-12-21 13:13     ` Peter Zijlstra
2021-12-21 13:13     ` Peter Zijlstra
2021-12-18 21:20 ` Yury Norov [this message]
2021-12-18 21:20 ` [PATCH 16/17] tools/bitmap: sync bitmap_weight Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20 ` [PATCH 17/17] MAINTAINERS: add cpumask and nodemask files to BITMAP_API Yury Norov
2021-12-18 21:20 ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov
2021-12-18 21:20   ` Yury Norov

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='20211218212014.1315894-16-yury.norov__41341.6491750226$1639862552$gmane$org@gmail.com' \
    --to=yury.norov@gmail.com \
    --cc=acme@kernel.org \
    --cc=agross@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=aklimov@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=amitkarwar@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=andy@infradead.org \
    --cc=anup.patel@wdc.com \
    --cc=ardb@kernel.org \
    --cc=jejb@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=mirq-linux@rere.qmqm.pl \
    --cc=paulmck@kernel.org \
    --cc=rafael@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.