碎碎念
我今天莫名开始纠结起,关于linux页表方面的问题。然后就想到,
【页表本身也是要占有一部分内存的,所以内核又是如何管理页表本身所占有的这部分内存的呢?】
找了很久,没太满意的答案。甚至都没有具体方向,所以也不太有希望从源码中找到答案。
暂时就以在stackoverflow上找到的这个提问,作为这次疑惑的收尾吧,可能要很久很久之后才能得到圆满的解答了。
回复的大致意思是:
【根据linux内核相关wiki,页表就是常驻在内存中的,不会换出。】
也不知道实际是如何的,还是希望有从动态层面上研究过内核的大神,来救救孩子。
问题
Are page tables always in memory? Would page tables be swapped out?
I think page tables is maintained by Linux kernel, so the memory need for page tables should not be swapped out, for the reason that the kernel do not have page fault thing. But, if the pgd, pmd, pte tables are all in main memory and never swapped out, the meaning of multilevel paging can not save any memory.
So I am confused about the how page tables themselves are managed.
回复
well, according to wikipedia, it is true that page tables aren’t swapped out - but swapping out tables isn’t what makes them memory efficient! lets take an example: our physical memory is 4GB in size, every page is 4KB so we would have needed 4MB size table in order to map every address. the solution to this problem isn’t swapping out part of the table, but creating a hierarchy of tables, so you can allocate only the parts in the hierarchy which you need. it is explained quite thoroughly here: Why using hierarchical page tables?