Skip to content

[Fix] hicache: mlock pinned host buffers so page aging never scans them - #47543

Merged
merrymercy merged 1 commit into
mainfrom
oss-sync-1f27e02572
Oct 31, 2026
Merged

merrymercy merged 1 commit into
mainfrom
oss-sync-1f27e02572

Conversation

@metamergebot

@metamergebot metamergebot commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

cudaHostRegister keeps a host pool's pages resident for DMA, but it leaves them on the kernel's evictable LRU. Proactive reclaim (page aging) clears the accessed flag of every page it scans there, pinned or not. On GPUs that read host memory through the CPU page tables (ATS), the device's next access to an aged page takes a page fault instead of a plain read, so host-to-device copies out of a HiCache host pool can fault page by page once the pool has been aged.

Locked pages sit on the unevictable LRU and are never scanned, so making mlock part of pinning removes those faults at the source. There is no new flag: a pinned buffer is a locked buffer.

Modifications

All in python/sglang/srt/mem_cache/pool_host/common.py:

  • _cuda_host_register mlocks the buffer once all of its ranges are registered. Every buffer that alloc_with_host_register pins (pin_memory=True) is therefore locked too: the HiCache host pools on CUDA and ROCm, and the other users of that allocator.
  • _cuda_host_unregister munlocks it once all of its ranges are unregistered, so a destroyed pool returns to ordinary memory. The locked range is kept on the tensor next to the registered ranges, under _HOST_LOCKED_RANGE_ATTR.
  • Before locking, the RLIMIT_MEMLOCK soft limit is raised to the hard limit, which needs no privilege. The locked range, the time the lock took, the limits, CAP_IPC_LOCK and VmLck are logged at INFO.
  • A refused lock (a memlock limit below the buffer size without CAP_IPC_LOCK, as in many containers) logs a warning with errno, the size, the limits, CAP_IPC_LOCK and VmLck, and says to raise the limit or grant the capability. The buffer stays registered and usable exactly as before this PR. Failing instead would turn deployments that work today into startup failures, for a lock that only keeps device reads fast and never affects correctness.
  • Unchanged: pin_memory=False (nothing is pinned or locked), torch-pinned buffers (npu, musa, xpu) and hipHostMalloc pools, which the device runtime allocates.

Cost: the host pool becomes unevictable memory, which it is pinned for DMA anyway. Locking adds work during startup; the INFO log reports its duration. On ATS platforms a registered pool could previously still be swapped out; with this change pinned means resident there too, as it already does on non-ATS platforms, so host pools must fit in RAM as they already must elsewhere.

Accuracy Tests

Not applicable: the pool's contents and every copy out of it are unchanged, only the pages' LRU placement is.

Speed Tests and Profiling

Not included in this PR.

Testing

  • test/registered/unit/mem_cache/test_hicache_host_register.py and test_mmap_allocator.py pass on CPU (22 tests). The cases that hand the allocator a fake buffer take the warning path and pass unchanged.
  • A direct check on a 64 MiB CPU tensor: _mlock_host_buffer logs the locked range and VmLck rises by 64 MiB; _munlock_host_buffer clears the attribute.
  • pre-commit (ruff, ruff-format, codespell) is clean on the changed file.

Checklist


CI States

Latest PR Test (Base): 🚫 Run #41141142235
Latest PR Test (Extra): ❌ Run #41141142033
Latest PR Test (AMD ROCm 10): ❌ Run #41141142086

`cudaHostRegister` keeps a host pool's pages resident for DMA, but leaves
them on the kernel's evictable LRU. Proactive reclaim (page aging) clears
the accessed flag of every page it scans there, pinned or not. On GPUs
that read host memory through the CPU page tables (ATS), the next device
access to an aged page takes a page fault instead of a plain read, so
copies out of an aged host pool fault page by page.

Make locking part of pinning in `mem_cache/pool_host/common.py`:

- `_cuda_host_register` mlocks the buffer once all of its ranges are
  registered, so every buffer that `alloc_with_host_register` pins
  (`pin_memory=True`) is locked too. Locked pages sit on the unevictable
  LRU and are never scanned.
- `_cuda_host_unregister` munlocks it once all ranges are unregistered,
  so a destroyed pool returns to ordinary memory. The locked range is
  kept on the tensor next to the registered ranges.
- Before locking, the RLIMIT_MEMLOCK soft limit is raised to the hard
  limit, which needs no privilege. The locked range is logged with the
  time it took, the limits, CAP_IPC_LOCK and VmLck.
- A refused lock (a memlock limit below the buffer size without
  CAP_IPC_LOCK, as in many containers) logs a warning with those details
  and leaves the buffer pinned and usable, exactly as before. Failing
  would turn working deployments into startup failures for a lock that
  only keeps device reads fast and never affects correctness.

Unchanged: `pin_memory=False` (nothing pinned or locked), torch-pinned
buffers (npu, musa, xpu) and `hipHostMalloc` pools, which the device
runtime allocates. On ATS platforms pinned now also means resident, so
host pools must fit in RAM there as they already must elsewhere.

Co-authored-by: ZYHowell <[email protected]>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@github-actions github-actions Bot added the hicache Hierarchical Caching for SGLang label Oct 30, 2026
@metamergebot metamergebot added the run-ci CI: run the baseline test suite on this PR label Oct 30, 2026
@merrymercy
merrymercy merged commit 9dbc462 into main Oct 31, 2026
195 of 220 checks passed
@merrymercy
merrymercy deleted the oss-sync-1f27e02572 branch October 31, 2026 04:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hicache Hierarchical Caching for SGLang run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants