Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4924928pxb; Thu, 14 Oct 2021 15:01:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbFnoRwI9h8sDI+2vMpJCUQ0ZmpJG1jpDAoexCxY4F9GxiTcjyy74hZ6G2PCTHDpZ6Vqee X-Received: by 2002:a17:906:c7d0:: with SMTP id dc16mr2004243ejb.555.1634248905478; Thu, 14 Oct 2021 15:01:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634248905; cv=none; d=google.com; s=arc-20160816; b=wuVAeZvYFuzJ9wEgYuF0tQCyLB4KSlRkLSglOIXQjon/JY2wpkbAC9pkccfxuqWaGM mqn2bxIIkPn+5zHICLa4M0KPWduj3XSFMnoZrU1P5pkgPDEkLbZCqCUBPdua60iAdEcJ ZWubKnX/yDlw3bSipdAqETKbjxi8omIL36rFXJq78eg8DTwUFRj7rh3JLYPteMMoQ8Gv KBefRrHi2w+HsjnoyW3obNugOwSQR6FQyNl1qV64tyBDpwibXAyaqHAI/+HrWT4SGYUV dGDNH9uevIO9s48GGsfOiESkL7N2w7tBjDlQFN1h/DyxbxMBMRQaKewFqYd++1Y+epQG iwNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=SaW0/T+S3qtTPwWA2uNkFcxxhKazoJ7rNJxWqIE27NI=; b=UOj56OgBGUv400aNjc5n7NzkT8MD7c4yde9s5IitLRCyw3fsZ6NKAGqoq0jakdxjJm iEYGVbFLy0/7WBkiMy8K7yQUITingMykbxpuAiAqYam1KyiqwZriMVsDBKtBksB80jP6 DMWcpeGLzeAbH2Jj/ryNPwkfRLzBZlykV91CAQ0qy1mOdiPqjpVrbyLSygi6/tWr6Vc9 1iEOEFfUG0orTCsiXtS5Hi2FBu2zaJU2+bJ111NcBINhcA5iGWxFYvU+ObnERbq0/Lhd KtW+dI6UOk7MlE8RDxmXd0+yL6qhOj/S3kyqM2nGhdqh0Offj6QccSI2rGdZ649KqN2k Dd8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=V6ZjrnV+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id v18si5884791ejh.182.2021.10.14.15.01.12; Thu, 14 Oct 2021 15:01:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=V6ZjrnV+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233648AbhJNRvK (ORCPT + 99 others); Thu, 14 Oct 2021 13:51:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231434AbhJNRvH (ORCPT ); Thu, 14 Oct 2021 13:51:07 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40275C061570 for ; Thu, 14 Oct 2021 10:49:02 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id g36so13628986lfv.3 for ; Thu, 14 Oct 2021 10:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SaW0/T+S3qtTPwWA2uNkFcxxhKazoJ7rNJxWqIE27NI=; b=V6ZjrnV+Fq7DNZ0MCcyJ/VsIfotirCJtUXn4CiavP5HB+HnKnk7fdRVDXKOSb1N40y j6M4ylzc2RO0XwRjNoAaQRGDUpEHjNX0hQAKKrcMAVUCdybAqMc84UyFXvge0HnpDE4X mUlcEoqYjfEq0s2OFwr3Wt7hut6aVbPtrj2QguEU59goAN6Soy60/QSLwbdeULFMU7gq yljbnk6C7Pz8EcZCnmd/X2VKKLzCu2BdJqOsiRMKOv6VePti3Zmmnn7ccJ7VJ9xGbQ6l nFaeRqQQki8f4NTlfqbbMW6zDsTCJABYDkaX5eLshUxlvEnypd4b3sjSTwXohsZe2ewI RKRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SaW0/T+S3qtTPwWA2uNkFcxxhKazoJ7rNJxWqIE27NI=; b=C8MduHj9k3NdJFyEcd7s10L68Uh6im4cn1VUOmloIxG4dW1wS+BRX0+RHstdHSar6p ZxKCqLrtN6tbKBurfs+46+JiXBFmBYB/aH/eGQAKnRovPYuOTbOLVwakIeTh9Wk3Eg7k V2EtWifv4kuh0kHy+j9u4ntoreoFTdyD1jjaQ+JCvNqtDTXCNM2HGkAWdVnPtwTFzAFk EH0p3wCCcKbK8RycEdpDetnFwVdPzglB184kpgVGHewmQJgHbGBIF3q0Xx7dodyqdge4 I7T4aqvx6KOTzeD0dKfq4eA9hniu5w/wJHT+79ddWPWVuDuLg9nOxwZ4MdGGq1lnGpQD GRIg== X-Gm-Message-State: AOAM530z/HKZExaeTThbCciDn6dxokH+0z5RgG8ygKEMfbANjFE8AueE Xudn6ZMzl9qzuhsfVdkP707zf3NNSQ0Clt6XyKXN0w== X-Received: by 2002:a05:6512:b0c:: with SMTP id w12mr6584550lfu.240.1634233740333; Thu, 14 Oct 2021 10:49:00 -0700 (PDT) MIME-Version: 1.0 References: <20211014132331.GA4811@kernel.org> In-Reply-To: From: Nick Desaulniers Date: Thu, 14 Oct 2021 10:48:48 -0700 Message-ID: Subject: Re: [PATCH] compiler_types: mark __compiletime_assert failure as __noreturn To: Peter Zijlstra Cc: Miguel Ojeda , Nathan Chancellor , Kees Cook , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, Rasmus Villemoes , Linus Torvalds Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 14, 2021 at 8:02 AM Peter Zijlstra wrote: > > On Thu, Oct 14, 2021 at 03:23:31PM +0200, Miguel Ojeda wrote: > > `__compiletime_assert` declares a fake `extern` function > > which appears (to the compiler) to be called when the test fails. > > > > Therefore, compilers may emit possibly-uninitialized warnings > > in some cases, even if it will be an error anyway (for compilers > > supporting the `error` attribute, e.g. GCC and Clang >= 14) > > or a link failure (for those that do not, e.g. Clang < 14). > > > > Annotating the fake function as `__noreturn` gives them > > the information they need to avoid the warning, > > e.g. see https://godbolt.org/z/x1v69jjYY. > > > > Link: https://lore.kernel.org/llvm/202110100514.3h9CI4s0-lkp@intel.com/ > > Reported-by: kernel test robot > > Signed-off-by: Miguel Ojeda > > --- > > include/linux/compiler_types.h | 8 +++++++- > > 1 file changed, 7 insertions(+), 1 deletion(-) > > > > diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h > > index b6ff83a714ca..ca1a66b8cd2f 100644 > > --- a/include/linux/compiler_types.h > > +++ b/include/linux/compiler_types.h > > @@ -298,7 +298,13 @@ struct ftrace_likely_data { > > #ifdef __OPTIMIZE__ > > # define __compiletime_assert(condition, msg, prefix, suffix) \ > > do { \ > > - extern void prefix ## suffix(void) __compiletime_error(msg); \ > > + /* \ > > + * __noreturn is needed to give the compiler enough \ > > + * information to avoid certain possibly-uninitialized \ > > + * warnings (regardless of the build failing). \ > > + */ \ > > + __noreturn extern void prefix ## suffix(void) \ > > + __compiletime_error(msg); \ > > if (!(condition)) \ > > prefix ## suffix(); \ > > } while (0) > > Should we not convert this to _Static_assert, now that all supported > compilers are of recent enough vintage to support that? It's a good question; I'm pretty sure we had a thread with Rasmus on the idea a while ago, and IIRC the answer is no. Basically, we can't convert BUILD_BUG_ON to _Static_assert because _Static_assert requires integer constant expressions (ICE) while many expressions passed to BUILD_BUG_ON in the kernel require that optimizations such as inlining run (they are not ICEs); BUILD_BUG_ON is more flexible. So you can't replace the guts of BUILD_BUG_ON wholesale with _Static_assert (without doing anything else); it would be preferable for kernel developers to use _Static_assert (I think we have a macro, static_assert, too) in cases where they have ICEs rather than BUILD_BUG_ON (though it flips the condition of the expression; _Static_assert errors if the expression evaluates to false; BUILD_BUG_ON when true), but I think there's too much muscle memory around just using BUILD_BUG_ON that if you introduced something new, folks wouldn't know to use that instead. Probably a better demonstration would be to try it and observe some of the spooky failures at build time that result. We may be able to separate the macro into two; BUILD_BUG_ON and BUILD_BUG_ON_OPT (or whatever color bikeshed), where the former uses _Static_assert under the hood, and the latter uses __attribute__((error)). Then we could go about converting cases that could not use _Static_assert to use the new macro, while the old macro is what folks still reach for first. I'm not sure how worthwhile that yakshave would be, but at least the front end of the compiler would error sooner in the case of _Static_assert, FWIW (not much). But I don't think we can ever eliminate __attribute__((error)) from the kernel unless we're ok outright removing asserts that aren't ICEs. I would not recommend that. I would like to see more usage of static_assert, but I'm not sure how best to promote that, and if it's worth discussing the subtle distinction between BUILD_BUG_ON vs _Static_assert again and again and again every time. -- Thanks, ~Nick Desaulniers