From c33e1d5cc6b87e396f7403edd6b37de4cf45f79b Mon Sep 17 00:00:00 2001 From: Liu Shuo Date: Wed, 28 Feb 2018 14:17:36 +0800 Subject: [PATCH] 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 Reviewed-by: Yin Fengwei Acked-by: Anthony Xu --- core/main.c | 73 +++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 42 deletions(-) diff --git a/core/main.c b/core/main.c index 1b7149c7d..277c07dab 100644 --- a/core/main.c +++ b/core/main.c @@ -92,8 +92,6 @@ static const int BSP; static cpuset_t cpumask; -static void do_close_pre(struct vmctx *ctx); -static void do_close_post(struct vmctx *ctx); static void vm_loop(struct vmctx *ctx); static int quit_vm_loop; @@ -531,24 +529,6 @@ do_open(const char *vmname) return ctx; } -static void -do_close_pre(struct vmctx *ctx) -{ - vm_destroy(ctx); - vm_close(ctx); -} - -static void -do_close_post(struct vmctx *ctx) -{ - pci_irq_deinit(ctx); - deinit_pci(ctx); - atkbdc_deinit(ctx); - vrtc_deinit(ctx); - vm_destroy(ctx); - vm_close(ctx); -} - static void sig_handler_term(int signo) { @@ -768,30 +748,26 @@ main(int argc, char *argv[]) /* set IOReq buffer page */ error = vm_set_shared_io_page(ctx, (unsigned long)vhm_req_buf); if (error) - do_close_pre(ctx); - assert(error == 0); + goto fail; if (guest_ncpus < 1) { fprintf(stderr, "Invalid guest vCPUs (%d)\n", guest_ncpus); - do_close_pre(ctx); - exit(1); + goto fail; } max_vcpus = num_vcpus_allowed(ctx); if (guest_ncpus > max_vcpus) { fprintf(stderr, "%d vCPUs requested but %d available\n", guest_ncpus, max_vcpus); - do_close_pre(ctx); - exit(1); + goto fail; } vm_set_memflags(ctx, memflags); err = vm_setup_memory(ctx, memsize, VM_MMAP_ALL); if (err) { fprintf(stderr, "Unable to setup memory (%d)\n", errno); - do_close_pre(ctx); - exit(1); + goto fail; } init_mem(); @@ -809,8 +785,7 @@ main(int argc, char *argv[]) * initialization */ if (init_pci(ctx) != 0) { - do_close_pre(ctx); - exit(1); + goto pci_fail; } if (gdb_port != 0) @@ -825,27 +800,23 @@ main(int argc, char *argv[]) if (mptgen) { error = mptable_build(ctx, guest_ncpus); if (error) { - do_close_post(ctx); - exit(1); + goto vm_fail; } } error = smbios_build(ctx); if (error) - do_close_post(ctx); - assert(error == 0); + goto vm_fail; if (acpi) { error = acpi_build(ctx, guest_ncpus); if (error) - do_close_post(ctx); - assert(error == 0); + goto vm_fail; } error = acrn_sw_load(ctx); if (error) - do_close_post(ctx); - assert(error == 0); + goto vm_fail; /* * Change the proc title to include the VM name. @@ -865,16 +836,34 @@ main(int argc, char *argv[]) */ mevent_dispatch(); - monitor_close(); vm_pause(ctx); fbsdrun_deletecpu(ctx, BSP); - vm_unsetup_memory(ctx); - do_close_post(ctx); - _ctx = 0; if (vm_get_suspend_mode() != VM_SUSPEND_RESET) break; + + pci_irq_deinit(ctx); + deinit_pci(ctx); + monitor_close(); + vrtc_deinit(ctx); + atkbdc_deinit(ctx); + vm_unsetup_memory(ctx); + vm_destroy(ctx); + vm_close(ctx); + _ctx = 0; + vm_set_suspend_mode(VM_SUSPEND_NONE); } +vm_fail: + pci_irq_deinit(ctx); + deinit_pci(ctx); +pci_fail: + monitor_close(); + vrtc_deinit(ctx); + atkbdc_deinit(ctx); + vm_unsetup_memory(ctx); +fail: + vm_destroy(ctx); + vm_close(ctx); exit(0); }