Skip to content

Commit 91b692a

Browse files
committed
KVM: SEV: provide helpers to charge/uncharge misc_cg
Avoid code duplication across all callers of misc_cg_try_charge and misc_cg_uncharge. The resource type for KVM is always derived from sev->es_active, and the quantity is always 1. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent f4d3165 commit 91b692a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

arch/x86/kvm/svm/sev.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,26 @@ static bool __sev_recycle_asids(int min_asid, int max_asid)
120120
return true;
121121
}
122122

123+
static int sev_misc_cg_try_charge(struct kvm_sev_info *sev)
124+
{
125+
enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
126+
return misc_cg_try_charge(type, sev->misc_cg, 1);
127+
}
128+
129+
static void sev_misc_cg_uncharge(struct kvm_sev_info *sev)
130+
{
131+
enum misc_res_type type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
132+
misc_cg_uncharge(type, sev->misc_cg, 1);
133+
}
134+
123135
static int sev_asid_new(struct kvm_sev_info *sev)
124136
{
125137
int asid, min_asid, max_asid, ret;
126138
bool retry = true;
127-
enum misc_res_type type;
128139

129-
type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
130140
WARN_ON(sev->misc_cg);
131141
sev->misc_cg = get_current_misc_cg();
132-
ret = misc_cg_try_charge(type, sev->misc_cg, 1);
142+
ret = sev_misc_cg_try_charge(sev);
133143
if (ret) {
134144
put_misc_cg(sev->misc_cg);
135145
sev->misc_cg = NULL;
@@ -162,7 +172,7 @@ static int sev_asid_new(struct kvm_sev_info *sev)
162172

163173
return asid;
164174
e_uncharge:
165-
misc_cg_uncharge(type, sev->misc_cg, 1);
175+
sev_misc_cg_uncharge(sev);
166176
put_misc_cg(sev->misc_cg);
167177
sev->misc_cg = NULL;
168178
return ret;
@@ -179,7 +189,6 @@ static void sev_asid_free(struct kvm_sev_info *sev)
179189
{
180190
struct svm_cpu_data *sd;
181191
int cpu;
182-
enum misc_res_type type;
183192

184193
mutex_lock(&sev_bitmap_lock);
185194

@@ -192,8 +201,7 @@ static void sev_asid_free(struct kvm_sev_info *sev)
192201

193202
mutex_unlock(&sev_bitmap_lock);
194203

195-
type = sev->es_active ? MISC_CG_RES_SEV_ES : MISC_CG_RES_SEV;
196-
misc_cg_uncharge(type, sev->misc_cg, 1);
204+
sev_misc_cg_uncharge(sev);
197205
put_misc_cg(sev->misc_cg);
198206
sev->misc_cg = NULL;
199207
}

0 commit comments

Comments
 (0)