Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/lib/lockdep: Add tests for AA and ABBA locking

Add test for AA and 2 threaded ABBA locking.

Rename AA.c to ABA.c since it was implementing an ABA instead of a pure
AA. Now both cases are covered.

The expected output for AA.c is that the process blocks and lockdep
reports a deadlock.

ABBA_2threads.c differs from ABBA.c in that lockdep keeps separate chains
of held locks per task. This can lead to different behaviour regarding
lock detection. The expected output for this test is that the process
blocks and lockdep reports a circular locking dependency.

These tests found a lockdep bug - fixed by the next commit.

Signed-off-by: Alfredo Alvarez Fernandez <alfredoalvarezfernandez@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/1455864533-7536-3-git-send-email-alfredoalvarezernandez@gmail.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Alfredo Alvarez Fernandez and committed by
Ingo Molnar
11a1ac20 9d5a23ac

+63 -4
+4 -4
tools/lib/lockdep/tests/AA.c
··· 1 1 #include <liblockdep/mutex.h> 2 2 3 - void main(void) 3 + int main(void) 4 4 { 5 - pthread_mutex_t a, b; 5 + pthread_mutex_t a; 6 6 7 7 pthread_mutex_init(&a, NULL); 8 - pthread_mutex_init(&b, NULL); 9 8 10 9 pthread_mutex_lock(&a); 11 - pthread_mutex_lock(&b); 12 10 pthread_mutex_lock(&a); 11 + 12 + return 0; 13 13 }
+13
tools/lib/lockdep/tests/ABA.c
··· 1 + #include <liblockdep/mutex.h> 2 + 3 + void main(void) 4 + { 5 + pthread_mutex_t a, b; 6 + 7 + pthread_mutex_init(&a, NULL); 8 + pthread_mutex_init(&b, NULL); 9 + 10 + pthread_mutex_lock(&a); 11 + pthread_mutex_lock(&b); 12 + pthread_mutex_lock(&a); 13 + }
+46
tools/lib/lockdep/tests/ABBA_2threads.c
··· 1 + #include <stdio.h> 2 + #include <pthread.h> 3 + 4 + pthread_mutex_t a = PTHREAD_MUTEX_INITIALIZER; 5 + pthread_mutex_t b = PTHREAD_MUTEX_INITIALIZER; 6 + pthread_barrier_t bar; 7 + 8 + void *ba_lock(void *arg) 9 + { 10 + int ret, i; 11 + 12 + pthread_mutex_lock(&b); 13 + 14 + if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD) 15 + pthread_barrier_destroy(&bar); 16 + 17 + pthread_mutex_lock(&a); 18 + 19 + pthread_mutex_unlock(&a); 20 + pthread_mutex_unlock(&b); 21 + } 22 + 23 + int main(void) 24 + { 25 + pthread_t t; 26 + 27 + pthread_barrier_init(&bar, NULL, 2); 28 + 29 + if (pthread_create(&t, NULL, ba_lock, NULL)) { 30 + fprintf(stderr, "pthread_create() failed\n"); 31 + return 1; 32 + } 33 + pthread_mutex_lock(&a); 34 + 35 + if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD) 36 + pthread_barrier_destroy(&bar); 37 + 38 + pthread_mutex_lock(&b); 39 + 40 + pthread_mutex_unlock(&b); 41 + pthread_mutex_unlock(&a); 42 + 43 + pthread_join(t, NULL); 44 + 45 + return 0; 46 + }