Outline of the Linux Memory Management System
This page covers kernel version 2.4.x, and is somewhat
incomplete.
Still, some folks have found it useful.
Very comprehensive coverage of the Linux VM system can be found in Mel
Gorman's
book, Understanding
the Linux Memory Manager
-- JAK
These pages contain my notes on the Linux memory-management
system. It is basically an educational experience on my part; while I
am familiar with virtual memory management in theory, this is my first
practical foray into the field. Hopefully others in the same position
will benefit from my experience. I assume the reader has a general
idea what "virtual memory" is, and is familiar with the notion of
address translation using address-mapping tables (page tables). In the
main document I try to present a reasonably organized view of the MM
system, but there are occasional links into the vmnotes.html
file containing my original
stream-of-consciousness attempts to puzzle out various aspects of the
code.
I am only looking at single-processor systems at the moment,
and
when it's necessary to understand platform-specific issues I
concentrate on Intel architecture because that's what I have
documentation for (though I may add some notes on SH3/SH4 later). Once
I'm sure I understand the single-processor issues, I may look at SMP.
In general, for each topic, I first describe the principles
embodied in the code at a high level; then I go on to interpret the
code in enough detail that I can understand what's going on. If you
have questions that aren't answered by my notes, please let me know so
I can clarify things. I want to provide enough information so that the
code is understandable, but avoid filling pages with fluffy
descriptions of stuff that's obvious by looking at the code.
I have a nifty Tcl
script that
replaces everything that looks like a function, macro, struct,
variable, typedef, filename, or line number with a link into the http://kneuro.net/cgi-bin/lxr/http/source
kernel cross-referencer, so from
almost anywhere in this text you can click to see the relevant
code. Note that the http://kneuro.net/cgi-bin/lxr/http/source links
attempt to open a (single) separate
window for you to view the code, so if you click on a link and
nothing seems to happen, see if you have a hidden or minimized
browser window.
I encourage you to have a look at the Linux MM web site;
there is some good documentation there. Especially, I encourage you to
have a look at (my slightly edited version of)
Paul Wilson's VM outline . That
document is somewhat out-of-date, but still provides a nice overview
of VM issues generally.
I'd love comments on this page, especially from folks who
understand
this stuff :-)
OK, enough yakking, here we go.
The basic principle Linux uses to fairly share memory
resources is
that each physical page of data (each logical page in Paul
Wilson's terminology)
carries with it a measure of its frequency of use, called its
age. The more frequently a page is referenced, the less
likely it is to be discarded by the kernel when memory becomes
scarce. I'll discuss the details of the code and algorithms first, and
then finally describe how it all works together to implement the
memory-management policy.
Here are the main pieces of the Linux MM puzzle:
- Initialization
- How
is the MM system initialized? How is paging initiated? Where do things
stand immediately before the init task is launched? (Note: this
section is quite hardware-specific.)
- Physical
Memory
Management - How does the kernel allocate and free pieces of
physical RAM?
- Kernel
Virtual Memory
Management - When the kernel needs memory for its own use, how
does it acquire that memory? How is it released? What is the
relationship between kernel virtual memory and physical memory?
- The
Page Cache - What is the
page cache? What is its purpose? How does it work?
- Process
Virtual Memory Management - How is process virtual memory
managed? How are pages allocated to a process's virtual address space?
How are they de-allocated? What is the relationship between process
virtual memory and kernel virtual memory? What is the relationship
between process virtual memory and
physical memory?
- Swapping
- How are pages
written to disk and read back in? Under what circumstances does this
occur?
- Memory
Management Policy -
How does the kernel arrange for processes to fairly share the
available memory resources? What algorithms are used to ensure that
the system behaves well under high memory load?
Questions and comments to
Joe
Knapka
The http://kneuro.net/cgi-bin/lxr/http/source
links in this page were produced by lxrreplace.tcl,
which is available for free.