2022-02-10 03:43:45

by Victor Erminpour

[permalink] [raw]
Subject: [PATCH] ACPI/IORT: Fix GCC 12 warning

When building with automatic stack variable initialization, GCC 12
complains about variables defined outside of switch case statements.
Move the variable into the case that uses it, which silences the warning:

./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be executed [-Werror=switch-unreachable]
1670 | struct acpi_iort_named_component *ncomp;
| ^~~~~

Signed-off-by: Victor Erminpour <[email protected]>
---
drivers/acpi/arm64/iort.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
index 3b23fb775ac4..5c5d2e56d756 100644
--- a/drivers/acpi/arm64/iort.c
+++ b/drivers/acpi/arm64/iort.c
@@ -1645,7 +1645,7 @@ void __init acpi_iort_init(void)
*/
phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
{
- phys_addr_t limit = PHYS_ADDR_MAX;
+ phys_addr_t local_limit, limit = PHYS_ADDR_MAX;
struct acpi_iort_node *node, *end;
struct acpi_table_iort *iort;
acpi_status status;
@@ -1667,17 +1667,15 @@ phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
break;

switch (node->type) {
- struct acpi_iort_named_component *ncomp;
- struct acpi_iort_root_complex *rc;
- phys_addr_t local_limit;
-
case ACPI_IORT_NODE_NAMED_COMPONENT:
+ struct acpi_iort_named_component *ncomp;
ncomp = (struct acpi_iort_named_component *)node->node_data;
local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
limit = min_not_zero(limit, local_limit);
break;

case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
+ struct acpi_iort_root_complex *rc;
if (node->revision < 1)
break;



2022-02-10 21:34:30

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH] ACPI/IORT: Fix GCC 12 warning

(cc Kees)

On Thu, 10 Feb 2022 at 18:36, Victor Erminpour
<[email protected]> wrote:
>
>
> On 2/10/22 1:11 AM, Ard Biesheuvel wrote:
> > On Thu, 10 Feb 2022 at 01:34, Victor Erminpour
> > <[email protected]> wrote:
> >> When building with automatic stack variable initialization, GCC 12
> >> complains about variables defined outside of switch case statements.
> >> Move the variable into the case that uses it, which silences the warning:
> >>
> >> ./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be executed [-Werror=switch-unreachable]
> >> 1670 | struct acpi_iort_named_component *ncomp;
> >> | ^~~~~
> >>
> >> Signed-off-by: Victor Erminpour <[email protected]>
> > GCC 12 is not released yet, and this is clearly a compiler bug (a
> > declaration is not a statement, and the hidden offending statement
> > [the zero-init] is emitted by the compiler itself), so please report
> > this to the GCC folks instead.
>
> Hi Ard,
>
> Thanks for the reply.
> This fix is similar to the following commits that have been integrated
> upstream:
> https://lore.kernel.org/linux-hardening/[email protected]/
> https://lore.kernel.org/linux-hardening/[email protected]/
>

If GCC 12 rejects valid C with this feature enabled, the compiler is
broken and needs to be fixed. Papering over this by making changes to
perfectly valid C code is a slippery slope that we should avoid.

Since GCC 12 is not released yet, there is time to get this fixed properly.

> You're not obligated to integrate this commit,

Why, thank you :-)

> but you may run into this
> issue once
> upstream starts using GCC 12 (or a patched version of GCC 11 in my case)
> with the
> CONFIG_INIT_STACK_ALL_ZERO option enabled.
>

Yes, that part was perfectly clear.

2022-02-10 23:53:58

by Robin Murphy

[permalink] [raw]
Subject: Re: [PATCH] ACPI/IORT: Fix GCC 12 warning

On 2022-02-10 00:32, Victor Erminpour wrote:
> When building with automatic stack variable initialization, GCC 12
> complains about variables defined outside of switch case statements.
> Move the variable into the case that uses it, which silences the warning:
>
> ./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be executed [-Werror=switch-unreachable]
> 1670 | struct acpi_iort_named_component *ncomp;
> | ^~~~~

Notwithstanding the fact that that warning is nonsensical, this patch
changes valid C code into invalid C code that doesn't even compile:

drivers/acpi/arm64/iort.c: In function ‘acpi_iort_dma_get_max_cpu_address’:
drivers/acpi/arm64/iort.c:1669:4: error: a label can only be part of a
statement and a declaration is not a statement
1669 | struct acpi_iort_named_component *ncomp;
| ^~~~~~
drivers/acpi/arm64/iort.c:1676:4: error: a label can only be part of a
statement and a declaration is not a statement
1676 | struct acpi_iort_root_complex *rc;
| ^~~~~~

Robin.

> Signed-off-by: Victor Erminpour <[email protected]>
> ---
> drivers/acpi/arm64/iort.c | 8 +++-----
> 1 file changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
> index 3b23fb775ac4..5c5d2e56d756 100644
> --- a/drivers/acpi/arm64/iort.c
> +++ b/drivers/acpi/arm64/iort.c
> @@ -1645,7 +1645,7 @@ void __init acpi_iort_init(void)
> */
> phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
> {
> - phys_addr_t limit = PHYS_ADDR_MAX;
> + phys_addr_t local_limit, limit = PHYS_ADDR_MAX;
> struct acpi_iort_node *node, *end;
> struct acpi_table_iort *iort;
> acpi_status status;
> @@ -1667,17 +1667,15 @@ phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
> break;
>
> switch (node->type) {
> - struct acpi_iort_named_component *ncomp;
> - struct acpi_iort_root_complex *rc;
> - phys_addr_t local_limit;
> -
> case ACPI_IORT_NODE_NAMED_COMPONENT:
> + struct acpi_iort_named_component *ncomp;
> ncomp = (struct acpi_iort_named_component *)node->node_data;
> local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
> limit = min_not_zero(limit, local_limit);
> break;
>
> case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
> + struct acpi_iort_root_complex *rc;
> if (node->revision < 1)
> break;
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

2022-02-11 03:51:33

by Victor Erminpour

[permalink] [raw]
Subject: Re: [PATCH] ACPI/IORT: Fix GCC 12 warning


On 2/10/22 1:11 AM, Ard Biesheuvel wrote:
> On Thu, 10 Feb 2022 at 01:34, Victor Erminpour
> <[email protected]> wrote:
>> When building with automatic stack variable initialization, GCC 12
>> complains about variables defined outside of switch case statements.
>> Move the variable into the case that uses it, which silences the warning:
>>
>> ./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be executed [-Werror=switch-unreachable]
>> 1670 | struct acpi_iort_named_component *ncomp;
>> | ^~~~~
>>
>> Signed-off-by: Victor Erminpour <[email protected]>
> GCC 12 is not released yet, and this is clearly a compiler bug (a
> declaration is not a statement, and the hidden offending statement
> [the zero-init] is emitted by the compiler itself), so please report
> this to the GCC folks instead.

Hi Ard,

Thanks for the reply.
This fix is similar to the following commits that have been integrated
upstream:
https://lore.kernel.org/linux-hardening/[email protected]/
https://lore.kernel.org/linux-hardening/[email protected]/

You're not obligated to integrate this commit, but you may run into this
issue once
upstream starts using GCC 12 (or a patched version of GCC 11 in my case)
with the
CONFIG_INIT_STACK_ALL_ZERO option enabled.

Regards,
--Victor


>
>> ---
>> drivers/acpi/arm64/iort.c | 8 +++-----
>> 1 file changed, 3 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
>> index 3b23fb775ac4..5c5d2e56d756 100644
>> --- a/drivers/acpi/arm64/iort.c
>> +++ b/drivers/acpi/arm64/iort.c
>> @@ -1645,7 +1645,7 @@ void __init acpi_iort_init(void)
>> */
>> phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
>> {
>> - phys_addr_t limit = PHYS_ADDR_MAX;
>> + phys_addr_t local_limit, limit = PHYS_ADDR_MAX;
>> struct acpi_iort_node *node, *end;
>> struct acpi_table_iort *iort;
>> acpi_status status;
>> @@ -1667,17 +1667,15 @@ phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
>> break;
>>
>> switch (node->type) {
>> - struct acpi_iort_named_component *ncomp;
>> - struct acpi_iort_root_complex *rc;
>> - phys_addr_t local_limit;
>> -
>> case ACPI_IORT_NODE_NAMED_COMPONENT:
>> + struct acpi_iort_named_component *ncomp;
>> ncomp = (struct acpi_iort_named_component *)node->node_data;
>> local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
>> limit = min_not_zero(limit, local_limit);
>> break;
>>
>> case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
>> + struct acpi_iort_root_complex *rc;
>> if (node->revision < 1)
>> break;
>>
>>
>> _______________________________________________
>> linux-arm-kernel mailing list
>> [email protected]
>> https://urldefense.com/v3/__http://lists.infradead.org/mailman/listinfo/linux-arm-kernel__;!!ACWV5N9M2RV99hQ!dguMEUG-y4wN_Scw_dP1umWKxZKxiQA2Fv9sHuQx6Un9QIqSMbkQVxTeIZInR2h7YL-f$

2022-02-11 06:07:14

by Robin Murphy

[permalink] [raw]
Subject: Re: [PATCH] ACPI/IORT: Fix GCC 12 warning

On 2022-02-10 18:27, Victor Erminpour wrote:
>
> On 2/10/22 10:06 AM, Robin Murphy wrote:
>> On 2022-02-10 00:32, Victor Erminpour wrote:
>>> When building with automatic stack variable initialization, GCC 12
>>> complains about variables defined outside of switch case statements.
>>> Move the variable into the case that uses it, which silences the
>>> warning:
>>>
>>> ./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be
>>> executed [-Werror=switch-unreachable]
>>>    1670 |                         struct acpi_iort_named_component
>>> *ncomp;
>>> | ^~~~~
>>
>> Notwithstanding the fact that that warning is nonsensical, this patch
>> changes valid C code into invalid C code that doesn't even compile:
>>
>> drivers/acpi/arm64/iort.c: In function
>> ‘acpi_iort_dma_get_max_cpu_address’:
>> drivers/acpi/arm64/iort.c:1669:4: error: a label can only be part of a
>> statement and a declaration is not a statement
>>  1669 |    struct acpi_iort_named_component *ncomp;
>>       |    ^~~~~~
>> drivers/acpi/arm64/iort.c:1676:4: error: a label can only be part of a
>> statement and a declaration is not a statement
>>  1676 |    struct acpi_iort_root_complex *rc;
>>       |    ^~~~~~
>>
>> Robin.
>>
>
> Hi Robin,
>
> Thank you for your constructive criticism.
> Could the solution be enclosing the case statement in curly braces?
>
> I know this isn't a big issue for you, but this is a legitimate error
> for people
> building the kernel with GCC 12 and CONFIG_INIT_STACK_ALL_ZERO enabled.

As Ard pointed out first, it is not a legitimate error, it is a spurious
error. The fact that you seemingly didn't get the label errors above,
nor even "ISO C90 forbids mixed declarations and code
[-Wdeclaration-after-statement]", implies that GCC 12 currently has a
completely broken notion of declarations vs. statements with that option
enabled, so GCC 12 needs fixing.

Robin.

>
> Regards,
> --Victor
>
>
>
>>> Signed-off-by: Victor Erminpour <[email protected]>
>>> ---
>>>   drivers/acpi/arm64/iort.c | 8 +++-----
>>>   1 file changed, 3 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
>>> index 3b23fb775ac4..5c5d2e56d756 100644
>>> --- a/drivers/acpi/arm64/iort.c
>>> +++ b/drivers/acpi/arm64/iort.c
>>> @@ -1645,7 +1645,7 @@ void __init acpi_iort_init(void)
>>>    */
>>>   phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
>>>   {
>>> -    phys_addr_t limit = PHYS_ADDR_MAX;
>>> +    phys_addr_t local_limit, limit = PHYS_ADDR_MAX;
>>>       struct acpi_iort_node *node, *end;
>>>       struct acpi_table_iort *iort;
>>>       acpi_status status;
>>> @@ -1667,17 +1667,15 @@ phys_addr_t __init
>>> acpi_iort_dma_get_max_cpu_address(void)
>>>               break;
>>>             switch (node->type) {
>>> -            struct acpi_iort_named_component *ncomp;
>>> -            struct acpi_iort_root_complex *rc;
>>> -            phys_addr_t local_limit;
>>> -
>>>           case ACPI_IORT_NODE_NAMED_COMPONENT:
>>> +            struct acpi_iort_named_component *ncomp;
>>>               ncomp = (struct acpi_iort_named_component
>>> *)node->node_data;
>>>               local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
>>>               limit = min_not_zero(limit, local_limit);
>>>               break;
>>>             case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
>>> +            struct acpi_iort_root_complex *rc;
>>>               if (node->revision < 1)
>>>                   break;
>>>
>>> _______________________________________________
>>> linux-arm-kernel mailing list
>>> [email protected]
>>> https://urldefense.com/v3/__http://lists.infradead.org/mailman/listinfo/linux-arm-kernel__;!!ACWV5N9M2RV99hQ!ZcNRWTaQIb1uVqcZAusSzXd2kBnWuqAuICpxkOm6bOWBouQoFygbcIMJVUNp34LqhNrc$
>>
>>

2022-02-11 06:41:07

by Victor Erminpour

[permalink] [raw]
Subject: Re: [PATCH] ACPI/IORT: Fix GCC 12 warning


On 2/10/22 10:06 AM, Robin Murphy wrote:
> On 2022-02-10 00:32, Victor Erminpour wrote:
>> When building with automatic stack variable initialization, GCC 12
>> complains about variables defined outside of switch case statements.
>> Move the variable into the case that uses it, which silences the
>> warning:
>>
>> ./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be
>> executed [-Werror=switch-unreachable]
>>    1670 |                         struct acpi_iort_named_component
>> *ncomp;
>> | ^~~~~
>
> Notwithstanding the fact that that warning is nonsensical, this patch
> changes valid C code into invalid C code that doesn't even compile:
>
> drivers/acpi/arm64/iort.c: In function
> ‘acpi_iort_dma_get_max_cpu_address’:
> drivers/acpi/arm64/iort.c:1669:4: error: a label can only be part of a
> statement and a declaration is not a statement
>  1669 |    struct acpi_iort_named_component *ncomp;
>       |    ^~~~~~
> drivers/acpi/arm64/iort.c:1676:4: error: a label can only be part of a
> statement and a declaration is not a statement
>  1676 |    struct acpi_iort_root_complex *rc;
>       |    ^~~~~~
>
> Robin.
>

Hi Robin,

Thank you for your constructive criticism.
Could the solution be enclosing the case statement in curly braces?

I know this isn't a big issue for you, but this is a legitimate error
for people
building the kernel with GCC 12 and CONFIG_INIT_STACK_ALL_ZERO enabled.

Regards,
--Victor



>> Signed-off-by: Victor Erminpour <[email protected]>
>> ---
>>   drivers/acpi/arm64/iort.c | 8 +++-----
>>   1 file changed, 3 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
>> index 3b23fb775ac4..5c5d2e56d756 100644
>> --- a/drivers/acpi/arm64/iort.c
>> +++ b/drivers/acpi/arm64/iort.c
>> @@ -1645,7 +1645,7 @@ void __init acpi_iort_init(void)
>>    */
>>   phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
>>   {
>> -    phys_addr_t limit = PHYS_ADDR_MAX;
>> +    phys_addr_t local_limit, limit = PHYS_ADDR_MAX;
>>       struct acpi_iort_node *node, *end;
>>       struct acpi_table_iort *iort;
>>       acpi_status status;
>> @@ -1667,17 +1667,15 @@ phys_addr_t __init
>> acpi_iort_dma_get_max_cpu_address(void)
>>               break;
>>             switch (node->type) {
>> -            struct acpi_iort_named_component *ncomp;
>> -            struct acpi_iort_root_complex *rc;
>> -            phys_addr_t local_limit;
>> -
>>           case ACPI_IORT_NODE_NAMED_COMPONENT:
>> +            struct acpi_iort_named_component *ncomp;
>>               ncomp = (struct acpi_iort_named_component
>> *)node->node_data;
>>               local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
>>               limit = min_not_zero(limit, local_limit);
>>               break;
>>             case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
>> +            struct acpi_iort_root_complex *rc;
>>               if (node->revision < 1)
>>                   break;
>>
>> _______________________________________________
>> linux-arm-kernel mailing list
>> [email protected]
>> https://urldefense.com/v3/__http://lists.infradead.org/mailman/listinfo/linux-arm-kernel__;!!ACWV5N9M2RV99hQ!ZcNRWTaQIb1uVqcZAusSzXd2kBnWuqAuICpxkOm6bOWBouQoFygbcIMJVUNp34LqhNrc$
>

2022-02-12 18:24:19

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH] ACPI/IORT: Fix GCC 12 warning

On Thu, 10 Feb 2022 at 20:29, Ard Biesheuvel <[email protected]> wrote:
>
> (cc Kees)
>

... for real this time :-)

> On Thu, 10 Feb 2022 at 18:36, Victor Erminpour
> <[email protected]> wrote:
> >
> >
> > On 2/10/22 1:11 AM, Ard Biesheuvel wrote:
> > > On Thu, 10 Feb 2022 at 01:34, Victor Erminpour
> > > <[email protected]> wrote:
> > >> When building with automatic stack variable initialization, GCC 12
> > >> complains about variables defined outside of switch case statements.
> > >> Move the variable into the case that uses it, which silences the warning:
> > >>
> > >> ./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be executed [-Werror=switch-unreachable]
> > >> 1670 | struct acpi_iort_named_component *ncomp;
> > >> | ^~~~~
> > >>
> > >> Signed-off-by: Victor Erminpour <[email protected]>
> > > GCC 12 is not released yet, and this is clearly a compiler bug (a
> > > declaration is not a statement, and the hidden offending statement
> > > [the zero-init] is emitted by the compiler itself), so please report
> > > this to the GCC folks instead.
> >
> > Hi Ard,
> >
> > Thanks for the reply.
> > This fix is similar to the following commits that have been integrated
> > upstream:
> > https://lore.kernel.org/linux-hardening/[email protected]/
> > https://lore.kernel.org/linux-hardening/[email protected]/
> >
>
> If GCC 12 rejects valid C with this feature enabled, the compiler is
> broken and needs to be fixed. Papering over this by making changes to
> perfectly valid C code is a slippery slope that we should avoid.
>
> Since GCC 12 is not released yet, there is time to get this fixed properly.
>
> > You're not obligated to integrate this commit,
>
> Why, thank you :-)
>
> > but you may run into this
> > issue once
> > upstream starts using GCC 12 (or a patched version of GCC 11 in my case)
> > with the
> > CONFIG_INIT_STACK_ALL_ZERO option enabled.
> >
>
> Yes, that part was perfectly clear.

2022-02-14 07:38:54

by Ard Biesheuvel

[permalink] [raw]
Subject: Re: [PATCH] ACPI/IORT: Fix GCC 12 warning

On Thu, 10 Feb 2022 at 01:34, Victor Erminpour
<[email protected]> wrote:
>
> When building with automatic stack variable initialization, GCC 12
> complains about variables defined outside of switch case statements.
> Move the variable into the case that uses it, which silences the warning:
>
> ./drivers/acpi/arm64/iort.c:1670:59: error: statement will never be executed [-Werror=switch-unreachable]
> 1670 | struct acpi_iort_named_component *ncomp;
> | ^~~~~
>
> Signed-off-by: Victor Erminpour <[email protected]>

GCC 12 is not released yet, and this is clearly a compiler bug (a
declaration is not a statement, and the hidden offending statement
[the zero-init] is emitted by the compiler itself), so please report
this to the GCC folks instead.


> ---
> drivers/acpi/arm64/iort.c | 8 +++-----
> 1 file changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c
> index 3b23fb775ac4..5c5d2e56d756 100644
> --- a/drivers/acpi/arm64/iort.c
> +++ b/drivers/acpi/arm64/iort.c
> @@ -1645,7 +1645,7 @@ void __init acpi_iort_init(void)
> */
> phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
> {
> - phys_addr_t limit = PHYS_ADDR_MAX;
> + phys_addr_t local_limit, limit = PHYS_ADDR_MAX;
> struct acpi_iort_node *node, *end;
> struct acpi_table_iort *iort;
> acpi_status status;
> @@ -1667,17 +1667,15 @@ phys_addr_t __init acpi_iort_dma_get_max_cpu_address(void)
> break;
>
> switch (node->type) {
> - struct acpi_iort_named_component *ncomp;
> - struct acpi_iort_root_complex *rc;
> - phys_addr_t local_limit;
> -
> case ACPI_IORT_NODE_NAMED_COMPONENT:
> + struct acpi_iort_named_component *ncomp;
> ncomp = (struct acpi_iort_named_component *)node->node_data;
> local_limit = DMA_BIT_MASK(ncomp->memory_address_limit);
> limit = min_not_zero(limit, local_limit);
> break;
>
> case ACPI_IORT_NODE_PCI_ROOT_COMPLEX:
> + struct acpi_iort_root_complex *rc;
> if (node->revision < 1)
> break;
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel