kernel stack problem

kernel stack problem

am 04.04.2007 02:50:15 von Rajat Jain

Hi List,

Recently I got into the problem that I think relates to the small size
of kernel stack. I had a function of the following form:

void func()
{
...
//Block1
{
struct huge_var x;
....
....
}
....
//Block2
{
struct huge_var y;
....
....
}
....
}

The above code resulted in stack overflow. I had an understanding that
since the scope of variables x & y are limited to their blocks only,
the space for them will be allocated and released when the block is
entered and exited respectively. And hence the kernel stack will not
overflow since both x and y will not occupy memory simultaneously (one
instance of the variable can easily fit in the kernel stack).

I solved this problem by having a single instance of struct huge_var
declared immediately at function begining and reusing it in both
blocks. But my question is when (and where) are the block scope
variable allocated? In my previous case, were the two variables
occupying two sperate memory areas simultaneously?

Ah, I understand that I should use kernel stack sparingly ... and will do so.

Thanks,

Rajat
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: kernel stack problem

am 04.04.2007 02:57:35 von Fabiano Ramos

> Hi List,
>
> Recently I got into the problem that I think relates to the small size
> of kernel stack. I had a function of the following form:
>
> void func()
> {
> ...
> //Block1
> {
> struct huge_var x;
> ....
> ....
> }
> ....
> //Block2
> {
> struct huge_var y;
> ....
> ....
> }
> ....
> }
>
> The above code resulted in stack overflow. I had an understanding that
> since the scope of variables x & y are limited to their blocks only,
> the space for them will be allocated and released when the block is
> entered and exited respectively. And hence the kernel stack will not
> overflow since both x and y will not occupy memory simultaneously (one
> instance of the variable can easily fit in the kernel stack).
>

This is wrong. Although the scope is at _block_ level, all variables are
allocated on the stack at _function_ level. So, when entering func() all
variables within it, including x and y, are allocated.

> I solved this problem by having a single instance of struct huge_var
> declared immediately at function begining and reusing it in both
> blocks. But my question is when (and where) are the block scope
> variable allocated? In my previous case, were the two variables
> occupying two sperate memory areas simultaneously?
>
> Ah, I understand that I should use kernel stack sparingly ... and will do so.
> Thanks,
>
> Rajat
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to ecartis@nl.linux.org
> Please read the FAQ at http://kernelnewbies.org/FAQ
>

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: kernel stack problem

am 04.04.2007 03:51:59 von jakj

> This is wrong. Although the scope is at _block_ level, all variables are
> allocated on the stack at _function_ level. So, when entering func() all
> variables within it, including x and y, are allocated.

But this doesn't make sense. Why would the compiler not immediately
optimize both variables into the same allocation? It seems obvious to me
that the amount of space pushed onto the stack when the function is
entered should be the maximum space needed by any combination of local
variables in-scope at any time. GCC wouldn't be that stupid, would it?

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ

Re: kernel stack problem

am 04.04.2007 05:19:55 von Fabiano Ramos

On Tue, 2007-04-03 at 21:51 -0400, John Anthony Kazos Jr. wrote:
> > This is wrong. Although the scope is at _block_ level, all variables are
> > allocated on the stack at _function_ level. So, when entering func() all
> > variables within it, including x and y, are allocated.
>
> But this doesn't make sense. Why would the compiler not immediately
> optimize both variables into the same allocation? It seems obvious to me
> that the amount of space pushed onto the stack when the function is
> entered should be the maximum space needed by any combination of local
> variables in-scope at any time. GCC wouldn't be that stupid, would it?


Yes, I agree. But take a look...

00:19:01 framos@core2duo:~ $> more teste.c
#include

int main()
{
{
int x=5;

printf("x=%d\n",x);
printf("addr(x)=%p\n", &x);

{
int y=10;

printf("y=%d\n",y);
printf("add(y)=%p\n", &y);
}

return 0;
}

00:19:04 framos@core2duo:~ $> ./teste
x=5
addr(x)=0xbf8b0b00
y=10
add(y)=0xbf8b0afc
00:19:06 framos@core2duo:~ $>



-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Re: kernel stack problem

am 04.04.2007 05:25:01 von Fabiano Ramos

On Tue, 2007-04-03 at 21:51 -0400, John Anthony Kazos Jr. wrote:
> > This is wrong. Although the scope is at _block_ level, all variables are
> > allocated on the stack at _function_ level. So, when entering func() all
> > variables within it, including x and y, are allocated.
>
> But this doesn't make sense. Why would the compiler not immediately
> optimize both variables into the same allocation? It seems obvious to me
> that the amount of space pushed onto the stack when the function is
> entered should be the maximum space needed by any combination of local
> variables in-scope at any time. GCC wouldn't be that stupid, would it?

Sorry, the last message was sent before I finished. As I was saying, I
agree, and it depends on the compiler optimization level:

00:21:13 framos@core2duo:~ $> more teste.c
#include

int main()
{

{
int x=5;

printf("x=%d\n",x);
printf("addr(x)=%p\n", &x);
}

{
int y=10;

printf("y=%d\n",y);
printf("add(y)=%p\n", &y);
}

return 0;
}


00:21:21 framos@core2duo:~ $> gcc -o teste teste.c
00:21:24 framos@core2duo:~ $> ./teste
x=5
addr(x)=0xbffb2200
y=10
add(y)=0xbffb21fc



If you use -O2 or -O3, yes, it works as we expect:

00:22:39 framos@core2duo:~ $> gcc -O2 -o teste teste.c
00:22:46 framos@core2duo:~ $> ./teste
x=5
addr(x)=0xbfd9e7ec
y=10
add(y)=0xbfd9e7ec


Cheers!


-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs