aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2014-09-20 01:27:25 -0400
committerTejun Heo <tj@kernel.org>2014-09-20 01:27:25 -0400
commite625305b390790717cf2cccf61efb81299647028 (patch)
tree87bde164e935bf06fca1b4adfa26d23f1cca4238 /lib
parent4843c3320c3d23ab4ecf520f5eaf485aff8c7252 (diff)
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: Tejun Heo <tj@kernel.org> Cc: Kent Overstreet <kmo@daterainc.com> Cc: Johannes Weiner <hannes@cmpxchg.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/percpu-refcount.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
index 70d28c91f35a..559ee0b20318 100644
--- a/lib/percpu-refcount.c
+++ b/lib/percpu-refcount.c
@@ -25,15 +25,15 @@
25 * works. 25 * works.
26 * 26 *
27 * Converting to non percpu mode is done with some RCUish stuff in 27 * Converting to non percpu mode is done with some RCUish stuff in
28 * percpu_ref_kill. Additionally, we need a bias value so that the atomic_t 28 * percpu_ref_kill. Additionally, we need a bias value so that the
29 * can't hit 0 before we've added up all the percpu refs. 29 * atomic_long_t can't hit 0 before we've added up all the percpu refs.
30 */ 30 */
31 31
32#define PCPU_COUNT_BIAS (1U << 31) 32#define PCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
33 33
34static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref) 34static unsigned long __percpu *pcpu_count_ptr(struct percpu_ref *ref)
35{ 35{
36 return (unsigned __percpu *)(ref->pcpu_count_ptr & ~PCPU_REF_DEAD); 36 return (unsigned long __percpu *)(ref->pcpu_count_ptr & ~PCPU_REF_DEAD);
37} 37}
38 38
39/** 39/**
@@ -43,7 +43,7 @@ static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref)
43 * @gfp: allocation mask to use 43 * @gfp: allocation mask to use
44 * 44 *
45 * Initializes the refcount in single atomic counter mode with a refcount of 1; 45 * Initializes the refcount in single atomic counter mode with a refcount of 1;
46 * analagous to atomic_set(ref, 1). 46 * analagous to atomic_long_set(ref, 1).
47 * 47 *
48 * Note that @release must not sleep - it may potentially be called from RCU 48 * Note that @release must not sleep - it may potentially be called from RCU
49 * callback context by percpu_ref_kill(). 49 * callback context by percpu_ref_kill().
@@ -51,9 +51,9 @@ static unsigned __percpu *pcpu_count_ptr(struct percpu_ref *ref)
51int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release, 51int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release,
52 gfp_t gfp) 52 gfp_t gfp)
53{ 53{
54 atomic_set(&ref->count, 1 + PCPU_COUNT_BIAS); 54 atomic_long_set(&ref->count, 1 + PCPU_COUNT_BIAS);
55 55
56 ref->pcpu_count_ptr = (unsigned long)alloc_percpu_gfp(unsigned, gfp); 56 ref->pcpu_count_ptr = (unsigned long)alloc_percpu_gfp(unsigned long, gfp);
57 if (!ref->pcpu_count_ptr) 57 if (!ref->pcpu_count_ptr)
58 return -ENOMEM; 58 return -ENOMEM;
59 59
@@ -75,13 +75,13 @@ EXPORT_SYMBOL_GPL(percpu_ref_init);
75 */ 75 */
76void percpu_ref_reinit(struct percpu_ref *ref) 76void percpu_ref_reinit(struct percpu_ref *ref)
77{ 77{
78 unsigned __percpu *pcpu_count = pcpu_count_ptr(ref); 78 unsigned long __percpu *pcpu_count = pcpu_count_ptr(ref);
79 int cpu; 79 int cpu;
80 80
81 BUG_ON(!pcpu_count); 81 BUG_ON(!pcpu_count);
82 WARN_ON(!percpu_ref_is_zero(ref)); 82 WARN_ON(!percpu_ref_is_zero(ref));
83 83
84 atomic_set(&ref->count, 1 + PCPU_COUNT_BIAS); 84 atomic_long_set(&ref->count, 1 + PCPU_COUNT_BIAS);
85 85
86 /* 86 /*
87 * Restore per-cpu operation. smp_store_release() is paired with 87 * Restore per-cpu operation. smp_store_release() is paired with
@@ -109,7 +109,7 @@ EXPORT_SYMBOL_GPL(percpu_ref_reinit);
109 */ 109 */
110void percpu_ref_exit(struct percpu_ref *ref) 110void percpu_ref_exit(struct percpu_ref *ref)
111{ 111{
112 unsigned __percpu *pcpu_count = pcpu_count_ptr(ref); 112 unsigned long __percpu *pcpu_count = pcpu_count_ptr(ref);
113 113
114 if (pcpu_count) { 114 if (pcpu_count) {
115 free_percpu(pcpu_count); 115 free_percpu(pcpu_count);
@@ -121,14 +121,15 @@ EXPORT_SYMBOL_GPL(percpu_ref_exit);
121static void percpu_ref_kill_rcu(struct rcu_head *rcu) 121static void percpu_ref_kill_rcu(struct rcu_head *rcu)
122{ 122{
123 struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu); 123 struct percpu_ref *ref = container_of(rcu, struct percpu_ref, rcu);
124 unsigned __percpu *pcpu_count = pcpu_count_ptr(ref); 124 unsigned long __percpu *pcpu_count = pcpu_count_ptr(ref);
125 unsigned count = 0; 125 unsigned long count = 0;
126 int cpu; 126 int cpu;
127 127
128 for_each_possible_cpu(cpu) 128 for_each_possible_cpu(cpu)
129 count += *per_cpu_ptr(pcpu_count, cpu); 129 count += *per_cpu_ptr(pcpu_count, cpu);
130 130
131 pr_debug("global %i pcpu %i", atomic_read(&ref->count), (int) count); 131 pr_debug("global %ld pcpu %ld",
132 atomic_long_read(&ref->count), (long)count);
132 133
133 /* 134 /*
134 * It's crucial that we sum the percpu counters _before_ adding the sum 135 * 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 * time is equivalent and saves us atomic operations: 144 * time is equivalent and saves us atomic operations:
144 */ 145 */
145 146
146 atomic_add((int) count - PCPU_COUNT_BIAS, &ref->count); 147 atomic_long_add((long)count - PCPU_COUNT_BIAS, &ref->count);
147 148
148 WARN_ONCE(atomic_read(&ref->count) <= 0, 149 WARN_ONCE(atomic_long_read(&ref->count) <= 0,
149 "percpu ref (%pf) <= 0 (%i) after killed", 150 "percpu ref (%pf) <= 0 (%ld) after killed",
150 ref->release, atomic_read(&ref->count)); 151 ref->release, atomic_long_read(&ref->count));
151 152
152 /* @ref is viewed as dead on all CPUs, send out kill confirmation */ 153 /* @ref is viewed as dead on all CPUs, send out kill confirmation */
153 if (ref->confirm_kill) 154 if (ref->confirm_kill)