On Thu, Oct 17, 2019 at 11:08 AM Alan Maguire alan.maguire@oracle.com wrote:
In preparation for module support for kunit and kunit tests, we need a way of retrieving non-exported symbols from the core kernel and modules. kunit_find_symbol() supports this.
Signed-off-by: Alan Maguire alan.maguire@oracle.com Signed-off-by: Knut Omang knut.omang@oracle.com
I think you suggested on another thread that splitting this patch out of this patchset might be a good idea. I agree with that. Can you send this patch separately?
include/kunit/test.h | 8 ++++++++ lib/kunit/test-test.c | 19 +++++++++++++++++++ lib/kunit/test.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+)
diff --git a/include/kunit/test.h b/include/kunit/test.h index dba4830..c645d18 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -339,6 +339,14 @@ static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
void kunit_cleanup(struct kunit *test);
+/**
- kunit_find_symbol() - lookup un-exported symbol in kernel or modules.
- @sym: symbol name.
- Returns symbol or ERR_PTR value on error.
Can you document which ERR_PTRs it returns?
- */
+void *kunit_find_symbol(const char *sym);
#define kunit_printk(lvl, test, fmt, ...) \ printk(lvl "\t# %s: " fmt, (test)->name, ##__VA_ARGS__)
diff --git a/lib/kunit/test-test.c b/lib/kunit/test-test.c index c4162a9..7f09dd0 100644 --- a/lib/kunit/test-test.c +++ b/lib/kunit/test-test.c @@ -330,3 +330,22 @@ static void kunit_resource_test_exit(struct kunit *test) .test_cases = kunit_resource_test_cases, }; kunit_test_suite(kunit_resource_test_suite);
+/*
- Find non-exported kernel symbol; we use the modules list as a safe
- choice that should always be present.
- */
+static void kunit_find_symbol_kernel(struct kunit *test) +{
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, kunit_find_symbol("modules"));
I think this should be a KUNIT_EXPECT_... here since nothing in this test case depends on this check passing.
+}
+static struct kunit_case kunit_find_symbol_test_cases[] = {
KUNIT_CASE(kunit_find_symbol_kernel),
+};
+static struct kunit_suite kunit_find_symbol_test_suite = {
.name = "kunit-find-symbol",
.test_cases = kunit_find_symbol_test_cases,
+}; +kunit_test_suite(kunit_find_symbol_test_suite); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 49ac5fe..a2b1b46 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -8,6 +8,7 @@
#include <kunit/test.h> #include <kunit/try-catch.h> +#include <linux/kallsyms.h> #include <linux/kernel.h> #include <linux/sched/debug.h> #include "string-stream-impl.h" @@ -478,3 +479,38 @@ void kunit_cleanup(struct kunit *test) kunit_resource_free(test, resource); } }
+/*
- Support for looking up kernel/module internal symbols to enable testing.
- */
+void *kunit_find_symbol(const char *sym) +{
unsigned long (*modlookup)(const char *name);
unsigned long addr = 0;
if (!sym || strlen(sym) > KSYM_NAME_LEN)
return ERR_PTR(-EINVAL);
/*
* Try for kernel-internal symbol first; fall back to modules
* if that fails.
*/
addr = kallsyms_lookup_name(sym);
if (addr)
return (void *)addr;
nit: please add a newline here.
modlookup = (void *)kallsyms_lookup_name("module_kallsyms_lookup_name");
Can you add a comment here explaining what module_kallsyms_lookup_name is and why you need to look it up this way?
if (modlookup)
addr = modlookup(sym);
if (addr)
return (void *)addr;
+#ifndef CONFIG_KALLSYMS_ALL
WARN_ONCE(true,
"CONFIG_KALLSYMS_ALL is not set, so unexported symbols like '%s' are not available\n",
sym);
return ERR_PTR(-ENOTSUPP);
+#else
WARN_ONCE(true, "symbol '%s' is not available\n", sym);
+#endif
return ERR_PTR(-ENOENT);
+}
1.8.3.1