提交 e625305b 编写于 作者: T Tejun Heo

percpu-refcount: make percpu_ref based on longs instead of ints

percpu_ref is currently based on ints and the number of refs it can
cover is (1 << 31).  This makes it impossible to use a percpu_ref to
count memory objects or pages on 64bit machines as it may overflow.
This forces those users to somehow aggregate the references before
contributing to the percpu_ref which is often cumbersome and sometimes
challenging to get the same level of performance as using the
percpu_ref directly.

While using ints for the percpu counters makes them pack tighter on
64bit machines, the possible gain from using ints instead of longs is
extremely small compared to the overall gain from per-cpu operation.
This patch makes percpu_ref based on longs so that it can be used to
directly count memory objects or pages.
Signed-off-by: NTejun Heo <tj@kernel.org>
Cc: Kent Overstreet <kmo@daterainc.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
上级 4843c332
...@@ -55,7 +55,7 @@ struct percpu_ref; ...@@ -55,7 +55,7 @@ struct percpu_ref;
typedef void (percpu_ref_func_t)(struct percpu_ref *); typedef void (percpu_ref_func_t)(struct percpu_ref *);
struct percpu_ref { struct percpu_ref {
atomic_t count; atomic_long_t count;
/* /*
* The low bit of the pointer indicates whether the ref is in percpu * The low bit of the pointer indicates whether the ref is in percpu
* mode; if set, then get/put will manipulate the atomic_t. * mode; if set, then get/put will manipulate the atomic_t.
...@@ -97,7 +97,7 @@ static inline void percpu_ref_kill(struct percpu_ref *ref) ...@@ -97,7 +97,7 @@ static inline void percpu_ref_kill(struct percpu_ref *ref)
* branches as it can't assume that @ref->pcpu_count is not NULL. * branches as it can't assume that @ref->pcpu_count is not NULL.
*/ */
static inline bool __pcpu_ref_alive(struct percpu_ref *ref, static inline bool __pcpu_ref_alive(struct percpu_ref *ref,
unsigned __percpu **pcpu_countp) unsigned long __percpu **pcpu_countp)
{ {
unsigned long pcpu_ptr = ACCESS_ONCE(ref->pcpu_count_ptr); unsigned long pcpu_ptr = ACCESS_ONCE(ref->pcpu_count_ptr);
...@@ -107,7 +107,7 @@ static inline bool __pcpu_ref_alive(struct percpu_ref *ref, ...@@ -107,7 +107,7 @@ static inline bool __pcpu_ref_alive(struct percpu_ref *ref,
if (unlikely(pcpu_ptr & PCPU_REF_DEAD)) if (unlikely(pcpu_ptr & PCPU_REF_DEAD))
return false; return false;
*pcpu_countp = (unsigned __percpu *)pcpu_ptr; *pcpu_countp = (unsigned long __percpu *)pcpu_ptr;
return true; return true;
} }
...@@ -119,14 +119,14 @@ static inline bool __pcpu_ref_alive(struct percpu_ref *ref, ...@@ -119,14 +119,14 @@ static inline bool __pcpu_ref_alive(struct percpu_ref *ref,
*/ */
static inline void percpu_ref_get(struct percpu_ref *ref) static inline void percpu_ref_get(struct percpu_ref *ref)
{ {
unsigned __percpu *pcpu_count; unsigned long __percpu *pcpu_count;
rcu_read_lock_sched(); rcu_read_lock_sched();
if (__pcpu_ref_alive(ref, &pcpu_count)) if (__pcpu_ref_alive(ref, &pcpu_count))
this_cpu_inc(*pcpu_count); this_cpu_inc(*pcpu_count);
else else
atomic_inc(&ref->count); atomic_long_inc(&ref->count);
rcu_read_unlock_sched(); rcu_read_unlock_sched();
} }
...@@ -142,7 +142,7 @@ static inline void percpu_ref_get(struct percpu_ref *ref) ...@@ -142,7 +142,7 @@ static inline void percpu_ref_get(struct percpu_ref *ref)
*/ */
static inline bool percpu_ref_tryget(struct percpu_ref *ref) static inline bool percpu_ref_tryget(struct percpu_ref *ref)
{ {
unsigned __percpu *pcpu_count; unsigned long __percpu *pcpu_count;
int ret = false; int ret = false;
rcu_read_lock_sched(); rcu_read_lock_sched();
...@@ -151,7 +151,7 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) ...@@ -151,7 +151,7 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref)
this_cpu_inc(*pcpu_count); this_cpu_inc(*pcpu_count);
ret = true; ret = true;
} else { } else {
ret = atomic_inc_not_zero(&ref->count); ret = atomic_long_inc_not_zero(&ref->count);
} }
rcu_read_unlock_sched(); rcu_read_unlock_sched();
...@@ -175,7 +175,7 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref) ...@@ -175,7 +175,7 @@ static inline bool percpu_ref_tryget(struct percpu_ref *ref)
*/ */
static inline bool percpu_ref_tryget_live(struct percpu_ref *ref) static inline bool percpu_ref_tryget_live(struct percpu_ref *ref)
{ {
unsigned __percpu *pcpu_count; unsigned long __percpu *pcpu_count;
int ret = false; int ret = false;
rcu_read_lock_sched(); rcu_read_lock_sched();
...@@ -199,13 +199,13 @@ static inline bool percpu_ref_tryget_live(struct percpu_ref *ref) ...@@ -199,13 +199,13 @@ static inline bool percpu_ref_tryget_live(struct percpu_ref *ref)
*/ */
static inline void percpu_ref_put(struct percpu_ref *ref) static inline void percpu_ref_put(struct percpu_ref *ref)
{ {
unsigned __percpu *pcpu_count; unsigned long __percpu *pcpu_count;
rcu_read_lock_sched(); rcu_read_lock_sched();
if (__pcpu_ref_alive(ref, &pcpu_count)) if (__pcpu_ref_alive(ref, &pcpu_count))
this_cpu_dec(*pcpu_count); this_cpu_dec(*pcpu_count);
else if (unlikely(atomic_dec_and_test(&ref->count))) else if (unlikely(atomic_long_dec_and_test(&ref->count)))
ref->release(ref); ref->release(ref);
rcu_read_unlock_sched(); rcu_read_unlock_sched();
...@@ -219,11 +219,11 @@ static inline void percpu_ref_put(struct percpu_ref *ref) ...@@ -219,11 +219,11 @@ static inline void percpu_ref_put(struct percpu_ref *ref)
*/ */
static inline bool percpu_ref_is_zero(struct percpu_ref *ref) static inline bool percpu_ref_is_zero(struct percpu_ref *ref)
{ {
unsigned __percpu *pcpu_count; unsigned long __percpu *pcpu_count;
if (__pcpu_ref_alive(ref, &pcpu_count)) if (__pcpu_ref_alive(ref, &pcpu_count))
return false; return false;
return !atomic_read(&ref->count); return !atomic_long_read(&ref->count);
} }
#endif #endif
...@@ -25,15 +25,15 @@ ...@@ -25,15 +25,15 @@
* works. * works.
* *
* Converting to non percpu mode is done with some RCUish stuff in * Converting to non percpu mode is done with some RCUish stuff in
* percpu_ref_kill. Additionally, we need a bias value so that the atomic_t * percpu_ref_kill. Additionally, we need a bias value so that the
* can't hit 0 before we've added up all the percpu refs. * atomic_long_t can't hit 0 before we've added up all the percpu refs.
*/ */
#define PCPU_COUNT_BIAS (1U << 31) #define PCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref) static unsigned long __percpu *pcpu_count_ptr(struct percpu_ref *ref)
{ {
return (unsigned __percpu *)(ref->pcpu_count_ptr & ~PCPU_REF_DEAD); return (unsigned long __percpu *)(ref->pcpu_count_ptr & ~PCPU_REF_DEAD);
} }
/** /**
...@@ -43,7 +43,7 @@ static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref) ...@@ -43,7 +43,7 @@ static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref)
* @gfp: allocation mask to use * @gfp: allocation mask to use
* *
* Initializes the refcount in single atomic counter mode with a refcount of 1; * Initializes the refcount in single atomic counter mode with a refcount of 1;
* analagous to atomic_set(ref, 1). * analagous to atomic_long_set(ref, 1).
* *
* Note that @release must not sleep - it may potentially be called from RCU * Note that @release must not sleep - it may potentially be called from RCU
* callback context by percpu_ref_kill(). * callback context by percpu_ref_kill().
...@@ -51,9 +51,9 @@ static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref) ...@@ -51,9 +51,9 @@ static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref)
int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release, int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release,
gfp_t gfp) gfp_t gfp)
{ {
atomic_set(&ref->count, 1 + PCPU_COUNT_BIAS); atomic_long_set(&ref->count, 1 + PCPU_COUNT_BIAS);
ref->pcpu_count_ptr = (unsigned long)alloc_percpu_gfp(unsigned, gfp); ref->pcpu_count_ptr = (unsigned long)alloc_percpu_gfp(unsigned long, gfp);
if (!ref->pcpu_count_ptr) if (!ref->pcpu_count_ptr)
return -ENOMEM; return -ENOMEM;
...@@ -75,13 +75,13 @@ EXPORT_SYMBOL_GPL(percpu_ref_init); ...@@ -75,13 +75,13 @@ EXPORT_SYMBOL_GPL(percpu_ref_init);
*/ */
void percpu_ref_reinit(struct percpu_ref *ref) void percpu_ref_reinit(struct percpu_ref *ref)
{ {
unsigned __percpu *pcpu_count = pcpu_count_ptr(ref); unsigned long __percpu *pcpu_count = pcpu_count_ptr(ref);
int cpu; int cpu;
BUG_ON(!pcpu_count); BUG_ON(!pcpu_count);
WARN_ON(!percpu_ref_is_zero(ref)); WARN_ON(!percpu_ref_is_zero(ref));
atomic_set(&ref->count, 1 + PCPU_COUNT_BIAS); atomic_long_set(&ref->count, 1 + PCPU_COUNT_BIAS);
/* /*
* Restore per-cpu operation. smp_store_release() is paired with * Restore per-cpu operation. smp_store_release() is paired with
...@@ -109,7 +109,7 @@ EXPORT_SYMBOL_GPL(percpu_ref_reinit); ...@@ -109,7 +109,7 @@ EXPORT_SYMBOL_GPL(percpu_ref_reinit);
*/ */
void percpu_ref_exit(struct percpu_ref *ref) void percpu_ref_exit(struct percpu_ref *ref)
{ {
unsigned __percpu *pcpu_count = pcpu_count_ptr(ref); unsigned long __percpu *pcpu_count = pcpu_count_ptr(ref);
if (pcpu_count) { if (pcpu_count) {
free_percpu(pcpu_count); free_percpu(pcpu_count);
...@@ -121,14 +121,15 @@ EXPORT_SYMBOL_GPL(percpu_ref_exit); ...@@ -121,14 +121,15 @@ EXPORT_SYMBOL_GPL(percpu_ref_exit);
static void percpu_ref_kill_rcu(struct rcu_head *rcu) static void percpu_ref_kill_rcu(struct rcu_head *rcu)
{ {
struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu); struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu);
unsigned __percpu *pcpu_count = pcpu_count_ptr(ref); unsigned long __percpu *pcpu_count = pcpu_count_ptr(ref);
unsigned count = 0; unsigned long count = 0;
int cpu; int cpu;
for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
count += *per_cpu_ptr(pcpu_count, cpu); count += *per_cpu_ptr(pcpu_count, cpu);
pr_debug("global %i pcpu %i", atomic_read(&ref->count), (int) count); pr_debug("global %ld pcpu %ld",
atomic_long_read(&ref->count), (long)count);
/* /*
* It's crucial that we sum the percpu counters _before_ adding the sum * It's crucial that we sum the percpu counters _before_ adding the sum
...@@ -143,11 +144,11 @@ static void percpu_ref_kill_rcu(struct rcu_head *rcu) ...@@ -143,11 +144,11 @@ static void percpu_ref_kill_rcu(struct rcu_head *rcu)
* time is equivalent and saves us atomic operations: * time is equivalent and saves us atomic operations:
*/ */
atomic_add((int) count - PCPU_COUNT_BIAS, &ref->count); atomic_long_add((long)count - PCPU_COUNT_BIAS, &ref->count);
WARN_ONCE(atomic_read(&ref->count) <= 0, WARN_ONCE(atomic_long_read(&ref->count) <= 0,
"percpu ref (%pf) <= 0 (%i) after killed", "percpu ref (%pf) <= 0 (%ld) after killed",
ref->release, atomic_read(&ref->count)); ref->release, atomic_long_read(&ref->count));
/* @ref is viewed as dead on all CPUs, send out kill confirmation */ /* @ref is viewed as dead on all CPUs, send out kill confirmation */
if (ref->confirm_kill) if (ref->confirm_kill)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册