Re: [PATCH] bug: Fix no-return-statement warning with !CONFIG_BUG

From: Adrian Hunter
Date: Thu Apr 11 2024 - 05:28:23 EST


On 11/04/24 11:22, Christophe Leroy wrote:
>
>
> Le 11/04/2024 à 10:12, Christophe Leroy a écrit :
>>
>>
>> Le 11/04/2024 à 09:16, Adrian Hunter a écrit :
>>> On 11/04/24 10:04, Arnd Bergmann wrote:
>>>> On Wed, Apr 10, 2024, at 17:32, Adrian Hunter wrote:
>>>>> BUG() does not return, and arch implementations of BUG() use
>>>>> unreachable()
>>>>> or other non-returning code. However with !CONFIG_BUG, the default
>>>>> implementation is often used instead, and that does not do that. x86
>>>>> always
>>>>> uses its own implementation, but powerpc with !CONFIG_BUG gives a build
>>>>> error:
>>>>>
>>>>>    kernel/time/timekeeping.c: In function ‘timekeeping_debug_get_ns’:
>>>>>    kernel/time/timekeeping.c:286:1: error: no return statement in
>>>>> function
>>>>>    returning non-void [-Werror=return-type]
>>>>>
>>>>> Add unreachable() to default !CONFIG_BUG BUG() implementation.
>>>>
>>>> I'm a bit worried about this patch, since we have had problems
>>>> with unreachable() inside of BUG() in the past, and as far as I
>>>> can remember, the current version was the only one that
>>>> actually did the right thing on all compilers.
>>>>
>>>> One problem with an unreachable() annotation here is that if
>>>> a compiler misanalyses the endless loop, it can decide to
>>>> throw out the entire code path leading up to it and just
>>>> run into undefined behavior instead of printing a BUG()
>>>> message.
>>>>
>>>> Do you know which compiler version show the warning above?
>>>
>>> Original report has a list
>>>
>>>     https://lore.kernel.org/all/CA+G9fYvjdZCW=7ZGxS6A_3bysjQ56YF7S-+PNLQ_8a4DKh1Bhg@xxxxxxxxxxxxxx/
>>>
>>
>> Looking at the report, I think the correct fix should be to use
>> BUILD_BUG() instead of BUG()
>
> I confirm the error goes away with the following change to next-20240411
> on powerpc tinyconfig with gcc 13.2
>
> diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
> index 4e18db1819f8..3d5ac0cdd721 100644
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -282,7 +282,7 @@ static inline void timekeeping_check_update(struct
> timekeeper *tk, u64 offset)
> }
> static inline u64 timekeeping_debug_get_ns(const struct tk_read_base *tkr)
> {
> - BUG();
> + BUILD_BUG();
> }
> #endif
>

That is fragile because it depends on defined(__OPTIMIZE__),
so it should still be:

BUILD_BUG();
return 0;