Skip to contentSkip to conversation

kunit: use kmemdup in kunit_filter_tests(), take suite as const

PATCH In review Daniel Latypov 3 participants 3 messages May 16, 2022 – Jul 6, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kunit: use kmemdup in kunit_filter_tests(), take suite as const

kmemdup() is easier than kmalloc() + memcpy(), per lkp bot.

Also make the input suite as const since we're now always making copies after commit a127b154a8f2 ("kunit: tool: allow filtering test cases via glob").

---
 lib/kunit/executor.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
lib/kunit/executor.c +2 -4
index 2f73a6a35a7e..43e15112460a 100644
@@ -55,7 +55,7 @@ static void kunit_parse_filter_glob(struct kunit_test_filter *parsed,
 
 /* Create a copy of suite with only tests that match test_glob. */
 static struct kunit_suite *
-kunit_filter_tests(struct kunit_suite *const suite, const char *test_glob)
+kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)
 {
 	int n = 0;
 	struct kunit_case *filtered, *test_case;
@@ -69,11 +69,9 @@ kunit_filter_tests(struct kunit_suite *const suite, const char *test_glob)
 	if (n == 0)
 		return NULL;
 
-	/* Use memcpy to workaround copy->name being const. */
-	copy = kmalloc(sizeof(*copy), GFP_KERNEL);
+	copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
 	if (!copy)
 		return ERR_PTR(-ENOMEM);
-	memcpy(copy, suite, sizeof(*copy));
 
 	filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
 	if (!filtered)

base-commit: 9660209d9418f2295d31fea0d32e313e9b2c1200

Signature · 1 line
2.36.0.550.gb090851708-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Daniel Latypov

kmemdup() is easier than kmalloc() + memcpy(), per lkp bot.

Also make the input suite as const since we're now always making copies after commit a127b154a8f2 ("kunit: tool: allow filtering test cases via glob").

---

Ah, this is definitely better. Thanks. I ran it under KASAN to double check, and no issues there, either.

Cheers,
-- David

Quoted 34 lines
 lib/kunit/executor.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
lib/kunit/executor.c +2 -4
index 2f73a6a35a7e..43e15112460a 100644
@@ -55,7 +55,7 @@ static void kunit_parse_filter_glob(struct kunit_test_filter *parsed,
 
 /* Create a copy of suite with only tests that match test_glob. */
 static struct kunit_suite *
-kunit_filter_tests(struct kunit_suite *const suite, const char *test_glob)
+kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)
 {
        int n = 0;
        struct kunit_case *filtered, *test_case;
@@ -69,11 +69,9 @@ kunit_filter_tests(struct kunit_suite *const suite, const char *test_glob)
        if (n == 0)
                return NULL;
 
-       /* Use memcpy to workaround copy->name being const. */
-       copy = kmalloc(sizeof(*copy), GFP_KERNEL);
+       copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL);
        if (!copy)
                return ERR_PTR(-ENOMEM);
-       memcpy(copy, suite, sizeof(*copy));
 
        filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL);
        if (!filtered)

base-commit: 9660209d9418f2295d31fea0d32e313e9b2c1200 --
2.36.0.550.gb090851708-goog