2001-10-02 15:15:53

by Dinesh Gandhewar

[permalink] [raw]
Subject:


Hello,
I have written a linux kernel module. The linux version is 2.2.14.
In this module I have declared an array of size 2048. If I use this array, the execution of this module function causes kernel to reboot. If I kmalloc() this array then execution of this module function doesnot cause any problem.
Can you explain this behaviour?
Thnaks,
Dinesh



2001-10-02 15:46:03

by Alan

[permalink] [raw]
Subject: Re: your mail

> I have written a linux kernel module. The linux version is 2.2.14.
> In this module I have declared an array of size 2048. If I use this array, the execution of this module function causes kernel to reboot. If I kmalloc() this array then execution of this module function doesnot cause any problem.
> Can you explain this behaviour?

Yes
--
Alan

[Oh wait you want to know why...]

Either

1. You are using it for DMA
2. You are putting it on the stack and causing a stack overflow

Subject: Re:



--On Tuesday, October 02, 2001 3:29 PM +0000 Dinesh Gandhewar
<[email protected]> wrote:

> In this module I have declared an array of size 2048. If I use this
> array, the execution of this module function causes kernel to reboot. If
> I kmalloc() this array then execution of this module function doesnot
> cause any problem.

If you are allocating it on the stack (i.e. as a local variable)
you are probably running out of kernel stack space (depending
what it's an array of).

If you are declaring it non-local, it's possible you are
overwriting the end of it, and, kmalloc() being what it
is, there happens to be some wasted space next to it.

--
Alex Bligh

2001-10-02 15:49:14

by Tommy Reynolds

[permalink] [raw]
Subject: Re:

"Dinesh Gandhewar" <[email protected]> was pleased to say:

> I have written a linux kernel module. The linux version is 2.2.14.
> In this module I have declared an array of size 2048. If I use this array, the
> execution of this module function causes kernel to reboot. If I kmalloc() this
> array then execution of this module function doesnot cause any problem.
> Can you explain this behaviour?

Unlike userland application programming, the kernel stack does not grow: it has
a fixed size. You are using too much stack space and corrupting your system.
The kernel stack is quite small (less than 8K is available for ALL nested
modules and interrupt handlers), so driver functions should use an absolute
minimum of local variables, such as a pointer to a per-instance data area.
Kernel-leval kmalloc() is efficient enough to use frequently.

---------------------------------------------+-----------------------------
Tommy Reynolds | mailto: <[email protected]>
Red Hat, Inc., Embedded Development Services | Phone: +1.256.704.9286
307 Wynn Drive NW, Huntsville, AL 35805 USA | FAX: +1.236.837.3839
Senior Software Developer | Mobile: +1.919.641.2923

2001-10-02 16:17:20

by Michael H. Warfield

[permalink] [raw]
Subject: Re: your mail

On Tue, Oct 02, 2001 at 03:29:45PM -0000, Dinesh Gandhewar wrote:

> Hello,
> I have written a linux kernel module. The linux version is 2.2.14.
> In this module I have declared an array of size 2048. If I use this
array, the execution of this module function causes kernel to
reboot. If I kmalloc() this array then execution of this module
function doesnot cause any problem.
> Can you explain this behaviour?

You didn't say how you declared the array or what the element
size was. If the array elements were larger than a char, by saying an
array of size 2048, do you mean in bytes or in array elements?

You also didn't say where you called your module from. Was it
in an interrupt handler or at insmod time or from a system call.

If it was a automatic array on the stack (declared inside the
function and not declared static), you probably overflowed the stack.

> Thnaks,
> Dinesh

Mike
--
Michael H. Warfield | (770) 985-6132 | [email protected]
(The Mad Wizard) | (678) 463-0932 | http://www.wittsend.com/mhw/
NIC whois: MHW9 | An optimist believes we live in the best of all
PGP Key: 0xDF1DD471 | possible worlds. A pessimist is sure of it!

2001-10-02 16:16:40

by Richard B. Johnson

[permalink] [raw]
Subject: Re: your mail

On 2 Oct 2001, Dinesh Gandhewar wrote:

>
> Hello,
> I have written a linux kernel module. The linux version is 2.2.14.
> In this module I have declared an array of size 2048. If I use this
> array, the execution of this module function causes kernel to reboot.
> If I kmalloc() this array then execution of this module function
> doesnot cause any problem.
> Can you explain this behaviour?
> Thnaks,
> Dinesh

I would check that you are not accidentally exceeding the bounds of
your array. Actual allocation occurs in page-size chunks. You may
be exceeding your 2048 byte-limit without exceeding the 4096-byte
page-size (of ix86).

However, a global array, or an array on the stack, has very strict
limits. You can blow things up on the stack by exceeding an array
boundary by one byte. And you can overwrite important memory objects
by exceeding the bounds of a global memory object.


Cheers,
Dick Johnson

Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).

I was going to compile a list of innovations that could be
attributed to Microsoft. Once I realized that Ctrl-Alt-Del
was handled in the BIOS, I found that there aren't any.