Repository navigation
[Fix] hicache: mlock pinned host buffers so page aging never scans them - #47543
Merged
Merged
Conversation
`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]>
There was a problem hiding this comment.
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.
metamergebot
requested review from
Ying1123,
alphabetc1,
hanming-lu,
hnyls2002,
hzh0425,
ispobock,
merrymercy,
xiezhq-hermann and
yizhang2077
as code owners
October 30, 2026 22:54
4 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
cudaHostRegisterkeeps 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_registermlocks the buffer once all of its ranges are registered. Every buffer thatalloc_with_host_registerpins (pin_memory=True) is therefore locked too: the HiCache host pools on CUDA and ROCm, and the other users of that allocator._cuda_host_unregistermunlocks 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.RLIMIT_MEMLOCKsoft limit is raised to the hard limit, which needs no privilege. The locked range, the time the lock took, the limits,CAP_IPC_LOCKandVmLckare logged at INFO.CAP_IPC_LOCK, as in many containers) logs a warning with errno, the size, the limits,CAP_IPC_LOCKandVmLck, 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.pin_memory=False(nothing is pinned or locked), torch-pinned buffers (npu, musa, xpu) andhipHostMallocpools, 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.pyandtest_mmap_allocator.pypass on CPU (22 tests). The cases that hand the allocator a fake buffer take the warning path and pass unchanged._mlock_host_bufferlogs the locked range andVmLckrises by 64 MiB;_munlock_host_bufferclears 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