Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2423402ybl; Thu, 9 Jan 2020 12:29:44 -0800 (PST) X-Google-Smtp-Source: APXvYqwVJaeJe5oblue1W/j+g/EfpJ2FzNzYTsutTHpbCHmUdD2AfJce3+S8qXlzbiseltQQqLlk X-Received: by 2002:aca:d5d3:: with SMTP id m202mr4487633oig.161.1578601784653; Thu, 09 Jan 2020 12:29:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578601784; cv=none; d=google.com; s=arc-20160816; b=m2JRLIn4a7Bhn2LKK2L7rLS5uqkJQmtqLidTYFXmCeG4cLkv6DSkDBb8TVtSoIBvgs esOpSLWpg9eKv3yl83uUPe3g+q1lURs+aFTVwXEZseqlHIUf5Aw1SNJmvwVEJ5SMOOOB g/EPzYmv/s0jv7SCXkLGfgTBKBxBhs2AwobkcOIuCsu7CPn/MIcqA4CYQZGW9vCWcaGQ PgY+lirSsghEW/4HSZnb8HtVE6eIR20JgPgphtSiBFsErnsFScKxyEOW4tpg6l0JdngB w4f/z4Vnokhwf6p0dB/Yjn5OkDq+jX9oY8fc8pac/tgwm4nL2EONotExc36+2hfj9Yl7 6RDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=2twDXwHYivkUht/v3bbFV8ZEGA66CjO/T88A4oOsdjQ=; b=TZ/w7N5n9jlR/zYbvsX144dRbXrPSxW1IaOZJTFlsRfTG7v9PmXW7FYisUVS2EO3xo siHv786VIGIud+3wo5qcq3rKe3LUN5c1xDdp+k3eCDt6ly3uuz6J06si0mJ+E5+Rxl5C DVDLHBHXs3thtfhSz04S30tclall7bxPsiUO9PUYF3ItnObkiS+IDLlXK0ZuoJatTLh1 tOUmBtKfRr1AQRO0JooTRgs7j+BpUMf4KrgEZFK3fXLBDRxjqbH1UEI42QsGO6NC6XYC sga5MdUSW0js60PNnai2P47XS5n2n7r4Su6VsZLFwPxJtPbb2l1ewImIJ408YWE/maLi rBaQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r5si4075361oic.19.2020.01.09.12.29.31; Thu, 09 Jan 2020 12:29:44 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387711AbgAIQgZ (ORCPT + 99 others); Thu, 9 Jan 2020 11:36:25 -0500 Received: from mga05.intel.com ([192.55.52.43]:21216 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731354AbgAIQgY (ORCPT ); Thu, 9 Jan 2020 11:36:24 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Jan 2020 08:36:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,414,1571727600"; d="scan'208";a="235422089" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.69]) by orsmga002.jf.intel.com with ESMTP; 09 Jan 2020 08:36:24 -0800 Date: Thu, 9 Jan 2020 08:36:24 -0800 From: Sean Christopherson To: Arvind Sankar Cc: David Laight , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] KVM: x86/mmu: Fix a benign Bitwise vs. Logical OR mixup Message-ID: <20200109163624.GA15001@linux.intel.com> References: <20200108001859.25254-1-sean.j.christopherson@intel.com> <20200109152629.GA25610@rani.riverdale.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200109152629.GA25610@rani.riverdale.lan> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 09, 2020 at 10:26:30AM -0500, Arvind Sankar wrote: > On Thu, Jan 09, 2020 at 02:13:48PM +0000, David Laight wrote: > > From: Sean Christopherson > > > Sent: 08 January 2020 00:19 > > > > > > Use a Logical OR in __is_rsvd_bits_set() to combine the two reserved bit > > > checks, which are obviously intended to be logical statements. Switching > > > to a Logical OR is functionally a nop, but allows the compiler to better > > > optimize the checks. > > > > > > Signed-off-by: Sean Christopherson > > > --- > > > arch/x86/kvm/mmu/mmu.c | 2 +- > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > index 7269130ea5e2..72e845709027 100644 > > > --- a/arch/x86/kvm/mmu/mmu.c > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > @@ -3970,7 +3970,7 @@ __is_rsvd_bits_set(struct rsvd_bits_validate *rsvd_check, u64 pte, int level) > > > { > > > int bit7 = (pte >> 7) & 1, low6 = pte & 0x3f; > > > > > > - return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) | > > > + return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) || > > > ((rsvd_check->bad_mt_xwr & (1ull << low6)) != 0); > > > > Are you sure this isn't deliberate? > > The best code almost certainly comes from also removing the '!= 0'. The '!= 0' is truly superfluous, removing it doesn't affect code generation. > > You also don't want to convert the expression result to zero. > > The function is static inline bool, so it's almost certainly a mistake > originally. The != 0 is superfluous, but this will get inlined anyway. Ya, the bitwise-OR was added in commit 25d92081ae2f ("nEPT: Add nEPT violation/misconfigration support"), and AFAICT it's unintentional. That being said, I was a bit hasty in stating that a logical-OR allows for better optimization, sort of. For FNAME(prefetch_invalid_gpte) and FNAME(walk_addr_generic), which branch on the result of is_rsvd_bits_set(), the logical-OR is marginally better. FNAME(prefetch_invalid_gpte) is what I initially looked at when saying "yep, that's better!". But for walk_shadow_page_get_mmio_spte(), because it aggregates the result in a loop, the bitwise-OR is better in that it eliminates a Jcc. And all that being said, there are two vastly superior optimizations that can be made: - Reorder the checks in FNAME(prefetch_invalid_gpte) to perform the !PRESENT and !ACCESSED checks before checking the reserved bits, as they are both more likely to fail and do not require additional memory accesses. - Rewrite __is_rsvd_bits_set() to make it templated. The reserved MT check is EPT only, i.e. bad_mt_xwr is always 0 for legacy 32/64-bit paging. So, I'll scrap this patch and send a mini series to effect the above optimizations. > > > > So: > > return (pte & rsvd_check->rsvd_bits_mask[bit7][level-1]) | (rsvd_check->bad_mt_xwr & (1ull << low6)); > > The code then doesn't have any branches to get mispredicted. > > > > David > > > > - > > Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK > > Registration No: 1397386 (Wales) > >