How the Linux Page Cache Works: Radix Trees, Writeback, and Why read() Hits DRAM

A process asks for a few bytes from a file. The kernel may return them without asking the storage device for anything at all—not because the bytes are in the CPU cache, but because Linux already has the file’s data in memory.

That distinction is the key to understanding the page cache. It is a shared, kernel-managed cache of file contents, connected to files and their offsets. Its lookup structures and writeback rules sit between ordinary file I/O and storage, while CPU caches and application-level caches solve different problems.

The contract: what the page cache does—and does not promise

For buffered I/O, Linux can keep file data in memory and reuse it across reads. The cache is not a private copy owned by each process, nor is it a promise that data will remain resident. Memory pressure can reclaim clean cached data, and a later access may need storage again.

A successful write() normally means the kernel accepted data into its buffering path; it does not, by itself, mean the data has reached persistent storage. Applications that need durability must use the relevant synchronization interfaces and account for filesystem and device behavior.

Three caches, three jobs

  • CPU cache: hardware caches memory cache lines to reduce the cost of CPU loads and stores. It is organized around physical memory access and coherence, not filenames or file offsets. See How CPU Caches Work: Lines, Associativity, and ….
  • Linux page cache: the kernel caches file contents for buffered file I/O and file-backed mappings. Its identity is tied to a file’s page-cache mapping and offset.
  • Database buffer pool: a database-managed cache of database pages, with policy and bookkeeping controlled by the database. Depending on its I/O mode, it may use the operating system’s page cache or arrange direct I/O to avoid that extra layer.

These layers can all be involved in a workload, but one should not be mistaken for another. More background: https://alphatechnology-hub.blogspot.com/2026/08/how-caching-works-beginner-guide-for.html (if this URL is present on the site).

Mechanism: file offset to cached data

A file’s address_space is the kernel object that describes its page-cache mapping and associated operations. Conceptually, the kernel uses a file offset to locate the cache entry covering that part of the file. Modern Linux uses XArray-based indexing for this mapping; older descriptions often call the structure a radix tree. The name changed, but the useful mental model remains: find the cached folio or page for a file and offset.

Linux folios are the unit used by much of the page-cache code. A folio may represent one base page or a larger, multi-page chunk. Older interfaces and explanations commonly say “page”; that remains useful shorthand, but it is not always the exact cache-entry size.

For a buffered read(), the kernel checks the relevant file-offset range in the page cache. On a hit, data is copied from the cached folio into the calling process’s user buffer. On a miss, the filesystem arranges to obtain the data from storage, the data is placed into the cache, and it is then copied to the user buffer. The exact path depends on the filesystem, file type, and I/O conditions.

This copy is why an ordinary buffered read() is not the same thing as mapping the file directly into a process. With mmap(), a file-backed virtual-memory mapping can refer to the same cached file data; faults establish the process’s mapping as needed, and reads and mappings can observe shared page-cache contents subject to normal synchronization rules. See How mmap Works: File-Backed Pages and How Virtual Memory Works.

Readahead: fetching what may be needed next

When access looks sequential, Linux may perform readahead: request nearby file data before the application explicitly asks for each part. The aim is to overlap storage work with computation and reduce stalls. Readahead is a prediction, not a guarantee; random access, access patterns, filesystem behavior, and memory pressure all affect its usefulness.

Writes, dirty data, and writeback

Buffered writes usually update data in the page cache and mark affected cache data dirty. Dirty means the in-memory contents need to be written toward the backing storage; it does not mean that every write immediately starts a device operation.

Linux writeback mechanisms send dirty data to storage. Background flusher activity and pressure from dirty-memory thresholds help keep dirty data from growing without bound; the kernel may also initiate writeback when an application’s progress would otherwise be held up. The exact thresholds and timing are kernel- and configuration-dependent, not fixed promises about a particular delay.

Laptop mode is a power-management policy intended to group disk activity and allow longer idle periods on suitable systems. It can affect when writeback is coordinated, but it is not a replacement for application-level durability requirements.

fsync() asks the system to synchronize a file’s modified data and required metadata to storage according to the filesystem’s and device’s semantics. It is the relevant tool when an application needs a stronger persistence boundary than write() alone. Correct durable-update patterns can also require synchronizing a containing directory when filesystem namespace changes matter.

Why readers share the same pages

The page cache is associated with the file mapping, not duplicated just because another process opens the same file. Two processes doing buffered reads of the same file and offsets can therefore use the same cached folios. Their user buffers are still separate: each read() copies bytes into its caller’s address space. File-backed mappings can also map the cache data into multiple processes, subject to mapping and access semantics.

Eviction, drop_caches, and O_DIRECT

The page cache competes for physical memory with other uses. Under memory pressure, Linux can reclaim eligible cache contents; clean file data is generally easier to discard because it can be read again, while dirty data needs writeback before it can be safely reclaimed. Eviction is not a simple rule that the oldest file always disappears, and recently used data is not guaranteed to stay forever.

/proc/sys/vm/drop_caches is an administrative interface for dropping eligible clean page-cache and/or reclaimable slab objects when requested with its supported flags. It is primarily useful for controlled testing and diagnostics, not as routine cache management. Dirty data is not simply thrown away; synchronize first if a test needs a clean starting point.

O_DIRECT requests direct I/O that aims to avoid the page cache for the data path, subject to filesystem and alignment constraints. It is not a universal “faster I/O” switch, and it does not mean the CPU caches are bypassed or that all filesystem metadata and device caching disappear. Mixing direct and buffered access to the same file ranges also needs care.

Common misconceptions

  • “A cache hit means the bytes came from the CPU cache.” No. A page-cache hit means the kernel found file data in memory. CPU caches may participate in the memory access, but they are a separate hardware layer.
  • “Every read() reaches the disk.” Not with buffered I/O when the requested data is already cached. On a miss, storage access may be needed.
  • “Each process gets its own cached copy.” Buffered readers share the file’s page-cache contents; each process’s destination buffer is distinct.
  • “write() means durable.” It usually does not. Use synchronization appropriate to the required persistence guarantees.
  • “The cache is a fixed-size pool that keeps everything.” No. Cache residency changes as memory demand and access patterns change.
  • “Radix tree is the current implementation name.” It appears in older explanations; modern Linux page-cache indexing uses XArray, while folio terminology reflects cache entries that can cover multiple base pages.

Takeaways

  • The page cache maps file offsets to cached file data through a file’s address_space.
  • Buffered read() copies cached data into a user buffer; file-backed mmap() can map the same underlying cached data.
  • Readahead may fetch likely-next data, while dirty-page writeback moves buffered modifications toward storage.
  • write(), fsync(), cache eviction, drop_caches, and O_DIRECT each have distinct semantics; none should be reduced to “the cache is on” or “the cache is off.”

Related reading: https://alphatechnology-hub.blogspot.com/2026/09/how-system-calls-work-user-mode-kernel.html. Also see How Virtual Memory Works.

Next Post Previous Post