9
Am meisten gelesen
10
Am meisten gelesen
13
Am meisten gelesen
Linux CMA (Contiguous Memory Allocator)
Mark Veltzer
<mark.veltzer@gmail.com>
Why contiguous memory?
● DMA
● Sharing between operating systems
Why large contiguous memory?
● Brain dead devices (200MB in one DMA
transfer?).
● Devices that uses large memory chunks.
● Performance of scatter/gather.
● kmalloc (4MB on x86, less on other platforms),
gfp (8MB) limitations.
● And again, sharing between operating
systems...
Solutions to the contiguous memory
problem
● Use small hardware buffers (the most common).
● Use scatter gather in hardware or in software (very
common if small buffers are not available – especially in
general availability products – because of the problems of
the next methods and the unavailability of an IOMMU).
● Reserve a large contiguous buffer (a.k.a the old way – try
not to use me if you can...).
● CMA (you are here! - half way to nirvana).
● IOMMU (a.k.a. the best thing since sliced bread – pure
bliss – your company will build a temple in your honor).
Doing it on your own (no CMA)
● First reserve a large buffer for your needs and tell the kernel to keep it's
filthy grubbing hands off your buffer...
● Find an free large area by looking at /proc/iomem. I found 200M free
buffer at address 800M in this example.
● Pass mem=800M memmap=200M$800M to the kernel at boot (this
actually depends on the kernel version, for some kernels only memmap
is needed, ugh!!!).
● Check it by passing bootmem_debug
● See that the allocation worked by looking at:
– dmesg | grep user (no guarantee, kernel log changes all the time)
– cat /proc/iomem (even better than previous).
– /sys/firmware/memmap/*/* (the raw data – can be machine parsed).
Doing it on your own (no CMA).
Continued...
● Now you want your driver/kernel code to access this
buffer

● request_mem_region with the same physical address
you passed as a kernel parameter.
● ioremap
● And just you the buffer you get from ioremap.
● Dont forget to iounmap and release_mem_region...
● Lets see an example

What's wrong with this approach?
● You need to find a free spot from /proc/iomem. This space can
change between board to board, chipset to chipset, at hotplug
time, pci plug&play non determinacy, between architectures and
more.
● The space will not be used for anything, ever. So that space is
lost even you never do anything with it.
● You need to know the size of RAM you want in advance, and be
accurate about it. This is because every byte you allocate is a
byte you can't use for regular system operations.
CMA
● What is it? A piece of code, now in the standard kernel.
● Enabled in the default Linux configuration and by most
current Linux distributions.
● A result of a long line of patches with various
approaches intended to solve the “large contiguous
memory” problem.
● Well integrated, both with the Linux memory allocator
(gfp) and with the Linux DMA subsystem.
How do you use it?
● Make sure it's in your kernel (CONFIG_CMA)
● You can make the default CMA context larger or smaller via various kernel configuration options
(CMA_AREAS, CMA_DEBUG, CMA_SIZE_MBYTES, CMA_SIZE_PERCENTAGE, ...)
● Use the regular DMA API to allocate DMA buffers. That's it.
●
This means that drivers do not need to be changed.
● Allocation time will be a little longer if you ask for large buffers (see next slide). But not by much.
● You can use the special CMA APIS (dma_contiguous_reserve, dma_contiguous_early_fixup,
dma_alloc_from_contiguous, dma_release_from_contiguous, dma_declare_contiguous) but you
don't have too. Try to avoid it.
How does it work?
● Pages in the kernel are marked as movable and non movable.
● Pages which are movable are pages where the physical addresses don't matter. This is true for most pages
(especially user space pages and the page cache which take up most of the space in the system).
●
When you alloc via cma (actually when the DMA api allocs and CMA is enabled) it migrated pages pages which are
migratable (movable) to make physical room for you buffer.
●
While the migration is taking place the CMA uses a feature of the page allocator which allow to mark a page block as
MIGRATE_ISOLATE which prevents the page allocator from allocating these pages while the moving is taking place.
●
Allocating is from CMA contexts/pool. By default there are 7 (controllable from kernel configuration) of those and one
is default. You can create your own and have a private pool for you driver if you are willing to use the special API.
Usually you don't need these but for special considerations (realtime etc) this may be an option.
How does this solve my problems?
● Well, with CMA all of your RAM is used and if
you never ask for that large contiguous space
then you can use all of your systems RAM.
● In addition CMA is integrated with the page
allocator so when you return pages (simply
dealloc via the DMA API) the pages actually go
back to being used by whomever.
● CMA is transparent to drivers while the previous
solution is not.
Issues with CMA
● get_user_pages() is an issue which is still to be
dealt with efficiently. get_user_pages() locks
pages and makes the unmovable. This is dealt
with today by moving pages away as soon as
they are locked which creates a bit of overhead.
● IOMMU is better (see next slide).
IOMMU
● IOMMU solves all of your large contiguous woes (at
least as they pertain to hardware, not to operating
system sharing).
● It also has a security advantage.
● And much better debugging and stability.
● Really, try to use a board with IOMMU
 It's well
worth the extra $$$
.
● Maybe you already have an iommu? Check
/sys/class/iommu/*...
References
● $KERNEL/doc/kernel-parameters.txt (cma=)
● $KERNEL/drivers/base/dma-contiguous.c
● $KERNEL/include/linux/dma-contiguous.h
● https://lwn.net/Articles/486301/ A deep dive into CMA
● https://lwn.net/Articles/474819/ DMA buffer sharing
● https://lwn.net/Articles/396657/ Older CMA article – some
of it irrelevant.
● https://lwn.net/Articles/480055/ The Android ION memory
allocator (replacement of pmem).

Weitere Àhnliche Inhalte

PPTX
Slab Allocator in Linux Kernel
PDF
malloc & vmalloc in Linux
PPTX
Linux Memory Management with CMA (Contiguous Memory Allocator)
PPTX
Linux Kernel MMC Storage driver Overview
PDF
Process Scheduler and Balancer in Linux Kernel
PDF
Memory Management with Page Folios
PPTX
Linux memory-management-kamal
PDF
ëŠŹëˆ…ìŠ€ 컀널 디ëČ„ê±° KGDB/KDB
Slab Allocator in Linux Kernel
malloc & vmalloc in Linux
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Kernel MMC Storage driver Overview
Process Scheduler and Balancer in Linux Kernel
Memory Management with Page Folios
Linux memory-management-kamal
ëŠŹëˆ…ìŠ€ 컀널 디ëČ„ê±° KGDB/KDB

Was ist angesagt? (20)

PPTX
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
PDF
How to use KASAN to debug memory corruption in OpenStack environment- (2)
PDF
BPF Internals (eBPF)
PPT
Linux Crash Dump Capture and Analysis
PDF
ACPI Debugging from Linux Kernel
PDF
Page cache in Linux kernel
PDF
semaphore & mutex.pdf
PDF
Decompressed vmlinux: linux kernel initialization from page table configurati...
PDF
Linux kernel debugging
PDF
The Linux Block Layer - Built for Fast Storage
PDF
Linux Locking Mechanisms
PPTX
Linux MMAP & Ioremap introduction
PDF
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
PDF
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
PDF
Namespaces and cgroups - the basis of Linux containers
PDF
Physical Memory Management.pdf
PDF
Memory Mapping Implementation (mmap) in Linux Kernel
PDF
Yet another introduction to Linux RCU
PDF
Process Address Space: The way to create virtual address (page table) of user...
PDF
Introduction to Modern U-Boot
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
How to use KASAN to debug memory corruption in OpenStack environment- (2)
BPF Internals (eBPF)
Linux Crash Dump Capture and Analysis
ACPI Debugging from Linux Kernel
Page cache in Linux kernel
semaphore & mutex.pdf
Decompressed vmlinux: linux kernel initialization from page table configurati...
Linux kernel debugging
The Linux Block Layer - Built for Fast Storage
Linux Locking Mechanisms
Linux MMAP & Ioremap introduction
Kernel Recipes 2015: Linux Kernel IO subsystem - How it works and how can I s...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
Namespaces and cgroups - the basis of Linux containers
Physical Memory Management.pdf
Memory Mapping Implementation (mmap) in Linux Kernel
Yet another introduction to Linux RCU
Process Address Space: The way to create virtual address (page table) of user...
Introduction to Modern U-Boot
Anzeige

Ähnlich wie Continguous Memory Allocator in the Linux Kernel (20)

PDF
GCMA: Guaranteed Contiguous Memory Allocator
PDF
gcma: guaranteed contiguous memory allocator
PDF
DMA_document__1696148675.pdf
PDF
Kvm performance optimization for ubuntu
PDF
kkMemory management
PDF
Memory management in Linux kernel
PPT
Driver development – memory management
PPT
Linux Performance Tunning Memory
PPT
Windows memory manager internals
PDF
Virtual memory 20070222-en
PDF
Memory management
PPTX
Memory Mapping.pptx wsetdryfugiop8ytrewsrdtfyguiopoidxfcgvhj
PDF
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
PDF
SO-Memoria.pdf
PDF
SO-Memoria.pdf
 
DOCX
virtual memory - Computer operating system
PPT
OPERATING SYSTEM IMPORTANT NOTES_UNIT-4.ppt
PPTX
4. Memory virtualization and management
PPT
Linux memory
GCMA: Guaranteed Contiguous Memory Allocator
gcma: guaranteed contiguous memory allocator
DMA_document__1696148675.pdf
Kvm performance optimization for ubuntu
kkMemory management
Memory management in Linux kernel
Driver development – memory management
Linux Performance Tunning Memory
Windows memory manager internals
Virtual memory 20070222-en
Memory management
Memory Mapping.pptx wsetdryfugiop8ytrewsrdtfyguiopoidxfcgvhj
Kernel Recipes 2017 - 20 years of Linux Virtual Memory - Andrea Arcangeli
SO-Memoria.pdf
SO-Memoria.pdf
 
virtual memory - Computer operating system
OPERATING SYSTEM IMPORTANT NOTES_UNIT-4.ppt
4. Memory virtualization and management
Linux memory
Anzeige

Mehr von Kernel TLV (20)

PDF
DPDK In Depth
PDF
Building Network Functions with eBPF & BCC
PDF
SGX Trusted Execution Environment
PDF
Fun with FUSE
PPTX
Kernel Proc Connector and Containers
PPTX
Bypassing ASLR Exploiting CVE 2015-7545
PDF
Present Absence of Linux Filesystem Security
PDF
OpenWrt From Top to Bottom
PDF
Make Your Containers Faster: Linux Container Performance Tools
PDF
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
PDF
File Systems: Why, How and Where
PDF
netfilter and iptables
PDF
KernelTLV Speaker Guidelines
PDF
Userfaultfd: Current Features, Limitations and Future Development
PDF
Linux Kernel Cryptographic API and Use Cases
PPTX
DMA Survival Guide
PPSX
FD.IO Vector Packet Processing
PPTX
WiFi and the Beast
PPTX
Introduction to DPDK
PDF
FreeBSD and Drivers
DPDK In Depth
Building Network Functions with eBPF & BCC
SGX Trusted Execution Environment
Fun with FUSE
Kernel Proc Connector and Containers
Bypassing ASLR Exploiting CVE 2015-7545
Present Absence of Linux Filesystem Security
OpenWrt From Top to Bottom
Make Your Containers Faster: Linux Container Performance Tools
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
File Systems: Why, How and Where
netfilter and iptables
KernelTLV Speaker Guidelines
Userfaultfd: Current Features, Limitations and Future Development
Linux Kernel Cryptographic API and Use Cases
DMA Survival Guide
FD.IO Vector Packet Processing
WiFi and the Beast
Introduction to DPDK
FreeBSD and Drivers

KĂŒrzlich hochgeladen (20)

PDF
Internet Download Manager IDM Crack powerful download accelerator New Version...
PPTX
Viber For Windows 25.7.1 Crack + Serial Keygen
PPTX
Human-Computer Interaction for Lecture 2
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
PDF
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
PDF
Cloud Native Aachen Meetup - Aug 21, 2025
PPTX
Human Computer Interaction lecture Chapter 2.pptx
PPTX
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
PDF
Practical Indispensable Project Management Tips for Delivering Successful Exp...
PPTX
Python is a high-level, interpreted programming language
PPTX
Download Adobe Photoshop Crack 2025 Free
PPTX
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
PDF
IT Consulting Services to Secure Future Growth
PPTX
Airline CRS | Airline CRS Systems | CRS System
PDF
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
PPTX
Human-Computer Interaction for Lecture 1
PDF
What Makes a Great Data Visualization Consulting Service.pdf
PPTX
Folder Lock 10.1.9 Crack With Serial Key
PPTX
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
PDF
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)
Internet Download Manager IDM Crack powerful download accelerator New Version...
Viber For Windows 25.7.1 Crack + Serial Keygen
Human-Computer Interaction for Lecture 2
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
Cloud Native Aachen Meetup - Aug 21, 2025
Human Computer Interaction lecture Chapter 2.pptx
MLforCyber_MLDataSetsandFeatures_Presentation.pptx
Practical Indispensable Project Management Tips for Delivering Successful Exp...
Python is a high-level, interpreted programming language
Download Adobe Photoshop Crack 2025 Free
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
IT Consulting Services to Secure Future Growth
Airline CRS | Airline CRS Systems | CRS System
PDF-XChange Editor Plus 10.7.0.398.0 Crack Free Download Latest 2025
Human-Computer Interaction for Lecture 1
What Makes a Great Data Visualization Consulting Service.pdf
Folder Lock 10.1.9 Crack With Serial Key
4Seller: The All-in-One Multi-Channel E-Commerce Management Platform for Glob...
MiniTool Power Data Recovery 12.6 Crack + Portable (Latest Version 2025)

Continguous Memory Allocator in the Linux Kernel

  • 1. Linux CMA (Contiguous Memory Allocator) Mark Veltzer <mark.veltzer@gmail.com>
  • 2. Why contiguous memory? ● DMA ● Sharing between operating systems
  • 3. Why large contiguous memory? ● Brain dead devices (200MB in one DMA transfer?). ● Devices that uses large memory chunks. ● Performance of scatter/gather. ● kmalloc (4MB on x86, less on other platforms), gfp (8MB) limitations. ● And again, sharing between operating systems...
  • 4. Solutions to the contiguous memory problem ● Use small hardware buffers (the most common). ● Use scatter gather in hardware or in software (very common if small buffers are not available – especially in general availability products – because of the problems of the next methods and the unavailability of an IOMMU). ● Reserve a large contiguous buffer (a.k.a the old way – try not to use me if you can...). ● CMA (you are here! - half way to nirvana). ● IOMMU (a.k.a. the best thing since sliced bread – pure bliss – your company will build a temple in your honor).
  • 5. Doing it on your own (no CMA) ● First reserve a large buffer for your needs and tell the kernel to keep it's filthy grubbing hands off your buffer... ● Find an free large area by looking at /proc/iomem. I found 200M free buffer at address 800M in this example. ● Pass mem=800M memmap=200M$800M to the kernel at boot (this actually depends on the kernel version, for some kernels only memmap is needed, ugh!!!). ● Check it by passing bootmem_debug ● See that the allocation worked by looking at: – dmesg | grep user (no guarantee, kernel log changes all the time) – cat /proc/iomem (even better than previous). – /sys/firmware/memmap/*/* (the raw data – can be machine parsed).
  • 6. Doing it on your own (no CMA). Continued... ● Now you want your driver/kernel code to access this buffer
 ● request_mem_region with the same physical address you passed as a kernel parameter. ● ioremap ● And just you the buffer you get from ioremap. ● Dont forget to iounmap and release_mem_region... ● Lets see an example

  • 7. What's wrong with this approach? ● You need to find a free spot from /proc/iomem. This space can change between board to board, chipset to chipset, at hotplug time, pci plug&play non determinacy, between architectures and more. ● The space will not be used for anything, ever. So that space is lost even you never do anything with it. ● You need to know the size of RAM you want in advance, and be accurate about it. This is because every byte you allocate is a byte you can't use for regular system operations.
  • 8. CMA ● What is it? A piece of code, now in the standard kernel. ● Enabled in the default Linux configuration and by most current Linux distributions. ● A result of a long line of patches with various approaches intended to solve the “large contiguous memory” problem. ● Well integrated, both with the Linux memory allocator (gfp) and with the Linux DMA subsystem.
  • 9. How do you use it? ● Make sure it's in your kernel (CONFIG_CMA) ● You can make the default CMA context larger or smaller via various kernel configuration options (CMA_AREAS, CMA_DEBUG, CMA_SIZE_MBYTES, CMA_SIZE_PERCENTAGE, ...) ● Use the regular DMA API to allocate DMA buffers. That's it. ● This means that drivers do not need to be changed. ● Allocation time will be a little longer if you ask for large buffers (see next slide). But not by much. ● You can use the special CMA APIS (dma_contiguous_reserve, dma_contiguous_early_fixup, dma_alloc_from_contiguous, dma_release_from_contiguous, dma_declare_contiguous) but you don't have too. Try to avoid it.
  • 10. How does it work? ● Pages in the kernel are marked as movable and non movable. ● Pages which are movable are pages where the physical addresses don't matter. This is true for most pages (especially user space pages and the page cache which take up most of the space in the system). ● When you alloc via cma (actually when the DMA api allocs and CMA is enabled) it migrated pages pages which are migratable (movable) to make physical room for you buffer. ● While the migration is taking place the CMA uses a feature of the page allocator which allow to mark a page block as MIGRATE_ISOLATE which prevents the page allocator from allocating these pages while the moving is taking place. ● Allocating is from CMA contexts/pool. By default there are 7 (controllable from kernel configuration) of those and one is default. You can create your own and have a private pool for you driver if you are willing to use the special API. Usually you don't need these but for special considerations (realtime etc) this may be an option.
  • 11. How does this solve my problems? ● Well, with CMA all of your RAM is used and if you never ask for that large contiguous space then you can use all of your systems RAM. ● In addition CMA is integrated with the page allocator so when you return pages (simply dealloc via the DMA API) the pages actually go back to being used by whomever. ● CMA is transparent to drivers while the previous solution is not.
  • 12. Issues with CMA ● get_user_pages() is an issue which is still to be dealt with efficiently. get_user_pages() locks pages and makes the unmovable. This is dealt with today by moving pages away as soon as they are locked which creates a bit of overhead. ● IOMMU is better (see next slide).
  • 13. IOMMU ● IOMMU solves all of your large contiguous woes (at least as they pertain to hardware, not to operating system sharing). ● It also has a security advantage. ● And much better debugging and stability. ● Really, try to use a board with IOMMU
 It's well worth the extra $$$
. ● Maybe you already have an iommu? Check /sys/class/iommu/*...
  • 14. References ● $KERNEL/doc/kernel-parameters.txt (cma=) ● $KERNEL/drivers/base/dma-contiguous.c ● $KERNEL/include/linux/dma-contiguous.h ● https://lwn.net/Articles/486301/ A deep dive into CMA ● https://lwn.net/Articles/474819/ DMA buffer sharing ● https://lwn.net/Articles/396657/ Older CMA article – some of it irrelevant. ● https://lwn.net/Articles/480055/ The Android ION memory allocator (replacement of pmem).