Fix use-after-free in get_xpu_view_from_cpu_tensor#262
Open
chaojun-zhang wants to merge 2 commits intovllm-project:mainfrom
Open
Fix use-after-free in get_xpu_view_from_cpu_tensor#262chaojun-zhang wants to merge 2 commits intovllm-project:mainfrom
chaojun-zhang wants to merge 2 commits intovllm-project:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a use-after-free risk where an XPU zero-copy view over a pinned CPU tensor could outlive the CPU tensor that owns the underlying host memory.
Changes:
- Extend
XPUHostViewAllocatorto retain ownership of the source CPU tensor for the lifetime of the XPU view. - Store the owner tensor in the
c10::DataPtrcontext so it is released when the view tensor is destroyed. - Add a regression test that drops the Python reference to the CPU tensor and validates the XPU view remains usable.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
csrc/xpu_view.cpp |
Keeps the source CPU tensor alive by attaching it to the DataPtr context used by the XPU view. |
tests/test_uva.py |
Adds a regression test for XPU view lifetime after the original CPU tensor reference is dropped. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
d9e39dc to
9c7799e
Compare
hsubramony
approved these changes
Apr 9, 2026
Signed-off-by: chaojun-zhang <chaojun.zhang@intel.com> Co-authored-by: Harish Subramony <harish.subramony@intel.com>
Signed-off-by: chaojun-zhang <chaojun.zhang@intel.com>
05585f4 to
9426c3e
Compare
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.
Essential Elements of an Effective PR Description Checklist
supported_models.mdandexamplesfor a new model.PLEASE FILL IN THE PR DESCRIPTION HERE ENSURING ALL CHECKLIST ITEMS ABOVE HAVE BEEN CONSIDERED.
Purpose
Problem
get_xpu_view_from_cpu_tensor creates a zero-copy XPU view over a CPU pinned-memory tensor by storing the raw host_ptr inside XPUHostViewAllocator. However, it held no reference
to the original cpu_tensor, so its reference count could drop to zero and its pinned memory could be freed while the XPU view was still alive — a classic use-after-free.
This is not a theoretical concern. In two real call sites inside vLLM:
view) still points to its memory.
Fix
XPUHostViewAllocator now holds a torch::Tensor owner_ member that keeps the source tensor's reference count elevated. The owner is stored as an OwnerContext in the DataPtr, so
it lives exactly as long as the XPU view tensor does and is released when the view is destroyed.
Test Plan
pytest -s -v tests/test_uva.py::test_view_lifetime_after_owner_drop
Test Result
(Optional) Documentation Update
BEFORE SUBMITTING, PLEASE READ https://docs.vllm.ai/en/latest/contributing (anything written below this line will be removed by GitHub Actions)