Skip to content

Commit

Permalink
kunit: make test->lock irq safe
Browse files Browse the repository at this point in the history
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/[email protected]
Signed-off-by: Vlastimil Babka <[email protected]>
Signed-off-by: Oliver Glitta <[email protected]>
Reviewed-by: Brendan Higgins <[email protected]>
Cc: Christoph Lameter <[email protected]>
Cc: Daniel Latypov <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Joonsoo Kim <[email protected]>
Cc: Marco Elver <[email protected]>
Cc: Pekka Enberg <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
tehcaster authored and torvalds committed Jun 29, 2021
1 parent 4acaa7d commit 26c6cb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions include/kunit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,9 @@ kunit_find_resource(struct kunit *test,
void *match_data)
{
struct kunit_resource *res, *found = NULL;
unsigned long flags;

spin_lock(&test->lock);
spin_lock_irqsave(&test->lock, flags);

list_for_each_entry_reverse(res, &test->resources, node) {
if (match(test, res, (void *)match_data)) {
Expand All @@ -526,7 +527,7 @@ kunit_find_resource(struct kunit *test,
}
}

spin_unlock(&test->lock);
spin_unlock_irqrestore(&test->lock, flags);

return found;
}
Expand Down
18 changes: 11 additions & 7 deletions lib/kunit/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ int kunit_add_resource(struct kunit *test,
void *data)
{
int ret = 0;
unsigned long flags;

res->free = free;
kref_init(&res->refcount);
Expand All @@ -487,10 +488,10 @@ int kunit_add_resource(struct kunit *test,
res->data = data;
}

spin_lock(&test->lock);
spin_lock_irqsave(&test->lock, flags);
list_add_tail(&res->node, &test->resources);
/* refcount for list is established by kref_init() */
spin_unlock(&test->lock);
spin_unlock_irqrestore(&test->lock, flags);

return ret;
}
Expand Down Expand Up @@ -548,9 +549,11 @@ EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);

void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
{
spin_lock(&test->lock);
unsigned long flags;

spin_lock_irqsave(&test->lock, flags);
list_del(&res->node);
spin_unlock(&test->lock);
spin_unlock_irqrestore(&test->lock, flags);
kunit_put_resource(res);
}
EXPORT_SYMBOL_GPL(kunit_remove_resource);
Expand Down Expand Up @@ -630,6 +633,7 @@ EXPORT_SYMBOL_GPL(kunit_kfree);
void kunit_cleanup(struct kunit *test)
{
struct kunit_resource *res;
unsigned long flags;

/*
* test->resources is a stack - each allocation must be freed in the
Expand All @@ -641,9 +645,9 @@ void kunit_cleanup(struct kunit *test)
* protect against the current node being deleted, not the next.
*/
while (true) {
spin_lock(&test->lock);
spin_lock_irqsave(&test->lock, flags);
if (list_empty(&test->resources)) {
spin_unlock(&test->lock);
spin_unlock_irqrestore(&test->lock, flags);
break;
}
res = list_last_entry(&test->resources,
Expand All @@ -654,7 +658,7 @@ void kunit_cleanup(struct kunit *test)
* resource, and this can't happen if the test->lock
* is held.
*/
spin_unlock(&test->lock);
spin_unlock_irqrestore(&test->lock, flags);
kunit_remove_resource(test, res);
}
current->kunit_test = NULL;
Expand Down

0 comments on commit 26c6cb7

Please sign in to comment.