From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7614CC433FE for ; Wed, 9 Dec 2020 11:28:52 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4135423AA8 for ; Wed, 9 Dec 2020 11:28:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4135423AA8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=crapouillou.net Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:To: Subject:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=KIoo0tpBt0h/1SSTmn1I829P+r9G1Dud2VSIUOWjU8w=; b=r6sAZ44G+AKPTZ06/xkD2KnNd EP3OuTA7IlEf82d9uOkq2efsRVW0FLLRhItB+nO+u7ZCupv+hn+n0+jh27idH519o2L7JeDcE1RxL nXYLrZBu/b58yUxCvuIv3F9HSbP7ZvRvbbxTIEaYfHvZBGzbl7lj+cOxTuI/vH+almj/m62tAxwiK /voHJLfNXJ37tD54FHQwsNgpNZykDQUEgmli2KxC2Jjj1indq0SHAc/16+8FKdw+Yhcw/w1yvIyw9 09tRL+fiP+jhQgJz4MCtoNEGcv2Hlkwi7Zylf1f9JVYAX3OAYGbS3f7KHdjfOc4Kn3tdrrmInf3vD lAlx5T4SQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kmxd4-0007TF-S7; Wed, 09 Dec 2020 11:27:30 +0000 Received: from aposti.net ([89.234.176.197]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kmxd1-0007Ss-2c for linux-arm-kernel@lists.infradead.org; Wed, 09 Dec 2020 11:27:28 +0000 Date: Wed, 09 Dec 2020 11:27:07 +0000 From: Paul Cercueil Subject: Re: [PATCH 1/2] if_enabled.h: Add IF_ENABLED_OR_ELSE() =?UTF-8?Q?and=0D=0A?= IF_ENABLED() macros To: Ard Biesheuvel Message-Id: <75L2LQ.XOYPRHI9W3U9@crapouillou.net> In-Reply-To: References: <20201208164821.2686082-1-paul@crapouillou.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201209_062727_255354_26393207 X-CRM114-Status: GOOD ( 23.90 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Arnd Bergmann , Linus Walleij , MIPS , Linux Kernel Mailing List , od@zcrc.me, Linux ARM Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Ard, Le mer. 9 d=E9c. 2020 =E0 10:38, Ard Biesheuvel a = =E9crit : > On Tue, 8 Dec 2020 at 17:49, Paul Cercueil = > wrote: >> = >> Introduce a new header , that brings two new = >> macros: >> IF_ENABLED_OR_ELSE() and IF_ENABLED(). >> = >> IF_ENABLED_OR_ELSE(CONFIG_FOO, a, b) evaluates to (a) if CONFIG_FOO = >> is set >> to 'y' or 'm', (b) otherwise. It is used internally to define the >> IF_ENABLED() macro. The (a) and (b) arguments must be of the same = >> type. >> = >> IF_ENABLED(CONFIG_FOO, ptr) evaluates to (ptr) if CONFIG_FOO is set = >> to 'y' >> or 'm', NULL otherwise. The (ptr) argument must be a pointer. >> = >> The IF_ENABLED() macro can be very useful to help GCC drop dead = >> code. >> = >> For instance, consider the following: >> = >> #ifdef CONFIG_FOO_SUSPEND >> static int foo_suspend(struct device *dev) >> { >> ... >> } >> #endif >> = >> static struct pm_ops foo_ops =3D { >> #ifdef CONFIG_FOO_SUSPEND >> .suspend =3D foo_suspend, >> #endif >> }; >> = >> While this works, the foo_suspend() macro is compiled conditionally, >> only when CONFIG_FOO_SUSPEND is set. This is problematic, as there = >> could >> be a build bug in this function, we wouldn't have a way to know = >> unless >> the config option is set. >> = >> An alternative is to declare foo_suspend() always, but mark it as = >> maybe >> unused: >> = >> static int __maybe_unused foo_suspend(struct device *dev) >> { >> ... >> } >> = >> static struct pm_ops foo_ops =3D { >> #ifdef CONFIG_FOO_SUSPEND >> .suspend =3D foo_suspend, >> #endif >> }; >> = >> Again, this works, but the __maybe_unused attribute is required to >> instruct the compiler that the function may not be referenced = >> anywhere, >> and is safe to remove without making a fuss about it. This makes the >> programmer responsible for tagging the functions that can be >> garbage-collected. >> = >> With this patch, it is now possible to write the following: >> = >> static int foo_suspend(struct device *dev) >> { >> ... >> } >> = >> static struct pm_ops foo_ops =3D { >> .suspend =3D IF_ENABLED(CONFIG_FOO_SUSPEND, foo_suspend), >> }; >> = >> The foo_suspend() function will now be automatically dropped by the >> compiler, and it does not require any specific attribute. >> = >> Signed-off-by: Paul Cercueil > = > Hi Paul, > = > I understand the issue you are trying to address here, but please note > that there are many cases where the struct member in question will not > even be declared if the associated CONFIG option is not set, in which > case this will cause a compile error. Of course. This is for the case where the field is always present. For = instance, (struct device_driver *)->pm is always present, independently = of CONFIG_PM. Cheers, -Paul > = >> --- >> include/linux/if_enabled.h | 22 ++++++++++++++++++++++ >> 1 file changed, 22 insertions(+) >> create mode 100644 include/linux/if_enabled.h >> = >> diff --git a/include/linux/if_enabled.h b/include/linux/if_enabled.h >> new file mode 100644 >> index 000000000000..8189d1402340 >> --- /dev/null >> +++ b/include/linux/if_enabled.h >> @@ -0,0 +1,22 @@ >> +/* SPDX-License-Identifier: GPL-2.0 */ >> +#ifndef __LINUX_IF_ENABLED_H >> +#define __LINUX_IF_ENABLED_H >> + >> +#include >> +#include >> +#include >> + >> +/* >> + * IF_ENABLED_OR_ELSE(CONFIG_FOO, a, b) evaluates to (a) if = >> CONFIG_FOO is set >> + * to 'y' or 'm', (b) otherwise. >> + */ >> +#define IF_ENABLED_OR_ELSE(option, a, b) \ >> + (BUILD_BUG_ON_ZERO(__same_type((a), (b))) || = >> IS_ENABLED(option) ? (a) : (b)) >> + >> +/* >> + * IF_ENABLED(CONFIG_FOO, ptr) evaluates to (ptr) if CONFIG_FOO is = >> set to 'y' >> + * or 'm', NULL otherwise. >> + */ >> +#define IF_ENABLED(option, ptr) IF_ENABLED_OR_ELSE(option, ptr, = >> NULL) >> + >> +#endif /* __LINUX_IF_ENABLED_H */ >> -- >> 2.29.2 >> = >> = >> _______________________________________________ >> linux-arm-kernel mailing list >> linux-arm-kernel@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel