2.6 kernel, switch/case statement and stack usage

Constantine Gavrilov constantine.gavrilov at gmail.com
Thu Mar 22 10:11:25 UTC 2007


Hello:

I have run into an annoying problem writing some kernel code in AS 4 
update 4 environment. I was seeing stack overflows and found the problem.

Some not very long case/switch cases (15-25 statements) are compiled by 
gcc compiler to use between 500 and 2000 bytes of stack space. This even 
for using enums that run from 0 to N without holes (in which case gcc 
optimizer is supposed to generate a table). Kbuild is used to build all 
code,  I do not use stack variables at all (except for 2 or 3 ints and 
function args), and some of  the code is even as simple as calling a 
separate function for each case statement. 

Re-writing the code to use a call function table for consecutive enums 
solved the problem in that specific code -- nearly 0 stack usage.  But 
there is also code that uses kernel non-consecutive enums and does not 
necessarily call functions for case entries (these are also most 
problematic places where compiler "eats" up to 2 KB of stack). Writing 
my own hash implementation and jump table instead of switch/case 
statement seems an annoying ugly hack to me even though I can do it.

Is it a known gcc/kernel 4K stack limitation (i.e. switch/case is a 
problem in 2.6 with 4K stack)? Is there an option to tell gcc "stack is 
small, do not over-use it" ?

My concern is that there is core kernel code affected by this. I do not 
think the problem is specific to AS 4, but will show up in Fedora as well.

I am sure there is no mistake, and the problem is switch/case construct 
and not what is under case (this has been verified).  BTW, using if/else 
if/else if generates nearly identical code and does not solve the stack 
usage problem.

Comments?

-- 
----------------------------------------
Constantine Gavrilov
Kernel Developer
Qlusters Software Ltd
1 Azrieli Center, Tel-Aviv
Phone: +972-3-6081977
Fax:   +972-3-6081841
----------------------------------------




More information about the Fedora-kernel-list mailing list