@@ -52,6 +52,57 @@ impl PciBindGuard {
5252 self . disarmed = true ;
5353 }
5454
55+ /// Adopt ownership of a `PCIe` device that is already bound to `vfio-pci`.
56+ ///
57+ /// This is the restart-reconciliation counterpart to
58+ /// [`prepare_pci_for_passthrough`]. It validates that the device exists,
59+ /// that all IOMMU peers are already on `vfio-pci`, and that the primary BDF
60+ /// is currently bound to `vfio-pci`, then returns a guard that will restore
61+ /// the device on drop. It does not write `driver_override`, `new_id`, or
62+ /// `drivers_probe`.
63+ pub fn adopt ( sysfs : & SysfsRoot , bdf : & str ) -> Result < Self , VfioError > {
64+ validate_pci_for_passthrough ( sysfs, bdf) ?;
65+ ensure_bound_to_vfio ( sysfs, bdf) ?;
66+
67+ let vfio_id = crate :: bind:: vfio_id_string ( sysfs, bdf) ;
68+
69+ Ok ( Self :: new_armed (
70+ bdf. to_string ( ) ,
71+ Vec :: new ( ) ,
72+ sysfs. clone ( ) ,
73+ vfio_id,
74+ ) )
75+ }
76+
77+ /// Adopt ownership of a complete IOMMU group already bound to `vfio-pci`.
78+ ///
79+ /// This is the restart-reconciliation counterpart to
80+ /// [`prepare_pci_group_for_passthrough`]. It validates that `bdfs` is the
81+ /// complete IOMMU group, that every declared device is already bound to
82+ /// `vfio-pci`, and then returns a guard that restores every declared device
83+ /// on drop. It does not write `driver_override`, `new_id`, or
84+ /// `drivers_probe`.
85+ pub fn adopt_group ( sysfs : & SysfsRoot , bdfs : & [ & str ] ) -> Result < Self , VfioError > {
86+ validate_pci_group_for_passthrough ( sysfs, bdfs) ?;
87+
88+ for bdf in bdfs {
89+ ensure_bound_to_vfio ( sysfs, bdf) ?;
90+ }
91+
92+ let ( primary, companions) = bdfs
93+ . split_first ( )
94+ . expect ( "validate_pci_group_for_passthrough rejects empty slices" ) ;
95+ let vfio_id = crate :: bind:: vfio_id_string ( sysfs, primary) ;
96+ let companion_bdfs: Vec < String > = companions. iter ( ) . map ( |s| ( * s) . to_string ( ) ) . collect ( ) ;
97+
98+ Ok ( Self :: new_armed (
99+ ( * primary) . to_string ( ) ,
100+ companion_bdfs,
101+ sysfs. clone ( ) ,
102+ vfio_id,
103+ ) )
104+ }
105+
55106 pub ( crate ) fn new_armed (
56107 bdf : String ,
57108 companion_bdfs : Vec < String > ,
@@ -91,6 +142,18 @@ impl PciBindGuard {
91142 }
92143}
93144
145+ fn ensure_bound_to_vfio ( sysfs : & SysfsRoot , bdf : & str ) -> Result < ( ) , VfioError > {
146+ let driver = crate :: bind:: current_driver_name ( sysfs, bdf) ;
147+ if driver. as_deref ( ) == Some ( "vfio-pci" ) {
148+ return Ok ( ( ) ) ;
149+ }
150+
151+ Err ( VfioError :: NotBoundToVfio {
152+ bdf : bdf. to_string ( ) ,
153+ driver : driver. unwrap_or_else ( || "<none>" . to_string ( ) ) ,
154+ } )
155+ }
156+
94157impl Drop for PciBindGuard {
95158 fn drop ( & mut self ) {
96159 if self . disarmed {
@@ -157,10 +220,7 @@ impl PciBindState {
157220/// Class-based filtering (GPU vs NIC vs other) is intentionally left to
158221/// the caller because portable device-class definitions are a driver-layer
159222/// concern (see RFC 0004's `DeviceResourceRequirement.class_name`).
160- pub fn probe_host_vfio_candidates (
161- sysfs : & SysfsRoot ,
162- vendor_filter : Option < & str > ,
163- ) -> Vec < PciInfo > {
223+ pub fn probe_host_vfio_candidates ( sysfs : & SysfsRoot , vendor_filter : Option < & str > ) -> Vec < PciInfo > {
164224 let devices_dir = sysfs. pci_devices_dir ( ) ;
165225 let entries = match fs:: read_dir ( & devices_dir) {
166226 Ok ( e) => e,
@@ -407,9 +467,7 @@ pub fn prepare_pci_group_for_passthrough(
407467 }
408468 Err ( err) => {
409469 for already in newly_bound. iter ( ) . rev ( ) {
410- if let Err ( restore_err) =
411- crate :: bind:: restore_to_host_driver ( sysfs, already)
412- {
470+ if let Err ( restore_err) = crate :: bind:: restore_to_host_driver ( sysfs, already) {
413471 tracing:: error!(
414472 bdf = %already,
415473 error = %restore_err,
@@ -603,6 +661,148 @@ mod tests {
603661 assert_eq ! ( override_val, "vfio-pci" ) ;
604662 }
605663
664+ #[ test]
665+ fn test_adopt_single_already_bound_device ( ) {
666+ let _refcount_guard = test_refcounts:: guard ( ) ;
667+ test_refcounts:: clear ( "15b3 101e" ) ;
668+ let ( tmp, sysfs) = setup_mock_sysfs ( ) ;
669+ create_pci_device (
670+ & sysfs,
671+ tmp. path ( ) ,
672+ "0000:81:00.2" ,
673+ "0x15b3" ,
674+ "0x101e" ,
675+ "0x020000" ,
676+ 7 ,
677+ ) ;
678+ create_probe_file ( & sysfs) ;
679+ create_remove_id_file ( & sysfs) ;
680+ set_mock_driver ( & sysfs, "0000:81:00.2" , "vfio-pci" ) ;
681+ fs:: write (
682+ sysfs. pci_device ( "0000:81:00.2" ) . join ( "driver_override" ) ,
683+ "vfio-pci" ,
684+ )
685+ . unwrap ( ) ;
686+
687+ {
688+ let guard = PciBindGuard :: adopt ( & sysfs, "0000:81:00.2" ) . unwrap ( ) ;
689+ assert_eq ! ( guard. bdf( ) , "0000:81:00.2" ) ;
690+ assert ! ( guard. companion_bdfs( ) . is_empty( ) ) ;
691+ }
692+
693+ let override_val =
694+ fs:: read_to_string ( sysfs. pci_device ( "0000:81:00.2" ) . join ( "driver_override" ) ) . unwrap ( ) ;
695+ assert_eq ! ( override_val. trim( ) , "" ) ;
696+ }
697+
698+ #[ test]
699+ fn test_adopt_rejects_device_not_bound_to_vfio ( ) {
700+ let ( tmp, sysfs) = setup_mock_sysfs ( ) ;
701+ create_pci_device (
702+ & sysfs,
703+ tmp. path ( ) ,
704+ "0000:81:00.2" ,
705+ "0x15b3" ,
706+ "0x101e" ,
707+ "0x020000" ,
708+ 7 ,
709+ ) ;
710+ set_mock_driver ( & sysfs, "0000:81:00.2" , "mlx5_core" ) ;
711+
712+ let err = PciBindGuard :: adopt ( & sysfs, "0000:81:00.2" ) . unwrap_err ( ) ;
713+ match err {
714+ VfioError :: NotBoundToVfio { bdf, driver } => {
715+ assert_eq ! ( bdf, "0000:81:00.2" ) ;
716+ assert_eq ! ( driver, "mlx5_core" ) ;
717+ }
718+ other => panic ! ( "expected NotBoundToVfio, got {other:?}" ) ,
719+ }
720+ }
721+
722+ #[ test]
723+ fn test_adopt_group_already_bound_devices ( ) {
724+ let _refcount_guard = test_refcounts:: guard ( ) ;
725+ test_refcounts:: clear ( "15b3 101e" ) ;
726+ test_refcounts:: clear ( "15b3 101f" ) ;
727+ let ( tmp, sysfs) = setup_mock_sysfs ( ) ;
728+ create_pci_device (
729+ & sysfs,
730+ tmp. path ( ) ,
731+ "0000:81:00.2" ,
732+ "0x15b3" ,
733+ "0x101e" ,
734+ "0x020000" ,
735+ 7 ,
736+ ) ;
737+ create_pci_device (
738+ & sysfs,
739+ tmp. path ( ) ,
740+ "0000:81:00.3" ,
741+ "0x15b3" ,
742+ "0x101f" ,
743+ "0x020000" ,
744+ 7 ,
745+ ) ;
746+ create_probe_file ( & sysfs) ;
747+ create_remove_id_file ( & sysfs) ;
748+ set_mock_driver ( & sysfs, "0000:81:00.2" , "vfio-pci" ) ;
749+ set_mock_driver ( & sysfs, "0000:81:00.3" , "vfio-pci" ) ;
750+ for bdf in [ "0000:81:00.2" , "0000:81:00.3" ] {
751+ fs:: write ( sysfs. pci_device ( bdf) . join ( "driver_override" ) , "vfio-pci" ) . unwrap ( ) ;
752+ }
753+
754+ {
755+ let guard =
756+ PciBindGuard :: adopt_group ( & sysfs, & [ "0000:81:00.2" , "0000:81:00.3" ] ) . unwrap ( ) ;
757+ assert_eq ! ( guard. bdf( ) , "0000:81:00.2" ) ;
758+ assert_eq ! ( guard. companion_bdfs( ) , & [ "0000:81:00.3" . to_string( ) ] ) ;
759+ }
760+
761+ for bdf in [ "0000:81:00.2" , "0000:81:00.3" ] {
762+ let override_val =
763+ fs:: read_to_string ( sysfs. pci_device ( bdf) . join ( "driver_override" ) ) . unwrap ( ) ;
764+ assert_eq ! (
765+ override_val. trim( ) ,
766+ "" ,
767+ "{bdf}: driver_override should be cleared after adopted group drops"
768+ ) ;
769+ }
770+ }
771+
772+ #[ test]
773+ fn test_adopt_group_rejects_member_not_bound_to_vfio ( ) {
774+ let ( tmp, sysfs) = setup_mock_sysfs ( ) ;
775+ create_pci_device (
776+ & sysfs,
777+ tmp. path ( ) ,
778+ "0000:81:00.2" ,
779+ "0x15b3" ,
780+ "0x101e" ,
781+ "0x020000" ,
782+ 7 ,
783+ ) ;
784+ create_pci_device (
785+ & sysfs,
786+ tmp. path ( ) ,
787+ "0000:81:00.3" ,
788+ "0x15b3" ,
789+ "0x101f" ,
790+ "0x020000" ,
791+ 7 ,
792+ ) ;
793+ set_mock_driver ( & sysfs, "0000:81:00.2" , "vfio-pci" ) ;
794+ set_mock_driver ( & sysfs, "0000:81:00.3" , "mlx5_core" ) ;
795+
796+ let err = PciBindGuard :: adopt_group ( & sysfs, & [ "0000:81:00.2" , "0000:81:00.3" ] ) . unwrap_err ( ) ;
797+ match err {
798+ VfioError :: NotBoundToVfio { bdf, driver } => {
799+ assert_eq ! ( bdf, "0000:81:00.3" ) ;
800+ assert_eq ! ( driver, "mlx5_core" ) ;
801+ }
802+ other => panic ! ( "expected NotBoundToVfio, got {other:?}" ) ,
803+ }
804+ }
805+
606806 #[ test]
607807 fn test_validate_pci_for_passthrough_accepts_valid_solo_device ( ) {
608808 let ( tmp, sysfs) = setup_mock_sysfs ( ) ;
@@ -1114,9 +1314,8 @@ mod tests {
11141314 9 ,
11151315 ) ;
11161316
1117- let err =
1118- prepare_pci_group_for_passthrough ( & sysfs, & [ "0000:81:00.2" , "0000:82:00.0" ] )
1119- . unwrap_err ( ) ;
1317+ let err = prepare_pci_group_for_passthrough ( & sysfs, & [ "0000:81:00.2" , "0000:82:00.0" ] )
1318+ . unwrap_err ( ) ;
11201319 match err {
11211320 VfioError :: GroupMismatch {
11221321 bdf,
@@ -1144,9 +1343,8 @@ mod tests {
11441343 7 ,
11451344 ) ;
11461345
1147- let err =
1148- prepare_pci_group_for_passthrough ( & sysfs, & [ "0000:81:00.2" , "0000:81:00.2" ] )
1149- . unwrap_err ( ) ;
1346+ let err = prepare_pci_group_for_passthrough ( & sysfs, & [ "0000:81:00.2" , "0000:81:00.2" ] )
1347+ . unwrap_err ( ) ;
11501348 assert ! ( matches!( err, VfioError :: InvalidBdf { .. } ) ) ;
11511349 }
11521350
@@ -1199,9 +1397,8 @@ mod tests {
11991397 )
12001398 . unwrap ( ) ;
12011399
1202- let err =
1203- prepare_pci_group_for_passthrough ( & sysfs, & [ "0000:81:00.2" , "0000:81:00.3" ] )
1204- . unwrap_err ( ) ;
1400+ let err = prepare_pci_group_for_passthrough ( & sysfs, & [ "0000:81:00.2" , "0000:81:00.3" ] )
1401+ . unwrap_err ( ) ;
12051402 assert ! ( matches!( err, VfioError :: BindFailed { .. } ) ) ;
12061403
12071404 // First device was already on vfio-pci (not newly bound), so it
@@ -1285,8 +1482,8 @@ mod tests {
12851482
12861483 // The first BDF is invalid; the second is valid and must still be
12871484 // restored even though the first call returns an error.
1288- let err = release_pci_group_from_passthrough ( & sysfs , & [ "bad-bdf" , "0000:81:00.2" ] )
1289- . unwrap_err ( ) ;
1485+ let err =
1486+ release_pci_group_from_passthrough ( & sysfs , & [ "bad-bdf" , "0000:81:00.2" ] ) . unwrap_err ( ) ;
12901487 assert ! ( matches!( err, VfioError :: InvalidBdf { .. } ) ) ;
12911488
12921489 let override_val =
0 commit comments