Skip to content

Commit

Permalink
CA-314317: Protect PVS-cache get_or_recreate_vdi by mutex
Browse files Browse the repository at this point in the history
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 c003bb1

Signed-off-by: Rob Hoes <[email protected]>
  • Loading branch information
robhoes authored and lippirk committed Sep 30, 2019
1 parent f5ae6cc commit da5a744
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions ocaml/xapi/pvs_cache_vdi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da5a744

Please sign in to comment.