Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 16: Page and Buffer Cache


pposis a byte offset into a file, andindexcontains the corresponding page cache offset.

Two auxiliary functions are provided for convenience:

<pagemap.h>
struct page * find_or_create_page(struct address_space *mapping,
pgoff_t index, gfp_t gfp_mask);
struct page * find_lock_page(struct address_space *mapping,
pgoff_t index);

find_or_create_pagedoes what the name promises — it looks up a page in the page cache and
allocates a fresh one if it is not there. The page is inserted into the cache and the LRU list by calling
add_to_page_cache_lru.

find_lock_pageworks likefind_get_page,butlocksthepage.

Caution: If the page is already locked from some other part of the kernel, the function can sleep until the
page is unlocked.

It is also possible to search for more than one page. Here are the prototypes of the responsible auxiliary
functions:

<pagemap.h>
unsigned find_get_pages(struct address_space *mapping, pgoff_t start,
unsigned int nr_pages, struct page **pages);
unsigned find_get_pages_contig(struct address_space *mapping, pgoff_t start,
unsigned int nr_pages, struct page **pages);
unsigned find_get_pages_tag(struct address_space *mapping, pgoff_t *index,
int tag, unsigned int nr_pages, struct page **pages);

❑ find_get_pagesreturns up tonr_pagespages in the mapping starting from the page cache off-
setstart. Pointers to the pages are placed on the arraypages. The function does not guarantee
to return a continuous range of pages — there can be holes for non-present pages. The return
value is the number of pages that were found.
❑ find_get_pages_contigworks similarly tofind_get_pages, but the selected page range is
guaranteed to be continuous. The function stops to add pages to thepagearray when the first
hole is discovered.
❑ find_get_pages_tagoperates likefind_pages, but only selects pages that have a specifictag
set. Additionally, theindexparameter points to the page cache index of the page that immedi-
ately follows the last page in the resulting page array.

16.4.3 Waiting on Pages


The kernel often needs to wait on pages until theirstatus has changed to some desired value. The syn-
chronization implementation, for instance, sometimes wants to ensure that writing back a page has been
finished and the contents in memory are identical with the data on the underlying block device. Pages
under writeback have thePG_writebackbit set.
Free download pdf