Skip to content

Commit 741a98d

Browse files
bjh83shuahkh
authored andcommitted
kunit: fix failure to build without printk
Previously KUnit assumed that printk would always be present, which is not a valid assumption to make. Fix that by removing call to vprintk_emit, and calling printk directly. This fixes a build error[1] reported by Randy. For context this change comes after much discussion. My first stab[2] at this was just to make the KUnit logging code compile out; however, it was agreed that if we were going to use vprintk_emit, then vprintk_emit should provide a no-op stub, which lead to my second attempt[3]. In response to me trying to stub out vprintk_emit, Sergey Senozhatsky suggested a way for me to remove our usage of vprintk_emit, which led to my third attempt at solving this[4]. In my third version of this patch[4], I completely removed vprintk_emit, as suggested by Sergey; however, there was a bit of debate over whether Sergey's solution was the best. The debate arose due to Sergey's version resulting in a checkpatch warning, which resulted in a debate over correct printk usage. Joe Perches offered an alternative fix which was somewhat less far reaching than what Sergey had suggested and importantly relied on continuing to use %pV. Much of the debated centered around whether %pV should be widely used, and whether Sergey's version would result in object size bloat. Ultimately, we decided to go with Sergey's version. Reported-by: Randy Dunlap <rdunlap@infradead.org> Link[1]: https://lore.kernel.org/linux-kselftest/c7229254-0d90-d90e-f3df-5b6d6fc0b51f@infradead.org/ Link[2]: https://lore.kernel.org/linux-kselftest/20190827174932.44177-1-brendanhiggins@google.com/ Link[3]: https://lore.kernel.org/linux-kselftest/20190827234835.234473-1-brendanhiggins@google.com/ Link[4]: https://lore.kernel.org/linux-kselftest/20190828093143.163302-1-brendanhiggins@google.com/ Cc: Stephen Rothwell <sfr@canb.auug.org.au> Cc: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> Cc: Joe Perches <joe@perches.com> Cc: Tim.Bird@sony.com Signed-off-by: Brendan Higgins <brendanhiggins@google.com> Acked-by: Randy Dunlap <rdunlap@infradead.org> # build-tested Reviewed-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 88aecb0 commit 741a98d

2 files changed

Lines changed: 8 additions & 54 deletions

File tree

‎include/kunit/test.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,8 @@ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
339339

340340
void kunit_cleanup(struct kunit *test);
341341

342-
void __printf(3, 4) kunit_printk(const char *level,
343-
const struct kunit *test,
344-
const char *fmt, ...);
342+
#define kunit_printk(lvl, test, fmt, ...) \
343+
printk(lvl "\t# %s: " fmt, (test)->name, ##__VA_ARGS__)
345344

346345
/**
347346
* kunit_info() - Prints an INFO level message associated with @test.

‎lib/kunit/test.c‎

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,12 @@ static void kunit_set_failure(struct kunit *test)
1616
WRITE_ONCE(test->success, false);
1717
}
1818

19-
static int kunit_vprintk_emit(int level, const char *fmt, va_list args)
20-
{
21-
return vprintk_emit(0, level, NULL, 0, fmt, args);
22-
}
23-
24-
static int kunit_printk_emit(int level, const char *fmt, ...)
25-
{
26-
va_list args;
27-
int ret;
28-
29-
va_start(args, fmt);
30-
ret = kunit_vprintk_emit(level, fmt, args);
31-
va_end(args);
32-
33-
return ret;
34-
}
35-
36-
static void kunit_vprintk(const struct kunit *test,
37-
const char *level,
38-
struct va_format *vaf)
39-
{
40-
kunit_printk_emit(level[1] - '0', "\t# %s: %pV", test->name, vaf);
41-
}
42-
4319
static void kunit_print_tap_version(void)
4420
{
4521
static bool kunit_has_printed_tap_version;
4622

4723
if (!kunit_has_printed_tap_version) {
48-
kunit_printk_emit(LOGLEVEL_INFO, "TAP version 14\n");
24+
pr_info("TAP version 14\n");
4925
kunit_has_printed_tap_version = true;
5026
}
5127
}
@@ -64,10 +40,8 @@ static size_t kunit_test_cases_len(struct kunit_case *test_cases)
6440
static void kunit_print_subtest_start(struct kunit_suite *suite)
6541
{
6642
kunit_print_tap_version();
67-
kunit_printk_emit(LOGLEVEL_INFO, "\t# Subtest: %s\n", suite->name);
68-
kunit_printk_emit(LOGLEVEL_INFO,
69-
"\t1..%zd\n",
70-
kunit_test_cases_len(suite->test_cases));
43+
pr_info("\t# Subtest: %s\n", suite->name);
44+
pr_info("\t1..%zd\n", kunit_test_cases_len(suite->test_cases));
7145
}
7246

7347
static void kunit_print_ok_not_ok(bool should_indent,
@@ -87,9 +61,7 @@ static void kunit_print_ok_not_ok(bool should_indent,
8761
else
8862
ok_not_ok = "not ok";
8963

90-
kunit_printk_emit(LOGLEVEL_INFO,
91-
"%s%s %zd - %s\n",
92-
indent, ok_not_ok, test_number, description);
64+
pr_info("%s%s %zd - %s\n", indent, ok_not_ok, test_number, description);
9365
}
9466

9567
static bool kunit_suite_has_succeeded(struct kunit_suite *suite)
@@ -133,11 +105,11 @@ static void kunit_print_string_stream(struct kunit *test,
133105
kunit_err(test,
134106
"Could not allocate buffer, dumping stream:\n");
135107
list_for_each_entry(fragment, &stream->fragments, node) {
136-
kunit_err(test, fragment->fragment);
108+
kunit_err(test, "%s", fragment->fragment);
137109
}
138110
kunit_err(test, "\n");
139111
} else {
140-
kunit_err(test, buf);
112+
kunit_err(test, "%s", buf);
141113
kunit_kfree(test, buf);
142114
}
143115
}
@@ -504,20 +476,3 @@ void kunit_cleanup(struct kunit *test)
504476
kunit_resource_free(test, resource);
505477
}
506478
}
507-
508-
void kunit_printk(const char *level,
509-
const struct kunit *test,
510-
const char *fmt, ...)
511-
{
512-
struct va_format vaf;
513-
va_list args;
514-
515-
va_start(args, fmt);
516-
517-
vaf.fmt = fmt;
518-
vaf.va = &args;
519-
520-
kunit_vprintk(test, level, &vaf);
521-
522-
va_end(args);
523-
}

0 commit comments

Comments
 (0)