[PATCH] lockdep: add lock_class information to lock_chain and output it

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Huang, Ying
Date: Friday, June 20, 2008 - 1:39 am

=EF=BB=BFThis patch is not intended to be merged. Just hope it is useful fo=
r
somebody want to investigate kernel locking behavior. The simple
scripts attached with the mail can be used to draw class chain graph
via graphviz.

Best Regards,
Huang Ying
------------------------------------------------------------->
This patch records array of lock_class into lock_chain, and export
lock_chain information via /proc/lockdep_chains.

It is based on x86/master branch of git-x86 tree, and has been tested
on x86_64 platform.

Signed-off-by: Huang Ying <ying.huang@intel.com>

---
 include/linux/lockdep.h    |    3 +
 kernel/lockdep.c           |   38 +++++++++++++++++-
 kernel/lockdep_internals.h |    6 ++
 kernel/lockdep_proc.c      |   91 ++++++++++++++++++++++++++++++++++++++++=
+++++
 4 files changed, 135 insertions(+), 3 deletions(-)

--- a/kernel/lockdep.c
+++ b/kernel/lockdep.c
@@ -1463,7 +1463,14 @@ out_bug:
 }
=20
 unsigned long nr_lock_chains;
-static struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
+struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
+atomic_t nr_chain_hlocks;
+static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
+
+struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
+{
+	return lock_classes + chain_hlocks[chain->base + i];
+}
=20
 /*
  * Look up a dependency chain. If the key is not present yet then
@@ -1471,10 +1478,15 @@ static struct lock_chain lock_chains[MAX
  * validated. If the key is already hashed, return 0.
  * (On return with 1 graph_lock is held.)
  */
-static inline int lookup_chain_cache(u64 chain_key, struct lock_class *cla=
ss)
+static inline int lookup_chain_cache(struct task_struct *curr,
+				     struct held_lock *hlock,
+				     u64 chain_key)
 {
+	struct lock_class *class =3D hlock->class;
 	struct list_head *hash_head =3D chainhashentry(chain_key);
 	struct lock_chain *chain;
+	struct held_lock *hlock_curr, *hlock_next;
+	int i, j, n;
=20
 	if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
 		return 0;
@@ -1522,6 +1534,26 @@ cache_hit:
 	}
 	chain =3D lock_chains + nr_lock_chains++;
 	chain->chain_key =3D chain_key;
+	chain->irq_context =3D hlock->irq_context;
+	/* Find the first held_lock of current chain */
+	hlock_next =3D hlock;
+	for (i =3D curr->lockdep_depth - 1; i >=3D 0; i--) {
+		hlock_curr =3D curr->held_locks + i;
+		if (hlock_curr->irq_context !=3D hlock_next->irq_context)
+			break;
+		hlock_next =3D hlock;
+	}
+	i++;
+	chain->depth =3D curr->lockdep_depth + 1 - i;
+	n =3D atomic_add_return(chain->depth, &nr_chain_hlocks);
+	if (unlikely(n < MAX_LOCKDEP_CHAIN_HLOCKS)) {
+		chain->base =3D n - chain->depth;
+		for (j =3D 0; j < chain->depth - 1; j++, i++) {
+			int lock_id =3D curr->held_locks[i].class - lock_classes;
+			chain_hlocks[chain->base + j] =3D lock_id;
+		}
+		chain_hlocks[chain->base + j] =3D class - lock_classes;
+	}
 	list_add_tail_rcu(&chain->entry, hash_head);
 	debug_atomic_inc(&chain_lookup_misses);
 	inc_chains();
@@ -1543,7 +1575,7 @@ static int validate_chain(struct task_st
 	 * graph_lock for us)
 	 */
 	if (!hlock->trylock && (hlock->check =3D=3D 2) &&
-			lookup_chain_cache(chain_key, hlock->class)) {
+	    lookup_chain_cache(curr, hlock, chain_key)) {
 		/*
 		 * Check whether last held lock:
 		 *
--- a/include/linux/lockdep.h
+++ b/include/linux/lockdep.h
@@ -182,6 +182,9 @@ struct lock_list {
  * We record lock dependency chains, so that we can cache them:
  */
 struct lock_chain {
+	u8				irq_context;
+	u8				depth;
+	u16				base;
 	struct list_head		entry;
 	u64				chain_key;
 };
--- a/kernel/lockdep_proc.c
+++ b/kernel/lockdep_proc.c
@@ -178,6 +178,93 @@ static const struct file_operations proc
 	.release	=3D seq_release,
 };
=20
+static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
+{
+	struct lock_chain *chain;
+
+	(*pos)++;
+
+	if (v =3D=3D SEQ_START_TOKEN)
+		chain =3D m->private;
+	else {
+		chain =3D v;
+
+		if (*pos < nr_lock_chains)
+			chain =3D lock_chains + *pos;
+		else
+			chain =3D NULL;
+	}
+
+	return chain;
+}
+
+static void *lc_start(struct seq_file *m, loff_t *pos)
+{
+	if (*pos =3D=3D 0)
+		return SEQ_START_TOKEN;
+
+	if (*pos < nr_lock_chains)
+		return lock_chains + *pos;
+
+	return NULL;
+}
+
+static void lc_stop(struct seq_file *m, void *v)
+{
+}
+
+static int lc_show(struct seq_file *m, void *v)
+{
+	struct lock_chain *chain =3D v;
+	struct lock_class *class;
+	int i;
+
+	if (v =3D=3D SEQ_START_TOKEN) {
+		seq_printf(m, "all lock chains:\n");
+		return 0;
+	}
+
+	seq_printf(m, "irq_context: %d\n", chain->irq_context);
+
+	for (i =3D 0; i < chain->depth; i++) {
+		class =3D lock_chain_get_class(chain, i);
+		seq_printf(m, "[%p] ", class->key);
+		print_name(m, class);
+		seq_puts(m, "\n");
+	}
+	seq_puts(m, "\n");
+
+	return 0;
+}
+
+static const struct seq_operations lockdep_chains_ops =3D {
+	.start	=3D lc_start,
+	.next	=3D lc_next,
+	.stop	=3D lc_stop,
+	.show	=3D lc_show,
+};
+
+static int lockdep_chains_open(struct inode *inode, struct file *file)
+{
+	int res =3D seq_open(file, &lockdep_chains_ops);
+	if (!res) {
+		struct seq_file *m =3D file->private_data;
+
+		if (nr_lock_chains)
+			m->private =3D lock_chains;
+		else
+			m->private =3D NULL;
+	}
+	return res;
+}
+
+static const struct file_operations proc_lockdep_chains_operations =3D {
+	.open		=3D lockdep_chains_open,
+	.read		=3D seq_read,
+	.llseek		=3D seq_lseek,
+	.release	=3D seq_release,
+};
+
 static void lockdep_stats_debug_show(struct seq_file *m)
 {
 #ifdef CONFIG_DEBUG_LOCKDEP
@@ -294,6 +381,8 @@ static int lockdep_stats_show(struct seq
 #ifdef CONFIG_PROVE_LOCKING
 	seq_printf(m, " dependency chains:             %11lu [max: %lu]\n",
 			nr_lock_chains, MAX_LOCKDEP_CHAINS);
+	seq_printf(m, " dependency chain hlocks:       %11d [max: %lu]\n",
+			atomic_read(&nr_chain_hlocks), MAX_LOCKDEP_CHAIN_HLOCKS);
 #endif
=20
 #ifdef CONFIG_TRACE_IRQFLAGS
@@ -661,6 +750,8 @@ static const struct file_operations proc
 static int __init lockdep_proc_init(void)
 {
 	proc_create("lockdep", S_IRUSR, NULL, &proc_lockdep_operations);
+	proc_create("lockdep_chains", S_IRUSR, NULL,
+		    &proc_lockdep_chains_operations);
 	proc_create("lockdep_stats", S_IRUSR, NULL,
 		    &proc_lockdep_stats_operations);
=20
--- a/kernel/lockdep_internals.h
+++ b/kernel/lockdep_internals.h
@@ -23,6 +23,8 @@
 #define MAX_LOCKDEP_CHAINS_BITS	14
 #define MAX_LOCKDEP_CHAINS	(1UL << MAX_LOCKDEP_CHAINS_BITS)
=20
+#define MAX_LOCKDEP_CHAIN_HLOCKS (MAX_LOCKDEP_CHAINS*5)
+
 /*
  * Stack-trace: tightly packed array of stack backtrace
  * addresses. Protected by the hash_lock.
@@ -30,15 +32,19 @@
 #define MAX_STACK_TRACE_ENTRIES	262144UL
=20
 extern struct list_head all_lock_classes;
+extern struct lock_chain lock_chains[];
=20
 extern void
 get_usage_chars(struct lock_class *class, char *c1, char *c2, char *c3, ch=
ar *c4);
=20
 extern const char * __get_key_name(struct lockdep_subclass_key *key, char =
*str);
=20
+struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i);
+
 extern unsigned long nr_lock_classes;
 extern unsigned long nr_list_entries;
 extern unsigned long nr_lock_chains;
+extern atomic_t nr_chain_hlocks;
 extern unsigned long nr_stack_trace_entries;
=20
 extern unsigned int nr_hardirq_chains;
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] lockdep: add lock_class information to lock_chain ..., Huang, Ying, (Fri Jun 20, 1:39 am)