All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alan Maguire <alan.maguire@oracle.com>
To: brendanhiggins@google.com, linux-kselftest@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com,
	keescook@chromium.org, yzaikin@google.com,
	akpm@linux-foundation.org, yamada.masahiro@socionext.com,
	catalin.marinas@arm.com, joe.lawrence@redhat.com,
	penguin-kernel@i-love.sakura.ne.jp, schowdary@nvidia.com,
	urezki@gmail.com, andriy.shevchenko@linux.intel.com,
	corbet@lwn.net, linux-doc@vger.kernel.org,
	Alan Maguire <alan.maguire@oracle.com>,
	Knut Omang <knut.omang@oracle.com>
Subject: [PATCH v3 linux-kselftest-test 3/6] kunit: add kunit_find_symbol() function for symbol lookup
Date: Thu, 17 Oct 2019 19:07:16 +0100	[thread overview]
Message-ID: <1571335639-21675-4-git-send-email-alan.maguire@oracle.com> (raw)
In-Reply-To: <1571335639-21675-1-git-send-email-alan.maguire@oracle.com>

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>
---
 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.
+ */
+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"));
+}
+
+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;
+	modlookup = (void *)kallsyms_lookup_name("module_kallsyms_lookup_name");
+	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


  parent reply	other threads:[~2019-10-17 18:08 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-17 18:07 [PATCH v3 linux-kselftest-test 0/6] kunit: support building core/tests as modules Alan Maguire
2019-10-17 18:07 ` [PATCH v3 linux-kselftest-test 1/6] kunit: move string-stream.h to lib/kunit/string-stream-impl.h Alan Maguire
2019-11-08  1:01   ` Brendan Higgins
2019-10-17 18:07 ` [PATCH v3 linux-kselftest-test 2/6] kunit: hide unexported try-catch interface in try-catch-impl.h Alan Maguire
2019-11-08  1:07   ` Brendan Higgins
2019-10-17 18:07 ` Alan Maguire [this message]
2019-11-08  1:24   ` [PATCH v3 linux-kselftest-test 3/6] kunit: add kunit_find_symbol() function for symbol lookup Brendan Higgins
2019-10-17 18:07 ` [PATCH v3 linux-kselftest-test 4/6] kunit: allow kunit tests to be loaded as a module Alan Maguire
2019-11-08  1:40   ` Brendan Higgins
2019-10-17 18:07 ` [PATCH v3 linux-kselftest-test 5/6] kunit: allow kunit " Alan Maguire
2019-11-08  2:43   ` Brendan Higgins
2019-11-08  3:02     ` Brendan Higgins
2019-11-08 15:30     ` Alan Maguire
2019-11-11 21:41       ` Brendan Higgins
     [not found]         ` <20191114063815.9403820706@mail.kernel.org>
     [not found]           ` <alpine.LRH.2.20.1911140750450.8907@dhcp-10-175-202-216.vpn.oracle.com>
2019-11-14 21:31             ` Brendan Higgins
2019-11-14 21:33               ` Brendan Higgins
2019-10-17 18:07 ` [PATCH v3 linux-kselftest-test 6/6] kunit: update documentation to describe module-based build Alan Maguire

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1571335639-21675-4-git-send-email-alan.maguire@oracle.com \
    --to=alan.maguire@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=brendanhiggins@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=corbet@lwn.net \
    --cc=joe.lawrence@redhat.com \
    --cc=keescook@chromium.org \
    --cc=knut.omang@oracle.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=penguin-kernel@i-love.sakura.ne.jp \
    --cc=schowdary@nvidia.com \
    --cc=urezki@gmail.com \
    --cc=yamada.masahiro@socionext.com \
    --cc=yzaikin@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.