Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3581845pxp; Tue, 8 Mar 2022 18:01:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJz61sqEFvLZHJgD1Pckp18VsE4g5ZdL8B3vQzLZDKY4l+BApq8idvpQ095MKOY0g8dyq6mI X-Received: by 2002:a17:902:c94e:b0:151:a988:f3dd with SMTP id i14-20020a170902c94e00b00151a988f3ddmr20680024pla.142.1646791315715; Tue, 08 Mar 2022 18:01:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646791315; cv=none; d=google.com; s=arc-20160816; b=bRqvJ5bEgrJTKhlnH/38GjazgOtnHs9T9TowzlVQqrumW8qtVXUXXvyzwfXYb0312p a+8EvnqLa6Ibcw7Xq2Ih48ADjGgeXig1obKqwdgdqZvLUAGGcLarVC0uQ4uYL7In9Q25 wtD0yVfP+4e2f/odTDaJejRlvLZUdUGzGBjYy3sO8yCDRB38oBZOh3/EMQu+ByvPI3i2 /i6xaefC0czkQYCzkaj/XTphjpvIzyMmxwLspctuSSRXKfzwbYGzkiuwJx2QJxdvwIHo PrDnpsbgEt45y70NdXmphvkRWk9ViGOfcuBpM2nuWNZGvq1pBU0vRbmZxPdN1990UWDd V1UA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=4bG9ceNDXZVVDENiolsCGhpylb9dUhIx0aVVVy9Bke4=; b=KgZxmmj58TdWtyuJCjqsGUrYMCPc/awjJN1oB4JeCWIWz14RYHm06TCD5WOktgy1cz bqWNfYtsniYJcatvuEX5xAfRKGJ98wkeZApG+hBAVSE9FDNRtL9OERsWVaztdYA0h27p jlCRQxNA7EbHa8d4eovBoONvq1ifjsGB3Jm5fV+5+S88UK//ifaoqTkGIzOOq60PWenA l5djcIxeXe3xcY32UryQyKIYIA6Zst3lYuIR9jwcxTT+ocJZ/lyizw1F/m+lyGYD8kHz dopCbA8GfSWZ5Flfu4nuzQy/Xi3uOHjEqrGAnlYFSjY0d0826Wv4ZgcQZx/rxVig3TlR 1eZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=HQ0DyEWK; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id i2-20020a170902c94200b0015202100f38si667511pla.163.2022.03.08.18.01.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Mar 2022 18:01:55 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=HQ0DyEWK; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0BE19109A72; Tue, 8 Mar 2022 16:35:27 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344131AbiCHQKh (ORCPT + 99 others); Tue, 8 Mar 2022 11:10:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233693AbiCHQKg (ORCPT ); Tue, 8 Mar 2022 11:10:36 -0500 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9D87A4FC7A for ; Tue, 8 Mar 2022 08:09:39 -0800 (PST) Received: by mail-oi1-x22c.google.com with SMTP id j83so8645210oih.6 for ; Tue, 08 Mar 2022 08:09:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=4bG9ceNDXZVVDENiolsCGhpylb9dUhIx0aVVVy9Bke4=; b=HQ0DyEWKpny5lxdCG2aCDyQ7Balh4hyLGaA20bc+yEU55VOW2NWcYOA3XRqVcrPhc2 3yqXu54hPjELKwKFbhISE5ffAvubeegWVOf2mBDeA5UotkqQIqT/lvBtLIsIl3EgI4ys HKL2jFWnT3Ft5Vo64glxv7T7AQUN/uwTpsl+cZ2Oj5P8rv0YCzNf9bQi5jkzWaJiUh0v SgwWKqegWKhcK6Fnq6LlRZGOttQ4BYbnyqFXuwyan3tkp+YWr0SErC5ugY7+wqZpbfPF dCXJO8uZd9O7/Cw+oKtytfqdf5SMKyxrD3hXUDIw1UCBMnk8I2zCm1fv+uITuVSE/xlt ftcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=4bG9ceNDXZVVDENiolsCGhpylb9dUhIx0aVVVy9Bke4=; b=u3YI2+KAoFEcu4gQl30a4Tq+l9iqpdniTshXhQScOsd08+Wlvbc6x3d8M/E6J5cecM E1hX+P5LQZJROA0ps8umoQgMTkxUd4SQmLfhgdUXHbauJC1cy5Q0C4/OM9OE8YZOOb8A yB+PpMag56wDxdppruF8m2ZbUrEskPQ3r6W3SAhQ8SegN2PC7gdsuWkQx1UnSl4pvEzu nu7psU8Mel7meLs4r41sPJaFpRVoE/AI0I9sVywaWjlKnXrrghEOBaFDfavnV4CYBaVJ RvAxvzcOblEOGxDotWran9YnPgn8wErpUNtbOBWqLT0fx3mQi/ytpXsY+OgmB1B+ixsx FWKw== X-Gm-Message-State: AOAM533DRusccb+yq0bPAhN6c82l5nNg3yje/7wWkXDVeRa6/7oCysRw YAyPNkp14y1qLcE69xqiVj7mI+ggN2gnBJ4G6zR08A== X-Received: by 2002:a05:6808:118d:b0:2d9:a01a:48c2 with SMTP id j13-20020a056808118d00b002d9a01a48c2mr2986270oil.269.1646755778269; Tue, 08 Mar 2022 08:09:38 -0800 (PST) MIME-Version: 1.0 References: <20220307063805.65030-1-likexu@tencent.com> In-Reply-To: From: Jim Mattson Date: Tue, 8 Mar 2022 08:09:27 -0800 Message-ID: Subject: Re: [PATCH] KVM: x86/pmu: Isolate TSX specific perf_event_attr.attr logic for AMD To: Like Xu Cc: Paolo Bonzini , Ravi Bangoria , Andi Kleen , kvm@vger.kernel.org, Sean Christopherson , Wanpeng Li , Vitaly Kuznetsov , Joerg Roedel , linux-kernel@vger.kernel.org, Kan Liang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no 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 Tue, Mar 8, 2022 at 3:59 AM Like Xu wrote: > > On 8/3/2022 5:39 am, Jim Mattson wrote: > > On Sun, Mar 6, 2022 at 10:38 PM Like Xu wrote= : > >> > >> From: Like Xu > >> > >> HSW_IN_TX* bits are used in generic code which are not supported on > >> AMD. Worse, these bits overlap with AMD EventSelect[11:8] and hence > >> using HSW_IN_TX* bits unconditionally in generic code is resulting in > >> unintentional pmu behavior on AMD. For example, if EventSelect[11:8] > >> is 0x2, pmc_reprogram_counter() wrongly assumes that > >> HSW_IN_TX_CHECKPOINTED is set and thus forces sampling period to be 0. > >> > >> Opportunistically remove two TSX specific incoming parameters for > >> the generic interface reprogram_counter(). > >> > >> Fixes: 103af0a98788 ("perf, kvm: Support the in_tx/in_tx_cp modifiers = in KVM arch perfmon emulation v5") > >> Co-developed-by: Ravi Bangoria > >> Signed-off-by: Ravi Bangoria > >> Signed-off-by: Like Xu > >> --- > >> Note: this patch is based on [1] which is considered to be a necessary= cornerstone. > >> [1] https://lore.kernel.org/kvm/20220302111334.12689-1-likexu@tencent.= com/ > >> > >> arch/x86/kvm/pmu.c | 29 ++++++++++++++--------------- > >> 1 file changed, 14 insertions(+), 15 deletions(-) > >> > >> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c > >> index 17c61c990282..d0f9515c37dd 100644 > >> --- a/arch/x86/kvm/pmu.c > >> +++ b/arch/x86/kvm/pmu.c > >> @@ -99,8 +99,7 @@ static void kvm_perf_overflow(struct perf_event *per= f_event, > >> > >> static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, > >> u64 config, bool exclude_user, > >> - bool exclude_kernel, bool intr, > >> - bool in_tx, bool in_tx_cp) > >> + bool exclude_kernel, bool intr) > >> { > >> struct perf_event *event; > >> struct perf_event_attr attr =3D { > >> @@ -116,16 +115,18 @@ static void pmc_reprogram_counter(struct kvm_pmc= *pmc, u32 type, > >> > >> attr.sample_period =3D get_sample_period(pmc, pmc->counter); > >> > >> - if (in_tx) > >> - attr.config |=3D HSW_IN_TX; > >> - if (in_tx_cp) { > >> - /* > >> - * HSW_IN_TX_CHECKPOINTED is not supported with nonzer= o > >> - * period. Just clear the sample period so at least > >> - * allocating the counter doesn't fail. > >> - */ > >> - attr.sample_period =3D 0; > >> - attr.config |=3D HSW_IN_TX_CHECKPOINTED; > >> + if (guest_cpuid_is_intel(pmc->vcpu)) { > > > > This is not the right condition to check. Per the SDM, both bits 32 > > and 33 "may only be set if the processor supports HLE or RTM." On > > other Intel processors, this bit is reserved and any attempts to set > > them result in a #GP. > > We already have this part of the code: > > entry =3D kvm_find_cpuid_entry(vcpu, 7, 0); > if (entry && > (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RT= M)) && > (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) > pmu->reserved_bits ^=3D HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; I stand corrected. > > additional constraint which is ignored here: "This bit may only be set > > for IA32_PERFEVTSEL2." I have confirmed that a #GP is raised for an > > attempt to set bit 33 in any PerfEvtSeln other than PerfEvtSel2 on a > > Broadwell Xeon E5. > > Yes, "19.3.6.5 Performance Monitoring and Intel=C2=AE TSX". > > I'm not sure if the host perf scheduler indicate this restriction. Whether it does or it doesn't, it's KVM's responsibility to synthesize the #GP if IN_TXCP is set for any PerfEvtSeln other than PerfEvtSel2.