More Related Content
Similar to Linux Memory Management
Similar to Linux Memory Management (20)
More from Anil Kumar Pugalia
More from Anil Kumar Pugalia (20)
Linux Memory Management
- 1. © 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Memory Management
- 2. 2© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
W's of Memory Management?
Memory Partitioning
Memory Organization
Memory Management in Linux
Swapping
- 3. 3© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Why Memory Management?
Transition from Uni-process → Multi-process
Every process needs its own memory
Memory Division
Program loaded to “any” available memory address
Relocation
Independence / Non-interference between Processes
Protection
Communication between Processes
Sharing
Process Memory with Attributes (like read only for code)
Logical Organization
Executing Programs bigger than available Memory
Physical Organization (Overlaying & Reusing)
- 4. 4© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What is Memory Management?
Key task: Division of Memory
Starting from the Division between
OS (Kernel)
Application (User)
Others follow
Relocation, Protection, Sharing
Logical & Physical Organization
May involve movement between primary &
secondary
- 5. 5© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Partitioning
Two traditional division mechanisms
Segmentation: Unequal sized
Examples: Linear Allocation, Buddy System
Paging: Equal sized
Examples: 4K-Paged Allocation
Examples in Linux
User-space malloc(): Segmentation
Slab sub-system: Paging
- 6. 6© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Pros & Cons
Memory Fragmentation
External in Segmentation (gets worser)
Internal in Paging
Space Efficiency
Segmentation: More meta but lesser allocation wastage
Paging: Negligible meta but significant allocation wastage
Time Efficiency
Segmentation: Complex search & Storage Algorithm
Paging: Simple Algorithms
- 7. 7© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Organization
All other needs (Relocation, Protection, Sharing, ...)
Beautifully achieved by the concept of virtualization
That is organizing the memory into
Physical & Virtual Address Spaces
And then using the virtual addressing for all purposes
Both may use either Paging or Segmentation
Best to use similar mechanisms
Linux uses Paging for both with 4KiB/8KiB page size
Virtualization can be achieved
Using special hardware called MMU
Using software MMU
- 8. 8© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Organization ...
Linux Kernels (>= 2.5.46) support both
Earlier kernels worked only with hardware MMU
uClinux used to be the solution for non-MMU
processors & controllers
Latest Intel, ARM, and most other architectures
have in-built MMU
Older x86, ARM7, and few such doesn't have a
hardware MMU
- 9. 9© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory (RAM)
Working of an MMU
1
1
0
1
Valid Tag Frame Number
TLB (in Cache)
1
Page Table (in RAM)
1
1
1
0
1
0
1
1
0
1
Page Number [31:12] Offset [11:0] Virtual Address
- 10. 10© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Management in Linux
In Linux, all these combined
User Space & Kernel Space
Memory Page Partitioning
Physical & Virtual Address Spaces
Device Addresses, Mapping & Access
Takes the following form ...
- 11. 11© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Memory Organization in Linux
An Example assuming 32-bit architecture
4GB Process
VA Space 1
User Space
4GB Process
VA Space N
.
.
.
3GB
Virtual
Address
for User
1GB VA
for Kernel
Kernel Space
(fixed by kernel)
Logical Address
(Non-swappable)
Virtual Address
(Swappable)
0x00000000
0xBFFFFFFF
0xFFFFFFFF
0xC0000000
Upto 3GB
Memory
(RAM)
Addressing
Upto 1GB Dev
Addressing
Physical Address Space
(fixed by architecture)
0x00000000
0xBFFFFFFF
0xC0000000
0xFFFFFFFF
Bus Address (incl.
both h/w memory &
registers)
Physical Address
(incl. only memory)
- 12. 12© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Memory Manager
Provides Access Control to h/w & memory resources
Provides Dynamic Memory to kernel sub-system
Drivers
File Systems
Stacks
Provides Virtual Memory to Kernel & User space
Kernel & User Processes run in their own virtual address spaces
Providing the various features of a Linux system
System reliability, Security
Communication
Program Execution Support
- 13. 13© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Swapping
Optimizes primary & secondary memory usage
Primary memory pages swapped out
When running out of memory
Secondary memory pages swapped in
When needed back
Linux maintains Page Cache in Primary Memory for
Code: Reloadable
Data: Unchanged vs Changed
Changed Data: Temporary (Dynamic) vs Persistent (Files)
Linux uses Swap (raw) partition on Secondary Memory
Related commands: mkswap, swapon/off
- 14. 14© 2010-17 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of Memory Management?
Memory Partitioning
Segmentation & Paging
Memory Organization
Physical & Virtual Addressing
Working of an MMU
Memory Management in Linux
Swapping & Swap Partition