From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 6D816308F10 for ; Fri, 15 Aug 2025 15:55:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755273328; cv=none; b=GIuQ35FXW7N/3aLv4vU7mUr+jilt+L65kOo9RLixq4onntaSKuMrCaKSUb2pD1SfT+BDQ85u1WOY+ncdF0vuhJQ5gTnGHMX9K/nEncoCijFRDqaToDz4Z3IGeD9a0XPVpzQcnnOclb6EWwoTuNg2Vy569CCbl0yOXoYx6DDIE+A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755273328; c=relaxed/simple; bh=wGA20jXRveGy5A3RdSltG8Mhq+1vci/NEVKX+vkU7E8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Kl7dQ6JVqn7DhK9ZjfXYQlNc6QDABG04Sckix8cIYLBF6rzth+sU5/7437vY0c0HdL+db5e7tUsvT2Kohy480Rh/cUnTmWQflD7/82920VwjBSIHdF9/Oo2ZtGU8n3oaNkei0tSXSCBDYYZIXcXe21fzS7BU1XHoSulBZigWLhw= 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=x3oYtxMa; arc=none smtp.client-ip=209.85.215.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="x3oYtxMa" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b4716fb7f2aso1478556a12.0 for ; Fri, 15 Aug 2025 08:55:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755273327; x=1755878127; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=z7Sa/kyYZ4LkgwyCX9rJEJ7u8HAAn0SslzIHQ/FhmRM=; b=x3oYtxMaycpQa+ZiFdutQkYdge2OTwH6KHNjeAXf7z48MhvczOS6w7AZRYD71S2rIX o00TZfrvCIHMcRe6fxP1zBLFeT3jY/DPHV7OCD0ZOKveJnmMqJ166nqKOMAKAtDOm10J RFQnqOETLIJPtCxNK1XXlQEQDireX42B/nYkEMgAkJ6zDz7em7l1lFSrH3l2bQtHvsh7 pUFIaYWdGCIvTEx/vM0NZITlN00HB3nAueFVvWWtFB0r4bTn4AY1DspH8rbs9BxoFYjH iilCdbjPjxgPTF/bocZ259O8Q4KpRTPq+hY4/1vK9KqTjDh7ZS4EoNQAMLy0kYV1bgF5 QUuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755273327; x=1755878127; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=z7Sa/kyYZ4LkgwyCX9rJEJ7u8HAAn0SslzIHQ/FhmRM=; b=HmUzubsHnmYd1dHCYU/1GTia4TADhG6O4ELOahTGNOH+048Dml5pyyPtJse89lJ6Rh 88segjLmZR/Fqq1BzIwSEXz/bSxvadUKRbCHViV8mRLf5FLTSZN9gtP3jO6ybaohhbfy 18iW2gquRvUlLQehrf0raRfKp8QlNpH32I0LFPDa6t3+W1sTy6utFfFajhUMtfXS20gV nBJ1zuhYdA3N8vo70Tq3DfR/FA2G6+SMGkGmmCosZx3ADmk0Ih+FbdC++haNFIlLK9x2 X616Wx5/1/2s5v3Qv7DXpa+JYR630qu9sORiu2KJV3vPw/dTvwzfdwNeLnP2qAcKGffc iAsA== X-Forwarded-Encrypted: i=1; AJvYcCU76+Yyv7kXJYRGiZyg8FosEQN6fVdOyHYExXugWJr6CSM6AaWXPXCs/sMd9Yyz0S1ImRc=@vger.kernel.org X-Gm-Message-State: AOJu0YyFoV7NDuY3YI680MI9B6ti6/ykQxtMzk8ta4UaKK4xXouawW8V u58n2Vq/w7t2HZijfg8kFIQmINu8H9PnIF463YqVPeCyrJ9H7RJegs1zvaj4GVUPzpDwd95YuEr F01iFJQ== X-Google-Smtp-Source: AGHT+IEdU+o+jxQRQjpxYUV2M2swV17WB5j94o9nqbjzzuHzapwMPWAh/lqVZpiY/Ko0sm3v0aaj/KNyHyA= X-Received: from pjbpl10.prod.google.com ([2002:a17:90b:268a:b0:321:370d:cae5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:ec86:b0:313:dcf4:37bc with SMTP id 98e67ed59e1d1-323421229eamr3334693a91.34.1755273326710; Fri, 15 Aug 2025 08:55:26 -0700 (PDT) Date: Fri, 15 Aug 2025 08:55:25 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250806195706.1650976-1-seanjc@google.com> <20250806195706.1650976-10-seanjc@google.com> <20250815113951.GC4067720@noisy.programming.kicks-ass.net> Message-ID: Subject: Re: [PATCH v5 09/44] perf/x86: Switch LVTPC to/from mediated PMI vector on guest load/put context From: Sean Christopherson To: Peter Zijlstra Cc: 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 , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Paolo Bonzini , 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, Kan Liang , Yongwei Ma , Mingwei Zhang , Xiong Zhang , Sandipan Das , Dapeng Mi Content-Type: text/plain; charset="us-ascii" On Fri, Aug 15, 2025, Sean Christopherson wrote: > On Fri, Aug 15, 2025, Peter Zijlstra wrote: > > > diff --git a/kernel/events/core.c b/kernel/events/core.c > > > index e1df3c3bfc0d..ad22b182762e 100644 > > > --- a/kernel/events/core.c > > > +++ b/kernel/events/core.c > > > @@ -6408,6 +6408,8 @@ void perf_load_guest_context(unsigned long data) > > > task_ctx_sched_out(cpuctx->task_ctx, NULL, EVENT_GUEST); > > > } > > > > > > + arch_perf_load_guest_context(data); > > > > So I still don't understand why this ever needs to reach the generic > > code. x86 pmu driver and x86 kvm can surely sort this out inside of x86, > > no? > > It's definitely possible to handle this entirely within x86, I just don't love > switching the LVTPC without the protection of perf_ctx_lock and perf_ctx_disable(). > It's not a sticking point for me if you strongly prefer something like this: > > diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c > index 0e5048ae86fa..86b81c217b97 100644 > --- a/arch/x86/kvm/pmu.c > +++ b/arch/x86/kvm/pmu.c > @@ -1319,7 +1319,9 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu) > > lockdep_assert_irqs_disabled(); > > - perf_load_guest_context(kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVTPC)); > + perf_load_guest_context(); > + > + perf_load_guest_lvtpc(kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVTPC)); Hmm, an argument for providing a dedicated perf_load_guest_lvtpc() APIs is that it would allow KVM to handle LVTPC writes in KVM's VM-Exit fastpath, i.e. without having to do a full put+reload of the guest context. So if we're confident that switching the host LVTPC outside of perf_{load,put}_guest_context() is functionally safe, I'm a-ok with it. > /* > * Disable all counters before loading event selectors and PMCs so that > @@ -1380,5 +1382,7 @@ void kvm_mediated_pmu_put(struct kvm_vcpu *vcpu) > > kvm_pmu_put_guest_pmcs(vcpu); > > + perf_put_guest_lvtpc(); > + > perf_put_guest_context(); > } >