Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kunit: make kunit_kfree() only work on pointers from kunit_malloc() and friends

kunit_kfree() exists to clean up allocations from kunit_kmalloc() and
friends early instead of waiting for this to happen automatically at the
end of the test.

But it can be used on *anything* registered with the kunit resource API.

E.g. the last 2 statements are equivalent:
struct kunit_resource *res = something();
kfree(res->data);
kunit_put_resource(res);

The problem is that there could be multiple resources that point to the
same `data`.

E.g. you can have a named resource acting as a pseudo-global variable in
a test. If you point it to data allocated with kunit_kmalloc(), then
calling `kunit_kfree(ptr)` has the chance to delete either the named
resource or to kfree `ptr`.
Which one it does depends on the order the resources are registered as
kunit_kfree() will delete resources in LIFO order.

So this patch restricts kunit_kfree() to only working on resources
created by kunit_kmalloc(). Calling it is therefore guaranteed to free
the memory, not do anything else.

Note: kunit_resource_instance_match() wasn't used outside of KUnit, so
it should be safe to remove from the public interface. It's also
generally dangerous, as shown above, and shouldn't be used.

Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Daniel Latypov and committed by
Shuah Khan
047a8a0a 4db4598b

+15 -18
-16
include/kunit/resource.h
··· 301 301 void *match_data); 302 302 303 303 /** 304 - * kunit_resource_instance_match() - Match a resource with the same instance. 305 - * @test: Test case to which the resource belongs. 306 - * @res: The resource. 307 - * @match_data: The resource pointer to match against. 308 - * 309 - * An instance of kunit_resource_match_t that matches a resource whose 310 - * allocation matches @match_data. 311 - */ 312 - static inline bool kunit_resource_instance_match(struct kunit *test, 313 - struct kunit_resource *res, 314 - void *match_data) 315 - { 316 - return res->data == match_data; 317 - } 318 - 319 - /** 320 304 * kunit_resource_name_match() - Match a resource with the same name. 321 305 * @test: Test case to which the resource belongs. 322 306 * @res: The resource.
+7
lib/kunit/kunit-test.c
··· 161 161 kunit_put_resource(res); 162 162 } 163 163 164 + static inline bool kunit_resource_instance_match(struct kunit *test, 165 + struct kunit_resource *res, 166 + void *match_data) 167 + { 168 + return res->data == match_data; 169 + } 170 + 164 171 /* 165 172 * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence 166 173 * they have a reference to the associated resource that they must release
+8 -2
lib/kunit/test.c
··· 713 713 } 714 714 EXPORT_SYMBOL_GPL(kunit_kmalloc_array); 715 715 716 + static inline bool kunit_kfree_match(struct kunit *test, 717 + struct kunit_resource *res, void *match_data) 718 + { 719 + /* Only match resources allocated with kunit_kmalloc() and friends. */ 720 + return res->free == kunit_kmalloc_array_free && res->data == match_data; 721 + } 722 + 716 723 void kunit_kfree(struct kunit *test, const void *ptr) 717 724 { 718 725 struct kunit_resource *res; 719 726 720 - res = kunit_find_resource(test, kunit_resource_instance_match, 721 - (void *)ptr); 727 + res = kunit_find_resource(test, kunit_kfree_match, (void *)ptr); 722 728 723 729 /* 724 730 * Removing the resource from the list of resources drops the