KVM: TDX: Fix uninitialized error code for __tdx_bringup()
Subsystems (4)
PATCH 1/1 KVM: TDX: Fix uninitialized error code for __tdx_bringup()
Fix a Smatch static checker warning reported by Dan:
arch/x86/kvm/vmx/tdx.c:3464 __tdx_bringup() warn: missing error code 'r'
--- arch/x86/kvm/vmx/tdx.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +6 -2
@@ -3466,11 +3466,15 @@ static int __init __tdx_bringup(void)/* Check TDX module and KVM capabilities */ if (!tdx_get_supported_attrs (&tdx_sysinfo->td_conf) || - !tdx_get_supported_xfam (&tdx_sysinfo->td_conf)) + !tdx_get_supported_xfam (&tdx_sysinfo->td_conf)) { + r = -EINVAL ; goto get_sysinfo_err; + } -if (!(tdx_sysinfo->features.tdx_features0 &MD_FIELD_ID_FEATURES0_TOPOLOGY_ENUM )) +if (!(tdx_sysinfo->features.tdx_features0 &MD_FIELD_ID_FEATURES0_TOPOLOGY_ENUM )) { + r = -EINVAL ; goto get_sysinfo_err; + }/* * TDX has its own limit of maximum vCPUs it can support for all
Signature · 1 line
2.43.0
Quoted 24 lines · Tony Lindgren
Fix a Smatch static checker warning reported by Dan:
arch/x86/kvm/vmx/tdx.c:3464 __tdx_bringup() warn: missing error code 'r'Reported-by Dan Carpenter <[email protected]>Fixes 61bb28279623 ("KVM: TDX: Get system-wide info about TDX module on initialization")Signed-off-by Tony Lindgren <[email protected]>--- arch/x86/kvm/vmx/tdx.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-1@@ -3466,11 +3466,15 @@ static int __init __tdx_bringup(void)/* Check TDX module and KVM capabilities */ if (!tdx_get_supported_attrs (&tdx_sysinfo->td_conf) || - !tdx_get_supported_xfam (&tdx_sysinfo->td_conf)) + !tdx_get_supported_xfam (&tdx_sysinfo->td_conf)) { + r = -EINVAL ; goto get_sysinfo_err;
arch/x86/kvm/vmx/tdx.c static int __init __tdx_bringup(void) · line 3480
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +4
-1+ } -if (!(tdx_sysinfo->features.tdx_features0 &MD_FIELD_ID_FEATURES0_TOPOLOGY_ENUM )) +if (!(tdx_sysinfo->features.tdx_features0 &MD_FIELD_ID_FEATURES0_TOPOLOGY_ENUM )) { + r = -EINVAL ; goto get_sysinfo_err; + }/* * TDX has its own limit of maximum vCPUs it can support for all
Looking at the code, seems all checks after tdx_get_sysinfo() have the same pattern when the check fails:
r = -EINVAL; goto get_sysinfo_err;
How about we just initialize r to -EINVAL once before tdx_get_sysinfo() so that all 'r = -EINVAL;' can be removed? I think in this way the code would be simpler (see below diff [*])?
The "Fixes" tag would be hard to identify, though, because the diff touches the code introduced multiple commits. But I am not sure whether this is a true issue since AFAICT we can use multiple "Fixes" tags.
[*] the diff:
arch/x86/kvm/vmx/tdx.c +2 -3
@@ -3460,12 +3460,11 @@ static int __init __tdx_bringup(void)if (r) goto tdx_bringup_err; + r = -EINVAL ;/* Get TDX global information for later use */ tdx_sysinfo =tdx_get_sysinfo (); -if (WARN_ON_ONCE (!tdx_sysinfo)) { - r = -EINVAL ; +if (WARN_ON_ONCE (!tdx_sysinfo)) goto get_sysinfo_err; - }/* Check TDX module and KVM capabilities */ if (!tdx_get_supported_attrs(&tdx_sysinfo->td_conf) ||
@@ -3508,14 +3507,11 @@ static int __init __tdx_bringup(void)if (td_conf->max_vcpus_per_td <num_present_cpus ()) { pr_err("Disable TDX: MAX_VCPU_PER_TD (%u) smaller than
number of logical CPUs (%u).\n",
td_conf->max_vcpus_per_td,
num_present_cpus());
- r = -EINVAL;
goto get_sysinfo_err;
}- if (misc_cg_set_capacity(MISC_CG_RES_TDX,tdx_get_nr_guest_keyids())) {
- r = -EINVAL;
+ if (misc_cg_set_capacity(MISC_CG_RES_TDX,tdx_get_nr_guest_keyids()))
goto get_sysinfo_err;
- }
/*
* Leave hardware virtualization enabled after TDX is enabled
Quoted 7 lines · Kai
How about we just initialize r to
-EINVALonce beforetdx_get_sysinfo()so that all 'r =-EINVAL;' can be removed? I think in this way the code would be simpler (see below diff [*])?The "Fixes" tag would be hard to identify, though, because the diff touches the code introduced multiple commits. But I am not sure whether this is a true issue since AFAICT we can use multiple "Fixes" tags.
Your diff looks fine to me, however my personal preference would be to do the fix first then clean-up :)
Regards,
Tony
Sean Christopherson
maintainerQuoted 6 lines · Tony Lindgren
Quoted 5 lines · Kai
How about we just initialize r to
-EINVALonce beforetdx_get_sysinfo()so that all 'r =-EINVAL;' can be removed? I think in this way the code would be simpler (see below diff [*])?The "Fixes" tag would be hard to identify,
No, Fixes always points at the commit(s) that introduced buggy behavior. While one might argue that commit 61bb28279623 was set up to fail by earlier commits, that commit is unequivocally the one and only Fixes commit.
Quoted 6 lines
Quoted 3 lines
though, because the diff
touches the code introduced multiple commits. But I am not sure whether this is a true issue since AFAICT we can use multiple "Fixes" tags.Your diff looks fine to me, however my personal preference would be to do the fix first then clean-up :)
Eh, fixes can also harden against similar failures in the future. I don't see any reason to split this one up. The buggy commit was introduced in v6.16 and Kai's suggestion applies cleanly there, so the more aggressive fix won't lead to stable@ conflicts either.
In short, let's go straight to Kai's version.
Quoted 11 lines · Sean Christopherson
Quoted 6 lines · Tony Lindgren
Quoted 5 lines · Kai
How about we just initialize r to
-EINVALonce beforetdx_get_sysinfo()so that all 'r =-EINVAL;' can be removed? I think in this way the code would be simpler (see below diff [*])?The "Fixes" tag would be hard to identify,
No, Fixes always points at the commit(s) that introduced buggy behavior. While one might argue that commit
61bb28279623was set up to fail by earlier commits, that commit is unequivocally the one and only Fixes commit.
Good to know. Thanks Sean!