Nick Piggin | 2dc91ab | 2010-08-18 04:37:37 +1000 | [diff] [blame] | 1 | /* |
| 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> |
Srivatsa S. Bhat | e30e2fd | 2011-12-22 02:45:29 +0530 | [diff] [blame] | 25 | #include <linux/cpu.h> |
Andi Kleen | eea62f8 | 2012-05-08 13:32:24 +0930 | [diff] [blame] | 26 | #include <linux/notifier.h> |
Nick Piggin | 2dc91ab | 2010-08-18 04:37:37 +1000 | [diff] [blame] | 27 | |
Nick Piggin | 2dc91ab | 2010-08-18 04:37:37 +1000 | [diff] [blame] | 28 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 29 | #define LOCKDEP_INIT_MAP lockdep_init_map |
Nick Piggin | 2dc91ab | 2010-08-18 04:37:37 +1000 | [diff] [blame] | 30 | #else |
| 31 | #define LOCKDEP_INIT_MAP(a, b, c, d) |
Nick Piggin | 2dc91ab | 2010-08-18 04:37:37 +1000 | [diff] [blame] | 32 | #endif |
| 33 | |
Andi Kleen | eea62f8 | 2012-05-08 13:32:24 +0930 | [diff] [blame] | 34 | struct lglock { |
| 35 | arch_spinlock_t __percpu *lock; |
| 36 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 37 | struct lock_class_key lock_key; |
| 38 | struct lockdep_map lock_dep_map; |
| 39 | #endif |
| 40 | }; |
Nick Piggin | 2dc91ab | 2010-08-18 04:37:37 +1000 | [diff] [blame] | 41 | |
| 42 | #define DEFINE_LGLOCK(name) \ |
Lai Jiangshan | 466cab8 | 2012-10-09 14:49:51 -0700 | [diff] [blame] | 43 | static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \ |
Andi Kleen | eea62f8 | 2012-05-08 13:32:24 +0930 | [diff] [blame] | 44 | = __ARCH_SPIN_LOCK_UNLOCKED; \ |
| 45 | struct lglock name = { .lock = &name ## _lock } |
| 46 | |
Lai Jiangshan | 4b2c551f | 2012-10-09 14:49:54 -0700 | [diff] [blame] | 47 | #define DEFINE_STATIC_LGLOCK(name) \ |
| 48 | static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \ |
| 49 | = __ARCH_SPIN_LOCK_UNLOCKED; \ |
| 50 | static struct lglock name = { .lock = &name ## _lock } |
| 51 | |
Andi Kleen | eea62f8 | 2012-05-08 13:32:24 +0930 | [diff] [blame] | 52 | void lg_lock_init(struct lglock *lg, char *name); |
| 53 | void lg_local_lock(struct lglock *lg); |
| 54 | void lg_local_unlock(struct lglock *lg); |
| 55 | void lg_local_lock_cpu(struct lglock *lg, int cpu); |
| 56 | void lg_local_unlock_cpu(struct lglock *lg, int cpu); |
| 57 | void lg_global_lock(struct lglock *lg); |
| 58 | void lg_global_unlock(struct lglock *lg); |
| 59 | |
Nick Piggin | 2dc91ab | 2010-08-18 04:37:37 +1000 | [diff] [blame] | 60 | #endif |