Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 16: Page and Buffer Cache


buffers. Before the kernel launches any operations to modify or process buffers associated with a
page, it must first check whether buffers are actually present — this is not always the case. It provides
page_has_buffers(page)to do this by checking whether the flag is set. This function is called at very
large number of places in the kernel sources and is therefore worthy of mention.

Interaction


Setting up a link between pages and buffers serves little purpose if there are no benefits for other parts
of the kernel. As already noted, some transfer operations to and from block devices may need to be
performed in units whose size depends on the block size of the underlying devices, whereas many
parts of the kernel prefer to carry out I/O operations with page granularity as this makes things much
easier — especially in terms of memory management.^11 In this scenario, buffers act as intermediaries
between the two worlds.

Reading Whole Pages in Buffers


Let us first look at the approach adopted by the kernelwhen it reads whole pages from a block device, as
is the case inblock_read_full_page. Let’s discuss the sections of interest as seen by buffer implementa-
tion. Figure 16-7 shows the buffer-related function calls that make upblock_read_full_page.

Nothing to do

Yes

No

1

block_read_full_page

!page_has_buffers? create_empty_buffers

get_block

lock_buffer

mark_buffer_async_read

submit_bh

buffer_uptodate?

Iterate over all buffers Not mapped

Iterate over all buffers
which were not uptodate

1

1

Figure 16-7: Code flow diagram for the buffer-related operations of
block_read_full_page.

block_read_full_pagereads a full page in three steps:


  1. The buffers are set up and their state is checked.

  2. The buffers are locked to rule out interference by other kernel threads in the next step.

  3. The data are transferred to the buffers.


The first step involves checking whether buffers are already attached to the page as this is not always
the case. If not, buffers are created using thecreate_empty_buffersfunction discussed a few sections

(^11) I/O operations are usually more efficient if data are read or written in pages. This was the main reason for introducing the BIO
layer that has replaced the old concept based on buffer heads.

Free download pdf