Linux Memory
Management - CMA
Fundamentals of Linux Memory
 What is Virtual Memory and Physical
Memory ?
 Why Virtual Memory Required ?
 What is difference between swap
memory(space) and virtual memory.
Contd…
 What is contiguous and Non contiguous
Memory? Why Contiguous memory required?
 What is Highmem and Lowmem in linux
kernel.
 What is Static Memory/Allocation and
Dynamic Memory/Allocation.
Contd…
 Address Types in Linux System.
• User Virtual Addresses
• Physical Address
• Bus Address
• Kernel Logical Address
• Kernel Virtual Address
Contd…
 Memory Limits of 32-bit and 64-bit System.
 What is PAE (Physical Address Extension) ?
 Zones in Memory, Zone Types in linux kernel.
 What is Fragmentation, Types of Fragmentation.
Contd…
a. Internal Fragmentation b. External Fragmentation
Contd…
 What is MMU (Memory Management Unit)
Contd…
 Kmalloc() and Vmalloc()
 What is slab, slob and slub
 SLOB: K&R allocator (1991-1999)
 SLAB: Solaris type allocator (1999-2008)
 SLUB: Unqueued allocator (2008-today)
Contd…
 What is Cache Memory.
Cache memory, also called CPU memory, is high-speed static random access memory
(SRAM) that a computer microprocessor can access more quickly than it can access
regular random access memory (RAM).
Contd…
 What is TLB(Translation Look aside Buffers)
1. Translation Lookaside Buffer (TLB) is nothing but a special cache used to keep track
of recently used transactions. TLB contains page table entries that have been most
recently used.
2. Given a virtual address, the processor examines the TLB if a page table entry is
present (TLB hit), the frame number is retrieved and the real address is formed. If
a page table entry is not found in the TLB (TLB miss), the page number is used to
index the process page table.
3. TLB first checks if the page is already in main memory, if not in main memory a
page fault is issued then the TLB is updated to include the new page entry.
Contd…
 Difference Between Cache and TLB
Paging
 Paging is a memory management technique that allows non-contiguous
memory allocations to processes, and avoids the problem of external
fragmentation.
 Physical memory is divided into discrete units called pages or frames.
 PAGE_SIZE = 4kb and 8kb
 Page Tables - the processor must have a mechanism for translating virtual
addresses into its corresponding physical addresses. This mechanism is called
a page table.
Contd…
 The 32 bits of a linear address are divided into three fields:
1. Directory - The most significant 10 bits
2. Table - The intermediate 10 bits
3. Offset - The least significant 12 bits
 Paging in PAE.
 Extended Paging- 10 bits –Directory 22 bits –Offset and HugePages/HugeTLB.
 Paging for 64-bit architecture – Hierarchical Paging(Multilevel Paging), Hashed
Paging and Inverted Paging
Contd…
Contd…
Contd…
Contd…
Buddy Memory Allocator and Compaction
 In a buddy system, the entire memory space available for allocation is
initially treated as a single block whose size is a power of 2. When the first
request is made, if its size is greater than half of the initial block then the
entire block is allocated.
Contd…
 Compaction is the process to move all the processes towards one end of
memory and all the free block of memory towards other end. (Memory
Defragmentation).
Segmentation
 A process is divided into Segments. The chunks that a program is divided into
which are not necessarily all of the same sizes are called segments.
Segmentation gives user’s view of the process which paging does not give.
 Segment Table – It maps two-dimensional Logical address into one-
dimensional Physical address.
Contd…
 Advantages of Segmentation –
No Internal fragmentation.
Segment Table consumes less space in comparison to Page table in paging.
 Disadvantage of Segmentation –
As processes are loaded and removed from the memory, the free memory
space is broken into little pieces, causing External fragmentation.
Page Fault and Page Replacement Policy
 A page fault occurs when a program attempts to access data or code that is in
its address space, but is not currently located in the system RAM.
Contd…
 Page Replacement Policy
FIFO, Optimal Page Replacement and LRU
 What is Demand Paging
The process of loading the page into memory on demand (whenever page
fault occurs) is known as demand paging.
DMA and IOMMU
 DMA (direct memory access) is a hardware feature that allows memory access to
occur independent of the program currently run by the micro processor.
1. It can either be used by I/O devices to directly read from or write to memory without
executing any micro processor instructions during the DMA transfer.
2. The micro processor can execute an unrelated program at the same time. Additionally,
DMA can be used for efficient copy of memory blocks, again concurrently with any
running program
 IOMMU (input–output memory management unit) is a hardware feature that
extends DMA access for I/O devices. It is similar to the MMU used to map virtual
memory addresses to physical memory address.
1. While the normal MMU is used to give each process its own virtual address space, the
IOMMU is used to give each I/O device its own virtual address space.
2. That way, the I/O device sees a simple contiguous address space, possibly accessible
with 32 bit addresses while in reality the physical address space is fragmented and
extends beyond 32 bit.
Contd…
ION Google Memory Manager
 Ion is a next-generation memory manager available from Google. It is designed to
support various types of memory allocation in a unified API set. Ion is a modular
and extensible design that can represent different hardware resources and their
restrictions.
 It provides API for Both Kernel Space and User Space.
 Key Ion terms are :-
1. Heap – A location from which to get memory allocations; these allocations can be
contiguous, discontinuous, or from system memory or nonsystem memory.
2. Client – A user of Ion; there is a unique client for every hardware block and user space
process. The client represents that block’s view of memory. Every caller of memory will
need to use an Ion client. A client can have a series of buffers. ion client is exposed only
to kernel modules. User space uses just the file descriptor (fd) and handles that are
returned from IOCTL calls.
3. Handle – Abstraction of a buffer, The actual buffer is traced internally in kernel code.
Contd…
 Types Of HEAP -
1. ION_HEAP_TYPE_SYSTEM – Virtually contiguous memory space; physically could be
discontinuous. The low-level memory alloc comes from the vmalloc() API in the
Linux kernel.
2. ION_HEAP_TYPE_SYSTEM_CONTIG – Physically contiguous memory space; the size of
alloc is bounded by OS memory alloc service. The low-level memory alloc comes
from the kmalloc() API in the Linux kernel.
3. ION_HEAP_TYPE_CARVEOUT – PMEM style preallocated memory chunk; physically
contiguous.
4. ION_HEAP_TYPE_IOMMU – Memory region allocated through the IOMMU API
Contd…
 Earlier Malloc was an inefficient way to allocate memory in Kernel Space &
User Space developed in C++.
 If a program required 10 memory allocation, malloc used to provide it 16
because of its fragments as 4, 8, 16, 32. So chip-makers developed their own
drivers like PMEM, Tegra, CMEM but often due to bad growth infrastructure
and development it caused the Apps to get too slow or freeze.
 What ION does is - it manages the memory more efficiently by creating and
managing a Memory pool and allocating it via Dynamic Memory Allocations.
 What is OOM (Out of Memory)
When the system cannot allocate a page, oom killer is invoked to kill the
processes on their badness score.
CMA (Contiguous Memory Allocation)
 As discussed already why contiguous memory allocation required ?
 There are two ways for a device driver to allocate a contiguous buffer:
1. The device driver can allocate a chunk of physical memory at boot-time. This is
reliable because most of the physical memory would be available at boot-time.
However, if the I/O device is not used, then the allocated physical memory is just
wasted.
2. A chunk of physical memory can be allocated on demand, but it may be difficult to
find a contiguous free range of the required size. The advantage, though, is that
memory is only allocated when needed.
CMA solves this exact problem by providing the advantages of both of these approaches
with none of their downsides
Contd…
 Allocate memory at boot time ,Simple to do (eg. via memblock), waste of
memory when device is not in use.
Contd…
 Dynamic Allocation -
 Allocate when device is opened, Uses memory more efficiently, memory
fragmentation problem
(bigger memory allocations = bigger fragmentation problems)
Example • Free memory (>90%) • Requested allocation (~30%) • Result: FAIL
Contd…
Contd…
 Movable, Reclaimable, Isolate and Unmovable are the Page Migrate Types in
Kernel
 Pageblocks
 MIGRATE_CMA – only movable pages can be allocated from this pageblock.
Contd…
Contd…
 Roughly speaking, migration consists of:
allocating a new page, copying contents of the old page to the new page, updating all
places where old page was referred, and freeing the old page.
 Problems with CMA -
1. get_user_pages() makes migration impossible. ext4 does not support migration of
journal pages. Some filesystems are not good on migration.
2. Allocation Failure – Direct Pinning and Indirect Pinning
3. Function Overhead and Write back cost
GCMA(Guaranteed CMA)
 To overcome cma problems Minchan Kim(Maintainer of CMA) and Seonjae Park(Research Scientist)
Suggested Idea.
The concept behind a guaranteed CMA (GCMA) can be described using a simple metaphor of rental rooms with
different classes of client:
1. Rooms are exclusively owned by a manager.
2. The manager rents the rooms out to clients. Each client is classified as primary or secondary-class.
3. The manager reclaims the rooms of secondary-class clients if primary class clients request the rooms.
 Basic idea of gcma is as same as cma's. It reserves large contiguous memory area during boot and
use it for contiguous memory allocator while let it be allocated for 2nd-class clients to keep memory
efficiency. If the pages allocated for 2nd-class clients necessary for contiguous allocation(doubtless
1st-class client), discard or migrate them.
 Difference with cma is choice and operation of 2nd-class client. In gcma, 2nd-class client should
allocate pages from the reserved area only if the allocated pages Mets following conditions.
1. Out of Kernel.
2. Quickly discardable or migratable
Contd..
 GCMA Chooses Frontswap and Cleancache two candidates for Second Class Client.
 Transcendent Memory(tmem)
tmem is used in the kernel with capability to utilize memory that it cannot enumerate,
sometime cannot track and cannot directly address. Tmem size is dynamic.
 Frontswap – it allows the linux subsystem to use transcendent memory When
available, in place of sending data to and from swap device, it just result faster
swapping.
1. Writethrough Mode
2. Writeback Mode
 Cleancache – Kernel fetches pages from slow disk, and retain copy of pages in
memory for reused, if any changes in data It will written to disk also, this are
clean pages and this grows number of cleanpages in page cache and kernel reclaim
cleanpages when required more memory. For tmem rather than discarding page
data put it into tmem is called cleancache.
Contd…
Contd…
 Limitations and Optimization for GCMA
1. Reclamation is required as it consume processing power, Rate of Reclaimation
increases as size of reserved memory area greater.
2. GCMA chooses Frontswap to writethrough mode to avoid slow write back, but it
degrades system performance because it consumes I/O resources.
 ZRAM- it is compressed block device in RAM, it is used as swap device. Google
Official recommendation as swap device in android devices to improve system
performance for low memory systems.
Contribution to Linux Kernel
 Improve Quality of Code and Enhance Existing Algorithms in Subsystems or
Drivers.
 Fix Bugs or Report the bug.(bugzilla.kernel.org)
 Complete the TO DO’s and View history of patches.
 Subscribe the Kernel Mailing List Kernel-newbie and lkml.
Questions & Answers
 Pankaj Suryawanshi

Linux Memory Management with CMA (Contiguous Memory Allocator)

  • 1.
  • 2.
    Fundamentals of LinuxMemory  What is Virtual Memory and Physical Memory ?  Why Virtual Memory Required ?  What is difference between swap memory(space) and virtual memory.
  • 3.
    Contd…  What iscontiguous and Non contiguous Memory? Why Contiguous memory required?  What is Highmem and Lowmem in linux kernel.  What is Static Memory/Allocation and Dynamic Memory/Allocation.
  • 4.
    Contd…  Address Typesin Linux System. • User Virtual Addresses • Physical Address • Bus Address • Kernel Logical Address • Kernel Virtual Address
  • 5.
    Contd…  Memory Limitsof 32-bit and 64-bit System.  What is PAE (Physical Address Extension) ?  Zones in Memory, Zone Types in linux kernel.  What is Fragmentation, Types of Fragmentation.
  • 6.
    Contd… a. Internal Fragmentationb. External Fragmentation
  • 7.
    Contd…  What isMMU (Memory Management Unit)
  • 8.
    Contd…  Kmalloc() andVmalloc()  What is slab, slob and slub  SLOB: K&R allocator (1991-1999)  SLAB: Solaris type allocator (1999-2008)  SLUB: Unqueued allocator (2008-today)
  • 9.
    Contd…  What isCache Memory. Cache memory, also called CPU memory, is high-speed static random access memory (SRAM) that a computer microprocessor can access more quickly than it can access regular random access memory (RAM).
  • 10.
    Contd…  What isTLB(Translation Look aside Buffers) 1. Translation Lookaside Buffer (TLB) is nothing but a special cache used to keep track of recently used transactions. TLB contains page table entries that have been most recently used. 2. Given a virtual address, the processor examines the TLB if a page table entry is present (TLB hit), the frame number is retrieved and the real address is formed. If a page table entry is not found in the TLB (TLB miss), the page number is used to index the process page table. 3. TLB first checks if the page is already in main memory, if not in main memory a page fault is issued then the TLB is updated to include the new page entry.
  • 11.
  • 12.
    Paging  Paging isa memory management technique that allows non-contiguous memory allocations to processes, and avoids the problem of external fragmentation.  Physical memory is divided into discrete units called pages or frames.  PAGE_SIZE = 4kb and 8kb  Page Tables - the processor must have a mechanism for translating virtual addresses into its corresponding physical addresses. This mechanism is called a page table.
  • 13.
    Contd…  The 32bits of a linear address are divided into three fields: 1. Directory - The most significant 10 bits 2. Table - The intermediate 10 bits 3. Offset - The least significant 12 bits  Paging in PAE.  Extended Paging- 10 bits –Directory 22 bits –Offset and HugePages/HugeTLB.  Paging for 64-bit architecture – Hierarchical Paging(Multilevel Paging), Hashed Paging and Inverted Paging
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    Buddy Memory Allocatorand Compaction  In a buddy system, the entire memory space available for allocation is initially treated as a single block whose size is a power of 2. When the first request is made, if its size is greater than half of the initial block then the entire block is allocated.
  • 19.
    Contd…  Compaction isthe process to move all the processes towards one end of memory and all the free block of memory towards other end. (Memory Defragmentation).
  • 20.
    Segmentation  A processis divided into Segments. The chunks that a program is divided into which are not necessarily all of the same sizes are called segments. Segmentation gives user’s view of the process which paging does not give.  Segment Table – It maps two-dimensional Logical address into one- dimensional Physical address.
  • 21.
    Contd…  Advantages ofSegmentation – No Internal fragmentation. Segment Table consumes less space in comparison to Page table in paging.  Disadvantage of Segmentation – As processes are loaded and removed from the memory, the free memory space is broken into little pieces, causing External fragmentation.
  • 22.
    Page Fault andPage Replacement Policy  A page fault occurs when a program attempts to access data or code that is in its address space, but is not currently located in the system RAM.
  • 23.
    Contd…  Page ReplacementPolicy FIFO, Optimal Page Replacement and LRU  What is Demand Paging The process of loading the page into memory on demand (whenever page fault occurs) is known as demand paging.
  • 24.
    DMA and IOMMU DMA (direct memory access) is a hardware feature that allows memory access to occur independent of the program currently run by the micro processor. 1. It can either be used by I/O devices to directly read from or write to memory without executing any micro processor instructions during the DMA transfer. 2. The micro processor can execute an unrelated program at the same time. Additionally, DMA can be used for efficient copy of memory blocks, again concurrently with any running program  IOMMU (input–output memory management unit) is a hardware feature that extends DMA access for I/O devices. It is similar to the MMU used to map virtual memory addresses to physical memory address. 1. While the normal MMU is used to give each process its own virtual address space, the IOMMU is used to give each I/O device its own virtual address space. 2. That way, the I/O device sees a simple contiguous address space, possibly accessible with 32 bit addresses while in reality the physical address space is fragmented and extends beyond 32 bit.
  • 25.
  • 26.
    ION Google MemoryManager  Ion is a next-generation memory manager available from Google. It is designed to support various types of memory allocation in a unified API set. Ion is a modular and extensible design that can represent different hardware resources and their restrictions.  It provides API for Both Kernel Space and User Space.  Key Ion terms are :- 1. Heap – A location from which to get memory allocations; these allocations can be contiguous, discontinuous, or from system memory or nonsystem memory. 2. Client – A user of Ion; there is a unique client for every hardware block and user space process. The client represents that block’s view of memory. Every caller of memory will need to use an Ion client. A client can have a series of buffers. ion client is exposed only to kernel modules. User space uses just the file descriptor (fd) and handles that are returned from IOCTL calls. 3. Handle – Abstraction of a buffer, The actual buffer is traced internally in kernel code.
  • 27.
    Contd…  Types OfHEAP - 1. ION_HEAP_TYPE_SYSTEM – Virtually contiguous memory space; physically could be discontinuous. The low-level memory alloc comes from the vmalloc() API in the Linux kernel. 2. ION_HEAP_TYPE_SYSTEM_CONTIG – Physically contiguous memory space; the size of alloc is bounded by OS memory alloc service. The low-level memory alloc comes from the kmalloc() API in the Linux kernel. 3. ION_HEAP_TYPE_CARVEOUT – PMEM style preallocated memory chunk; physically contiguous. 4. ION_HEAP_TYPE_IOMMU – Memory region allocated through the IOMMU API
  • 28.
    Contd…  Earlier Mallocwas an inefficient way to allocate memory in Kernel Space & User Space developed in C++.  If a program required 10 memory allocation, malloc used to provide it 16 because of its fragments as 4, 8, 16, 32. So chip-makers developed their own drivers like PMEM, Tegra, CMEM but often due to bad growth infrastructure and development it caused the Apps to get too slow or freeze.  What ION does is - it manages the memory more efficiently by creating and managing a Memory pool and allocating it via Dynamic Memory Allocations.  What is OOM (Out of Memory) When the system cannot allocate a page, oom killer is invoked to kill the processes on their badness score.
  • 29.
    CMA (Contiguous MemoryAllocation)  As discussed already why contiguous memory allocation required ?  There are two ways for a device driver to allocate a contiguous buffer: 1. The device driver can allocate a chunk of physical memory at boot-time. This is reliable because most of the physical memory would be available at boot-time. However, if the I/O device is not used, then the allocated physical memory is just wasted. 2. A chunk of physical memory can be allocated on demand, but it may be difficult to find a contiguous free range of the required size. The advantage, though, is that memory is only allocated when needed. CMA solves this exact problem by providing the advantages of both of these approaches with none of their downsides
  • 30.
    Contd…  Allocate memoryat boot time ,Simple to do (eg. via memblock), waste of memory when device is not in use.
  • 31.
    Contd…  Dynamic Allocation-  Allocate when device is opened, Uses memory more efficiently, memory fragmentation problem (bigger memory allocations = bigger fragmentation problems) Example • Free memory (>90%) • Requested allocation (~30%) • Result: FAIL
  • 32.
  • 33.
    Contd…  Movable, Reclaimable,Isolate and Unmovable are the Page Migrate Types in Kernel  Pageblocks  MIGRATE_CMA – only movable pages can be allocated from this pageblock.
  • 34.
  • 35.
    Contd…  Roughly speaking,migration consists of: allocating a new page, copying contents of the old page to the new page, updating all places where old page was referred, and freeing the old page.  Problems with CMA - 1. get_user_pages() makes migration impossible. ext4 does not support migration of journal pages. Some filesystems are not good on migration. 2. Allocation Failure – Direct Pinning and Indirect Pinning 3. Function Overhead and Write back cost
  • 36.
    GCMA(Guaranteed CMA)  Toovercome cma problems Minchan Kim(Maintainer of CMA) and Seonjae Park(Research Scientist) Suggested Idea. The concept behind a guaranteed CMA (GCMA) can be described using a simple metaphor of rental rooms with different classes of client: 1. Rooms are exclusively owned by a manager. 2. The manager rents the rooms out to clients. Each client is classified as primary or secondary-class. 3. The manager reclaims the rooms of secondary-class clients if primary class clients request the rooms.  Basic idea of gcma is as same as cma's. It reserves large contiguous memory area during boot and use it for contiguous memory allocator while let it be allocated for 2nd-class clients to keep memory efficiency. If the pages allocated for 2nd-class clients necessary for contiguous allocation(doubtless 1st-class client), discard or migrate them.  Difference with cma is choice and operation of 2nd-class client. In gcma, 2nd-class client should allocate pages from the reserved area only if the allocated pages Mets following conditions. 1. Out of Kernel. 2. Quickly discardable or migratable
  • 37.
    Contd..  GCMA ChoosesFrontswap and Cleancache two candidates for Second Class Client.  Transcendent Memory(tmem) tmem is used in the kernel with capability to utilize memory that it cannot enumerate, sometime cannot track and cannot directly address. Tmem size is dynamic.  Frontswap – it allows the linux subsystem to use transcendent memory When available, in place of sending data to and from swap device, it just result faster swapping. 1. Writethrough Mode 2. Writeback Mode  Cleancache – Kernel fetches pages from slow disk, and retain copy of pages in memory for reused, if any changes in data It will written to disk also, this are clean pages and this grows number of cleanpages in page cache and kernel reclaim cleanpages when required more memory. For tmem rather than discarding page data put it into tmem is called cleancache.
  • 38.
  • 39.
    Contd…  Limitations andOptimization for GCMA 1. Reclamation is required as it consume processing power, Rate of Reclaimation increases as size of reserved memory area greater. 2. GCMA chooses Frontswap to writethrough mode to avoid slow write back, but it degrades system performance because it consumes I/O resources.  ZRAM- it is compressed block device in RAM, it is used as swap device. Google Official recommendation as swap device in android devices to improve system performance for low memory systems.
  • 40.
    Contribution to LinuxKernel  Improve Quality of Code and Enhance Existing Algorithms in Subsystems or Drivers.  Fix Bugs or Report the bug.(bugzilla.kernel.org)  Complete the TO DO’s and View history of patches.  Subscribe the Kernel Mailing List Kernel-newbie and lkml.
  • 41.
    Questions & Answers Pankaj Suryawanshi