> For the complete documentation index, see [llms.txt](https://richardweiyang-2.gitbook.io/kernel-exploring/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://richardweiyang-2.gitbook.io/kernel-exploring/nei-cun-guan-li/00-index/03-page_table_fault/21-folio_index.md).

# Folio.index

## linear\_page\_index()

```
static inline pgoff_t linear_page_index(const struct vm_area_struct *vma,
					const unsigned long address)
{
	pgoff_t pgoff;
	pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
	pgoff += vma->vm_pgoff;
	return pgoff;
}
```

其中vm\_start/vm\_pgoff的含义可以参考[vma单个vma的内容](/kernel-exploring/nei-cun-guan-li/00-index/05-vma.md)中的解释。

对于文件映射，得到的就是对应地址在文件中的实际偏移。 对于匿名映射，得到的正常来说就还是address。

## 设置

```
    __handle_mm_fault
        vmf.pgoff = linear_page_index(vma, address)                   // 匿名：虚拟地址；文件：文件内偏移
        ...
        do_anonymous_page
            folio_add_new_anon_rmap
                __folio_set_anon
                    folio->index = linear_page_index(vma, address)    // 也就是对应的进程虚拟地址
        do_fault
	...
            filemap_fault
                index = vmf->pgoff
                __filemap_get_folio(, index, ) -> __filemap_get_folio_mpol(, index, )

                    index = mapping_align_index(mapping, index);      // index对齐文件系统最小order
                    order = __ffs(index);                             // order是从index对齐地址i中获取的
                    folio = filemap_alloc_folio(, order, );           // 再用order去分配folio,所以隐含了index和order对齐的信息

                    filemap_add_folio(folio, index, )
                        __filemap_add_folio(folio, index, )
                            XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio))
                            folio->index = xas.xa_index;              // 实际上就是文件内偏移
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://richardweiyang-2.gitbook.io/kernel-exploring/nei-cun-guan-li/00-index/03-page_table_fault/21-folio_index.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
