内核如何知道虚拟地址空间中的哪些页面对应于换出的物理页面框架? [英] How does kernel know, which pages in the virtual address space correspond to a swapped out physical page frame?

查看:171
本文介绍了内核如何知道虚拟地址空间中的哪些页面对应于换出的物理页面框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情况:内核耗尽了物理RAM,需要换出一个页面.它选择最近最少使用的页面框架,并希望将其内容换出到磁盘上,并将该框架分配给另一个进程.

Consider the following situation: the kernel has exhausted the physical RAM and needs to swap out a page. It picks least recently used page frame and wants to swap its contents out to the disk and allocate that frame to another process.

令我困扰的是,该页面框架通常已经映射到多个进程的多个(相同)页面.内核必须以某种方式找到所有这些进程,并将页面标记为换出.它是如何实现的?

What bothers me is that this page frame was already mapped to, generally speaking, several (identical) pages of several processes. The kernel has to somehow find all of those processes and mark the page as swapped out. How does it carry that out?

谢谢.

问题的插图:

在交换过程1和2具有共享的页面1之前,该页面1驻留在物理内存帧1中:

Before the swapping processes 1 and 2 had a shared Page 1, which resided in the physical memory frame 1:

现在,系统中的内存已用尽,内核通过从帧1换出页面1并将其替换为页面2来为进程3分配内存. 为此,它必须

Now, the memory in the system is exhausted and kernel allocates memory for process 3 by swapping out Page 1 from frame 1 and replacing it with Page 2. In order to do that, it has to

1)找到所有流程,请参阅第1页(在本例中为流程1和流程2)

1) find all the processes, referring to Page 1 (Process 1 and Process 2 in our case)

2)修改其页面表条目,将当前"位设置为0,然后在交换"中​​设置页面1的位置

2) modify their Page Table Entries, setting "Present" bit to 0 and setting the Page 1 location in Swap

所以,我不知道如何执行步骤1.内核不能只是迭代地查看每个进程的页表以找到指向表1的页表项.应该有某种从页框到页表项的反向映射.

So, I don't get, how the step 1 is carried out. Kernel couldn't be just iteratively looking into every process's Page Tables in order to find the Page Table Entry, pointing to frame 1. There should be some kind of reverse mapping from page frames to Page Table Entries.

答案是:

页表管理的最重要和最重要的变化是引入了反向映射(rmap).将其称为"rmap"是有意的,因为它是缩写"的常见用法,不应与之混淆. Rik van Riel开发的-rmap树,除了反向映射之外,对库存VM的更改更多.

"The most significant and important change to page table management is the introduction of Reverse Mapping (rmap). Referring to it as "rmap" is deliberate as it is the common usage of the "acronym" and should not be confused with the -rmap tree developed by Rik van Riel which has many more alterations to the stock VM than just the reverse mapping.

在单个句子中,rmap赋予了定位仅在struct页面的情况下映射特定页面的所有PTE的能力.在2.4中,查找映射共享页面的所有PTE(例如内存映射的共享库)的唯一方法是线性搜索属于所有进程的所有页面表.这太昂贵了,Linux尝试通过使用交换高速缓存来避免该问题(请参阅第11.4节).这意味着,对于许多共享页面,Linux可能不得不换掉整个进程,而不考虑页面年龄和使用模式.相反,2.6具有与每个结构页面相关联的PTE链,可以遍历该结构以从引用该结构的所有页面表中删除该页面.这样,LRU中的页面可以以一种智能的方式换出,而无需求助于交换整个过程."

In a single sentence, rmap grants the ability to locate all PTEs which map a particular page given just the struct page. In 2.4, the only way to find all PTEs which map a shared page, such as a memory mapped shared library, is to linearaly search all page tables belonging to all processes. This is far too expensive and Linux tries to avoid the problem by using the swap cache (see Section 11.4). This means that with many shared pages, Linux may have to swap out entire processes regardless of the page age and usage patterns. 2.6 instead has a PTE chain associated with every struct page which may be traversed to remove a page from all page tables that reference it. This way, pages in the LRU can be swapped out in an intelligent manner without resorting to swapping entire processes."

来自了解Linux内存管理,"Linux2.6的新功能"

from Understanding the Linux Memory Management, "what's new in Linux2.6"

推荐答案

Linux:

使用交换文件时,页面表条目将更新为一个标记为无效的条目,并保存有关交换文件中保存位置的信息.也就是说:swap_info数组的索引和swap_map内的偏移量.

When swap file is used the Page Table Entry gets updated with one marked as invalid and holding information about where it is saved in the swap file. That is: an index to the swap_info array and an offset within the swap_map.

x86上的页面表条目类型(pte_t)的示例(有点旧).一些 的位被硬件用作标志:

Example from (an a bit old) Page Table Entry type (pte_t) on a x86. Some of the bits are used as flags by the hardware:

Bit         Function
_PAGE_PRESENT   Page is resident in memory and not swapped out
_PAGE_PROTNONE  Page is resident but not accessable
_PAGE_RW        Set if the page may be written to
_PAGE_USER      Set if the page is accessible from user space
_PAGE_DIRTY     Set if the page is written to
_PAGE_ACCESSED  Set if the page is accessed

表3.1:页面表条目保护和状态位

另请参见

See also another SO answer with a diagram of the x86-64 page table format. When the low bit = 0, the hardware ignores all the other bits, so the kernel can use them for anything. Even in a "present" entry, there are some guaranteed-ignored bits that aren't reserved for future hardware use, so the kernel can use them for its own purposes.

大概其他架构是相似的.

Presumably other architectures are similar.

简单来说:进程指向页面,页面被更新.因此,实际上,过程也被更新.当请求物理页面时,它将被交换,因此所有进程也将被交换.关键是换出内存后,页表条目不会被删除.

In simple terms: A process points to a page, the page get updated. Thus the processes are, in effect, also updated. When the physical page get requested it is swapped in and thus all processes as well. The point being that the Page Table Entry is not removed when memory is swapped out.

您可能会发现其中一些有用的东西:

You might find some of this useful:

内核文档包括梅尔·高曼(Mel Gorman)的书(2007):

虚拟机页面的生存期.

这篇关于内核如何知道虚拟地址空间中的哪些页面对应于换出的物理页面框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆