Skip to content

Commit dd58f3c

Browse files
lrq-maxbonzini
authored andcommitted
KVM: fix error handling in svm_hardware_setup
rename svm_hardware_unsetup as svm_hardware_teardown, move it before svm_hardware_setup, and call it to free all memory if fail to setup in svm_hardware_setup, otherwise memory will be leaked remove __exit attribute for it since it is called in __init function Signed-off-by: Li RongQing <[email protected]> Reviewed-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent d80b64f commit dd58f3c

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

arch/x86/kvm/svm.c

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,24 @@ static __init void svm_adjust_mmio_mask(void)
13491349
kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
13501350
}
13511351

1352+
static void svm_hardware_teardown(void)
1353+
{
1354+
int cpu;
1355+
1356+
if (svm_sev_enabled()) {
1357+
bitmap_free(sev_asid_bitmap);
1358+
bitmap_free(sev_reclaim_asid_bitmap);
1359+
1360+
sev_flush_asids();
1361+
}
1362+
1363+
for_each_possible_cpu(cpu)
1364+
svm_cpu_uninit(cpu);
1365+
1366+
__free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
1367+
iopm_base = 0;
1368+
}
1369+
13521370
static __init int svm_hardware_setup(void)
13531371
{
13541372
int cpu;
@@ -1462,29 +1480,10 @@ static __init int svm_hardware_setup(void)
14621480
return 0;
14631481

14641482
err:
1465-
__free_pages(iopm_pages, IOPM_ALLOC_ORDER);
1466-
iopm_base = 0;
1483+
svm_hardware_teardown();
14671484
return r;
14681485
}
14691486

1470-
static __exit void svm_hardware_unsetup(void)
1471-
{
1472-
int cpu;
1473-
1474-
if (svm_sev_enabled()) {
1475-
bitmap_free(sev_asid_bitmap);
1476-
bitmap_free(sev_reclaim_asid_bitmap);
1477-
1478-
sev_flush_asids();
1479-
}
1480-
1481-
for_each_possible_cpu(cpu)
1482-
svm_cpu_uninit(cpu);
1483-
1484-
__free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
1485-
iopm_base = 0;
1486-
}
1487-
14881487
static void init_seg(struct vmcb_seg *seg)
14891488
{
14901489
seg->selector = 0;
@@ -7385,7 +7384,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
73857384
.cpu_has_kvm_support = has_svm,
73867385
.disabled_by_bios = is_disabled,
73877386
.hardware_setup = svm_hardware_setup,
7388-
.hardware_unsetup = svm_hardware_unsetup,
7387+
.hardware_unsetup = svm_hardware_teardown,
73897388
.check_processor_compatibility = svm_check_processor_compat,
73907389
.hardware_enable = svm_hardware_enable,
73917390
.hardware_disable = svm_hardware_disable,

0 commit comments

Comments
 (0)