Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp286792rwl; Thu, 6 Apr 2023 19:20:06 -0700 (PDT) X-Google-Smtp-Source: AKy350aWpABeQGg3c/DqcHckBA61evLoYgv3cklWFTtglqbXoazC26P2fveiUm1jQhZTuYRuxFtx X-Received: by 2002:a17:902:ea0b:b0:19e:76b7:c7d2 with SMTP id s11-20020a170902ea0b00b0019e76b7c7d2mr1704334plg.26.1680834006389; Thu, 06 Apr 2023 19:20:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680834006; cv=none; d=google.com; s=arc-20160816; b=bZ0nZiuk41jRrAi2eKK2sOZPnoqMStbIZOTS+rpwOgAz2HUnPzr+4esW61aHgJz7rs uXpPU8o2AzG9ONGZE9P3iaXn7izgUuD2rHNCURFOanbctJ37RD9oGosPFZ3enqD2Wd/U 0nbmSq3zK6BwU2XqhMyUfAsSxCiNKRNX0+0WuuX3eMkPLD2VC9cVq045LsW47uoZkpeo n7TiTQBc142TCw6JI/ADR2GgMRYbpDHEgvkV3nwofq2o2YO4C9sbCGUR73Kej473bomc 2ofLu6X+y+uDZHtmaN0TrPKhekC93NvRuHWNAlG072KAJ5xTi0M9hsxUXoOztERzb/MP icyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=NM3Z7cWxI8VgV2XX878CM3uoNsycYkoMYtaVz8/N1I0=; b=HSvMaIg4NEOQ3GuHlKnVtt89Xik0AvW2ANkCR0s2rdoMkw8kFXbIkGNAL7Z38gRTKT auAI1+Cxm4Md8l15zHVY8fAKgJj3aIjDXij7AaHTZLP5E1ppw+sVDq7HQ2MMCKwkZKTl wJh9byUirEisETiIN3Q5kbqap0WnHRcZqUVr6YK90s+dxfywpEroiQH/sINZvi1Z95N7 9Dp0mw1DLaMMwibPh+Cehr6QkUDGbWXqSjD9lXB/kSE5DRerl5KqUHzOqTsYNZcDfN6v 3TFPoBUqd+4vDiyy1r/AsWVa9ZJJuqU3FiHrCqodIuYrrabpcpbrMLxJThQ9nx0SQL+D Zkkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=JGWnVDon; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a5-20020a1709027d8500b0019a826d3054si2706906plm.640.2023.04.06.19.19.47; Thu, 06 Apr 2023 19:20:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=JGWnVDon; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240434AbjDGCSo (ORCPT + 99 others); Thu, 6 Apr 2023 22:18:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240435AbjDGCSn (ORCPT ); Thu, 6 Apr 2023 22:18:43 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EC9C19A2 for ; Thu, 6 Apr 2023 19:18:41 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e22-20020a25e716000000b00b8c0264c7bdso2478787ybh.20 for ; Thu, 06 Apr 2023 19:18:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680833920; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NM3Z7cWxI8VgV2XX878CM3uoNsycYkoMYtaVz8/N1I0=; b=JGWnVDonFr7VmZ2kaJUOTe//CwQ+BYZhctxv/nQh12y8X44DdQq2iVCQBI9tqZxGd/ 0KquaLo1cvhtPAH0IlRjD8MPKMTxKugwlzhjJP2l1LtcDbDXPBESuAwf1d1+k6MrnOHU D1XiV1c3GqNobn2XqB1fnwGqP+9WYk7lHBPMfRDUR3kfJHOu91BelcC7JfjOFGDm/A3o C9PGz6pf30pvdw5FFLM9QtCO5jfks1FeMiId2MdQjteS3SzYbfqIIgbFPiiX/egFsNX7 TuNT7xBKg7Vc99HIbCdDmBVlh/qyL3u20GMRE4UEsQjjDjnc/z1mWtAi+K2eBEF8WjNX Wr9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680833920; 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=NM3Z7cWxI8VgV2XX878CM3uoNsycYkoMYtaVz8/N1I0=; b=D6ltNKfu1yWCuGKdmmfgkxOsk/q9vNdrhN5PgKorVwr6PmHfwAxjeQAHGNgmLxixUz RZ0U0DZUw7D+W0iHg9EJYxOyOp1BExSz6XpE1VW4exWn9XtWZdV8aDQMq/xOX2/CNmx1 zYsiZG2PB8GebkO6K42Jz878UyQaKRG8BpwZlevcr+tZhkXOz2lMwpuPGeVRHggt/JH7 +LBrQ/WIVrdlC4kHP9jb2RDTMNTVa5tOzg469EbJwRiID+IJsjHRmESXrJJ7S2J/gKZe ofFfFAg8qXMr8dawHQ4FRWoiA/BujHCKnG7ALuJgRfLAMUpkDqAmtMc+mYMnA2vyCqD1 qY4Q== X-Gm-Message-State: AAQBX9cru/+nQEE0Hutxxb3JEwtmPu8/uiYne1/Uo3zMl9ADcfGGWHvD g4PrHcK3SNf/m5Hpz6oty8D+X9s6b1w= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:909:b0:a27:3ecc:ffe7 with SMTP id bu9-20020a056902090900b00a273eccffe7mr3536502ybb.3.1680833920841; Thu, 06 Apr 2023 19:18:40 -0700 (PDT) Date: Thu, 6 Apr 2023 19:18:39 -0700 In-Reply-To: <20230310105346.12302-6-likexu@tencent.com> Mime-Version: 1.0 References: <20230310105346.12302-1-likexu@tencent.com> <20230310105346.12302-6-likexu@tencent.com> Message-ID: Subject: Re: [PATCH 5/5] KVM: x86/pmu: Hide guest counter updates from the VMRUN instruction From: Sean Christopherson To: Like Xu Cc: Paolo Bonzini , Ravi Bangoria , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-7.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 10, 2023, Like Xu wrote: > From: Like Xu > > When AMD guest is counting (branch) instructions event, its vPMU should > first subtract one for any relevant (branch)-instructions enabled counter > (when it precedes VMRUN and cannot be preempted) to offset the inevitable > plus-one effect of the VMRUN instruction immediately follows. > > Based on a number of micro observations (also the reason why x86_64/ > pmu_event_filter_test fails on AMD Zen platforms), each VMRUN will > increment all hw-(branch)-instructions counters by 1, even if they are > only enabled for guest code. This issue seriously affects the performance > understanding of guest developers based on (branch) instruction events. > > If the current physical register value on the hardware is ~0x0, it triggers > an overflow in the guest world right after running VMRUN. Although this > cannot be avoided on mainstream released hardware, the resulting PMI > (if configured) will not be incorrectly injected into the guest by vPMU, > since the delayed injection mechanism for a normal counter overflow > depends only on the change of pmc->counter values. IIUC, this is saying that KVM may get a spurious PMI, but otherwise nothing bad will happen? > +static inline bool event_is_branch_instruction(struct kvm_pmc *pmc) > +{ > + return eventsel_match_perf_hw_id(pmc, PERF_COUNT_HW_INSTRUCTIONS) || > + eventsel_match_perf_hw_id(pmc, > + PERF_COUNT_HW_BRANCH_INSTRUCTIONS); > +} > + > +static inline bool quirky_pmc_will_count_vmrun(struct kvm_pmc *pmc) > +{ > + return event_is_branch_instruction(pmc) && event_is_allowed(pmc) && > + !static_call(kvm_x86_get_cpl)(pmc->vcpu); Wait, really? VMRUN is counted if and only if it enters to a CPL0 guest? Can someone from AMD confirm this? I was going to say we should just treat this as "normal" behavior, but counting CPL0 but not CPL>0 is definitely quirky.