Merge pull request #51 from kubuntugamer/fix/gsp-abi-value-validation

Thanks @kubuntugamer for tightening gsp_abi validation so typos warn instead of silently selecting 620 framing!
This commit is contained in:
Eric Hartford
2026-12-01 00:16:47 -05:00
committed by GitHub
3 changed files with 50 additions and 9 deletions

View File

@ -67,7 +67,7 @@ extern bool nv_rs_rmapi_gpu_needs_registers(void *node, u32 minor);
extern int nv_rs_rmapi_gpu_attach_registers(void *node, u32 minor, const u32 *values, u32 count);
extern int nv_rs_rmapi_set_host_bridge(void *node, u16 vendor, u16 device);
extern int nv_rs_rmapi_set_force_p2p(void *node, u32 force_p2p_mask);
extern void nv_rs_rmapi_set_gsp_abi(u32 gsp_abi_ver);
extern bool nv_rs_rmapi_set_gsp_abi(u32 gsp_abi_ver);
extern void nv_rs_rmapi_set_driver_version(const char *version, size_t len);
extern int nv_rs_rmapi_gpu_release(void *node, u32 minor);
extern u32 nv_rs_rmapi_register_fd(bool caller_registered, bool target_control,
@ -1880,7 +1880,9 @@ int nv_rs_rmapi_node_init(void)
pci_dev_put(bridge);
}
nv_rs_rmapi_set_force_p2p(state, force_p2p);
nv_rs_rmapi_set_gsp_abi(gsp_abi);
if (!nv_rs_rmapi_set_gsp_abi(gsp_abi))
pr_warn("nvidia_rs: gsp_abi=%u is not 580 or 620, keeping the default GSP-RM ABI\n",
gsp_abi);
if (driver_version && *driver_version)
nv_rs_rmapi_set_driver_version(driver_version, strlen(driver_version));
}

View File

@ -1799,20 +1799,26 @@ pub unsafe extern "C" fn nv_rs_rmapi_set_driver_version(ptr: *const u8, len: usi
/// Sets the GSP-RM wire protocol ABI version (`620` = 620.70.06 MCTP+NVDM default,
/// `580` = 580.178.04 legacy).
///
/// Returns `false` when `gsp_abi_ver` is neither `580` nor `620`; the active ABI is
/// then left unchanged, so an out-of-range `gsp_abi` module parameter keeps the
/// default framing instead of silently selecting the 620 wire protocol.
#[no_mangle]
pub extern "C" fn nv_rs_rmapi_set_gsp_abi(gsp_abi_ver: u32) {
pub extern "C" fn nv_rs_rmapi_set_gsp_abi(gsp_abi_ver: u32) -> bool {
#[cfg(not(nv_rs_edu_guest))]
{
let abi = if gsp_abi_ver == 580 {
nv_rs_core::gpu::gsp_rpc::GspAbiVersion::R580
} else {
nv_rs_core::gpu::gsp_rpc::GspAbiVersion::R620
};
crate::gsp_bringup::set_gsp_abi(abi);
match nv_rs_core::gpu::gsp_rpc::GspAbiVersion::from_module_param(gsp_abi_ver) {
Some(abi) => {
crate::gsp_bringup::set_gsp_abi(abi);
true
}
None => false,
}
}
#[cfg(nv_rs_edu_guest)]
{
let _ = gsp_abi_ver;
true
}
}

View File

@ -65,6 +65,20 @@ pub enum GspAbiVersion {
/// Default GSP-RM ABI for Turing–Blackwell–Rubin (`620.70.06`).
pub const DEFAULT_GSP_ABI: GspAbiVersion = GspAbiVersion::R620;
impl GspAbiVersion {
/// Parses the `gsp_abi` module parameter, which takes only `580` or `620`.
///
/// Anything else is rejected rather than coerced: the value selects how bytes
/// are framed on the GSP queues, so a typo must not silently switch protocols.
pub const fn from_module_param(value: u32) -> Option<Self> {
match value {
580 => Some(Self::R580),
620 => Some(Self::R620),
_ => None,
}
}
}
/// 620.70.06 MCTP/NVDM framing constants (`mctp_format.h`, `nvdm_format.h`, `message_queue_priv.h`).
pub const MCTP_MAGIC: u32 = 0x4d43_5450; // "MCTP"
pub const MCTP_MAGIC_SEEN: u32 = 0x5345_454e; // "SEEN"
@ -1825,6 +1839,25 @@ mod tests {
assert_eq!(out, payload);
}
#[test]
fn gsp_abi_module_param_accepts_580_and_620_only() {
assert_eq!(
GspAbiVersion::from_module_param(580),
Some(GspAbiVersion::R580)
);
assert_eq!(
GspAbiVersion::from_module_param(620),
Some(GspAbiVersion::R620)
);
// Everything that is not 580 used to select R620 silently.
assert_eq!(GspAbiVersion::from_module_param(0), None);
assert_eq!(GspAbiVersion::from_module_param(1), None);
assert_eq!(GspAbiVersion::from_module_param(62_073), None);
assert_eq!(GspAbiVersion::from_module_param(u32::MAX), None);
// The module default stays 620.70.06 framing.
assert_eq!(DEFAULT_GSP_ABI, GspAbiVersion::R620);
}
#[test]
fn gsp_620_mctp_nvdm_gmcapi_wire_parity() {
// Exact mctp_format.h + nvdm_format.h + message_queue_priv.h bitfields.