5

当我们 malloc 内存时,只有虚拟内存可用,它实际上指向零页。当我们尝试写入分配的内存时,将分配真正的物理内存,此时会有copy-on-wright将零从零页复制到由page-fault映射的物理内存。我的问题是,如何/在哪里实现零填充需求在 linux 源代码中,我想禁用这个功能来做一些测试。我猜它可能发生在页面错误过程中,而不是 brk() 或 mmap()。

与按需零填充相关的类似主题。ZFOD奶牛

4

2 回答 2

1

您想使用 MAP_UNINITIALIZED 参数来映射并在内核编译中启用 CONFIG_MMAP_ALLOW_UNINITIALIZED。

MAP_UNINITIALIZED (since Linux 2.6.33) Don't clear anonymous pages. This flag is intended to improve performance on embedded devices. This flag is honored only if the kernel was configured with the CONFIG_MMAP_ALLOW_UNINITIAL‐ IZED option. Because of the security implications, that option is normally enabled only on embedded devices (i.e., devices where one has complete control of the contents of user memory).

于 2018-07-05T03:25:51.477 回答
0

如果您希望您的用户空间进程在每个 *alloc 调用中分配实际内存,我认为在下一个选项中:

  • 如果是出于性能原因,您可以替换 malloc+memset 的所有 calloc 调用,这样进程将始终拥有一个真实的内存页面。但是,内核仍然可以合并一些内存页面。

  • 禁用内存过度使用,以便每个 malloc 都将在此时返回页面。这样,您的程序将无法分配比可用内存更多的内存(RAM + 交换)。请参阅https://www.kernel.org/doc/Documentation/vm/overcommit-accounting

于 2017-07-07T13:57:32.120 回答