From 46b157008cc14f0b2de9f8efaf4228e54205f226 Mon Sep 17 00:00:00 2001 From: Jian Jun Chen Date: Tue, 29 Oct 2019 15:21:42 +0800 Subject: [PATCH] dm: file lock should be held till all mmap is done Lock should be held till all the mmap operations are done. This is to avoid the mmap failure when multiple guests are created concurrently. For example consider the following case in which vm1 and vm2 are created by acrnd concurrently: - vm1 is created with 4G+2M memory. - 4G+2M memory is reserved in hugetlb now and vm1 continues to allocate memory for the lowmem without lock held. - 2G memory is allocated by vm1 for its lowmem, and 2G+2M memory is available in hugetlb. - At this time vm2 is created with 1G+2M memory. It finds that enough memory is reserved (2G+2M), so it does not try to reserve more memory. - vm2 allocates some memory for its lowmem/highmem/ovmf. - vm1 tries to allocate memory for its highmem/ovmf, the allocation will fail. vm1 creation failed in this case. Tracked-On: #3947 Signed-off-by: Jian Jun Chen Reviewed-by: Yin Fengwei Acked-by: Yu Wang --- devicemodel/core/hugetlb.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/devicemodel/core/hugetlb.c b/devicemodel/core/hugetlb.c index d2e410e36..8c6d7027e 100644 --- a/devicemodel/core/hugetlb.c +++ b/devicemodel/core/hugetlb.c @@ -740,15 +740,13 @@ int hugetlb_setup_memory(struct vmctx *ctx) } } + lock_acrn_hugetlb(); + /* it will check each level memory need */ has_gap = hugetlb_check_memgap(); if (has_gap) { - lock_acrn_hugetlb(); - if (!hugetlb_reserve_pages()) { - unlock_acrn_hugetlb(); - goto err; - } - unlock_acrn_hugetlb(); + if (!hugetlb_reserve_pages()) + goto err_lock; } /* align up total size with huge page size for vma alignment */ @@ -775,7 +773,7 @@ int hugetlb_setup_memory(struct vmctx *ctx) MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (ptr == MAP_FAILED) { perror("anony mmap fail"); - goto err; + goto err_lock; } /* align up baseaddr according to hugepage level size */ @@ -791,23 +789,25 @@ int hugetlb_setup_memory(struct vmctx *ctx) /* mmap lowmem */ if (mmap_hugetlbfs(ctx, 0, get_lowmem_param, adj_lowmem_param) < 0) { perror("lowmem mmap failed"); - goto err; + goto err_lock; } /* mmap highmem */ if (mmap_hugetlbfs(ctx, ctx->highmem_gpa_base, get_highmem_param, adj_highmem_param) < 0) { perror("highmem mmap failed"); - goto err; + goto err_lock; } /* mmap biosmem */ if (mmap_hugetlbfs(ctx, 4 * GB - ctx->biosmem, get_biosmem_param, adj_biosmem_param) < 0) { perror("biosmem mmap failed"); - goto err; + goto err_lock; } + unlock_acrn_hugetlb(); + /* dump hugepage really setup */ printf("\nreally setup hugepage with:\n"); for (level = HUGETLB_LV1; level < hugetlb_lv_max; level++) { @@ -846,6 +846,8 @@ int hugetlb_setup_memory(struct vmctx *ctx) return 0; +err_lock: + unlock_acrn_hugetlb(); err: if (ptr) { munmap(ptr, total_size);