From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.7 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796195933; b=IsojgBoAN6ZCyg7/aNfNHR8f3kBiIIi/0Kc4jsDf0hOl5BkeO4eWkyuE7fDsvBZkJuDv 2wNHQwZrQ2ri8Uo0HSOBkxYkEjulKE4L9ZQKvXRPhPxEYJLUSFX+F/1/08xPRnYwFSpY1 DHewjj1jje91xnC5MOvaqv2sAeaKqfNo5gvTfbkogCvm1pcUo6Tkgo4h9zEbZtMI8m4Uf XapOPBDebM+e5zh6IEeUIyE0nfcYenYImIMTGObCWiOF3ISnaC800QoGLzxh4e9k9B67x iIzvnaIawQplon0U417LbJ5EZLTxroP1AIVn7ts5HxRwMEFbIc30X1uHtqvDpokryIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796195933; 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=2g2o8Qst0ZbOhY/x3a0pZxfHmdNF2DgG8Ae9i/joPlo=; b=1NSIH1TpOa6VYEBJClZ4E1/Q/PwF5+1OMol4YK9yX0ysGxV3IDosioC0lZLhUY4rLdR2 YTwsTxd8TgphOsdDLYEH+jWmmEd5O+9F9iF720+mfK84GTCzEfuzT72zMSVv2y1zPN0wm 6Ki4YOiwrEQEblArNR5D5T9RLLnKsRUYeSMskLiZ3sT+S1nhNNmCSoqqdovTZ2OHqoVTG TA48qNcKQx91vAVqF+H6014ZHpQnXgvuGasq2gZnKQtL553af3BlPOewZhony/nmyMNoh cqt0ood4v1hZuteYn8yaLI6PiRzgpk38lvwB6/E+2GKJvM3N/cGiC1K1FBhVBfLisBQ== 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=QKgTdjAM; arc=none smtp.remote-ip=192.198.163.7 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (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 80AB9606A327; Wed, 2 Dec 2026 07:18:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 80AB9606A327 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, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=QKgTdjAM DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796195933; x=1827731933; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=2g2o8Qst0ZbOhY/x3a0pZxfHmdNF2DgG8Ae9i/joPlo=; b=QKgTdjAMZ81Wpk7dsJPn+Y45namCxu7zum5tAWjztdJXu895pObUoc+3 K8Fd6cgYRNx4hPDAhSrFNvzrQ28H+TSpA/r5CfSCLnmZGUKKe//6ysbHq vGy0R7P44HRqVYVY57t1iMSAwKMdQrnJIJanx94LDxNAZr68cXTcjfAee HqpeIhIulQd364YY8SA8uZ074QNWUnDjjOZAQv6WlZdCGketK/LJ2xyRj j3ch4MJH+S+3/a6ZndCDFNrFAvRi9+ymZkFu2alLr9Fce7qgBaV/cS3B+ VA0M5z1DarLopPywlOWj5mXnMsnxnSPg88/njHr7KfoGp3rJoUdkFXWd9 A==; X-CSE-ConnectionGUID: dHeG/tDYS/qJhOUB8fwyRQ== X-CSE-MsgGUID: 9ZosC7iqQZ29o8rZqimE1w== X-IronPort-AV: E=McAfee;i="6800,10657,11983"; a="4949286" X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="4949286" Received: from orviesa008.jf.intel.com ([10.64.159.148]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 23:18:52 -0800 X-CSE-ConnectionGUID: Gba5vq/gQNaaq779CBmnfA== X-CSE-MsgGUID: k99lY9pvTXuB8/Yj2+tFaA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,246,1787036400"; d="scan'208";a="13235485" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.241.239]) ([10.124.241.239]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 23:18:46 -0800 Message-ID: Date: Wed, 2 Dec 2026 15:18:43 +0800 X-Mailing-List: kvm@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/2/2026 1:48 PM, Yosry Ahmed wrote: > On Tue, Dec 1, 2026 at 9:26 PM Mi, Dapeng wrote: >> >> On 12/2/2026 12:32 PM, Yosry Ahmed wrote: >>> On Tue, Dec 1, 2026 at 7:39 PM Mi, Dapeng wrote: >>>> On 12/2/2026 9:14 AM, Sean Christopherson wrote: >>>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote: >>>>>> On Tue, Dec 1, 2026 at 3:50 PM Sean Christopherson wrote: >>>>>>> On Tue, Dec 01, 2026, Yosry Ahmed wrote: >>>>>>>> When loading the guest PMU context, clear PERF_GLOBAL_CTRL after >>>>>>>> switching to guest LVTPC. Otherwise, the host perf NMI handler may set >>>>>>>> bits in PERF_GLOBAL_CTRL after KVM clears it (specifically on Intel, >>>>>>>> through __intel_pmu_enable_all()). >>>>>>> But disabling PERF_GLOBAL_CTRL should prevent taking any perf NMIs, no? >>>>>> If I am reading the code correctly, perf_event_nmi_handler() -> >>>>>> intel_pmu_handle_irq() -> __intel_pmu_enable_all() will be executed on >>>>>> an NMI even if it wasn't a perf NMI (__intel_pmu_enable_all() is >>>>>> called in the done label even if status is clear), but I might be >>>>>> mistaken. >>>>> That feels like a perf bug? I would expect perf_load_guest_context() to prevent >>>>> trying to enable perf events. >>> IIUC, perf not clearing PERF_GLOBAL_CTRL after >>> perf_load_guest_context() is the reason KVM clears PERF_GLOBAL_CTRL >>> here to begin with: >>> https://lore.kernel.org/all/aR43LoV1ti5-2WRD@google.com/. >>> >>>> perf_load_guest_context() schedule out all host events, and >>>> perf_event_nmi_handler() should early return and don't touch any PMU MSRs >>>> if active_events is 0. While the issue is that active_events currently >>>> doesn't really track the active events as what it intends. Instead, it >>>> seems to track the created events regardless of it's active. This is a perf >>>> bug. >>>> >>>> It's intended to clear GLOBAL_CTRL MSR before calling >>>> perf_load_guest_lvtpc(), it prevents triggering PMI from hit into >>>> perf_load_guest_lvtpc(). >>>> >>>> It looks the better way to fix this issue is to make active_events really >>>> track the active events (increase/decrease active_events in >>>> x86_pmu_add()/x86_pmu_del()). >>> If active_events is always 0 while the guest LVTPC is loaded, then we >>> can probably drop guest_lvtpc_loaded, right? >> I think so. >> >> >>> Having perf clear GLOBAL_CTRL when no events are active (e.g. in >>> perf_load_guest_context() and in the NMI handler) would also let KVM >>> drop the MSR write completely. It seems like that's what Peter and >>> Sean initially expected based on the discussion linked above >> Currently GLOBAL_CTRL keeps enabled for all counters by default even though >> there is no active event. Whether a single event is enabled depends on its >> EVENTSEL "enable" field. GLOBAL_CTRL is usually used to ensure all counters >> are enabled or disabled simultaneously. >> >> In theory, it can work by making perf_load_guest_context() clear >> GLOBAL_CTRL, but it won't be a simple change and could break current x86 >> perf convention. It may cause some corner issues. >> >> 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? > - 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.  > - 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. 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'm fine with either way.