Skip to content

Commit ee43f23

Browse files
Liu Shuojren1
authored andcommitted
dm: release host memory after devices de-init
Devices' de-init process might access some mapped memory space, such as virtio virtqueues. Access after unmap will cause a fault. Release the memory map after de-init processes can avoid it. Reading more code, there are many error handling lost to unmap the memory. Refined the code to do it. Signed-off-by: Liu Shuo <shuo.a.liu@intel.com> Reviewed-by: Yin Fengwei <fengwei.yin@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent cee499f commit ee43f23

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

devicemodel/core/main.c

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ static const int BSP;
9292

9393
static cpuset_t cpumask;
9494

95-
static void do_close_pre(struct vmctx *ctx);
96-
static void do_close_post(struct vmctx *ctx);
9795
static void vm_loop(struct vmctx *ctx);
9896

9997
static int quit_vm_loop;
@@ -531,24 +529,6 @@ do_open(const char *vmname)
531529
return ctx;
532530
}
533531

534-
static void
535-
do_close_pre(struct vmctx *ctx)
536-
{
537-
vm_destroy(ctx);
538-
vm_close(ctx);
539-
}
540-
541-
static void
542-
do_close_post(struct vmctx *ctx)
543-
{
544-
pci_irq_deinit(ctx);
545-
deinit_pci(ctx);
546-
atkbdc_deinit(ctx);
547-
vrtc_deinit(ctx);
548-
vm_destroy(ctx);
549-
vm_close(ctx);
550-
}
551-
552532
static void
553533
sig_handler_term(int signo)
554534
{
@@ -768,30 +748,26 @@ main(int argc, char *argv[])
768748
/* set IOReq buffer page */
769749
error = vm_set_shared_io_page(ctx, (unsigned long)vhm_req_buf);
770750
if (error)
771-
do_close_pre(ctx);
772-
assert(error == 0);
751+
goto fail;
773752

774753
if (guest_ncpus < 1) {
775754
fprintf(stderr, "Invalid guest vCPUs (%d)\n",
776755
guest_ncpus);
777-
do_close_pre(ctx);
778-
exit(1);
756+
goto fail;
779757
}
780758

781759
max_vcpus = num_vcpus_allowed(ctx);
782760
if (guest_ncpus > max_vcpus) {
783761
fprintf(stderr, "%d vCPUs requested but %d available\n",
784762
guest_ncpus, max_vcpus);
785-
do_close_pre(ctx);
786-
exit(1);
763+
goto fail;
787764
}
788765

789766
vm_set_memflags(ctx, memflags);
790767
err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL);
791768
if (err) {
792769
fprintf(stderr, "Unable to setup memory (%d)\n", errno);
793-
do_close_pre(ctx);
794-
exit(1);
770+
goto fail;
795771
}
796772

797773
init_mem();
@@ -809,8 +785,7 @@ main(int argc, char *argv[])
809785
* initialization
810786
*/
811787
if (init_pci(ctx) != 0) {
812-
do_close_pre(ctx);
813-
exit(1);
788+
goto pci_fail;
814789
}
815790

816791
if (gdb_port != 0)
@@ -825,27 +800,23 @@ main(int argc, char *argv[])
825800
if (mptgen) {
826801
error = mptable_build(ctx, guest_ncpus);
827802
if (error) {
828-
do_close_post(ctx);
829-
exit(1);
803+
goto vm_fail;
830804
}
831805
}
832806

833807
error = smbios_build(ctx);
834808
if (error)
835-
do_close_post(ctx);
836-
assert(error == 0);
809+
goto vm_fail;
837810

838811
if (acpi) {
839812
error = acpi_build(ctx, guest_ncpus);
840813
if (error)
841-
do_close_post(ctx);
842-
assert(error == 0);
814+
goto vm_fail;
843815
}
844816

845817
error = acrn_sw_load(ctx);
846818
if (error)
847-
do_close_post(ctx);
848-
assert(error == 0);
819+
goto vm_fail;
849820

850821
/*
851822
* Change the proc title to include the VM name.
@@ -865,16 +836,34 @@ main(int argc, char *argv[])
865836
*/
866837
mevent_dispatch();
867838

868-
monitor_close();
869839
vm_pause(ctx);
870840
fbsdrun_deletecpu(ctx, BSP);
871-
vm_unsetup_memory(ctx);
872-
do_close_post(ctx);
873-
_ctx = 0;
874841

875842
if (vm_get_suspend_mode() != VM_SUSPEND_RESET)
876843
break;
844+
845+
pci_irq_deinit(ctx);
846+
deinit_pci(ctx);
847+
monitor_close();
848+
vrtc_deinit(ctx);
849+
atkbdc_deinit(ctx);
850+
vm_unsetup_memory(ctx);
851+
vm_destroy(ctx);
852+
vm_close(ctx);
853+
_ctx = 0;
854+
877855
vm_set_suspend_mode(VM_SUSPEND_NONE);
878856
}
857+
vm_fail:
858+
pci_irq_deinit(ctx);
859+
deinit_pci(ctx);
860+
pci_fail:
861+
monitor_close();
862+
vrtc_deinit(ctx);
863+
atkbdc_deinit(ctx);
864+
vm_unsetup_memory(ctx);
865+
fail:
866+
vm_destroy(ctx);
867+
vm_close(ctx);
879868
exit(0);
880869
}

0 commit comments

Comments
 (0)