Linux 内核:在 paging_init 时零页分配的作用 [英] Linux kernel: Role of zero page allocation at paging_init time

查看:28
本文介绍了Linux 内核:在 paging_init 时零页分配的作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 arch/arm 启动时的内核内存预留.

I am trying to understand the kernel memory reservation at bootup for arch/arm.

setup_arch() 中有一个调用 paging_init() 用于设置页表、区域内存映射的初始化等.在分配实际的mem_map之前,它还分配了一个零页.

There's a call paging_init() for setting page tables, initialization of zone memory map etc in setup_arch(). It also allocate one zero page before allocating actual mem_map.

void __init paging_init(const struct machine_desc *mdesc)
{
    void *zero_page;
    ---
    zero_page = early_alloc(PAGE_SIZE);
    ---
    empty_zero_page = virt_to_page(zero_page);
    __flush_dcache_page(NULL, empty_zero_page);
}

谁能解释一下零页的作用?

这个问题是这个的一部分.

推荐答案

零页是用零填充的页.您可以映射到此页面并获得宽归零的虚拟区域.每当您写入此页面之一时,COW 都会起作用,您将获得一个新的.反之亦然:如果您有一个零数据的内存区域,您可以将此数据映射到零页并用0"数据释放这些页.换句话说,这是关于内核如何节省内存的.

Zero page is a page filled with zeros. You can make a mapping to this page and get wide zeroed virtual region. Whenever you will write to one of this pages the COW will work and you will get a new one. The reverse is true too: if you have a memory region with zero data, you can map this data to zero page and free these pages with "0" data. In other words that is about how kernel can save memory.

附言注意COW与零页没有直接联系,它是一个更广泛和更笼统的概念

p.s. Note that COW is not directly connected with zero pages, it is a more wide and general concept

来自@artless_noise 的补充:

它还允许分配大数组,但不消耗内存.所有页面最初都是零页面并映射到相同的物理零页面.如果数组是稀疏的,那么只有少数条目(4k 大小)会消耗内存.内核不需要清理(零)分配的内存.'tlb' 和 'cache' 不会浪费在填充条目上.

It also allows a large array to be allocated, but not consume memory. All pages are initially the zero page and map to the same physical zero page. If the array is sparse, then only the few entries (in 4k size) will consume memory. The kernel doesn't need to sanitize (zero) allocated memory. The 'tlb' and 'cache' are not wasted in filling entries.

这篇关于Linux 内核:在 paging_init 时零页分配的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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