blob: c92ebd100d9b653e25ed9accc4d1ac4569bea784 [file] [log] [blame]
Nick Piggin2dc91ab2010-08-18 04:37:37 +10001/*
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. Bhate30e2fd2011-12-22 02:45:29 +053025#include <linux/cpu.h>
Andi Kleeneea62f82012-05-08 13:32:24 +093026#include <linux/notifier.h>
Nick Piggin2dc91ab2010-08-18 04:37:37 +100027
Josh Triplett64b47e82014-04-07 15:39:45 -070028#ifdef CONFIG_SMP
29
Nick Piggin2dc91ab2010-08-18 04:37:37 +100030#ifdef CONFIG_DEBUG_LOCK_ALLOC
31#define LOCKDEP_INIT_MAP lockdep_init_map
Nick Piggin2dc91ab2010-08-18 04:37:37 +100032#else
33#define LOCKDEP_INIT_MAP(a, b, c, d)
Nick Piggin2dc91ab2010-08-18 04:37:37 +100034#endif
35
Andi Kleeneea62f82012-05-08 13:32:24 +093036struct lglock {
37 arch_spinlock_t __percpu *lock;
38#ifdef CONFIG_DEBUG_LOCK_ALLOC
39 struct lock_class_key lock_key;
40 struct lockdep_map lock_dep_map;
41#endif
42};
Nick Piggin2dc91ab2010-08-18 04:37:37 +100043
44#define DEFINE_LGLOCK(name) \
Lai Jiangshan466cab82012-10-09 14:49:51 -070045 static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
Andi Kleeneea62f82012-05-08 13:32:24 +093046 = __ARCH_SPIN_LOCK_UNLOCKED; \
47 struct lglock name = { .lock = &name ## _lock }
48
Lai Jiangshan4b2c551f2012-10-09 14:49:54 -070049#define DEFINE_STATIC_LGLOCK(name) \
50 static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
51 = __ARCH_SPIN_LOCK_UNLOCKED; \
52 static struct lglock name = { .lock = &name ## _lock }
53
Andi Kleeneea62f82012-05-08 13:32:24 +093054void lg_lock_init(struct lglock *lg, char *name);
Peter Zijlstrab17718d2015-06-05 17:30:23 +020055
Andi Kleeneea62f82012-05-08 13:32:24 +093056void lg_local_lock(struct lglock *lg);
57void lg_local_unlock(struct lglock *lg);
58void lg_local_lock_cpu(struct lglock *lg, int cpu);
59void lg_local_unlock_cpu(struct lglock *lg, int cpu);
Peter Zijlstrab17718d2015-06-05 17:30:23 +020060
61void lg_double_lock(struct lglock *lg, int cpu1, int cpu2);
62void lg_double_unlock(struct lglock *lg, int cpu1, int cpu2);
63
Andi Kleeneea62f82012-05-08 13:32:24 +093064void lg_global_lock(struct lglock *lg);
65void lg_global_unlock(struct lglock *lg);
66
Josh Triplett64b47e82014-04-07 15:39:45 -070067#else
68/* When !CONFIG_SMP, map lglock to spinlock */
69#define lglock spinlock
70#define DEFINE_LGLOCK(name) DEFINE_SPINLOCK(name)
71#define DEFINE_STATIC_LGLOCK(name) static DEFINE_SPINLOCK(name)
72#define lg_lock_init(lg, name) spin_lock_init(lg)
73#define lg_local_lock spin_lock
74#define lg_local_unlock spin_unlock
75#define lg_local_lock_cpu(lg, cpu) spin_lock(lg)
76#define lg_local_unlock_cpu(lg, cpu) spin_unlock(lg)
77#define lg_global_lock spin_lock
78#define lg_global_unlock spin_unlock
79#endif
80
Nick Piggin2dc91ab2010-08-18 04:37:37 +100081#endif