Skip to content

fix: forward untracked pointers to the driver on the synchronous free path - #258

Merged
hami-robot[bot] merged 1 commit into
Project-HAMi:mainfrom
veyron-kairo:fix/sync-free-untracked-passthrough
Sep 22, 2026
Merged

hami-robot[bot] merged 1 commit into
Project-HAMi:mainfrom
veyron-kairo:fix/sync-free-untracked-passthrough

Conversation

@veyron-kairo

@veyron-kairo veyron-kairo commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

What this does

remove_chunk is the synchronous free path behind cuMemFree_v2 -> free_raw. It returned -1 without freeing when a pointer wasn't found in device_overallocated, both on the empty-list early return and on the loop not-found return. That -1 propagates back through free_raw and the cuMemFree_v2 hook, so freeing a pointer libvgpu didn't track leaves the memory live on the device and hands the caller a value that isn't a valid CUresult.

remove_chunk_async already forwards such frees to the real driver; its comment notes the old behavior "leaked and surfaced as an unrecognized error code". This applies the same fix to the synchronous path, using the existing cuMemoryFree helper that the found path already calls.

Fixes #257

How a pointer reaches this path untracked

  • a stream-ordered allocation (cuMemAllocAsync) freed with cuMemFree instead of cuMemFreeAsync
  • an allocation made before tracking started

Verification

The change reuses cuMemoryFree(dptr), which remove_chunk already calls on its found path, so it adds no new dependency and is build-identical to existing code. I couldn't run a full build locally (it needs the CUDA toolkit); the Build libvgpu CI will confirm compilation.

I used AI assistance while reviewing the allocator . I read the code and the async twin's fix myself and take responsibility for the change.

Summary by CodeRabbit

  • Bug Fixes
    • Improved memory cleanup for untracked device-pointer allocations.
    • Device memory can now be freed correctly even when no matching allocation record exists.

… path

remove_chunk returned -1 without freeing when a pointer was not found in
device_overallocated, and on the empty-list early return. free_raw and the
cuMemFree_v2 hook pass that value straight back, so freeing a pointer libvgpu
did not track (a stream-ordered allocation reaching cuMemFree, or one made
before tracking started) leaves the memory allocated on the device and hands
the caller -1, which is not a valid CUresult.

remove_chunk_async already forwards such frees to the real driver; do the same
on the synchronous path.

Signed-off-by: Shridhar Panigrahi <198173519+veyron-kairo@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

remove_chunk now forwards untracked device-pointer frees to cuMemoryFree when the allocation list is empty or has no matching entry. Tracked allocations keep their existing bookkeeping and free behavior.

Changes

Synchronous free handling

Layer / File(s) Summary
Forward untracked frees
src/allocator/allocator.c
remove_chunk calls cuMemoryFree for empty allocation lists and unmatched pointers instead of returning -1.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • Project-HAMi/HAMi-core#217: Updates the analogous asynchronous path to free untracked device pointers through the CUDA free API.

Suggested labels: enhancement

Suggested reviewers: imlach

Poem

A rabbit hops through allocator night,
Untracked pointers now take flight.
cuMemoryFree clears the way,
No strange error code holds sway.
Synchronous frees now end bright.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address issue #257 by forwarding untracked pointers to cuMemoryFree on both synchronous not-found paths.
Out of Scope Changes check ✅ Passed The changes are limited to the synchronous free-path fix described in issue #257 and exclude the separate accounting concern.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes forwarding untracked pointers to the driver during synchronous free operations.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@hami-robot hami-robot Bot added the size/S label Aug 9, 2026
@coderabbitai coderabbitai Bot added the enhancement New feature or request label Aug 9, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/allocator/allocator.c`:
- Around line 184-187: Update remove_chunk so the device_overallocated
empty-list check and subsequent lookup/removal are performed while holding
mutex. Ensure concurrent inserts cannot cause cuMemoryFree(dptr) to bypass
removal of a matching chunk, and preserve the existing not-found behavior after
the synchronized check.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6bba5273-9445-429d-97b5-1c85cec4049a

📥 Commits

Reviewing files that changed from the base of the PR and between 5496322 and 6546ba9.

📒 Files selected for processing (1)
  • src/allocator/allocator.c

Comment thread src/allocator/allocator.c
Comment thread src/allocator/allocator.c
@archlitchi

Copy link
Copy Markdown
Member

/assign

@maverick-woo

Copy link
Copy Markdown
Contributor

Looks good overall. Please move the empty-list check under the mutex and remove the verbose comments.

@archlitchi archlitchi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@hami-robot hami-robot Bot added the lgtm label Sep 22, 2026
@hami-robot

hami-robot Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: archlitchi, veyron-kairo

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot Bot added the approved label Sep 22, 2026
@hami-robot
hami-robot Bot merged commit 410dfbe into Project-HAMi:main Sep 22, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cuMemFree of an untracked pointer leaks the allocation and returns an invalid CUresult

3 participants