blob: 0d24e932db0b62c97337069985f794276a455b83 [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
28/* can make br locks by using local lock for read side, global lock for write */
Andi Kleeneea62f82012-05-08 13:32:24 +093029#define br_lock_init(name) lg_lock_init(name, #name)
30#define br_read_lock(name) lg_local_lock(name)
31#define br_read_unlock(name) lg_local_unlock(name)
32#define br_write_lock(name) lg_global_lock(name)
33#define br_write_unlock(name) lg_global_unlock(name)
Nick Piggin2dc91ab2010-08-18 04:37:37 +100034
Lai Jiangshan4b2c551f2012-10-09 14:49:54 -070035#define DEFINE_BRLOCK(name) DEFINE_LGLOCK(name)
36#define DEFINE_STATIC_BRLOCK(name) DEFINE_STATIC_LGLOCK(name)
Nick Piggin2dc91ab2010-08-18 04:37:37 +100037
Nick Piggin2dc91ab2010-08-18 04:37:37 +100038#ifdef CONFIG_DEBUG_LOCK_ALLOC
39#define LOCKDEP_INIT_MAP lockdep_init_map
Nick Piggin2dc91ab2010-08-18 04:37:37 +100040#else
41#define LOCKDEP_INIT_MAP(a, b, c, d)
Nick Piggin2dc91ab2010-08-18 04:37:37 +100042#endif
43
Andi Kleeneea62f82012-05-08 13:32:24 +093044struct lglock {
45 arch_spinlock_t __percpu *lock;
46#ifdef CONFIG_DEBUG_LOCK_ALLOC
47 struct lock_class_key lock_key;
48 struct lockdep_map lock_dep_map;
49#endif
50};
Nick Piggin2dc91ab2010-08-18 04:37:37 +100051
52#define DEFINE_LGLOCK(name) \
Lai Jiangshan466cab82012-10-09 14:49:51 -070053 static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
Andi Kleeneea62f82012-05-08 13:32:24 +093054 = __ARCH_SPIN_LOCK_UNLOCKED; \
55 struct lglock name = { .lock = &name ## _lock }
56
Lai Jiangshan4b2c551f2012-10-09 14:49:54 -070057#define DEFINE_STATIC_LGLOCK(name) \
58 static DEFINE_PER_CPU(arch_spinlock_t, name ## _lock) \
59 = __ARCH_SPIN_LOCK_UNLOCKED; \
60 static struct lglock name = { .lock = &name ## _lock }
61
Andi Kleeneea62f82012-05-08 13:32:24 +093062void lg_lock_init(struct lglock *lg, char *name);
63void lg_local_lock(struct lglock *lg);
64void lg_local_unlock(struct lglock *lg);
65void lg_local_lock_cpu(struct lglock *lg, int cpu);
66void lg_local_unlock_cpu(struct lglock *lg, int cpu);
67void lg_global_lock(struct lglock *lg);
68void lg_global_unlock(struct lglock *lg);
69
Nick Piggin2dc91ab2010-08-18 04:37:37 +100070#endif