From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.20 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796260196; b=UQatW6WMNMKYNowlG4SFXt4Ru33FTRr7wp0cbOALTcYtGbMX7LO4hx+gUNTh13ItpMmB STuA+BKnyRfRysYO4YfU/3g4AnpaS/Ulld+GRAjCmGM0O07BiZNXx7gadEyIP/AaVKZPx QIPsMJ+DMB6OQSI4So9F+Oic4PFT16kctkPCmubnVweQ/q6PoreeaqYQz+Y7Es4Oz5aL3 D7tY7gPyLHGKGp0qhRg01Y6CAP/acsNEQuEc+P6zied90YcOS8EOx8xPCQRFGyZGXNeVj zox6Mnj8w0y42/zS3OOT9hh4Mlzlm42QtRgWzg0sU6ucyPuUpQjYF9grZKBXVgjOCbw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796260196; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=ol3eSkvV4ONZ/DSSB4KRiyr9T5BsE8I2SXsjtgBC43A=; b=Q8y4vz2+tfGeGIiGuz/QcGiXnkpQ0A1pDSfr63iRxlhrBciqUQCg+8cP3V2MslvJmfGu pGo4NEjqp2OSNV6l3jZYETJY1ASZAkfF67H0MO0Hf4GnsmyEsif3Ke7CPqQdcQ04DJ82M dpnKJcN/v7AV8gA6bAjKcx4m0UovrAGKpMpw2X8+k1IDdDllzylBSgnJhKqfOxgpXekOk WCz/fyuVBjEv4DBXFRXwAimYI8gVN1QR18Lguaas1Nv94o8e/JNhyF70c8NufcGawKckS 7hNY0nDNaRRYjH1NUfTOnQcIsb6UL6NJhfo80iLcMJcqByraG/OygYBRP6I48wUTTVw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=f5AzrR8P; arc=none smtp.remote-ip=198.175.65.20 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.20]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2B2B606A322; Thu, 3 Dec 2026 01:09:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C2B2B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=f5AzrR8P DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796260196; x=1827796196; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=ol3eSkvV4ONZ/DSSB4KRiyr9T5BsE8I2SXsjtgBC43A=; b=f5AzrR8PxVhpKdkIxL3/0yxjcZ2D1SdmpyyqY9fTqHewu7GarCLCd7Wx 2AK/LG2eqOm3WzQjCuXaJOGavtT65tvbT2viJ020i9GjvknXR2ER544uW rA6hSzDPn3ffqMBb112Zc9qIAnNgF6Z2EH3CzTGvHYhGuj2Ys+CACzENg qUSDBZrx4mYMGAtXP5gbUg4etVBzQCjXR0h8GV/mv6eDFCGJInabkvbk4 kMe3a2ayqCzJWbodVY75neb69f2SVH/+V3MxZxxKzSy+3pefdpX25arRz aRPDk/Rri6eRfmPBVpVHHV6r/av7iukccAf1W8z1Sj3FCdV4K1S8cTFk8 Q==; X-CSE-ConnectionGUID: S5QfeL1tQw+m3pfGCyehow== X-CSE-MsgGUID: 9GrPprmnRYCprrSvo1VX2w== X-IronPort-AV: E=McAfee;i="6800,10657,11984"; a="5249639" X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="5249639" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by orvoesa112.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 17:09:51 -0800 X-CSE-ConnectionGUID: 99anfN7nR02GC9fULfVYJA== X-CSE-MsgGUID: 1CYeBpLfQMW57QaBwvfnfQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,247,1787036400"; d="scan'208";a="13607112" Received: from unknown (HELO [10.238.2.182]) ([10.238.2.182]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 17:09:47 -0800 Message-ID: <57f648a3-3b15-4c6f-bb7f-9a219c41ad9a@linux.intel.com> Date: Thu, 3 Dec 2026 09:09:44 +0800 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] KVM: x86/pmu: Clear PERF_GLOBAL_CTRL after switching to guest LVTPC To: Yosry Ahmed Cc: Sean Christopherson , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Paolo Bonzini , x86@kernel.org, Mingwei Zhang , linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20261201171413.2289887-1-yosry@kernel.org> <20261201171413.2289887-3-yosry@kernel.org> <73acc0fd-a6e1-4ff4-9c0b-65bb3c757e63@linux.intel.com> <0f15903d-8354-496e-8d03-3ea3e15e38df@linux.intel.com> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12/3/2026 5:04 AM, Yosry Ahmed wrote: >>>> IMO, making active_events really track active events could be the simplest >>>> and straightforward way to fix this issue. >>> Taking a closer look, I don't think this is viable: >>> - active_events is global, not per-CPU. >> Yes, but we can extend the active_events to per-cpu, PMI is per-cpu, right? > Maybe? I didn't take a closer look to check how straightforward it might be. > >>> - If active_events tracks currently scheduled events (which is what we >>> need here AFAICT), bailing in the NMI handler can miss a PMI if a PMI >>> was in-flight or delayed when the event is scheduled out. >> Generally the GLOBAL_CTRL is cleared in the entire schedule_in or >> schedule_out process until the scheduling finishes, this would prevent the >> PMI is triggered again in the event scheduling in theory. > Hmm yeah if we always disable the PMU first (and account for NMI delays). > >>> - active_events is not necessarily 0 while the guest mediated PMU >>> context is loaded. Other PMUs like Intel PT/BTS could have active >>> events AFAICT. >> Yes, Intel PT is an issue. The simplest way to fix the issue is to disallow >> PT and mediated vPMU to work in coexistence. > That was also a sticking point in the dynamic PMU context switching > RFC, Intel PT and BTS use the same PMI vector but are not mediated (so > are not scheduled out). Yes, that's what I'm concerning. PT and BTS shares same PMI with PMU, while currently we only disable the PMU instead of all including PT and BTS. If PT and BTS are not disabled, is it safe enough to switch the PMI vector? > > I think changing the semantics of active_events or adding mutual > exclusion between Intel PT/BTS and the mediated vPMU is a larger lift > that can be explored separately. I agree. The fix for active_events can be done as a separate patchset, it's more complicated than I initially thought. > >> An alternative way is to move perf_load_guest_lvtpc() into >> perf_load_guest_context(), call perf_load_guest_context() between the >> disabling perf contexts and enabling perf contexts. Actually it goes back >> our previous implementation. > I think this works as an alternative to this patch, although it still > doesn't guarantee PERF_GLOBAL_CTRL being 0 when LVTPC is switched > AFAICT. I think perf_ctx_disable() may not clear PERF_GLOBAL_CTRL if > there are no core PMU events being scheduled out. Also, > perf_ctx_enable() will re-enable PERF_GLOBAL_CTRL before returning > anyway, so KVM still needs to clear it afterwards. So it doesn't buy > us much compared to this patch, does it? Did I miss something? perf_ctx_disable() would always call x86_pmu_disable() to disable the whole PMU if it's not skipped, it doesn't check if there are active events.  >From my point of view,  the inside of perf_load_guest_context() may be the best place to call perf_load_guest_lvtpc() to switch the PMI vector, especially considering if we also need to disable PT and BTS PMU before switching the PMI vector. But as you mentioned, Peter didn't like it. perf_load_guest_context() is perf/core scope function which shared by all architectures, while perf_load_guest_lvtpc() is a x86 specific function. It makes sense that Peter doesn't want they are mixed together.  > > Additionally, we'd need to restore the arch hook which I think Peter > didn't like in previous implementations: > https://lore.kernel.org/all/20250815113951.GC4067720@noisy.programming.kicks-ass.net/ > >> I'm fine with either way. > I think out of all the options I prefer this patch because it's a much > smaller change and is more stable-friendly. I am also fine with moving > perf_load_guest_lvtpc() into perf_load_guest_context() if others are > okay with it. The reason that I don't like current implementation is that the PMU is not fully disabled before switching the PMI vector. Although all host events are scheduled out, it doesn't mean all PMU functionalities are disabled, like the PERF_METRICS. PERF_METRICS has no an extra independent MSR to manage the enabling, which is different with other counters. If PERF_METRICS is not disabled in the GLOBAL_CTRL, a PERF_METRICS overflow PMI could be triggered during the PMI vector switching, it could be unsafe. As the change log says, we could move the clearing of GLOBAL_CTRL into perf_load_guest_context() and put it before the vector switching, which looks better than current implementation. If we want to ensure the 100% safe, we may also need to disable PT and BTS along with the PMU before the vector switching. Thanks.