Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp3898342pxu; Wed, 9 Dec 2020 03:32:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJytwk3MxHoxujBkc+fyMgPpogDGxC16Oc6VyJS91b+yIYkFEOYRJYnJJ2jXh2R7AT8oekKc X-Received: by 2002:a05:6402:1748:: with SMTP id v8mr1612335edx.136.1607513523676; Wed, 09 Dec 2020 03:32:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607513523; cv=none; d=google.com; s=arc-20160816; b=y590uI4CUZW/O1JxdOIwwjYwTmj403hHF1EAp4ftHlM4edmf85Od9/8ifqB+uF015t Kzopcm9v/YxJ4OPeseTwiB+4ZUrMej9nt+FUuTYajxtxg2wjJ+OFtadm13VQvpcOqyo+ QVUoOWL46M+Cq3IiGWTypnWCSCR4t/FpEiVoQRqEFV3PL9Qn6s6ZCzoAYht7aMaXyCI0 ysP5e7h06OL1W5HpF5OmaeFSlcdazR4G4nhCBoRadx0pYSnUxsGKlzGpbYJCQ5zNTFfv +4gslXRaw3m3Mh2SI1rJW0/hRG83TqZF/C8NrWiNl5gFsLo7Nkc7zPerRfPm1om1LxRX 2Kdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:cc:to:subject:from:date; bh=HgVdBcRlClHwJ5N6cKvOMa+gs3afjqaXfq8nshkI6qA=; b=wuAhxIaCK3O+mOQT1N7Cp9GqElLlwyxAUNw1xCEkJ8OZWQ3/o7rPJwi+kW5NkEBN45 7+/vNoH6pdwdzOjRoxTRFttIQ/m7MQC5nwpBbEzpqPAsJxub8Sb2/T2tvDBMCszl+HYH w/zg61415Tt5JUrQT9fhr/txcxlQeZGEd0j55L7bc6qBs8Tyci6bDyIL2tnJbOKpgRgB /GKyMqASU0XZFgPXhkHTMMyCLTFdTZlN8zq4ZTbbC67uwGgy/gSoMRm//lH3l7lett3A 85SeXT1pceLHYfIfMy2oFbvlFZlMUfPaAzXjxU7aR3/z+FeTu8B0KTs3/KgXx3M+Y4c5 lO4g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p1si571686ejf.418.2020.12.09.03.31.41; Wed, 09 Dec 2020 03:32:03 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=crapouillou.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730598AbgLIL2E convert rfc822-to-8bit (ORCPT + 99 others); Wed, 9 Dec 2020 06:28:04 -0500 Received: from aposti.net ([89.234.176.197]:60676 "EHLO aposti.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727970AbgLIL2D (ORCPT ); Wed, 9 Dec 2020 06:28:03 -0500 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 Cc: Linus Walleij , Arnd Bergmann , MIPS , Linux Kernel Mailing List , od@zcrc.me, Linux ARM Message-Id: <75L2LQ.XOYPRHI9W3U9@crapouillou.net> In-Reply-To: References: <20201208164821.2686082-1-paul@crapouillou.net> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Ard, Le mer. 9 d?c. 2020 ? 10:38, Ard Biesheuvel a ?crit : > 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 = { >> #ifdef CONFIG_FOO_SUSPEND >> .suspend = 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 = { >> #ifdef CONFIG_FOO_SUSPEND >> .suspend = 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 = { >> .suspend = 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