[RFC PATCH 6/7] lib/percpu_counter: allow (un)charging percpu counters without alloc/free

Harry Yoo
2025-04-24T08:09:01Z

Introduce percpu_counter_{charge,uncharge}_many() to allow explicit
charging and uncharging of percpu memory used by percpu_counter, without
requiring allocation and deallocation of the counters just to
(un)charge.

This is useful in cases where percpu counters preallocated and reused
multiple times—for example, when a slab constructor allocates percpu
counters that are (un)charged multiple times over their lifetime,
until they are finally freed by the destructor.

Signed-off-by: Harry Yoo <harry.yoo@oracle.com>
---
include/linux/percpu_counter.h | 2 ++
lib/percpu_counter.c | 25 +++++++++++++++++++++++++
2 files changed, 27 insertions(+)

diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h
index 3a44dd1e33d2..6e6b0752b1e4 100644
--- a/include/linux/percpu_counter.h
+++ b/include/linux/percpu_counter.h
@@ -46,6 +46,8 @@ int __percpu_counter_init_many(struct percpu_counter *fbc, s64 amount,
#define percpu_counter_init(fbc, value, gfp) \
percpu_counter_init_many(fbc, value, gfp, 1)

+bool percpu_counter_charge_many(struct percpu_counter *fbc, gfp_t gfp, u32 nr_counters);
+void percpu_counter_uncharge_many(struct percpu_counter *fbc, u32 nr_counters);
void percpu_counter_destroy_many(struct percpu_counter *fbc, u32 nr_counters);
static inline void percpu_counter_destroy(struct percpu_counter *fbc)
{
diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c
index 2891f94a11c6..a9fe96787725 100644
--- a/lib/percpu_counter.c
+++ b/lib/percpu_counter.c
@@ -224,6 +224,31 @@ int __percpu_counter_init_many(struct percpu_counter *fbc, s64 amount,
}
EXPORT_SYMBOL(__percpu_counter_init_many);

+bool percpu_counter_charge_many(struct percpu_counter *fbc, gfp_t gfp, u32 nr_counters)
+{
+ s32 __percpu *counters;
+ size_t counter_size;
+ size_t charge_size;
+
+ counter_size = ALIGN(sizeof(*counters), __alignof__(*counters));
+ counters = fbc->counters;
+ charge_size = nr_counters * counter_size;
+
+ return pcpu_charge(counters, charge_size, gfp);
+}
+
+void percpu_counter_uncharge_many(struct percpu_counter *fbc, u32 nr_counters)
+{
+ s32 __percpu *counters;
+ size_t counter_size;
+ size_t uncharge_size;
+
+ counter_size = ALIGN(sizeof(*counters), __alignof__(*counters));
+ counters = fbc->counters;
+ uncharge_size = nr_counters * counter_size;
+ pcpu_uncharge(counters, uncharge_size);
+}
+
void percpu_counter_destroy_many(struct percpu_counter *fbc, u32 nr_counters)
{
unsigned long flags __maybe_unused;
--
2.43.0