Skip to content

Commit 26c6cb7

Browse files
tehcastertorvalds
authored andcommitted
kunit: make test->lock irq safe
The upcoming SLUB kunit test will be calling kunit_find_named_resource() from a context with disabled interrupts. That means kunit's test->lock needs to be IRQ safe to avoid potential deadlocks and lockdep splats. This patch therefore changes the test->lock usage to spin_lock_irqsave() and spin_unlock_irqrestore(). Link: https://lkml.kernel.org/r/20210511150734.3492-1-glittao@gmail.com Signed-off-by: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Oliver Glitta <glittao@gmail.com> Reviewed-by: Brendan Higgins <brendanhiggins@google.com> Cc: Christoph Lameter <cl@linux.com> Cc: Daniel Latypov <dlatypov@google.com> Cc: David Rientjes <rientjes@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Marco Elver <elver@google.com> Cc: Pekka Enberg <penberg@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4acaa7d commit 26c6cb7

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

include/kunit/test.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,9 @@ kunit_find_resource(struct kunit *test,
515515
void *match_data)
516516
{
517517
struct kunit_resource *res, *found = NULL;
518+
unsigned long flags;
518519

519-
spin_lock(&test->lock);
520+
spin_lock_irqsave(&test->lock, flags);
520521

521522
list_for_each_entry_reverse(res, &test->resources, node) {
522523
if (match(test, res, (void *)match_data)) {
@@ -526,7 +527,7 @@ kunit_find_resource(struct kunit *test,
526527
}
527528
}
528529

529-
spin_unlock(&test->lock);
530+
spin_unlock_irqrestore(&test->lock, flags);
530531

531532
return found;
532533
}

lib/kunit/test.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ int kunit_add_resource(struct kunit *test,
475475
void *data)
476476
{
477477
int ret = 0;
478+
unsigned long flags;
478479

479480
res->free = free;
480481
kref_init(&res->refcount);
@@ -487,10 +488,10 @@ int kunit_add_resource(struct kunit *test,
487488
res->data = data;
488489
}
489490

490-
spin_lock(&test->lock);
491+
spin_lock_irqsave(&test->lock, flags);
491492
list_add_tail(&res->node, &test->resources);
492493
/* refcount for list is established by kref_init() */
493-
spin_unlock(&test->lock);
494+
spin_unlock_irqrestore(&test->lock, flags);
494495

495496
return ret;
496497
}
@@ -548,9 +549,11 @@ EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
548549

549550
void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
550551
{
551-
spin_lock(&test->lock);
552+
unsigned long flags;
553+
554+
spin_lock_irqsave(&test->lock, flags);
552555
list_del(&res->node);
553-
spin_unlock(&test->lock);
556+
spin_unlock_irqrestore(&test->lock, flags);
554557
kunit_put_resource(res);
555558
}
556559
EXPORT_SYMBOL_GPL(kunit_remove_resource);
@@ -630,6 +633,7 @@ EXPORT_SYMBOL_GPL(kunit_kfree);
630633
void kunit_cleanup(struct kunit *test)
631634
{
632635
struct kunit_resource *res;
636+
unsigned long flags;
633637

634638
/*
635639
* test->resources is a stack - each allocation must be freed in the
@@ -641,9 +645,9 @@ void kunit_cleanup(struct kunit *test)
641645
* protect against the current node being deleted, not the next.
642646
*/
643647
while (true) {
644-
spin_lock(&test->lock);
648+
spin_lock_irqsave(&test->lock, flags);
645649
if (list_empty(&test->resources)) {
646-
spin_unlock(&test->lock);
650+
spin_unlock_irqrestore(&test->lock, flags);
647651
break;
648652
}
649653
res = list_last_entry(&test->resources,
@@ -654,7 +658,7 @@ void kunit_cleanup(struct kunit *test)
654658
* resource, and this can't happen if the test->lock
655659
* is held.
656660
*/
657-
spin_unlock(&test->lock);
661+
spin_unlock_irqrestore(&test->lock, flags);
658662
kunit_remove_resource(test, res);
659663
}
660664
current->kunit_test = NULL;

0 commit comments

Comments
 (0)