[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: When should ralloc.c be used?

From: Daniel Colascione
Subject: Re: When should ralloc.c be used?
Date: Fri, 28 Oct 2016 01:11:08 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0

On 10/28/2016 12:46 AM, Eli Zaretskii wrote:
From: Daniel Colascione <address@hidden>
Cc: Stefan Monnier <address@hidden>,  address@hidden,  address@hidden
Date: Thu, 27 Oct 2016 23:23:05 -0700

We're talking about setting aside address space only, not asking
the OS to make guarantees about future memory availability.  All major
operating systems, even ones like Windows that don't do overcommit,
provide ways to reserve address space without asking the OS to guarantee
availability of memory.

Not sure I understand you: if a portion of the address space has been
reserved, how come these addresses won't be available when we try to
commit them later?  There might not be physical pages available for
that, but virtual memory for those addresses must be available, no?

I'm not sure I understand what you're confused about, so I'll try a broader explanation.

Say I mmap (anonymously, for simplicity) a page PROT_NONE. After the initial mapping, that address space is unavailable for other uses. But because the page protections are PROT_NONE, my program has no legal right to access that page, so the OS doesn't have to guarantee that it can find a physical page to back that page I've mmaped. In this state, the memory is reserved.

The 20GB PROT_NONE address space reservation itself requires very little memory. It's just a note in the kernel's VM interval tree that says "the addresses in range [0x20000, 0x500020000) are reserved". Virtual memory is

Now imagine I change the protections to PROT_READ|PROT_WRITE --- once the PROT_READ|PROT_WRITE mprotect succeeds, my program has every right to access that page; under a strict accounting scheme (that is, without overcommit), the OS has to guarantee that it'll be able to go find a physical page to back that virtual page. In this state, the memory is committed -- the kernel has committed to finding backing storage for that page at some point when the current process tries to access it.

Say you have a strict-accounting system with 1GB of RAM and 1GB of swap. I can write a program that reserves 20GB of address space. That's fine. The kernel isn't promising to give you 20GB of memory: it's setting address space. Now if I attempt to map 20GB PROT_READ|PROT_WRITE, on any reasonable (i..e, not overcommit) system, mmap should fail, since there's no way a system with 1GB of RAM and 1GB of swap can promise to provide 20GB of private memory.

Overcommit confuses the issue: the kernel will _commit_ to as much memory as you ask it for and then renege on that commitment when it finds it convenient. An overcommit system with 1GB of RAM and 1GB of swap will happily let you make that 20GB PROT_READ|PROT_WRITE mapping. It'll just kill you after you use more than 2GB of that mapping. A non-overcommit system understands how to say "sorry, I can't let you do that" up front. On a non-overcommit system, your process will never be killed for accessing memory that the kernel told the process in advance that it could use.

I think Jérémie is working with a mental model where every memory mapping is commit, and only an overcommit system allows you to commit more memory than the system actually has. Any system will let you reserve more address space than you have available commit: reservations are cheap.

(In Windows, the corresponding concepts MEM_RESERVE and MEM_COMMIT. Windows is much more explicit about the difference between memory reservations and memory commitments than Linux is, because most of the time, Linux users don't care about the difference.)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]