percpu_counter.c 5.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6
/*
 * Fast batching percpu counters.
 */

#include <linux/percpu_counter.h>
7 8 9
#include <linux/mutex.h>
#include <linux/init.h>
#include <linux/cpu.h>
10
#include <linux/module.h>
T
Tejun Heo 已提交
11
#include <linux/debugobjects.h>
12

13
#ifdef CONFIG_HOTPLUG_CPU
14
static LIST_HEAD(percpu_counters);
15
static DEFINE_SPINLOCK(percpu_counters_lock);
16
#endif
17

T
Tejun Heo 已提交
18 19 20 21
#ifdef CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER

static struct debug_obj_descr percpu_counter_debug_descr;

22
static bool percpu_counter_fixup_free(void *addr, enum debug_obj_state state)
T
Tejun Heo 已提交
23 24 25 26 27 28 29
{
	struct percpu_counter *fbc = addr;

	switch (state) {
	case ODEBUG_STATE_ACTIVE:
		percpu_counter_destroy(fbc);
		debug_object_free(fbc, &percpu_counter_debug_descr);
30
		return true;
T
Tejun Heo 已提交
31
	default:
32
		return false;
T
Tejun Heo 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	}
}

static struct debug_obj_descr percpu_counter_debug_descr = {
	.name		= "percpu_counter",
	.fixup_free	= percpu_counter_fixup_free,
};

static inline void debug_percpu_counter_activate(struct percpu_counter *fbc)
{
	debug_object_init(fbc, &percpu_counter_debug_descr);
	debug_object_activate(fbc, &percpu_counter_debug_descr);
}

static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc)
{
	debug_object_deactivate(fbc, &percpu_counter_debug_descr);
	debug_object_free(fbc, &percpu_counter_debug_descr);
}

#else	/* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */
static inline void debug_percpu_counter_activate(struct percpu_counter *fbc)
{ }
static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc)
{ }
#endif	/* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */

P
Peter Zijlstra 已提交
60 61 62
void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
{
	int cpu;
S
Shaohua Li 已提交
63
	unsigned long flags;
P
Peter Zijlstra 已提交
64

S
Shaohua Li 已提交
65
	raw_spin_lock_irqsave(&fbc->lock, flags);
P
Peter Zijlstra 已提交
66 67 68 69 70
	for_each_possible_cpu(cpu) {
		s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
		*pcount = 0;
	}
	fbc->count = amount;
S
Shaohua Li 已提交
71
	raw_spin_unlock_irqrestore(&fbc->lock, flags);
P
Peter Zijlstra 已提交
72 73 74
}
EXPORT_SYMBOL(percpu_counter_set);

75 76 77 78 79 80 81
/**
 * This function is both preempt and irq safe. The former is due to explicit
 * preemption disable. The latter is guaranteed by the fact that the slow path
 * is explicitly protected by an irq-safe spinlock whereas the fast patch uses
 * this_cpu_add which is irq-safe by definition. Hence there is no need muck
 * with irq state before calling this one
 */
82
void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
83
{
84
	s64 count;
85

86
	preempt_disable();
87
	count = __this_cpu_read(*fbc->counters) + amount;
88
	if (count >= batch || count <= -batch) {
S
Shaohua Li 已提交
89 90
		unsigned long flags;
		raw_spin_lock_irqsave(&fbc->lock, flags);
91
		fbc->count += count;
92
		__this_cpu_sub(*fbc->counters, count - amount);
S
Shaohua Li 已提交
93
		raw_spin_unlock_irqrestore(&fbc->lock, flags);
94
	} else {
95
		this_cpu_add(*fbc->counters, amount);
96
	}
97
	preempt_enable();
98
}
99
EXPORT_SYMBOL(percpu_counter_add_batch);
100 101 102 103 104

/*
 * Add up all the per-cpu counts, return the result.  This is a more accurate
 * but much slower version of percpu_counter_read_positive()
 */
105
s64 __percpu_counter_sum(struct percpu_counter *fbc)
106
{
107
	s64 ret;
108
	int cpu;
S
Shaohua Li 已提交
109
	unsigned long flags;
110

S
Shaohua Li 已提交
111
	raw_spin_lock_irqsave(&fbc->lock, flags);
112
	ret = fbc->count;
113
	for_each_online_cpu(cpu) {
114
		s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
115 116
		ret += *pcount;
	}
S
Shaohua Li 已提交
117
	raw_spin_unlock_irqrestore(&fbc->lock, flags);
P
Peter Zijlstra 已提交
118
	return ret;
119
}
P
Peter Zijlstra 已提交
120
EXPORT_SYMBOL(__percpu_counter_sum);
121

122
int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
123
			  struct lock_class_key *key)
124
{
125 126
	unsigned long flags __maybe_unused;

127
	raw_spin_lock_init(&fbc->lock);
128
	lockdep_set_class(&fbc->lock, key);
129
	fbc->count = amount;
130
	fbc->counters = alloc_percpu_gfp(s32, gfp);
131 132
	if (!fbc->counters)
		return -ENOMEM;
T
Tejun Heo 已提交
133 134 135

	debug_percpu_counter_activate(fbc);

136
#ifdef CONFIG_HOTPLUG_CPU
137
	INIT_LIST_HEAD(&fbc->list);
138
	spin_lock_irqsave(&percpu_counters_lock, flags);
139
	list_add(&fbc->list, &percpu_counters);
140
	spin_unlock_irqrestore(&percpu_counters_lock, flags);
141
#endif
142
	return 0;
143
}
144
EXPORT_SYMBOL(__percpu_counter_init);
145 146 147

void percpu_counter_destroy(struct percpu_counter *fbc)
{
148 149
	unsigned long flags __maybe_unused;

150 151 152
	if (!fbc->counters)
		return;

T
Tejun Heo 已提交
153 154
	debug_percpu_counter_deactivate(fbc);

155
#ifdef CONFIG_HOTPLUG_CPU
156
	spin_lock_irqsave(&percpu_counters_lock, flags);
157
	list_del(&fbc->list);
158
	spin_unlock_irqrestore(&percpu_counters_lock, flags);
159
#endif
160 161
	free_percpu(fbc->counters);
	fbc->counters = NULL;
162 163 164
}
EXPORT_SYMBOL(percpu_counter_destroy);

165 166 167
int percpu_counter_batch __read_mostly = 32;
EXPORT_SYMBOL(percpu_counter_batch);

168
static int compute_batch_value(unsigned int cpu)
169 170 171 172
{
	int nr = num_online_cpus();

	percpu_counter_batch = max(32, nr*2);
173
	return 0;
174 175
}

176
static int percpu_counter_cpu_dead(unsigned int cpu)
177
{
178
#ifdef CONFIG_HOTPLUG_CPU
179 180
	struct percpu_counter *fbc;

181
	compute_batch_value(cpu);
182

183
	spin_lock_irq(&percpu_counters_lock);
184 185 186
	list_for_each_entry(fbc, &percpu_counters, list) {
		s32 *pcount;

187
		raw_spin_lock(&fbc->lock);
188 189 190
		pcount = per_cpu_ptr(fbc->counters, cpu);
		fbc->count += *pcount;
		*pcount = 0;
191
		raw_spin_unlock(&fbc->lock);
192
	}
193
	spin_unlock_irq(&percpu_counters_lock);
194
#endif
195
	return 0;
196 197
}

198 199 200 201
/*
 * Compare counter against given value.
 * Return 1 if greater, 0 if equal and -1 if less
 */
202
int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
203 204 205 206 207
{
	s64	count;

	count = percpu_counter_read(fbc);
	/* Check to see if rough count will be sufficient for comparison */
208
	if (abs(count - rhs) > (batch * num_online_cpus())) {
209 210 211 212 213 214 215 216 217 218 219 220 221 222
		if (count > rhs)
			return 1;
		else
			return -1;
	}
	/* Need to use precise count */
	count = percpu_counter_sum(fbc);
	if (count > rhs)
		return 1;
	else if (count < rhs)
		return -1;
	else
		return 0;
}
223
EXPORT_SYMBOL(__percpu_counter_compare);
224

225 226
static int __init percpu_counter_startup(void)
{
227 228 229 230 231 232 233 234 235
	int ret;

	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "lib/percpu_cnt:online",
				compute_batch_value, NULL);
	WARN_ON(ret < 0);
	ret = cpuhp_setup_state_nocalls(CPUHP_PERCPU_CNT_DEAD,
					"lib/percpu_cnt:dead", NULL,
					percpu_counter_cpu_dead);
	WARN_ON(ret < 0);
236 237 238
	return 0;
}
module_init(percpu_counter_startup);