Skip to content

Commit 5eadeb7

Browse files
botta633fbq
authored andcommitted
locking/lockdep: Add a test for lockdep_set_subclass()
Add a test case to ensure that no new name string literal will be created in lockdep_set_subclass(), otherwise a warning will be triggered in look_up_lock_class(). Add this to catch the problem in the future. [boqun: Reword the title, replace #if with #ifdef and rename functions and variables] Signed-off-by: Ahmed Ehab <[email protected]> Signed-off-by: Boqun Feng <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/
1 parent d7fe143 commit 5eadeb7

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

lib/locking-selftest.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,43 @@ static void local_lock_3B(void)
27102710

27112711
}
27122712

2713+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
2714+
static inline const char *rw_semaphore_lockdep_name(struct rw_semaphore *rwsem)
2715+
{
2716+
return rwsem->dep_map.name;
2717+
}
2718+
#else
2719+
static inline const char *rw_semaphore_lockdep_name(struct rw_semaphore *rwsem)
2720+
{
2721+
return NULL;
2722+
}
2723+
#endif
2724+
2725+
static void test_lockdep_set_subclass_name(void)
2726+
{
2727+
const char *name_before = rw_semaphore_lockdep_name(&rwsem_X1);
2728+
const char *name_after;
2729+
2730+
lockdep_set_subclass(&rwsem_X1, 1);
2731+
name_after = rw_semaphore_lockdep_name(&rwsem_X1);
2732+
DEBUG_LOCKS_WARN_ON(name_before != name_after);
2733+
}
2734+
2735+
/*
2736+
* lockdep_set_subclass() should reuse the existing lock class name instead
2737+
* of creating a new one.
2738+
*/
2739+
static void lockdep_set_subclass_name_test(void)
2740+
{
2741+
printk(" --------------------------------------------------------------------------\n");
2742+
printk(" | lockdep_set_subclass() name test|\n");
2743+
printk(" -----------------------------------\n");
2744+
2745+
print_testname("compare name before and after");
2746+
dotest(test_lockdep_set_subclass_name, SUCCESS, LOCKTYPE_RWSEM);
2747+
pr_cont("\n");
2748+
}
2749+
27132750
static void local_lock_tests(void)
27142751
{
27152752
printk(" --------------------------------------------------------------------------\n");
@@ -2920,6 +2957,8 @@ void locking_selftest(void)
29202957
dotest(hardirq_deadlock_softirq_not_deadlock, FAILURE, LOCKTYPE_SPECIAL);
29212958
pr_cont("\n");
29222959

2960+
lockdep_set_subclass_name_test();
2961+
29232962
if (unexpected_testcase_failures) {
29242963
printk("-----------------------------------------------------------------\n");
29252964
debug_locks = 0;

0 commit comments

Comments
 (0)