How zero-fill-on demand is implemented in Linux kernel, and where I can disable it?
Asked Answered
A

2

6

When we malloc memory, only virtual memory is available, and it actually pointed to zero-page. The real physical memory will be allocated when we try to write to the malloced memory, at this moment, there will be copy-on-wright that copy zeros from zero-page to physical memory which mapped by page-fault. My problem is, how/where zero-fill-on demand is implemented in linux source code, I want to disable this functionality to do some test. I guess it may happened in page-fault procedure, rather than brk() or mmap().

Similar topics related to zero-fill-on-demand. ZFOD and COW.

Adenosine answered 7/7, 2017 at 10:27 Comment(4)
Why do you want to disable it? If it is for timing tests, perhaps you could force the copy-on-write to happen on the malloc'ed memory before you do the timing tests.Software
Malloc memory happened in user space, but copy-on-write happened in kernel space. I don't think I can have such ability to control it when this happens.Adenosine
See the function prep_new_page() in mm/page_alloc.c. If you comment out the (conditional) call to prep_zero_page() then the pages should remain uninitialized.Tableau
@Ctx, after comment out the prep_zero_page, the OS crashed. I guess the reason it's because other processes need zeroed pages, if we fail to provide such pages, then errors occured. Now, I'm wondering if all pages allocated are from free_list, if so, if I can clean pages before they are put into free_list, and then disable prep_zero_page().Adenosine
R
1

You want to use the MAP_UNINITIALIZED parameter to mmap and enable CONFIG_MMAP_ALLOW_UNINITIALIZED in your kernel compilation.

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).

Rayford answered 5/7, 2018 at 3:25 Comment(0)
P
0

If you want your userspace process to allocate real memory every *alloc call, I think in the next options:

  • If it is for performance reasons, you can replace all calloc calls for malloc+memset so processes will always have a real memory page. However, the kernel could still be able to merge some memory pages.

  • Disable memory overcommit so that every malloc will return the page at the moment. This way, your program will not be able to allocate more memory than available (RAM + swap). See https://www.kernel.org/doc/Documentation/vm/overcommit-accounting

Person answered 7/7, 2017 at 13:57 Comment(1)
The answer given by @Tableau more match my answer, but still thank you for your post.Adenosine

© 2022 - 2024 — McMap. All rights reserved.