From patchwork Thu Nov 22 03:14:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 151711 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp207021ljp; Wed, 21 Nov 2018 19:15:49 -0800 (PST) X-Google-Smtp-Source: AFSGD/XgjFEurr6euEWoUn6kaYeQrSmkaH7Iau90Q1ImfoUnWc9xoyTeyVuYVKmm3ivnaaQjeIl0 X-Received: by 2002:a17:902:7b91:: with SMTP id w17mr9484209pll.111.1542856548873; Wed, 21 Nov 2018 19:15:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542856548; cv=none; d=google.com; s=arc-20160816; b=vZqOYZTqBjiubTqVRAfxrDelnTEw19hay00MXNY4ps+ZKU62XE9lcsLm8o1WTEF363 W9F1uzbg5sMYS6zt+oUzp9F3I962qi7RypSwriUMeZHfpDS5TlTJ6ORoV+hBLwadUCEu PWDz18K00E9KlSqsBlMOY4ju/sgxf3aw7rws3kMGn2YQvDsyQ220YYvFSqhF/Idd576V aHDjbfwrkv4YfT0pYwX0Ygi2M6mlgrYrhHALhhf9otOZXBF0ZQgE3zzOsSMGTAimHEpM ZdHeBtBEuYR0Mn2dRyY036a8cOJ1DCBGgKoTSAjWxfouyHiJmXxkeVvOgOVMygRlQN/D hh5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=9OSyc5/6h79up3LZctr0MsZesAr0KVm6SQ4LlSnW5bY=; b=nU1FHvPBQ966/U67vGKNfneTDJNqX8fQucpQ6OuwdFqv96NgbqxAXxoOO8B4eUFgt1 7YYKp8FXwqaWKKapMAwYRsDVh+n7FnFI8v282pN8nJBP7szYFiSrGEX24QQFzta0vigX jLrKljEc2GQIsj+X9NTX7LJqqXpQOV5moh58XuE3yilldVffM0X0lOcVqrElHabsEB5q bLj4Rn2KjsF1hL1mCS4fhvYPHa3odxA6iFZHZ8WQpnnq4iMoxTheTI64cr5QwQ1D+bL1 ME0JtcIBdc32m0zDpWco97nEoWTWu7scMg2QNjwGPmopeYZ9+5WH9rgzaoA3FQYOvxtj yw4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=Yafmn75o; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k5si47568969pgr.69.2018.11.21.19.15.48; Wed, 21 Nov 2018 19:15:48 -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=@nifty.com header.s=dec2015msa header.b=Yafmn75o; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391998AbeKVNxH (ORCPT + 32 others); Thu, 22 Nov 2018 08:53:07 -0500 Received: from conuserg-09.nifty.com ([210.131.2.76]:51398 "EHLO conuserg-09.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732521AbeKVNxH (ORCPT ); Thu, 22 Nov 2018 08:53:07 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-09.nifty.com with ESMTP id wAM3EZ3E004964; Thu, 22 Nov 2018 12:14:35 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-09.nifty.com wAM3EZ3E004964 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1542856476; bh=9OSyc5/6h79up3LZctr0MsZesAr0KVm6SQ4LlSnW5bY=; h=From:To:Cc:Subject:Date:From; b=Yafmn75oaOyr5NJXB53AXKyKEQ82YStVfgyWMUh75FHve14O2bMsA5BjYpzudQMbY zkeRiHteRqa3gn1Wjhu34jbQdOR2LsEjo1KY0ii3hZvpKa9F9MbX24ZDOfT6g93uRr ISZLKNlsMEbC3lCjGenOi875bafdv0twO7Ema2m0VabWiqc3G0E/INYb6737S++9bs dJnf3+9ZRoAuAM55XWUKbGPUYr981WISiRuMgXnl2U3xpbrcsQBBqg68GeRHn7vpdc hjiLt8NR7DW0uKc2ZJJ7PKGIalBNr9YOM9qUlU4FfreESbPi6S7OSdyXKYBrXjNUwK ZV236b42ZavRQ== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Andrew Morton Cc: Luc Van Oostenryck , Nick Desaulniers , Kees Cook , Josh Triplett , Masahiro Yamada , Alexei Starovoitov , linux-kernel@vger.kernel.org, NeilBrown , Greg Kroah-Hartman , Ingo Molnar , Crt Mori , Dan Carpenter Subject: [PATCH v4 1/3] kernel.h: disable type-checks in container_of() for Sparse Date: Thu, 22 Nov 2018 12:14:20 +0900 Message-Id: <1542856462-18836-1-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When I tried to enable BUILD_BUG_ON for Sparse, the kbuild test robot reported lots of "unknown expression" warnings from container_of(), which seemed false positive. I addressed this in [1], but fixing Sparse is the right thing to do. The issue was fixed by Sparse commit 0eb8175d3e9c ("fix expansion of function designator"), but it will take time until the fixed version of Sparse is widely available. Disable the container_of() type checks for Sparse for now. [1] https://lore.kernel.org/lkml/1542623503-3755-1-git-send-email-yamada.masahiro@socionext.com/ Signed-off-by: Masahiro Yamada --- Changes in v4: - New patch Changes in v3: None Changes in v2: None include/linux/kernel.h | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) -- 2.7.4 Reviewed-by: Luc Van Oostenryck diff --git a/include/linux/kernel.h b/include/linux/kernel.h index d6aac75..d8c4adb 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -985,6 +985,21 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } #define __CONCAT(a, b) a ## b #define CONCATENATE(a, b) __CONCAT(a, b) +/* + * TODO: + * Sparse emits "unknown expression" warnings. + * It was fixed by commit 0eb8175d3e9c0d20354763d07ce3d4c0e543d988 in Sparse. + * Remove the following workaround when the fixed Sparse is widely available. + */ +#ifdef __CHECKER__ +#define TYPE_CHECK_CONTAINER_OF(ptr, type, member) do {} while (0) +#else +#define TYPE_CHECK_CONTAINER_OF(ptr, type, member) \ + BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ + !__same_type(*(ptr), void), \ + "pointer type mismatch in container_of()") +#endif + /** * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. @@ -994,9 +1009,7 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } */ #define container_of(ptr, type, member) ({ \ void *__mptr = (void *)(ptr); \ - BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ - !__same_type(*(ptr), void), \ - "pointer type mismatch in container_of()"); \ + TYPE_CHECK_CONTAINER_OF(ptr, type, member); \ ((type *)(__mptr - offsetof(type, member))); }) /** @@ -1009,9 +1022,7 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } */ #define container_of_safe(ptr, type, member) ({ \ void *__mptr = (void *)(ptr); \ - BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ - !__same_type(*(ptr), void), \ - "pointer type mismatch in container_of()"); \ + TYPE_CHECK_CONTAINER_OF(ptr, type, member); \ IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \ ((type *)(__mptr - offsetof(type, member))); }) From patchwork Thu Nov 22 03:14:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 151710 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp206923ljp; Wed, 21 Nov 2018 19:15:40 -0800 (PST) X-Google-Smtp-Source: AFSGD/UkGeyvZh2Pe33boQItBc7zvTd0v1cLVopc2zBf4KONV7WXCEbU3ODPniubp+D4lennllsv X-Received: by 2002:a17:902:59c8:: with SMTP id d8mr9466713plj.116.1542856540418; Wed, 21 Nov 2018 19:15:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542856540; cv=none; d=google.com; s=arc-20160816; b=R3vVLT4CBaZ7U1R+qUGY9t5PhtsylmCotWvNRgf8D34sOQifRPErIJKZRue2NP5MeS qG2Ei3WLdaJiOnEsNh3SVap6Ge7b/y/hwoY4JCsszS69hnmS3WPhVELTGd/z1P0W93Py oCxc1QEbjqPoCU6igbihjqc3e5KHbbmx7BnK4piYnQDcWuy1c91oDsOD2BWw+i6WVOVL DKCRH+4seJHBo5b85fIrBk9Xj7pOijDiGrrWofv+lefwhXIqiyaEGejgiDxau5+QEd9I 47IypK2Qk56xCjv/s9bWNJdMVwStAOb95Ds+pW+hM/LC0SUHY+ikbJhOYvYxm+iBzsbF CpFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=EC6OrDl+Pe6xGCqyh6yaEOjJ/Vdp3qzeTiZNHyrNG5o=; b=wbdW98i+SiiaPRglB/LysITc1HU2M4IP6e3fTCvgO4PLCWFtcHSmQvgTSoIfroDRH5 jm34vaNLbUfL9jl8KNIz6n3EVFISPTJv069BxgBWaaXhjTyUH8VtkJCErzAngtah2szb yPcG0y7K66fegnVinSeAhwoE8CjfJDnV5wTVxtumcJcI9tfREbsj1gjIsW5bRRw8HXpD qyXJfaRFRsJ2AazoPDgA745SGkvgZDGeCl99Yw53pYql+gjKdD0s4P0Po3d95u5nzUhd SsbdzKgu7qa7UlBrVBS3yJPZznxTsT5NrlgY4+x17TO+jcdMzHhue+K1nTWRYIKf2IAa MgdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=hjyuNNvk; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w21-v6si48556745pll.283.2018.11.21.19.15.40; Wed, 21 Nov 2018 19:15:40 -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=@nifty.com header.s=dec2015msa header.b=hjyuNNvk; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391982AbeKVNw7 (ORCPT + 32 others); Thu, 22 Nov 2018 08:52:59 -0500 Received: from conuserg-09.nifty.com ([210.131.2.76]:51145 "EHLO conuserg-09.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732521AbeKVNw6 (ORCPT ); Thu, 22 Nov 2018 08:52:58 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-09.nifty.com with ESMTP id wAM3EZ3F004964; Thu, 22 Nov 2018 12:14:36 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-09.nifty.com wAM3EZ3F004964 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1542856476; bh=EC6OrDl+Pe6xGCqyh6yaEOjJ/Vdp3qzeTiZNHyrNG5o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hjyuNNvk1c0hopbgMPdWHghO38rHW7Rz523ZlAlLjuR/jZmbg6xv4y+/rflnb9sBg 6EDX192L0HhoEauJNDUcu0HY4X1StRdrXVGIRdAxerPOq5wIG17vsHbOuigF5eSbAi o58qT/ZTRPQpS6dNngbRbYl+ZWg1kjqbVZsWroUPWRL5RXRinTVQaGp8JYesvWJL9v s+8LZpxlVughcuzhgj5Idqpxj8zbbHJ649AZb/+LbG9/cOVISpDnlafXPVmeccpXUA pqDBojJ/khiBqbo0COGx6FPXoa1/bJ9mPvIfZ9J936PmSR7mUQui0u+4BupNKBceoy s6cDpCfY/vHbg== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Andrew Morton Cc: Luc Van Oostenryck , Nick Desaulniers , Kees Cook , Josh Triplett , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH v4 2/3] build_bug.h: remove negative-array fallback for BUILD_BUG_ON() Date: Thu, 22 Nov 2018 12:14:21 +0900 Message-Id: <1542856462-18836-2-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1542856462-18836-1-git-send-email-yamada.masahiro@socionext.com> References: <1542856462-18836-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The kernel can only be compiled with an optimization option (-O2, -Os, or the currently proposed -Og). Hence, __OPTIMIZE__ is always defined in the kernel source. The fallback for the -O0 case is just hypothetical and pointless. Moreover, commit 0bb95f80a38f ("Makefile: Globally enable VLA warning") enabled -Wvla warning. The use of variable length arrays is banned. Signed-off-by: Masahiro Yamada Acked-by: Kees Cook Reviewed-by: Nick Desaulniers Tested-by: Nick Desaulniers --- Changes in v4: None Changes in v3: None Changes in v2: None include/linux/build_bug.h | 14 -------------- 1 file changed, 14 deletions(-) -- 2.7.4 Acked-by: Miguel Ojeda diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h index 43d1fd5..d415c64 100644 --- a/include/linux/build_bug.h +++ b/include/linux/build_bug.h @@ -51,23 +51,9 @@ * If you have some code which relies on certain constants being equal, or * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to * detect if someone changes it. - * - * The implementation uses gcc's reluctance to create a negative array, but gcc - * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to - * inline functions). Luckily, in 4.3 they added the "error" function - * attribute just for this type of case. Thus, we use a negative sized array - * (should always create an error on gcc versions older than 4.4) and then call - * an undefined function with the error attribute (should always create an - * error on gcc 4.3 and later). If for some reason, neither creates a - * compile-time error, we'll still have a link-time error, which is harder to - * track down. */ -#ifndef __OPTIMIZE__ -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) -#else #define BUILD_BUG_ON(condition) \ BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) -#endif /** * BUILD_BUG - break compile if used. From patchwork Thu Nov 22 03:14:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 151709 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp206904ljp; Wed, 21 Nov 2018 19:15:39 -0800 (PST) X-Google-Smtp-Source: AFSGD/UAKFjLG6Yp5FKua0F/cCkUGa+iMhXnQtrd0/5KXvPhSIZP9oadkMJ74TDT9h24/nARrOU4 X-Received: by 2002:a17:902:142:: with SMTP id 60mr9743970plb.330.1542856539023; Wed, 21 Nov 2018 19:15:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542856539; cv=none; d=google.com; s=arc-20160816; b=EfCm6Zs4bXPN2DLE2tKqCL2YgUzKoVRWB5S/paWVswo1QwD/UtEZNRSWYZqudI2Oiv 3AI7EFHsYgklnr/gKhTXmK/XsoP8xm6tHP0kglQuvF95a9UoK1PmfThjH1wwzOup+RWX BMWRVJcmasLguc2QsEpzw7Z00HndeW9YhSY4/7r03zVwtiiNBi8dTzBrhqcYlspko1Y4 pMilM4nga2H2Jb/mfYWonj/2OUNLRs5T/aFcITT8Hrz2e1D2ERuF0XGHPcJ+HMGUoW5x jAA/YCXQFyR9kSrEfGIfwroWfTZz9Hyxr4K+mqckag8fuMdunkgiTOqvHne5//Vad1wi UO+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=PhO+PXx9f29vDsYbmMZinUspy8MjVFdMSRysGapnTRY=; b=iAXUHdIqF12AUvBq+Xfk15/ciiEFZNJy9z9XV+BzHeNEBjiugsA9yRNgAeiVkx6L8v aOCQnNDmiHnJLMSje+/vG66e65vFAOxhJOA7F0xUPWmlHmcdw/wEBmbNVinKo3UGgWJ5 LgGABlQdSIIvX0ULXYELyYIftnzGM0+PhxjnjSyGnhzI7ozmDgCAwkwb8Kd/4cgN4NnN Nm6MD2Ug/4d1XbF6w9wi7Q1XRlT31KmVie3CXQzjeDnjiVyHbrnhE/50THrTMqyXA+9D GO7svCPeWzbrXk2wmdmySWazKMYkgLu2c6ZIKChB/Olp9KbAy/VLAuggj2cWE/HD83AL Vemg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b="1b/iYELC"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w21-v6si48556745pll.283.2018.11.21.19.15.38; Wed, 21 Nov 2018 19:15:39 -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=@nifty.com header.s=dec2015msa header.b="1b/iYELC"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391970AbeKVNw6 (ORCPT + 32 others); Thu, 22 Nov 2018 08:52:58 -0500 Received: from conuserg-09.nifty.com ([210.131.2.76]:51151 "EHLO conuserg-09.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388435AbeKVNw5 (ORCPT ); Thu, 22 Nov 2018 08:52:57 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-09.nifty.com with ESMTP id wAM3EZ3G004964; Thu, 22 Nov 2018 12:14:37 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-09.nifty.com wAM3EZ3G004964 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1542856477; bh=PhO+PXx9f29vDsYbmMZinUspy8MjVFdMSRysGapnTRY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1b/iYELC4BWncKnU+Ljph1/TTJtoa1gAbiaSl5jSf7oZV1H5gs/iYS9dIcKExeI8X BoflL7F1YU49SqvGSfuhkhWfkVaqJsaQJJpB3lKyAbMc2krIu9Qmy2Y2jXKwLm/B7Q AmxlY65F0kMPbdxF74Moer+hvT1QUPln9XIy6ySDdrcK2tm9xXzQpeGexAQv9jvygJ AILw+BdUZylvLZYSS/2biUK9ThbuUH0wdPtJ3uApHwynhgmTTJIvidfiPPtJFNV33D O7I2zBdJndX4at8Eq8wsnjZ76KkcHiYfX7NKHvH10Wz1RSQvmtRy1Jci/cI644+yZJ OlNbb76M63JtQ== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Andrew Morton Cc: Luc Van Oostenryck , Nick Desaulniers , Kees Cook , Josh Triplett , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH v4 3/3] build_bug.h: remove most of dummy BUILD_BUG_ON stubs for Sparse Date: Thu, 22 Nov 2018 12:14:22 +0900 Message-Id: <1542856462-18836-3-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1542856462-18836-1-git-send-email-yamada.masahiro@socionext.com> References: <1542856462-18836-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The introduction of these dummy BUILD_BUG_ON stubs dates back to commit 903c0c7cdc21 ("sparse: define dummy BUILD_BUG_ON definition for sparse"). At that time, BUILD_BUG_ON() was implemented with the negative array trick *and* the link-time trick, like this: extern int __build_bug_on_failed; #define BUILD_BUG_ON(condition) \ do { \ ((void)sizeof(char[1 - 2*!!(condition)])); \ if (condition) __build_bug_on_failed = 1; \ } while(0) Sparse is more strict about the negative array trick than GCC because Sparse requires the array length to be really constant. Here is the simple test code for the macro above: static const int x = 0; BUILD_BUG_ON(x); GCC is absolutely fine with it (-Wvla was enabled only very recently), but Sparse warns like this: error: bad constant expression error: cannot size expression (If you are using a newer version of Sparse, you will see a different warning message, "warning: Variable length array is used".) Anyway, Sparse was producing many false positives, and noisier than it should be at that time. With the previous commit, the leftover negative array trick is gone. Sparse is fine with the current BUILD_BUG_ON(), which is implemented by using the 'error' attribute. I am keeping the stub for BUILD_BUG_ON_ZERO(). Otherwise, Sparse would complain about the following code, which GCC is fine with: static const int x = 0; int y = BUILD_BUG_ON_ZERO(x); Signed-off-by: Masahiro Yamada Acked-by: Kees Cook Reviewed-by: Luc Van Oostenryck Reviewed-by: Nick Desaulniers Tested-by: Nick Desaulniers --- Changes in v4: None Changes in v3: - Add Kees' Acked-by - Clarify log that BUILD_BUG_ON() *was* producing false positives - Keep the stub for BUILD_BUG_ON_ZERO() Changes in v2: - Fix a coding style error (two consecutive blank lines) include/linux/build_bug.h | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) -- 2.7.4 Acked-by: Miguel Ojeda diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h index d415c64..faeec74 100644 --- a/include/linux/build_bug.h +++ b/include/linux/build_bug.h @@ -5,21 +5,8 @@ #include #ifdef __CHECKER__ -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) #define BUILD_BUG_ON_ZERO(e) (0) -#define BUILD_BUG_ON_INVALID(e) (0) -#define BUILD_BUG_ON_MSG(cond, msg) (0) -#define BUILD_BUG_ON(condition) (0) -#define BUILD_BUG() (0) #else /* __CHECKER__ */ - -/* Force a compilation error if a constant expression is not a power of 2 */ -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ - BUILD_BUG_ON(((n) & ((n) - 1)) != 0) -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ - BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) - /* * Force a compilation error if condition is true, but also produce a * result (of value 0 and type size_t), so the expression can be used @@ -27,6 +14,13 @@ * aren't permitted). */ #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) +#endif /* __CHECKER__ */ + +/* Force a compilation error if a constant expression is not a power of 2 */ +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ + BUILD_BUG_ON(((n) & ((n) - 1)) != 0) +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ + BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) /* * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the @@ -64,6 +58,4 @@ */ #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") -#endif /* __CHECKER__ */ - #endif /* _LINUX_BUILD_BUG_H */