From da5a74428790b90174786d03a31ca0918ec56c33 Mon Sep 17 00:00:00 2001 From: Rob Hoes Date: Mon, 8 Apr 2019 14:08:41 +0100 Subject: [PATCH] CA-314317: Protect PVS-cache get_or_recreate_vdi by mutex The function `Pvs_cache_vdi.get_or_recreate_vdi` may be called for multiple concurrently starting VMs. Without the mutex, this can lead to multiple VDIs being created, while the should only be one for a given `PVS_cache_storage` object. cherry-picked from commit c003bb17aca6f2ac64057ef6835ff304e683b6d6 Signed-off-by: Rob Hoes --- ocaml/xapi/pvs_cache_vdi.ml | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ocaml/xapi/pvs_cache_vdi.ml b/ocaml/xapi/pvs_cache_vdi.ml index f19ea59c12b..563bd7457d5 100644 --- a/ocaml/xapi/pvs_cache_vdi.ml +++ b/ocaml/xapi/pvs_cache_vdi.ml @@ -64,15 +64,19 @@ let get_vdi ~__context ~self = end end +let m = Mutex.create () + let get_or_recreate_vdi ~__context ~self = - match get_vdi ~__context ~self with - | None -> - let sR = Db.PVS_cache_storage.get_SR ~__context ~self in - let size = Db.PVS_cache_storage.get_size ~__context ~self in - let vdi = create_vdi ~__context ~sR ~size in - Db.PVS_cache_storage.set_VDI ~__context ~self ~value:vdi; - vdi - | Some vdi -> vdi + Stdext.Threadext.Mutex.execute m (fun () -> + match get_vdi ~__context ~self with + | None -> + let sR = Db.PVS_cache_storage.get_SR ~__context ~self in + let size = Db.PVS_cache_storage.get_size ~__context ~self in + let vdi = create_vdi ~__context ~sR ~size in + Db.PVS_cache_storage.set_VDI ~__context ~self ~value:vdi; + vdi + | Some vdi -> vdi + ) let destroy_vdi ~__context ~self = match get_vdi ~__context ~self with