Fix undefined/miscompiled construct in kernel parameters
Quoted 9 lines · Linus Torvalds
Quoted 4 lines · Andi Kleen
The
parse_argscall ininit/main.cdoes pointer arithmetic between two different external symbols. This is undefined in C (only pointer arthmetic in the same symbol is defined) and gets miscompiled on AMD64 with gcc 3.2,That's silly. You're making the code less readable, with some silly parameters. Why does it get miscompiled on amd64, and let's just fix that one.
Because &arbitary_symbol_a - &arbitary_symbol_b is undefined in C and the amd64 gcc 3.2 choses to miscompile it (it results in a very big number because it converts the 56/40 division to an inversed multiplication in a wrong way). I actually wrote a compiler bug report first, but the compiler developers rightly pointed out that it is undefined.
Note this is not the first time we had such problems, it happened with __pa(&symbol) too (amd64 has a special macro to work around that). I don't know why it happens more often on amd64 than on i386, perhaps it has something to do with the RIP relative addressing or the negative kernel code model. ppc seems to have similar problems.
Passing an end pointer is not too different from passing an max index in my opinion.
Another way would be to use the anonymous asm trick proposed by rth some time ago for a similar problem (asm("" : "=g" (output) : "0" (input)) to hide the symbols from the compiler), but I didn't do that because it looked more ugly to me.
If you prefer to do it with the anonymous asm I can do it, but I don't see a big problem with passing an end pointer.
-Andi
Quoted 6 lines · Andi Kleen
Because
&arbitary_symbol_a-&arbitary_symbol_bis undefined in C and the amd64 gcc 3.2 choses to miscompile it (it results in a very big number because it converts the 56/40 division to an inversed multiplication in a wrong way). I actually wrote a compiler bug report first, but the compiler developers rightly pointed out that it is undefined.
They are not arbitrary symbols. They are symbols in the same data structure, set up by the linker script. Gcc doesn't know that, but the fact that gcc doesn't know doesn't mean that gcc should be lazy and doesn't really excuse buggy code.
The gcc developers you talked to are picking their legalistic noses, and it's sad that this isn't exactly the first time it has happened.
Linus
Quoted 7 lines
They are not arbitrary symbols. They are symbols in the same data structure, set up by the linker script. Gcc doesn't know that, but the fact that gcc doesn't know doesn't mean that gcc should be lazy and doesn't really excuse buggy code.
The gcc developers you talked to are picking their legalistic noses, and it's sad that this isn't exactly the first time it has happened.
I tend to agree, feel free to flame them. But it doesn't help me right now when I want to get a booting kernel. Could you merge that change or if you prefer I can rewrite it to anonymous asm (but it will be probably more ugly). I just need some workaround.
Thanks.
-Andi
Hi,
Quoted 4 lines · Andi Kleen
I tend to agree, feel free to flame them. But it doesn't help me right now when I want to get a booting kernel. Could you merge that change or if you prefer I can rewrite it to anonymous asm (but it will be probably more ugly). I just need some workaround.
Does the patch below work better?
bye, Roman
init/main.c +3 -3
@@ -383,7 +383,7 @@ asmlinkage void __init start_kernel(void {char * command_line;extern char saved_command_line[]; -extern struct kernel_param __start___param, __stop___param; +extern struct kernel_param __start___param[], __stop___param[];/* * Interrupts are still disabled. Do necessary setups, then * enable them
@@ -403,8 +403,8 @@ asmlinkage void __init start_kernel(voidbuild_all_zonelists ();page_alloc_init ();printk ("Kernel command line: %s\n" , saved_command_line); -parse_args ("Booting kernel" , command_line, &__start___param, - &__stop___param - &__start___param, +parse_args ("Booting kernel" , command_line, __start___param, + __stop___param - __start___param, &unknown_bootoption);trap_init ();rcu_init ();
Quoted 10 lines · Roman Zippel
Hi,
Quoted 4 lines · Andi Kleen
I tend to agree, feel free to flame them. But it doesn't help me right now when I want to get a booting kernel. Could you merge that change or if you prefer I can rewrite it to anonymous asm (but it will be probably more ugly). I just need some workaround.
Does the patch below work better?
Nope, generates exactly the same code.
-Andi
Quoted 16 lines · Roman Zippel
Does the patch below work better?
bye, Roman
init/main.c +1
-1@@ -383,7 +383,7 @@ asmlinkage void __init start_kernel(void {char * command_line;extern char saved_command_line[]; -extern struct kernel_param __start___param, __stop___param; +extern struct kernel_param __start___param[], __stop___param[];/* * Interrupts are still disabled. Do necessary setups, then * enable them
init/main.c asmlinkage void __init start_kernel(void · line 410
Quoted 10 lines
init/main.c +2
-2@@ -403,8 +403,8 @@ asmlinkage void __init start_kernel(voidbuild_all_zonelists ();page_alloc_init ();printk ("Kernel command line: %s\n" , saved_command_line); -parse_args ("Booting kernel" , command_line, &__start___param, - &__stop___param - &__start___param, +parse_args ("Booting kernel" , command_line, __start___param, + __stop___param - __start___param, &unknown_bootoption);trap_init ();rcu_init ();
Linus, I'd REALLY prefer this patch be applied, even though the problem turned out to be one of amd64 alignment, which can be worked around.
Even if struct kernel_param doesn't suffer from .sdata problems, this formulation is closer to Correct. I'd really prefer that all such linker-script generated arrays used the [] form, and not worry about the size of the data object involved.
r~