Skip to content

Using shm to reduce memory copy while using remote connector#2601

Merged
maobaolong merged 8 commits intoLMCache:devfrom
maobaolong:shm
Feb 24, 2026
Merged

Using shm to reduce memory copy while using remote connector#2601
maobaolong merged 8 commits intoLMCache:devfrom
maobaolong:shm

Conversation

@maobaolong
Copy link
Copy Markdown
Collaborator

@maobaolong maobaolong commented Feb 14, 2026

What this PR does / why we need it:

This pull request introduced POSIX shared memory (SHM) for CPU-pinned memory allocations. This integration aims to optimize data transfer by reducing memory copy overhead, especially when using remote connectors. The changes include C++ implementations for SHM-backed pinned memory.

This PR also introduced a new demonstrate ShmFileConnector leverages this SHM capability, allowing a C++ worker process to perform file I/O directly on shared memory regions, thereby streamlining data movement between LMCache and external storage.

Special notes for your reviewers:

If applicable:

  • this PR contains user facing changes - docs added
  • this PR contains unit tests.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @maobaolong, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances LMCache's memory management by integrating POSIX shared memory (SHM) for CPU-pinned memory allocations. This integration aims to optimize data transfer by reducing memory copy overhead, especially when using remote connectors. The changes include C++ implementations for SHM-backed pinned memory, their exposure to Python, and their seamless integration into the existing memory allocation framework. A new ShmFileConnector leverages this SHM capability, allowing a C++ worker process to perform file I/O directly on shared memory regions, thereby streamlining data movement between LMCache and external storage. The PR also includes robust testing and verification for this new functionality.

Highlights

  • Shared Memory (SHM) Allocation: New C++ functions alloc_shm_pinned_ptr and free_shm_pinned_ptr are introduced to manage CUDA-pinned memory backed by POSIX shared memory, enabling zero-copy data transfer between processes.
  • Python Bindings and Fallbacks: The new SHM allocation/deallocation functions are exposed to Python via pybind11, and Python-based fallback implementations are added using multiprocessing.shared_memory and ctypes for non-CUDA environments.
  • Memory Management Integration: The core memory allocation and deallocation functions in lmcache/v1/memory_management.py are updated to conditionally utilize SHM-backed pinned memory when a shm_name is specified, and MixedMemoryAllocator now supports this configuration.
  • New Remote Connector for SHM: A new ShmFileConnector is implemented, facilitating remote file I/O through a C++ subprocess (shm_file_worker) that directly interacts with shared memory, supporting both PIPE and TCP communication modes.
  • Comprehensive Testing and Verification: New CMake files, a C++ shm_file_worker executable, and extensive Python tests (test_shm_allocator.py) are added to verify the end-to-end functionality of SHM allocation and the ShmFileConnector. A verification script (verify_shmfile_tcp.py) is also included for IDC/TCP mode testing.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • CMakeLists.txt
    • Added a root CMakeLists.txt file for project configuration.
  • csrc/mem_alloc.cpp
    • Included fcntl.h and implemented functions for shared memory (SHM) pinned pointer allocation and deallocation.
  • csrc/mem_alloc.h
    • Declared new functions for shared memory (SHM) pinned pointer management.
  • csrc/pybind.cpp
    • Exposed shared memory (SHM) pinned pointer allocation and deallocation functions to Python.
  • lmcache/non_cuda_equivalents.py
    • Added Python fallback implementations for shared memory (SHM) pinned pointer allocation and deallocation.
  • lmcache/v1/memory_management.py
    • Updated CPU memory allocation and freeing to support shared memory (SHM) and integrated SHM name handling into MixedMemoryAllocator.
  • lmcache/v1/standalone/main.py
    • Removed the fmt argument from LMCacheMetadata initialization.
  • lmcache/v1/storage_backend/local_cpu_backend.py
    • Passed the configuration object to the MixedMemoryAllocator.
  • tests/v1/shm_allocator/csrc/.gitignore
    • Added build/ to the gitignore file.
  • tests/v1/shm_allocator/csrc/CMakeLists.txt
    • Added CMake configuration for building the shm_file_worker executable.
  • tests/v1/shm_allocator/csrc/shm_file_worker.cpp
    • Added a C++ executable for shared memory (SHM) file I/O operations.
  • tests/v1/shm_allocator/shmfile_basic_check.yaml
    • Added a new configuration file for testing the ShmFileConnector.
  • tests/v1/shm_allocator/shmfile_connector.py
    • Added a new ShmFileConnector for remote file I/O using shared memory and a C++ worker.
  • tests/v1/shm_allocator/test_shm_allocator.py
    • Added comprehensive tests for the ShmFileConnector and shared memory (SHM) worker functionality.
  • tests/v1/shm_allocator/verify_shmfile_tcp.py
    • Added a Python script to verify shared memory (SHM) file connector functionality in TCP mode.
Activity
  • New core functionality for shared memory (SHM) management has been introduced in C++.
  • Python bindings for the new SHM functions have been added, along with Python fallback implementations.
  • The memory management system has been updated to integrate SHM-backed allocations.
  • A new ShmFileConnector has been developed to leverage SHM for remote file I/O.
  • Extensive testing, including unit and end-to-end tests, has been added for the new SHM features and connector.
  • A dedicated verification script for IDC/TCP mode testing of the SHM connector has been included.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant new feature: a remote connector that leverages POSIX shared memory for inter-process communication, implemented with C++ allocation functions, a C++ worker process for file I/O, and a Python connector. However, a critical security vulnerability has been identified in the shm_file_worker process, allowing remote arbitrary file read/write (path traversal) and arbitrary memory access via integer overflows. This is compounded by the worker binding to all network interfaces without authentication, potentially leading to full host system compromise. Beyond these critical security concerns, further improvements are needed for robustness in error handling, memory access, and adherence to Python style conventions.

Comment thread tests/v1/shm_allocator/csrc/shm_file_worker.cpp Outdated
Comment thread tests/v1/shm_allocator/csrc/shm_file_worker.cpp
Comment thread tests/v1/shm_allocator/csrc/shm_file_worker.cpp
Comment thread tests/v1/shm_allocator/csrc/shm_file_worker.cpp
Comment thread csrc/pybind.cpp Outdated
Comment thread tests/v1/shm_allocator/test_shm_allocator.py Outdated
Comment thread tests/v1/shm_allocator/verify_shmfile_tcp.py Outdated
@maobaolong maobaolong added the full Run comprehensive tests on this PR label Feb 15, 2026
Comment thread lmcache/v1/memory_management.py
Comment thread lmcache/v1/memory_management.py Outdated
@sammshen
Copy link
Copy Markdown
Contributor

small comment LGTM!

@maobaolong
Copy link
Copy Markdown
Collaborator Author

@sammshen Thanks for the review, abstracted a helper method, PTAL.

@maobaolong maobaolong requested a review from sammshen February 21, 2026 01:39
Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: baoloongmao <baoloongmao@tencent.com>
- Fix integer overflow vulnerabilities in shm_file_worker by adding robust boundary checks
- Change default network binding from 0.0.0.0 to 127.0.0.1 for better security
- Add GIL release for free_shm_pinned_ptr to prevent blocking Python interpreter
- Move all imports to the top of test files for better code organization

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Copy link
Copy Markdown
Contributor

@sammshen sammshen left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Collaborator

@chunxiaozheng chunxiaozheng left a comment

Choose a reason for hiding this comment

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

lgtm

@maobaolong maobaolong merged commit f7cc47b into LMCache:dev Feb 24, 2026
24 checks passed
sammshen pushed a commit to sammshen/LMCache that referenced this pull request Mar 1, 2026
…#2601)

* Shm and demo

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* support set worker addr to verify in container env

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add missing file

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix unit

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add a shm memory example

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Move to right folder

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix security vulnerabilities based on Gemini code review

- Fix integer overflow vulnerabilities in shm_file_worker by adding robust boundary checks
- Change default network binding from 0.0.0.0 to 127.0.0.1 for better security
- Add GIL release for free_shm_pinned_ptr to prevent blocking Python interpreter
- Move all imports to the top of test files for better code organization

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Address comment

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
hlin99 pushed a commit to hlin99/LMCache that referenced this pull request Mar 2, 2026
…#2601)

* Shm and demo

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* support set worker addr to verify in container env

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add missing file

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix unit

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add a shm memory example

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Move to right folder

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix security vulnerabilities based on Gemini code review

- Fix integer overflow vulnerabilities in shm_file_worker by adding robust boundary checks
- Change default network binding from 0.0.0.0 to 127.0.0.1 for better security
- Add GIL release for free_shm_pinned_ptr to prevent blocking Python interpreter
- Move all imports to the top of test files for better code organization

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Address comment

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
mauryaavinash95 pushed a commit to mauryaavinash95/LMCache that referenced this pull request Mar 7, 2026
…#2601)

* Shm and demo

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* support set worker addr to verify in container env

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add missing file

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix unit

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add a shm memory example

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Move to right folder

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix security vulnerabilities based on Gemini code review

- Fix integer overflow vulnerabilities in shm_file_worker by adding robust boundary checks
- Change default network binding from 0.0.0.0 to 127.0.0.1 for better security
- Add GIL release for free_shm_pinned_ptr to prevent blocking Python interpreter
- Move all imports to the top of test files for better code organization

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Address comment

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
shaoxiawjc pushed a commit to shaoxiawjc/LMCache that referenced this pull request Mar 11, 2026
…#2601)

* Shm and demo

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* support set worker addr to verify in container env

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add missing file

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix unit

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add a shm memory example

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Move to right folder

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix security vulnerabilities based on Gemini code review

- Fix integer overflow vulnerabilities in shm_file_worker by adding robust boundary checks
- Change default network binding from 0.0.0.0 to 127.0.0.1 for better security
- Add GIL release for free_shm_pinned_ptr to prevent blocking Python interpreter
- Move all imports to the top of test files for better code organization

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Address comment

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: shaoxiawjc <wjc2800@163.com>
realAaronWu pushed a commit to realAaronWu/LMCache that referenced this pull request Mar 20, 2026
…#2601)

* Shm and demo

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* support set worker addr to verify in container env

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add missing file

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix unit

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Add a shm memory example

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Move to right folder

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Fix security vulnerabilities based on Gemini code review

- Fix integer overflow vulnerabilities in shm_file_worker by adding robust boundary checks
- Change default network binding from 0.0.0.0 to 127.0.0.1 for better security
- Add GIL release for free_shm_pinned_ptr to prevent blocking Python interpreter
- Move all imports to the top of test files for better code organization

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

* Address comment

Signed-off-by: baoloongmao <baoloongmao@tencent.com>

---------

Signed-off-by: baoloongmao <baoloongmao@tencent.com>
Signed-off-by: Aaron Wu <aaron.wu@dell.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full Run comprehensive tests on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants