]> git.baikalelectronics.ru Git - kernel.git/commit
kunit: Make kunit_remove_resource() idempotent
authorDavid Gow <davidgow@google.com>
Sat, 2 Apr 2022 04:35:29 +0000 (12:35 +0800)
committerShuah Khan <skhan@linuxfoundation.org>
Tue, 5 Apr 2022 19:32:50 +0000 (13:32 -0600)
commitc1927798191c290d318425491d47cc90fd1c5555
treebeff30b890129fc822ada22e2480f42c69cf84bf
parent836d5ea77ef2b3ba267bd2e91273f26ba8ab2587
kunit: Make kunit_remove_resource() idempotent

The kunit_remove_resource() function is used to unlink a resource from
the list of resources in the test, making it no longer show up in
kunit_find_resource().

However, this could lead to a race condition if two threads called
kunit_remove_resource() on the same resource at the same time: the
resource would be removed from the list twice (causing a crash at the
second list_del()), and the refcount for the resource would be
decremented twice (instead of once, for the reference held by the
resource list).

Fix both problems, the first by using list_del_init(), and the second by
checking if the resource has already been removed using list_empty(),
and only decrementing its refcount if it has not.

Also add a KUnit test for the kunit_remove_resource() function which
tests this behaviour.

Reported-by: Daniel Latypov <dlatypov@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
lib/kunit/kunit-test.c
lib/kunit/resource.c