Linux Memory Management
Feb 4, 2002
Yongguang Zhang
Coming up soon
Today's Lecture
- Q&A
- Project 2 Results
- Revised Scheduled for Projects
- Kernel Hints
- Memory Management
Q&A
- How is project 3?
- Any other questions
Project 2 Results
Lectures From Now On
- How to deal with Linux kernel in general
- Basics data structures, utility routines
- Learning and problem solving skills
- Topics about different parts of the kernel
- I will go very fast (high level)
- Details you can learn by doing the projects
- Revised Schedule
- This Friday: 8 projects hand-out
- Next week: study the projects, form groups, choose 5
- Project 4-8 due: after next week: 1 per week
More UML Hints
- Increasing memory used by UML
./linux ubd0=...... mem=128M
- Modify files in the UML source code tree
- Some are read-only (to save space)
../_src -> /projects/cs378.ygz/src/linux
linux/arch/i386/kernel/entry.S -> ../../../../_src/arch/i386/kernel/entry.S
- You need to make your own copy in place of the link
cd linux/arch/i386/kernel
mv entry.S entry.S.link
cp -p entry.S.link entry.S
How to Debug a Kernel?
- Add instrument print statements
- NO: printf()
- Use printk()
- Include include/linux/kernel.h
- Example: printk(KERN_DEBUG "i = %u\n", i);
- Note: no ',' between KERN_DEBUG and the format string
- See include/linux/kernel.h for other KERN_ values
- printk() internally uses a 1K buffer and does not check for buffer overflow, so be careful!
- Debugging UML with gdb
- We will do this next time
Helpful Websites
- http://kernelnewbies.org/
- #kernelnewbies is an IRC network dedicated to the "newbie" kernel hacker. The audience mostly consists of people who are learning about the kernel, working on kernel projects or professional kernel hackers that want to help less seasoned kernel people.
(Hacking does NOT mean breaking into other people's system!)
Linux Memory Management
- Memory Model: Demand Paged Virtual Memory
- Time to review related topics in CS372!
- What is virtual memory? What is physical memory?
- What is address translation?
- What is primary memory? What is secondary memory?
- What is paging? What is a page?
- What is page replacement policy?
- What is a "dirty" page?
- What is page fault?
A Very Abstract Model
Architecture Specifies
- Page: Basic unit of memory management
- i386 architecture: page size is 4KB (212)
- Addressable memory space
- 32-bit iX86 architectures: 4GB (232)
- 64-bit Alpha architecture: 8TB (243)
- Memory management highly related to hardware
- Lots of routines implemented in assembly code (why?)
3-Level Page Table
- 3-level of indirect table access to address a page
- Page Global Directory -> Page Middle Directory -> Page Table -> Page
- 32-bit memory address:
- 10-bit (table), 0-bit (table), 10-bit (table), 12-bit (page)
- 43-bit memory address:
- 10-bit, 10-bit, 10-bit, 13-bit (page)
- Data structures and functions
- Look for pgd_t, pmd_t, pte_t
- In include/asm-xxx/: page.h, pgalloc.h, pgtable.h
3-Level Page Table Illustration
Virtual Memory Layout
- User Space
- Code segment, data segment
- Addressable: 0 to PAGE_OFFSET-1
- Kernel Space
- Code segment, data segment
- Shared by all processes
- Addressable: PAGE_OFFSET-1 to (unsigned)(-1)
- In i386 architecture
- PAGE_OFFSET=0xc0000000
- This means: 1GB reserved for kernel, 3GB for user
Memory Mapping
- Map virtual address into physical address
- Kernel space:
- Permanent mapped
- Be careful when you write code in kernel space
- Don't use too much memory
- Never use recursive call (kernel stack is limited too)
- Linux "Virtual" Memory Management
- A clean interface to support different memory mapping used under different architecture
- To start: include/linux/mm.h, linux/mm
Homework
- Review Chapter 2 and 6 of "Understanding the Linux Kernel" book
© 2002 Yongguang Zhang