KVM: TDX: Charge misc cgroup before allocating HKID
Subsystems (4)
PATCH 1/1 KVM: TDX: Charge misc cgroup before allocating HKID
Add a tdx_hkid_alloc() helper that charges the misc cgroup before allocating an HKID, and unwind the charge if HKID allocation fails.
__tdx_td_init() currently allocates an HKID before charging the misc cgroup. If the charge fails, the error path calls tdx_hkid_free(), which uncharges a resource that was never successfully charged. This can make the misc-cgroup usage negative.
Charge the cgroup before allocating the HKID. Wrapping both steps in tdx_hkid_alloc() makes it the exact counterpart of tdx_hkid_free(), i.e. keeps resource allocation and release symmetric, and lets __tdx_td_init() simply bail on failure instead of open coding the unwind.
--- arch/x86/kvm/vmx/tdx.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)
arch/x86/kvm/vmx/tdx.c +27 -7
@@ -260,6 +260,31 @@ static __always_inline void tdvmcall_set_return_val(struct kvm_vcpu *vcpu,to_tdx (vcpu)->vp_enter_args.r11 = val; } +static int tdx_hkid_alloc (struct kvm_tdx *kvm_tdx) +{ +int ret; + + kvm_tdx->misc_cg =get_current_misc_cg (); + ret =misc_cg_try_charge (MISC_CG_RES_TDX , kvm_tdx->misc_cg,1 ); +if (ret) + goto put_misc_cg; + + ret =tdx_guest_keyid_alloc (); +if (ret <0 ) + goto uncharge_misc_cg; + + kvm_tdx->hkid = ret; +return 0 ; + +uncharge_misc_cg: +misc_cg_uncharge (MISC_CG_RES_TDX , kvm_tdx->misc_cg,1 ); +put_misc_cg: +put_misc_cg (kvm_tdx->misc_cg); + kvm_tdx->misc_cg =NULL ; + +return ret; +} +static inline void tdx_hkid_free (struct kvm_tdx *kvm_tdx) {tdx_guest_keyid_free (kvm_tdx->hkid);
@@ -2445,14 +2470,9 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,u64 err, rcx; *seamcall_err =0 ; - ret =tdx_guest_keyid_alloc (); -if (ret <0 ) -return ret; - kvm_tdx->hkid = ret; - kvm_tdx->misc_cg =get_current_misc_cg (); - ret =misc_cg_try_charge (MISC_CG_RES_TDX , kvm_tdx->misc_cg,1 ); + ret =tdx_hkid_alloc (kvm_tdx);if (ret) - goto free_hkid; +return ret; ret = -ENOMEM ;
base-commit: 1b731e5ded480bd1e5546aed35584238661ce72e
Signature · 1 line
2.46.0
Quoted 13 lines · Binbin Wu
Add a
tdx_hkid_alloc()helper that charges the misc cgroup before allocating an HKID, and unwind the charge if HKID allocation fails.
__tdx_td_init()currently allocates an HKID before charging the misc cgroup. If the charge fails, the error path callstdx_hkid_free(), which uncharges a resource that was never successfully charged. This can make the misc-cgroup usage negative.Charge the cgroup before allocating the HKID. Wrapping both steps in
tdx_hkid_alloc()makes it the exact counterpart oftdx_hkid_free(), i.e. keeps resource allocation and release symmetric, and lets__tdx_td_init()simply bail on failure instead of open coding the unwind.
Quoted 5 lines
Reported-by [email protected]Fixes 7c035bea9407 ("KVM: TDX: Register TDX host key IDs to cgroup misc controller")Signed-off-by Binbin Wu <[email protected]>
As a straightforward bug fix:
But it seems a bit awkward how the keyid allocator is carefully hidden away in arch/x86 but the KVM caller does the cgroup maintenance. Hmm, I'd wonder if we could move the struct misc_cg pointer to struct tdx_td or otherwise pass it in, and make this stuff managed by arch/x86.
I think the only reason it is KVM managed is that an old cgroup patch got applied on top of the base series. The old design from the era of that patch had the keyid range exported, and KVM used it to manage the keyid allocation. Then when the keyid range got hidden, it resulted in the alloc/free functions getting exported. So I wonder if the new tdx_hkid_alloc() should live in arch/x86. Otherwise we are doing the thing where KVM just wraps arch/x86 exports to do what it needed to do in the first place.
But not needed for this patch in any case.
Quoted 59 lines
--- arch/x86/kvm/vmx/tdx.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-)arch/x86/kvm/vmx/tdx.c +27
-7@@ -260,6 +260,31 @@ static __always_inline void tdvmcall_set_return_val(struct kvm_vcpu *vcpu,to_tdx (vcpu)->vp_enter_args.r11 = val; } +static int tdx_hkid_alloc (struct kvm_tdx *kvm_tdx) +{ +int ret; + + kvm_tdx->misc_cg =get_current_misc_cg (); + ret =misc_cg_try_charge (MISC_CG_RES_TDX , kvm_tdx->misc_cg,1 ); +if (ret) + goto put_misc_cg; + + ret =tdx_guest_keyid_alloc (); +if (ret <0 ) + goto uncharge_misc_cg; + + kvm_tdx->hkid = ret; +return 0 ; + +uncharge_misc_cg: +misc_cg_uncharge (MISC_CG_RES_TDX , kvm_tdx->misc_cg,1 ); +put_misc_cg: +put_misc_cg (kvm_tdx->misc_cg); + kvm_tdx->misc_cg =NULL ; + +return ret; +} +static inline void tdx_hkid_free (struct kvm_tdx *kvm_tdx) {tdx_guest_keyid_free (kvm_tdx->hkid);@@ -2445,14 +2470,9 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,u64 err, rcx; *seamcall_err =0 ; - ret =tdx_guest_keyid_alloc (); -if (ret <0 ) -return ret; - kvm_tdx->hkid = ret; - kvm_tdx->misc_cg =get_current_misc_cg (); - ret =misc_cg_try_charge (MISC_CG_RES_TDX , kvm_tdx->misc_cg,1 ); + ret =tdx_hkid_alloc (kvm_tdx);if (ret) - goto free_hkid; +return ret; ret = -ENOMEM ;base-commit:
1b731e5ded480bd1e5546aed35584238661ce72e
Quoted 28 lines · Rick P
Quoted 13 lines · Binbin Wu
Add a
tdx_hkid_alloc()helper that charges the misc cgroup before allocating an HKID, and unwind the charge if HKID allocation fails.
__tdx_td_init()currently allocates an HKID before charging the misc cgroup. If the charge fails, the error path callstdx_hkid_free(), which uncharges a resource that was never successfully charged. This can make the misc-cgroup usage negative.Charge the cgroup before allocating the HKID. Wrapping both steps in
tdx_hkid_alloc()makes it the exact counterpart oftdx_hkid_free(), i.e. keeps resource allocation and release symmetric, and lets__tdx_td_init()simply bail on failure instead of open coding the unwind.Quoted 5 lines
Reported-by [email protected]Fixes 7c035bea9407 ("KVM: TDX: Register TDX host key IDs to cgroup misc controller")Signed-off-by Binbin Wu <[email protected]>As a straightforward bug fix:
Reviewed-by Rick Edgecombe <[email protected]>But it seems a bit awkward how the keyid allocator is carefully hidden away in
arch/x86but the KVM caller does the cgroup maintenance. Hmm, I'd wonder if we could move the structmisc_cgpointer to structtdx_tdor otherwise pass it in, and make this stuff managed byarch/x86.
Quoted 7 lines
I think the only reason it is KVM managed is that an old cgroup patch got applied on top of the base series. The old design from the era of that patch had the keyid range exported, and KVM used it to manage the keyid allocation. Then when the keyid range got hidden, it resulted in the alloc/free functions getting exported. So I wonder if the new
tdx_hkid_alloc()should live inarch/x86. Otherwise we are doing the thing where KVM just wrapsarch/x86exports to do what it needed to do in the first place.
Yes, make sense.
Quoted 2 lines
But not needed for this patch in any case.
It could be a separate cleanup patch.
Quoted 14 lines · Binbin Wu
Add a
tdx_hkid_alloc()helper that charges the misc cgroup before allocating an HKID, and unwind the charge if HKID allocation fails.
__tdx_td_init()currently allocates an HKID before charging the misc cgroup. If the charge fails, the error path callstdx_hkid_free(), which uncharges a resource that was never successfully charged. This can make the misc-cgroup usage negative.Charge the cgroup before allocating the HKID. Wrapping both steps in
tdx_hkid_alloc()makes it the exact counterpart oftdx_hkid_free(), i.e. keeps resource allocation and release symmetric, and lets__tdx_td_init()simply bail on failure instead of open coding the unwind.
Quoted 5 lines
Reported-by [email protected]Fixes 7c035bea9407 ("KVM: TDX: Register TDX host key IDs to cgroup misc controller")Signed-off-by Binbin Wu <[email protected]>
Quoted 9 lines · Binbin Wu
Add a
tdx_hkid_alloc()helper that charges the misc cgroup before allocating an HKID, and unwind the charge if HKID allocation fails.
__tdx_td_init()currently allocates an HKID before charging the misc cgroup. If the charge fails, the error path callstdx_hkid_free(), which uncharges a resource that was never successfully charged. This can make the misc-cgroup usage negative.[...]
Applied to kvm-x86 vmx, thanks!
[1/1] KVM: TDX: Charge misc cgroup before allocating HKID
https://github.com/kvm-x86/linux/commit/93f1e8445a2f