Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751645AbdILPTD (ORCPT ); Tue, 12 Sep 2017 11:19:03 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59630 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751430AbdILPS6 (ORCPT ); Tue, 12 Sep 2017 11:18:58 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2886981E0C Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=rkrcmar@redhat.com Date: Tue, 12 Sep 2017 17:18:52 +0200 From: Radim =?utf-8?B?S3LEjW3DocWZ?= To: Dmitry Vyukov Cc: David Hildenbrand , Paolo Bonzini , LKML , KVM list , llvmlinux@lists.linuxfoundation.org, Alexander Potapenko , andreyknvl , Michael Davidson , Greg Hackmann , Nick Desaulniers Subject: Re: "KVM: x86: generalize guest_cpuid_has_ helpers" breaks clang Message-ID: <20170912151851.GA24313@flask> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 12 Sep 2017 15:18:58 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1439 Lines: 42 2017-09-12 16:42+0200, Dmitry Vyukov: > Hi Radim, > > I've just noticed that your commit "KVM: x86: generalize > guest_cpuid_has_ helpers" breaks clang build on this assert: > > static __always_inline struct cpuid_reg x86_feature_cpuid(unsigned x86_feature) > { > unsigned x86_leaf = x86_feature / 32; > > BUILD_BUG_ON(!__builtin_constant_p(x86_leaf)); > > > In clang __builtin_constant_p is never true for function arguments, > it's true only for compile-time constants (what you can use as stack > array size, or C++ template argument). What would work is an > additional macro along the lines of: GCC optimizes it thanks to __always_inline, so the x86_feature is constant in each instance of this function ... the goal is to have compile-time input checking. > #define x86_feature_cpuid(x) (BUILD_BUG_ON(!__builtin_constant_p(x), > __x86_feature_cpuid(x)) > > But again assuming that caller pass the constant directly. The __builtin_constant_p() check is just a canary, the important ones are BUILD_BUG_ON(x86_leaf >= ARRAY_SIZE(reverse_cpuid)); BUILD_BUG_ON(reverse_cpuid[x86_leaf].function == 0); and these would be very awkward if moved out of the function. > Could you please fix it? Sure, I can just make them BUG_ON (or WARN_ON with error handling), but I tried with clang version 4.0.1 and got no errors -- are you using an older version? (or a command other than `make HOSTCC=clang CC=clang`) Thanks.