[RFC 3/6] lockdep: LOCKED_ACCESS: Maintain the keys of acqchains

From: Boqun Feng
Date: Wed Feb 03 2016 - 11:47:33 EST


Add held_lock::prev_acqchain_key and task_struct::curr_acqchain_key to
maintain the keys of acqchains as the same as what lockdep does for
keys of lock classes.

Signed-off-by: Boqun Feng <boqun.feng@xxxxxxxxx>
---
include/linux/lockdep.h | 3 +++
include/linux/sched.h | 3 +++
kernel/fork.c | 3 +++
kernel/locking/lockdep.c | 31 +++++++++++++++++++++++++++++++
4 files changed, 40 insertions(+)

diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
index 140c1c3..2ffe6c3 100644
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -244,6 +244,9 @@ struct held_lock {
* with zero), here we store the previous hash value:
*/
u64 prev_chain_key;
+#ifdef CONFIG_LOCKED_ACCESS
+ u64 prev_acqchain_key;
+#endif
unsigned long acquire_ip;
struct lockdep_map *instance;
struct lockdep_map *nest_lock;
diff --git a/include/linux/sched.h b/include/linux/sched.h
index a10494a..c24348e 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1643,6 +1643,9 @@ struct task_struct {
unsigned int lockdep_recursion;
struct held_lock held_locks[MAX_LOCK_DEPTH];
gfp_t lockdep_reclaim_gfp;
+#ifdef CONFIG_LOCKED_ACCESS
+ u64 curr_acqchain_key;
+#endif
#endif
#ifdef CONFIG_UBSAN
unsigned int in_ubsan;
diff --git a/kernel/fork.c b/kernel/fork.c
index 2e391c7..d9fc51b 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1404,6 +1404,9 @@ static struct task_struct *copy_process(unsigned long clone_flags,
p->lockdep_depth = 0; /* no locks held yet */
p->curr_chain_key = 0;
p->lockdep_recursion = 0;
+# ifdef CONFIG_LOCKED_ACCESS
+ p->curr_acqchain_key = 0;
+# endif
#endif

#ifdef CONFIG_DEBUG_MUTEXES
diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index 3aff961..77732a9 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -3096,6 +3096,9 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
int chain_head = 0;
int class_idx;
u64 chain_key;
+#ifdef CONFIG_LOCKED_ACCESS
+ u64 acqchain_key;
+#endif

if (unlikely(!debug_locks))
return 0;
@@ -3203,6 +3206,9 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
return 0;

chain_key = curr->curr_chain_key;
+#ifdef CONFIG_LOCKED_ACCESS
+ acqchain_key = curr->curr_acqchain_key;
+#endif
if (!depth) {
/*
* How can we have a chain hash when we ain't got no keys?!
@@ -3213,12 +3219,23 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
}

hlock->prev_chain_key = chain_key;
+#ifdef CONFIG_LOCKED_ACCESS
+ hlock->prev_acqchain_key = acqchain_key;
+#endif
if (separate_irq_context(curr, hlock)) {
chain_key = 0;
+
+#ifdef CONFIG_LOCKED_ACCESS
+ acqchain_key = 0;
+#endif
+
chain_head = 1;
}
chain_key = iterate_chain_key(chain_key, id);

+#ifdef CONFIG_LOCKED_ACCESS
+ acqchain_key = iterate_acqchain_key(acqchain_key, ip);
+#endif
if (nest_lock && !__lock_is_held(nest_lock))
return print_lock_nested_lock_not_held(curr, hlock, ip);

@@ -3226,6 +3243,9 @@ static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
return 0;

curr->curr_chain_key = chain_key;
+#ifdef CONFIG_LOCKED_ACCESS
+ curr->curr_acqchain_key = acqchain_key;
+#endif
curr->lockdep_depth++;
check_chain_key(curr);
#ifdef CONFIG_DEBUG_LOCKDEP
@@ -3355,6 +3375,9 @@ found_it:

curr->lockdep_depth = i;
curr->curr_chain_key = hlock->prev_chain_key;
+#ifdef CONFIG_LOCKED_ACCESS
+ curr->curr_acqchain_key = hlock->prev_acqchain_key;
+#endif

for (; i < depth; i++) {
hlock = curr->held_locks + i;
@@ -3445,6 +3468,9 @@ found_it:

curr->lockdep_depth = i;
curr->curr_chain_key = hlock->prev_chain_key;
+#ifdef CONFIG_LOCKED_ACCESS
+ curr->curr_acqchain_key = hlock->prev_acqchain_key;
+#endif

for (i++; i < depth; i++) {
hlock = curr->held_locks + i;
@@ -3886,6 +3912,11 @@ void lockdep_reset(void)

raw_local_irq_save(flags);
current->curr_chain_key = 0;
+
+#ifdef CONFIG_LOCKED_ACCESS
+ current->curr_acqchain_key = 0;
+#endif
+
current->lockdep_depth = 0;
current->lockdep_recursion = 0;
memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
--
2.7.0