From mboxrd@z Thu Jan 1 00:00:00 1970 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 E38102FFFB9 for ; Sat, 6 Dec 2025 00:18:32 +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=1764980315; cv=none; b=Fml4s4ilI4EHTkQ+jNAM9F8K6/02/XfX/uIPg2O9SXAcTqXGV9nj4ATz5wMkycSrYMW+twxamuQNuHFb+4chumY9c0YCMH+4YcClngrSKRceJx1pVOKZHfGDu4ByusxmYWRT5BQky52cOW2TCH5EoJmReRzzViP4s3JLr14YaLs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764980315; c=relaxed/simple; bh=dDJu5oLi1PjxkU6HXADJID7cFgoQLWGm6y/UNcHsMk8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g+gvdm9x289jMrm8TIj2BDyIhuFHF/7tHYzIyjufpeT3k+ypbFnqyJxsLj+8C+sO7jJDfuDrnOMb7qMsZY5h3NmGqxLfdSdN10L4biKEmoaZCb8jNI2kfc+grB1cg+9KRtpxgwqliPMRFYtsjyHMVhxzkYhc6CUKDXmOaXFWk5g= 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=fFTYcCdR; 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="fFTYcCdR" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-349a2f509acso368628a91.1 for ; Fri, 05 Dec 2025 16:18:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764980312; x=1765585112; darn=lists.linux.dev; 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=DfpzWNiupf9oyWVJGMgXVTAvVfysSBzo629ptknGMjs=; b=fFTYcCdRrx+G1C0DvHCS+2s3xMVTED1CFi+/RWZMrEbA/3gz43/crSlGtUeb5B7rK7 Aj6e/OJomerROzilnjCYOvvSbAoXcAXSovTjXO9kLL7TK0Co89b+QOoW15CGzmZETgzk NWNThbDJgwZj23fL7U47kTCt2VwHMH/wT7bvX7KjF5RWGPzIMaqV5DcnU7jvyk23wTQT ZKuja7L5045kPjLhWzGKMN1wy3xgjFTDp8EOIdcEbNBG6CqGzJ2rj2ukMSGvWyE4KtNu pUieewQlylU0PANL4TMQarlF9DeOstrxxL6FEYVssQZKwVBMe+XEyAWLbhToZCkQeV/D flWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764980312; x=1765585112; 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=DfpzWNiupf9oyWVJGMgXVTAvVfysSBzo629ptknGMjs=; b=GkBH/bxWGPTR5LTvJRdy0IReGR0LKkkR+ewYMDb0ppyx1uMHQzwOdBB9eGZ3+En62Q AB5OiDxFYtMHLYcyc1dcZmzsMyHQy+BlF1Y4rgH6RuR3S+FU9RY4Y/P9yqFpiCqMzLKR +VZADUKJ+MZecXgd1AvBWxnvvDQgnmGVDsEoiPQNjKFY1f3CkN26U3l2ti0316AK00Nm h07QyVJgRHJBCtPE8p85/drrf5O2AG0ZZ96VRcJjMgMkPJvyRzWS4Rj32Ppv7MVBoh7N HR2HnBWQ77eaSS8xABdOfh+tXirlU9UOU1ZSVgHL0FKIpOrJUju8ZpLM+kFGC9Ltk1ZJ 5EXQ== X-Forwarded-Encrypted: i=1; AJvYcCX/9q8kTCsgSjWiAuCGgIxgncXMW41McXPx/oFC9w3ujgO7ADYsKPCj2sDu5mEP4x72OW4bzz9uz5M=@lists.linux.dev X-Gm-Message-State: AOJu0YzjYOopqlUnubWhj3q/APhFkxH/oOPVcZbl8kk7hwVvVieR7xZI C16SUINtE1qlGyoepJlyiFGy/+LgiZoN4BU97xc+/XsggU2wrwT1AgeqG3QtG6+3+Ab9icDXbmz Q9g5rYQ== X-Google-Smtp-Source: AGHT+IECzGPhAX0jAKjnbhpxSt8tw9U2yWj2g1X4v/o1b69MxFOOkQezmGTBDbALh6waJ5b9ZWPa3i4t9I0= X-Received: from pjbsb13.prod.google.com ([2002:a17:90b:50cd:b0:340:c0e9:24b6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:55c4:b0:341:8adc:76d2 with SMTP id 98e67ed59e1d1-349a2518ceamr601732a91.16.1764980312307; Fri, 05 Dec 2025 16:18:32 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 5 Dec 2025 16:17:09 -0800 In-Reply-To: <20251206001720.468579-1-seanjc@google.com> Precedence: bulk X-Mailing-List: loongarch@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251206001720.468579-1-seanjc@google.com> X-Mailer: git-send-email 2.52.0.223.gf5cc29aaa4-goog Message-ID: <20251206001720.468579-34-seanjc@google.com> Subject: [PATCH v6 33/44] KVM: x86/pmu: Expose enable_mediated_pmu parameter to user space From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Xin Li , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, loongarch@lists.linux.dev, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Mingwei Zhang , Xudong Hao , Sandipan Das , Dapeng Mi , Xiong Zhang , Manali Shukla , Jim Mattson Content-Type: text/plain; charset="UTF-8" From: Dapeng Mi Expose enable_mediated_pmu parameter to user space, i.e. allow userspace to enable/disable mediated vPMU support. Document the mediated versus perf-based behavior as part of the kernel-parameters.txt entry, and opportunistically add an entry for the core enable_pmu param as well. Signed-off-by: Dapeng Mi Signed-off-by: Mingwei Zhang Tested-by: Xudong Hao Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- .../admin-guide/kernel-parameters.txt | 49 +++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/vmx/vmx.c | 2 + 3 files changed, 53 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 6c42061ca20e..ed6f2ed94756 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -2926,6 +2926,26 @@ Default is Y (on). + kvm.enable_pmu=[KVM,X86] + If enabled, KVM will virtualize PMU functionality based + on the virtual CPU model defined by userspace. This + can be overridden on a per-VM basis via + KVM_CAP_PMU_CAPABILITY. + + If disabled, KVM will not virtualize PMU functionality, + e.g. MSRs, PMCs, PMIs, etc., even if userspace defines + a virtual CPU model that contains PMU assets. + + Note, KVM's vPMU support implicitly requires running + with an in-kernel local APIC, e.g. to deliver PMIs to + the guest. Running without an in-kernel local APIC is + not supported, though KVM will allow such a combination + (with severely degraded functionality). + + See also enable_mediated_pmu. + + Default is Y (on). + kvm.enable_virt_at_load=[KVM,ARM64,LOONGARCH,MIPS,RISCV,X86] If enabled, KVM will enable virtualization in hardware when KVM is loaded, and disable virtualization when KVM @@ -2972,6 +2992,35 @@ If the value is 0 (the default), KVM will pick a period based on the ratio, such that a page is zapped after 1 hour on average. + kvm-{amd,intel}.enable_mediated_pmu=[KVM,AMD,INTEL] + If enabled, KVM will provide a mediated virtual PMU, + instead of the default perf-based virtual PMU (if + kvm.enable_pmu is true and PMU is enumerated via the + virtual CPU model). + + With a perf-based vPMU, KVM operates as a user of perf, + i.e. emulates guest PMU counters using perf events. + KVM-created perf events are managed by perf as regular + (guest-only) events, e.g. are scheduled in/out, contend + for hardware resources, etc. Using a perf-based vPMU + allows guest and host usage of the PMU to co-exist, but + incurs non-trivial overhead and can result in silently + dropped guest events (due to resource contention). + + With a mediated vPMU, hardware PMU state is context + switched around the world switch to/from the guest. + KVM mediates which events the guest can utilize, but + gives the guest direct access to all other PMU assets + when possible (KVM may intercept some accesses if the + virtual CPU model provides a subset of hardware PMU + functionality). Using a mediated vPMU significantly + reduces PMU virtualization overhead and eliminates lost + guest events, but is mutually exclusive with using perf + to profile KVM guests and adds latency to most VM-Exits + (to context switch PMU state). + + Default is N (off). + kvm-amd.nested= [KVM,AMD] Control nested virtualization feature in KVM/SVM. Default is 1 (enabled). diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index cbebd3a18918..20fb7c38bf75 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -170,6 +170,8 @@ module_param(intercept_smi, bool, 0444); bool vnmi = true; module_param(vnmi, bool, 0444); +module_param(enable_mediated_pmu, bool, 0444); + static bool svm_gp_erratum_intercept = true; static u8 rsm_ins_bytes[] = "\x0f\xaa"; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f0a20ff2a941..62ba2a2b9e98 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -150,6 +150,8 @@ module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); extern bool __read_mostly allow_smaller_maxphyaddr; module_param(allow_smaller_maxphyaddr, bool, S_IRUGO); +module_param(enable_mediated_pmu, bool, 0444); + #define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD) #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE #define KVM_VM_CR0_ALWAYS_ON \ -- 2.52.0.223.gf5cc29aaa4-goog