Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp976943ybl; Wed, 11 Dec 2019 10:25:49 -0800 (PST) X-Google-Smtp-Source: APXvYqyYWwYsmRhRDTRHrndsS8ZEAyk5mC1VZo3bazvEtAVoTEVjAJhG7wfljdNuBlkEhSEfgks+ X-Received: by 2002:a9d:7a46:: with SMTP id z6mr3412759otm.194.1576088749525; Wed, 11 Dec 2019 10:25:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576088749; cv=none; d=google.com; s=arc-20160816; b=bmlr8ZS3KPF4RcrxZ+VY43yU3tTnyld6KCehrnpzQk8BnTOde9Nejul+9nZh+aPTsP UOHi1ckR6uoKYtBH2mnkci5kk6R0T3rLQCN4oYv2gIaQZDaLDlYuO+OAjb8zvwBXPztr Gl7qcjPpbA0VkC3E3x61isKw+sUu48pfY5nWsppi2w+y3/oOhfAxgtbYs4iss8LVZUka 3Y/qAFNyVW2/a/cibCGH0nLBB2vMmVRyNOCSVSHSz8pLtd90GRbW/5UODXaFlwtF998D sp+vNEQDa49rvaOk7SzKYvYgtfyqYm6JXlbr8GYJ+tTi1Veb+sqUdmThu+6XTzQlF/ll 77GA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=M9tA+Tr9ovbWOXuGiYbM0B2FW2ZyDkx7Yvdcfc6MPLs=; b=btRQ2lEwkucxw43yXIF57YmtlClTd9j2znC6756/SO5UiSmsh8b6+KW7My6Sp/V95v AO9Apm0sRDuyLxS+vGynRy1oljDVRMPwXsmU9SUBMOBJUb2qHiS4ISvNGMmUNc3jSUkR lL2CFWDeaEPY60ALivXN+dc9wt+kcpnH1yyeTYBXoEcirde1Pj9ydPElBrOVrqwES/h7 djQeCmTH7uwfWawKISCvWhxZlLIoym1839Tc1BJQSvWeHT9l+QRqIKO38cJEoW46XhAP 3UM9Df5EJEe/xZy7lBZKnTAe4MeFRD4MneSEy71Ii8ewJF6xxMeoUZ3F/JrP6jLbXQfR 4GCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=idAqrZS5; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 65si1140328oif.14.2019.12.11.10.25.36; Wed, 11 Dec 2019 10:25:49 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=idAqrZS5; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730127AbfLKSYs (ORCPT + 99 others); Wed, 11 Dec 2019 13:24:48 -0500 Received: from mail-il1-f196.google.com ([209.85.166.196]:38800 "EHLO mail-il1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726411AbfLKSYs (ORCPT ); Wed, 11 Dec 2019 13:24:48 -0500 Received: by mail-il1-f196.google.com with SMTP id f5so1825831ilq.5 for ; Wed, 11 Dec 2019 10:24:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=M9tA+Tr9ovbWOXuGiYbM0B2FW2ZyDkx7Yvdcfc6MPLs=; b=idAqrZS5yOYwdeK/lgm7YpOyYtpiEJT5h1P6uhhaane+g2Xn7IV2dkSWP53z017gAQ /AlG0kqHi/x8IM6vj96WukdMd3t7bSnKbqp5avoga1TPrk8lNOneJRYi9DrVcClaM5rV uMgh5WkX8h3tYuTaK69vJOzJm8f0+V4ZPUEOhI47JT+9C8E3vc27FsRZS2QoQ5OC8p33 kbpTVseXf9aflsmpw57dqgtpsIZKmbXYkuNctxJ6lcFkdTeQNFXDHSSF5LUtNox/2mDw OXWH7XGEe8tn/Bqh0f1qD3KYZ0F3d9lblN/7QzTO+cw7ICLs84AwE3dnQQJ8lYLgRFxE SRDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=M9tA+Tr9ovbWOXuGiYbM0B2FW2ZyDkx7Yvdcfc6MPLs=; b=CtbHxQtbO3Pl77vhvt2ChDsWZfeR3c94n7vQtyUthy6BSh6K3/XAzEs7ZSIUkOEyIV DUG1vu25SSVi/XMSuCqb+GM9dyA/nUwjoJ8Ie/qf7EREMFD9sDWRZc5QQVcRkcQiOEtE AaiSWZjr+Vf6z1BJZNtE3gtqeYPx82BxfGvrFe3ValyoI85OrAIv/3no3tEAbbXtSbvO e5+yaCbRkR+fvNqJflA4COdQ1SLkwY06YgWXTtA5dVEvKR14Suxxd0AUlLSX6AwrCQW4 3I+cFtOBI3aN7yPx2nmXApG0FxP70dx3F7HuE+aSK+rxsqLM/35FElLPHx3bjs2JfUuS 2JRA== X-Gm-Message-State: APjAAAWyaHDUB3TusW+F8KCYUtmygw7L+YDrELtW1nVuBVG1IybW9KBl 9acTaia7Qv6drQNwjEb7PRziN8t/9LySeqqaHFeE8w== X-Received: by 2002:a05:6e02:8eb:: with SMTP id n11mr4519653ilt.26.1576088687084; Wed, 11 Dec 2019 10:24:47 -0800 (PST) MIME-Version: 1.0 References: <20191211175822.1925-1-sean.j.christopherson@intel.com> <20191211175822.1925-2-sean.j.christopherson@intel.com> In-Reply-To: <20191211175822.1925-2-sean.j.christopherson@intel.com> From: Jim Mattson Date: Wed, 11 Dec 2019 10:24:36 -0800 Message-ID: Subject: Re: [PATCH 1/2] KVM: x86: Add build-time assertion on usage of bit() To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Joerg Roedel , kvm list , LKML Content-Type: text/plain; charset="UTF-8" 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 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? > + > + return 1 << (feature & 31); Why not BIT(feature & 31)? > } > > static inline u8 vcpu_virt_addr_bits(struct kvm_vcpu *vcpu) > -- > 2.24.0 >