[PATCH] Introduce callchains for guests

From: Tianyi Liu
Date: Thu Aug 31 2023 - 04:11:57 EST


This patch serves as the foundation for enabling callchains for guests,
(used by `perf kvm`). This functionality is useful for me, and I
noticed it holds the top spot on the perf wiki TODO list [1], so I'd like
to implement it. This patch introduces a `perf_callchain_guest` interface,
which different architectures can implement according to their own
specifications.

This is part of a series of patches. Since these patches are spread across
various modules like perf, kvm, arch/*, I plan to first submit some
foundational patches and gradually submit the complete implementation.
The full implementation can be found at [2], and it has been tested on
an x86_64 machine.

Since perf tools currently do not support callchain types with
PERF_CONTEXT_GUEST*, I temporarily used PERF_CONTEXT_KERNEL for ease of
testing and validation. This will be rectified once the perf tools patches
are applied.

Furthermore, do you think it's necessary to introduce a new guest filter
in the parameters of `get_perf_callchain`? Alternatively, should we add
two separate filters, `guest_user` and `guest_kernel`, to make these
filters entirely independent?

This is my first time submitting a patch for a new feature in the kernel,
and I would greatly appreciate any suggestions.

[1] https://perf.wiki.kernel.org/index.php/Todo
[2] https://github.com/i-Pear/linux/pull/2/files

Signed-off-by: Tianyi Liu <i.pear@xxxxxxxxxxx>
---
kernel/events/callchain.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/kernel/events/callchain.c b/kernel/events/callchain.c
index 1273be84392c..eacb6ccf7034 100644
--- a/kernel/events/callchain.c
+++ b/kernel/events/callchain.c
@@ -45,6 +45,10 @@ __weak void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
{
}

+__weak void perf_callchain_guest(struct perf_callchain_entry_ctx *entry)
+{
+}
+
static void release_callchain_buffers_rcu(struct rcu_head *head)
{
struct callchain_cpus_entries *entries;
@@ -183,6 +187,7 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
struct perf_callchain_entry *entry;
struct perf_callchain_entry_ctx ctx;
int rctx;
+ unsigned int guest_state;

entry = get_callchain_entry(&rctx);
if (!entry)
@@ -194,6 +199,26 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
ctx.contexts = 0;
ctx.contexts_maxed = false;

+ guest_state = perf_guest_state();
+ if (guest_state) {
+ if (add_mark) {
+ if (guest_state & PERF_GUEST_USER)
+ /*
+ * TODO: Change this to PERF_CONTEXT_GUEST_USER,
+ * which is currently not recognized by perf utils.
+ */
+ perf_callchain_store_context(&ctx, PERF_CONTEXT_KERNEL);
+ else
+ /*
+ * TODO: Change this to PERF_CONTEXT_GUEST_KERNEL,
+ * which is currently not recognized by perf utils.
+ */
+ perf_callchain_store_context(&ctx, PERF_CONTEXT_KERNEL);
+ }
+ perf_callchain_guest(&ctx);
+ goto exit_put;
+ }
+
if (kernel && !user_mode(regs)) {
if (add_mark)
perf_callchain_store_context(&ctx, PERF_CONTEXT_KERNEL);
--
2.34.1