Skip to content

feat: enforce vGPU memory isolation for child and ssh processes - #2576

Closed
MuhammadBilal561 wants to merge 1 commit into
Project-HAMi:masterfrom
MuhammadBilal561:master
Closed

feat: enforce vGPU memory isolation for child and ssh processes#2576
MuhammadBilal561 wants to merge 1 commit into
Project-HAMi:masterfrom
MuhammadBilal561:master

Conversation

@MuhammadBilal561

@MuhammadBilal561 MuhammadBilal561 commented Aug 11, 2026

Copy link
Copy Markdown

/kind feature

What this PR does / why we need it:
Enforces vGPU memory isolation across child and SSH processes by injecting overrideEnv. This guarantees that memory ceilings and core allocation limits are strictly maintained across process boundaries spawned within the container environment.

Which issue(s) this PR fixes:

Special notes for your reviewer:
Includes updates to libvgpu for process-level memory enforcement and pkg/ device plugin logic to handle overrideEnv injections.

Does this PR introduce a user-facing change?:
NONE

Summary by CodeRabbit

  • New Features

    • Allocation now preserves HAMi-related environment settings, including indexed values, in an override file mounted read-only inside containers.
  • Bug Fixes

    • Allocation continues successfully if the override file cannot be written; the issue is logged for visibility.
  • Chores

    • Updated the integrated virtual GPU component version.

@hami-robot

hami-robot Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: MuhammadBilal561
Once this PR has been reviewed and has the lgtm label, please assign shouren for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Welcome @MuhammadBilal561! It looks like this is your first PR to Project-HAMi/HAMi 🎉

@hami-robot hami-robot Bot added the size/L label Aug 11, 2026
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Override environment allocation

Layer / File(s) Summary
Generate filtered override environment
pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go
The plugin filters HAMi-related environment variables and indexed variants, sorts their keys, and renders newline-delimited entries.
Persist and mount override environment
pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go
During non-MIG allocation, the plugin writes overrideEnv to the container cache and mounts it read-only at /overrideEnv. Write failures are logged without failing allocation.

libvgpu reference update

Layer / File(s) Summary
Update libvgpu reference
libvgpu
The submodule reference points to a new commit.

ListAndWatch test cleanup

Layer / File(s) Summary
Remove send-error test coverage
pkg/device-plugin/nvidiadevice/nvinternal/plugin/server_test.go
The mock server and ListAndWatch Send-error test were removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant NVIDIADevicePlugin
  participant buildOverrideEnv
  participant ContainerCache
  participant ContainerRuntime
  NVIDIADevicePlugin->>buildOverrideEnv: Filter HAMi environment variables
  buildOverrideEnv-->>NVIDIADevicePlugin: Return sorted overrideEnv content
  NVIDIADevicePlugin->>ContainerCache: Write overrideEnv
  NVIDIADevicePlugin->>ContainerRuntime: Mount file read-only at /overrideEnv
Loading

Possibly related PRs

Suggested reviewers: nakshatra480

Poem

A rabbit sorts the keys in line,
Writes HAMi vars in neat design.
A read-only path now joins the nest,
While old Send tests take their rest.
libvgpu hops to commits new.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: enforcing vGPU memory isolation for child and SSH processes through overrideEnv injection.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 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.

Signed-off-by: MuhammadBilal561 <bilalrehan2006@gmail.com>

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go (1)

892-912: 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift

Store overrideEnv outside the writable cache mount.

overrideEnvHostPath is inside cacheFileHostDirectory. The container also receives this directory as a read-write mount at hostHookPath/vgpu. A process can modify hostHookPath/vgpu/overrideEnv and change the constraints that libvgpu reads for later processes.

Write overrideEnv in a separate host directory that is not exposed through a writable container mount. Keep that directory non-writable to the container. Add lifecycle cleanup for the separate file.

🤖 Prompt for 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.

In `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go` around lines 892
- 912, Update the overrideEnv handling near buildOverrideEnv and the
response.Mounts setup so the file is written under a separate host directory,
outside cacheFileHostDirectory and any writable container mount. Mount only that
separate file read-only at /overrideEnv, ensure the containing directory is not
exposed writable, and add lifecycle cleanup for the generated file and
directory.
🤖 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 `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go`:
- Around line 894-897: Update the override-file handling around os.WriteFile to
create the file unconditionally, including when overrideEnvContent is empty. If
creation fails, call PodAllocationFailed, return the write error immediately,
and do not add the mount; only proceed to mount the successfully created file.

---

Outside diff comments:
In `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go`:
- Around line 892-912: Update the overrideEnv handling near buildOverrideEnv and
the response.Mounts setup so the file is written under a separate host
directory, outside cacheFileHostDirectory and any writable container mount.
Mount only that separate file read-only at /overrideEnv, ensure the containing
directory is not exposed writable, and add lifecycle cleanup for the generated
file and directory.
🪄 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: f4a499f1-7e15-424d-9fd2-be2d71a1b06d

📥 Commits

Reviewing files that changed from the base of the PR and between 634bf2b and 4560817.

📒 Files selected for processing (3)
  • libvgpu
  • pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go
  • pkg/device-plugin/nvidiadevice/nvinternal/plugin/server_test.go
💤 Files with no reviewable changes (1)
  • pkg/device-plugin/nvidiadevice/nvinternal/plugin/server_test.go

Comment on lines +894 to +897
if len(overrideEnvContent) > 0 {
if err := os.WriteFile(overrideEnvHostPath, []byte(overrideEnvContent), 0644); err != nil {
klog.ErrorS(err, "failed to write overrideEnv for container", "path", overrideEnvHostPath)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Fail allocation when the override file cannot be created.

If os.WriteFile fails, this code still adds a mount whose host source may not exist. Kubelet can then fail the container mount after Allocate reports success. Continuing also removes the memory-isolation guarantee for child processes.

Create the file unconditionally, including an empty file when needed. If creation fails, call PodAllocationFailed and return the error before adding the mount.

Proposed fix
-				if len(overrideEnvContent) > 0 {
-					if err := os.WriteFile(overrideEnvHostPath, []byte(overrideEnvContent), 0644); err != nil {
-						klog.ErrorS(err, "failed to write overrideEnv for container", "path", overrideEnvHostPath)
-					}
+				if err := os.WriteFile(overrideEnvHostPath, []byte(overrideEnvContent), 0644); err != nil {
+					PodAllocationFailed(nodename, current, NodeLockNvidia)
+					return nil, fmt.Errorf("write overrideEnv for container: %w", err)
 				}

Also applies to: 910-912

🤖 Prompt for 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.

In `@pkg/device-plugin/nvidiadevice/nvinternal/plugin/server.go` around lines 894
- 897, Update the override-file handling around os.WriteFile to create the file
unconditionally, including when overrideEnvContent is empty. If creation fails,
call PodAllocationFailed, return the write error immediately, and do not add the
mount; only proceed to mount the successfully created file.

@mesutoezdil

Copy link
Copy Markdown
Contributor

This is being closed because it does not comply with the contribution guidelines.

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.

2 participants