SlideShare a Scribd company logo
Linux –
Memory Management
    D3 Tsung-Han “Johnny” Lin
12/4/26   Linux - Memory Management   2
Outline
•   Memory in Linux
     –     Dynamic Allocation
     –     Contiguous Allocation




•Memory Allocation
     – Buddy System
     – Zone
     – SLAB Allocator
•   Per-CPU data




         12/4/26                   Linux - Memory Management   3
MMU - Simple History

Early Linux on x86
                                                        Linux 2.5.46
       (MMU)




          Embedded Developer?!




                                     µClinux
                           (Linux for microconcroller)

                                 Jeff Dionne et al.1998

12/4/26                     Linux - Memory Management                  4
All about PAGE


• Physical page -> page frame
     – Page descriptor, page

     – All pages, mem_map




12/4/26          Linux - Memory Management   5
Initialization

                 query or invoke              Kernel
          BIOS




                             machine_specific_memory_setup()

                             setup_memory_map()

                             struct page* mem_map



                   Physical Memory

12/4/26           Linux - Memory Management                    6
Memory




12/4/26   Linux - Memory Management   7
How pages are allocated?



 12/4/26   Linux - Memory Management   8
Kernel API
• alloc_pages

• __get_free_pages

• kmalloc

• …

• But, seriously

12/4/26      Linux - Memory Management   9
HOW?

12/4/26   Linux - Memory Management   10
Hierarchy Overview
App w/ glibc heap




                                                                                   Zone Allocator
                                                            Slab Allocator
                              glibc




                                                                                                       Buddy Allocator
                                                                                                                         MMU




                                          Kernel                                                                                Physical
                                        Subsystems         VM Subsystem                                                         Memory

                                                                               kswapd
App w/ heap




                                                                             bdflush                 Disk
                                                                                                    Driver


User Space                            Kernel Space                                                                       Disk
                    12/4/26                          Linux - Memory Management                                                      11
Memory Zone
Physical Memory (mem_map)




                                                       0x37999999, 896MB

                            ZONE_DMA               ZONE_NORMAL                        ZONE_HIGHMEM
             12/4/26            0x01000000, 16MB    Linux - Memory Management                        12
                                                                                0x40000000, 1GB
Buddy System
zone descriptor
                  free_area[10]                  1024 pages                 ……
      ………




                      ………




                                                  ………
  free_area[11]
                  free_area[4]
                  free_area[3]

                  free_area[2]                     4 pages                  ……
      ………




                  free_area[1]              2 pages                   2 pages         ……
                  free_area[0]          1 page               1 page     1 page   ……

   12/4/26             Linux - Memory Management                                 13
Zone, again


• ZONE_DMA, 0 ~ 16 MB

          896MB ?!
• ZONE_NORMAL, 16 MB ~ 896 MB

• ZONE_HIGHMEM, rest of them …



12/4/26     Linux - Memory Management   14
Kernel View
                                 Linear – 4 GB                               Physical
                     TASK_SIZE
                                                 0x0

                                     3 GB

PAGE_OFFSET                                                                  ZONE_HIGHMEM
0xc0000000                                                 0x40000000, 1GB
                                     Kernel
              8 MB                    Image                                   Reserved
                                                         0x37999999, 896MB

                                     mem_map           128 MB

                                                                             ZONE_NORMAL    1 GB
                                      …




                                                          0x01000000, 16MB
          896 MB                   ZONE_NORMAL                                ZONE_DMA
                                        +
                                    ZONE_DMA
                                                 0xffffffff
    12/4/26                                    Linux - Memory Management                     15
Do we need this large?!




12/4/26   Linux - Memory Management   16
SLAB Allocator
• First introduced in
                                    大辞泉
     SunOS 5.4
                              slab/slǽb/
• Implemented in Linux
                              名 詞
     2.2                      1 (石木材金属などの)広い厚板;(材木
                              の)背板.
•    SLAB system              2 (食べ物などの)厚切り
      – SLAB                  a thick slab of apple pie|厚切りのアップル
                              パイ.
      – SLOB, embedded system 3⦅ 俗語⦅     〘野球〙ピッチャーズプレート.
      – SLUB, efficient ver.  4⦅ ~⦅⦅
                                 the        俗語⦅   (病院などの)死体置
                              き台
      – SLQB, per-CPU scaling on the slab|(死体となって)安置されて.
• kmalloc()!

    12/4/26              Linux - Memory Management
                                                     Cache !!   17
Design of SLAB
cache_chain

                slab_full
                                   ……
                                                          page   struct
   cache
                                                                  slab
                                         slab
                                                                 object   Page 1
                                                          page




                                                                  …
    cache
               slab_partial              slab
(kmem_cache)                                                     object
                                                          page




                                                                  …
                                         slab
                                                                 object   Page 2
   cache
                                                          page
               slab_partial
                                   ……

   12/4/26                    Linux - Memory Management                   18
Per-CPU Allocation
•   Request/Release single page
     –    Frequently

•   Per-CPU
     –    Reducing cache invalidation
     –    Reducing locking
     –    A simple data array

•   In 2.4 kernel
     –    get_cpu(), preemption disabled
     –    Access array directly

•   In 2.6 kernel
     –    Wrapper
     –    percpu interface
     –    Cache alignment



12/4/26                           Linux - Memory Management   19
Review
App w/ glibc heap




                                                                                   Zone Allocator
                                                            Slab Allocator
                              glibc




                                                                                                       Buddy Allocator
                                                                                                                         MMU




                                          Kernel                                                                                Physical
                                        Subsystems         VM Subsystem                                                         Memory

                                                                               kswapd
App w/ heap




                                                                             bdflush                 Disk
                                                                                                    Driver


User Space                            Kernel Space                                                                       Disk
                    12/4/26                          Linux - Memory Management                                                      20
Reference
• Linux Kernel Development, 3rd
• Understanding the Linux Kernel, 3rd
     – http://www.makelinux.net/books/ulk3/t
          oc
• Understanding the Virtual Memory
    Manager
     – http://www.kernel.org/doc/gorman/htm
          l/understand/index.html


12/4/26              Linux - Memory Management   21
Ad

More Related Content

What's hot (20)

Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)
Pankaj Suryawanshi
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
Adrian Huang
 
Kdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysisKdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysis
Buland Singh
 
LISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceLISA2019 Linux Systems Performance
LISA2019 Linux Systems Performance
Brendan Gregg
 
Memory management
Memory managementMemory management
Memory management
Adrien Mahieux
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
Kamal Maiti
 
Memory Management with Page Folios
Memory Management with Page FoliosMemory Management with Page Folios
Memory Management with Page Folios
Adrian Huang
 
Linux Systems Performance 2016
Linux Systems Performance 2016Linux Systems Performance 2016
Linux Systems Performance 2016
Brendan Gregg
 
Linux memory
Linux memoryLinux memory
Linux memory
ericrain911
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
shimosawa
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
Adrian Huang
 
Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
Adrian Huang
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
Adrian Huang
 
Reverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux KernelReverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux Kernel
Adrian Huang
 
Linux Performance Profiling and Monitoring
Linux Performance Profiling and MonitoringLinux Performance Profiling and Monitoring
Linux Performance Profiling and Monitoring
Georg Schönberger
 
Linux Network Stack
Linux Network StackLinux Network Stack
Linux Network Stack
Adrien Mahieux
 
Linux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBLinux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKB
shimosawa
 
eBPF Perf Tools 2019
eBPF Perf Tools 2019eBPF Perf Tools 2019
eBPF Perf Tools 2019
Brendan Gregg
 
Kernel_Crash_Dump_Analysis
Kernel_Crash_Dump_AnalysisKernel_Crash_Dump_Analysis
Kernel_Crash_Dump_Analysis
Buland Singh
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
Adrian Huang
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)
Pankaj Suryawanshi
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
Adrian Huang
 
Kdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysisKdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysis
Buland Singh
 
LISA2019 Linux Systems Performance
LISA2019 Linux Systems PerformanceLISA2019 Linux Systems Performance
LISA2019 Linux Systems Performance
Brendan Gregg
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
Kamal Maiti
 
Memory Management with Page Folios
Memory Management with Page FoliosMemory Management with Page Folios
Memory Management with Page Folios
Adrian Huang
 
Linux Systems Performance 2016
Linux Systems Performance 2016Linux Systems Performance 2016
Linux Systems Performance 2016
Brendan Gregg
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
shimosawa
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
Adrian Huang
 
Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
Adrian Huang
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
Adrian Huang
 
Reverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux KernelReverse Mapping (rmap) in Linux Kernel
Reverse Mapping (rmap) in Linux Kernel
Adrian Huang
 
Linux Performance Profiling and Monitoring
Linux Performance Profiling and MonitoringLinux Performance Profiling and Monitoring
Linux Performance Profiling and Monitoring
Georg Schönberger
 
Linux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBLinux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKB
shimosawa
 
eBPF Perf Tools 2019
eBPF Perf Tools 2019eBPF Perf Tools 2019
eBPF Perf Tools 2019
Brendan Gregg
 
Kernel_Crash_Dump_Analysis
Kernel_Crash_Dump_AnalysisKernel_Crash_Dump_Analysis
Kernel_Crash_Dump_Analysis
Buland Singh
 
Slab Allocator in Linux Kernel
Slab Allocator in Linux KernelSlab Allocator in Linux Kernel
Slab Allocator in Linux Kernel
Adrian Huang
 

Viewers also liked (20)

Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in Linux
Raghu Udiyar
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
Anil Kumar Pugalia
 
Memory management in linux
Memory management in linuxMemory management in linux
Memory management in linux
Dr. C.V. Suresh Babu
 
Process' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/LinuxProcess' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/Linux
Varun Mahajan
 
Linux memory consumption
Linux memory consumptionLinux memory consumption
Linux memory consumption
haish
 
Linux Memory
Linux MemoryLinux Memory
Linux Memory
Vitaly Nahshunov
 
Understanding of linux kernel memory model
Understanding of linux kernel memory modelUnderstanding of linux kernel memory model
Understanding of linux kernel memory model
SeongJae Park
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
Suvendu Kumar Dash
 
Linux/DB Tuning (DevSumi2010, Japanese)
Linux/DB Tuning (DevSumi2010, Japanese)Linux/DB Tuning (DevSumi2010, Japanese)
Linux/DB Tuning (DevSumi2010, Japanese)
Yoshinori Matsunobu
 
Christo kutrovsky oracle, memory & linux
Christo kutrovsky   oracle, memory & linuxChristo kutrovsky   oracle, memory & linux
Christo kutrovsky oracle, memory & linux
Kyle Hailey
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
Anil Kumar Pugalia
 
Linux File System
Linux File SystemLinux File System
Linux File System
Anil Kumar Pugalia
 
Memory management
Memory managementMemory management
Memory management
Rajni Sirohi
 
Day 1-1-intro-sw-vuls
Day 1-1-intro-sw-vulsDay 1-1-intro-sw-vuls
Day 1-1-intro-sw-vuls
Stephanie Rogers
 
Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)
David Evans
 
Linux memorymanagement
Linux memorymanagementLinux memorymanagement
Linux memorymanagement
pradeepelinux
 
Penginstalan linux slackware
Penginstalan linux slackware Penginstalan linux slackware
Penginstalan linux slackware
alfianostefano
 
Fedora15 lovelock-pres
Fedora15 lovelock-presFedora15 lovelock-pres
Fedora15 lovelock-pres
Julio Villarreal Pelegrino
 
Fedora Embedded at foss.in 2010
Fedora Embedded at foss.in 2010Fedora Embedded at foss.in 2010
Fedora Embedded at foss.in 2010
Saleem Ansari
 
Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)
George Tzinos
 
Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in Linux
Raghu Udiyar
 
Process' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/LinuxProcess' Virtual Address Space in GNU/Linux
Process' Virtual Address Space in GNU/Linux
Varun Mahajan
 
Linux memory consumption
Linux memory consumptionLinux memory consumption
Linux memory consumption
haish
 
Understanding of linux kernel memory model
Understanding of linux kernel memory modelUnderstanding of linux kernel memory model
Understanding of linux kernel memory model
SeongJae Park
 
Linux/DB Tuning (DevSumi2010, Japanese)
Linux/DB Tuning (DevSumi2010, Japanese)Linux/DB Tuning (DevSumi2010, Japanese)
Linux/DB Tuning (DevSumi2010, Japanese)
Yoshinori Matsunobu
 
Christo kutrovsky oracle, memory & linux
Christo kutrovsky   oracle, memory & linuxChristo kutrovsky   oracle, memory & linux
Christo kutrovsky oracle, memory & linux
Kyle Hailey
 
Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)
David Evans
 
Linux memorymanagement
Linux memorymanagementLinux memorymanagement
Linux memorymanagement
pradeepelinux
 
Penginstalan linux slackware
Penginstalan linux slackware Penginstalan linux slackware
Penginstalan linux slackware
alfianostefano
 
Fedora Embedded at foss.in 2010
Fedora Embedded at foss.in 2010Fedora Embedded at foss.in 2010
Fedora Embedded at foss.in 2010
Saleem Ansari
 
Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)
George Tzinos
 
Ad

Similar to Linux Memory Management (20)

Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
peknap
 
Dsmp Whitepaper Release 3
Dsmp Whitepaper Release 3Dsmp Whitepaper Release 3
Dsmp Whitepaper Release 3
gelfstrom
 
Memory Management in Linux-.ppt|FREE DOWNLOAD
Memory Management in Linux-.ppt|FREE DOWNLOADMemory Management in Linux-.ppt|FREE DOWNLOAD
Memory Management in Linux-.ppt|FREE DOWNLOAD
faizanayub16
 
Virtual memory 20070222-en
Virtual memory 20070222-enVirtual memory 20070222-en
Virtual memory 20070222-en
Tetsuyuki Kobayashi
 
Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...
Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...
Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...
vchidambaram
 
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
RootedCON
 
CSC1100 - Chapter05 - Storage
CSC1100 - Chapter05 - StorageCSC1100 - Chapter05 - Storage
CSC1100 - Chapter05 - Storage
Yhal Htet Aung
 
Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...
Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...
Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...
Kuniyasu Suzaki
 
Massively Parallel Architectures
Massively Parallel ArchitecturesMassively Parallel Architectures
Massively Parallel Architectures
Jason Hearne-McGuiness
 
My sql with enterprise storage
My sql with enterprise storageMy sql with enterprise storage
My sql with enterprise storage
Caroline_Rose
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory management
Vandana Salve
 
Os solaris memory management
Os  solaris memory managementOs  solaris memory management
Os solaris memory management
Tech_MX
 
EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...
EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...
EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...
Kuniyasu Suzaki
 
Windows memory manager internals
Windows memory manager internalsWindows memory manager internals
Windows memory manager internals
Sisimon Soman
 
Os
OsOs
Os
Agnas Jasmine
 
Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...
Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...
Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...
Kuniyasu Suzaki
 
Conceptual framework storage devices (2)
Conceptual framework   storage devices (2)Conceptual framework   storage devices (2)
Conceptual framework storage devices (2)
Rajendra Sharma
 
Spectrum Scale Memory Usage
Spectrum Scale Memory UsageSpectrum Scale Memory Usage
Spectrum Scale Memory Usage
Tomer Perry
 
INFLOW-2014-NVM-Compression
INFLOW-2014-NVM-CompressionINFLOW-2014-NVM-Compression
INFLOW-2014-NVM-Compression
Dhananjoy ( Joy ) Das
 
Updates
UpdatesUpdates
Updates
Isa Ansharullah
 
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libra...
peknap
 
Dsmp Whitepaper Release 3
Dsmp Whitepaper Release 3Dsmp Whitepaper Release 3
Dsmp Whitepaper Release 3
gelfstrom
 
Memory Management in Linux-.ppt|FREE DOWNLOAD
Memory Management in Linux-.ppt|FREE DOWNLOADMemory Management in Linux-.ppt|FREE DOWNLOAD
Memory Management in Linux-.ppt|FREE DOWNLOAD
faizanayub16
 
Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...
Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...
Coerced Cache Eviction: Dealing with Misbehaving Disks through Discreet-Mode ...
vchidambaram
 
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
Jaime Peñalba - Kernel exploitation. ¿El octavo arte? [rooted2019]
RootedCON
 
CSC1100 - Chapter05 - Storage
CSC1100 - Chapter05 - StorageCSC1100 - Chapter05 - Storage
CSC1100 - Chapter05 - Storage
Yhal Htet Aung
 
Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...
Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...
Linux Symposium 2009 Slide Suzaki "Effect of readahead and file system block ...
Kuniyasu Suzaki
 
My sql with enterprise storage
My sql with enterprise storageMy sql with enterprise storage
My sql with enterprise storage
Caroline_Rose
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory management
Vandana Salve
 
Os solaris memory management
Os  solaris memory managementOs  solaris memory management
Os solaris memory management
Tech_MX
 
EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...
EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...
EuroSec2012 "Effects of Memory Randomization, Sanitization and Page Cache on ...
Kuniyasu Suzaki
 
Windows memory manager internals
Windows memory manager internalsWindows memory manager internals
Windows memory manager internals
Sisimon Soman
 
Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...
Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...
Linux.Conf.AU 2009 (LCA09) Slide "OS Circular: Internet bootable OS Archive" ...
Kuniyasu Suzaki
 
Conceptual framework storage devices (2)
Conceptual framework   storage devices (2)Conceptual framework   storage devices (2)
Conceptual framework storage devices (2)
Rajendra Sharma
 
Spectrum Scale Memory Usage
Spectrum Scale Memory UsageSpectrum Scale Memory Usage
Spectrum Scale Memory Usage
Tomer Perry
 
Ad

Linux Memory Management

  • 1. Linux – Memory Management D3 Tsung-Han “Johnny” Lin
  • 2. 12/4/26 Linux - Memory Management 2
  • 3. Outline • Memory in Linux – Dynamic Allocation – Contiguous Allocation •Memory Allocation – Buddy System – Zone – SLAB Allocator • Per-CPU data 12/4/26 Linux - Memory Management 3
  • 4. MMU - Simple History Early Linux on x86 Linux 2.5.46 (MMU) Embedded Developer?! µClinux (Linux for microconcroller) Jeff Dionne et al.1998 12/4/26 Linux - Memory Management 4
  • 5. All about PAGE • Physical page -> page frame – Page descriptor, page – All pages, mem_map 12/4/26 Linux - Memory Management 5
  • 6. Initialization query or invoke Kernel BIOS machine_specific_memory_setup() setup_memory_map() struct page* mem_map Physical Memory 12/4/26 Linux - Memory Management 6
  • 7. Memory 12/4/26 Linux - Memory Management 7
  • 8. How pages are allocated? 12/4/26 Linux - Memory Management 8
  • 9. Kernel API • alloc_pages • __get_free_pages • kmalloc • … • But, seriously 12/4/26 Linux - Memory Management 9
  • 10. HOW? 12/4/26 Linux - Memory Management 10
  • 11. Hierarchy Overview App w/ glibc heap Zone Allocator Slab Allocator glibc Buddy Allocator MMU Kernel Physical Subsystems VM Subsystem Memory kswapd App w/ heap bdflush Disk Driver User Space Kernel Space Disk 12/4/26 Linux - Memory Management 11
  • 12. Memory Zone Physical Memory (mem_map) 0x37999999, 896MB ZONE_DMA ZONE_NORMAL ZONE_HIGHMEM 12/4/26 0x01000000, 16MB Linux - Memory Management 12 0x40000000, 1GB
  • 13. Buddy System zone descriptor free_area[10] 1024 pages …… ……… ……… ……… free_area[11] free_area[4] free_area[3] free_area[2] 4 pages …… ……… free_area[1] 2 pages 2 pages …… free_area[0] 1 page 1 page 1 page …… 12/4/26 Linux - Memory Management 13
  • 14. Zone, again • ZONE_DMA, 0 ~ 16 MB 896MB ?! • ZONE_NORMAL, 16 MB ~ 896 MB • ZONE_HIGHMEM, rest of them … 12/4/26 Linux - Memory Management 14
  • 15. Kernel View Linear – 4 GB Physical TASK_SIZE 0x0 3 GB PAGE_OFFSET ZONE_HIGHMEM 0xc0000000 0x40000000, 1GB Kernel 8 MB Image Reserved 0x37999999, 896MB mem_map 128 MB ZONE_NORMAL 1 GB … 0x01000000, 16MB 896 MB ZONE_NORMAL ZONE_DMA + ZONE_DMA 0xffffffff 12/4/26 Linux - Memory Management 15
  • 16. Do we need this large?! 12/4/26 Linux - Memory Management 16
  • 17. SLAB Allocator • First introduced in 大辞泉 SunOS 5.4 slab/slǽb/ • Implemented in Linux 名 詞 2.2 1 (石木材金属などの)広い厚板;(材木 の)背板. • SLAB system 2 (食べ物などの)厚切り – SLAB a thick slab of apple pie|厚切りのアップル パイ. – SLOB, embedded system 3⦅ 俗語⦅ 〘野球〙ピッチャーズプレート. – SLUB, efficient ver. 4⦅ ~⦅⦅ the 俗語⦅ (病院などの)死体置 き台 – SLQB, per-CPU scaling on the slab|(死体となって)安置されて. • kmalloc()! 12/4/26 Linux - Memory Management Cache !! 17
  • 18. Design of SLAB cache_chain slab_full …… page struct cache slab slab object Page 1 page … cache slab_partial slab (kmem_cache) object page … slab object Page 2 cache page slab_partial …… 12/4/26 Linux - Memory Management 18
  • 19. Per-CPU Allocation • Request/Release single page – Frequently • Per-CPU – Reducing cache invalidation – Reducing locking – A simple data array • In 2.4 kernel – get_cpu(), preemption disabled – Access array directly • In 2.6 kernel – Wrapper – percpu interface – Cache alignment 12/4/26 Linux - Memory Management 19
  • 20. Review App w/ glibc heap Zone Allocator Slab Allocator glibc Buddy Allocator MMU Kernel Physical Subsystems VM Subsystem Memory kswapd App w/ heap bdflush Disk Driver User Space Kernel Space Disk 12/4/26 Linux - Memory Management 20
  • 21. Reference • Linux Kernel Development, 3rd • Understanding the Linux Kernel, 3rd – http://www.makelinux.net/books/ulk3/t oc • Understanding the Virtual Memory Manager – http://www.kernel.org/doc/gorman/htm l/understand/index.html 12/4/26 Linux - Memory Management 21