Linux Heterogeneous Memory Management: Avoid 7 Mistakes
Linux heterogeneous memory management (HMM) is the kernel code that lets a device such as a GPU share a process address space with the CPU and use its own memory for that process's data. This article is based on the official kernel HMM documentation (the page for kernel 7.4.0-rc1, read on 2 November 2026), not on a hands-on test: HMM is a driver API, and using it needs a device driver and suitable hardware. The old version of this post described PCIe ATS and PRI as the core of HMM; the kernel document does not describe HMM that way, so that part is gone.
Contents
- What problem Linux heterogeneous memory management solves
- The two features of Linux heterogeneous memory management
- Mirroring in Linux heterogeneous memory management
- Flags: ask for exactly the access you need
- Device private memory in Linux heterogeneous memory management
- Seven Linux heterogeneous memory management mistakes to avoid
- Exclusive access for device atomics
- Where to go next
- FAQ
What problem Linux heterogeneous memory management solves
The documentation starts with what it calls a "split address space". Devices with gigabytes of on-board memory used driver-specific allocators, so a program had to copy its data between normal memory (malloc, mmap) and memory from the device API. For flat arrays that is manageable. For lists and trees, the docs say, every pointer relation must be re-mapped, which is error prone, and each library may have to copy its input again.
The bus makes it worse. The docs give about 32 GB/s for PCIe 4.0 with 16 lanes, which they call 33 times less than the fastest GPU memory at 1 TB/s, and they note that device access to main memory has an order of magnitude higher latency than access to its own memory. So a shared address space is only useful if data can also move to device memory while the device uses it.
The two features of Linux heterogeneous memory management
The docs name two main features:
- Address space mirroring. The CPU page table is duplicated in the device page table, so the same address points to the same physical memory. HMM gives helpers to fill the device page table and to follow CPU page table updates.
- Device memory as ZONE_DEVICE pages. Each page of device memory gets a
struct page. The CPU cannot map these pages, but they let main memory migrate to device memory with the existing migration code. To the CPU it looks like a page swapped out to disk.
Linux heterogeneous memory management only gives helpers. The docs are clear that the policy, what to migrate and when, belongs to the device driver.

Mirroring in Linux heterogeneous memory management
A driver that mirrors a process address space first registers an interval notifier:
int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
struct mm_struct *mm, unsigned long start,
unsigned long length,
const struct mmu_interval_notifier_ops *ops);
In ops->invalidate(), the driver must update the device page table for the range, and the docs require the device to complete that update before the callback returns. To fill a range, the current page names hmm_range_fault_unlocked_timeout() as the normal interface. It triggers page faults through the generic mm code, just like a CPU fault. The pattern from the docs, shortened:
again:
ret = hmm_range_fault_unlocked_timeout(&range, timeout);
if (ret)
goto out_put;
take_lock(driver->update);
if (mmu_interval_read_retry(range.notifier, range.notifier_seq)) {
release_lock(driver->update);
goto again;
}
/* Use pfns array content to update device page table,
* under the update lock */
release_lock(driver->update);
The timeout is in jiffies, and 0 means retry forever. The function returns -EBUSY when the retry budget runs out before it gets a stable range, and -EINTR if taking the mmap lock is interrupted or a fatal signal is pending.
Flags: ask for exactly the access you need
Linux heterogeneous memory management lets a driver fault in a whole range with one policy instead of setting each page.
The hmm_range struct has default_flags and pfn_flags_mask. With default_flags = HMM_PFN_REQ_FAULT and a mask of 0, every page in the range is faulted with at least read permission. To ask for write access on one page only, the docs set pfn_flags_mask = HMM_PFN_REQ_WRITE and put HMM_PFN_REQ_WRITE in that page's entry. After the call, the entries show the real state, for example HMM_PFN_VALID | HMM_PFN_WRITE for a writable page.
Device private memory in Linux heterogeneous memory management
To move data into device memory, a driver first creates device private pages:
res = request_free_mem_region(&iomem_resource, /* number of bytes */,
"name of driver resource");
pagemap.type = MEMORY_DEVICE_PRIVATE;
pagemap.range.start = res->start;
pagemap.range.end = res->end;
pagemap.nr_range = 1;
pagemap.ops = &device_devmem_ops;
memremap_pages(&pagemap, numa_node_id());
Migration then runs in three calls under the mmap lock: migrate_vma_setup() collects the source pages, the driver allocates destination pages and copies the data with its own DMA engine, migrate_vma_pages() commits the move, and migrate_vma_finalize() replaces the special migration entries. The docs mention two important details. A page that was never populated arrives as a "zero" entry, so the driver can clear device memory instead of copying zeros. And a CPU thread can fault on the same page during the move; the driver then sees that MIGRATE_PFN_MIGRATE was cleared and must roll that page back.
Seven Linux heterogeneous memory management mistakes to avoid
- Holding the mmap lock when calling the unlocked helper. The docs say the caller must not hold
mmap_read_lockbeforehmm_range_fault_unlocked_timeout(). - Using
hmm_range_fault()for VMAs that drop the lock. It needs the lock held and cannot support regions such as userfaultfd ones. The docs tell new callers to prefer the unlocked version. - Checking for a retry without the driver lock.
mmu_interval_read_retry()must run under the same lock thatinvalidate()takes, or a CPU page table update can race with you. - Returning from
invalidate()too early. The device update has to be complete first. - Expecting file-backed memory to migrate. The docs say only anonymous private VMA ranges can be migrated to or from device private memory today.
- Assuming device memory has its own accounting. Device pages count as normal pages in rss and in the memory cgroup. The docs warn that the OOM killer may kill a process that uses a lot of device memory and frees little system memory.
- Pinning device memory. The docs state it can never be pinned by a driver or through GUP, so it is always freed when the process exits.
Exclusive access for device atomics
Some devices need exclusive access to a page to run atomic operations on system memory. make_device_exclusive() replaces the mappings with special swap entries, so any CPU access faults and restores the original mapping. The driver learns about it through MMU notifiers, and exclusivity lasts until it drops the page lock and reference.
Where to go next
If you are not writing a driver, Linux heterogeneous memory management is mostly invisible: your GPU stack uses it for you. For the user-side view of memory on a Linux server, the LVM guide and the kernel build post are more practical starting points. Driver authors should read the full kernel page, which also links the general page migration document.
FAQ
Is Linux heterogeneous memory management only for GPUs? The docs mention GPU, DSP and FPGA devices that run work for a process.
Does the CPU ever read device private memory directly? No. Any CPU access to such a page triggers a page fault and a migration back to main memory.
Was Linux heterogeneous memory management tested on hardware for this post? No. This article only reports what the kernel documentation says.
Comments
Post a Comment