Re: change of policy on mmap?

Linus Torvalds (torvalds@cs.helsinki.fi)
Fri, 19 Jul 1996 13:56:24 +0300 (EET DST)


On Fri, 19 Jul 1996, Peter Van Eynde wrote:
>
> I'm porting a program to Linux that mmaps 128+ MB on startup, but it only
> uses about 16 megs of it. The reasons for this bad design is that it uses
> a copying garbage collector with different "segments" to store data in.
>
> The memory is mmaped rwx, anonymous, private and static. Thus linux 2.0.x
> always checks if there is enough mem+swap for this --- I don't want this
> to happen.
>
> 1 How can I tell the kernel to mmap it, but _not_ to check if it is
> available. How should I modify the mmap call, or could you add another
> flag?

You can trivially alter the kernel not to check for memory availability,
but then the program will obviously only run on your hacked kernel (or on
machines with enough mem+swap). If you want to take this approach, just
uncomment tha calls to "vm_enough_memory(pages)" that exist in
linux/mm/mmap.c (it's used in both sys_brk() and do_mmap(), you can
delete both or just the one that is bothering you).

> 2 To trow away data (copying gc...) I should do mmap with /dev/null,
> right? (or was it /dev/zero)

You can use /dev/null, or alternatively an "anonymous" mapping with a
"fd" of -1, and the MAP_ANONYMOUS flag set. That way you don't need the
open/close stuff at all..

> I remember that a while ago there was a thread on usenet about "malloc
> lies in Linux". It was then said that mallocing 1GB was ok, so long as you
> didn't use the memory. Has policy changed or is mmap(rwx) so different?

It has never been true that you can malloc() as much as you want,
although some people have claimed it to be so. However, mmap() didn't use
to check at all, and that did change with 2.0.

Even now the checks are more "heuristics" than real checks that you can
depend on making sure you don't run out of memory. You can fool the
heuristics if you know what you're doing.

Linus