Skip to content

Commit 7170b7e

Browse files
sulixshuahkh
authored andcommitted
kunit: Add "hooks" to call into KUnit when it's built as a module
KUnit has several macros and functions intended for use from non-test code. These hooks, currently the kunit_get_current_test() and kunit_fail_current_test() macros, didn't work when CONFIG_KUNIT=m. In order to support this case, the required functions and static data need to be available unconditionally, even when KUnit itself is not built-in. The new 'hooks.c' file is therefore always included, and has both the static key required for kunit_get_current_test(), and a table of function pointers in struct kunit_hooks_table. This is filled in with the real implementations by kunit_install_hooks(), which is kept in hooks-impl.h and called when the kunit module is loaded. This can be extended for future features which require similar "hook" behaviour, such as static stubs, by simply adding new entries to the struct, and the appropriate code to set them. Fixed white-space errors during commit: Shuah Khan <[email protected]> Resolved merge conflicts with: db105c3 ("kunit: Export kunit_running()") This patch supersedes the above. Shuah Khan <[email protected]> Signed-off-by: David Gow <[email protected]> Reviewed-by: Rae Moar <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 2dc9d6c commit 7170b7e

File tree

7 files changed

+82
-35
lines changed

7 files changed

+82
-35
lines changed

Documentation/dev-tools/kunit/usage.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,9 @@ We can do this via the ``kunit_test`` field in ``task_struct``, which we can
648648
access using the ``kunit_get_current_test()`` function in ``kunit/test-bug.h``.
649649

650650
``kunit_get_current_test()`` is safe to call even if KUnit is not enabled. If
651-
KUnit is not enabled, was built as a module (``CONFIG_KUNIT=m``), or no test is
652-
running in the current task, it will return ``NULL``. This compiles down to
653-
either a no-op or a static key check, so will have a negligible performance
654-
impact when no test is running.
651+
KUnit is not enabled, or if no test is running in the current task, it will
652+
return ``NULL``. This compiles down to either a no-op or a static key check,
653+
so will have a negligible performance impact when no test is running.
655654

656655
The example below uses this to implement a "mock" implementation of a function, ``foo``:
657656

@@ -726,8 +725,6 @@ structures as shown below:
726725
#endif
727726
728727
``kunit_fail_current_test()`` is safe to call even if KUnit is not enabled. If
729-
KUnit is not enabled, was built as a module (``CONFIG_KUNIT=m``), or no test is
730-
running in the current task, it will do nothing. This compiles down to either a
731-
no-op or a static key check, so will have a negligible performance impact when
732-
no test is running.
733-
728+
KUnit is not enabled, or if no test is running in the current task, it will do
729+
nothing. This compiles down to either a no-op or a static key check, so will
730+
have a negligible performance impact when no test is running.

include/kunit/test-bug.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
/*
3-
* KUnit API allowing dynamic analysis tools to interact with KUnit tests
3+
* KUnit API providing hooks for non-test code to interact with tests.
44
*
55
* Copyright (C) 2020, Google LLC.
66
* Author: Uriel Guajardo <[email protected]>
@@ -9,14 +9,19 @@
99
#ifndef _KUNIT_TEST_BUG_H
1010
#define _KUNIT_TEST_BUG_H
1111

12-
#if IS_BUILTIN(CONFIG_KUNIT)
12+
#if IS_ENABLED(CONFIG_KUNIT)
1313

1414
#include <linux/jump_label.h> /* For static branch */
1515
#include <linux/sched.h>
1616

1717
/* Static key if KUnit is running any tests. */
1818
DECLARE_STATIC_KEY_FALSE(kunit_running);
1919

20+
/* Hooks table: a table of function pointers filled in when kunit loads */
21+
extern struct kunit_hooks_table {
22+
__printf(3, 4) void (*fail_current_test)(const char*, int, const char*, ...);
23+
} kunit_hooks;
24+
2025
/**
2126
* kunit_get_current_test() - Return a pointer to the currently running
2227
* KUnit test.
@@ -43,33 +48,20 @@ static inline struct kunit *kunit_get_current_test(void)
4348
* kunit_fail_current_test() - If a KUnit test is running, fail it.
4449
*
4550
* If a KUnit test is running in the current task, mark that test as failed.
46-
*
47-
* This macro will only work if KUnit is built-in (though the tests
48-
* themselves can be modules). Otherwise, it compiles down to nothing.
4951
*/
5052
#define kunit_fail_current_test(fmt, ...) do { \
5153
if (static_branch_unlikely(&kunit_running)) { \
52-
__kunit_fail_current_test(__FILE__, __LINE__, \
54+
/* Guaranteed to be non-NULL when kunit_running true*/ \
55+
kunit_hooks.fail_current_test(__FILE__, __LINE__, \
5356
fmt, ##__VA_ARGS__); \
5457
} \
5558
} while (0)
5659

57-
58-
extern __printf(3, 4) void __kunit_fail_current_test(const char *file, int line,
59-
const char *fmt, ...);
60-
6160
#else
6261

6362
static inline struct kunit *kunit_get_current_test(void) { return NULL; }
6463

65-
/* We define this with an empty helper function so format string warnings work */
66-
#define kunit_fail_current_test(fmt, ...) \
67-
__kunit_fail_current_test(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
68-
69-
static inline __printf(3, 4) void __kunit_fail_current_test(const char *file, int line,
70-
const char *fmt, ...)
71-
{
72-
}
64+
#define kunit_fail_current_test(fmt, ...) do {} while (0)
7365

7466
#endif
7567

lib/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ CFLAGS_test_fpu.o += $(FPU_CFLAGS)
126126
obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/
127127

128128
obj-$(CONFIG_KUNIT) += kunit/
129+
# Include the KUnit hooks unconditionally. They'll compile to nothing if
130+
# CONFIG_KUNIT=n, otherwise will be a small table of static data (static key,
131+
# function pointers) which need to be built-in even when KUnit is a module.
132+
ifeq ($(CONFIG_KUNIT), m)
133+
obj-y += kunit/hooks.o
134+
else
135+
obj-$(CONFIG_KUNIT) += kunit/hooks.o
136+
endif
129137

130138
ifeq ($(CONFIG_DEBUG_KOBJECT),y)
131139
CFLAGS_kobject.o += -DDEBUG

lib/kunit/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
1111
kunit-objs += debugfs.o
1212
endif
1313

14+
# KUnit 'hooks' are built-in even when KUnit is built as a module.
15+
lib-y += hooks.o
16+
1417
obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
1518

1619
# string-stream-test compiles built-in only.

lib/kunit/hooks-impl.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Declarations for hook implementations.
4+
*
5+
* These will be set as the function pointers in struct kunit_hook_table,
6+
* found in include/kunit/test-bug.h.
7+
*
8+
* Copyright (C) 2023, Google LLC.
9+
* Author: David Gow <[email protected]>
10+
*/
11+
12+
#ifndef _KUNIT_HOOKS_IMPL_H
13+
#define _KUNIT_HOOKS_IMPL_H
14+
15+
#include <kunit/test-bug.h>
16+
17+
/* List of declarations. */
18+
void __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...);
19+
20+
/* Code to set all of the function pointers. */
21+
static inline void kunit_install_hooks(void)
22+
{
23+
/* Install the KUnit hook functions. */
24+
kunit_hooks.fail_current_test = __kunit_fail_current_test_impl;
25+
}
26+
27+
#endif /* _KUNIT_HOOKS_IMPL_H */

lib/kunit/hooks.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* KUnit 'Hooks' implementation.
4+
*
5+
* This file contains code / structures which should be built-in even when
6+
* KUnit itself is built as a module.
7+
*
8+
* Copyright (C) 2022, Google LLC.
9+
* Author: David Gow <[email protected]>
10+
*/
11+
12+
13+
#include <kunit/test-bug.h>
14+
15+
DEFINE_STATIC_KEY_FALSE(kunit_running);
16+
EXPORT_SYMBOL(kunit_running);
17+
18+
/* Function pointers for hooks. */
19+
struct kunit_hooks_table kunit_hooks;
20+
EXPORT_SYMBOL(kunit_hooks);
21+

lib/kunit/test.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@
1717
#include <linux/sched.h>
1818

1919
#include "debugfs.h"
20+
#include "hooks-impl.h"
2021
#include "string-stream.h"
2122
#include "try-catch-impl.h"
2223

23-
DEFINE_STATIC_KEY_FALSE(kunit_running);
24-
EXPORT_SYMBOL_GPL(kunit_running);
25-
26-
#if IS_BUILTIN(CONFIG_KUNIT)
2724
/*
28-
* Fail the current test and print an error message to the log.
25+
* Hook to fail the current test and print an error message to the log.
2926
*/
30-
void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
27+
void __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...)
3128
{
3229
va_list args;
3330
int len;
@@ -54,8 +51,6 @@ void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
5451
kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
5552
kunit_kfree(current->kunit_test, buffer);
5653
}
57-
EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
58-
#endif
5954

6055
/*
6156
* Enable KUnit tests to run.
@@ -778,6 +773,9 @@ EXPORT_SYMBOL_GPL(kunit_cleanup);
778773

779774
static int __init kunit_init(void)
780775
{
776+
/* Install the KUnit hook functions. */
777+
kunit_install_hooks();
778+
781779
kunit_debugfs_init();
782780
#ifdef CONFIG_MODULES
783781
return register_module_notifier(&kunit_mod_nb);
@@ -789,6 +787,7 @@ late_initcall(kunit_init);
789787

790788
static void __exit kunit_exit(void)
791789
{
790+
memset(&kunit_hooks, 0, sizeof(kunit_hooks));
792791
#ifdef CONFIG_MODULES
793792
unregister_module_notifier(&kunit_mod_nb);
794793
#endif

0 commit comments

Comments
 (0)