From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry@kernel.org>
Subject: [PATCH] KVM: SVM: Add a helper to get LBR field pointer to dedup MSR accesses
Date: Tue, 10 Mar 2026 15:04:14 -0700 [thread overview]
Message-ID: <20260310220414.2569208-1-seanjc@google.com> (raw)
Add a helper to get a pointer to the corresponding VMCB field given an LBR
MSR index, and use it to dedup the handling in svm_{g,s}et_msr().
No functional change intended.
Suggested-by: Yosry Ahmed <yosry@kernel.org>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
arch/x86/kvm/svm/svm.c | 48 +++++++++++++++++-------------------------
1 file changed, 19 insertions(+), 29 deletions(-)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 3407deac90bd..88999b6fc8a3 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2750,6 +2750,23 @@ static int svm_get_feature_msr(u32 msr, u64 *data)
return 0;
}
+static __always_inline u64 *svm_vmcb_lbr(struct vcpu_svm *svm, u32 msr)
+{
+ switch (msr) {
+ case MSR_IA32_LASTBRANCHFROMIP:
+ return &svm->vmcb->save.br_from;
+ case MSR_IA32_LASTBRANCHTOIP:
+ return &svm->vmcb->save.br_to;
+ case MSR_IA32_LASTINTFROMIP:
+ return &svm->vmcb->save.last_excp_from;
+ case MSR_IA32_LASTINTTOIP:
+ return &svm->vmcb->save.last_excp_to;
+ default:
+ break;
+ }
+ BUILD_BUG();
+}
+
static bool sev_es_prevent_msr_access(struct kvm_vcpu *vcpu,
struct msr_data *msr_info)
{
@@ -2826,16 +2843,10 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
msr_info->data = lbrv ? svm->vmcb->save.dbgctl : 0;
break;
case MSR_IA32_LASTBRANCHFROMIP:
- msr_info->data = lbrv ? svm->vmcb->save.br_from : 0;
- break;
case MSR_IA32_LASTBRANCHTOIP:
- msr_info->data = lbrv ? svm->vmcb->save.br_to : 0;
- break;
case MSR_IA32_LASTINTFROMIP:
- msr_info->data = lbrv ? svm->vmcb->save.last_excp_from : 0;
- break;
case MSR_IA32_LASTINTTOIP:
- msr_info->data = lbrv ? svm->vmcb->save.last_excp_to : 0;
+ msr_info->data = lbrv ? *svm_vmcb_lbr(svm, msr_info->index) : 0;
break;
case MSR_VM_HSAVE_PA:
msr_info->data = svm->nested.hsave_msr;
@@ -3111,35 +3122,14 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
svm_update_lbrv(vcpu);
break;
case MSR_IA32_LASTBRANCHFROMIP:
- if (!lbrv)
- return KVM_MSR_RET_UNSUPPORTED;
- if (!msr->host_initiated)
- return 1;
- svm->vmcb->save.br_from = data;
- vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
- break;
case MSR_IA32_LASTBRANCHTOIP:
- if (!lbrv)
- return KVM_MSR_RET_UNSUPPORTED;
- if (!msr->host_initiated)
- return 1;
- svm->vmcb->save.br_to = data;
- vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
- break;
case MSR_IA32_LASTINTFROMIP:
- if (!lbrv)
- return KVM_MSR_RET_UNSUPPORTED;
- if (!msr->host_initiated)
- return 1;
- svm->vmcb->save.last_excp_from = data;
- vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
- break;
case MSR_IA32_LASTINTTOIP:
if (!lbrv)
return KVM_MSR_RET_UNSUPPORTED;
if (!msr->host_initiated)
return 1;
- svm->vmcb->save.last_excp_to = data;
+ *svm_vmcb_lbr(svm, ecx) = data;
vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
break;
case MSR_VM_HSAVE_PA:
base-commit: 5128b972fb2801ad9aca54d990a75611ab5283a9
--
2.53.0.473.g4a7958ca14-goog
next reply other threads:[~2026-03-10 22:04 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-10 22:04 Sean Christopherson [this message]
2026-03-11 1:45 ` [PATCH] KVM: SVM: Add a helper to get LBR field pointer to dedup MSR accesses Yosry Ahmed
2026-03-13 18:23 ` Yosry Ahmed
2026-03-13 20:15 ` Sean Christopherson
2026-03-13 20:38 ` Yosry Ahmed
2026-03-13 22:17 ` Sean Christopherson
2026-03-13 22:25 ` Yosry Ahmed
2026-04-03 15:13 ` 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=20260310220414.2569208-1-seanjc@google.com \
--to=seanjc@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=yosry@kernel.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