From: Wei Zhang <zhanwei@google.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Wei Zhang <zhanwei@google.com>,
Suleiman Souhlal <suleiman@google.com>,
Sangwhan Moon <sxm@google.com>, Ingo Molnar <mingo@redhat.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Sean Christopherson <seanjc@google.com>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] KVM: x86: allow guest to send its _stext for kvm profiling
Date: Tue, 12 Apr 2022 19:58:45 +0000 [thread overview]
Message-ID: <20220412195846.3692374-2-zhanwei@google.com> (raw)
In-Reply-To: <20220412195846.3692374-1-zhanwei@google.com>
The profiling buffer is indexed by (pc - _stext) in do_profile_hits(),
which doesn't work for KVM profiling because the pc represents an address
in the guest kernel. readprofile is broken in this case, unless the guest
kernel happens to have the same _stext as the host kernel.
This patch adds a new hypercall so guests could send its _stext to the
host, which will then be used to adjust the calculation for KVM profiling.
Signed-off-by: Wei Zhang <zhanwei@google.com>
---
arch/x86/kvm/x86.c | 15 +++++++++++++++
include/linux/kvm_host.h | 4 ++++
include/uapi/linux/kvm_para.h | 1 +
virt/kvm/Kconfig | 5 +++++
4 files changed, 25 insertions(+)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 547ba00ef64f..abeacdd5d362 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9246,6 +9246,12 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
vcpu->arch.complete_userspace_io = complete_hypercall_exit;
return 0;
}
+#ifdef CONFIG_ACCURATE_KVM_PROFILING
+ case KVM_HC_GUEST_STEXT:
+ vcpu->kvm->guest_stext = a0;
+ ret = 0;
+ break;
+#endif
default:
ret = -KVM_ENOSYS;
break;
@@ -10261,6 +10267,15 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
*/
if (unlikely(prof_on == KVM_PROFILING)) {
unsigned long rip = kvm_rip_read(vcpu);
+#ifdef CONFIG_ACCURATE_KVM_PROFILING
+ /*
+ * Profiling buffer is indexed by (rip - _stext), but it's
+ * supposed to be indexed by (rip - guest_stext) instead.
+ * Therefore apply an offest in advance to get correct results.
+ */
+ if (vcpu->kvm->guest_stext)
+ rip += (unsigned long)_stext - vcpu->kvm->guest_stext;
+#endif
profile_hit(KVM_PROFILING, (void *)rip);
}
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 3f9b22c4983a..65caaa4d87c4 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -781,6 +781,10 @@ struct kvm {
struct notifier_block pm_notifier;
#endif
char stats_id[KVM_STATS_NAME_SIZE];
+
+#ifdef CONFIG_ACCURATE_KVM_PROFILING
+ unsigned long guest_stext;
+#endif
};
#define kvm_err(fmt, ...) \
diff --git a/include/uapi/linux/kvm_para.h b/include/uapi/linux/kvm_para.h
index 960c7e93d1a9..dcb4ba1f033c 100644
--- a/include/uapi/linux/kvm_para.h
+++ b/include/uapi/linux/kvm_para.h
@@ -30,6 +30,7 @@
#define KVM_HC_SEND_IPI 10
#define KVM_HC_SCHED_YIELD 11
#define KVM_HC_MAP_GPA_RANGE 12
+#define KVM_HC_GUEST_STEXT 13
/*
* hypercalls use architecture specific
diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig
index a8c5c9f06b3c..8798f75ddade 100644
--- a/virt/kvm/Kconfig
+++ b/virt/kvm/Kconfig
@@ -72,3 +72,8 @@ config KVM_XFER_TO_GUEST_WORK
config HAVE_KVM_PM_NOTIFIER
bool
+
+# Offer an additional hypercall to a guest so it could pass value of _stext to
+# host, which will be used to adjust the calculation of KVM profiling.
+config ACCURATE_KVM_PROFILING
+ bool
--
2.35.1.1178.g4f1659d476-goog
next prev parent reply other threads:[~2022-04-12 20:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-12 19:58 [PATCH 0/2] KVM: x86: Fix incorrect VM-exit profiling Wei Zhang
2022-04-12 19:58 ` Wei Zhang [this message]
2022-05-09 23:55 ` [PATCH 1/2] KVM: x86: allow guest to send its _stext for kvm profiling Sean Christopherson
2022-05-11 16:45 ` Wei Zhang
2022-04-12 19:58 ` [PATCH 2/2] KVM: x86: illustrative example for sending guest _stext with a hypercall Wei Zhang
2022-05-09 23:57 ` [PATCH 0/2] KVM: x86: Fix incorrect VM-exit profiling Sean Christopherson
2022-05-11 16:45 ` Wei Zhang
2022-05-11 19:42 ` Sean Christopherson
2022-05-16 19:30 ` Wei Zhang
2022-05-18 4:27 ` Suleiman Souhlal
2022-05-18 15:34 ` Sean Christopherson
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=20220412195846.3692374-2-zhanwei@google.com \
--to=zhanwei@google.com \
--cc=jmattson@google.com \
--cc=joro@8bytes.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=suleiman@google.com \
--cc=sxm@google.com \
--cc=vkuznets@redhat.com \
--cc=wanpengli@tencent.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox