Linux kernel: A Big Picture

Linux-2.6.26


如何从线性地址定位到映射文件的具体某一页面
首先根据线性地址addr可以找到对应的vm_area_struct,其中的vm_pgoff为调用mmap时指定的pgoff参数,表示mmap的起始地址在文件中的偏移,以PAGE_SIZE为单位;
addr-vma->start表示线性地址在vma内的偏移,右移PAGE_SIZE位,得到以PAGE_SIZE为单位的偏移,与前面的vm_pgoff相加,可得到在映射文件中的偏移,记为index;
以该index为参数,在映射文件的mapping指向的address_space里查找page_tree(radix_tree)缓存。
pgoff_t  inner_pgoff = (address - vma->vm_start) >> PAGE_SHIFT;
index = (inner_pgoff + vma->vm_pgoff) >> (PAGE_CACHE_SHIFT - PAGE_SHIFT);


struct page结构中index的作用及reverse mapping
index指明该page在对应的文件中的页偏移,用于reverse mapping时找到对应的线性地址。
当系统页面紧缺需要回收页面时,为释放该page,要找到所有引用该页的pte。
page->mapping->i_mmap字段记录了所有使用到该page的所有进程的vm_area_struct,
通过以下方式可以计算出
pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
代码参见vma_address()函数[mm/rmap.c]
当然,得到address后还要进一步检查该address是否落在vma的区间内,因为可能一个文件做了多次映射,对应有多个vm_area_struct结构。

对于non-linear的mapping,则要对mapping->i_mmap_nonlinear链表的所有vma对应的页表项扫描!
参见《Understanding the Linux Kernel》[Section 17.2 Reverse Mapping >17.2.2.2. The try_to_unmap_file( ) function]


The types of pages considered by the PFRA

(Page Frame Reclaim Algorithm)

Type of pages

Description

Reclaim action

Unreclaimable

Free pages (included in buddy system lists)

Reserved pages (with PG_reserved flag set)

Pages dynamically allocated by the kernel

Pages in the Kernel Mode stacks of the processes

Temporarily locked pages (with PG_locked flag set)

Memory locked pages (in memory regions with VM_LOCKED flag set)

(No reclaiming allowed or needed)

Swappable

Anonymous pages in User Mode address spaces(e.g., all pages in the User Mode heap or stack of a process are anonymous)

Mapped pages of tmpfs filesystem (e.g., pages of IPC shared memory)

Save the page contents in a swap area

Syncable

Mapped pages in User Mode address spaces

Pages included in the page cache and containing data of disk files

Block device buffer pages

Pages of some disk caches (e.g., the inode cache )

Synchronize the page with its image on disk, if necessary

Discardable

Unused pages included in memory caches (e.g., slab allocator caches)

Unused pages of the dentry cache

Nothing to be done









end