Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 16: Page and Buffer Cache


Consider the situation in which a process has read a file linearly from position A to position B. Then
this practice will usually continue for a while. It therefore makes sense to read ahead of B (say, until
position C) such that when requests for pages between B and C are issued from the process, the data are
already contained in the page cache.


Naturally readahead cannot be tackled by the page cache alone, but support by the VFS and mem-
ory management layers is required. In fact, the read-ahead mechanism was discussed in Sections 8.5.2
and 8.5.1. Recall that readahead iscontrolled from three places as far as the kernel is directly concerned^6 :



  1. do_generic_mapping_read, a generic read routine in which most filesystems that rely on the
    standard routines of the kernel to read data end up at some point.

  2. Thepagefaulthandlerfilemap_fault, which is responsible to read missing pages for mem-
    ory mappings.

  3. __generic_file_splice_read, a routine invoked to support thesplicesystem call that
    allows for passing data between two file descriptors directly in kernel space, without the
    need to involve userspace.^7


The temporal flow of readahead routines on the source code level were discussed in Chapter 8, but it is
also instructive to observe the behavior from a higher level. Such a viewpoint is provided in Figure 16-4.
For the sake of simplicity, I restrict my consideration todo_generic_mapping_readin the following.


page accessed, but not present

Pages read by asynchronous readahead
asynchronous

async_readahead in background

file_ra_state->start PG_Readahead

file_ra_
state->size

page_cache_async_readahead

page_cache_sync_readahead

file_ra_state->
async_size

....


Figure 16-4: Overview of the readahead mechanism and the required interplay
between VFS and page cache.

Suppose that a process has opened a file and wants to read in the first page. The page is not yet contained
in the page cache. Since typical users will not only read in a single page, but multiple sequential


(^6) These are at least the places covered in this book. Readahead can also be influenced from userland with themadvise,fadvice,
andreadaheadsystem calls, but I will not discuss them any further.
(^7) I do not discuss this system call anywhere in more detail, but refer you to the manual pagesplice(2)for more information.

Free download pdf