unsafe fn rewrite_attached_ids(
fd: c_int,
rc: c_int,
req: c_ulong,
arg: *mut c_void,
)Expand description
Scrub the hidden GPUs out of a successful GET_ATTACHED_IDS / GET_PROBED_IDS response so
the NVIDIA libraries never try to peer-init a GPU the container cannot reach: it drops every id
whose /dev/nvidia node is absent. Split from filtered_ioctl so the whole thing runs under
catch_unwind.
The guards run cheap-to-expensive and each returns early — leaving the response untouched — the moment one fails, so the overwhelmingly common non-enumeration ioctl pays almost nothing:
- Request gate: the syscall succeeded (
rc == 0),argis non-null, and the request is the RM_CONTROL ioctl matched by DIR|TYPE|NR (size-agnostic, viaioc_no_size). - fd identity gate: when
NVIDIACTL_RDEVwas resolved at install,fstatthe fd and require a character device whoserdevequals/dev/nvidiactl’s — so only that char device’s responses are rewritten. Anfstatfailure passes through untouched; if the rdev was never resolved the gate is skipped and matching relies on the request/size checks alone. - Payload gate: the RM call itself reports success (
ctrl.status == 0) and carries a parameter pointer (ctrl.params != 0). - Command + exact-size dispatch:
cmdselects ATTACHED or PROBED, andparams_sizemust equal that command’s exact struct size. Requiring the exact size both disambiguates the two (their layouts share a leading array) and stops a lying size from steering a rewrite under a different layout. Both APIs are filtered so behavior is uniform across driver versions — buggy 570-595 where it actually drops GPUs, and correct drivers where the strict-subset rule makes it a no-op. - Filter the leading
gpuIds[32]:filter_idskeeps an id only whengpuid_to_minorresolves it and the corresponding/dev/nvidianode is present. For PROBED the trailingexcludedGpuIds[]is deliberately left untouched (it lists GPUs to exclude, not to use).
When PIXELFLUX_GPU_FILTER_DEBUG is set, the before/after live-id counts are logged.