Skip to content

fix(vGPUmonitor): clamp corrupt device count from shared memory region - #2497

Closed
Nitish08-08 wants to merge 1 commit into
Project-HAMi:masterfrom
Nitish08-08:fix/vgpu-monitor-clamp-device-num
Closed

fix(vGPUmonitor): clamp corrupt device count from shared memory region#2497
Nitish08-08 wants to merge 1 commit into
Project-HAMi:masterfrom
Nitish08-08:fix/vgpu-monitor-clamp-device-num

Conversation

@Nitish08-08

@Nitish08-08 Nitish08-08 commented Aug 8, 2026

Copy link
Copy Markdown

DeviceNum() in the v0/v1 shared-memory spec (pkg/monitor/nvidia/{v0,v1}/spec.go) returned int(sr.num) unclamped. num is read from an mmap'd shared-memory cache written by the containerized libvgpu, so a torn/corrupt value (larger than the fixed [16] device arrays) makes collectContainerMetrics (cmd/vGPUmonitor/metrics.go) index out of bounds on every device array, and SetDeviceSmLimit/SetDeviceMemoryLimit write out of bounds - panicking the vGPUmonitor daemonset pod. This clamps DeviceNum() to [0, maxDevices], mirroring the existing activeProcs() hardening, and makes the limit setters iterate the clamped DeviceNum(). Verification: go test ./pkg/monitor/nvidia/v0/ ./pkg/monitor/nvidia/v1/ -short -count=1 passes; added regression cases for num > maxDevices and corrupt math.MaxUint64.

Summary by CodeRabbit

  • Bug Fixes
    • Improved NVIDIA device count handling by enforcing valid device limits.
    • Prevented resource limit operations from exceeding the supported device range.
    • Improved stability when shared device counts are invalid or out of bounds.

@hami-robot
hami-robot Bot requested review from FouoF and lengrongfu August 8, 2026 20:17
@hami-robot

hami-robot Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Nitish08-08
Once this PR has been reviewed and has the lgtm label, please assign fouof 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

@github-actions github-actions Bot added the kind/bug Something isn't working label Aug 8, 2026
@hami-robot hami-robot Bot added the size/M label Aug 8, 2026
@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 66833a74-b1fb-499a-b3e9-46ba254685d4

📥 Commits

Reviewing files that changed from the base of the PR and between 9d3e647 and ce243bc.

📒 Files selected for processing (1)
  • pkg/monitor/nvidia/v1/spec.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/monitor/nvidia/v1/spec.go

📝 Walkthrough

Walkthrough

DeviceNum now clamps NVIDIA device counts to valid bounds in v0 and v1. Shared-memory and device-memory limit setters use the clamped count for iteration.

Changes

NVIDIA device count bounds

Layer / File(s) Summary
Validated device iteration
pkg/monitor/nvidia/v0/spec.go, pkg/monitor/nvidia/v1/spec.go
DeviceNum clamps negative and oversized counts. SetDeviceSmLimit and SetDeviceMemoryLimit use the validated count for per-device updates.

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

Possibly related issues

Possibly related PRs

Suggested reviewers: lengrongfu, fouof, ouyangluwei163

Poem

A rabbit checks the GPU array,
And bounds each count with care.
No loop hops past the end,
Memory limits safely bend.
v0 and v1 now share the fare.

🚥 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: clamping corrupt vGPU monitor device counts from shared memory.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 💡 2
⚔️ Resolve merge conflicts 💡
  • Resolve merge conflict in branch fix/vgpu-monitor-clamp-device-num
🛠️ 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.

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

🧹 Nitpick comments (1)
pkg/monitor/nvidia/v0/spec_test.go (1)

52-53: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Exercise the bounded limit-setter paths in both test suites.

The new boundary cases validate only DeviceNum(). They do not prove that the two limit setters use the clamped count.

  • pkg/monitor/nvidia/v0/spec_test.go#L52-L53: call SetDeviceSmLimit and SetDeviceMemoryLimit with num = math.MaxUint64, then verify bounded writes and no panic.
  • pkg/monitor/nvidia/v1/spec_test.go#L138-L155: add the same setter assertions for the v1 specification.
🤖 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/monitor/nvidia/v0/spec_test.go` around lines 52 - 53, Extend the boundary
tests in pkg/monitor/nvidia/v0/spec_test.go:52-53 to use num = math.MaxUint64
with SetDeviceSmLimit and SetDeviceMemoryLimit, asserting bounded writes and no
panic. Add equivalent setter assertions in
pkg/monitor/nvidia/v1/spec_test.go:138-155, verifying both setters honor the
clamped device count.
🤖 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.

Nitpick comments:
In `@pkg/monitor/nvidia/v0/spec_test.go`:
- Around line 52-53: Extend the boundary tests in
pkg/monitor/nvidia/v0/spec_test.go:52-53 to use num = math.MaxUint64 with
SetDeviceSmLimit and SetDeviceMemoryLimit, asserting bounded writes and no
panic. Add equivalent setter assertions in
pkg/monitor/nvidia/v1/spec_test.go:138-155, verifying both setters honor the
clamped device count.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8551c801-3772-46c0-b922-4aeb70b1c1ec

📥 Commits

Reviewing files that changed from the base of the PR and between 3616313 and 9d3e647.

📒 Files selected for processing (4)
  • pkg/monitor/nvidia/v0/spec.go
  • pkg/monitor/nvidia/v0/spec_test.go
  • pkg/monitor/nvidia/v1/spec.go
  • pkg/monitor/nvidia/v1/spec_test.go

@Nitish08-08
Nitish08-08 force-pushed the fix/vgpu-monitor-clamp-device-num branch from 9d3e647 to ce243bc Compare August 8, 2026 20:22
@hami-robot hami-robot Bot added size/S and removed size/M labels Aug 8, 2026
DeviceNum() returned int(sr.num) unclamped even though num is read from an
mmap'd shared-memory cache written by the contained libvgpu. A torn or
corrupt value larger than maxDevices makes metrics.go iterate out of bounds
over the fixed-size [16] device arrays, and SetDeviceSmLimit/
SetDeviceMemoryLimit write OOB, panicking the vGPUmonitor daemonset pod.

Clamp DeviceNum() to [0, maxDevices], mirroring the existing activeProcs()
hardening, and make the limit setters iterate DeviceNum().

Signed-off-by: Nitish <nitishsahani0708@gmail.com>
@Nitish08-08
Nitish08-08 force-pushed the fix/vgpu-monitor-clamp-device-num branch from ce243bc to cc79045 Compare August 9, 2026 02:24
@Nitish08-08 Nitish08-08 closed this Aug 9, 2026
@Nitish08-08
Nitish08-08 deleted the fix/vgpu-monitor-clamp-device-num branch August 9, 2026 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant