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.

Qinchen
  • 381
  • 1
  • 3
  • 14
  • 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. – Ian Abbott Jul 07 '17 at 10:40
  • 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. – Qinchen Jul 07 '17 at 11:31
  • 1
    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. – Ctx Jul 07 '17 at 12:46
  • @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()`. – Qinchen Jul 14 '17 at 05:26

2 Answers2

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

John Meacham
  • 785
  • 7
  • 9
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

eugenioperez
  • 627
  • 7
  • 15