Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1259249rwb; Wed, 7 Dec 2022 10:29:53 -0800 (PST) X-Google-Smtp-Source: AA0mqf46ZYODhudfRRP1ad+du1mxqhCFQYe8irxiaUxzbb8XSjJ8QfbIZSnwdW+ofVgiORFsneRK X-Received: by 2002:a17:906:32ce:b0:78d:9022:f146 with SMTP id k14-20020a17090632ce00b0078d9022f146mr60497051ejk.656.1670437792829; Wed, 07 Dec 2022 10:29:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670437792; cv=none; d=google.com; s=arc-20160816; b=k397RBZ5g6Y2efx8SMwOR2hvVPEkQgerR34l3Zz55NiUpH7STCzcoUzCuFlTXnQ9wO 4fGPNLMK6sGwBH4vrXSrF12qIZ40gFonx99Xn17/z4ulE3KM1FE4g/5qy8+Il+yknEua 1I23Jd1K2gLPG4Y2IW+9nmUbqmS0RbTUNR98r3kZ7zm+PNWIs+F5cT0V8ffXJ4k7jc5G YDnrNYXCGAQop8LSbPqdPDr23eUHDJqWBAVgx1ccAFM6921GtiHKpXjR86vZ0haTzqij RnBNj6lvLRdm0cfAHjLP+so0eN8iI9BUKhkRhvN8uri669h2scLL/Hmy5F7Q12UzQnkX mMKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=DoJAqfR7ZvwAFiT9srWB2lyZBimUwRAh5SkQOgCpWVU=; b=qMwTDxzxHYotljSCIEgyxvgMplUc/AtDUUSpDVPWHed/2q8vd2RE4iAgoRzIzxjyhC 1mhnlMSbc/aF0/pdWd0pGmciAdQjNHQQ6tSqfTSnBbuv+Lgula496jdUhFthGEuYQzUz /QA7QN0fB1A1ZEPtBqNB3KOb7Pl3kDRYMIbtOzFQtELAgVZtiqEhILPUcXahvvg+QJny 8/j8cz4UBdWMw0/SIbPrasUH3PdnSs40piHBVNMVWOsUSjYQlj8Akz+0oz/CnASqLE/0 nYqJGcDuDK2q5ObXwMbeACc3yPoeeZyAt5FwYnshHlJ4scDlS8etQ7NMGDuZLVAQuOq7 DGVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=agsFaYqE; 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 k13-20020a170906680d00b0078dec48d442si12335348ejr.680.2022.12.07.10.29.34; Wed, 07 Dec 2022 10:29:52 -0800 (PST) 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=agsFaYqE; 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 S230100AbiLGRtG (ORCPT + 74 others); Wed, 7 Dec 2022 12:49:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229643AbiLGRtE (ORCPT ); Wed, 7 Dec 2022 12:49:04 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C19E68697 for ; Wed, 7 Dec 2022 09:49:03 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id e7-20020a17090a77c700b00216928a3917so2138086pjs.4 for ; Wed, 07 Dec 2022 09:49:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=DoJAqfR7ZvwAFiT9srWB2lyZBimUwRAh5SkQOgCpWVU=; b=agsFaYqECAL1MPKDSRlaJV8kh8LVOmH7JyrbPFY5LZHTSTSh4H1W0rBB/2jFHBM+ug Ftz6j0F8PaZ/B0r106lYXc+WPNhnsE5AeHrn52PmxZ6jA4CxChancNX5S4XeRqatj7ml Z+VNICtE76VbH/asrkz0yXijc36RDB2MozVL3yONb34dKckZdK3ORVSMef3B+SAWUYjR yf7dCgc2ybBs1pTC7KU/zZ+J/nXsJjgUWHmLWU6YMVhQRl92etqNEXmiqUjBKcSSso3V eYgjy4z83ZLVYjGSAkg+BH96z7VcIMqQ95O6AgQNoxiKZfnRe8UCGwDOPQ48N9xy8ZrG Lw+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=DoJAqfR7ZvwAFiT9srWB2lyZBimUwRAh5SkQOgCpWVU=; b=BoAGznVELt054AoJpyUALIzEheS9G7hv4Nov0VEqwo744PKKwG/gPpW4WGUbLikAXE EPttJ+WAyVKm9jQFFsXvA8hG2WJFsgXPH+gjr3Z1HA5XEfJ3NNWPerYYoafu0rSFvKo/ YR4MdvcalSCp+TCITnm6WBK2c7jqpF0K2OmfKCT2d9wODk592w548W4Kr7LmOkELai+T sZByj++ktM3Y7NTwdxK/niVK4LZGEBrnHR8GhkN/Lel8rMmd5m1Rh0TZR0uF+/7aSMiN CN3OhhIBvpbJurWia71RHlVL42p8Mljss0yfDZ6sm/AHWC4lZtlMwg2Hp9NNMaaRJmoj Sl0w== X-Gm-Message-State: ANoB5pkiuRD/+tMixwEDWksvgqWutX/GPgw7lL4q1gWXwtV2RwCgp4rq J4FeO0yJIpo/MdTc9+no+o408Q== X-Received: by 2002:a05:6a21:9996:b0:a4:efde:2ed8 with SMTP id ve22-20020a056a21999600b000a4efde2ed8mr1749638pzb.0.1670435342977; Wed, 07 Dec 2022 09:49:02 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id m17-20020a170902f21100b00189393ab02csm14805031plc.99.2022.12.07.09.49.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Dec 2022 09:49:02 -0800 (PST) Date: Wed, 7 Dec 2022 17:48:58 +0000 From: Sean Christopherson To: Like Xu Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: x86/pmu: Avoid ternary operator by directly referring to counters->type Message-ID: References: <20221205113718.1487-1-likexu@tencent.com> <38b2a836-f9a4-23e4-107b-61efc74638a4@gmail.com> <7db2bc22-99b8-96f3-66f3-d1695e2e82c1@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7db2bc22-99b8-96f3-66f3-d1695e2e82c1@gmail.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 Wed, Dec 07, 2022, Like Xu wrote: > On 7/12/2022 1:19 am, Sean Christopherson wrote: > > On Tue, Dec 06, 2022, Like Xu wrote: > > > > > diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c > > > > > index e5cec07ca8d9..28b0a784f6e9 100644 > > > > > --- a/arch/x86/kvm/vmx/pmu_intel.c > > > > > +++ b/arch/x86/kvm/vmx/pmu_intel.c > > > > > @@ -142,7 +142,7 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, > > > > > } > > > > > if (idx >= num_counters) > > > > > return NULL; > > > > > - *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP]; > > > > > + *mask &= pmu->counter_bitmask[counters->type]; > > > > > > > > In terms of readability, I have a slight preference for the current code as I > > IMO, using counters->type directly just like pmc_bitmask() will add more readability > and opportunistically helps some stale compilers behave better. Anyone that cares about this level of micro-optimization absolutely should be using a toolchain that's at or near the bleeding edge. > > > > don't have to look at counters->type to understand its possible values. > > > When someone tries to add a new type of pmc type, the code bugs up. > > > > Are there new types coming along? If so, I definitely would not object to refactoring > > this code in the context of a series that adds a new type(s). But "fixing" this one > > case is not sufficient to support a new type, e.g. intel_is_valid_rdpmc_ecx() also > > needs to be updated. Actually, even this function would need additional updates > > to perform a similar sanity check. > > True but this part of the change is semantically relevant, which should not > be present in a harmless generic optimization like this one. Right ? For modern compilers, it's not an optimization. > > if (fixed) { > > counters = pmu->fixed_counters; > > num_counters = pmu->nr_arch_fixed_counters; > > } else { > > counters = pmu->gp_counters; > > num_counters = pmu->nr_arch_gp_counters; > > } > > if (idx >= num_counters) > > return NULL; > > > > > And, this one will make all usage of pmu->counter_bitmask[] more consistent. > > > > How's that? There's literally one instance of using ->type > > > > static inline u64 pmc_bitmask(struct kvm_pmc *pmc) > > { > > struct kvm_pmu *pmu = pmc_to_pmu(pmc); > > > > return pmu->counter_bitmask[pmc->type]; > > } > > > > everything else is hardcoded. And using pmc->type there make perfect sense in > > that case. But in intel_rdpmc_ecx_to_pmc(), there is already usage of "fixed", > > so IMO switching to ->type makes that function somewhat inconsistent with itself. > > More, it's rare to see code like " [ a ? b : c] " in the world of both KVM and x86. There are a few false positives here, but ternary operators are common. $ git grep ? arch/x86/kvm | wc -l 292 If you're saying that indexing an array with a ternary operator is rare, then sure, but only because there is almost never anything that fits such a pattern, not because it's an inherently bad pattern. > Good practice (branchless) should be scattered everywhere and not the other > way around. Once again, modern compilers will not generate branches for this code.