Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp535717rdf; Fri, 3 Nov 2023 07:51:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFfdydOjrQJs8yryziACuHzvsNkb8WqQkeO4CcXTq0HukiqrJdyG/tXdNJ0TqUpEOZ57v1A X-Received: by 2002:a17:903:905:b0:1c8:90bf:4239 with SMTP id ll5-20020a170903090500b001c890bf4239mr15856036plb.42.1699023095638; Fri, 03 Nov 2023 07:51:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699023095; cv=none; d=google.com; s=arc-20160816; b=xzgwoZDs8ihPIkTmt2TD3jNfRVa+AzMXFs7WIttC4Nooy29AkXYzsnYabp2kf9qjU6 5h/sMQVpn0jdtKNmOTSENh8y++4m+q2UOzREOK1Ax5AjwbNpwfnbphXvEv6q5U8sX8pu NWGRsUp4JRlPjrSmLTTzSNzRcYNFsCM4DGxmPTDfp44xigesnD+RV70N9hc6v8OKXZE1 QP8n53gJLDPgVgK/h84OeF5xeYypYFbBUM7kykrifsBskQ67PWXDF1JU6jL0DVNKA6DN F5WQeIE/mVebnICZOlTbqQBQxlKGS6c1Wm/tD76jGXQ52uAcuMqeQXSy7FhieEGG9m8j fccg== 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=zD/MaSMg/kgZSChzxbcT6Dosmg6sUB4leV3Wuu940+w=; fh=eVGfLUtBPmEy7kugkAqmGTlR+u0cHaQAdxVMmvhj+aA=; b=IAz/Tjnh5LsQFlh2t8pRsUPDxLweZZ/fA/1Swus3/P/a41F6kHpqmrcLCHIJ+3huTa WM2O3DARvklVHDojlLDp9AQsEUe+GYCftFb/BlhnTyXcoAwujLFSzyoehvq/kngCpADq K59mD4U+cknmWVEzpnqDzAIl/8gMssAH1zdA0woh+2mWx8SUSsPX9s5B8IabkjuFLFcW h70C8QlfUE9i1ZKgk4gZDZyVxinzZXaT8+7lGZcl3Z6wRb0yjH/6/kZABOgpmRi20v+V j0KkRlWONu7Q6oo1mDFfpza81s8tVjPGjI/thUFevN0Z5UNFzhlwhXCMy6lcMjIClptB 7nFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CaROKxZN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id d16-20020a170902ced000b001bdd35033efsi1684066plg.374.2023.11.03.07.51.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 07:51:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=CaROKxZN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 41834807E8BB; Fri, 3 Nov 2023 07:51:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229490AbjKCOvX (ORCPT + 99 others); Fri, 3 Nov 2023 10:51:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbjKCOvW (ORCPT ); Fri, 3 Nov 2023 10:51:22 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D0C7D47 for ; Fri, 3 Nov 2023 07:51:17 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-53eeb28e8e5so10988a12.1 for ; Fri, 03 Nov 2023 07:51:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699023075; x=1699627875; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=zD/MaSMg/kgZSChzxbcT6Dosmg6sUB4leV3Wuu940+w=; b=CaROKxZNsEHaGYgGtOOs8Cb7ObewnZGsScJ7QKRhyssuMSUVg7Q1akmnIz894E+ejc gVnsU02rLrtmTMSVv7xBcFM/Cq2TyqFRQaFdKuxZc580q1qbs+iHOqM/xpp1J1OQUFEA z5umLy5WWekNNmQDT5Ztgu5e98xAv0Wied9/n635HNfueu1972gXKftw0RO7Ns8WA23x dYNoZ8DqHb3lohpCiThNiJW2xPZ4cMouU4Fsz12qMURxHivkJV8Ymd+FOd12epqU21lg rUzt664ebb/yY/T4bLKNkLpJmXEuUt5u4ub15SxF2HKDzkzmWqEXyScDrtYYajLOILPc oKsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699023075; x=1699627875; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zD/MaSMg/kgZSChzxbcT6Dosmg6sUB4leV3Wuu940+w=; b=gFXQOYtzSGrqUnzwga5qOejZVZzi0obC7rxZdTOg57KRCFJOi6KsM6GInHxrPXMw/F 8lf4xN5uovp0J/cj9GM7apuGXZuDn8KTRrdEG4SM2tUL8mwQdy1NsXkH/dm5ruBFRT1K H2zggT9Do16oHdS5Hy45xlFN/ppXyiMicZ/5EODU4IW13kr+lv+P++J5omnyPP1MYIxi ZJuoTcGmIcR4sP2FJ5wHgQlWflkSKFLy6v6YILjEXyb5gNBYb9MTNWb5uXnjpHwKn91o 5hNjzwO0sal95+UCmBusn0we2UWALOFprD9vMpK+ktMTb6UIWrlaYf7zn6cFe5j7JSRy dW5A== X-Gm-Message-State: AOJu0YySD789RIX4IYhR9RMgB3PscV6oeR2wt8GhC13xVp3iIWEv39o3 7mqWby9ei2HdfOJRvrtFgMXw7IuLNilhy1EupznmtpCin2JcOkidzRE= X-Received: by 2002:a50:d50f:0:b0:542:d737:dc7e with SMTP id u15-20020a50d50f000000b00542d737dc7emr231923edi.0.1699023075385; Fri, 03 Nov 2023 07:51:15 -0700 (PDT) MIME-Version: 1.0 References: <20231031090613.2872700-1-dapeng1.mi@linux.intel.com> <20231031090613.2872700-2-dapeng1.mi@linux.intel.com> <85706bd7-7df0-4d4b-932c-d807ddb14f9e@linux.intel.com> <8272bad5-323e-46ae-9244-7b76832393fb@linux.intel.com> In-Reply-To: <8272bad5-323e-46ae-9244-7b76832393fb@linux.intel.com> From: Jim Mattson Date: Fri, 3 Nov 2023 07:50:59 -0700 Message-ID: Subject: Re: [Patch 1/2] KVM: x86/pmu: Add Intel CPUID-hinted TopDown slots event To: "Mi, Dapeng" Cc: "Liang, Kan" , Sean Christopherson , Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Zhenyu Wang , Zhang Xiong , Mingwei Zhang , Like Xu , Dapeng Mi , Like Xu Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, 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 lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 03 Nov 2023 07:51:32 -0700 (PDT) On Fri, Nov 3, 2023 at 3:03=E2=80=AFAM Mi, Dapeng wrote: > > > On 11/3/2023 1:45 AM, Jim Mattson wrote: > > On Wed, Nov 1, 2023 at 7:07=E2=80=AFPM Mi, Dapeng wrote: > >> > >> On 11/1/2023 9:33 PM, Liang, Kan wrote: > >>> On 2023-10-31 11:31 p.m., Mi, Dapeng wrote: > >>>> On 11/1/2023 11:04 AM, Jim Mattson wrote: > >>>>> On Tue, Oct 31, 2023 at 6:59=E2=80=AFPM Mi, Dapeng > >>>>> wrote: > >>>>>> On 11/1/2023 2:22 AM, Jim Mattson wrote: > >>>>>>> On Tue, Oct 31, 2023 at 1:58=E2=80=AFAM Dapeng Mi > >>>>>>> wrote: > >>>>>>>> This patch adds support for the architectural topdown slots even= t > >>>>>>>> which > >>>>>>>> is hinted by CPUID.0AH.EBX. > >>>>>>> Can't a guest already program an event selector to count event se= lect > >>>>>>> 0xa4, unit mask 1, unless the event is prohibited by > >>>>>>> KVM_SET_PMU_EVENT_FILTER? > >>>>>> Actually defining this new slots arch event is to do the sanity ch= eck > >>>>>> for supported arch-events which is enumerated by CPUID.0AH.EBX. > >>>>>> Currently vPMU would check if the arch event from guest is support= ed by > >>>>>> KVM. If not, it would be rejected just like intel_hw_event_availab= le() > >>>>>> shows. > >>>>>> > >>>>>> If we don't add the slots event in the intel_arch_events[] array, = guest > >>>>>> may program the slots event and pass the sanity check of KVM on a > >>>>>> platform which actually doesn't support slots event and program th= e > >>>>>> event on a real GP counter and got an invalid count. This is not > >>>>>> correct. > >>>>> On physical hardware, it is possible to program a GP counter with t= he > >>>>> event selector and unit mask of the slots event whether or not the > >>>>> platform supports it. Isn't KVM wrong to disallow something that a > >>>>> physical CPU allows? > >>>> Yeah, I agree. But I'm not sure if this is a flaw on PMU driver. If = an > >>>> event is not supported by the hardware, we can't predict the PMU's > >>>> behavior and a meaningless count may be returned and this could misl= ead > >>>> the user. > >>> The user can program any events on the GP counter. The perf doesn't > >>> limit it. For the unsupported event, 0 should be returned. Please kee= p > >>> in mind, the event list keeps updating. If the kernel checks for each > >>> event, it could be a disaster. I don't think it's a flaw. > >> > >> Thanks Kan, it would be ok as long as 0 is always returned for > >> unsupported events. IMO, it's a nice to have feature that KVM does thi= s > >> sanity check for supported arch events, it won't break anything. > > The hardware PMU most assuredly does not return 0 for unsupported event= s. > > > > For example, if I use host perf to sample event selector 0xa4 unit > > mask 1 on a Broadwell host (406f1), I get... > > > > # perf stat -e r01a4 sleep 10 > > > > Performance counter stats for 'sleep 10': > > > > 386,964 r01a4 > > > > 10.000907211 seconds time elapsed > > > > Broadwell does not advertise support for architectural event 7 in > > CPUID.0AH:EBX, so KVM will refuse to measure this event inside a > > guest. That seems broken to me. > > > Yeah, I also saw similar results on Coffee Lake which doesn't support > slots events and the return count seems to be a random and meaningless > value. If so, this meaningless value may mislead the guest perf user. > From this point view it looks the sanity check in KVM is useful, but it > indeed leads to different behavior between guest and host. Calling this a "sanity check" is somewhat specious, since the guards are based on the guest CPUID rather than the host CPUID. There is nothing to prevent the hypervisor from setting guest CPUID.0AH:EAX[31:24] to 32 and guest CPUID.0AH:EBX to 0, making 32 architectural events available to the guest. If it were really a sanity check, it would prevent the guest from using architectural events that are not supported by the host. Moreover, I'm pretty sure that a "sanity check" would only apply to top-down slots. Have there been any physical CPUs to date that support architectural events, but do not support all of the first seven architectural events? > I'm neutral on either keeping or removing this check. How's other > reviewers' opinion on this? > > > > > >>> Thanks, > >>> Kan > >>>> Add Kan to confirm this. > >>>> > >>>> Hi Kan, > >>>> > >>>> Have you any comments on this? Thanks. > >>>> > >>>> > >>>>>>> AFAICT, this change just enables event filtering based on > >>>>>>> CPUID.0AH:EBX[bit 7] (though it's not clear to me why two indepen= dent > >>>>>>> mechanisms are necessary for event filtering). > >>>>>> IMO, these are two different things. this change is just to enable= the > >>>>>> supported arch events check for slot events, the event filtering i= s > >>>>>> another thing. > >>>>> How is clearing CPUID.0AH:EBX[bit 7] any different from putting {ev= ent > >>>>> select 0xa4, unit mask 1} in a deny list with the PMU event filter? > >>>> I think there is no difference in the conclusion but with two differ= ent > >>>> methods. > >>>> > >>>>