Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux, will zeroed page pagefault on first read or on first write?

My question is Linux specific and needs understanding of kernel, virtual memory, mmap, pagefaults. I have C program with large static arrays, which will go into bss section (memory, initialized to zero). When program starts, this memory is not physically allocated, there is only virtual memory, and every page of virtual memory is mapped to the special zero page (the page of all zeroes). When application access this page, the pagefault will be generated and physical page will be allocated.

The question is: Will such pagefault be generated on first read access or on first write access to the page from bss section?

like image 331
osgx Avatar asked Aug 24 '12 19:08

osgx


People also ask

How page fault is handled in Linux?

The mapping between the virtual address space and physical memory is handled by the Linux kernel and by the CPU's MMU using pages of memory. When the CPU needs to access a page that isn't in memory it raises a page fault. A major page fault is one that can only be satisfied by accessing the disk.

Does Linux use demand paging?

Linux uses demand paging to load executable images into a processes virtual memory. Whenever a command is executed, the file containing it is opened and its contents are mapped into the processes virtual memory.

What is page memory in Linux?

June 2019) In computer operating systems, memory paging is a memory management scheme by which a computer stores and retrieves data from secondary storage for use in main memory. In this scheme, the operating system retrieves data from secondary storage in same-size blocks called pages.

What is page fault overhead?

In computing, a page fault (sometimes called PF or hard fault) is an exception that the memory management unit (MMU) raises when a process accesses a memory page without proper preparations. Accessing the page requires a mapping to be added to the process's virtual address space.


1 Answers

Linux allocates a zero page to this memory (one zero page for the whole region) and then will essentially do COW (copy on write) behavior on the page because you are changing the contents. So you will not get read faults (unless the page was swapped out or its a minor page fault which means the page was in memory but not mapped).

So only write faults will cause a fault that causes allocation of a new page on the zero page.

like image 51
Jesus Ramos Avatar answered Sep 24 '22 14:09

Jesus Ramos