Linux Memory Management
Feb 11, 2002
Yongguang Zhang
Today's Lecture
Last Lecture
- Page Frame
- Memory Zone
- Page Allocation: Buddy System and Free List
- Memory Manage: Slab Cache
- Useful routines:
- kmalloc() and kfree()
- valloc() and vfree()
Swapping and Page Cache
- For Pages in a Process's User Space
- Swap: Secondary Memory on the disk
- Page Cache: Main Memory
- Data Structure: 3 sets of lists
- Active pages, usually mapped by a process's PTE
- active_list (in mm/page_alloc.c)
- Inactive, unmapped, clean or dirty
- inactive_dirty_list (in mm/page_alloc.c)
- Clean pages, unmapped (one list per zone)
- zone_t.inactive_clean_list (in include/linux/mmzone.h)
Kernel Swap Daemon
- Implemented as a kernel thread
- kswapd() (in mm/vmscan.c)
- Wake up periodically
- Wake up more frequently if memory shortage
- Check memory and if memory is tight
- Age pages that have not be used
- Move pages to inactive lists
- Write dirty pages to disk
- Swap pages out if necessary
More kswapd()
- Call swap_out() to scan inactive page lists
- Removes page reference from process's page table
- Actual swapping is done independently by file I/O
- Call refill_inactive_scan() to
- Scan the active_list to find unused page
- Call age_page_down() to reduce page->age count
- If page->age is zero, move to inactive_dirty_list
- Call page_launder() to clean dirty pages:
- Scan inactive_dirty_list for dirty pages, write to disk
- Move clean pages to the zone's inactive_clean_list
Life Cycle of a Page in Cache
- A page is read into memory from disk
- Caused by page-fault, or read-ahead
- Added to page cache: active_list
- Page is "dirty" if written by the process
- If not used, page->age count gradually reduced
- If page->age is zero, moved to inactive_dirty_list
- May be moved to an inactive_clean_list later
- Page can be recovered from an inactive list
- Inactive page can be released
- reclaim_page() to free page for used by others
Process Address Space
- Linear Addresses in the Virtual Address Space
Address Space in ELF Format
- Compile-Time Arrangement:
- Code: start_code (usually 0x0) to end_code
- Data: start_data to end_data
- BSS (Heap): start_brk to brk (growable)
- Run-Time Arrangement:
- Stack: start_stack (growable)
- Arguments: arg_start to arg_end
- Environment: env_start to env_end (0xbfffffff)
Process Memory Descriptor
- One per process (in include/linux/sched.h)
struct mm_struct {
struct vm_area_struct * mmap;
...
pgd_t * pgd;
...
unsigned long start_code, end_code, start_data, end_data;
unsigned long start_brk, brk, start_stack;
unsigned long arg_start, arg_end, env_start, env_end;
...
};
VM Area
VM Area Handlers
- To Create VM Area and Do the Mapping
- do_mmap() (in include/linux/mm.h)
- Used in system calls execve(), brk()
- To Release a VM Area and Shrink the Address Space
- do_munmap() (in mm/mmap.c)
- Used in system calls brk()
- To Find a VM Area that includes a given address
- find_vma() (in mm/mmap.c)
Page Fault
- Exception handler
- Raised by address translation (hardware)
- Call do_page_fault() to handle this interrupt
- do_page_fault()
- Architecture-specific
- i386: arch/i386/mm/fault.c
- Find a page frame in the physical memory
- Swap the missing page in
- Update the page tables
do_page_fault()
...
vma = find_vma(mm, address);
if (!vma)
goto bad_area;
if (vma->vm_start <= address)
goto good_area;
...
good_area:
...
handle_mm_fault(mm, vma, address, write);
...
bad_area:
...
force_sig_info(SIGSEGV, &info, tsk);
...
Memory Management Summary
- Physical Page Management
- Page Table Structure
- Memory Zones and Page Allocation
- Kernel Memory Management
- Slab and Allocation Routines
- Virtual Memory Management (Process Address Space)
- Page Cache and Swapping
- Memory Layout and VM Area
- Page Fault
Next Lecture
- Process Management
- Time to review related topics in CS372
- Process, thread
- Context switch
- Scheduling
More about Projects 4-8
- Choose 5 out of the following 8 projects
- 1) KP4L: Exercise 6 (Shared Memory)
- 2) KP4L: Exercise 7 (Virtual Memory)
- 3) KP4L: Exercise 8 (Synchronization)
- 4) KP4L: Exercise 9 (Scheduler)
- 5) KP4L: Exercise 10 (Device Driver)
- 6) inode undelete (File System) (see course web site)
- 7) Packet Dropper (Networking)
- 8) IPsec (Special Topic)
Choosing Projects
- Based on Your Interests
- Not to Guess which one is Harder
© 2002 Yongguang Zhang