linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] linux/kconfig.h: replace IF_ENABLED() with PTR_IF() in <linux/kernel.h>
@ 2021-05-05 17:45 Masahiro Yamada
  2021-05-05 21:09 ` Kees Cook
  2021-05-08  1:59 ` Stephen Rothwell
  0 siblings, 2 replies; 4+ messages in thread
From: Masahiro Yamada @ 2021-05-05 17:45 UTC (permalink / raw)
  To: linux-kbuild
  Cc: Masahiro Yamada, Andrew Morton, Andy Shevchenko, Arnd Bergmann,
	Guilherme G. Piccoli, Kars Mulder, Kees Cook,
	Kishon Vijay Abraham I, Linus Walleij, Paul Cercueil,
	Peter Zijlstra, Thomas Gleixner, chao, linux-gpio, linux-kernel,
	linux-mips

<linux/kconfig.h> is included from all the kernel-space source files,
including C, assembly, linker scripts. It is intended to contain a
minimal set of macros to evaluate CONFIG options.

IF_ENABLED() is an intruder here because (x ? y : z) is C code, which
should not be included from assembly files or linker scripts.

Also, <linux/kconfig.h> is no longer self-contained because NULL is
defined in <linux/stddef.h>.

Move IF_ENABLED() out to <linux/kernel.h> as PTR_IF(). PTF_IF()
takes the general boolean expression instead of a CONFIG option
so that it fits better in <linux/kernel.h>.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

Changes in v2:
  - Keep PTF_IF macro in pinctrl-ingenic.c

 drivers/pinctrl/pinctrl-ingenic.c | 2 ++
 include/linux/kconfig.h           | 6 ------
 include/linux/kernel.h            | 2 ++
 3 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/pinctrl/pinctrl-ingenic.c b/drivers/pinctrl/pinctrl-ingenic.c
index 651a36b9dcc0..0ee69f8e20b2 100644
--- a/drivers/pinctrl/pinctrl-ingenic.c
+++ b/drivers/pinctrl/pinctrl-ingenic.c
@@ -3854,6 +3854,8 @@ static int __init ingenic_pinctrl_probe(struct platform_device *pdev)
 	return 0;
 }
 
+#define IF_ENABLED(cfg, ptr)	PTR_IF(IS_ENABLED(cfg), (ptr))
+
 static const struct of_device_id ingenic_pinctrl_of_match[] = {
 	{
 		.compatible = "ingenic,jz4730-pinctrl",
diff --git a/include/linux/kconfig.h b/include/linux/kconfig.h
index 24a59cb06963..cc8fa109cfa3 100644
--- a/include/linux/kconfig.h
+++ b/include/linux/kconfig.h
@@ -70,10 +70,4 @@
  */
 #define IS_ENABLED(option) __or(IS_BUILTIN(option), IS_MODULE(option))
 
-/*
- * IF_ENABLED(CONFIG_FOO, ptr) evaluates to (ptr) if CONFIG_FOO is set to 'y'
- * or 'm', NULL otherwise.
- */
-#define IF_ENABLED(option, ptr) (IS_ENABLED(option) ? (ptr) : NULL)
-
 #endif /* __LINUX_KCONFIG_H */
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 5b7ed6dc99ac..8685ca4cf287 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -38,6 +38,8 @@
 #define PTR_ALIGN_DOWN(p, a)	((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
 #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
 
+#define PTR_IF(cond, ptr)	((cond) ? (ptr) : NULL)
+
 /* generic data direction definitions */
 #define READ			0
 #define WRITE			1
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] linux/kconfig.h: replace IF_ENABLED() with PTR_IF() in <linux/kernel.h>
  2021-05-05 17:45 [PATCH v2] linux/kconfig.h: replace IF_ENABLED() with PTR_IF() in <linux/kernel.h> Masahiro Yamada
@ 2021-05-05 21:09 ` Kees Cook
  2021-05-08  1:59 ` Stephen Rothwell
  1 sibling, 0 replies; 4+ messages in thread
From: Kees Cook @ 2021-05-05 21:09 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Andrew Morton, Andy Shevchenko, Arnd Bergmann,
	Guilherme G. Piccoli, Kars Mulder, Kishon Vijay Abraham I,
	Linus Walleij, Paul Cercueil, Peter Zijlstra, Thomas Gleixner,
	chao, linux-gpio, linux-kernel, linux-mips

On Thu, May 06, 2021 at 02:45:15AM +0900, Masahiro Yamada wrote:
> <linux/kconfig.h> is included from all the kernel-space source files,
> including C, assembly, linker scripts. It is intended to contain a
> minimal set of macros to evaluate CONFIG options.
> 
> IF_ENABLED() is an intruder here because (x ? y : z) is C code, which
> should not be included from assembly files or linker scripts.
> 
> Also, <linux/kconfig.h> is no longer self-contained because NULL is
> defined in <linux/stddef.h>.
> 
> Move IF_ENABLED() out to <linux/kernel.h> as PTR_IF(). PTF_IF()
> takes the general boolean expression instead of a CONFIG option
> so that it fits better in <linux/kernel.h>.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] linux/kconfig.h: replace IF_ENABLED() with PTR_IF() in <linux/kernel.h>
  2021-05-05 17:45 [PATCH v2] linux/kconfig.h: replace IF_ENABLED() with PTR_IF() in <linux/kernel.h> Masahiro Yamada
  2021-05-05 21:09 ` Kees Cook
@ 2021-05-08  1:59 ` Stephen Rothwell
  2021-05-08  4:23   ` Masahiro Yamada
  1 sibling, 1 reply; 4+ messages in thread
From: Stephen Rothwell @ 2021-05-08  1:59 UTC (permalink / raw)
  To: Masahiro Yamada
  Cc: linux-kbuild, Andrew Morton, Andy Shevchenko, Arnd Bergmann,
	Guilherme G. Piccoli, Kars Mulder, Kees Cook,
	Kishon Vijay Abraham I, Linus Walleij, Paul Cercueil,
	Peter Zijlstra, Thomas Gleixner, chao, linux-gpio, linux-kernel,
	linux-mips

[-- Attachment #1: Type: text/plain, Size: 2691 bytes --]

Hi Masahiro,

On Thu,  6 May 2021 02:45:15 +0900 Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> <linux/kconfig.h> is included from all the kernel-space source files,
> including C, assembly, linker scripts. It is intended to contain a
> minimal set of macros to evaluate CONFIG options.
> 
> IF_ENABLED() is an intruder here because (x ? y : z) is C code, which
> should not be included from assembly files or linker scripts.

Except it doesn't matter unless IF_ENABLED() is used by one of those.

> Also, <linux/kconfig.h> is no longer self-contained because NULL is
> defined in <linux/stddef.h>.

Again, it doesn't matter unless IF_ENABLED() is used.

> Move IF_ENABLED() out to <linux/kernel.h> as PTR_IF(). PTF_IF()
> takes the general boolean expression instead of a CONFIG option
> so that it fits better in <linux/kernel.h>.
> 
> Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> ---
> 
> Changes in v2:
>   - Keep PTF_IF macro in pinctrl-ingenic.c
> 
>  drivers/pinctrl/pinctrl-ingenic.c | 2 ++
>  include/linux/kconfig.h           | 6 ------
>  include/linux/kernel.h            | 2 ++
>  3 files changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/pinctrl/pinctrl-ingenic.c b/drivers/pinctrl/pinctrl-ingenic.c
> index 651a36b9dcc0..0ee69f8e20b2 100644
> --- a/drivers/pinctrl/pinctrl-ingenic.c
> +++ b/drivers/pinctrl/pinctrl-ingenic.c
> @@ -3854,6 +3854,8 @@ static int __init ingenic_pinctrl_probe(struct platform_device *pdev)
>  	return 0;
>  }
>  
> +#define IF_ENABLED(cfg, ptr)	PTR_IF(IS_ENABLED(cfg), (ptr))
> +
>  static const struct of_device_id ingenic_pinctrl_of_match[] = {
>  	{
>  		.compatible = "ingenic,jz4730-pinctrl",

You also need to include linux/kernel.h in
drivers/pinctrl/pinctrl-ingenic.c (for completeness).

Also, I don't understand why the use of IF_ENABLED doesn't produce
"defined but not used" warnings (if the function "ptr" is not marked as
__maybe_unused) ...

Also, if there is only one user of IF_ENABLED (and therefore PTR_IF),
why not just put it in that file and save me rebuilding the world again
every day because kernel.h is changed (again).  I guess that is going
to happen just because kconfig.h is being changed and that is also
included by everything :-(

Also, is anyone else ever going to use PTR_IF() without having to also
use IS_ENABLED()?

So, in case it is not obvious, I consider this patch unnecessary churn
(as was probably the patch that introduced IF_ENABLED in the first
place).

As an aside, this should not have been added to the kbuild tree in
linux-next until after -rc1 was released ...

-- 
Cheers,
Stephen Rothwell

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] linux/kconfig.h: replace IF_ENABLED() with PTR_IF() in <linux/kernel.h>
  2021-05-08  1:59 ` Stephen Rothwell
@ 2021-05-08  4:23   ` Masahiro Yamada
  0 siblings, 0 replies; 4+ messages in thread
From: Masahiro Yamada @ 2021-05-08  4:23 UTC (permalink / raw)
  To: Stephen Rothwell
  Cc: Linux Kbuild mailing list, Andrew Morton, Andy Shevchenko,
	Arnd Bergmann, Guilherme G. Piccoli, Kars Mulder, Kees Cook,
	Kishon Vijay Abraham I, Linus Walleij, Paul Cercueil,
	Peter Zijlstra, Thomas Gleixner, chao, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List, open list:BROADCOM NVRAM DRIVER

Hi Stephen,


On Sat, May 8, 2021 at 10:59 AM Stephen Rothwell <sfr@canb.auug.org.au> wrote:
>
> Hi Masahiro,
>
> On Thu,  6 May 2021 02:45:15 +0900 Masahiro Yamada <masahiroy@kernel.org> wrote:
> >
> > <linux/kconfig.h> is included from all the kernel-space source files,
> > including C, assembly, linker scripts. It is intended to contain a
> > minimal set of macros to evaluate CONFIG options.
> >
> > IF_ENABLED() is an intruder here because (x ? y : z) is C code, which
> > should not be included from assembly files or linker scripts.
>
> Except it doesn't matter unless IF_ENABLED() is used by one of those.

Yes. This is rather coding policy of <linux/kconfig.h>

> > Also, <linux/kconfig.h> is no longer self-contained because NULL is
> > defined in <linux/stddef.h>.
>
> Again, it doesn't matter unless IF_ENABLED() is used.

If you want to use IF_ENABLED(), you  must understand that
it is internally using NULL and include <linux/stddef.h>.

It is the reason why we should make headers self-contained.



>
> > Move IF_ENABLED() out to <linux/kernel.h> as PTR_IF(). PTF_IF()
> > takes the general boolean expression instead of a CONFIG option
> > so that it fits better in <linux/kernel.h>.
> >
> > Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
> > ---
> >
> > Changes in v2:
> >   - Keep PTF_IF macro in pinctrl-ingenic.c
> >
> >  drivers/pinctrl/pinctrl-ingenic.c | 2 ++
> >  include/linux/kconfig.h           | 6 ------
> >  include/linux/kernel.h            | 2 ++
> >  3 files changed, 4 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/pinctrl/pinctrl-ingenic.c b/drivers/pinctrl/pinctrl-ingenic.c
> > index 651a36b9dcc0..0ee69f8e20b2 100644
> > --- a/drivers/pinctrl/pinctrl-ingenic.c
> > +++ b/drivers/pinctrl/pinctrl-ingenic.c
> > @@ -3854,6 +3854,8 @@ static int __init ingenic_pinctrl_probe(struct platform_device *pdev)
> >       return 0;
> >  }
> >
> > +#define IF_ENABLED(cfg, ptr) PTR_IF(IS_ENABLED(cfg), (ptr))
> > +
> >  static const struct of_device_id ingenic_pinctrl_of_match[] = {
> >       {
> >               .compatible = "ingenic,jz4730-pinctrl",
>
> You also need to include linux/kernel.h in
> drivers/pinctrl/pinctrl-ingenic.c (for completeness).

<linux/kernel.h> is too widely used, so some other headers
would have already included it eventually.
But, correct, it is better to include it explicitly for completeness.


>
> Also, I don't understand why the use of IF_ENABLED doesn't produce
> "defined but not used" warnings (if the function "ptr" is not marked as
> __maybe_unused) ...

The function is optimized out by the compiler, not by the pre-processor.
So, -Wunused-but-set-variable does not complain about it.


> Also, if there is only one user of IF_ENABLED (and therefore PTR_IF),
> why not just put it in that file and save me rebuilding the world again
> every day because kernel.h is changed (again).  I guess that is going
> to happen just because kconfig.h is being changed and that is also
> included by everything :-(

I understand, but this is a one-time fix.

<linux/kconfig.h> is not changed so often.

I believe this will be useful for SET_SYSTEM_SLEEP_PM_OPS cleanups.
So, the macro must be placed in a public header.


> Also, is anyone else ever going to use PTR_IF() without having to also
> use IS_ENABLED()?

Yes or no. I am not sure about the future of this.

I prefer this in a more general expression,
so IS_REACHABLE or the combination of two CONFIG options
will work.

  PTR_IF(IS_REACHABLE(CONFIG_FOO),  ...)

  PTR_IF( IS_ENABLED(CONFIG_FOO) && IS_ENABLED(CONFIG_BAR), ...)





>
> So, in case it is not obvious, I consider this patch unnecessary churn
> (as was probably the patch that introduced IF_ENABLED in the first
> place).
>
> As an aside, this should not have been added to the kbuild tree in
> linux-next until after -rc1 was released ...

Rather, I was considering sending a pull req before -rc1.

Otherwise, other drivers may start to use it.



>
> --
> Cheers,
> Stephen Rothwell



--
Best Regards
Masahiro Yamada

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-08  4:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-05 17:45 [PATCH v2] linux/kconfig.h: replace IF_ENABLED() with PTR_IF() in <linux/kernel.h> Masahiro Yamada
2021-05-05 21:09 ` Kees Cook
2021-05-08  1:59 ` Stephen Rothwell
2021-05-08  4:23   ` Masahiro Yamada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).