All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: "Marek Behún" <kabel@kernel.org>
Cc: "Stefan Roese" <sr@denx.de>,
	u-boot@lists.denx.de, "Marek Behún" <marek.behun@nic.cz>
Subject: Re: [PATCH u-boot-marvell v2 8/9] arm: mvebu: spl: Use IS_ENABLED() instead of #ifdef where possible
Date: Thu, 16 Dec 2021 19:16:40 +0100	[thread overview]
Message-ID: <20211216181640.c3aolyfege4675ib@pali> (raw)
In-Reply-To: <20211214140104.7a410847@thinkpad>

On Tuesday 14 December 2021 14:01:04 Marek Behún wrote:
> On Tue, 14 Dec 2021 13:48:31 +0100
> Stefan Roese <sr@denx.de> wrote:
> 
> > On 12/14/21 13:45, Marek Behún wrote:
> > > On Tue, 14 Dec 2021 12:12:34 +0100
> > > Pali Rohár <pali@kernel.org> wrote:
> > >   
> > >> On Tuesday 14 December 2021 10:45:15 Marek Behún wrote:  
> > >>> On Tue, 14 Dec 2021 10:36:00 +0100
> > >>> Pali Rohár <pali@kernel.org> wrote:
> > >>>      
> > >>>> On Friday 26 November 2021 15:37:37 Marek Behún wrote:  
> > >>>>> @@ -340,17 +333,17 @@ void board_init_f(ulong dummy)
> > >>>>>   	timer_init();
> > >>>>>   
> > >>>>>   	/* Armada 375 does not support SerDes and DDR3 init yet */
> > >>>>> -#if !defined(CONFIG_ARMADA_375)
> > >>>>> -	/* First init the serdes PHY's */
> > >>>>> -	serdes_phy_config();
> > >>>>> -
> > >>>>> -	/* Setup DDR */
> > >>>>> -	ret = ddr3_init();
> > >>>>> -	if (ret) {
> > >>>>> -		debug("ddr3_init() failed: %d\n", ret);
> > >>>>> -		hang();
> > >>>>> +	if (!IS_ENABLED(CONFIG_ARMADA_375)) {
> > >>>>> +		/* First init the serdes PHY's */
> > >>>>> +		serdes_phy_config();
> > >>>>> +
> > >>>>> +		/* Setup DDR */
> > >>>>> +		ret = ddr3_init();
> > >>>>> +		if (ret) {
> > >>>>> +			debug("ddr3_init() failed: %d\n", ret);
> > >>>>> +			hang();
> > >>>>> +		}
> > >>>>>   	}
> > >>>>> -#endif  
> > >>>>
> > >>>> As written in comment above there is no SerDes and DDR3 support for
> > >>>> Armada 375 and therefore there is no serdes_phy_config() or ddr3_init()
> > >>>> function. So this code needs not to be compiled at all and usage of
> > >>>> #ifdef is correct here.  
> > >>>
> > >>> #ifdefs are almost never correct in C-files, for the parts of the code
> > >>> they guard isn't put through syntactic analysis, and can therefore
> > >>> contain bugs which we are not warned about.
> > >>>
> > >>> Using if (IS_ENABLED()) almost never producess a different binary,
> > >>> because the code is optimized away.
> > >>>
> > >>> Marek  
> > >>
> > >> There is no function serdes_phy_config() for Armada 375, so if you put
> > >> it outside of #ifdef you will get compile error.  
> > > 
> > > The function is always declared in
> > >    arch/arm/mach-mvebu/include/mach/cpu.h
> > > regardless of architecture.
> > > 
> > > Thus an error will be raised only when linking, and the compliation was
> > > done with -O0, which I don't think anyone does.
> > > 
> > > Anyway, if we want to support -O0, this can and should be solved via
> > > defining serdes_phy_config() as empty static inline function in the
> > > cpu.h header, guarded by #ifdef. In header files #ifdefs are allowed,
> > > in this manner:
> > >    #if X
> > >    declare function
> > >    #else
> > >    define that function as empty static inline
> > >    #endif
> > > 
> > > So if you think we should support -O0, I can do this.
> > > 
> > > But the #ifdefs should really go away from real C code, that is the way
> > > both Linux and U-Boot are trying to go for the last couple of years, if
> > > I understand it correctly.  
> > 
> > Yes, the #ifdef's really should be avoided if possible. So *if* your
> > patch above does not generate any build issues, then I don't see any
> > problems to include it. I personally don't think that we need to support
> > -O0 builds.
> 
> db-88f6720_defconfig builds without issue (armada 375). And it builds the
> relevant file, spl/arch/arm/mach-mvebu/spl.o.
> 
> Marek

-O0 is useful for debugging purposes, it generates more readable
assembler code.

Anyway, the issue here is that those two functions are not defined and
implemented for armada 375 soc. #ifdef is here to selectively do not
compile code which is not implemented on armada 375. And this cannot be
done by normal if(). The reason that it currently works is just because
gcc compiler does not do all checks before doing optimizations and so it
currently does generate any errors or warnings. But this is just
undefined behavior and like any other undefined behavior it may change
in some future version of gcc (or changing compiler to some other).

This approach with converting correct #ifdef to if() with undefined
behavior just hides the real issue that those two functions are not
defined and implemented for all mvebu platforms.

Why not rather to define these two functions are empty static inline
stubs with big comment that they are missing? I think this is proper
solution as it does not depends on undefined behavior of compiler and
linker, supports also -O0 and removes that #ifdef in spl.c file.

  reply	other threads:[~2021-12-16 18:16 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-26 14:37 [PATCH u-boot-marvell v2 0/9] More verifications for kwbimage in SPL Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 1/9] arm: mvebu: Check that kwbimage offset and blocksize are valid Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 2/9] SPL: Add struct spl_boot_device parameter into spl_parse_board_header() Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 3/9] arm: mvebu: Check that kwbimage blockid matches boot mode Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 4/9] SPL: Add support for checking board / BootROM specific image types Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 5/9] arm: mvebu: Check for kwbimage data checksum Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 6/9] arm: mvebu: spl: Print srcaddr in error message Marek Behún
2021-11-30  6:20   ` Stefan Roese
2021-12-14 11:10   ` Pali Rohár
2021-12-14 13:11     ` Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 7/9] arm: mvebu: spl: Use preferred types u8/u16/u32 instead of uintN_t Marek Behún
2021-11-30  6:20   ` Stefan Roese
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 8/9] arm: mvebu: spl: Use IS_ENABLED() instead of #ifdef where possible Marek Behún
2021-11-30  6:22   ` Stefan Roese
2021-12-14  9:36   ` Pali Rohár
2021-12-14  9:45     ` Marek Behún
2021-12-14 11:12       ` Pali Rohár
2021-12-14 12:45         ` Marek Behún
2021-12-14 12:48           ` Stefan Roese
2021-12-14 13:01             ` Marek Behún
2021-12-16 18:16               ` Pali Rohár [this message]
2021-12-16 22:09                 ` Marek Behún
2021-12-16 22:17                   ` Marek Behún
2021-11-26 14:37 ` [PATCH u-boot-marvell v2 9/9] arm: mvebu: spl: Fix 100 column exceeds Marek Behún
2021-11-30  6:22   ` Stefan Roese

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211216181640.c3aolyfege4675ib@pali \
    --to=pali@kernel.org \
    --cc=kabel@kernel.org \
    --cc=marek.behun@nic.cz \
    --cc=sr@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.