CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 4 of 7 · 3 min read

The page cache

In plain words: The kernel does not read file pages from disk every time they are needed. It keeps them in memory in the page cache, and a read or a mapped access takes the page from there if it is present. The page cache is why a file read a second time is fast, and why the "free" memory of a Linux machine is small: the kernel uses spare memory for the cache, and gives it up when programs need it.

The page cache and the mapping use the same frames. When a program first touches a mapped page, the page fault of Level 6 page 7 occurs. The kernel looks for the page in the cache. If it is there, the fault is minor, and only the page table changes. If it is not, the kernel reads it from disk first, and the fault is major. Two processes that map the same file, even a private one that has not written, use the same frame for the same page. The listing of the mappings of any process shows that the C library is mapped that way:

7f4509400000-7f4509428000 r--p 00000000 ... /usr/lib/x86_64-linux-gnu/libc.so.6
7f4509428000-7f45095b1000 r-xp 00028000 ... /usr/lib/x86_64-linux-gnu/libc.so.6
7f45095b1000-7f4509600000 r--p 001b1000 ... /usr/lib/x86_64-linux-gnu/libc.so.6
7f4509604000-7f4509606000 rw-p 00203000 ... /usr/lib/x86_64-linux-gnu/libc.so.6

Each line is one mapping of the file libc.so.6: read-only data, code (r-xp), more read-only data, and writable data (rw-p, private). Every process on the machine shares the same cached frames for the code lines.

What if the file is not a whole number of pages? A test mapped three pages (12,288 bytes) of a 5,000-byte file:

Access Result
m[4999], the last byte of the file 97, the letter a
m[5000], past the end but in the same page 0 (the rest of the last page is zeros)
m[8192], in a page wholly past the end the signal SIGBUS

A page that contains part of the file is allowed, and its tail is zero. A page with no part of the file raises SIGBUS (the signal for a bus error). The same signal arrives if another process shortens the file while it is mapped, or if the disk fails to deliver a page. A reader of the file through read would get an error code instead. A program that maps a file others may change must be ready for SIGBUS, and this is a real cost of the method.

Page cache

The page cache is the part of memory in which the kernel keeps recently used pages of files, so that reads and mapped accesses are served from memory when the page is present, and writes can be delayed and combined before they go to disk.

Example: the second read of the same 256 MiB file in the tests took no disk access, because all its pages were in the page cache.

Check yourself: 1. Why does a Linux machine show little "free" memory? 2. When is a fault on a mapped file page minor, and when major? 3. What happens when a program reads a mapped page that lies wholly past the end of the file?