memory-management - set_bh_page 对页面缓存中给定的缓冲区头到底做了什么?

标签 memory-management linux-kernel operating-system paging block-device

我在深入研究内核源代码时注意到了这个函数 set_bh_page()。但是,我无法清楚地理解它的作用。

我只能在 fs/buffer.c 文件中找到这条注释:

/* Link the buffer to its page */

set_bh_page(bh, page, offset);

但我仍然不清楚它的作用。

所以,为了说清楚,我想了解这个函数调用与缓冲区和物理页面有什么关系,以及它是否与页面缓存本身有什么关系。

更新 1:

函数alloc_page_buffers()调用了这个set_bh_page(),对此有一些评论,如下:

Create the appropriate buffers when a given a page for data area and the size of each buffer.. User the bh->b_this_page linked list to follow the buffers created. Return NULL if unable to create more buffers.

我检查了谁调用了 alloc_page_buffers(),其中一个是 read_page(),它有这样的描述:

Read a page from a file.

We both read the page, and attach buffers to the page to record the address of each block (using bmap). These addresses will be used to write the block later, completely bypassing the filesystem. This usage is similar to how swap files are handled, and allows us to write to a file with no concerns of memory allocation failing.

所以,通过查看read_page()的源码,我的理解是分配的buffer_head必须关联到它的物理页地址,就像直接映射一样.

对吗?

最佳答案

当内核需要从 block 设备访问 block 并且发现页面缓存中没有包含该 block 的页面时,它会分配一个页面,称为 block 设备缓冲页或者只是一个缓冲页,然后将请求的 block 写入其中。该过程以 grow_buffers 开始函数,调用 alloc_page_buffers ,声明如下:

struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size, bool retry);

page 指向将要保存该 block 的缓冲区页的描述符。 size 表示 block 的大小(以字节为单位),其中缓冲页的所有 block 大小相同。请注意, block 是 block 设备的内存区域,而缓冲区是主内存的内存区域。缓冲区保存单个 block 的数据,并且大小相同。所以缓冲页面看起来像这样:

       .
       .
       .
|-------------|
|    buffer   |
|-------------|
|    buffer   |
|-------------|
|    buffer   |
|-------------|
       .
       .
       .

每个缓冲区中包含的 block 由缓冲区头 标识。你可以找到 buffer_head 的结构声明 here . b_bdevb_blocknr 字段一起标识 block 设备上的 block 。请注意,每个缓冲区头都有一个指向同一缓冲区页内下一个缓冲区头的指针。 alloc_page_buffers 函数分配并初始化指定缓冲页的所有缓冲区的缓冲区头。 alloc_page_buffers 调用 set_bh_page 函数来初始化缓冲区头的两个特定字段,b_pageb_data,这两个字段被描述通过代码中的注释:

struct page *b_page;    /* the page this bh is mapped to */
char        *b_data;    /* pointer to data within the page */

如您所见,它“将缓冲区链接到它的页面”。

关于memory-management - set_bh_page 对页面缓存中给定的缓冲区头到底做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49507256/

相关文章:

java - 是否可以将 Shift、Ctrl、Alt 或 Win 键之一用于其他目的,例如键入文本?

c - 如何提供中断上下文和进程上下文之间的同步

linux - 当操作系统等待用户输入时,CPU 是否仍在执行任何指令?

Python C API 读取列表列表,分配内存和全局变量

javascript - 原始数据方法

iphone - ivars 的另一个内存管理查询

c - 使用多维数组优化 C 以实现性能与内存优化

linux - 直线代码指令导致的开销差异很大

将 urb 转换为 skbuff

linux-kernel - 将物理设备映射到用户空间中的指针