blob: e285f373559498e422ce5d7f75b02d57e7ef0585 [file] [log] [blame]
Nicholas Flintham1e3d3112013-04-10 10:48:38 +01001/*
2 * Specialised local-global spinlock. Can only be declared as global variables
3 * to avoid overhead and keep things simple (and we don't want to start using
4 * these inside dynamically allocated structures).
5 *
6 * "local/global locks" (lglocks) can be used to:
7 *
8 * - Provide fast exclusive access to per-CPU data, with exclusive access to
9 * another CPU's data allowed but possibly subject to contention, and to
10 * provide very slow exclusive access to all per-CPU data.
11 * - Or to provide very fast and scalable read serialisation, and to provide
12 * very slow exclusive serialisation of data (not necessarily per-CPU data).
13 *
14 * Brlocks are also implemented as a short-hand notation for the latter use
15 * case.
16 *
17 * Copyright 2009, 2010, Nick Piggin, Novell Inc.
18 */
19#ifndef __LINUX_LGLOCK_H
20#define __LINUX_LGLOCK_H
21
22#include <linux/spinlock.h>
23#include <linux/lockdep.h>
24#include <linux/percpu.h>
25#include <linux/cpu.h>
26
27#define br_lock_init(name) name##_lock_init()
28#define br_read_lock(name) name##_local_lock()
29#define br_read_unlock(name) name##_local_unlock()
30#define br_write_lock(name) name##_global_lock_online()
31#define br_write_unlock(name) name##_global_unlock_online()
32
33#define DECLARE_BRLOCK(name) DECLARE_LGLOCK(name)
34#define DEFINE_BRLOCK(name) DEFINE_LGLOCK(name)
35
36
37#define lg_lock_init(name) name##_lock_init()
38#define lg_local_lock(name) name##_local_lock()
39#define lg_local_unlock(name) name##_local_unlock()
40#define lg_local_lock_cpu(name, cpu) name##_local_lock_cpu(cpu)
41#define lg_local_unlock_cpu(name, cpu) name##_local_unlock_cpu(cpu)
42#define lg_global_lock(name) name##_global_lock()
43#define lg_global_unlock(name) name##_global_unlock()
44#define lg_global_lock_online(name) name##_global_lock_online()
45#define lg_global_unlock_online(name) name##_global_unlock_online()
46
47#ifdef CONFIG_DEBUG_LOCK_ALLOC
48#define LOCKDEP_INIT_MAP lockdep_init_map
49
50#define DEFINE_LGLOCK_LOCKDEP(name) \
51 struct lock_class_key name##_lock_key; \
52 struct lockdep_map name##_lock_dep_map; \
53 EXPORT_SYMBOL(name##_lock_dep_map)
54
55#else
56#define LOCKDEP_INIT_MAP(a, b, c, d)
57
58#define DEFINE_LGLOCK_LOCKDEP(name)
59#endif
60
61
62#define DECLARE_LGLOCK(name) \
63 extern void name##_lock_init(void); \
64 extern void name##_local_lock(void); \
65 extern void name##_local_unlock(void); \
66 extern void name##_local_lock_cpu(int cpu); \
67 extern void name##_local_unlock_cpu(int cpu); \
68 extern void name##_global_lock(void); \
69 extern void name##_global_unlock(void); \
70 extern void name##_global_lock_online(void); \
71 extern void name##_global_unlock_online(void); \
72
73#define DEFINE_LGLOCK(name) \
74 \
75 DEFINE_SPINLOCK(name##_cpu_lock); \
76 cpumask_t name##_cpus __read_mostly; \
77 DEFINE_PER_CPU(arch_spinlock_t, name##_lock); \
78 DEFINE_LGLOCK_LOCKDEP(name); \
79 \
80 static int \
81 name##_lg_cpu_callback(struct notifier_block *nb, \
82 unsigned long action, void *hcpu) \
83 { \
84 switch (action & ~CPU_TASKS_FROZEN) { \
85 case CPU_UP_PREPARE: \
86 spin_lock(&name##_cpu_lock); \
87 cpu_set((unsigned long)hcpu, name##_cpus); \
88 spin_unlock(&name##_cpu_lock); \
89 break; \
90 case CPU_UP_CANCELED: case CPU_DEAD: \
91 spin_lock(&name##_cpu_lock); \
92 cpu_clear((unsigned long)hcpu, name##_cpus); \
93 spin_unlock(&name##_cpu_lock); \
94 } \
95 return NOTIFY_OK; \
96 } \
97 static struct notifier_block name##_lg_cpu_notifier = { \
98 .notifier_call = name##_lg_cpu_callback, \
99 }; \
100 void name##_lock_init(void) { \
101 int i; \
102 LOCKDEP_INIT_MAP(&name##_lock_dep_map, #name, &name##_lock_key, 0); \
103 for_each_possible_cpu(i) { \
104 arch_spinlock_t *lock; \
105 lock = &per_cpu(name##_lock, i); \
106 *lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; \
107 } \
108 register_hotcpu_notifier(&name##_lg_cpu_notifier); \
109 get_online_cpus(); \
110 for_each_online_cpu(i) \
111 cpu_set(i, name##_cpus); \
112 put_online_cpus(); \
113 } \
114 EXPORT_SYMBOL(name##_lock_init); \
115 \
116 void name##_local_lock(void) { \
117 arch_spinlock_t *lock; \
118 preempt_disable(); \
119 rwlock_acquire_read(&name##_lock_dep_map, 0, 0, _THIS_IP_); \
120 lock = &__get_cpu_var(name##_lock); \
121 arch_spin_lock(lock); \
122 } \
123 EXPORT_SYMBOL(name##_local_lock); \
124 \
125 void name##_local_unlock(void) { \
126 arch_spinlock_t *lock; \
127 rwlock_release(&name##_lock_dep_map, 1, _THIS_IP_); \
128 lock = &__get_cpu_var(name##_lock); \
129 arch_spin_unlock(lock); \
130 preempt_enable(); \
131 } \
132 EXPORT_SYMBOL(name##_local_unlock); \
133 \
134 void name##_local_lock_cpu(int cpu) { \
135 arch_spinlock_t *lock; \
136 preempt_disable(); \
137 rwlock_acquire_read(&name##_lock_dep_map, 0, 0, _THIS_IP_); \
138 lock = &per_cpu(name##_lock, cpu); \
139 arch_spin_lock(lock); \
140 } \
141 EXPORT_SYMBOL(name##_local_lock_cpu); \
142 \
143 void name##_local_unlock_cpu(int cpu) { \
144 arch_spinlock_t *lock; \
145 rwlock_release(&name##_lock_dep_map, 1, _THIS_IP_); \
146 lock = &per_cpu(name##_lock, cpu); \
147 arch_spin_unlock(lock); \
148 preempt_enable(); \
149 } \
150 EXPORT_SYMBOL(name##_local_unlock_cpu); \
151 \
152 void name##_global_lock_online(void) { \
153 int i; \
154 spin_lock(&name##_cpu_lock); \
155 rwlock_acquire(&name##_lock_dep_map, 0, 0, _RET_IP_); \
156 for_each_cpu(i, &name##_cpus) { \
157 arch_spinlock_t *lock; \
158 lock = &per_cpu(name##_lock, i); \
159 arch_spin_lock(lock); \
160 } \
161 } \
162 EXPORT_SYMBOL(name##_global_lock_online); \
163 \
164 void name##_global_unlock_online(void) { \
165 int i; \
166 rwlock_release(&name##_lock_dep_map, 1, _RET_IP_); \
167 for_each_cpu(i, &name##_cpus) { \
168 arch_spinlock_t *lock; \
169 lock = &per_cpu(name##_lock, i); \
170 arch_spin_unlock(lock); \
171 } \
172 spin_unlock(&name##_cpu_lock); \
173 } \
174 EXPORT_SYMBOL(name##_global_unlock_online); \
175 \
176 void name##_global_lock(void) { \
177 int i; \
178 preempt_disable(); \
179 rwlock_acquire(&name##_lock_dep_map, 0, 0, _RET_IP_); \
180 for_each_possible_cpu(i) { \
181 arch_spinlock_t *lock; \
182 lock = &per_cpu(name##_lock, i); \
183 arch_spin_lock(lock); \
184 } \
185 } \
186 EXPORT_SYMBOL(name##_global_lock); \
187 \
188 void name##_global_unlock(void) { \
189 int i; \
190 rwlock_release(&name##_lock_dep_map, 1, _RET_IP_); \
191 for_each_possible_cpu(i) { \
192 arch_spinlock_t *lock; \
193 lock = &per_cpu(name##_lock, i); \
194 arch_spin_unlock(lock); \
195 } \
196 preempt_enable(); \
197 } \
198 EXPORT_SYMBOL(name##_global_unlock);
199#endif