Demand paging and copy-on-write

See why a huge program needs frames only for the pages it touches, and why a forked child needs a new frame only when a write reaches a shared one. Virtual memory depth · Operating systems.

A text editor has 2 GB of address space counting libraries and buffers, yet one editing session touches about 40 MB of it. The shell that starts the editor forks a child, and the child appears at once, although copying 2 GB would take far longer. How can a machine hold a program that large, and copy it that fast? It does the work late: a page is brought in only when something first touches it, and copied only when someone writes to it.

Three states a page can be in

Follow one process, P, with a page table of four entries. A page is present when a frame of RAM holds it now. It is valid but not present when it belongs to a region P may use, such as its heap, its stack or a mapped file, but no frame holds it yet. It lies outside every region when no part of P owns that address. The kernel keeps a list of regions, and the page table is checked on every access.

For the traces below, assume private mappings, enough free frames, no eviction and one new frame for each newly touched page; real systems can share a zero page on a first read. An access that cannot complete from its entry causes a page fault, a jump into the kernel, which acts on the state of the page and, for writes, on the write permission of the entry.

Demand paging loads on first touch

Demand paging loads a page only when an access needs it. Take P with four pages and four frames, F0 to F3. Page 0 is present in F0. Page 1 belongs to the program's file and is stored in file block S0. Page 2 is a fresh heap page, valid but with no contents yet. Page 3 lies beyond every region.

P reads page 0. It is present, so the read completes and nothing else happens. P then writes W to page 2, which is valid but not present, so the CPU raises a fault. A fresh page has nothing to load, so the kernel takes a free frame, F1, fills it with zeros, marks the entry present and returns to the same write. That instruction runs again, finds a present page and completes, marking the page dirty, changed since loaded; a page only read stays clean. Zero-filling matters: a frame can hold another process's old data, and zeros leak none of it.

P next reads page 1, which has stored contents, so the kernel reads block S0 into the next free frame, F2, before it marks the entry present. It marks it read-only, because a read loaded the page, so a later write still faults. Finally P reads page 3. No region covers it, so the kernel loads nothing and rejects the access as a segmentation fault, which by default ends the process. F0, F1 and F2 now hold pages and F3 is still free: pages P never touches cost no frame.

A page is loaded when it is first touched

Read the steps as text

  1. P has 4 pages: 1 present, 2 valid but not present and 1 outside every region. The machine has 4 frames, 1 in use. The file holds 1 copy: page 1 of P in S0.
  2. P reads page 0. The entry is valid and present, so the read completes from frame F0.
  3. P writes W to page 2, which is valid but not present, so the CPU raises a page fault. The kernel fills free frame F1 with zeros, marks the entry present and runs the access again, which completes and marks the page dirty.
  4. P reads page 1, which is valid but not present, so the CPU raises a page fault. The kernel reads page 1's copy from S0 into free frame F2, marks the entry present and read-only and runs the access again, which completes.
  5. P reads page 3, which is outside every region, so the kernel rejects it as a segmentation fault. No frame is filled and the page table does not change.

A fork shares frames

Now the shell forks. Copying every present page would need a new frame for each; instead the kernel gives the child a copy of the parent's page table and nothing else. Take P with page 0 in F0 holding A and page 1 in F1 holding B, and fork a child C. Both tables now point at F0 and F1, marked read-only in both, and each frame has a reference count, the number of page-table entries that point to it: 2 for F0 and F1. No byte was copied, so the fork finishes almost at once.

Reading is harmless: C reads page 0 and the read completes from F0 with no change. Then C writes X to page 0 and the read-only mark stops the write with a fault. This is copy-on-write: F0 has count 2, so two tables still depend on the contents A. The kernel copies A into the free frame F2, points C's entry at F2 with write permission, and lowers F0 to count 1. The write runs again and completes, F2 holds X, P still sees A in F0, and page 1 is still shared in F1 with count 2.

A fork shares frames and the first write copies one

Read the steps as text

  1. P has 2 pages: 2 present. The machine has 4 frames, 2 in use.
  2. P forks C. C gets a copy of the page table, and every present private writable page is now read-only in both tables. Frames F0 and F1 now have reference count 2.
  3. C reads page 0. The entry is valid and present, so the read completes from frame F0.
  4. C writes X to page 0, but the entry is read-only, so it faults. Frame F0 has reference count 2, so the kernel copies it into free frame F2 and maps the copy writable for C only; F0's count drops to 1. The write then completes.

The count decides

A copy is needed only while someone else still uses the frame. Fork again from P holding D in F0 and E in F1. Then C releases page 0 alone, for example by unmapping only that region, and F0 falls to count 1. Now P writes Z to page 0. Its entry is still read-only from the fork, so the write faults, but no other table can see F0. The kernel only restores write permission, and the write completes in F0 with no copy and no new frame.

P then writes Y to page 1. It is the same writer and the same kind of instruction, but F1 still has count 2 because C maps it, so the write is copied into F2 and F1 drops to count 1.

The reference count decides whether a write copies

Read the steps as text

  1. P has 2 pages: 2 present. The machine has 4 frames, 2 in use.
  2. P forks C. C gets a copy of the page table, and every present private writable page is now read-only in both tables. Frames F0 and F1 now have reference count 2.
  3. C unmaps page 0. Frame F0 now has reference count 1.
  4. P writes Z to page 0, but the entry is read-only, so it faults. Frame F0 has reference count 1, so the kernel only restores write permission, with no copy. The write then completes.
  5. P writes Y to page 1, but the entry is read-only, so it faults. Frame F1 has reference count 2, so the kernel copies it into free frame F2 and maps the copy writable for P only; F1's count drops to 1. The write then completes.

The region decides

The read-only mark from a fork is a device of the fork, not the program's real permission. Take P with page 0 in F0 holding program code, I, in a region that forbids writes, and page 1 in F1 holding data, D. After the fork C, both frames have count 2 and every entry is read-only: page 1 only because of the sharing, page 0 also because of its region.

C writes X to page 1. Its region allows writes, so this is copy-on-write: F1 is copied into F2 for C and falls to count 1. C then writes Y to page 0. Its region forbids writes, so the kernel copies nothing, takes no frame and rejects the write as a segmentation fault; F0 stays at count 2.

A read-only region refuses a write that sharing would only delay

Read the steps as text

  1. P has 2 pages: 2 present. The machine has 4 frames, 2 in use. The file holds 1 copy: page 0 of P in S0.
  2. P forks C. C gets a copy of the page table, and every present private writable page is now read-only in both tables, and a page in a read-only region stays read-only. Frames F0 and F1 now have reference count 2.
  3. C writes X to page 1, but the entry is read-only, so it faults. Frame F1 has reference count 2, so the kernel copies it into free frame F2 and maps the copy writable for C only; F1's count drops to 1. The write then completes.
  4. C writes Y to page 0, which lies in a read-only region, so the kernel rejects it as a segmentation fault. No copy is made, no frame is taken and the page table does not change.

Recap

  • Demand paging loads a valid page on its first touch, then runs the same instruction again; an address outside every region is rejected.
  • In this model, a fork copies the page table and marks private writable pages read-only in both processes; a frame's reference count records how many tables map it.
  • A write to a shared page faults: at count 2 or more the kernel copies the page, at count 1 it restores write permission, and a write the region forbids is rejected.
Where this appears

The laziness changes when the work happens, not what the program sees: every access returns the right bytes, because the faulting instruction runs again once the entry is ready. The forked child costs frames only for the pages that either side writes afterwards.

Fork followed by exec. A shell usually forks and the child promptly starts another program, replacing its memory. Because the fork shared frames instead of copying them, almost nothing was copied before they were thrown away.

Shared mappings. Copy-on-write applies to private memory. A writable region mapped as shared remains shared after a fork, and a write by one is meant to be seen by the other.

Files and anonymous memory. A file-backed page obtains its contents from the file or cache; a fresh anonymous page, such as heap or stack, starts as zeros.

Next: TLB and address-translation cost.