From nobody Wed Oct 8 16:43:49 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3317D335C7 for ; Thu, 26 Jun 2025 00:12:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896752; cv=none; b=WX9564IZK2WUPyEK5WHF678vuvQo6IDZJJC0Z3Nk3WgIQxSmnBysD92PZuGzxLIunRFfiu1F06q7uAY9fW+hAgiMGdu4n6/0/bFsKR0m+ipibuopm9n/rlQLpfrhzQWSugOB/dF25dneAZ4E4Gy8E7GkyoyKVg4DUesK8XjNG9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896752; c=relaxed/simple; bh=2XyDfFWH/0eDwvNxvoMEhqm8qOkWL90TlQT4lYrrQX4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=thQT9Azzw9chafA1RAUO0HNpf6oZl1znKH7OiMXnJWgdFfCURn1oQbhbWlTiy15lQqgmDlIAlKtAfbk13QmkjxXy/B002zncl4Bp/ODQLDh8ShD44W1IZu2Gef9PznLYSFN28yWJSRmomEREltVDaJdq5MYxtmbMZnh7hFu5QL4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RZAC4igl; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RZAC4igl" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3138e671316so335679a91.0 for ; Wed, 25 Jun 2025 17:12:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750896750; x=1751501550; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XIDgjwUm8CjgtXg9AIcmlFZZ7eZmoPHxzXcxCBew1V0=; b=RZAC4iglHDc5wFi5q3yqGFjPakn8kr1A7dPSAmqa1fhRxDppBSuhFxdmXgCipNd2SD s5+bMK70UA0lJZxbmumw0gRV/Qpuj6DLs8plH4pE8X7VjGEfrji88BuGbyyOl0E8e0WH ZSFE888OtxweQHNUd+l18qw6qPJW95DJNjnM7F0TAJrJ2O2nZGURWBKeZYGImrRWlJgz qyryiiE5sVAxg3ojccoa29ePlbd+lvAVkVd5mkUlrwPeFtvRJxJALJmfIUmw9iFwjQrv Bfqim8qncHG8l9BOjMKKLQpgrDaCK6cDXsU5mb0ukcseGpM9mDa5MTkbWRHCiCjfoKuf GXsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750896750; x=1751501550; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XIDgjwUm8CjgtXg9AIcmlFZZ7eZmoPHxzXcxCBew1V0=; b=tdbZ9yNFL4zqqhdw/q+0TnvjMaZIAA74JJz27kbxWy44Em69d9Rm15UG3O1E8vDlF7 rz6n6fOUgH5psTmsuZWgXSg7hGKyDLtv8QnjYyL4tkr/Ck0Up6SBcEkrTCqvbbJ7mgI+ t9n59YxLfCCU0UaLycMXAqe4Dv45gV9eHIJOiFQv6vI5p+r1RHTiEgVfMLRV0Frd/Fxq YAwOvAdN8lbjJ46SaXYWRKCW2KzQqTOlGhSQVb1137KI8B2yGWfpDI20y1DvV0m21vXH euZo2LR6dFxVWfF3y2HVwvZJCBrHxBtz1PRgY1/f9pBF26oHZRummoYW+W1LsEPxThrM Q/kQ== X-Forwarded-Encrypted: i=1; AJvYcCU1zS/icVUA55j+yFO4fPYBqX4Ly9VBjScqtkDhSZWzQcZ8oGrdhUDZsdknmYq7wbxSbS6HEFGBXTEkG4o=@vger.kernel.org X-Gm-Message-State: AOJu0YwR5xvIMkZJbACLEgnDjjJfZn+Ee4Bt9yJ/h4co2GcN/9mkYQs1 1VzA76/KGiRX4pDAAh+c0Rfel3avY5on/al+eBN30nctkcjWmq+8SLF2TBLzLjloWGjpIKFK6NE 9nnx5hw== X-Google-Smtp-Source: AGHT+IFxqzI3I7qPfHhdkJFEVHE7dAj1xtEraHFWyog88QsjEjNsPNLon63KPUv4mv/v10Bg3S343op+M7c= X-Received: from pjvf13.prod.google.com ([2002:a17:90a:da8d:b0:2fe:800f:23a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5345:b0:311:1617:5bc4 with SMTP id 98e67ed59e1d1-316ed3a99d5mr1851989a91.12.1750896750459; Wed, 25 Jun 2025 17:12:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 25 Jun 2025 17:12:21 -0700 In-Reply-To: <20250626001225.744268-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250626001225.744268-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250626001225.744268-2-seanjc@google.com> Subject: [PATCH v5 1/5] KVM: x86: Replace growing set of *_in_guest bools with a u64 From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jim Mattson Store each "disabled exit" boolean in a single bit rather than a byte. No functional change intended. Suggested-by: Sean Christopherson Signed-off-by: Jim Mattson Link: https://lore.kernel.org/r/20250530185239.2335185-2-jmattson@google.com Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- arch/x86/include/asm/kvm_host.h | 5 +---- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 9 +-------- arch/x86/kvm/x86.h | 13 +++++++++---- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index bf26dfb5416d..6fdeeb5870b3 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1407,10 +1407,7 @@ struct kvm_arch { =20 gpa_t wall_clock; =20 - bool mwait_in_guest; - bool hlt_in_guest; - bool pause_in_guest; - bool cstate_in_guest; + u64 disabled_exits; =20 s64 kvmclock_offset; =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index ab11d1d0ec51..5ad54640314f 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5015,7 +5015,7 @@ static int svm_vm_init(struct kvm *kvm) } =20 if (!pause_filter_count || !pause_filter_thresh) - kvm->arch.pause_in_guest =3D true; + kvm_disable_exits(kvm, KVM_X86_DISABLE_EXITS_PAUSE); =20 if (enable_apicv) { int ret =3D avic_vm_init(kvm); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 65949882afa9..702486e7511c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7519,7 +7519,7 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu) int vmx_vm_init(struct kvm *kvm) { if (!ple_gap) - kvm->arch.pause_in_guest =3D true; + kvm_disable_exits(kvm, KVM_X86_DISABLE_EXITS_PAUSE); =20 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { switch (l1tf_mitigation) { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2806f7104295..2d893993262b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6481,14 +6481,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) pr_warn_once(SMT_RSB_MSG); =20 - if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) - kvm->arch.pause_in_guest =3D true; - if (cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) - kvm->arch.mwait_in_guest =3D true; - if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) - kvm->arch.hlt_in_guest =3D true; - if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) - kvm->arch.cstate_in_guest =3D true; + kvm_disable_exits(kvm, cap->args[0]); r =3D 0; disable_exits_unlock: mutex_unlock(&kvm->lock); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index db4e6a90e83d..31ae58b765f3 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -521,24 +521,29 @@ static inline u64 nsec_to_cycles(struct kvm_vcpu *vcp= u, u64 nsec) __rem; \ }) =20 +static inline void kvm_disable_exits(struct kvm *kvm, u64 mask) +{ + kvm->arch.disabled_exits |=3D mask; +} + static inline bool kvm_mwait_in_guest(struct kvm *kvm) { - return kvm->arch.mwait_in_guest; + return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_MWAIT; } =20 static inline bool kvm_hlt_in_guest(struct kvm *kvm) { - return kvm->arch.hlt_in_guest; + return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_HLT; } =20 static inline bool kvm_pause_in_guest(struct kvm *kvm) { - return kvm->arch.pause_in_guest; + return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_PAUSE; } =20 static inline bool kvm_cstate_in_guest(struct kvm *kvm) { - return kvm->arch.cstate_in_guest; + return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_CSTATE; } =20 static inline bool kvm_notify_vmexit_enabled(struct kvm *kvm) --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Wed Oct 8 16:43:49 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D71922EB10 for ; Thu, 26 Jun 2025 00:12:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896754; cv=none; b=RZbduK1rKCeiRywHKgPRAfrA+3XSBPx2L1j+CWUMqRSg21Cfw9xqgclglUV03nNZ/iMNuJh4azt7RrZxK1bJYXE6nlQlyT13MNNCJ8drp6qFsOhZznHVUAcjgAh6fyVjefXNpiuGH6wNZD3KlG+Y3ndmlpINi8gSkcGQQla4dBw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896754; c=relaxed/simple; bh=wz6AYJSwpgL+PNCPg7+NHYUGvEuhrNDWmDHVzVRm01k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ETsblY1jn6QJQvZtmGwv7L75rS3skjVbH6SyAHJPlUHhsnKvDpodzaN45ecRBnnaluYXC9ZbJIgHBoeapxZRhz3EeP2+SZpKOzxv5R4vMld2DZ88grqnFaQhvjmB6Bp25pZHZCCPFyf30vffIOVUhs2lUcPFhCkxrV1wcFcej4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3sMKlwz9; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3sMKlwz9" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-311d670ad35so303992a91.3 for ; Wed, 25 Jun 2025 17:12:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750896752; x=1751501552; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XSP+SKXtRx+biMOjK34h1pSwHQW4K9lXK2fj/zxr4DU=; b=3sMKlwz9qPtKKLaEpNon+V6uLYLytKWqOA+RyWF/ioTyc1QZA8WbEacOgBe1SYIkLd 1MZlSwqn/yMs7AS7LuFpSGlD+PRGCnlAaUf8fUUM+ddsKDJJjt7wxKireXpQEaI5RZWd nmqh+praSMt2kgtb1xfdrg8JMR2JO33vZW/Fhh36uMu59Cdfuq7BSbV51eKDCO/Ph8gs aG75en7P7+B3DgKZt08dGuCXbBm1nPte/6IozlYOqhRBLTzHNsY6t0t32S66wmRFOBAV wy/jz2/d1umwICkkjUs3FoMo6SE4RgLuwGkqpZvheoW9deAOLiRflQflehVTARglM1ca X0ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750896752; x=1751501552; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XSP+SKXtRx+biMOjK34h1pSwHQW4K9lXK2fj/zxr4DU=; b=ev95Z+iro0sqnKuuUgOsg7MMR5pymiZj3AdapGA1I0oBKDcLgivF4E9fEMTqJv6V3I 3g2znYw2FHyQc1jvNwFMt6Oo0XXuTb+VBxoibPcWddjpw9bzi9FOh8ZTODWfgtHrrzoM 6La67hAqiW9HThdwi/KVT6J7r2ZdL47o3zgth5go3bOCxFCrrh5IbtbyjHMrHgPR/1my vO0js0ul+3YPbDQHpbHZvLH6wYI2hBk25ieOhbsVcSWKj7/rAToI7bSeBAeKQvKUngN3 ZzPEcWQO4QpvGRqChBE+y+vUIQ1Zv7BHgjiiTNwGZpzvZPHTjtDNr+8wb0uBzaErxaA9 kufA== X-Forwarded-Encrypted: i=1; AJvYcCWaaaHZJecakPKlaYScn9Ng+Z9VGltFS51/hcKCjS1avIrJc80GrfshC2Q6AVDqOuf2M2mHs6ahpJt0uOA=@vger.kernel.org X-Gm-Message-State: AOJu0YzRTAzN1413N9dYKLb2UsAvaIgGWOqvdwEKrBxxiRHfaths4TS0 zSuTjb84T42uha8VmyvwNnzoQlxV6x59v2oVkruOxueNJ3lT7qMuK4H/FObkp7IPJVHTkIlIM4S KdJOEFg== X-Google-Smtp-Source: AGHT+IFx2aeEYWWp0A4eOd+dZkDdacmFG8AeQwkHeIe1hmy4RL4XWPzZG1HZhh54tyUmB3eNC5v+ZYUBeck= X-Received: from pjbsz14.prod.google.com ([2002:a17:90b:2d4e:b0:313:1c10:3595]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5484:b0:312:1d2d:18e1 with SMTP id 98e67ed59e1d1-315f2671e07mr5802724a91.22.1750896752146; Wed, 25 Jun 2025 17:12:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 25 Jun 2025 17:12:22 -0700 In-Reply-To: <20250626001225.744268-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250626001225.744268-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250626001225.744268-3-seanjc@google.com> Subject: [PATCH v5 2/5] KVM: x86: Provide a capability to disable APERF/MPERF read intercepts From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jim Mattson Allow a guest to read the physical IA32_APERF and IA32_MPERF MSRs without interception. The IA32_APERF and IA32_MPERF MSRs are not virtualized. Writes are not handled at all. The MSR values are not zeroed on vCPU creation, saved on suspend, or restored on resume. No accommodation is made for processor migration or for sharing a logical processor with other tasks. No adjustments are made for non-unit TSC multipliers. The MSRs do not account for time the same way as the comparable PMU events, whether the PMU is virtualized by the traditional emulation method or the new mediated pass-through approach. Nonetheless, in a properly constrained environment, this capability can be combined with a guest CPUID table that advertises support for CPUID.6:ECX.APERFMPERF[bit 0] to induce a Linux guest to report the effective physical CPU frequency in /proc/cpuinfo. Moreover, there is no performance cost for this capability. Signed-off-by: Jim Mattson Link: https://lore.kernel.org/r/20250530185239.2335185-3-jmattson@google.com Signed-off-by: Sean Christopherson Reviewed-by: Xiaoyao Li --- Documentation/virt/kvm/api.rst | 23 +++++++++++++++++++++++ arch/x86/kvm/svm/nested.c | 4 +++- arch/x86/kvm/svm/svm.c | 5 +++++ arch/x86/kvm/vmx/nested.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 4 ++++ arch/x86/kvm/x86.c | 6 +++++- arch/x86/kvm/x86.h | 5 +++++ include/uapi/linux/kvm.h | 1 + tools/include/uapi/linux/kvm.h | 1 + 9 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 43ed57e048a8..27ced3ee2b53 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7844,6 +7844,7 @@ Valid bits in args[0] are:: #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) #define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) + #define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4) =20 Enabling this capability on a VM provides userspace with a way to no longer intercept some instructions for improved latency in some @@ -7854,6 +7855,28 @@ all such vmexits. =20 Do not enable KVM_FEATURE_PV_UNHALT if you disable HLT exits. =20 +Virtualizing the ``IA32_APERF`` and ``IA32_MPERF`` MSRs requires more +than just disabling APERF/MPERF exits. While both Intel and AMD +document strict usage conditions for these MSRs--emphasizing that only +the ratio of their deltas over a time interval (T0 to T1) is +architecturally defined--simply passing through the MSRs can still +produce an incorrect ratio. + +This erroneous ratio can occur if, between T0 and T1: + +1. The vCPU thread migrates between logical processors. +2. Live migration or suspend/resume operations take place. +3. Another task shares the vCPU's logical processor. +4. C-states lower thean C0 are emulated (e.g., via HLT interception). +5. The guest TSC frequency doesn't match the host TSC frequency. + +Due to these complexities, KVM does not automatically associate this +passthrough capability with the guest CPUID bit, +``CPUID.6:ECX.APERFMPERF[bit 0]``. Userspace VMMs that deem this +mechanism adequate for virtualizing the ``IA32_APERF`` and +``IA32_MPERF`` MSRs must set the guest CPUID bit explicitly. + + 7.14 KVM_CAP_S390_HPAGE_1M -------------------------- =20 diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 749f7b866ac8..b7fd2e869998 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -194,7 +194,7 @@ void recalc_intercepts(struct vcpu_svm *svm) * Hardcode the capacity of the array based on the maximum number of _offs= ets_. * MSRs are batched together, so there are fewer offsets than MSRs. */ -static int nested_svm_msrpm_merge_offsets[6] __ro_after_init; +static int nested_svm_msrpm_merge_offsets[7] __ro_after_init; static int nested_svm_nr_msrpm_merge_offsets __ro_after_init; typedef unsigned long nsvm_msrpm_merge_t; =20 @@ -216,6 +216,8 @@ int __init nested_svm_init_msrpm_merge_offsets(void) MSR_IA32_SPEC_CTRL, MSR_IA32_PRED_CMD, MSR_IA32_FLUSH_CMD, + MSR_IA32_APERF, + MSR_IA32_MPERF, MSR_IA32_LASTBRANCHFROMIP, MSR_IA32_LASTBRANCHTOIP, MSR_IA32_LASTINTFROMIP, diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 5ad54640314f..d9931c6c4bc6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -839,6 +839,11 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu = *vcpu) svm_set_intercept_for_msr(vcpu, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW, guest_cpuid_is_intel_compatible(vcpu)); =20 + if (kvm_aperfmperf_in_guest(vcpu->kvm)) { + svm_disable_intercept_for_msr(vcpu, MSR_IA32_APERF, MSR_TYPE_R); + svm_disable_intercept_for_msr(vcpu, MSR_IA32_MPERF, MSR_TYPE_R); + } + if (sev_es_guest(vcpu->kvm)) sev_es_recalc_msr_intercepts(vcpu); =20 diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index c69df3aba8d1..b8ea1969113d 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -715,6 +715,12 @@ static inline bool nested_vmx_prepare_msr_bitmap(struc= t kvm_vcpu *vcpu, nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, MSR_IA32_FLUSH_CMD, MSR_TYPE_W); =20 + nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, + MSR_IA32_APERF, MSR_TYPE_R); + + nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, + MSR_IA32_MPERF, MSR_TYPE_R); + kvm_vcpu_unmap(vcpu, &map); =20 vmx->nested.force_msr_bitmap_recalc =3D false; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 702486e7511c..58c5dd368222 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4088,6 +4088,10 @@ void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu) vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R); vmx_disable_intercept_for_msr(vcpu, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R); } + if (kvm_aperfmperf_in_guest(vcpu->kvm)) { + vmx_disable_intercept_for_msr(vcpu, MSR_IA32_APERF, MSR_TYPE_R); + vmx_disable_intercept_for_msr(vcpu, MSR_IA32_MPERF, MSR_TYPE_R); + } =20 /* PT MSRs can be passed through iff PT is exposed to the guest. */ if (vmx_pt_mode_is_host_guest()) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2d893993262b..28541a612526 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4580,6 +4580,9 @@ static u64 kvm_get_allowed_disable_exits(void) { u64 r =3D KVM_X86_DISABLE_EXITS_PAUSE; =20 + if (boot_cpu_has(X86_FEATURE_APERFMPERF)) + r |=3D KVM_X86_DISABLE_EXITS_APERFMPERF; + if (!mitigate_smt_rsb) { r |=3D KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_CSTATE; @@ -6478,7 +6481,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, =20 if (!mitigate_smt_rsb && boot_cpu_has_bug(X86_BUG_SMT_RSB) && cpu_smt_possible() && - (cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE)) + (cap->args[0] & ~(KVM_X86_DISABLE_EXITS_PAUSE | + KVM_X86_DISABLE_EXITS_APERFMPERF))) pr_warn_once(SMT_RSB_MSG); =20 kvm_disable_exits(kvm, cap->args[0]); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 31ae58b765f3..bcfd9b719ada 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -546,6 +546,11 @@ static inline bool kvm_cstate_in_guest(struct kvm *kvm) return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_CSTATE; } =20 +static inline bool kvm_aperfmperf_in_guest(struct kvm *kvm) +{ + return kvm->arch.disabled_exits & KVM_X86_DISABLE_EXITS_APERFMPERF; +} + static inline bool kvm_notify_vmexit_enabled(struct kvm *kvm) { return kvm->arch.notify_vmexit_flags & KVM_X86_NOTIFY_VMEXIT_ENABLED; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 7a4c35ff03fe..aeb2ca10b190 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -644,6 +644,7 @@ struct kvm_ioeventfd { #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) #define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) +#define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4) =20 /* for KVM_ENABLE_CAP */ struct kvm_enable_cap { diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index d00b85cb168c..7415a3863891 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -618,6 +618,7 @@ struct kvm_ioeventfd { #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) #define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) +#define KVM_X86_DISABLE_EXITS_APERFMPERF (1 << 4) =20 /* for KVM_ENABLE_CAP */ struct kvm_enable_cap { --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Wed Oct 8 16:43:49 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2910421348 for ; Thu, 26 Jun 2025 00:12:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896755; cv=none; b=cWWk6Z/BMifYVb+LMC+zI5KA7RzEL+RoWDo1rvcfcR60NNGJjqBPTzb1ViHvhKwvhpffwffkisr+rEdQWFrvn+2Bce5LuFWjHP0rPld+rqrNN+zi4TgyE9V4qZOo7YR7XyI0P6n53cVsUmExFHGTOnoKVVHtg4ajWiq/iKRIQoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896755; c=relaxed/simple; bh=64rymz2Kz4S/Z4aZF2H6eAnIafN/041V9v/2O/McSuM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oYO/X07WLZKlQGP1c+Z/GDB9GOXX3ECzb0pBOoZ5X5+ahSZUcPtZk+d6Zer8sNTJ39/fzjGiLnBXaTKo9X2c5NvSSc4JpaC0yO8xXGYJY6KrqviH9iJGbtwqVWdMsZDagsk12ZtAllexi81XQ802WWmbmXvUh3SMVIN4SjuAvt4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ufvpqGi2; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ufvpqGi2" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2349fe994a9so2709325ad.1 for ; Wed, 25 Jun 2025 17:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750896753; x=1751501553; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=7jYV3TYqsZyydz0IjaYFSoLu2pCs+jwctwQKh0JY6aA=; b=ufvpqGi2StfUbRKMylNAPoSlUqIiXyZpmJZCyty7Y4PvfQWjkdihC+6Kj45UE/qyKd txBZwGdiqIJ2XKzRh+Ixnm5b8FHRUSEkJPuA4xe7zSAV/Oa/6fOUD37XdFXOngF9e7sk q8GBhPdAKyRHt7Yle7WQhHnxuaLwojW56uqMyZQO0N/OSoyEJBkTWM2507rQYdVBuGef s7YPRdABW/J+LtmpsauZXBlS4Px5vTg01C6QT3icF72MgbW43iFWgl01l20D+tYU7C4k Iv8QqAYCykehFG2orMA8lW6ArbQMVWqMnZnt+OCMiDGdIq+u4ZLYTvqStQ/c3LO7zNBJ bAPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750896753; x=1751501553; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7jYV3TYqsZyydz0IjaYFSoLu2pCs+jwctwQKh0JY6aA=; b=jxb/GUB+6CCrcLKTmnfMqSBRKvubOuh1D9lMX/bborTNJVTpL/Lpv81QfC0GZ47sX8 xf7CLI3/ZiyiFyA3Oei3ovhdTp16pH5+clfU4I01mPkByMWIleuXAOwAt5lOJekYO2gu EqntfHf2cb5KkTBnOx4Vw0Sw08jHYd29QTug3vSd+K+Vop7cGNuF7dKM6Oy74nt8zgs6 RMyuHbiMwJsvt9uDam8LijEm45b8VJcbAgxNAUdTvHxjrQELAzsFTkHu3rHKC0z74zWD hBZLYIP3XsDe1+17lXntFt046Jbn8zIyHx7cA8ME8H5+9nTKiwm9RtJT/FO89DvRvIfM 0UDQ== X-Forwarded-Encrypted: i=1; AJvYcCXn0fc+fsvsxccUJ893K4ZBWScKWECpljWnF0JZ3m7Fr+FmmoDt6Eco/7RX7URHt20SMwQG0KLfh+mZClE=@vger.kernel.org X-Gm-Message-State: AOJu0YxuvI1aAo9E5WfyxSxSw0zcB+3M+VoAU8MHM01P5MhDyPhM3HtZ MEcOGEv2uyfTHleaGgD8ExY/rWqewY+u67ZWI5SUi9fNSevUV7aRBSr2yul8Pu7XosbIsnzr9lN yY1uIzw== X-Google-Smtp-Source: AGHT+IEokWeu3xZE8pF2I2mOTnRiQq8sbkVZWMeUSzDfllmerKjnZZu1jBEaneetis5nr35QOidy+TLEhOA= X-Received: from pjbta6.prod.google.com ([2002:a17:90b:4ec6:b0:312:e5dd:9248]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f10:b0:223:f9a4:3f99 with SMTP id d9443c01a7336-23824044733mr89766355ad.29.1750896753599; Wed, 25 Jun 2025 17:12:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 25 Jun 2025 17:12:23 -0700 In-Reply-To: <20250626001225.744268-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250626001225.744268-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250626001225.744268-4-seanjc@google.com> Subject: [PATCH v5 3/5] KVM: selftests: Expand set of APIs for pinning tasks to a single CPU From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expand kvm_pin_this_task_to_pcpu() into a set of APIs to allow pinning a task (or self) to a CPU (any or specific). This will allow deduplicating code throughout a variety of selftests. Opportunistically use "self" instead of "this_task" as it is both more concise and less ambiguous. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/include/kvm_util.h | 31 ++++++++++++++++++- tools/testing/selftests/kvm/lib/kvm_util.c | 15 +++++---- tools/testing/selftests/kvm/lib/memstress.c | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index 91908d4a6edf..23a506d7eca3 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -21,6 +21,8 @@ #include #include =20 +#include + #include "kvm_util_arch.h" #include "kvm_util_types.h" #include "sparsebit.h" @@ -1054,7 +1056,34 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kv= m_vm *vm); =20 void kvm_set_files_rlimit(uint32_t nr_vcpus); =20 -void kvm_pin_this_task_to_pcpu(uint32_t pcpu); +int __pin_task_to_cpu(pthread_t task, int cpu); + +static inline void pin_task_to_cpu(pthread_t task, int cpu) +{ + int r; + + r =3D __pin_task_to_cpu(task, cpu); + TEST_ASSERT(!r, "Failed to set thread affinity to pCPU '%u'", cpu); +} + +static inline int pin_task_to_any_cpu(pthread_t task) +{ + int cpu =3D sched_getcpu(); + + pin_task_to_cpu(task, cpu); + return cpu; +} + +static inline void pin_self_to_cpu(int cpu) +{ + pin_task_to_cpu(pthread_self(), cpu); +} + +static inline int pin_self_to_any_cpu(void) +{ + return pin_task_to_any_cpu(pthread_self()); +} + void kvm_print_vcpu_pinning_help(void); void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcp= u[], int nr_vcpus); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 90f90f1c194f..c3f5142b0a54 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -620,15 +620,14 @@ struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm= _vm *vm) return vm_vcpu_recreate(vm, 0); } =20 -void kvm_pin_this_task_to_pcpu(uint32_t pcpu) +int __pin_task_to_cpu(pthread_t task, int cpu) { - cpu_set_t mask; - int r; + cpu_set_t cpuset; =20 - CPU_ZERO(&mask); - CPU_SET(pcpu, &mask); - r =3D sched_setaffinity(0, sizeof(mask), &mask); - TEST_ASSERT(!r, "sched_setaffinity() failed for pCPU '%u'.", pcpu); + CPU_ZERO(&cpuset); + CPU_SET(cpu, &cpuset); + + return pthread_setaffinity_np(task, sizeof(cpuset), &cpuset); } =20 static uint32_t parse_pcpu(const char *cpu_str, const cpu_set_t *allowed_m= ask) @@ -682,7 +681,7 @@ void kvm_parse_vcpu_pinning(const char *pcpus_string, u= int32_t vcpu_to_pcpu[], =20 /* 2. Check if the main worker needs to be pinned. */ if (cpu) { - kvm_pin_this_task_to_pcpu(parse_pcpu(cpu, &allowed_mask)); + pin_self_to_cpu(parse_pcpu(cpu, &allowed_mask)); cpu =3D strtok(NULL, delim); } =20 diff --git a/tools/testing/selftests/kvm/lib/memstress.c b/tools/testing/se= lftests/kvm/lib/memstress.c index 313277486a1d..557c0a0a5658 100644 --- a/tools/testing/selftests/kvm/lib/memstress.c +++ b/tools/testing/selftests/kvm/lib/memstress.c @@ -265,7 +265,7 @@ static void *vcpu_thread_main(void *data) int vcpu_idx =3D vcpu->vcpu_idx; =20 if (memstress_args.pin_vcpus) - kvm_pin_this_task_to_pcpu(memstress_args.vcpu_to_pcpu[vcpu_idx]); + pin_self_to_cpu(memstress_args.vcpu_to_pcpu[vcpu_idx]); =20 WRITE_ONCE(vcpu->running, true); =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Wed Oct 8 16:43:49 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFADB16DEB3 for ; Thu, 26 Jun 2025 00:12:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896757; cv=none; b=EN9o910KyfSICLQa3tyWR3+ZDKfIfelRUoKsQqRx26IlCZoT19O+uedOr0+IHrXj41Un3P8IdCJd/fs/AOjYMHUJ+RZGWzbIQTaO2h6gu3BsbEkJrQJU8++7n5K02dEe1mEvQvgAfwoFLUyOtRUMCetV708eIAp+7Vw2thzn21U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896757; c=relaxed/simple; bh=S8YepjEcxw5aH+WyUJggwrBoMjNRQccpLKuukAOR0KI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AyT0JPGuIV8n1OBVa3/0SWC7rVDOjFQdDxO40VyLOkTq7JeTo0/k+AKVnu52y1fzM7zCEO5/LWfiT8U4oosq1GZpcK8NjJL40EPyW7GW8ZPlkkR66QW68JMpm87Kow4mKjdA4/BZ3EAwjmulsuXAA6g9VOPbu8PKcGB1yCesAwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZGYnwZMy; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZGYnwZMy" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-313d346dc8dso363296a91.1 for ; Wed, 25 Jun 2025 17:12:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750896755; x=1751501555; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=yHMfQhJxDmXXWypElOJUlq66wLvvI9/lh/NpqNUW0Yc=; b=ZGYnwZMy+x+LXIlYgfZsUSN4ZFRFOxzd8gaQegM9wzipZck/jv/fVBb1rx2onw4hKJ QqfpKPGo1S9Q7b3E2D5QXzw+aquCVQNpFGKob6LQ7sRHKEeSFuVWXygqg7IYuWeUJjwo SpY5cVKYUOaygIjkhG835p3DsJIDNiJjmNXrByZfXn0DxnzvY7e6CW94UZPaWSOxMTRX X6mDkEFaBMR/GTv5/nXj3PzepyAoS9dMfeDM8/2BD8hJggpF1If5thRMJWFGbrAhX5LI M5h0qlUax8+EJPW+nBh5iC7okGMb34ytxjmR66xx1nNeWE2tUIACKxxFl8zoLjWcoynF AtOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750896755; x=1751501555; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yHMfQhJxDmXXWypElOJUlq66wLvvI9/lh/NpqNUW0Yc=; b=NjZR+DD1dUFl8wIf5uvsnh66ML/sFCc84VccbtTZrqO2ggwDZZdefBg4J8LSrrIE0a 9TrXleGaJY90Ij5Oe2XqSZfSAWC9InF3+To/gtKyNhrCvCfK3WVJRV+glzSCoclUO326 +5MO2PgZnoaHTezb/Cqc1a2xfLuPWDxBL2YZPsKAaZwcbyA2hjQFiJRAPLxdi1/+txL8 MDzal0Ij9xRg8r7OeOe9oHhY/IcSM8Ar0C9ZfDK62LkSBVRb9rbkutMqMISfhYPBLSUO z+aOWgkZ+TloQAgIFzuh3taWsmfx/xotyxCJLJnuiowYBlnrToO5QOTd15I1caTCNhul +n8g== X-Forwarded-Encrypted: i=1; AJvYcCUsZDRG5g+a0RXwiu1VZWIW26ad9pUn7/tvMMW3GMHv8ulgcCE9XyIDQckPv6xsQ+2l5Tp5dSaWvwBqeto=@vger.kernel.org X-Gm-Message-State: AOJu0Yw2RZ3qvZRrYYMO8q/QrJbo1JaRJp2AoJMf0+Eh4/y0yOr/1Zcl ngfYewaYQQG59gHazwhmwf6mESkQEEShhjtY1bIcv43jpvYqYJn7ot9y/khltCq5zf0kHWy4WvZ oFwboIA== X-Google-Smtp-Source: AGHT+IFowPBB2GQmhp5GlgAqt6FlhUMcLhBMaNbsbDieOoJyK6IVevkDm0adw8K1pYlNDs+iSlSqacu2PHU= X-Received: from pjbnd12.prod.google.com ([2002:a17:90b:4ccc:b0:2e0:915d:d594]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:350e:b0:312:f54e:ba28 with SMTP id 98e67ed59e1d1-315f2689fa0mr7190023a91.24.1750896755366; Wed, 25 Jun 2025 17:12:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 25 Jun 2025 17:12:24 -0700 In-Reply-To: <20250626001225.744268-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250626001225.744268-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250626001225.744268-5-seanjc@google.com> Subject: [PATCH v5 4/5] KVM: selftests: Test behavior of KVM_X86_DISABLE_EXITS_APERFMPERF From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jim Mattson For a VCPU thread pinned to a single LPU, verify that interleaved host and guest reads of IA32_[AM]PERF return strictly increasing values when APERFMPERF exiting is disabled. Run the test in both L1 and L2 to verify that KVM passes through the APERF and MPERF MSRs when L1 doesn't want to intercept them (or any MSRs). Signed-off-by: Jim Mattson Link: https://lore.kernel.org/r/20250530185239.2335185-4-jmattson@google.com Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/aperfmperf_test.c | 213 ++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/aperfmperf_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selft= ests/kvm/Makefile.kvm index 028456f1aae1..40920445bfbe 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -135,6 +135,7 @@ TEST_GEN_PROGS_x86 +=3D x86/amx_test TEST_GEN_PROGS_x86 +=3D x86/max_vcpuid_cap_test TEST_GEN_PROGS_x86 +=3D x86/triple_fault_event_test TEST_GEN_PROGS_x86 +=3D x86/recalc_apic_map_test +TEST_GEN_PROGS_x86 +=3D x86/aperfmperf_test TEST_GEN_PROGS_x86 +=3D access_tracking_perf_test TEST_GEN_PROGS_x86 +=3D coalesced_io_test TEST_GEN_PROGS_x86 +=3D dirty_log_perf_test diff --git a/tools/testing/selftests/kvm/x86/aperfmperf_test.c b/tools/test= ing/selftests/kvm/x86/aperfmperf_test.c new file mode 100644 index 000000000000..8b15a13df939 --- /dev/null +++ b/tools/testing/selftests/kvm/x86/aperfmperf_test.c @@ -0,0 +1,213 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Test for KVM_X86_DISABLE_EXITS_APERFMPERF + * + * Copyright (C) 2025, Google LLC. + * + * Test the ability to disable VM-exits for rdmsr of IA32_APERF and + * IA32_MPERF. When these VM-exits are disabled, reads of these MSRs + * return the host's values. + * + * Note: Requires read access to /dev/cpu//msr to read host MSRs. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "test_util.h" +#include "vmx.h" + +#define NUM_ITERATIONS 10000 + +static int open_dev_msr(int cpu) +{ + char path[PATH_MAX]; + + snprintf(path, sizeof(path), "/dev/cpu/%d/msr", cpu); + return open_path_or_exit(path, O_RDONLY); +} + +static uint64_t read_dev_msr(int msr_fd, uint32_t msr) +{ + uint64_t data; + ssize_t rc; + + rc =3D pread(msr_fd, &data, sizeof(data), msr); + TEST_ASSERT(rc =3D=3D sizeof(data), "Read of MSR 0x%x failed", msr); + + return data; +} + +static void guest_read_aperf_mperf(void) +{ + int i; + + for (i =3D 0; i < NUM_ITERATIONS; i++) + GUEST_SYNC2(rdmsr(MSR_IA32_APERF), rdmsr(MSR_IA32_MPERF)); +} + +#define L2_GUEST_STACK_SIZE 64 + +static void l2_guest_code(void) +{ + guest_read_aperf_mperf(); + GUEST_DONE(); +} + +static void l1_svm_code(struct svm_test_data *svm) +{ + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; + struct vmcb *vmcb =3D svm->vmcb; + + generic_svm_setup(svm, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE= ]); + run_guest(vmcb, svm->vmcb_gpa); +} + +static void l1_vmx_code(struct vmx_pages *vmx) +{ + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; + + GUEST_ASSERT_EQ(prepare_for_vmx_operation(vmx), true); + GUEST_ASSERT_EQ(load_vmcs(vmx), true); + + prepare_vmcs(vmx, NULL, &l2_guest_stack[L2_GUEST_STACK_SIZE]); + + /* + * Enable MSR bitmaps (the bitmap itself is allocated, zeroed, and set + * in the VMCS by prepare_vmcs()), as MSR exiting mandatory on Intel. + */ + vmwrite(CPU_BASED_VM_EXEC_CONTROL, + vmreadz(CPU_BASED_VM_EXEC_CONTROL) | CPU_BASED_USE_MSR_BITMAPS); + + GUEST_ASSERT(!vmwrite(GUEST_RIP, (u64)l2_guest_code)); + GUEST_ASSERT(!vmlaunch()); +} + +static void guest_code(void *nested_test_data) +{ + guest_read_aperf_mperf(); + + if (this_cpu_has(X86_FEATURE_SVM)) + l1_svm_code(nested_test_data); + else if (this_cpu_has(X86_FEATURE_VMX)) + l1_vmx_code(nested_test_data); + else + GUEST_DONE(); + + TEST_FAIL("L2 should have signaled 'done'"); +} + +static void guest_no_aperfmperf(void) +{ + uint64_t msr_val; + uint8_t vector; + + vector =3D rdmsr_safe(MSR_IA32_APERF, &msr_val); + GUEST_ASSERT(vector =3D=3D GP_VECTOR); + + vector =3D rdmsr_safe(MSR_IA32_APERF, &msr_val); + GUEST_ASSERT(vector =3D=3D GP_VECTOR); + + GUEST_DONE(); +} + +int main(int argc, char *argv[]) +{ + const bool has_nested =3D kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86= _FEATURE_VMX); + uint64_t host_aperf_before, host_mperf_before; + vm_vaddr_t nested_test_data_gva; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + int msr_fd, cpu, i; + + /* Sanity check that APERF/MPERF are unsupported by default. */ + vm =3D vm_create_with_one_vcpu(&vcpu, guest_no_aperfmperf); + vcpu_run(vcpu); + TEST_ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE); + kvm_vm_free(vm); + + cpu =3D pin_self_to_any_cpu(); + + msr_fd =3D open_dev_msr(cpu); + + /* + * This test requires a non-standard VM initialization, because + * KVM_ENABLE_CAP cannot be used on a VM file descriptor after + * a VCPU has been created. + */ + vm =3D vm_create(1); + + TEST_REQUIRE(vm_check_cap(vm, KVM_CAP_X86_DISABLE_EXITS) & + KVM_X86_DISABLE_EXITS_APERFMPERF); + + vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS, + KVM_X86_DISABLE_EXITS_APERFMPERF); + + vcpu =3D vm_vcpu_add(vm, 0, guest_code); + + if (!has_nested) + nested_test_data_gva =3D NONCANONICAL; + else if (kvm_cpu_has(X86_FEATURE_SVM)) + vcpu_alloc_svm(vm, &nested_test_data_gva); + else + vcpu_alloc_vmx(vm, &nested_test_data_gva); + + vcpu_args_set(vcpu, 1, nested_test_data_gva); + + host_aperf_before =3D read_dev_msr(msr_fd, MSR_IA32_APERF); + host_mperf_before =3D read_dev_msr(msr_fd, MSR_IA32_MPERF); + + for (i =3D 0; i <=3D NUM_ITERATIONS * (1 + has_nested); i++) { + uint64_t host_aperf_after, host_mperf_after; + uint64_t guest_aperf, guest_mperf; + struct ucall uc; + + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_DONE: + goto done; + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + case UCALL_SYNC: + guest_aperf =3D uc.args[0]; + guest_mperf =3D uc.args[1]; + + host_aperf_after =3D read_dev_msr(msr_fd, MSR_IA32_APERF); + host_mperf_after =3D read_dev_msr(msr_fd, MSR_IA32_MPERF); + + TEST_ASSERT(host_aperf_before < guest_aperf, + "APERF: host_before (0x%" PRIx64 ") >=3D guest (0x%" PRIx64 ")", + host_aperf_before, guest_aperf); + TEST_ASSERT(guest_aperf < host_aperf_after, + "APERF: guest (0x%" PRIx64 ") >=3D host_after (0x%" PRIx64 ")", + guest_aperf, host_aperf_after); + TEST_ASSERT(host_mperf_before < guest_mperf, + "MPERF: host_before (0x%" PRIx64 ") >=3D guest (0x%" PRIx64 ")", + host_mperf_before, guest_mperf); + TEST_ASSERT(guest_mperf < host_mperf_after, + "MPERF: guest (0x%" PRIx64 ") >=3D host_after (0x%" PRIx64 ")", + guest_mperf, host_mperf_after); + + host_aperf_before =3D host_aperf_after; + host_mperf_before =3D host_mperf_after; + + break; + } + } + TEST_FAIL("Didn't receive UCALL_DONE\n"); +done: + kvm_vm_free(vm); + close(msr_fd); + + return 0; +} --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Wed Oct 8 16:43:49 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6A3C194A65 for ; Thu, 26 Jun 2025 00:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896759; cv=none; b=MLypoahL3Nkaz+HP3wbUe/ghpsbEqnt7pD5aI/vjiaEtmd69+G/eDTZZvMEJaeiajGPReaa1NKgNYQqGm1YHbqFaB1kpxKpzP2mN1o/MJ2n+JbMNTiubz/e/gUJHx8XoB+Vhm6cfp6C7AmdzBPfQ8auWIwhKqW3Xs3bhb1eSnFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750896759; c=relaxed/simple; bh=YYqdIgfs7prMRCqGJLkiae8o8yLiIzZye7Nt81y7N1s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i5v+UG0pYaetMdK9RZuboLKNew71Qdaz8jsmjcrGJcQFexQZqeCrXRRlZ3RF3N6xILMV4MlrHVVaKcoSLVggfOiiQ1DEZxBABnY8VHcbrtxI1QRXiWdcXT3LaVr32A4K50Jtrx6FA12gO1RBm7g+UhYoRgmwf2Si9toulNcJtA4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=RvJjAi32; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RvJjAi32" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3121cffd7e8so280223a91.0 for ; Wed, 25 Jun 2025 17:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750896757; x=1751501557; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=TejvodaYmBPMcebcEQjSEKnmOuBSBKtSthLIMV0TmZs=; b=RvJjAi32HmWeZn8O8UTTG69t+NRplPkjjtN4hDaqzO4Z8AU8lvQV7+tft/dI24gavj QnF6yAeJgURax/+qb2tiRpKDW74cMSMNCEBLlKBqugut0ynsVKS64Ch8PUEPRcRylZfb 3V29/q8J/Qf3TKdT4uioOJ+XKXcJa7qzssYwE/UdYPXXLfK6N3Bfl+f8qXhHfmTJEEiV osxG1oyZSWbMoazAAunz/Buba6hV70/Jw37BB8u6c2Yi5ORFKQLAXuslFvBPC0x+1Rtj wSLCkjJi8BXp0UcYlmmEQprMhgx3Owa/ow0sgYge6iFLgiId1/93D7PWuEPltWhCKtMv JghA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750896757; x=1751501557; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TejvodaYmBPMcebcEQjSEKnmOuBSBKtSthLIMV0TmZs=; b=DsAEhjBlP6YdMItHnB07gpWA8jczhd6gh34NeRTKb1YEqdJtxQCmOUD5382BbBploF XioTOsEmujwdYWa2f0mRkIUr7A5VxcnTpAeHxY/205C10YZQkNQB+8GFNJtwDxKbrmQ3 ahHQtqkRB9s8PUmrCC7Wo1l71GWY59aNpI7H9+0JFbhDNfNHEmB/NK2ABO3QxyF382pf wb1uN7+e65YYqVzC+nLci8T5ligiIWkLe7/ICR/OoTv8kFPSReZmLmrCO54CeIGdrfYn IfQrhCq5+cBf5aKU4alEmwddhWtU36Lom9BwTqR9VyJ5rESkmzxJyyO9/wn5KV5y2JZu RWww== X-Forwarded-Encrypted: i=1; AJvYcCUq5ZLyVo7sVTR6M1SF9jFvgxir9egP9dNcbJ8mF3ErjHorLxE5kqb3JTZqgQhpp/cSul2UMVyqLZmUjEA=@vger.kernel.org X-Gm-Message-State: AOJu0Yzcoz/NGqLth4zRmj9qpmktMfBSYVFPS7wl24tdrfOP/ae7HiVI 4IQisI0sQKU5bah7zOsTOb7EyNagx5Hp+4XT6TMfMAiZb6kA8ydU00/6l4tv7erp3e1Mun49wGT ioqSK+Q== X-Google-Smtp-Source: AGHT+IG+NlZ+68CsdHzgcIfwCRnU1b472g9YpYS5EpkOr0QdzQPPYC4WRVn9r71YvhtQ94AlekS4VDTCDtU= X-Received: from pjbqo12.prod.google.com ([2002:a17:90b:3dcc:b0:311:f699:df0a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f90:b0:311:ea13:2e61 with SMTP id 98e67ed59e1d1-315f26c1b74mr6093342a91.34.1750896757146; Wed, 25 Jun 2025 17:12:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 25 Jun 2025 17:12:25 -0700 In-Reply-To: <20250626001225.744268-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250626001225.744268-1-seanjc@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250626001225.744268-6-seanjc@google.com> Subject: [PATCH v5 5/5] KVM: selftests: Convert arch_timer tests to common helpers to pin task From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert the arch timer tests to use __pin_task_to_cpu() and pin_self_to_cpu(). No functional change intended. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/arch_timer.c | 7 +----- .../kvm/arm64/arch_timer_edge_cases.c | 23 ++----------------- 2 files changed, 3 insertions(+), 27 deletions(-) diff --git a/tools/testing/selftests/kvm/arch_timer.c b/tools/testing/selft= ests/kvm/arch_timer.c index acb2cb596332..cf8fb67104f1 100644 --- a/tools/testing/selftests/kvm/arch_timer.c +++ b/tools/testing/selftests/kvm/arch_timer.c @@ -98,16 +98,11 @@ static uint32_t test_get_pcpu(void) static int test_migrate_vcpu(unsigned int vcpu_idx) { int ret; - cpu_set_t cpuset; uint32_t new_pcpu =3D test_get_pcpu(); =20 - CPU_ZERO(&cpuset); - CPU_SET(new_pcpu, &cpuset); - pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu); =20 - ret =3D pthread_setaffinity_np(pt_vcpu_run[vcpu_idx], - sizeof(cpuset), &cpuset); + ret =3D __pin_task_to_cpu(pt_vcpu_run[vcpu_idx], new_pcpu); =20 /* Allow the error where the vCPU thread is already finished */ TEST_ASSERT(ret =3D=3D 0 || ret =3D=3D ESRCH, diff --git a/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c b/to= ols/testing/selftests/kvm/arm64/arch_timer_edge_cases.c index 4e71740a098b..ce74d069cb7b 100644 --- a/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c +++ b/tools/testing/selftests/kvm/arm64/arch_timer_edge_cases.c @@ -862,25 +862,6 @@ static uint32_t next_pcpu(void) return next; } =20 -static void migrate_self(uint32_t new_pcpu) -{ - int ret; - cpu_set_t cpuset; - pthread_t thread; - - thread =3D pthread_self(); - - CPU_ZERO(&cpuset); - CPU_SET(new_pcpu, &cpuset); - - pr_debug("Migrating from %u to %u\n", sched_getcpu(), new_pcpu); - - ret =3D pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset); - - TEST_ASSERT(ret =3D=3D 0, "Failed to migrate to pCPU: %u; ret: %d\n", - new_pcpu, ret); -} - static void kvm_set_cntxct(struct kvm_vcpu *vcpu, uint64_t cnt, enum arch_timer timer) { @@ -907,7 +888,7 @@ static void handle_sync(struct kvm_vcpu *vcpu, struct u= call *uc) sched_yield(); break; case USERSPACE_MIGRATE_SELF: - migrate_self(next_pcpu()); + pin_self_to_cpu(next_pcpu()); break; default: break; @@ -919,7 +900,7 @@ static void test_run(struct kvm_vm *vm, struct kvm_vcpu= *vcpu) struct ucall uc; =20 /* Start on CPU 0 */ - migrate_self(0); + pin_self_to_cpu(0); =20 while (true) { vcpu_run(vcpu); --=20 2.50.0.727.gbf7dc18ff4-goog