opae.io: handle sysfs exceptions
Writing to /sys/bus/pci/drivers/vfio-pci/bind will fail if the
driver enumeration doesn't claim the device.
In recent kernels, eg 5.15, writing the same VID/DID multiple times
to /sys/bus/pci/drivers/vfio-pci/new_id will result in EEXIST. Catch
EEXIST and continue to process the device, else exit on other OSError.
-# Copyright(c) 2020-2021, Intel Corporation
+# Copyright(c) 2020-2022, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# POSSIBILITY OF SUCH DAMAGE.
import contextlib
+import errno
import grp
import json
import os
@@ -104,8 +105,13 @@ def unbind_driver(driver, pci_addr):
def bind_driver(driver, pci_addr):
bind = '/sys/bus/pci/drivers/{}/bind'.format(driver)
if os.path.exists(bind):
+ try:
with open(bind, 'w') as outf:
outf.write(pci_addr)
+ except OSError:
+ return False
+ return True
+ return False
def get_dev_dict(file_name):
if os.path.isfile(file_name):
@@ -140,8 +146,12 @@ def vfio_init(pci_addr, new_owner=''):
print('Binding {} to vfio-pci'.format(msg))
new_id = '/sys/bus/pci/drivers/vfio-pci/new_id'
+ try:
with open(new_id, 'w') as outf:
outf.write('{} {}'.format(vid_did[0], vid_did[1]))
+ except OSError as exc:
+ if exc.errno != errno.EEXIST:
+ return
time.sleep(0.25)
