Skip to content

Commit ea0f24e

Browse files
simonjbeaumontjohnelse
authored andcommitted
Check PGPU not in use before using for passthrough
This is done with the same mutex used for allocating vGPUs. Signed-off-by: Si Beaumont <[email protected]>
1 parent 2bc256f commit ea0f24e

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

ocaml/xapi/vgpuops.ml

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,35 @@ let vgpu_of_vgpu ~__context vm_r vgpu =
3838
let vgpus_of_vm ~__context vm_r =
3939
List.map (vgpu_of_vgpu ~__context vm_r) vm_r.API.vM_VGPUs
4040

41+
let m = Mutex.create ()
42+
4143
let create_passthrough_vgpu ~__context ~vm vgpu available_pgpus pcis =
4244
debug "Create vGPUs";
4345
let compatible_pgpus = Db.GPU_group.get_PGPUs ~__context ~self:vgpu.gpu_group_ref in
4446
let pgpus = List.intersect compatible_pgpus available_pgpus in
4547
let rec reserve_one = function
4648
| [] -> None
4749
| pgpu :: remaining ->
48-
let pci = Db.PGPU.get_PCI ~__context ~self:pgpu in
49-
if Pciops.reserve ~__context pci then
50-
Some (pgpu, pci)
51-
else
52-
reserve_one remaining
50+
try
51+
Xapi_pgpu_helpers.assert_capacity_exists_for_VGPU_type ~__context
52+
~self:pgpu ~vgpu_type:vgpu.type_ref;
53+
let pci = Db.PGPU.get_PCI ~__context ~self:pgpu in
54+
if Pciops.reserve ~__context pci then
55+
Some (pgpu, pci)
56+
else failwith "Could not reserve PCI" (* will retry remaining *)
57+
with _ -> reserve_one remaining
5358
in
54-
match reserve_one pgpus with
55-
| None ->
56-
raise (Api_errors.Server_error (Api_errors.vm_requires_gpu, [
57-
Ref.string_of vm;
58-
Ref.string_of vgpu.gpu_group_ref
59-
]))
60-
| Some (pgpu, pci) ->
61-
List.filter (fun g -> g <> pgpu) available_pgpus,
62-
pci :: pcis
59+
Threadext.Mutex.execute m (fun () ->
60+
match reserve_one pgpus with
61+
| None ->
62+
raise (Api_errors.Server_error (Api_errors.vm_requires_gpu, [
63+
Ref.string_of vm;
64+
Ref.string_of vgpu.gpu_group_ref
65+
]))
66+
| Some (pgpu, pci) ->
67+
List.filter (fun g -> g <> pgpu) available_pgpus,
68+
pci :: pcis
69+
)
6370

6471
let add_pcis_to_vm ~__context vm passthru_vgpus =
6572
let pcis =
@@ -86,7 +93,6 @@ let add_pcis_to_vm ~__context vm passthru_vgpus =
8693
let value = String.concat "," (List.map Pciops.to_string devs) in
8794
Db.VM.add_to_other_config ~__context ~self:vm ~key:Xapi_globs.vgpu_pci ~value
8895

89-
let m = Mutex.create ()
9096
let create_virtual_vgpu ~__context vm vgpu =
9197
let host = Helpers.get_localhost ~__context in
9298
let available_pgpus = Db.Host.get_PGPUs ~__context ~self:host in

0 commit comments

Comments
 (0)