@@ -47,7 +47,7 @@ func NewVFIODevice(devInfo *config.DeviceInfo) *VFIODevice {
4747
4848// Attach is standard interface of api.Device, it's used to add device to some
4949// DeviceReceiver
50- func (device * VFIODevice ) Attach (devReceiver api.DeviceReceiver ) error {
50+ func (device * VFIODevice ) Attach (devReceiver api.DeviceReceiver ) ( retErr error ) {
5151 skip , err := device .bumpAttachCount (true )
5252 if err != nil {
5353 return err
@@ -56,6 +56,12 @@ func (device *VFIODevice) Attach(devReceiver api.DeviceReceiver) error {
5656 return nil
5757 }
5858
59+ defer func () {
60+ if retErr != nil {
61+ device .bumpAttachCount (false )
62+ }
63+ }()
64+
5965 vfioGroup := filepath .Base (device .DeviceInfo .HostPath )
6066 iommuDevicesPath := filepath .Join (config .SysIOMMUPath , vfioGroup , "devices" )
6167
@@ -90,13 +96,12 @@ func (device *VFIODevice) Attach(devReceiver api.DeviceReceiver) error {
9096 "device-group" : device .DeviceInfo .HostPath ,
9197 "device-type" : "vfio-passthrough" ,
9298 }).Info ("Device group attached" )
93- device .AttachCount = 1
9499 return nil
95100}
96101
97102// Detach is standard interface of api.Device, it's used to remove device from some
98103// DeviceReceiver
99- func (device * VFIODevice ) Detach (devReceiver api.DeviceReceiver ) error {
104+ func (device * VFIODevice ) Detach (devReceiver api.DeviceReceiver ) ( retErr error ) {
100105 skip , err := device .bumpAttachCount (false )
101106 if err != nil {
102107 return err
@@ -105,6 +110,12 @@ func (device *VFIODevice) Detach(devReceiver api.DeviceReceiver) error {
105110 return nil
106111 }
107112
113+ defer func () {
114+ if retErr != nil {
115+ device .bumpAttachCount (true )
116+ }
117+ }()
118+
108119 // hotplug a VFIO device is actually hotplugging a group of iommu devices
109120 if err := devReceiver .HotplugRemoveDevice (device , config .DeviceVFIO ); err != nil {
110121 deviceLogger ().WithError (err ).Error ("Failed to remove device" )
@@ -115,7 +126,6 @@ func (device *VFIODevice) Detach(devReceiver api.DeviceReceiver) error {
115126 "device-group" : device .DeviceInfo .HostPath ,
116127 "device-type" : "vfio-passthrough" ,
117128 }).Info ("Device group detached" )
118- device .AttachCount = 0
119129 return nil
120130}
121131
0 commit comments