Skip to contentSkip to conversation

usertools: add check for IOMMU support in dpdk-devbind

PATCH Superseded Fidaullah Noonari 5 participants 13 messages Mar 15, 2022 – Oct 17, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usertools: add check for IOMMU support in dpdk-devbind

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---
 usertools/dpdk-devbind.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
usertools/dpdk-devbind.py +39 -0
index ace4627218..91fa4c7620 100755
@@ -96,6 +96,7 @@
 b_flag = None
 status_flag = False
 force_flag = False
+noiommu_flag = False
 args = []
 
 
@@ -466,9 +467,30 @@ def unbind_all(dev_list, force=False):
         unbind_one(d, force)
 
 
+def check_iommu():
+    """Check if IOMMU is enabled on system"""
+    if len(os.listdir("/sys/class/iommu")) != 0:
+        return True
+    return False
+
+
+def enable_noiommu_mode():
+    """Enables the noiommu mode for vfio drivers"""
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+    try:
+        f = open(filename, "w")
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))
+    f.write("1")
+    f.close()
+    print("Warning: enabling unsafe no IOMMU mode for vfio drivers")
+
+
 def bind_all(dev_list, driver, force=False):
     """Bind method, takes a list of device locations"""
     global devices
+    global noiommu_flag
 
     # a common user error is to forget to specify the driver the devices need to
     # be bound to. check if the driver is a valid device, and if it is, show
@@ -492,6 +514,14 @@ def bind_all(dev_list, driver, force=False):
     except ValueError as ex:
         sys.exit(ex)
 
+    # check for IOMMU support
+    if not check_iommu():
+        if noiommu_flag:
+            enable_noiommu_mode()
+        elif driver == "vfio-pci":
+            print("Warning: IOMMU support is disabled")
+            print("Info: use --noiommu-mode for binding in noiommu mode")
+
     for d in dev_list:
         bind_one(d, driver, force)
 
@@ -634,6 +664,7 @@ def parse_args():
     global status_flag
     global status_dev
     global force_flag
+    global noiommu_flag
     global args
 
     parser = argparse.ArgumentParser(
@@ -687,6 +718,12 @@ def parse_args():
 Override restriction on binding devices in use by Linux"
 WARNING: This can lead to loss of network connection and should be used with caution.
 """)
+    parser.add_argument(
+        '--noiommu-mode',
+        action='store_true',
+        help="""
+if IOMMU is not available, Enables no IOMMU mode for vfio drivers.
+        """)
     parser.add_argument(
         'devices',
         metavar='DEVICE',
@@ -706,6 +743,8 @@ def parse_args():
         status_dev = "all"
     if opt.force:
         force_flag = True
+    if opt.noiommu_mode:
+        noiommu_flag = True
     if opt.bind:
         b_flag = opt.bind
     elif opt.unbind:
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · Fidaullah Noonari

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---

Hi,

patch generally looks good, but some more feedback inline below. The main thing missing is that we should probably also check the current value of the vfio noiommu flag and take that into account when printing warnings or errors.

/Bruce

Quoted 16 lines
 usertools/dpdk-devbind.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
usertools/dpdk-devbind.py +1 -0
index ace4627218..91fa4c7620 100755
@@ -96,6 +96,7 @@
 b_flag = None
 status_flag = False
 force_flag = False
+noiommu_flag = False
 args = []
 
 

usertools/dpdk-devbind.py def unbind_all(dev_list, force=False): · line 474

Quoted 8 lines
usertools/dpdk-devbind.py +5 -0
@@ -466,9 +467,30 @@ def unbind_all(dev_list, force=False):
         unbind_one(d, force)
 
 
+def check_iommu():
+    """Check if IOMMU is enabled on system"""
+    if len(os.listdir("/sys/class/iommu")) != 0:
+        return True
+    return False

I'd suggest renaming the function for clarity, since "check_iommu" is a little unclear as to what the return value is i.e. false could mean you can't check, rather than it doesn't have one. "has_iommu()" might be clearer.

Can probably be shortened to one-line body:

    def has_iommu():
        """..."""
        return len(os.listdir("/sys/class/iommu")) > 0
Quoted 24 lines
usertools/dpdk-devbind.py +16 -0
+
+
+def enable_noiommu_mode():
+    """Enables the noiommu mode for vfio drivers"""
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+    try:
+        f = open(filename, "w")
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))
+    f.write("1")
+    f.close()
+    print("Warning: enabling unsafe no IOMMU mode for vfio drivers")
+
+
 def bind_all(dev_list, driver, force=False):
     """Bind method, takes a list of device locations"""
     global devices
+    global noiommu_flag
 
     # a common user error is to forget to specify the driver the devices need to
     # be bound to. check if the driver is a valid device, and if it is, show
@@ -492,6 +514,14 @@ def bind_all(dev_list, driver, force=False):
     except ValueError as ex:

usertools/dpdk-devbind.py def bind_all(dev_list, driver, force=False): · line 524

Quoted 10 lines
usertools/dpdk-devbind.py +8 -0
         sys.exit(ex)
 
+    # check for IOMMU support
+    if not check_iommu():
+        if noiommu_flag:
+            enable_noiommu_mode()
+        elif driver == "vfio-pci":
+            print("Warning: IOMMU support is disabled")
+            print("Info: use --noiommu-mode for binding in noiommu mode")
+

Rather than just warning here, I think this needs a little extra check to see if no-iommu mode is already enabled. If so, just continue, if it is not, then the warning should be an error and no binding should be attempted.

Quoted 36 lines
usertools/dpdk-devbind.py +9 -0
     for d in dev_list:
         bind_one(d, driver, force)
 
@@ -634,6 +664,7 @@ def parse_args():
     global status_flag
     global status_dev
     global force_flag
+    global noiommu_flag
     global args
 
     parser = argparse.ArgumentParser(
@@ -687,6 +718,12 @@ def parse_args():
 Override restriction on binding devices in use by Linux"
 WARNING: This can lead to loss of network connection and should be used with caution.
 """)
+    parser.add_argument(
+        '--noiommu-mode',
+        action='store_true',
+        help="""
+if IOMMU is not available, Enables no IOMMU mode for vfio drivers.
+        """)
     parser.add_argument(
         'devices',
         metavar='DEVICE',
@@ -706,6 +743,8 @@ def parse_args():
         status_dev = "all"
     if opt.force:
         force_flag = True
+    if opt.noiommu_mode:
+        noiommu_flag = True
     if opt.bind:
         b_flag = opt.bind
     elif opt.unbind:
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 1/1 usertools: add check for IOMMU support in dpdk-devbind

In reply to Fidaullah Noonari

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---
 usertools/dpdk-devbind.py | 47 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
usertools/dpdk-devbind.py +47 -0
index ace4627218..e2181efac8 100755
@@ -96,6 +96,7 @@
 b_flag = None
 status_flag = False
 force_flag = False
+noiommu_flag = False
 args = []
 
 
@@ -466,6 +467,39 @@ def unbind_all(dev_list, force=False):
         unbind_one(d, force)
 
 
+def has_iommu():
+    """Check if IOMMU is enabled on system"""
+    return len(os.listdir("/sys/class/iommu")) > 0
+
+
+def check_noiommu_mode():
+    """checks and enables the noiommu mode for vfio drivers"""
+    global noiommu_flag
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+    try:
+        with open(filename,"r") as f:
+            if f.read(1) == "1":
+                return
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))
+
+    if not noiommu_flag:
+        print("Error: failed to bind vfio-pci - IOMMU support is disabled")
+        print("Info: use --noiommu-mode for binding in noiommu mode")
+        sys.exit()
+
+    try:
+        with open(filename, "w") as f:
+            f.write("1")
+            f.close()
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))
+    print("Warning: enabling unsafe no IOMMU mode for vfio drivers")
+
+
 def bind_all(dev_list, driver, force=False):
     """Bind method, takes a list of device locations"""
     global devices
@@ -492,6 +526,10 @@ def bind_all(dev_list, driver, force=False):
     except ValueError as ex:
         sys.exit(ex)
 
+    # check for IOMMU support
+    if driver == "vfio-pci" and not has_iommu():
+        check_noiommu_mode()
+
     for d in dev_list:
         bind_one(d, driver, force)
 
@@ -634,6 +672,7 @@ def parse_args():
     global status_flag
     global status_dev
     global force_flag
+    global noiommu_flag
     global args
 
     parser = argparse.ArgumentParser(
@@ -687,6 +726,12 @@ def parse_args():
 Override restriction on binding devices in use by Linux"
 WARNING: This can lead to loss of network connection and should be used with caution.
 """)
+    parser.add_argument(
+        '--noiommu-mode',
+        action='store_true',
+        help="""
+if IOMMU is not available, Enables no IOMMU mode for vfio drivers.
+        """)
     parser.add_argument(
         'devices',
         metavar='DEVICE',
@@ -706,6 +751,8 @@ def parse_args():
         status_dev = "all"
     if opt.force:
         force_flag = True
+    if opt.noiommu_mode:
+        noiommu_flag = True
     if opt.bind:
         b_flag = opt.bind
     elif opt.unbind:
Signature · 1 line
2.25.1
Acked-by on v3 1/1
#3 lore raw
Quoted 7 lines · Fidaullah Noonari

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---

Hi,

usertools/dpdk-devbind.py def unbind_all(dev_list, force=False): · line 486

Quoted 12 lines
usertools/dpdk-devbind.py +12 -0
+def check_noiommu_mode():
+    """checks and enables the noiommu mode for vfio drivers"""
+    global noiommu_flag
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+    try:
+        with open(filename,"r") as f:
+            if f.read(1) == "1":
+                return
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))

Nitpick, but maybe use f-strings instead of old-school string interpolation?

usertools/dpdk-devbind.py def unbind_all(dev_list, force=False): · line 499

Quoted 13 lines
usertools/dpdk-devbind.py +13 -0
+
+    if not noiommu_flag:
+        print("Error: failed to bind vfio-pci - IOMMU support is disabled")
+        print("Info: use --noiommu-mode for binding in noiommu mode")
+        sys.exit()
+
+    try:
+        with open(filename, "w") as f:
+            f.write("1")
+            f.close()
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))

Same as above.

Otherwise LGTM,

Signature · 2 lines
Thanks,
Anatoly
#4 lore raw
Quoted 11 lines · Anatoly
Quoted 7 lines · Fidaullah Noonari

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---

Hi,

Quoted 15 lines

usertools/dpdk-devbind.py def unbind_all(dev_list, force=False): · line 486

Quoted 12 lines
usertools/dpdk-devbind.py +12 -0
+def check_noiommu_mode():
+    """checks and enables the noiommu mode for vfio drivers"""
+    global noiommu_flag
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+    try:
+        with open(filename,"r") as f:
+            if f.read(1) == "1":
+                return
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))

Nitpick, but maybe use f-strings instead of old-school string interpolation?

Do we still not need to support some distros that don't have Python earlier than 3.6, where this support was added?

#5 lore raw

31/03/2022 16:44, Bruce Richardson:

Quoted 28 lines · Anatoly
Quoted 11 lines · Fidaullah Noonari
Quoted 7 lines

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---

Hi,

Quoted 15 lines

usertools/dpdk-devbind.py def unbind_all(dev_list, force=False): · line 486

Quoted 12 lines
usertools/dpdk-devbind.py +12 -0
+def check_noiommu_mode():
+    """checks and enables the noiommu mode for vfio drivers"""
+    global noiommu_flag
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+    try:
+        with open(filename,"r") as f:
+            if f.read(1) == "1":
+                return
+    except OSError as err:
+        sys.exit("Error: failed to enable unsafe noiommu mode - Cannot open %s: %s"
+                % (filename, err))

Nitpick, but maybe use f-strings instead of old-school string interpolation?

Quoted 2 lines

Do we still not need to support some distros that don't have Python earlier than 3.6, where this support was added?

The requirements have been upgraded to Python 3.6 with the new pmdinfo.

Are we waiting for a new version?

#6 lore raw

PATCH v4 1/1 usertools: add check for IOMMU support in dpdk-devbind

In reply to Fidaullah Noonari

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---
 usertools/dpdk-devbind.py | 45 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
usertools/dpdk-devbind.py +45 -0
index 4d9c1be666..d875ff23d6 100755
@@ -100,6 +100,7 @@
 b_flag = None
 status_flag = False
 force_flag = False
+noiommu_flag = False
 args = []
 
 
@@ -470,6 +471,37 @@ def unbind_all(dev_list, force=False):
         unbind_one(d, force)
 
 
+def has_iommu():
+    """Check if IOMMU is enabled on system"""
+    return len(os.listdir("/sys/class/iommu")) > 0
+
+
+def check_noiommu_mode():
+    """checks and enables the noiommu mode for vfio drivers"""
+    global noiommu_flag
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+    try:
+        with open(filename,"r") as f:
+            if f.read(1) == "1":
+                return
+    except OSError as err:
+        sys.exit(f"Error: failed to enable unsafe noiommu mode - Cannot open {filename}: {err}")
+
+    if not noiommu_flag:
+        print("Error: failed to bind vfio-pci - IOMMU support is disabled")
+        print("Info: use --noiommu-mode for binding in noiommu mode")
+        sys.exit()
+
+    try:
+        with open(filename, "w") as f:
+            f.write("1")
+            f.close()
+    except OSError as err:
+        sys.exit(f"Error: failed to enable unsafe noiommu mode - Cannot open {filename}: {err}")
+    print("Warning: enabling unsafe no IOMMU mode for vfio drivers")
+
+
 def bind_all(dev_list, driver, force=False):
     """Bind method, takes a list of device locations"""
     global devices
@@ -496,6 +528,10 @@ def bind_all(dev_list, driver, force=False):
     except ValueError as ex:
         sys.exit(ex)
 
+    # check for IOMMU support
+    if driver == "vfio-pci" and not has_iommu():
+        check_noiommu_mode()
+
     for d in dev_list:
         bind_one(d, driver, force)
 
@@ -638,6 +674,7 @@ def parse_args():
     global status_flag
     global status_dev
     global force_flag
+    global noiommu_flag
     global args
 
     parser = argparse.ArgumentParser(
@@ -691,6 +728,12 @@ def parse_args():
 Override restriction on binding devices in use by Linux"
 WARNING: This can lead to loss of network connection and should be used with caution.
 """)
+    parser.add_argument(
+        '--noiommu-mode',
+        action='store_true',
+        help="""
+if IOMMU is not available, Enables no IOMMU mode for vfio drivers.
+        """)
     parser.add_argument(
         'devices',
         metavar='DEVICE',
@@ -710,6 +753,8 @@ def parse_args():
         status_dev = "all"
     if opt.force:
         force_flag = True
+    if opt.noiommu_mode:
+        noiommu_flag = True
     if opt.bind:
         b_flag = opt.bind
     elif opt.unbind:
Signature · 1 line
2.25.1
#7 lore raw
Quoted 6 lines · On Wed, 12 Oct 2022 17:38:17 +0500 Fidaullah Noonari…

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

Not sure why this patch got stuck.
The flake8 tool reports:

usertools/dpdk-devbind.py:489:27: E231 missing whitespace after ',' Fix by:

usertools/dpdk-devbind.py +1 -1
index e7a5665fa2..a089313db1 100755
@@ -486,7 +486,7 @@ def check_noiommu_mode():
     filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
 
     try:
-        with open(filename,"r") as f:
+        with open(filename, "r") as f:
             if f.read(1) == "1":
                 return
     except OSError as err:
#8 lore raw

PATCH 1/1 usertools/devbind: add support for non-IOMMU mode

In reply to Fidaullah Noonari

From: Fidaullah Noonari <[email protected]>

This patch adds noiommu option to dpdk-devbind. If the no IOMMU is detected, then if noiommu flag is set the vfio-pci unsafe_noiommu_mode flag is set.

---
v2 - rebase patch and fix some minor stuff
remove unneeded f.close, simplify error and help messages

 usertools/dpdk-devbind.py | 40 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
usertools/dpdk-devbind.py +40 -0
index b276e8efc8..db336f519d 100755
@@ -107,6 +107,7 @@
 b_flag = None
 status_flag = False
 force_flag = False
+noiommu_flag = False
 args = []
 
 
@@ -477,6 +478,34 @@ def unbind_all(dev_list, force=False):
         unbind_one(d, force)
 
 
+def has_iommu():
+    """Check if IOMMU is enabled on system"""
+    return len(os.listdir("/sys/class/iommu")) > 0
+
+
+def check_noiommu_mode():
+    """checks and enables the noiommu mode for vfio drivers"""
+    global noiommu_flag
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+    try:
+        with open(filename, "r") as f:
+            if f.read(1) == "1":
+                return
+    except OSError as err:
+        sys.exit(f"Error: failed to check unsafe noiommu mode - Cannot open {filename}: {err}")
+
+    if not noiommu_flag:
+        sys.exit("Error: IOMMU support is disabled, use --noiommu-mode for binding in noiommu mode")
+
+    try:
+        with open(filename, "w") as f:
+            f.write("1")
+    except OSError as err:
+        sys.exit(f"Error: failed to enable unsafe noiommu mode - Cannot open {filename}: {err}")
+    print("Warning: enabling unsafe no IOMMU mode for vfio drivers")
+
+
 def bind_all(dev_list, driver, force=False):
     """Bind method, takes a list of device locations"""
     global devices
@@ -503,6 +532,10 @@ def bind_all(dev_list, driver, force=False):
     except ValueError as ex:
         sys.exit(ex)
 
+    # check for IOMMU support
+    if driver == "vfio-pci" and not has_iommu():
+        check_noiommu_mode()
+
     for d in dev_list:
         bind_one(d, driver, force)
 
@@ -648,6 +681,7 @@ def parse_args():
     global status_flag
     global status_dev
     global force_flag
+    global noiommu_flag
     global args
 
     parser = argparse.ArgumentParser(
@@ -694,6 +728,10 @@ def parse_args():
         '--unbind',
         action='store_true',
         help="Unbind a device (equivalent to \"-b none\")")
+    parser.add_argument(
+        '--noiommu-mode',
+        action='store_true',
+        help="If IOMMU is not available, Enables no IOMMU mode for vfio drivers")
     parser.add_argument(
         '--force',
         action='store_true',
@@ -720,6 +758,8 @@ def parse_args():
         status_dev = "all"
     if opt.force:
         force_flag = True
+    if opt.noiommu_mode:
+        noiommu_flag = True
     if opt.bind:
         b_flag = opt.bind
     elif opt.unbind:
Signature · 1 line
2.45.2
Acked-by on v1 1/1
#9 lore raw

Re: [PATCH] usertools/devbind: add support for non-IOMMU mode

Quoted 6 lines · Stephen Hemminger

From: Fidaullah Noonari <[email protected]>

This patch adds noiommu option to dpdk-devbind. If the no IOMMU is detected, then if noiommu flag is set the vfio-pci unsafe_noiommu_mode flag is set.

Quoted 6 lines

---
v2 - rebase patch and fix some minor stuff
remove unneeded f.close, simplify error and help messages

#10 lore raw

Re: [PATCH] usertools/devbind: add support for non-IOMMU mode

03/10/2024 11:27, Bruce Richardson:

Quoted 7 lines · Stephen Hemminger
Quoted 6 lines

From: Fidaullah Noonari <[email protected]>

This patch adds noiommu option to dpdk-devbind. If the no IOMMU is detected, then if noiommu flag is set the vfio-pci unsafe_noiommu_mode flag is set.

Quoted 7 lines
Quoted 6 lines

---
v2 - rebase patch and fix some minor stuff
remove unneeded f.close, simplify error and help messages

Applied with few wording changes, thanks.

#11 lore raw
In reply to Fidaullah Noonari
Quoted 7 lines · On Mon, 21 Mar 2022 17:27:27 +0500 Fidaullah Noonari…

binding with vfio driver, when IOMMU is disabled, causes program to crash. this patch adds a flag for noiommmu-mode. when this is set, if IOMMU is disabled, it changes vfio into unsafe noiommu mode and prints warning message.

---

Minor indentation issues reported by flake8 python checker:

./usertools/dpdk-devbind.py:489:27: E231 missing whitespace after ',' ./usertools/dpdk-devbind.py:494:17: E128 continuation line under-indented for visual indent ./usertools/dpdk-devbind.py:507:17: E128 continuation line under-indented for visual indent

Acked-by on v3 1/1
#12 lore raw
In reply to Fidaullah Noonari

usertools/dpdk-devbind.py def unbind_all(dev_list, force=False): · line 483

Quoted 10 lines · On Mon, 21 Mar 2022 17:27:27 +0500 Fidaullah Noonari…
usertools/dpdk-devbind.py +10 -0
+
+def check_noiommu_mode():
+    """checks and enables the noiommu mode for vfio drivers"""
+    global noiommu_flag
+    filename = "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode"
+
+    try:
+        with open(filename,"r") as f:
+            if f.read(1) == "1":
+                return

Note: flake8 reports missing comma after filename in the open() statement.

Overall