mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tianyi Liu <i.pear@outlook.com>
To: peterz@infradead.org, acme@kernel.org,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: mingo@redhat.com, mark.rutland@arm.com,
	alexander.shishkin@linux.intel.com, jolsa@kernel.org,
	namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com,
	Tianyi Liu <i.pear@outlook.com>
Subject: [PATCH] Introduce callchains for guests
Date: Thu, 31 Aug 2023 16:09:02 +0800	[thread overview]
Message-ID: <SY4P282MB10848199D3F93AA47A845B8B9DE5A@SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM> (raw)

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@outlook.com>
---
 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


             reply	other threads:[~2023-08-31  8:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-31  8:09 Tianyi Liu [this message]
2023-08-31  9:45 ` kernel test robot

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=SY4P282MB10848199D3F93AA47A845B8B9DE5A@SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM \
    --to=i.pear@outlook.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.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

all inboxes | Powered by JetHome®