skyel
skyel

Reputation: 763

Linux reserved heap size

I have noticed that on a 2.6.32 MIPS kernel the memory layout is always the same. That is a program has the heap starting at 0x10146000 for most of the processes(or at least those that I looked into). Also another similarity is that always the /lib32/ld-2.9.so starts at 2aaa8000.

So basically the heap has reserved in the virtual memory all this size, and I would like to modify it, because it seems that at some point if I run out of virtual memory (in the rest of the address space) mmap will fail without attempting to map in the above mentioned space, although the heap is barely a few MB. Does anybody now where does the kernel set this addresses?

Upvotes: 0

Views: 1793

Answers (1)

jleahy
jleahy

Reputation: 16855

If you look in arch/mips/mm/mmap.c you'll find there are two ways of laying our memory in Linux, which is chosen depends on the return value of mmap_is_legacy, which in turn depends on whether you have enabled an unlimited stack (forces the legacy mode) and whether your binary in compiled which the flag PT_GNU_STACK (not having this set forces compatibility mode). The new layout was added in 2.6.7 and is described at http://lwn.net/Articles/90311/.

To put it simply the old layout looks like this:

| CODE ---- | HEAP ----------> | MMAP ------>  | <-------- STACK |
| 0GB       |                  | 2GB/3         |             2GB |

The new layout looks like this:

| CODE ---- | HEAP ----------> | <------------- MMAP | --- STACK |
| 0GB       |                  |             2GB-8MB |       2GB |

Notice that in the old layout there is a fixed division between the heap and the mmap region, whereas in the new region it's flexible. Chances are you're either running an old kernel that lacks the new mode, or you're running in the compatibility mode I described previously.

Notice the address you found was 0x2AAA8000 is around 2GB/3 (libc is the first thing to be mapped in), and 0x10146000 is around 256MB (which will be enough to fit your program's code, data and uninitialized data segments).

Upvotes: 2

Related Questions