mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sandipan Das <sandipan.das@amd.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] KVM: SVM: Emulate PERF_CNTR_GLOBAL_STATUS_SET for PerfMonV2
Date: Tue, 15 Jul 2025 20:08:33 +0530	[thread overview]
Message-ID: <ab9bcf19-3804-4678-99c2-d2ac8b04a343@amd.com> (raw)
In-Reply-To: <aHFL-QjqG4hDVV4I@google.com>

On 7/11/2025 11:08 PM, Sean Christopherson wrote:
> On Fri, Jul 11, 2025, Sean Christopherson wrote:
>> Emulate PERF_CNTR_GLOBAL_STATUS_SET when PerfMonV2 is enumerated to the
>> guest, as the MSR is supposed to exist in all AMD v2 PMUs.
>>
>> Fixes: 4a2771895ca6 ("KVM: x86/svm/pmu: Add AMD PerfMonV2 support")
>> Cc: stable@vger.kernel.org
>> Cc: Sandipan Das <sandipan.das@amd.com>
>> Signed-off-by: Sean Christopherson <seanjc@google.com>
>> ---
> 
> ...
> 
>> @@ -711,6 +712,10 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
>>  		if (!msr_info->host_initiated)
>>  			pmu->global_status &= ~data;
>>  		break;
>> +	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
>> +		if (!msr_info->host_initiated)
>> +			pmu->global_status |= data & ~pmu->global_status_rsvd;
>> +		break;
>>  	default:
>>  		kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
>>  		return kvm_pmu_call(set_msr)(vcpu, msr_info);
> 
> Tested with a hacky KUT test to verify I got the semantics correct.  I think I did?
> 
>   static void test_pmu_msrs(void)
>   {
> 	const unsigned long long rsvd = GENMASK_ULL(63, 6);
> 
> 	wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR, -1ull);
> 	report(!rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS),
> 	       "Wanted '0', got 0x%" PRIx64, rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS));
> 
> 	wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET, -1ull);
> 	report(rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS) == ~rsvd,
> 	       "Wanted '0x%llx', got 0x%" PRIx64,
> 	       ~rsvd, rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS));
> 
> 	wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR, -1ull);
> 	report(!rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS),
> 	       "Wanted '0', got 0x%" PRIx64, rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS));
> 
> 	wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET, 0);
> 	report(!rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS),
> 	       "Wanted '0', got 0x%" PRIx64, rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS));
>   }
> 
> One oddity is that the test fails when run on the mediated PMU on Turin, i.e. when
> the guest can write MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET directly.
> 
>   FAIL: Wanted '0x3f', got 0xc000000000000ff
> 
> Bits 59:58 failing is expected, because lack of KVM support for DebugCtl[FPCI]
> and DebugCtl[FLBRI] doesn't remove them from hardware.  Disabling interception
> of MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET creates a virtualization hole on that
> front, but I don't know that it's worth closing.  Letting the guest manually
> freeze its counters doesn't seem terribly interesting.
> 
> Bits 7:6 being set is _much_ more interesting, at least to me.  They're allegedly
> reserved per the APM, and CPUID 0x80000022 says there are only 6 counters, so...

Some of these reserved bits correspond to internal functionality :)
While it is not ideal for software to be able to toggle them, my understanding is
that these bits can be ignored. Otherwise, KVM has to intercept writes to both
MSR_AMD64_PERF_CNTR_GLOBAL_CTRL and MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET.

  reply	other threads:[~2025-07-15 14:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-11 17:27 Sean Christopherson
2025-07-11 17:38 ` Sean Christopherson
2025-07-15 14:38   ` Sandipan Das [this message]
2025-08-19 23:12 ` 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=ab9bcf19-3804-4678-99c2-d2ac8b04a343@amd.com \
    --to=sandipan.das@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.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

all inboxes | Powered by JetHome®