Skip to content

Commit d80b64f

Browse files
MiaoheLinbonzini
authored andcommitted
KVM: SVM: Fix potential memory leak in svm_cpu_init()
When kmalloc memory for sd->sev_vmcbs failed, we forget to free the page held by sd->save_area. Also get rid of the var r as '-ENOMEM' is actually the only possible outcome here. Reviewed-by: Liran Alon <[email protected]> Reviewed-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Miaohe Lin <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 23520b2 commit d80b64f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

arch/x86/kvm/svm.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,33 +1005,32 @@ static void svm_cpu_uninit(int cpu)
10051005
static int svm_cpu_init(int cpu)
10061006
{
10071007
struct svm_cpu_data *sd;
1008-
int r;
10091008

10101009
sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
10111010
if (!sd)
10121011
return -ENOMEM;
10131012
sd->cpu = cpu;
1014-
r = -ENOMEM;
10151013
sd->save_area = alloc_page(GFP_KERNEL);
10161014
if (!sd->save_area)
1017-
goto err_1;
1015+
goto free_cpu_data;
10181016

10191017
if (svm_sev_enabled()) {
1020-
r = -ENOMEM;
10211018
sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
10221019
sizeof(void *),
10231020
GFP_KERNEL);
10241021
if (!sd->sev_vmcbs)
1025-
goto err_1;
1022+
goto free_save_area;
10261023
}
10271024

10281025
per_cpu(svm_data, cpu) = sd;
10291026

10301027
return 0;
10311028

1032-
err_1:
1029+
free_save_area:
1030+
__free_page(sd->save_area);
1031+
free_cpu_data:
10331032
kfree(sd);
1034-
return r;
1033+
return -ENOMEM;
10351034

10361035
}
10371036

0 commit comments

Comments
 (0)