buffer page和page cache

老版本的kernel中,对disk的cache分为两种:

  • page cache:来自disk的数据,以page为单位
  • buffer cache: 对disk block的缓存,主要是来自VFS的访问

从2.4.10开始,buffer cache就不用了,不再单独针对block buffer进行内存分配了。block buffer都放在page中,这些page就叫buffer page,buffer page也保存在page cache中。

Formally, a buffer page is a page of data associated with additional descriptors called “buffer heads,” whose main purpose is to quickly locate the disk address of each individual block in the page. In fact, the chunks of data stored in a page belonging to the page cache are not necessarily adjacent on disk.

可见,buffer page就是带数据的page加上buffer head以定义该page中每个block的信息。

另一段Robert Love在Quora上的回答:
https://www.quora.com/Linux-Kernel/What-is-the-major-difference-between-the-buffer-cache-and-the-page-cache

The page cache caches pages of files to optimize file I/O. The buffer cache caches disk blocks to optimize block I/O.

This is simple to implement, but with an obvious inelegance and inefficiency. Starting with Linux kernel version 2.4, the contents of the two caches were unified. The VM subsystem now drives I/O and it does so out of the page cache. If cached data has both a file and a block representation—as most data does—the buffer cache will simply point into the page cache; thus only one instance of the data is cached in memory. The page cache is what you picture when you think of a disk cache: It caches file data from a disk to make subsequent I/O faster.

The buffer cache remains, however, as the kernel still needs to perform block I/O in terms of blocks, not pages. As most blocks represent file data, most of the buffer cache is represented by the page cache. But a small amount of block data isn’t file backed—metadata and raw block I/O for example—and thus is solely represented by the buffer cache.

buffer_head
buffer head的描述符类型。包含所有内核进行block操作需要的信息,buffer_head包含两方面的信息,一个是block buffer的设备信息,包括所属的块设备以及逻辑块号(logic block number)等,另一个是block buffer在所属的page中的信息,包括在page中的位置等。

Kernel何时会创建buffer page?
两种情况:

  • 文件的数据保存在不连续的block buffer中。这样当数据load到page中时,必须要以block buffer来分别记录各个block buffer的信息。buffer page插入到file对象的radix_tree中
  • 访问单个disk block时,如读取super block和inode block。buffer_page插入到块设备文件(bdev文件系统)的radix_tree中。This kind of buffer pages must satisfy a strong constraint: all the block buffers must refer to adjacent blocks of the underlying block device.

如果判断一个page是不是buffer page?
如果一个page在page cache中,并且page descriptor的PG_private flag设置了,那么page的private域就指向该page的第一个buffer head,表明这就是一个buffer page.

你可能感兴趣的:(kernel,内存分配)