Skip to contentSkip to conversation

lockdep: LD_PRELOAD support

PATCH Superseded Sasha Levin 4 participants 20 messages Feb 6, 2013 – Feb 8, 2013 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover lockdep: LD_PRELOAD support

This patch series adds in LD_PRELOAD support for liblockdep.

We store lockdep_map in an rb-tree and hook the pthread_mutex/ pthread_rwlock calls, allowing us to add lockdep testing on any program without touching it's source code.

The first couple of patches remove the need for lockdep_init and lockdep_set_thread, as well as some fixes for several tests.

The other patches add rbtree and LD_PRELOAD support, along with testing for that.

The last patch adds a small script that wraps all of it, making testing really simple:

liblockdep perf [perf command line]

There is a case where it won't work well: some programs, such as firefox, hook malloc() and add a pthread_mutex lock in the allocation path, which recurses back into our liblockdep code.

To solve that I'm planning to add a local cache to allocate from when liblockdep detects recursion onto itself, but that's outside of the scope of this patch.

Instead of taking the perf in this series another option would be to just revert the existing perf patch from me in core/locking, which would mean perf would just work with the new LD_PRELOAD feature.

Sasha Levin (11):
liblockdep: remove the need for liblockdep_init
liblockdep: remove the need for liblockdep_set_thread
perf: stop using liblockdep_init and liblockdep_set_thread
liblockdep: fix AA test
liblockdep: correct the ABCDBCDA test
liblockdep: rbtree support
liblockdep: prevent multiple declarations of CALLER_ADDR0
liblockdep: keep headers declarations even if lib is disabled
liblockdep: support using LD_PRELOAD
liblockdep: add tests for the LD_PRELOAD feature
liblockdep: preload helper

 tools/lib/lockdep/Makefile                         |  16 +-
 tools/lib/lockdep/common.c                         |  10 +-
 tools/lib/lockdep/include/liblockdep/common.h      |   5 +-
 tools/lib/lockdep/include/liblockdep/mutex.h       |   4 +-
 tools/lib/lockdep/include/liblockdep/rwlock.h      |   4 +-
 tools/lib/lockdep/lockdep                          |   3 +
 tools/lib/lockdep/preload.c                        | 184 +++++++++++++++++++++
 tools/lib/lockdep/rbtree.c                         |   1 +
 tools/lib/lockdep/run_tests.sh                     |  12 ++
 tools/lib/lockdep/tests/AA.c                       |   5 +-
 tools/lib/lockdep/tests/ABBA.c                     |   3 -
 tools/lib/lockdep/tests/ABBCCA.c                   |   3 -
 tools/lib/lockdep/tests/ABBCCDDA.c                 |   3 -
 tools/lib/lockdep/tests/ABCABC.c                   |   3 -
 tools/lib/lockdep/tests/ABCDBCDA.c                 |  13 +-
 tools/lib/lockdep/tests/ABCDBDDA.c                 |   3 -
 tools/lib/lockdep/tests/WW.c                       |   3 -
 tools/lib/lockdep/tests/unlock_balance.c           |   3 -
 tools/lib/lockdep/uinclude/linux/kernel.h          |   2 +
 tools/lib/lockdep/uinclude/linux/lockdep.h         |   5 +-
 tools/lib/lockdep/uinclude/linux/rbtree.h          |   1 +
 .../lib/lockdep/uinclude/linux/rbtree_augmented.h  |   2 +
 tools/perf/builtin-sched.c                         |   2 -
 tools/perf/builtin-top.c                           |   4 -
 tools/perf/config/feature-tests.mak                |   1 -
 tools/perf/perf.c                                  |   3 -
 tools/perf/util/liblockdep.h                       |   2 -
 27 files changed, 236 insertions(+), 64 deletions(-)
 create mode 100755 tools/lib/lockdep/lockdep
 create mode 100644 tools/lib/lockdep/preload.c
 create mode 100644 tools/lib/lockdep/rbtree.c
 create mode 100644 tools/lib/lockdep/uinclude/linux/rbtree.h
 create mode 100644 tools/lib/lockdep/uinclude/linux/rbtree_augmented.h
Signature · 1 line
1.8.1.2
#1 lore raw

PATCH 1/11 liblockdep: remove the need for liblockdep_init

Use a constructor in the library instead of making the user manually call liblockdep_init().

---
 tools/lib/lockdep/common.c                    | 2 +-
 tools/lib/lockdep/include/liblockdep/common.h | 1 -
 tools/lib/lockdep/tests/AA.c                  | 1 -
 tools/lib/lockdep/tests/ABBA.c                | 1 -
 tools/lib/lockdep/tests/ABBCCA.c              | 1 -
 tools/lib/lockdep/tests/ABBCCDDA.c            | 1 -
 tools/lib/lockdep/tests/ABCABC.c              | 1 -
 tools/lib/lockdep/tests/ABCDBCDA.c            | 1 -
 tools/lib/lockdep/tests/ABCDBDDA.c            | 1 -
 tools/lib/lockdep/tests/WW.c                  | 1 -
 tools/lib/lockdep/tests/unlock_balance.c      | 1 -
 tools/lib/lockdep/uinclude/linux/lockdep.h    | 1 -
 12 files changed, 1 insertion(+), 12 deletions(-)
tools/lib/lockdep/common.c +1 -1
index 99d3b4b..d2946e0 100644
@@ -10,7 +10,7 @@ __thread struct task_struct current_obj;
 bool debug_locks = true;
 bool debug_locks_silent;
 
-void liblockdep_init(void)
+__attribute__((constructor)) static void liblockdep_init(void)
 {
 	lockdep_init();
 }
tools/lib/lockdep/include/liblockdep/common.h +0 -1
index b72f9c1..f6f7fec8 100644
@@ -26,7 +26,6 @@ struct lockdep_map {
 #endif
 };
 
-void liblockdep_init(void);
 void liblockdep_set_thread(void);
 void lockdep_init_map(struct lockdep_map *lock, const char *name,
 			struct lock_class_key *key, int subclass);
tools/lib/lockdep/tests/AA.c +0 -1
index 933d32f..3f71333 100644
@@ -4,7 +4,6 @@ void main(void)
 {
 	pthread_mutex_t a, b;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_mutex_init(&a, NULL);
tools/lib/lockdep/tests/ABBA.c +0 -1
index 9f5146b..08d87a7 100644
@@ -5,7 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_mutex_init(&a, NULL);
tools/lib/lockdep/tests/ABBCCA.c +0 -1
index b7435d7..c5d0e5c 100644
@@ -5,7 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_mutex_init(&a, NULL);
tools/lib/lockdep/tests/ABBCCDDA.c +0 -1
index 2425330..e12dc98 100644
@@ -5,7 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c, d;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_mutex_init(&a, NULL);
tools/lib/lockdep/tests/ABCABC.c +0 -1
index 2ee30fe..70879c95 100644
@@ -5,7 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_mutex_init(&a, NULL);
tools/lib/lockdep/tests/ABCDBCDA.c +0 -1
index 32d19d6..00cd676 100644
@@ -5,7 +5,6 @@ void main(void)
 {
 	liblockdep_pthread_mutex_t a, b, c, d;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	liblockdep_pthread_mutex_init(&a, NULL);
tools/lib/lockdep/tests/ABCDBDDA.c +0 -1
index 850eaca..19b3ed1 100644
@@ -5,7 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c, d;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_mutex_init(&a, NULL);
tools/lib/lockdep/tests/WW.c +0 -1
index 4b1be0f..2467b13 100644
@@ -4,7 +4,6 @@ void main(void)
 {
 	pthread_rwlock_t a, b;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_rwlock_init(&a, NULL);
tools/lib/lockdep/tests/unlock_balance.c +0 -1
index 9dfaf97..07a4c21 100644
@@ -4,7 +4,6 @@ void main(void)
 {
 	pthread_mutex_t a;
 
-	liblockdep_init();
 	liblockdep_set_thread();
 
 	pthread_mutex_init(&a, NULL);
tools/lib/lockdep/uinclude/linux/lockdep.h +0 -1
index 1ed83a5..611cb28 100644
@@ -25,7 +25,6 @@ struct task_struct {
 extern __thread struct task_struct current_obj;
 #define current (&current_obj)
 
-void liblockdep_init(void);
 void liblockdep_set_thread(void);
 
 #define debug_locks_off() 1
Signature · 1 line
1.8.1.2
#2 lore raw

PATCH 2/11 liblockdep: remove the need for liblockdep_set_thread

In reply to Sasha Levin

Generate the task_struct data on the fly when needed, instead on on thread creation.

---
 tools/lib/lockdep/common.c                    | 8 +++++---
 tools/lib/lockdep/include/liblockdep/common.h | 1 -
 tools/lib/lockdep/tests/AA.c                  | 2 --
 tools/lib/lockdep/tests/ABBA.c                | 2 --
 tools/lib/lockdep/tests/ABBCCA.c              | 2 --
 tools/lib/lockdep/tests/ABBCCDDA.c            | 2 --
 tools/lib/lockdep/tests/ABCABC.c              | 2 --
 tools/lib/lockdep/tests/ABCDBCDA.c            | 2 --
 tools/lib/lockdep/tests/ABCDBDDA.c            | 2 --
 tools/lib/lockdep/tests/WW.c                  | 2 --
 tools/lib/lockdep/tests/unlock_balance.c      | 2 --
 tools/lib/lockdep/uinclude/linux/lockdep.h    | 4 ++--
 12 files changed, 7 insertions(+), 24 deletions(-)
tools/lib/lockdep/common.c +5 -3
index d2946e0..c0f1166 100644
@@ -15,8 +15,10 @@ __attribute__((constructor)) static void liblockdep_init(void)
 	lockdep_init();
 }
 
-void liblockdep_set_thread(void)
+struct task_struct *__curr(void)
 {
-	prctl(PR_GET_NAME, current->comm);
-	current->pid = syscall(__NR_gettid);
+	prctl(PR_GET_NAME, current_obj.comm);
+	current_obj.pid = syscall(__NR_gettid);
+
+	return &current_obj;
 }
tools/lib/lockdep/include/liblockdep/common.h +0 -1
index f6f7fec8..a675ef0 100644
@@ -26,7 +26,6 @@ struct lockdep_map {
 #endif
 };
 
-void liblockdep_set_thread(void);
 void lockdep_init_map(struct lockdep_map *lock, const char *name,
 			struct lock_class_key *key, int subclass);
 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
tools/lib/lockdep/tests/AA.c +0 -2
index 3f71333..a443e65 100644
@@ -4,8 +4,6 @@ void main(void)
 {
 	pthread_mutex_t a, b;
 
-	liblockdep_set_thread();
-
 	pthread_mutex_init(&a, NULL);
 	pthread_mutex_init(&b, NULL);
 
tools/lib/lockdep/tests/ABBA.c +0 -2
index 08d87a7..07f0e29 100644
@@ -5,8 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b;
 
-	liblockdep_set_thread();
-
 	pthread_mutex_init(&a, NULL);
 	pthread_mutex_init(&b, NULL);
 
tools/lib/lockdep/tests/ABBCCA.c +0 -2
index c5d0e5c..843db09 100644
@@ -5,8 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c;
 
-	liblockdep_set_thread();
-
 	pthread_mutex_init(&a, NULL);
 	pthread_mutex_init(&b, NULL);
 	pthread_mutex_init(&c, NULL);
tools/lib/lockdep/tests/ABBCCDDA.c +0 -2
index e12dc98..33620e2 100644
@@ -5,8 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c, d;
 
-	liblockdep_set_thread();
-
 	pthread_mutex_init(&a, NULL);
 	pthread_mutex_init(&b, NULL);
 	pthread_mutex_init(&c, NULL);
tools/lib/lockdep/tests/ABCABC.c +0 -2
index 70879c95..3fee51e 100644
@@ -5,8 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c;
 
-	liblockdep_set_thread();
-
 	pthread_mutex_init(&a, NULL);
 	pthread_mutex_init(&b, NULL);
 	pthread_mutex_init(&c, NULL);
tools/lib/lockdep/tests/ABCDBCDA.c +0 -2
index 00cd676..aadf11e 100644
@@ -5,8 +5,6 @@ void main(void)
 {
 	liblockdep_pthread_mutex_t a, b, c, d;
 
-	liblockdep_set_thread();
-
 	liblockdep_pthread_mutex_init(&a, NULL);
 	liblockdep_pthread_mutex_init(&b, NULL);
 	liblockdep_pthread_mutex_init(&c, NULL);
tools/lib/lockdep/tests/ABCDBDDA.c +0 -2
index 19b3ed1..680c6cf 100644
@@ -5,8 +5,6 @@ void main(void)
 {
 	pthread_mutex_t a, b, c, d;
 
-	liblockdep_set_thread();
-
 	pthread_mutex_init(&a, NULL);
 	pthread_mutex_init(&b, NULL);
 	pthread_mutex_init(&c, NULL);
tools/lib/lockdep/tests/WW.c +0 -2
index 2467b13..d44f77d 100644
@@ -4,8 +4,6 @@ void main(void)
 {
 	pthread_rwlock_t a, b;
 
-	liblockdep_set_thread();
-
 	pthread_rwlock_init(&a, NULL);
 	pthread_rwlock_init(&b, NULL);
 
tools/lib/lockdep/tests/unlock_balance.c +0 -2
index 07a4c21..0bc62de 100644
@@ -4,8 +4,6 @@ void main(void)
 {
 	pthread_mutex_t a;
 
-	liblockdep_set_thread();
-
 	pthread_mutex_init(&a, NULL);
 
 	pthread_mutex_lock(&a);
tools/lib/lockdep/uinclude/linux/lockdep.h +2 -2
index 611cb28..52609c5 100644
@@ -23,9 +23,9 @@ struct task_struct {
 };
 
 extern __thread struct task_struct current_obj;
-#define current (&current_obj)
+extern struct task_struct *__curr(void);
 
-void liblockdep_set_thread(void);
+#define current (__curr())
 
 #define debug_locks_off() 1
 #define task_pid_nr(tsk) ((tsk)->pid)
Signature · 1 line
1.8.1.2
#3 lore raw

PATCH 3/11 perf: stop using liblockdep_init and liblockdep_set_thread

In reply to Sasha Levin

These functions are no longer needed by liblockdep, drop them.

---
 tools/perf/builtin-sched.c          | 2 --
 tools/perf/builtin-top.c            | 4 ----
 tools/perf/config/feature-tests.mak | 1 -
 tools/perf/perf.c                   | 3 ---
 tools/perf/util/liblockdep.h        | 2 --
 5 files changed, 12 deletions(-)
tools/perf/builtin-sched.c +0 -2
index 53d9225..e1f0c44 100644
@@ -468,8 +468,6 @@ static void *thread_func(void *ctx)
 	char comm2[22];
 	int fd;
 
-	liblockdep_set_thread();
-
 	free(parms);
 
 	sprintf(comm2, ":%s", this_task->comm);
tools/perf/builtin-top.c +0 -4
index c9b99ef..c9ff395 100644
@@ -588,8 +588,6 @@ static void *display_thread_tui(void *arg)
 		.refresh	= top->delay_secs,
 	};
 
-	liblockdep_set_thread();
-
 	perf_top__sort_new_samples(top);
 
 	/*
@@ -615,8 +613,6 @@ static void *display_thread(void *arg)
 	struct perf_top *top = arg;
 	int delay_msecs, c;
 
-	liblockdep_set_thread();
-
 	tcgetattr(0, &save);
 	tc = save;
 	tc.c_lflag &= ~(ICANON | ECHO);
tools/perf/config/feature-tests.mak +0 -1
index 1f5a37e..20f8f7b 100644
@@ -223,7 +223,6 @@ define SOURCE_LIBLOCKDEP
 
 int main(void)
 {
-	liblockdep_init();
 	return 0;
 }
 endef
tools/perf/perf.c +0 -3
index ddbd315..0f661fb 100644
@@ -446,9 +446,6 @@ int main(int argc, const char **argv)
 {
 	const char *cmd;
 
-	liblockdep_init();
-	liblockdep_set_thread();
-
 	page_size = sysconf(_SC_PAGE_SIZE);
 
 	cmd = perf_extract_argv0_path(argv[0]);
tools/perf/util/liblockdep.h +0 -2
index 628a2f5..798263f 100644
@@ -5,7 +5,5 @@
 #else
 
 #define LIBLOCKDEP_PTHREAD_MUTEX_INITIALIZER(mtx) PTHREAD_MUTEX_INITIALIZER
-#define liblockdep_init()
-#define liblockdep_set_thread()
 
 #endif
Signature · 1 line
1.8.1.2
#4 lore raw

PATCH 4/11 liblockdep: fix AA test

In reply to Sasha Levin

We were accidently unlocking the dummy mutex in the test, which meant that we were testing unlock balance instead of AA deadlock.

---
 tools/lib/lockdep/tests/AA.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/lockdep/tests/AA.c +1 -1
index a443e65..0f782ff 100644
@@ -8,6 +8,6 @@ void main(void)
 	pthread_mutex_init(&b, NULL);
 
 	pthread_mutex_lock(&a);
-	pthread_mutex_unlock(&b);
+	pthread_mutex_lock(&b);
 	pthread_mutex_lock(&a);
 }
Signature · 1 line
1.8.1.2
#5 lore raw

PATCH 5/11 liblockdep: correct the ABCDBCDA test

In reply to Sasha Levin

There's no need to use liblockdep specific calls, they are wrapped for us.

---
 tools/lib/lockdep/tests/ABCDBCDA.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
tools/lib/lockdep/tests/ABCDBCDA.c +5 -5
index aadf11e..427ba56 100644
@@ -3,12 +3,12 @@
 
 void main(void)
 {
-	liblockdep_pthread_mutex_t a, b, c, d;
+	pthread_mutex_t a, b, c, d;
 
-	liblockdep_pthread_mutex_init(&a, NULL);
-	liblockdep_pthread_mutex_init(&b, NULL);
-	liblockdep_pthread_mutex_init(&c, NULL);
-	liblockdep_pthread_mutex_init(&d, NULL);
+	pthread_mutex_init(&a, NULL);
+	pthread_mutex_init(&b, NULL);
+	pthread_mutex_init(&c, NULL);
+	pthread_mutex_init(&d, NULL);
 
 	LOCK_UNLOCK_2(a, b);
 	LOCK_UNLOCK_2(c, d);
Signature · 1 line
1.8.1.2
#6 lore raw

PATCH 6/11 liblockdep: rbtree support

In reply to Sasha Levin

We re-use kernel's rbtree structure for the preload improvement in the following patches.

---
 tools/lib/lockdep/rbtree.c                          | 1 +
 tools/lib/lockdep/uinclude/linux/rbtree.h           | 1 +
 tools/lib/lockdep/uinclude/linux/rbtree_augmented.h | 2 ++
 3 files changed, 4 insertions(+)
 create mode 100644 tools/lib/lockdep/rbtree.c
 create mode 100644 tools/lib/lockdep/uinclude/linux/rbtree.h
 create mode 100644 tools/lib/lockdep/uinclude/linux/rbtree_augmented.h
tools/lib/lockdep/rbtree.c new file +1 -0
new file mode 100644
index 0000000..f7f4303
@@ -0,0 +1 @@
+#include "../../../lib/rbtree.c"
tools/lib/lockdep/uinclude/linux/rbtree.h new file +1 -0
new file mode 100644
index 0000000..965901d
@@ -0,0 +1 @@
+#include "../../../include/linux/rbtree.h"
tools/lib/lockdep/uinclude/linux/rbtree_augmented.h new file +2 -0
new file mode 100644
index 0000000..c375947
@@ -0,0 +1,2 @@
+#define __always_inline
+#include "../../../include/linux/rbtree_augmented.h"
Signature · 1 line
1.8.1.2
#7 lore raw

PATCH 7/11 liblockdep: prevent multiple declarations of CALLER_ADDR0

In reply to Sasha Levin

They are declared once in the public and once in the private headers, prevent declaring them twice if both headers are used.

---
 tools/lib/lockdep/include/liblockdep/common.h | 3 +++
 tools/lib/lockdep/uinclude/linux/kernel.h     | 2 ++
 2 files changed, 5 insertions(+)
tools/lib/lockdep/include/liblockdep/common.h +3 -0
index a675ef0..1bad66c 100644
@@ -3,8 +3,11 @@
 
 #include <pthread.h>
 
+#ifndef CALLER_ADDR0
 #define CALLER_ADDR0 (__builtin_return_address(0))
 #define _THIS_IP_ CALLER_ADDR0
+#endif
+
 #define NR_LOCKDEP_CACHING_CLASSES 2
 #define MAX_LOCKDEP_SUBCLASSES 8UL
 
tools/lib/lockdep/uinclude/linux/kernel.h +2 -0
index da97ce8..af02ac1 100644
@@ -28,7 +28,9 @@
 #define noinline
 #define list_add_tail_rcu list_add_tail
 
+#ifndef CALLER_ADDR0
 #define _THIS_IP_ CALLER_ADDR0
 #define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
+#endif
 
 #endif
Signature · 1 line
1.8.1.2
#8 lore raw

PATCH 8/11 liblockdep: keep headers declarations even if lib is disabled

In reply to Sasha Levin

We need the public headers in the core module code for the preload thing.

---
 tools/lib/lockdep/include/liblockdep/mutex.h  | 4 ++--
 tools/lib/lockdep/include/liblockdep/rwlock.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/lib/lockdep/include/liblockdep/mutex.h +2 -2
index 5154a9d..6ebe733 100644
@@ -1,8 +1,6 @@
 #ifndef _LIBLOCKDEP_MUTEX_H
 #define _LIBLOCKDEP_MUTEX_H
 
-#ifdef __USE_LIBLOCKDEP
-
 #include <pthread.h>
 #include "common.h"
 
@@ -61,6 +59,8 @@ static inline int liblockdep_pthread_mutex_destroy(liblockdep_pthread_mutex_t *l
 	return pthread_mutex_destroy(&lock->mutex);
 }
 
+#ifdef __USE_LIBLOCKDEP
+
 #define pthread_mutex_t         liblockdep_pthread_mutex_t
 #define pthread_mutex_init      liblockdep_pthread_mutex_init
 #define pthread_mutex_lock      liblockdep_pthread_mutex_lock
tools/lib/lockdep/include/liblockdep/rwlock.h +2 -2
index 26f9a68..a1d820b 100644
@@ -1,8 +1,6 @@
 #ifndef _LIBLOCKDEP_RWLOCK_H
 #define _LIBLOCKDEP_RWLOCK_H
 
-#ifdef __USE_LIBLOCKDEP
-
 #include <pthread.h>
 #include "common.h"
 
@@ -77,6 +75,8 @@ static inline int liblockdep_rwlock_destroy(liblockdep_pthread_rwlock_t *lock)
 	return pthread_rwlock_destroy(&lock->rwlock);
 }
 
+#ifdef __USE_LIBLOCKDEP
+
 #define pthread_rwlock_t		liblockdep_pthread_rwlock_t
 #define pthread_rwlock_init		liblockdep_pthread_rwlock_init
 #define pthread_rwlock_rdlock		liblockdep_pthread_rwlock_rdlock
Signature · 1 line
1.8.1.2
#9 lore raw

PATCH 9/11 liblockdep: support using LD_PRELOAD

In reply to Sasha Levin

This allows lockdep to be used without being compiled in the original program.

Usage is quite simple:

LD_PRELOAD=/path/to/liblockdep.so /path/to/my/program

And magically, you'll have lockdep in your program!

---
 tools/lib/lockdep/Makefile  |   4 +-
 tools/lib/lockdep/preload.c | 184 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 186 insertions(+), 2 deletions(-)
 create mode 100644 tools/lib/lockdep/preload.c
tools/lib/lockdep/Makefile +2 -2
index e82ffe9..245f8ba 100644
@@ -143,7 +143,7 @@ do_app_build =						\
 
 do_compile_shared_library =			\
 	($(print_shared_lib_compile)		\
-	$(CC) --shared $^ -o $@)
+	$(CC) --shared -ldl $^ -o $@)
 
 do_build_static_lib =				\
 	($(print_static_lib_build)		\
@@ -161,7 +161,7 @@ $(obj)/%.o: $(src)/%.c
 %.o: $(src)/%.c
 	$(Q)$(call do_compile)
 
-PEVENT_LIB_OBJS = common.o lockdep.o
+PEVENT_LIB_OBJS = common.o lockdep.o preload.o rbtree.o
 
 ALL_OBJS = $(PEVENT_LIB_OBJS)
 
tools/lib/lockdep/preload.c new file +184 -0

184 lines not on this page · raw message

Signature · 1 line
1.8.1.2
#10 lore raw

Re: [PATCH 09/11] liblockdep: support using LD_PRELOAD

Hi Sasha,

Quoted 15 lines · Sasha Levin

This allows lockdep to be used without being compiled in the original program.

Usage is quite simple:

LD_PRELOAD=/path/to/liblockdep.so /path/to/my/program

And magically, you'll have lockdep in your program!

---

tools/lib/lockdep/preload.c new file +0 -0
new file mode 100644
index 0000000..0f71c23

...

tools/lib/lockdep/preload.c line 114

Quoted 5 lines
tools/lib/lockdep/preload.c new file +5 -0
+int pthread_rwlock_init(pthread_rwlock_t *rwlock,
+			const pthread_rwlockattr_t *attr)
+{
+	if (ll_pthread_rwlock_init == NULL)
+		init_preload();

Why is this one special, doesn't init_preload being a constructor make this redundant?

Jamie

#11 lore raw

Re: [PATCH 09/11] liblockdep: support using LD_PRELOAD

Quoted 7 lines · Jamie Iles
Quoted 5 lines

+int pthread_rwlock_init(pthread_rwlock_t *rwlock,

tools/lib/lockdep/preload.c line 114

Quoted 4 lines
tools/lib/lockdep/preload.c new file +4 -0
+			const pthread_rwlockattr_t *attr)
+{
+	if (ll_pthread_rwlock_init == NULL)
+		init_preload();

Why is this one special, doesn't init_preload being a constructor make this redundant?

I was testing it on different things, and stumbled on an interesting case:
when pthread_mutex was taken from the constructor of a different module.

In that case, the other constructor would try to init the mutex and take a lock, but we would segfault because we haven't resolved the pthread symbols yet ourselves (since our constructor was yet to be called).

Thanks,
Sasha

#12 lore raw

Re: [PATCH 09/11] liblockdep: support using LD_PRELOAD

Quoted 9 lines · Sasha Levin
Quoted 7 lines · Jamie Iles
Quoted 5 lines

+int pthread_rwlock_init(pthread_rwlock_t *rwlock,

tools/lib/lockdep/preload.c line 114

Quoted 4 lines
tools/lib/lockdep/preload.c new file +4 -0
+			const pthread_rwlockattr_t *attr)
+{
+	if (ll_pthread_rwlock_init == NULL)
+		init_preload();

Why is this one special, doesn't init_preload being a constructor make this redundant?

Quoted 6 lines

I was testing it on different things, and stumbled on an interesting case:
when pthread_mutex was taken from the constructor of a different module.

In that case, the other constructor would try to init the mutex and take a lock, but we would segfault because we haven't resolved the pthread symbols yet ourselves (since our constructor was yet to be called).

Okay, that makes sense, but shouldn't we do this for all of the lock operations? pthread locks can be statically initialized and they are initializaed lazily on the first access so I think that this could happen on any of the lock operations.

Jamie

#13 lore raw

Re: [PATCH 09/11] liblockdep: support using LD_PRELOAD

Quoted 13 lines · Jamie Iles
Quoted 12 lines · Sasha Levin
Quoted 7 lines · Jamie Iles
Quoted 5 lines

+int pthread_rwlock_init(pthread_rwlock_t *rwlock,

tools/lib/lockdep/preload.c line 114

Quoted 4 lines
tools/lib/lockdep/preload.c new file +4 -0
+			const pthread_rwlockattr_t *attr)
+{
+	if (ll_pthread_rwlock_init == NULL)
+		init_preload();

Why is this one special, doesn't init_preload being a constructor make this redundant?

I was testing it on different things, and stumbled on an interesting case:
when pthread_mutex was taken from the constructor of a different module.

Quoted 8 lines
Quoted 3 lines

In that case, the other constructor would try to init the mutex and take a lock, but we would segfault because we haven't resolved the pthread symbols yet ourselves (since our constructor was yet to be called).

Okay, that makes sense, but shouldn't we do this for all of the lock operations? pthread locks can be statically initialized and they are initializaed lazily on the first access so I think that this could happen on any of the lock operations.

hmm... I've had it only in init() because I thought it doesn't make sense to actually lock/unlock in constructor code, but yeah - better safe than sorry.

Thanks,
Sasha

#14 lore raw

PATCH 10/11 liblockdep: add tests for the LD_PRELOAD feature

In reply to Sasha Levin

Use same tests, but without compiling with liblockdep in the first place.

---
 tools/lib/lockdep/run_tests.sh | 12 ++++++++++++
 1 file changed, 12 insertions(+)
tools/lib/lockdep/run_tests.sh +12 -0
index 4dd32d1..211e91d 100755
@@ -13,3 +13,15 @@ for i in `ls tests/*.c`; do
 	fi
 	rm tests/$testname
 done
+
+for i in `ls tests/*.c`; do
+	testname=$(basename -s .c "$i")
+	gcc -o tests/$testname -lpthread -Iinclude $i &> /dev/null
+	echo -ne "(PRELOAD) $testname... "
+	if [ $(LD_PRELOAD=./liblockdep.so timeout 1 ./tests/$testname | wc -l) -gt 0 ]; then
+		echo "PASSED!"
+	else
+		echo "FAILED!"
+	fi
+	rm tests/$testname
+done
Signature · 1 line
1.8.1.2
#15 lore raw

PATCH 11/11 liblockdep: preload helper

In reply to Sasha Levin

This is a simple wrapper to make using liblockdep on existing applications much easier.

After running 'make && make install', it becomes quite simple to test things with liblockdep. For example, to try it on perf:

liblockdep perf

No other integration required.

---
 tools/lib/lockdep/Makefile | 12 ++++++++----
 tools/lib/lockdep/lockdep  |  3 +++
 2 files changed, 11 insertions(+), 4 deletions(-)
 create mode 100755 tools/lib/lockdep/lockdep
tools/lib/lockdep/Makefile +8 -4
index 245f8ba..b22122f 100644
@@ -34,7 +34,9 @@ DESTDIR ?=
 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
 
 prefix ?= /usr/local
-bindir_relative = lib
+libdir_relative = lib
+libdir = $(prefix)/$(libdir_relative)
+bindir_relative = bin
 bindir = $(prefix)/$(bindir_relative)
 
 export DESTDIR DESTDIR_SQ INSTALL
@@ -90,13 +92,14 @@ objtree		:= $(CURDIR)
 src		:= $(srctree)
 obj		:= $(objtree)
 
-export prefix bindir src obj
+export prefix libdir bindir src obj
 
 # Shell quotes
+libdir_SQ = $(subst ','\'',$(libdir))
 bindir_SQ = $(subst ','\'',$(bindir))
-bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
 
 LIB_FILE = liblockdep.a liblockdep.so
+BIN_FILE = lockdep
 
 CONFIG_INCLUDES =
 CONFIG_LIBS	=
@@ -229,7 +232,8 @@ define do_install
 endef
 
 install_lib: all_cmd
-	$(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ))
+	$(Q)$(call do_install,$(LIB_FILE),$(libdir_SQ))
+	$(Q)$(call do_install,$(BIN_FILE),$(bindir_SQ))
 
 install: install_lib
 
tools/lib/lockdep/lockdep new file +3 -0
new file mode 100755
index 0000000..616bf9a
@@ -0,0 +1,3 @@
+#! /bin/bash
+
+LD_PRELOAD=liblockdep.so "$@"
Signature · 1 line
1.8.1.2
maintainer
#16 lore raw

Re: [PATCH 11/11] liblockdep: preload helper

Hi Sasha,

Quoted 7 lines · Sasha Levin

This is a simple wrapper to make using liblockdep on existing applications much easier.

After running 'make && make install', it becomes quite simple to test things with liblockdep. For example, to try it on perf:

liblockdep perf

Shouldn't it be 'lockdep perf ...'?

Thanks,
Namhyung

Quoted 61 lines

No other integration required.

---
 tools/lib/lockdep/Makefile | 12 ++++++++----
 tools/lib/lockdep/lockdep  |  3 +++
 2 files changed, 11 insertions(+), 4 deletions(-)
 create mode 100755 tools/lib/lockdep/lockdep
tools/lib/lockdep/Makefile +8 -4
index 245f8ba..b22122f 100644
@@ -34,7 +34,9 @@ DESTDIR ?=
 DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))'
 
 prefix ?= /usr/local
-bindir_relative = lib
+libdir_relative = lib
+libdir = $(prefix)/$(libdir_relative)
+bindir_relative = bin
 bindir = $(prefix)/$(bindir_relative)
 
 export DESTDIR DESTDIR_SQ INSTALL
@@ -90,13 +92,14 @@ objtree		:= $(CURDIR)
 src		:= $(srctree)
 obj		:= $(objtree)
 
-export prefix bindir src obj
+export prefix libdir bindir src obj
 
 # Shell quotes
+libdir_SQ = $(subst ','\'',$(libdir))
 bindir_SQ = $(subst ','\'',$(bindir))
-bindir_relative_SQ = $(subst ','\'',$(bindir_relative))
 
 LIB_FILE = liblockdep.a liblockdep.so
+BIN_FILE = lockdep
 
 CONFIG_INCLUDES =
 CONFIG_LIBS	=
@@ -229,7 +232,8 @@ define do_install
 endef
 
 install_lib: all_cmd
-	$(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ))
+	$(Q)$(call do_install,$(LIB_FILE),$(libdir_SQ))
+	$(Q)$(call do_install,$(BIN_FILE),$(bindir_SQ))
 
 install: install_lib
 
tools/lib/lockdep/lockdep new file +3 -0
new file mode 100755
index 0000000..616bf9a
@@ -0,0 +1,3 @@
+#! /bin/bash
+
+LD_PRELOAD=liblockdep.so "$@"
maintainer
#17 lore raw

Re: [PATCH 11/11] liblockdep: preload helper

In reply to Sasha Levin

tools/lib/lockdep/lockdep line 3

Quoted 9 lines · Sasha Levin
tools/lib/lockdep/lockdep new file +3 -0
new file mode 100755
index 0000000..616bf9a
@@ -0,0 +1,3 @@
+#! /bin/bash
+
+LD_PRELOAD=liblockdep.so "$@"

Just try to be more conservative ;-)

LD_PRELOAD="liblockdep.so $LD_PRELOAD" "$@"

Thanks,
Namhyung

#18 lore raw

Re: [PATCH 11/11] liblockdep: preload helper

Quoted 13 lines · Namhyung Kim

tools/lib/lockdep/lockdep line 3

Quoted 9 lines · Sasha Levin
tools/lib/lockdep/lockdep new file +3 -0
new file mode 100755
index 0000000..616bf9a
@@ -0,0 +1,3 @@
+#! /bin/bash
+
+LD_PRELOAD=liblockdep.so "$@"

Just try to be more conservative ;-)

Quoted 1 line

LD_PRELOAD="liblockdep.so $LD_PRELOAD" "$@"

Will do, thanks!

And yes, it's supposed to be 'lockdep perf' :

Thanks,
Sasha