SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionGene Chang
 
Linux kernel memory allocators
Linux kernel memory allocatorsLinux kernel memory allocators
Linux kernel memory allocatorsHao-Ran Liu
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBshimosawa
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in LinuxAdrian Huang
 
Memory management in Linux kernel
Memory management in Linux kernelMemory management in Linux kernel
Memory management in Linux kernelVadim Nikitin
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdfAdrian Huang
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File SystemAdrian Huang
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Adrian Huang
 
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 NLKBshimosawa
 
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 KernelAdrian Huang
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdfAdrian 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
 
Memory Management with Page Folios
Memory Management with Page FoliosMemory Management with Page Folios
Memory Management with Page FoliosAdrian Huang
 
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/LinuxVarun Mahajan
 
New Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using TracingNew Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using TracingScyllaDB
 
USENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPFUSENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPFTaeung Song
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)Brendan Gregg
 

Tendances (20)

Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Linux memory
Linux memoryLinux memory
Linux memory
 
Linux kernel memory allocators
Linux kernel memory allocatorsLinux kernel memory allocators
Linux kernel memory allocators
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
 
spinlock.pdf
spinlock.pdfspinlock.pdf
spinlock.pdf
 
Memory management in Linux kernel
Memory management in Linux kernelMemory management in Linux kernel
Memory management in Linux kernel
 
Physical Memory Management.pdf
Physical Memory Management.pdfPhysical Memory Management.pdf
Physical Memory Management.pdf
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...
 
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
 
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
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
 
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)
 
Memory Management with Page Folios
Memory Management with Page FoliosMemory Management with Page Folios
Memory Management with Page Folios
 
Linux Memory
Linux MemoryLinux Memory
Linux Memory
 
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
 
New Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using TracingNew Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using Tracing
 
USENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPFUSENIX Vault'19: Performance analysis in Linux storage stack with BPF
USENIX Vault'19: Performance analysis in Linux storage stack with BPF
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
 

En vedette

Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in LinuxRaghu Udiyar
 
Linux memory consumption
Linux memory consumptionLinux memory consumption
Linux memory consumptionhaish
 
Understanding of linux kernel memory model
Understanding of linux kernel memory modelUnderstanding of linux kernel memory model
Understanding of linux kernel memory modelSeongJae 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 & linuxKyle 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 memorymanagementpradeepelinux
 
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 2010Saleem Ansari
 
Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)George Tzinos
 
Qi -- Lightweight Boot Loader Applied in Mobile and Embedded Devices
Qi -- Lightweight Boot Loader Applied in Mobile and Embedded DevicesQi -- Lightweight Boot Loader Applied in Mobile and Embedded Devices
Qi -- Lightweight Boot Loader Applied in Mobile and Embedded DevicesNational Cheng Kung University
 

En vedette (20)

Memory management in Linux
Memory management in LinuxMemory management in Linux
Memory management in Linux
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Memory management in linux
Memory management in linuxMemory management in linux
Memory management in linux
 
Linux memory consumption
Linux memory consumptionLinux memory consumption
Linux memory consumption
 
Understanding of linux kernel memory model
Understanding of linux kernel memory modelUnderstanding of linux kernel memory model
Understanding of linux kernel memory model
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux/DB Tuning (DevSumi2010, Japanese)
Linux/DB Tuning (DevSumi2010, Japanese)Linux/DB Tuning (DevSumi2010, Japanese)
Linux/DB Tuning (DevSumi2010, Japanese)
 
Christo kutrovsky oracle, memory & linux
Christo kutrovsky   oracle, memory & linuxChristo kutrovsky   oracle, memory & linux
Christo kutrovsky oracle, memory & linux
 
Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Linux File System
Linux File SystemLinux File System
Linux File System
 
Memory management
Memory managementMemory management
Memory management
 
Day 1-1-intro-sw-vuls
Day 1-1-intro-sw-vulsDay 1-1-intro-sw-vuls
Day 1-1-intro-sw-vuls
 
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)
 
Linux memorymanagement
Linux memorymanagementLinux memorymanagement
Linux memorymanagement
 
Penginstalan linux slackware
Penginstalan linux slackware Penginstalan linux slackware
Penginstalan linux slackware
 
Fedora15 lovelock-pres
Fedora15 lovelock-presFedora15 lovelock-pres
Fedora15 lovelock-pres
 
Fedora Embedded at foss.in 2010
Fedora Embedded at foss.in 2010Fedora Embedded at foss.in 2010
Fedora Embedded at foss.in 2010
 
Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)Segmentation fault (Web - Site - Project)
Segmentation fault (Web - Site - Project)
 
Qi -- Lightweight Boot Loader Applied in Mobile and Embedded Devices
Qi -- Lightweight Boot Loader Applied in Mobile and Embedded DevicesQi -- Lightweight Boot Loader Applied in Mobile and Embedded Devices
Qi -- Lightweight Boot Loader Applied in Mobile and Embedded Devices
 
Java sockets
Java socketsJava sockets
Java sockets
 

Similaire à Linux Memory Management

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 3gelfstrom
 
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 - StorageYhal 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 storageCaroline_Rose
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory managementVandana Salve
 
Os solaris memory management
Os  solaris memory managementOs  solaris memory management
Os solaris memory managementTech_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 internalsSisimon 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 UsageTomer Perry
 

Similaire à 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...
 
Dsmp Whitepaper Release 3
Dsmp Whitepaper Release 3Dsmp Whitepaper Release 3
Dsmp Whitepaper Release 3
 
Virtual memory 20070222-en
Virtual memory 20070222-enVirtual memory 20070222-en
Virtual memory 20070222-en
 
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 ...
 
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]
 
CSC1100 - Chapter05 - Storage
CSC1100 - Chapter05 - StorageCSC1100 - Chapter05 - Storage
CSC1100 - Chapter05 - Storage
 
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 ...
 
Massively Parallel Architectures
Massively Parallel ArchitecturesMassively Parallel Architectures
Massively Parallel Architectures
 
My sql with enterprise storage
My sql with enterprise storageMy sql with enterprise storage
My sql with enterprise storage
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory management
 
Shignled disk
Shignled diskShignled disk
Shignled disk
 
Os solaris memory management
Os  solaris memory managementOs  solaris memory management
Os solaris memory management
 
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 ...
 
Windows memory manager internals
Windows memory manager internalsWindows memory manager internals
Windows memory manager internals
 
Os
OsOs
Os
 
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" ...
 
Conceptual framework storage devices (2)
Conceptual framework   storage devices (2)Conceptual framework   storage devices (2)
Conceptual framework storage devices (2)
 
Spectrum Scale Memory Usage
Spectrum Scale Memory UsageSpectrum Scale Memory Usage
Spectrum Scale Memory Usage
 
INFLOW-2014-NVM-Compression
INFLOW-2014-NVM-CompressionINFLOW-2014-NVM-Compression
INFLOW-2014-NVM-Compression
 
Updates
UpdatesUpdates
Updates
 

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