gcc-4.2.0 20070316 (prerelease) miscompiles sparc64 kernel
The 2.6.21-rc5 kernel compiled with gcc-4.2.0 20070316 boots ok but is thrown into an infinite loop by `hdparm -Tt /dev/hda'. Sysrq-P tells me that the call chain in the loop is:
__up_read or __handle_mm_fault [varies]do_sparc64_faultsparc64_realfault_commoncompat_sys_shmat
The kernel works when built with gcc-3.4.6, 4.0.4, or 4.1.2.
I'll do some more debugging next week, if I have time.
/Mikael
From: Mikael Pettersson <[email protected]>
Date: Wed, 4 Apr 2007 09:23:07 +0200 (MEST)
Quoted 4 lines
The 2.6.21-rc5 kernel compiled with gcc-4.2.0 20070316 boots ok but is thrown into an infinite loop by `hdparm -Tt
/dev/hda'. Sysrq-P tells me that the call chain in the loop is:
Quoted 8 lines
__up_reador__handle_mm_fault[varies]do_sparc64_faultsparc64_realfault_commoncompat_sys_shmatThe kernel works when built with gcc-3.4.6, 4.0.4, or 4.1.2.
I'll do some more debugging next week, if I have time.
Any updates?
Quoted 12 lines · David Miller
From: Mikael Pettersson <[email protected]>
Date: Wed, 4 Apr 2007 09:23:07 +0200 (MEST)Quoted 9 lines
The 2.6.21-rc5 kernel compiled with gcc-4.2.0 20070316 boots ok but is thrown into an infinite loop by `hdparm -Tt
/dev/hda'. Sysrq-P tells me that the call chain in the loop is:
__up_reador__handle_mm_fault[varies]do_sparc64_faultsparc64_realfault_commoncompat_sys_shmat
Quoted 5 lines
Quoted 3 lines
The kernel works when built with gcc-3.4.6, 4.0.4, or 4.1.2.
I'll do some more debugging next week, if I have time.
Any updates?
Yes, but I can't yet reproduce the problem in a user-mode test case, or pinpoint the exact object code that broke.
In ipc/compat.c:compat_sys_shmat():
long compat_sys_shmat(int first, int second, compat_uptr_t third, int version,
void __user *uptr)
{
int err;
unsigned long raddr;
compat_ulong_t __user *uaddr;
if (version = 1)
return -EINVAL;
err = do_shmat(first, uptr, second, &raddr);
if (err < 0)
return err;
uaddr = compat_ptr(third);
return put_user(raddr, uaddr);
}The syscall originated from a 32-bit process, and `third' is a high user-space pointer 0xffxxxxxx. If I insert a printk("uaddr %#lx third %#x\n", (long)uaddr, third) just before the final put_user(), I find that with gcc-4.2.0 uaddr will be 0xffffffffffxxxxxx, i.e. sign-extended from third, and this causes sparc64_realfault_common() to loop. With gcc-4.1.2 uaddr is 0x00000000ffxxxxxx and put_user() works.
Both gcc-4.2.0 and gcc-4.1.2 compile compat_ptr(u32) as a nop, so it seems that something earlier in the call chain must have left a sign-extended value in the argument register for third'. And true enough, in arch/sparc64/kernel/sys32.S we find that the entry for sys32_ipc() explicitly sign-extends %o1/%o2/%o3. %o3 is passed on as-is to u32 third' in compat_sys_ipc() and `compat_uptr_t third' in compat_sys_shmat().
I don't know the sparc64 ABI rules for how the high 32 bits of a 64-bit register containing a 32-bit value are supposed to be (sign-extended, zero-extended, or undefined), but it seems that u32 values should be zero-extended, which would make the sign-extension in sys32.S broken.
At this point I'm lost. I don't even understand how this could have worked with gcc < 4.2.0, much less where exactly gcc-4.2.0 broke things.
/Mikael
From: Mikael Pettersson <[email protected]>
Date: Fri, 13 Apr 2007 11:23:48 +0200
Quoted 14 lines
Both gcc-4.2.0 and gcc-4.1.2 compile
compat_ptr(u32)as a nop, so it seems that something earlier in the call chain must have left a sign-extended value in the argument register forthird'. And true enough, in arch/sparc64/kernel/sys32.S we find that the entry for sys32_ipc() explicitly sign-extends %o1/%o2/%o3. %o3 is passed on as-is tou32 third' incompat_sys_ipc()and `compat_uptr_t third' incompat_sys_shmat().I don't know the sparc64 ABI rules for how the high 32 bits of a 64-bit register containing a 32-bit value are supposed to be (sign-extended, zero-extended, or undefined), but it seems that u32 values should be zero-extended, which would make the sign-extension in
sys32.Sbroken.
Quoted 3 lines
At this point I'm lost. I don't even understand how this could have worked with gcc < 4.2.0, much less where exactly gcc-4.2.0 broke things.
The system call dispatch code for 32-bit syscalls clears the upper 32-bits of all argument registers before calling down into the system call implementations. In your case that would be the sys32.S code.
Look at arch/sparc64/kernel/entry.S:linux_sparc_syscall32 where we do the "srl x, 0, y" instructions which clear the upper 32-bits, moving input argument values to the appropriate output argument registers in parallel.
The caller is supposed to sign/zero extend arguments, as needed, in the sparc ABI.
compat_sys_ipc() is a mess of sign and zero extension, and I'm not surprised there is a bug here :-)
I think the fix is to eliminate the sys32.S sys32_ipc entry, and replace the sys32_ipc references in arch/sparc64/kernel/systbls.S with compat_sys_ipc. compat_sys_ipc does all the necessary sign extensions, so if we just use the default zero-extended values the right thing will happen.
Could you test that fix?
Quoted 21 lines · David Miller
From: Mikael Pettersson <[email protected]>
Date: Fri, 13 Apr 2007 11:23:48 +0200Quoted 14 lines
Both gcc-4.2.0 and gcc-4.1.2 compile
compat_ptr(u32)as a nop, so it seems that something earlier in the call chain must have left a sign-extended value in the argument register forthird'. And true enough, in arch/sparc64/kernel/sys32.S we find that the entry for sys32_ipc() explicitly sign-extends %o1/%o2/%o3. %o3 is passed on as-is tou32 third' incompat_sys_ipc()and `compat_uptr_t third' incompat_sys_shmat().I don't know the sparc64 ABI rules for how the high 32 bits of a 64-bit register containing a 32-bit value are supposed to be (sign-extended, zero-extended, or undefined), but it seems that u32 values should be zero-extended, which would make the sign-extension in
sys32.Sbroken.Quoted 3 lines
At this point I'm lost. I don't even understand how this could have worked with gcc < 4.2.0, much less where exactly gcc-4.2.0 broke things.
Quoted 8 lines
The system call dispatch code for 32-bit syscalls clears the upper 32-bits of all argument registers before calling down into the system call implementations. In your case that would be the
sys32.Scode.Look at arch/sparc64/kernel/entry.S:linux_sparc_syscall32 where we do the "srl x, 0, y" instructions which clear the upper 32-bits, moving input argument values to the appropriate output argument registers in parallel.
Hmm:
entry.S:linux_sparc_syscall32 zero-extends (srl) %o3 (and others) and calls sys32.S:sys32_ipc, which sign-extends (sra) %o3 (and others) and jumps to compat_sys_ipc(), which passes %o3 on as-is to an unsigned int in compat_sys_shmat().
This looks buggy. I just don't understand how it could have worked before.
Quoted 6 lines
The caller is supposed to sign/zero extend arguments, as needed, in the sparc ABI.
compat_sys_ipc()is a mess of sign and zero extension, and I'm not surprised there is a bug here :-)
Quoted 7 lines
I think the fix is to eliminate the
sys32.Ssys32_ipcentry, and replace thesys32_ipcreferences inarch/sparc64/kernel/systbls.Swithcompat_sys_ipc.compat_sys_ipcdoes all the necessary sign extensions, so if we just use the default zero-extended values the right thing will happen.Could you test that fix?
Yes, I'll test that in the beginning of next week when I'm back to where my sparc64 box is.
/Mikael
From: Mikael Pettersson <[email protected]>
Date: Fri, 13 Apr 2007 13:11:04 +0200
Quoted 7 lines
Hmm:
entry.S:linux_sparc_syscall32 zero-extends (srl) %o3 (and others) and calls sys32.S:sys32_ipc, which sign-extends (sra) %o3 (and others) and jumps tocompat_sys_ipc(), which passes %o3 on as-is to an unsigned int incompat_sys_shmat().This looks buggy. I just don't understand how it could have worked before.
I wonder the same thing. Perhaps gcc was chopping off the high bits somehow with other versions.
Indeed, looking at the assembler for compat_sys_ipc() with gcc-4.0.x I get:
sra %o1, 0, %o0 ! first, first sra %o2, 0, %o1 ! second, second srl %o3, 0, %o2 ! third, third sra %o4, 0, %o3 ! version, version srl %g3, 0, %o4 ! ptr, ptr or %o7, %g0, %g1 call compat_sys_shmat, 0 ! or %g1, %g0, %o7
Thus, it is clearing %o3 explicitly.
I bet this is an optimization, because due to the arg passing rules of the ABI on sparc64 it should not need to zero extend those arguments declared as u32 at all, yet it is above :)
Quoted 2 lines
Yes, I'll test that in the beginning of next week when I'm back to where my sparc64 box is.
Thank you.
In the mean time it is clear to me this is a bug and I will push this fix after I do some light testing of my own.
Thanks again Mikael.