You are on page 1of 13

Memory Management in Linux

David Chang OS Lab., NCTU,Taiwan

Agenda
Initialization Page Fault Handling Page Cache and Swap Cache Swap Area Management Copying Virtual Address Spaces

Initialization
paging_init()
set up the page tables to map in all the available physical memory Kernel has the virtual address from 3G~4G physical address = virtual address - 3G (see figure)

free_area_init()
allocate and initialize the page structures
Linux use a page structure for one physical page all page structures are marked as DMAable and RESERVED

allocate and initialize the swap cache and free area list

Address Mapping in Kernel


0

E.g. 16M

3G

4G Virtual address

Physical address

Initialization
mem_init()
fixup the page structures and make all the available pages free.

Page Fault Handling


do_page_fault()
this routine is a dispatcher that is invoked when page fault occurs Works:
find out the faulting address find out the reason for the fault and call the right routine protection fault: do_wp_page() page-not-present fault: do_no_page()

Page Fault Handling


do_wp_page()
handle write protection fault Copy-On-Write if necessary

do_no_page()
used to page-in a page determine the source of the page
Executable file? Swap area ? Or Anonymous page?

Page Fault Handling


Page-in from an executable file
When an executable file is opened, Linux will attach the file operations for the file. The file operations include a mmap( ) function For UFS, this mmap() function points to generic_file_mmap( ) After opening the file, Linux maps segments by do_mmap( ) do_mmap() will actually call the mmap() file operation to attach the vm_operations for the vma The vm operation contains a nopage( ) which is called when page fault occurs( see filemap_nopage() )

Page Fault Handling


do_mmap()
construct a vma insert the vma->vm_op by calling file->f_op->mmap() brings in pages if VM_LOCKED is specified

filemap_nopage()
look for the page from page cache first read-in the page if cache miss read-ahead if necessary

Swap Area Management


do_swap_page()
determine if the vma that contains the faulting page has swapin() vm_operation call swap_in() if the vma doesnt have swapin() vm operation

swap_in()
the default swap in operation: allocate a free page and get the content of the page from the swap area de-allocate the swap page in swap area!!!!!!!

Swap Area Organization


swap_info A swap area lockmap 1111111111111 1111111111111 111 11100000000 00SWAP-SPACE

Swap Area Management


swap_on()
enable a swap area allocate pages for lockmap and swapmap insert the swap area into the swap list

Copying Virtual Address Spaces


When Linux create a new process by fork(), it will call copy_mm() to copy the address space from the father to the child. The child can share the same address space with its father if the CLONE_VM is specified copy_mm()
new_page_tables( )
allocate a page for PTEs and copy the kernel-part PTEs

dup_mmap( )
duplicate the mmap

You might also like