Skip to content

Commit 7c035be

Browse files
Zhiming Hubonzini
authored andcommitted
KVM: TDX: Register TDX host key IDs to cgroup misc controller
TDX host key IDs (HKID) are limit resources in a machine, and the misc cgroup lets the machine owner track their usage and limits the possibility of abusing them outside the owner's control. The cgroup v2 miscellaneous subsystem was introduced to control the resource of AMD SEV & SEV-ES ASIDs. Likewise introduce HKIDs as a misc resource. Signed-off-by: Zhiming Hu <zhiming.hu@intel.com> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 20d9137 commit 7c035be

File tree

6 files changed

+31
-0
lines changed

6 files changed

+31
-0
lines changed

arch/x86/include/asm/tdx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const char *tdx_dump_mce_info(struct mce *m);
125125
const struct tdx_sys_info *tdx_get_sysinfo(void);
126126

127127
int tdx_guest_keyid_alloc(void);
128+
u32 tdx_get_nr_guest_keyids(void);
128129
void tdx_guest_keyid_free(unsigned int keyid);
129130

130131
struct tdx_td {
@@ -180,6 +181,7 @@ u64 tdh_phymem_page_wbinvd_tdr(struct tdx_td *td);
180181
static inline void tdx_init(void) { }
181182
static inline int tdx_cpu_enable(void) { return -ENODEV; }
182183
static inline int tdx_enable(void) { return -ENODEV; }
184+
static inline u32 tdx_get_nr_guest_keyids(void) { return 0; }
183185
static inline const char *tdx_dump_mce_info(struct mce *m) { return NULL; }
184186
static inline const struct tdx_sys_info *tdx_get_sysinfo(void) { return NULL; }
185187
#endif /* CONFIG_INTEL_TDX_HOST */

arch/x86/kvm/vmx/tdx.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <linux/cpu.h>
33
#include <asm/cpufeature.h>
4+
#include <linux/misc_cgroup.h>
45
#include <asm/tdx.h>
56
#include "capabilities.h"
67
#include "mmu.h"
@@ -140,6 +141,9 @@ static inline void tdx_hkid_free(struct kvm_tdx *kvm_tdx)
140141
tdx_guest_keyid_free(kvm_tdx->hkid);
141142
kvm_tdx->hkid = -1;
142143
atomic_dec(&nr_configured_hkid);
144+
misc_cg_uncharge(MISC_CG_RES_TDX, kvm_tdx->misc_cg, 1);
145+
put_misc_cg(kvm_tdx->misc_cg);
146+
kvm_tdx->misc_cg = NULL;
143147
}
144148

145149
static inline bool is_hkid_assigned(struct kvm_tdx *kvm_tdx)
@@ -675,6 +679,10 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
675679
if (ret < 0)
676680
return ret;
677681
kvm_tdx->hkid = ret;
682+
kvm_tdx->misc_cg = get_current_misc_cg();
683+
ret = misc_cg_try_charge(MISC_CG_RES_TDX, kvm_tdx->misc_cg, 1);
684+
if (ret)
685+
goto free_hkid;
678686

679687
ret = -ENOMEM;
680688

@@ -1459,6 +1467,11 @@ static int __init __tdx_bringup(void)
14591467
goto get_sysinfo_err;
14601468
}
14611469

1470+
if (misc_cg_set_capacity(MISC_CG_RES_TDX, tdx_get_nr_guest_keyids())) {
1471+
r = -EINVAL;
1472+
goto get_sysinfo_err;
1473+
}
1474+
14621475
/*
14631476
* Leave hardware virtualization enabled after TDX is enabled
14641477
* successfully. TDX CPU hotplug depends on this.
@@ -1475,6 +1488,7 @@ static int __init __tdx_bringup(void)
14751488
void tdx_cleanup(void)
14761489
{
14771490
if (enable_tdx) {
1491+
misc_cg_set_capacity(MISC_CG_RES_TDX, 0);
14781492
__tdx_cleanup();
14791493
kvm_disable_virtualization();
14801494
}

arch/x86/kvm/vmx/tdx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ enum kvm_tdx_state {
2121
struct kvm_tdx {
2222
struct kvm kvm;
2323

24+
struct misc_cg *misc_cg;
2425
int hkid;
2526
enum kvm_tdx_state state;
2627

arch/x86/virt/vmx/tdx/tdx.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,12 @@ const struct tdx_sys_info *tdx_get_sysinfo(void)
14761476
}
14771477
EXPORT_SYMBOL_GPL(tdx_get_sysinfo);
14781478

1479+
u32 tdx_get_nr_guest_keyids(void)
1480+
{
1481+
return tdx_nr_guest_keyids;
1482+
}
1483+
EXPORT_SYMBOL_GPL(tdx_get_nr_guest_keyids);
1484+
14791485
int tdx_guest_keyid_alloc(void)
14801486
{
14811487
return ida_alloc_range(&tdx_guest_keyid_pool, tdx_guest_keyid_start,

include/linux/misc_cgroup.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ enum misc_res_type {
1717
MISC_CG_RES_SEV,
1818
/** @MISC_CG_RES_SEV_ES: AMD SEV-ES ASIDs resource */
1919
MISC_CG_RES_SEV_ES,
20+
#endif
21+
#ifdef CONFIG_INTEL_TDX_HOST
22+
/* Intel TDX HKIDs resource */
23+
MISC_CG_RES_TDX,
2024
#endif
2125
/** @MISC_CG_RES_TYPES: count of enum misc_res_type constants */
2226
MISC_CG_RES_TYPES

kernel/cgroup/misc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ static const char *const misc_res_name[] = {
2424
/* AMD SEV-ES ASIDs resource */
2525
"sev_es",
2626
#endif
27+
#ifdef CONFIG_INTEL_TDX_HOST
28+
/* Intel TDX HKIDs resource */
29+
"tdx",
30+
#endif
2731
};
2832

2933
/* Root misc cgroup */

0 commit comments

Comments
 (0)