From: Tzung-Bi Shih <tzungbi@kernel.org>
To: bleung@chromium.org, brendan.higgins@linux.dev, davidgow@google.com
Cc: tzungbi@kernel.org, rmoar@google.com, rostedt@goodmis.org,
mhiramat@kernel.org, naveen@kernel.org,
anil.s.keshavamurthy@intel.com, davem@davemloft.net,
chrome-platform@lists.linux.dev, linux-kselftest@vger.kernel.org,
kunit-dev@googlegroups.com, linux-trace-kernel@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [RFC PATCH 6/7] kunit: Expose 'kprobes stub' API to redirect functions
Date: Tue, 20 May 2025 08:24:33 +0000 [thread overview]
Message-ID: <20250520082435.2255639-7-tzungbi@kernel.org> (raw)
In-Reply-To: <20250520082435.2255639-1-tzungbi@kernel.org>
Add function redirection API based on Kprobes.
Suggested-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
include/kunit/kprobes_stub.h | 19 ++++++
lib/kunit/Kconfig | 7 +++
lib/kunit/Makefile | 4 ++
lib/kunit/kprobes_stub.c | 113 +++++++++++++++++++++++++++++++++++
4 files changed, 143 insertions(+)
create mode 100644 include/kunit/kprobes_stub.h
create mode 100644 lib/kunit/kprobes_stub.c
diff --git a/include/kunit/kprobes_stub.h b/include/kunit/kprobes_stub.h
new file mode 100644
index 000000000000..af77c86fe48e
--- /dev/null
+++ b/include/kunit/kprobes_stub.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _KUNIT_KPROBES_STUB_H
+#define _KUNIT_KPROBES_STUB_H
+
+struct kunit;
+
+#define kunit_activate_kprobes_stub(test, func, replacement) do { \
+ typecheck_fn(typeof(&func), replacement); \
+ __kunit_activate_kprobes_stub(test, #func, func, replacement); \
+} while (0)
+
+void __kunit_activate_kprobes_stub(struct kunit *test,
+ const char *name,
+ void *real_fn_addr,
+ void *replacement_addr);
+
+void kunit_deactivate_kprobes_stub(struct kunit *test, void *real_fn_addr);
+
+#endif /* _KUNIT_KPROBES_STUB_H */
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
index 933fda1df5c3..13fdd3471060 100644
--- a/lib/kunit/Kconfig
+++ b/lib/kunit/Kconfig
@@ -104,4 +104,11 @@ config KUNIT_FTRACE_STUBS
NOTE: this does not work on all architectures (like UML, arm64) and
relies on a lot of magic (see the dependencies list).
+config KUNIT_KPROBES_STUBS
+ bool "Support for stubbing out functions in KUnit tests with kprobes"
+ depends on KPROBES
+ help
+ Builds support for stubbing out functions for the duration of KUnit
+ test cases or suites using kprobes.
+
endif # KUNIT
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index 0ecb255576e2..727ce86eb61f 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -33,3 +33,7 @@ obj-$(CONFIG_KUNIT_EXAMPLE_TEST) += kunit-example-test.o
ifeq ($(CONFIG_KUNIT_FTRACE_STUBS),y)
kunit-objs += ftrace_stub.o
endif
+
+ifeq ($(CONFIG_KUNIT_KPROBES_STUBS),y)
+kunit-objs += kprobes_stub.o
+endif
diff --git a/lib/kunit/kprobes_stub.c b/lib/kunit/kprobes_stub.c
new file mode 100644
index 000000000000..95f1dcba346b
--- /dev/null
+++ b/lib/kunit/kprobes_stub.c
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit function redirection (kprobes stubbing) API.
+ */
+
+#include <kunit/test.h>
+#include <kunit/kprobes_stub.h>
+
+#include <linux/kprobes.h>
+
+struct kunit_kprobes_stub_ctx {
+ unsigned long real_fn_addr;
+ unsigned long replacement_addr;
+ struct kprobe kp;
+};
+
+static void __kunit_kprobes_stub_resource_free(struct kunit_resource *res)
+{
+ struct kunit_kprobes_stub_ctx *ctx = res->data;
+
+ unregister_kprobe(&ctx->kp);
+ kfree(ctx);
+}
+
+static int kprobe_handler(struct kprobe *kp, struct pt_regs *regs)
+{
+ struct kunit_kprobes_stub_ctx *ctx = container_of(kp, struct kunit_kprobes_stub_ctx, kp);
+
+ instruction_pointer_set(regs, ctx->replacement_addr);
+ return 1;
+}
+
+/* Matching function for kunit_find_resource(). match_data is real_fn_addr. */
+static bool __kunit_kprobes_stub_resource_match(struct kunit *test,
+ struct kunit_resource *res,
+ void *match_real_fn_addr)
+{
+ struct kunit_kprobes_stub_ctx *ctx = res->data;
+
+ /* Make sure the resource is a kprobes stub resource. */
+ if (res->free != &__kunit_kprobes_stub_resource_free)
+ return false;
+
+ return ctx->real_fn_addr == (unsigned long)match_real_fn_addr;
+}
+
+void __kunit_activate_kprobes_stub(struct kunit *test,
+ const char *name,
+ void *real_fn_addr,
+ void *replacement_addr)
+{
+ struct kunit_kprobes_stub_ctx *ctx;
+ struct kunit_resource *res;
+
+ KUNIT_ASSERT_PTR_NE_MSG(test, real_fn_addr, NULL,
+ "Tried to activate a stub for function NULL");
+
+ /* If the replacement address is NULL, deactivate the stub. */
+ if (!replacement_addr) {
+ kunit_deactivate_kprobes_stub(test, real_fn_addr);
+ return;
+ }
+
+ /* Look up any existing stubs for this function, and replace them. */
+ res = kunit_find_resource(test,
+ __kunit_kprobes_stub_resource_match,
+ real_fn_addr);
+ if (res) {
+ ctx = res->data;
+ ctx->replacement_addr = (unsigned long)replacement_addr;
+
+ /* We got an extra reference from find_resource(), so put it. */
+ kunit_put_resource(res);
+ } else {
+ ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL_MSG(test, ctx, "Failed to allocate kunit stub for %s",
+ name);
+
+ ctx->real_fn_addr = (unsigned long)real_fn_addr;
+ ctx->replacement_addr = (unsigned long)replacement_addr;
+
+ ctx->kp.addr = real_fn_addr;
+ ctx->kp.pre_handler = kprobe_handler;
+ KUNIT_ASSERT_EQ_MSG(test, register_kprobe(&ctx->kp), 0,
+ "Failed to allocate kunit stub for %s", name);
+
+ kunit_alloc_resource(test, NULL,
+ __kunit_kprobes_stub_resource_free,
+ GFP_KERNEL, ctx);
+ }
+}
+EXPORT_SYMBOL_GPL(__kunit_activate_kprobes_stub);
+
+void kunit_deactivate_kprobes_stub(struct kunit *test, void *real_fn_addr)
+{
+ struct kunit_resource *res;
+
+ KUNIT_ASSERT_PTR_NE_MSG(test, real_fn_addr, NULL, "Tried to deactivate a NULL stub.");
+
+ res = kunit_find_resource(test,
+ __kunit_kprobes_stub_resource_match,
+ real_fn_addr);
+ KUNIT_ASSERT_PTR_NE_MSG(test, res, NULL,
+ "Tried to deactivate a nonexistent stub.");
+
+ /*
+ * Free the stub. We 'put' twice, as we got a reference
+ * from kunit_find_resource()
+ */
+ kunit_remove_resource(test, res);
+ kunit_put_resource(res);
+}
+EXPORT_SYMBOL_GPL(kunit_deactivate_kprobes_stub);
--
2.49.0.1101.gccaa498523-goog
next prev parent reply other threads:[~2025-05-20 8:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-20 8:24 [RFC PATCH 0/7] platform/chrome: Add Kunit tests for protocol device drivers Tzung-Bi Shih
2025-05-20 8:24 ` [RFC PATCH 1/7] kunit: expose ftrace-based API for stubbing out functions during tests Tzung-Bi Shih
2025-05-20 8:24 ` [RFC PATCH 2/7] platform/chrome: kunit: cros_ec_i2c: Add tests with ftrace stub Tzung-Bi Shih
2025-05-20 8:24 ` [RFC PATCH 3/7] platform/chrome: kunit: cros_ec_i2c: Use static stub instead Tzung-Bi Shih
2025-05-20 8:24 ` [RFC PATCH 4/7] platform/chrome: kunit: cros_ec_spi: Add tests with ftrace stub Tzung-Bi Shih
2025-05-20 8:24 ` [RFC PATCH 5/7] platform/chrome: kunit: cros_ec_spi: Call .probe() directly Tzung-Bi Shih
2025-05-20 8:24 ` Tzung-Bi Shih [this message]
2025-05-20 8:24 ` [RFC PATCH 7/7] platform/chrome: kunit: cros_ec_spi: Use kprobes stub instead Tzung-Bi Shih
2025-05-20 16:04 ` [RFC PATCH 0/7] platform/chrome: Add Kunit tests for protocol device drivers Daniel Latypov
2025-06-30 6:32 ` Tzung-Bi Shih
2025-07-01 5:22 ` Daniel Latypov
2025-07-01 12:20 ` Tzung-Bi Shih
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=20250520082435.2255639-7-tzungbi@kernel.org \
--to=tzungbi@kernel.org \
--cc=anil.s.keshavamurthy@intel.com \
--cc=bleung@chromium.org \
--cc=brendan.higgins@linux.dev \
--cc=chrome-platform@lists.linux.dev \
--cc=davem@davemloft.net \
--cc=davidgow@google.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=naveen@kernel.org \
--cc=rmoar@google.com \
--cc=rostedt@goodmis.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox