mirror of
https://github.com/torvalds/linux.git
synced 2024-11-21 19:46:16 +00:00
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 <skhan@linuxfoundation.org>
Resolved merge conflicts with:
db105c37a4
("kunit: Export kunit_running()")
This patch supersedes the above.
Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Rae Moar <rmoar@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:
parent
2dc9d6ca52
commit
7170b7ed6a
@ -648,10 +648,9 @@ We can do this via the ``kunit_test`` field in ``task_struct``, which we can
|
||||
access using the ``kunit_get_current_test()`` function in ``kunit/test-bug.h``.
|
||||
|
||||
``kunit_get_current_test()`` is safe to call even if KUnit is not enabled. If
|
||||
KUnit is not enabled, was built as a module (``CONFIG_KUNIT=m``), or no test is
|
||||
running in the current task, it will return ``NULL``. This compiles down to
|
||||
either a no-op or a static key check, so will have a negligible performance
|
||||
impact when no test is running.
|
||||
KUnit is not enabled, or if no test is running in the current task, it will
|
||||
return ``NULL``. This compiles down to either a no-op or a static key check,
|
||||
so will have a negligible performance impact when no test is running.
|
||||
|
||||
The example below uses this to implement a "mock" implementation of a function, ``foo``:
|
||||
|
||||
@ -726,8 +725,6 @@ structures as shown below:
|
||||
#endif
|
||||
|
||||
``kunit_fail_current_test()`` is safe to call even if KUnit is not enabled. If
|
||||
KUnit is not enabled, was built as a module (``CONFIG_KUNIT=m``), or no test is
|
||||
running in the current task, it will do nothing. This compiles down to either a
|
||||
no-op or a static key check, so will have a negligible performance impact when
|
||||
no test is running.
|
||||
|
||||
KUnit is not enabled, or if no test is running in the current task, it will do
|
||||
nothing. This compiles down to either a no-op or a static key check, so will
|
||||
have a negligible performance impact when no test is running.
|
||||
|
@ -1,6 +1,6 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/*
|
||||
* KUnit API allowing dynamic analysis tools to interact with KUnit tests
|
||||
* KUnit API providing hooks for non-test code to interact with tests.
|
||||
*
|
||||
* Copyright (C) 2020, Google LLC.
|
||||
* Author: Uriel Guajardo <urielguajardo@google.com>
|
||||
@ -9,7 +9,7 @@
|
||||
#ifndef _KUNIT_TEST_BUG_H
|
||||
#define _KUNIT_TEST_BUG_H
|
||||
|
||||
#if IS_BUILTIN(CONFIG_KUNIT)
|
||||
#if IS_ENABLED(CONFIG_KUNIT)
|
||||
|
||||
#include <linux/jump_label.h> /* For static branch */
|
||||
#include <linux/sched.h>
|
||||
@ -17,6 +17,11 @@
|
||||
/* Static key if KUnit is running any tests. */
|
||||
DECLARE_STATIC_KEY_FALSE(kunit_running);
|
||||
|
||||
/* Hooks table: a table of function pointers filled in when kunit loads */
|
||||
extern struct kunit_hooks_table {
|
||||
__printf(3, 4) void (*fail_current_test)(const char*, int, const char*, ...);
|
||||
} kunit_hooks;
|
||||
|
||||
/**
|
||||
* kunit_get_current_test() - Return a pointer to the currently running
|
||||
* KUnit test.
|
||||
@ -43,33 +48,20 @@ static inline struct kunit *kunit_get_current_test(void)
|
||||
* kunit_fail_current_test() - If a KUnit test is running, fail it.
|
||||
*
|
||||
* If a KUnit test is running in the current task, mark that test as failed.
|
||||
*
|
||||
* This macro will only work if KUnit is built-in (though the tests
|
||||
* themselves can be modules). Otherwise, it compiles down to nothing.
|
||||
*/
|
||||
#define kunit_fail_current_test(fmt, ...) do { \
|
||||
if (static_branch_unlikely(&kunit_running)) { \
|
||||
__kunit_fail_current_test(__FILE__, __LINE__, \
|
||||
/* Guaranteed to be non-NULL when kunit_running true*/ \
|
||||
kunit_hooks.fail_current_test(__FILE__, __LINE__, \
|
||||
fmt, ##__VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
extern __printf(3, 4) void __kunit_fail_current_test(const char *file, int line,
|
||||
const char *fmt, ...);
|
||||
|
||||
#else
|
||||
|
||||
static inline struct kunit *kunit_get_current_test(void) { return NULL; }
|
||||
|
||||
/* We define this with an empty helper function so format string warnings work */
|
||||
#define kunit_fail_current_test(fmt, ...) \
|
||||
__kunit_fail_current_test(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
|
||||
|
||||
static inline __printf(3, 4) void __kunit_fail_current_test(const char *file, int line,
|
||||
const char *fmt, ...)
|
||||
{
|
||||
}
|
||||
#define kunit_fail_current_test(fmt, ...) do {} while (0)
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -126,6 +126,14 @@ CFLAGS_test_fpu.o += $(FPU_CFLAGS)
|
||||
obj-$(CONFIG_TEST_LIVEPATCH) += livepatch/
|
||||
|
||||
obj-$(CONFIG_KUNIT) += kunit/
|
||||
# Include the KUnit hooks unconditionally. They'll compile to nothing if
|
||||
# CONFIG_KUNIT=n, otherwise will be a small table of static data (static key,
|
||||
# function pointers) which need to be built-in even when KUnit is a module.
|
||||
ifeq ($(CONFIG_KUNIT), m)
|
||||
obj-y += kunit/hooks.o
|
||||
else
|
||||
obj-$(CONFIG_KUNIT) += kunit/hooks.o
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_DEBUG_KOBJECT),y)
|
||||
CFLAGS_kobject.o += -DDEBUG
|
||||
|
@ -11,6 +11,9 @@ ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
|
||||
kunit-objs += debugfs.o
|
||||
endif
|
||||
|
||||
# KUnit 'hooks' are built-in even when KUnit is built as a module.
|
||||
lib-y += hooks.o
|
||||
|
||||
obj-$(CONFIG_KUNIT_TEST) += kunit-test.o
|
||||
|
||||
# string-stream-test compiles built-in only.
|
||||
|
27
lib/kunit/hooks-impl.h
Normal file
27
lib/kunit/hooks-impl.h
Normal file
@ -0,0 +1,27 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/*
|
||||
* Declarations for hook implementations.
|
||||
*
|
||||
* These will be set as the function pointers in struct kunit_hook_table,
|
||||
* found in include/kunit/test-bug.h.
|
||||
*
|
||||
* Copyright (C) 2023, Google LLC.
|
||||
* Author: David Gow <davidgow@google.com>
|
||||
*/
|
||||
|
||||
#ifndef _KUNIT_HOOKS_IMPL_H
|
||||
#define _KUNIT_HOOKS_IMPL_H
|
||||
|
||||
#include <kunit/test-bug.h>
|
||||
|
||||
/* List of declarations. */
|
||||
void __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...);
|
||||
|
||||
/* Code to set all of the function pointers. */
|
||||
static inline void kunit_install_hooks(void)
|
||||
{
|
||||
/* Install the KUnit hook functions. */
|
||||
kunit_hooks.fail_current_test = __kunit_fail_current_test_impl;
|
||||
}
|
||||
|
||||
#endif /* _KUNIT_HOOKS_IMPL_H */
|
21
lib/kunit/hooks.c
Normal file
21
lib/kunit/hooks.c
Normal file
@ -0,0 +1,21 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
/*
|
||||
* KUnit 'Hooks' implementation.
|
||||
*
|
||||
* This file contains code / structures which should be built-in even when
|
||||
* KUnit itself is built as a module.
|
||||
*
|
||||
* Copyright (C) 2022, Google LLC.
|
||||
* Author: David Gow <davidgow@google.com>
|
||||
*/
|
||||
|
||||
|
||||
#include <kunit/test-bug.h>
|
||||
|
||||
DEFINE_STATIC_KEY_FALSE(kunit_running);
|
||||
EXPORT_SYMBOL(kunit_running);
|
||||
|
||||
/* Function pointers for hooks. */
|
||||
struct kunit_hooks_table kunit_hooks;
|
||||
EXPORT_SYMBOL(kunit_hooks);
|
||||
|
@ -17,17 +17,14 @@
|
||||
#include <linux/sched.h>
|
||||
|
||||
#include "debugfs.h"
|
||||
#include "hooks-impl.h"
|
||||
#include "string-stream.h"
|
||||
#include "try-catch-impl.h"
|
||||
|
||||
DEFINE_STATIC_KEY_FALSE(kunit_running);
|
||||
EXPORT_SYMBOL_GPL(kunit_running);
|
||||
|
||||
#if IS_BUILTIN(CONFIG_KUNIT)
|
||||
/*
|
||||
* Fail the current test and print an error message to the log.
|
||||
* Hook to fail the current test and print an error message to the log.
|
||||
*/
|
||||
void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
|
||||
void __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int len;
|
||||
@ -54,8 +51,6 @@ void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
|
||||
kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
|
||||
kunit_kfree(current->kunit_test, buffer);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Enable KUnit tests to run.
|
||||
@ -778,6 +773,9 @@ EXPORT_SYMBOL_GPL(kunit_cleanup);
|
||||
|
||||
static int __init kunit_init(void)
|
||||
{
|
||||
/* Install the KUnit hook functions. */
|
||||
kunit_install_hooks();
|
||||
|
||||
kunit_debugfs_init();
|
||||
#ifdef CONFIG_MODULES
|
||||
return register_module_notifier(&kunit_mod_nb);
|
||||
@ -789,6 +787,7 @@ late_initcall(kunit_init);
|
||||
|
||||
static void __exit kunit_exit(void)
|
||||
{
|
||||
memset(&kunit_hooks, 0, sizeof(kunit_hooks));
|
||||
#ifdef CONFIG_MODULES
|
||||
unregister_module_notifier(&kunit_mod_nb);
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user