Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1031977ybl; Wed, 11 Dec 2019 11:20:09 -0800 (PST) X-Google-Smtp-Source: APXvYqz4ISEbMbiE6S/MChiH0lxpaes/o2Eue9/pAxfonWtp+LATvO9YuH0b3meILCewnheVXnqV X-Received: by 2002:aca:d610:: with SMTP id n16mr4208904oig.108.1576092009387; Wed, 11 Dec 2019 11:20:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576092009; cv=none; d=google.com; s=arc-20160816; b=B6OXzIqhEJeTUmcBrGg/iHbm89qSfsZcGFKS9dixLChMG0ucJaTQDALB4nwwS5/gXM QgKEVzE2lAcilN3tV0g7+d7rq4r7Oih615Na/RoX5G/C8CiAk2fLSCoBYzsrk9nMtP0X 2hmtQwtazFsQ3amV/MbUJFVs/6bFcw/GUkbP73NOo7fey4/xTFSjGO1ejmx3RyaWrbCC 1BHm5vM/UVUkrj3nah5Mc+FbfXYxfCesMq8JszaVicdWghSx630cokjkHnifLfJ10z+i XgmGHtPB0nRR0KVMcviGLNuLrkeEVOB/qymL4Iuw6ffijZjLVx364vyKYjN6l843R8Ex 8AmQ== 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=PV/07rlch82hsOlenSaWXsWDxhhq1WXYqcx8dnjWzmQ=; b=X6yVA2DzI3T/7TDe5AFW6oiVZtSs7E0tOLtJbegAcU3odONu4lDuLjSUlIsYnnkMdm OST8+NWLq2WQNGcX63SDdbmNzOuyvDWXNEppr3ZNhYG1pX4yg85t55OS6ukq37ecBObf Ej2yDHD+yLaRoAVGla8JH4jBc4eQOLFWoboNcD7VJDj0rDjLmOaclv3BnIpCpz6Ylsh4 hw+FDXJ/FI+bbu/hkGFhNTuomdAUtSU0n87cmC0LrQJ7hzRvmD9eARaOFIYAyKyCPcRO kfV4pJg16C1yMRji6F3B/bPxdJHoIPxR7BmcYVKhzfqAalASUpKA1wScr/j48txzHA/u FE3w== 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 d187si1749537oif.33.2019.12.11.11.19.56; Wed, 11 Dec 2019 11:20:09 -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 S1727314AbfLKTST (ORCPT + 99 others); Wed, 11 Dec 2019 14:18:19 -0500 Received: from mga18.intel.com ([134.134.136.126]:23830 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726312AbfLKTST (ORCPT ); Wed, 11 Dec 2019 14:18:19 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 11 Dec 2019 11:18:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,302,1571727600"; d="scan'208";a="245399141" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by fmsmga002.fm.intel.com with ESMTP; 11 Dec 2019 11:18:17 -0800 Date: Wed, 11 Dec 2019 11:18:17 -0800 From: Sean Christopherson To: Jim Mattson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Joerg Roedel , kvm list , LKML Subject: Re: [PATCH 1/2] KVM: x86: Add build-time assertion on usage of bit() Message-ID: <20191211191817.GJ5044@linux.intel.com> References: <20191211175822.1925-1-sean.j.christopherson@intel.com> <20191211175822.1925-2-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Wed, Dec 11, 2019 at 10:24:36AM -0800, Jim Mattson wrote: > On Wed, Dec 11, 2019 at 9:58 AM Sean Christopherson > wrote: > > > > Add build-time checks to ensure KVM isn't trying to do a reverse CPUID > > lookup on Linux-defined feature bits, along with comments to explain > > the gory details of X86_FEATUREs and bit(). > > > > Signed-off-by: Sean Christopherson > > --- > > > > Note, the premature newline in the first line of the second comment is > > intentional to reduce churn in the next patch. > > > > arch/x86/kvm/x86.h | 23 +++++++++++++++++++++-- > > 1 file changed, 21 insertions(+), 2 deletions(-) > > > > diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h > > index cab5e71f0f0f..4ee4175c66a7 100644 > > --- a/arch/x86/kvm/x86.h > > +++ b/arch/x86/kvm/x86.h > > @@ -144,9 +144,28 @@ static inline bool is_pae_paging(struct kvm_vcpu *vcpu) > > return !is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu); > > } > > > > -static inline u32 bit(int bitno) > > +/* > > + * Retrieve the bit mask from an X86_FEATURE_* definition. Features contain > > + * the hardware defined bit number (stored in bits 4:0) and a software defined > > + * "word" (stored in bits 31:5). The word is used to index into arrays of > > + * bit masks that hold the per-cpu feature capabilities, e.g. this_cpu_has(). > > + */ > > +static __always_inline u32 bit(int feature) > > { > > - return 1 << (bitno & 31); > > + /* > > + * bit() is intended to be used only for hardware-defined > > + * words, i.e. words whose bits directly correspond to a CPUID leaf. > > + * Retrieving the bit mask from a Linux-defined word is nonsensical > > + * as the bit number/mask is an arbitrary software-defined value and > > + * can't be used by KVM to query/control guest capabilities. > > + */ > > + BUILD_BUG_ON((feature >> 5) == CPUID_LNX_1); > > + BUILD_BUG_ON((feature >> 5) == CPUID_LNX_2); > > + BUILD_BUG_ON((feature >> 5) == CPUID_LNX_3); > > + BUILD_BUG_ON((feature >> 5) == CPUID_LNX_4); > > + BUILD_BUG_ON((feature >> 5) > CPUID_7_EDX); > > What is magical about CPUID_7_EDX? It's currently the last cpufeatures word. My thought was to force this to be updated in order to do reverse lookup on the next new word. I didn't want to use NCAPINTS because that gets updated when a new word is added to cpufeatures, i.e. wouldn't catch the case where the next new word is a Linux-defined word, which is extremely unlikely but theoretically possible. > > + > > + return 1 << (feature & 31); > > Why not BIT(feature & 31)? That's a very good question. > > } > > > > static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu) > > -- > > 2.24.0 > >