Skip to content

Commit a70d36e

Browse files
melverpaulmckrcu
authored andcommitted
kcsan: Make barrier tests compatible with lockdep
The barrier tests in selftest and the kcsan_test module only need the spinlock and mutex to test correct barrier instrumentation. Therefore, these were initially placed on the stack. However, lockdep asserts that locks are in static storage, and will generate this warning: | INFO: trying to register non-static key. | The code is fine but needs lockdep annotation, or maybe | you didn't initialize this object before use? | turning off the locking correctness validator. | CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.16.0-rc1+ #3208 | Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.13.0-1ubuntu1.1 04/01/2014 | Call Trace: | <TASK> | dump_stack_lvl+0x88/0xd8 | dump_stack+0x15/0x1b | register_lock_class+0x6b3/0x840 | ... | test_barrier+0x490/0x14c7 | kcsan_selftest+0x47/0xa0 | ... To fix, move the test locks into static storage. Fixing the above also revealed that lock operations are strengthened on first use with lockdep enabled, due to lockdep calling out into non-instrumented files (recall that kernel/locking/lockdep.c is not instrumented with KCSAN). Only kcsan_test checks for over-instrumentation of *_lock() operations, where we can simply "warm up" the test locks to avoid the test case failing with lockdep. Reported-by: Paul E. McKenney <paulmck@kernel.org> Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent bd3d5bd commit a70d36e

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

kernel/kcsan/kcsan_test.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ static struct {
300300
long val[8];
301301
} test_struct;
302302
static DEFINE_SEQLOCK(test_seqlock);
303+
static DEFINE_SPINLOCK(test_spinlock);
304+
static DEFINE_MUTEX(test_mutex);
303305

304306
/*
305307
* Helper to avoid compiler optimizing out reads, and to generate source values
@@ -523,8 +525,6 @@ static void test_barrier_nothreads(struct kunit *test)
523525
struct kcsan_scoped_access *reorder_access = NULL;
524526
#endif
525527
arch_spinlock_t arch_spinlock = __ARCH_SPIN_LOCK_UNLOCKED;
526-
DEFINE_SPINLOCK(spinlock);
527-
DEFINE_MUTEX(mutex);
528528
atomic_t dummy;
529529

530530
KCSAN_TEST_REQUIRES(test, reorder_access != NULL);
@@ -543,6 +543,15 @@ static void test_barrier_nothreads(struct kunit *test)
543543
#define KCSAN_EXPECT_WRITE_BARRIER(b, o) __KCSAN_EXPECT_BARRIER(KCSAN_ACCESS_WRITE, b, o, #b)
544544
#define KCSAN_EXPECT_RW_BARRIER(b, o) __KCSAN_EXPECT_BARRIER(KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE, b, o, #b)
545545

546+
/*
547+
* Lockdep initialization can strengthen certain locking operations due
548+
* to calling into instrumented files; "warm up" our locks.
549+
*/
550+
spin_lock(&test_spinlock);
551+
spin_unlock(&test_spinlock);
552+
mutex_lock(&test_mutex);
553+
mutex_unlock(&test_mutex);
554+
546555
/* Force creating a valid entry in reorder_access first. */
547556
test_var = 0;
548557
while (test_var++ < 1000000 && reorder_access->size != sizeof(test_var))
@@ -592,10 +601,10 @@ static void test_barrier_nothreads(struct kunit *test)
592601
KCSAN_EXPECT_READ_BARRIER(clear_bit_unlock_is_negative_byte(0, &test_var), true);
593602
KCSAN_EXPECT_READ_BARRIER(arch_spin_lock(&arch_spinlock), false);
594603
KCSAN_EXPECT_READ_BARRIER(arch_spin_unlock(&arch_spinlock), true);
595-
KCSAN_EXPECT_READ_BARRIER(spin_lock(&spinlock), false);
596-
KCSAN_EXPECT_READ_BARRIER(spin_unlock(&spinlock), true);
597-
KCSAN_EXPECT_READ_BARRIER(mutex_lock(&mutex), false);
598-
KCSAN_EXPECT_READ_BARRIER(mutex_unlock(&mutex), true);
604+
KCSAN_EXPECT_READ_BARRIER(spin_lock(&test_spinlock), false);
605+
KCSAN_EXPECT_READ_BARRIER(spin_unlock(&test_spinlock), true);
606+
KCSAN_EXPECT_READ_BARRIER(mutex_lock(&test_mutex), false);
607+
KCSAN_EXPECT_READ_BARRIER(mutex_unlock(&test_mutex), true);
599608

600609
KCSAN_EXPECT_WRITE_BARRIER(mb(), true);
601610
KCSAN_EXPECT_WRITE_BARRIER(wmb(), true);
@@ -638,10 +647,10 @@ static void test_barrier_nothreads(struct kunit *test)
638647
KCSAN_EXPECT_WRITE_BARRIER(clear_bit_unlock_is_negative_byte(0, &test_var), true);
639648
KCSAN_EXPECT_WRITE_BARRIER(arch_spin_lock(&arch_spinlock), false);
640649
KCSAN_EXPECT_WRITE_BARRIER(arch_spin_unlock(&arch_spinlock), true);
641-
KCSAN_EXPECT_WRITE_BARRIER(spin_lock(&spinlock), false);
642-
KCSAN_EXPECT_WRITE_BARRIER(spin_unlock(&spinlock), true);
643-
KCSAN_EXPECT_WRITE_BARRIER(mutex_lock(&mutex), false);
644-
KCSAN_EXPECT_WRITE_BARRIER(mutex_unlock(&mutex), true);
650+
KCSAN_EXPECT_WRITE_BARRIER(spin_lock(&test_spinlock), false);
651+
KCSAN_EXPECT_WRITE_BARRIER(spin_unlock(&test_spinlock), true);
652+
KCSAN_EXPECT_WRITE_BARRIER(mutex_lock(&test_mutex), false);
653+
KCSAN_EXPECT_WRITE_BARRIER(mutex_unlock(&test_mutex), true);
645654

646655
KCSAN_EXPECT_RW_BARRIER(mb(), true);
647656
KCSAN_EXPECT_RW_BARRIER(wmb(), true);
@@ -684,10 +693,10 @@ static void test_barrier_nothreads(struct kunit *test)
684693
KCSAN_EXPECT_RW_BARRIER(clear_bit_unlock_is_negative_byte(0, &test_var), true);
685694
KCSAN_EXPECT_RW_BARRIER(arch_spin_lock(&arch_spinlock), false);
686695
KCSAN_EXPECT_RW_BARRIER(arch_spin_unlock(&arch_spinlock), true);
687-
KCSAN_EXPECT_RW_BARRIER(spin_lock(&spinlock), false);
688-
KCSAN_EXPECT_RW_BARRIER(spin_unlock(&spinlock), true);
689-
KCSAN_EXPECT_RW_BARRIER(mutex_lock(&mutex), false);
690-
KCSAN_EXPECT_RW_BARRIER(mutex_unlock(&mutex), true);
696+
KCSAN_EXPECT_RW_BARRIER(spin_lock(&test_spinlock), false);
697+
KCSAN_EXPECT_RW_BARRIER(spin_unlock(&test_spinlock), true);
698+
KCSAN_EXPECT_RW_BARRIER(mutex_lock(&test_mutex), false);
699+
KCSAN_EXPECT_RW_BARRIER(mutex_unlock(&test_mutex), true);
691700

692701
kcsan_nestable_atomic_end();
693702
}

kernel/kcsan/selftest.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ static bool __init test_matching_access(void)
113113
* positives: simple test to check at boot certain barriers are always properly
114114
* instrumented. See kcsan_test for a more complete test.
115115
*/
116+
static DEFINE_SPINLOCK(test_spinlock);
116117
static bool __init test_barrier(void)
117118
{
118119
#ifdef CONFIG_KCSAN_WEAK_MEMORY
@@ -122,7 +123,6 @@ static bool __init test_barrier(void)
122123
#endif
123124
bool ret = true;
124125
arch_spinlock_t arch_spinlock = __ARCH_SPIN_LOCK_UNLOCKED;
125-
DEFINE_SPINLOCK(spinlock);
126126
atomic_t dummy;
127127
long test_var;
128128

@@ -172,8 +172,8 @@ static bool __init test_barrier(void)
172172
KCSAN_CHECK_READ_BARRIER(clear_bit_unlock_is_negative_byte(0, &test_var));
173173
arch_spin_lock(&arch_spinlock);
174174
KCSAN_CHECK_READ_BARRIER(arch_spin_unlock(&arch_spinlock));
175-
spin_lock(&spinlock);
176-
KCSAN_CHECK_READ_BARRIER(spin_unlock(&spinlock));
175+
spin_lock(&test_spinlock);
176+
KCSAN_CHECK_READ_BARRIER(spin_unlock(&test_spinlock));
177177

178178
KCSAN_CHECK_WRITE_BARRIER(mb());
179179
KCSAN_CHECK_WRITE_BARRIER(wmb());
@@ -202,8 +202,8 @@ static bool __init test_barrier(void)
202202
KCSAN_CHECK_WRITE_BARRIER(clear_bit_unlock_is_negative_byte(0, &test_var));
203203
arch_spin_lock(&arch_spinlock);
204204
KCSAN_CHECK_WRITE_BARRIER(arch_spin_unlock(&arch_spinlock));
205-
spin_lock(&spinlock);
206-
KCSAN_CHECK_WRITE_BARRIER(spin_unlock(&spinlock));
205+
spin_lock(&test_spinlock);
206+
KCSAN_CHECK_WRITE_BARRIER(spin_unlock(&test_spinlock));
207207

208208
KCSAN_CHECK_RW_BARRIER(mb());
209209
KCSAN_CHECK_RW_BARRIER(wmb());
@@ -235,8 +235,8 @@ static bool __init test_barrier(void)
235235
KCSAN_CHECK_RW_BARRIER(clear_bit_unlock_is_negative_byte(0, &test_var));
236236
arch_spin_lock(&arch_spinlock);
237237
KCSAN_CHECK_RW_BARRIER(arch_spin_unlock(&arch_spinlock));
238-
spin_lock(&spinlock);
239-
KCSAN_CHECK_RW_BARRIER(spin_unlock(&spinlock));
238+
spin_lock(&test_spinlock);
239+
KCSAN_CHECK_RW_BARRIER(spin_unlock(&test_spinlock));
240240

241241
kcsan_nestable_atomic_end();
242242

0 commit comments

Comments
 (0)