Skip to content

[AMD-SMI] Clean up SMI - #3655

Merged
marifamd merged 6 commits into
developfrom
dgalants/smi-cleanup
Apr 17, 2026
Merged

[AMD-SMI] Clean up SMI#3655
marifamd merged 6 commits into
developfrom
dgalants/smi-cleanup

Conversation

@dmitrii-galantsev

@dmitrii-galantsev dmitrii-galantsev commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

Clean up SMI

@dmitrii-galantsev dmitrii-galantsev changed the title [SWDEV-544298] AINIC implementation (#2798) [AMD-SMI] Clean up SMI Mar 2, 2026
@dmitrii-galantsev
dmitrii-galantsev force-pushed the dgalants/smi-cleanup branch 2 times, most recently from 20f402b to 870e661 Compare March 2, 2026 19:54
@marifamd
marifamd force-pushed the dgalants/smi-cleanup branch from 769016d to 66b3957 Compare March 4, 2026 23:23
@dmitrii-galantsev
dmitrii-galantsev marked this pull request as ready for review March 5, 2026 23:01
@dmitrii-galantsev
dmitrii-galantsev requested a review from a team as a code owner March 5, 2026 23:01
Copilot AI review requested due to automatic review settings March 5, 2026 23:01
@dmitrii-galantsev
dmitrii-galantsev force-pushed the dgalants/smi-cleanup branch 2 times, most recently from d02dbc8 to ad3b705 Compare March 5, 2026 23:05

This comment was marked as outdated.

@marifamd
marifamd force-pushed the dgalants/smi-cleanup branch from ad3b705 to 0736d81 Compare March 6, 2026 07:00

This comment was marked as outdated.

This comment was marked as outdated.

The C API was renamed from amdsmi_get_cpusocket_handles to
amdsmi_get_cpu_handles in commit 4260c0a, but the Python layer
kept the old name. This left a stale function name throughout the
Python interface and CLI that didn't match the C API.

Rename the Python function to match the C API and update all call
sites. Also remove the redundant dict-wrapping amdsmi_get_cpu_handles
function that shadowed the renamed implementation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
dmitrii-galantsev and others added 4 commits April 15, 2026 12:33
…udit

Fixes 10 bugs identified in the code hygiene audit (issue #3634):

- py-interface: sys.getsizeof → ctypes.sizeof for correct C type sizes
- py-interface: PAGE_FAULT_START mapped to wrong enum (copy-paste bug)
- amd_smi_no_drm_nic.cc: sizeof(array-1) → sizeof(array) - 1 in snprintf
- amd_smi_system.cc: OOB write via sockets[0] on empty vector → push_back
- amd_smi_utils.cc: num_pages = 0 assigned to pointer, not *num_pages
- rocm_smi_device.cc: isRestartInProgress checked stale input instead of
  local isSystemAMDGPUModuleLive
- rocm_smi_utils.cc: dangling pointer from local string c_str() → literal
- rocm_smi_utils.cc: pclose(NULL) UB when popen fails → add NULL guard
- amd_smi_lspci_commands.cc: multi-char literal ']----' → string "]----"
- rocm_smi_kfd.cc: stoi truncates uint64_t GPU IDs → stoull

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Fixes 5 more issues from the audit (issue #3634):

- amd_smi_lspci_commands.cc: Validate BDF and search_key before passing
  to popen() to prevent shell injection (C-1)
- amd_smi_system.cc: Replace popen("readlink ...") with POSIX readlink()
  syscall to eliminate command injection vector (C-2)
- smi_nic.cpp: Allocate proper buffer for ethtool_perm_addr flexible
  array member to prevent stack buffer overflow (H-6)
- amd_smi_utils.cc: Add width specifiers to sscanf %s formats to prevent
  buffer overflows on char[10] stack buffers (H-5)
- amd_smi_cper.cc: Close file descriptor on short read error path to
  prevent fd leak (M-7)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…e hygiene audit

Fixes bugs of the same classes addressed in commit 562d704b3b, found in
code added since that commit:

- rocm_smi_kfd.cc: stoi → stoull for GPU ID stored in uint64_t (same
  bug as previously fixed at line 435)
- amd_smi_utils.cc: Guard pclose(NULL) UB in shared_ptr<FILE> deleter
- rocm_smi.cc: Fix refcount/fd leak on 3 error paths in
  rsmi_event_notification_init() — refcount was incremented but never
  decremented on ioctl failure, invalid anon_fd, or fdopen failure
- rocm_smi.cc: Add %255s/%255[^\n] width specifiers to sscanf calls
- Replace &vec[0] with vec.data() across production code, tests, and
  examples to eliminate UB when vectors are empty (31 locations)
- amd_smi.cc: Convert non-standard VLA to std::vector

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
… audit

Fixes bugs of the same classes addressed in commit 43dbf30eb3, found in
code added since that commit:

- fdinfo.cc: Add %12s width specifier to sscanf on 13-byte char buffer
  to prevent stack buffer overflow from malformed /proc fdinfo entries
- rocm_smi_binary_parser.cc: Close file handle on calloc failure, and
  close file handle + free buffer on unsupported metrics version, to
  prevent fd and memory leaks on error paths
- rocm_smi_monitor.cc: Close DIR* handle on regex exception path to
  prevent directory handle leak

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>
Remove search_key from the shell command entirely. Previously,
get_lspci_device_data() interpolated user-influenced search_key
into a shell pipeline ("lspci ... | grep -i 'KEY'"), requiring
fragile character sanitization. Now only the regex-validated BDF
touches the shell; filtering is done in C++ via std::string::find().

Co-Authored-By: Claude Opus 4 (1M context) <noreply@anthropic.com>

Copilot AI 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.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread projects/amdsmi/src/nic/ai-nic/amdsmi_unified/src/smi_nic.cpp
Comment thread projects/amdsmi/src/amd_smi/amd_smi_cper.cc
@marifamd
marifamd merged commit 291c930 into develop Apr 17, 2026
36 checks passed
@marifamd
marifamd deleted the dgalants/smi-cleanup branch April 17, 2026 20:30
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.

4 participants