gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:48:48 26 4
gpt4 key购买 nike

我在深入研究内核源代码时注意到了这个函数 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com