Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp751511pxb; Wed, 25 Aug 2021 14:15:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKLL6aABrxqLhOlMvOCA9a2pTk1HjffztHj2YPIHzu6GHEl3LHon3KlekjI/34BsQKv2pg X-Received: by 2002:a92:d7c1:: with SMTP id g1mr267589ilq.24.1629926157964; Wed, 25 Aug 2021 14:15:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629926157; cv=none; d=google.com; s=arc-20160816; b=I5508UJXmVAeK8ibJ10WxqKCMoOt9LQB7AcRJlNip3wWChk2nvS4LQ2MUJAVpeTzar DhGwwCmBrFAxc3Dw2zlE3mdeWBouLnXXqefo9i6tm0SwLpnWcj1gXFpjbdbkFkzcUaTT ZRA2P2Yd8djHoxxajF1SXtnsy/zcgV1pDO80uQNreFYKGdTrAlKrga/7UF3v2ovf2Q/S eH/rA+O89h/mHDZDacOeHU6n2MoJJU+rlk4qLOMfr6ptTgQXc81qzll9rIYT/FTB/ElT sOgKCwz9uTnpoB/NXM3wntZcq1EG4bs4xHwz054aRzpzp0XnbTsKbKjLfGehMQ2yIMLw nwbg== 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=E9tATzQmNnf1n87t/HdQMMPUKAwA0uk3NMuEvkkauNQ=; b=K4AcrxyJWEUXudxBz80v5u0JXujfneX+2FuASzzx6+A9xl5mUk8kU6s90NE9/uq0Tv DFArAqSIOli5aVHLDWS3eWBOgCwrcCUkJLk3t7b6de0sGTEl+IRLNDebyGvPECqZWXuU MFujuVh9pzlwUb3ZS4DW1ui9jJRuqOpF27BuPJJcASOQjP6EBOHBV0LCjnvjS4EzkacZ oiUOB34+Ufie03le9EDbsi/uyM81Z4suMUSzkWOiVlc6N+XMzInIu+IhOxaeCcDH1bwX DRrJAvXRxydJOsoce935e4JeNLtJW6ykOfvTm2zxrav4wl4A6sGg4vQeHAfwivYyQ1IK PNcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=rDk6QdPm; 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 q74si818534iod.9.2021.08.25.14.15.47; Wed, 25 Aug 2021 14:15:57 -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=20161025 header.b=rDk6QdPm; 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 S232017AbhHYVKv (ORCPT + 99 others); Wed, 25 Aug 2021 17:10:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229560AbhHYVKu (ORCPT ); Wed, 25 Aug 2021 17:10:50 -0400 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E5C7C061757 for ; Wed, 25 Aug 2021 14:10:04 -0700 (PDT) Received: by mail-lf1-x12f.google.com with SMTP id p38so1889080lfa.0 for ; Wed, 25 Aug 2021 14:10:04 -0700 (PDT) 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=E9tATzQmNnf1n87t/HdQMMPUKAwA0uk3NMuEvkkauNQ=; b=rDk6QdPmNKIfsBWw+RCk/zTo1/Iz/KktStVviVo1+3U2kouL2fe+6wd/+A6pEaCMzh xzM7QapYS3avKaOtSvCRpfQi1VypfGMygawhsTaUr+awCsB60kx+mrvhp8FXcMEcdXqO k3IksxDiST5MMM06Rug+w9s0ESAs5BEt6zxwHbiXGdm6tVzS06DDI3L+Lt856rQlitrh fVxEvhZ64781J7thqfN5yP8emsMeF7kIh9DgpVHuJN04jnJVHa/d/zG2uAegWku2U2Q2 Xc1QFKy0jWfRFrx/OwfmhlXYphVlf3P8Fh9tkf4nRNAo32xs40V655CNr61QKh0PP7Fd npvw== 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=E9tATzQmNnf1n87t/HdQMMPUKAwA0uk3NMuEvkkauNQ=; b=bJiJ70PModG0MwsCQ30vizJLo1rwNgNbnRIhr2OnZ8rNzxL0MGeuMZHZiIlP+D7Raz RfLn4OPM55/lSAg2OyQ9bWbhfP0THLDb7X1ZH6K99i79ti+0UdqagpJVDp9jvvlnKwG5 h90CyJvpDZnESu6cWKHmQqk55czhPGVxEOxoCZl08s+qEFp/aTkxQl+w2YLAw4m5qPL1 GTCU+kdzZuxhZCV8ZZsNBOH31tkXcty8n8RbDFczeZCnW7W5OOr+QrNI0Y5i2ef4qM13 dPZQatei+OB5Ok6SLcDuFuZc0UK0Q8WTYSYoBe+Ap7FswGzeImd7XWAe+bzItLVdXtWi T58g== X-Gm-Message-State: AOAM5332H61zm61tCgs4c1fuz8WwulQbXz1vk9vCpXay5QNjXH3Y1ivF 8J2Maf+UpSL+83j18ZWoRp+n/heGEpE8uJImNEsZew== X-Received: by 2002:a05:6512:3b85:: with SMTP id g5mr87035lfv.368.1629925802350; Wed, 25 Aug 2021 14:10:02 -0700 (PDT) MIME-Version: 1.0 References: <20210817005624.1455428-1-nathan@kernel.org> <80fa539a-b767-76ed-dafa-4d8d1a6b063e@kernel.org> <5c856f36-69a7-e274-f72a-c3aef195adeb@kernel.org> <202108171056.EDCE562@keescook> <3f28b45e-e725-8b75-042a-d34d90c56361@kernel.org> In-Reply-To: From: Nick Desaulniers Date: Wed, 25 Aug 2021 14:09:50 -0700 Message-ID: Subject: Re: [PATCH] kbuild: Enable -Wimplicit-fallthrough for clang 14.0.0+ To: Masahiro Yamada , "Gustavo A. R. Silva" , Nathan Chancellor Cc: Kees Cook , Linus Torvalds , Linux Kbuild mailing list , Linux Kernel Mailing List , clang-built-linux , linux-hardening@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 17, 2021 at 2:18 PM Masahiro Yamada wrote: > > On Wed, Aug 18, 2021 at 3:25 AM Nathan Chancellor wrote: > > > > On 8/17/2021 11:03 AM, Kees Cook wrote: > > > On Mon, Aug 16, 2021 at 09:55:28PM -0700, Nathan Chancellor wrote: > > >> If you/Gustavo would prefer, I can upgrade that check to > > >> > > >> ifneq ($(call cc-option, -Wunreachable-code-fallthrough),) > > >> > > >> I was just trying to save a call to the compiler, as that is more expensive > > >> than a shell test call. > > > > > > I prefer the option test -- this means no changes are needed on the > > > kernel build side if it ever finds itself backported to earlier versions > > > (and it handles the current case of "14" not meaning "absolute latest"). > > > > > > More specifically, I think you want this (untested): > > > > That should work but since -Wunreachable-code-fallthrough is off by > > default, I did not really see a reason to include it in KBUILD_CFLAGS. I > > do not have a strong opinion though, your version is smaller than mine > > is so we can just go with that. I'll defer to Gustavo on it since he has > > put in all of the work cleaning up the warnings. > > > > https://github.com/llvm/llvm-project/commit/9ed4a94d6451046a51ef393cd62f00710820a7e8 > > did two things: > > (1) Change the -Wimplicit-fallthrough behavior so that it fits > to our use in the kernel > > (2) Add a new option -Wunreachable-code-fallthrough > that works like the previous -Wimplicit-fallthrough of > Clang <= 13.0.0 > > > They are separate things. > > Checking the presence of -Wunreachable-code-fallthrough > does not make sense since we are only interested in (1) here. > > > > So, checking the Clang version is sensible and matches > the explanation in the comment block. > > > Moreover, using $(shell test ...) is less expensive than cc-option. > > > If you want to make it even faster, you can use only > built-in functions, like this: > > > # Warn about unmarked fall-throughs in switch statement. > # Clang prior to 14.0.0 warned on unreachable fallthroughs with > # -Wimplicit-fallthrough, which is unacceptable due to IS_ENABLED(). > # https://bugs.llvm.org/show_bug.cgi?id=51094 > ifeq ($(firstword $(sort $(CONFIG_CLANG_VERSION) 140000)),140000) > KBUILD_CFLAGS += -Wimplicit-fallthrough > endif > > > > The $(sort ...) is alphabetical sort, not numeric sort. > It works for us because the minimum Clang version is 10.0.1 > (that is CONFIG_CLANG_VERSION is always 6-digit) > > It will break when Clang version 100.0.0 is released. > > But, before that, we will raise the minimum supported clang version, > and this conditional will go away. I'd much rather pay the cost of cc-option to have a more precise check; Linus is right: when I upgrade AOSP's fork of LLVM, it may not be the fully released version of clang-14 though we have already moved the version numbers upstream to clang-14. I think we should strive to prefer feature tests over version tests, which are brittle. ``` # Clang would warn about unreachable fall throughs until clang-14. ifdef CONFIG_CC_IS_CLANG ifneq ($(call cc-option,-Wunreachable-code-fallthrough),) KBUILD_CFLAGS += -Wimplicit-fallthrough endif endif ``` Is precisely what we want. -- Thanks, ~Nick Desaulniers