Skip to contentSkip to conversation

device-assignment: Better fd tracking

PATCH Superseded Alex Williamson 4 participants 7 messages Jul 7, 2010 – Jul 12, 2010 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device-assignment: Better fd tracking

Commit 909bfdba fixed a hole with not closing resource file descriptors but we need to be more careful about tracking which are real fds, otherwise we might close fd 0, which doesn't work out so well for stdio.

---

 hw/device-assignment.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
hw/device-assignment.c +5 -5
index 48ac73c..3bcb63d 100644
@@ -694,6 +694,7 @@ again:
 
         rp = dev->regions + r;
         rp->valid = 0;
+        rp->resource_fd = -1;
         size = end - start + 1;
         flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
         if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
@@ -785,7 +786,8 @@ static void free_assigned_device(AssignedDevice *dev)
                         fprintf(stderr,
 				"Failed to unmap assigned device region: %s\n",
 				strerror(errno));
-                    close(pci_region->resource_fd);
+                    if (pci_region->resource_fd >= 0)
+                        close(pci_region->resource_fd);
                 }
 	    }
         }
@@ -793,10 +795,8 @@ static void free_assigned_device(AssignedDevice *dev)
         if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
             assigned_dev_unregister_msix_mmio(dev);
 
-        if (dev->real_device.config_fd) {
+        if (dev->real_device.config_fd >= 0)
             close(dev->real_device.config_fd);
-            dev->real_device.config_fd = 0;
-        }
 
 #ifdef KVM_CAP_IRQ_ROUTING
         free_dev_irq_entries(dev);
@@ -1415,7 +1415,7 @@ static int assigned_initfn(struct PCIDevice *pci_dev)
 
     if (!dev->host.seg && !dev->host.bus && !dev->host.dev && !dev->host.func) {
         error_report("pci-assign: error: no host device specified");
-        goto out;
+        return -1;
     }
 
     if (get_real_device(dev, dev->host.seg, dev->host.bus,
#1 lore raw
Quoted 46 lines · Alex Williamson

Commit 909bfdba fixed a hole with not closing resource file descriptors but we need to be more careful about tracking which are real fds, otherwise we might close fd 0, which doesn't work out so well for stdio.

---

 hw/device-assignment.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
hw/device-assignment.c +4 -4
index 48ac73c..3bcb63d 100644
@@ -694,6 +694,7 @@ again:
 
         rp = dev->regions + r;
         rp->valid = 0;
+        rp->resource_fd = -1;
         size = end - start + 1;
         flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
         if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
@@ -785,7 +786,8 @@ static void free_assigned_device(AssignedDevice *dev)
                         fprintf(stderr,
 				"Failed to unmap assigned device region: %s\n",
 				strerror(errno));
-                    close(pci_region->resource_fd);
+                    if (pci_region->resource_fd >= 0)
+                        close(pci_region->resource_fd);
                 }
 	    }
         }
@@ -793,10 +795,8 @@ static void free_assigned_device(AssignedDevice *dev)
         if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
             assigned_dev_unregister_msix_mmio(dev);
 
-        if (dev->real_device.config_fd) {
+        if (dev->real_device.config_fd >= 0)
             close(dev->real_device.config_fd);
-            dev->real_device.config_fd = 0;
-        }
 
 #ifdef KVM_CAP_IRQ_ROUTING
         free_dev_irq_entries(dev);

hw/device-assignment.c static int assigned_initfn(struct PCIDevice *pci_dev) · line 1421

Quoted 8 lines
hw/device-assignment.c +1 -1
@@ -1415,7 +1415,7 @@ static int assigned_initfn(struct PCIDevice *pci_dev)
 
     if (!dev->host.seg && !dev->host.bus && !dev->host.dev && !dev->host.func) {
         error_report("pci-assign: error: no host device specified");
-        goto out;
+        return -1;
     }
 
     if (get_real_device(dev, dev->host.seg, dev->host.bus,

and why not change the goto out in the next 2 if-check's to return -1
to skip the free_assigned_device(dev) call for those 2 cases as well ?

#2 lore raw
Quoted 53 lines · Don Dutile
Quoted 52 lines · Alex Williamson

Commit 909bfdba fixed a hole with not closing resource file descriptors but we need to be more careful about tracking which are real fds, otherwise we might close fd 0, which doesn't work out so well for stdio.

---

 hw/device-assignment.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
hw/device-assignment.c +5 -5
index 48ac73c..3bcb63d 100644
@@ -694,6 +694,7 @@ again:
 
         rp = dev->regions + r;
         rp->valid = 0;
+        rp->resource_fd = -1;
         size = end - start + 1;
         flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
         if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
@@ -785,7 +786,8 @@ static void free_assigned_device(AssignedDevice *dev)
                         fprintf(stderr,
 				"Failed to unmap assigned device region: %s\n",
 				strerror(errno));
-                    close(pci_region->resource_fd);
+                    if (pci_region->resource_fd >= 0)
+                        close(pci_region->resource_fd);
                 }
 	    }
         }
@@ -793,10 +795,8 @@ static void free_assigned_device(AssignedDevice *dev)
         if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
             assigned_dev_unregister_msix_mmio(dev);
 
-        if (dev->real_device.config_fd) {
+        if (dev->real_device.config_fd >= 0)
             close(dev->real_device.config_fd);
-            dev->real_device.config_fd = 0;
-        }
 
 #ifdef KVM_CAP_IRQ_ROUTING
         free_dev_irq_entries(dev);
@@ -1415,7 +1415,7 @@ static int assigned_initfn(struct PCIDevice *pci_dev)
 
     if (!dev->host.seg && !dev->host.bus && !dev->host.dev && !dev->host.func) {
         error_report("pci-assign: error: no host device specified");
-        goto out;
+        return -1;
     }
 
Quoted 5 lines

hw/device-assignment.c static int assigned_initfn(struct PCIDevice *pci_dev) · line 1421

Quoted 2 lines
hw/device-assignment.c +0 -0
     if (get_real_device(dev, dev->host.seg, dev->host.bus,

and why not change the goto out in the next 2 if-check's to return -1
to skip the free_assigned_device(dev) call for those 2 cases as well ?

This first check is only doing a sanity check, nothing gets setup, so it's safe to just return directly. get_real_device() has a couple paths where it can exit after it has at least opened the config_fd, so it'd be nice to close that down, which free_assigned_device() does. I figure free_assigned_device() is safe to call for any exit from get_real_device() because a) setting the config_fd is the first thing it does, so config_fd should always be set to something and b) the resource_fds are still protected by the valid flag, which will be zero because the structure is allocated with mallocz, so we won't try to free uninitialized resource_fds. Thanks,

Alex

#3 lore raw
In reply to Alex Williamson

hw/device-assignment.c static void free_assigned_device(AssignedDevice *dev) · line 791

Quoted 12 lines · Alex Williamson

Commit 909bfdba fixed a hole with not closing resource file descriptors but we need to be more careful about tracking which are real fds, otherwise we might close fd 0, which doesn't work out so well for stdio.

hw/device-assignment.c +2 -1
@@ -785,7 +786,8 @@ static void free_assigned_device(AssignedDevice *dev)
                          fprintf(stderr,
  				"Failed to unmap assigned device region: %s\n",
  				strerror(errno));
-                    close(pci_region->resource_fd);
+                    if (pci_region->resource_fd>= 0)
+                        close(pci_region->resource_fd);
 

Missing { }s.

hw/device-assignment.c static void free_assigned_device(AssignedDevice *dev) · line 800

Quoted 13 lines
(quoted diff) +0 -0
                  }
  	    }
          }
hw/device-assignment.c +1 -3
@@ -793,10 +795,8 @@ static void free_assigned_device(AssignedDevice *dev)
          if (dev->cap.available&  ASSIGNED_DEVICE_CAP_MSIX)
              assigned_dev_unregister_msix_mmio(dev);
 
-        if (dev->real_device.config_fd) {
+        if (dev->real_device.config_fd>= 0)
              close(dev->real_device.config_fd);
-            dev->real_device.config_fd = 0;
-        }
 

Again.

hw/device-assignment.c static int assigned_initfn(struct PCIDevice *pci_dev) · line 1420

Quoted 11 lines
hw/device-assignment.c +1 -1
  #ifdef KVM_CAP_IRQ_ROUTING
          free_dev_irq_entries(dev);
@@ -1415,7 +1415,7 @@ static int assigned_initfn(struct PCIDevice *pci_dev)
 
      if (!dev->host.seg&&  !dev->host.bus&&  !dev->host.dev&&  !dev->host.func) {
          error_report("pci-assign: error: no host device specified");
-        goto out;
+        return -1;
      }
 

-error is better than -1, but this is not introduces by this patch, so okay.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

PATCH v2 1/1 device-assignment: Better fd tracking

In reply to Alex Williamson

Commit 909bfdba fixed a hole with not closing resource file descriptors but we need to be more careful about tracking which are real fds, otherwise we might close fd 0, which doesn't work out so well for stdio.

---

v2: fix qemu style braces

 hw/device-assignment.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
hw/device-assignment.c +6 -4
index 48ac73c..de9470c 100644
@@ -694,6 +694,7 @@ again:
 
         rp = dev->regions + r;
         rp->valid = 0;
+        rp->resource_fd = -1;
         size = end - start + 1;
         flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
         if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
@@ -785,7 +786,9 @@ static void free_assigned_device(AssignedDevice *dev)
                         fprintf(stderr,
 				"Failed to unmap assigned device region: %s\n",
 				strerror(errno));
-                    close(pci_region->resource_fd);
+                    if (pci_region->resource_fd >= 0) {
+                        close(pci_region->resource_fd);
+                    }
                 }
 	    }
         }
@@ -793,9 +796,8 @@ static void free_assigned_device(AssignedDevice *dev)
         if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
             assigned_dev_unregister_msix_mmio(dev);
 
-        if (dev->real_device.config_fd) {
+        if (dev->real_device.config_fd >= 0) {
             close(dev->real_device.config_fd);
-            dev->real_device.config_fd = 0;
         }
 
 #ifdef KVM_CAP_IRQ_ROUTING
@@ -1415,7 +1417,7 @@ static int assigned_initfn(struct PCIDevice *pci_dev)
 
     if (!dev->host.seg && !dev->host.bus && !dev->host.dev && !dev->host.func) {
         error_report("pci-assign: error: no host device specified");
-        goto out;
+        return -1;
     }
 
     if (get_real_device(dev, dev->host.seg, dev->host.bus,
Acked-by on v2 1/1
#5 lore raw
Quoted 57 lines · Alex Williamson

Commit 909bfdba fixed a hole with not closing resource file descriptors but we need to be more careful about tracking which are real fds, otherwise we might close fd 0, which doesn't work out so well for stdio.

---

v2: fix qemu style braces

 hw/device-assignment.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
hw/device-assignment.c +6 -5
index 48ac73c..de9470c 100644
@@ -694,6 +694,7 @@ again:
 
         rp = dev->regions + r;
         rp->valid = 0;
+        rp->resource_fd = -1;
         size = end - start + 1;
         flags &= IORESOURCE_IO | IORESOURCE_MEM | IORESOURCE_PREFETCH;
         if (size == 0 || (flags & ~IORESOURCE_PREFETCH) == 0)
@@ -785,7 +786,9 @@ static void free_assigned_device(AssignedDevice *dev)
                         fprintf(stderr,
 				"Failed to unmap assigned device region: %s\n",
 				strerror(errno));
-                    close(pci_region->resource_fd);
+                    if (pci_region->resource_fd >= 0) {
+                        close(pci_region->resource_fd);
+                    }
                 }
 	    }
         }
@@ -793,9 +796,8 @@ static void free_assigned_device(AssignedDevice *dev)
         if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
             assigned_dev_unregister_msix_mmio(dev);
 
-        if (dev->real_device.config_fd) {
+        if (dev->real_device.config_fd >= 0) {
             close(dev->real_device.config_fd);
-            dev->real_device.config_fd = 0;
         }
 
 #ifdef KVM_CAP_IRQ_ROUTING
@@ -1415,7 +1417,7 @@ static int assigned_initfn(struct PCIDevice *pci_dev)
 
     if (!dev->host.seg && !dev->host.bus && !dev->host.dev && !dev->host.func) {
         error_report("pci-assign: error: no host device specified");
-        goto out;
+        return -1;
     }
 
     if (get_real_device(dev, dev->host.seg, dev->host.bus,
 
--
Quoted 3 lines

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

thanks for reply on other if check's.

#6 lore raw
In reply to Alex Williamson
Quoted 11 lines · Alex Williamson

Commit 909bfdba fixed a hole with not closing resource file descriptors but we need to be more careful about tracking which are real fds, otherwise we might close fd 0, which doesn't work out so well for stdio.

---

v2: fix qemu style braces

 hw/device-assignment.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

Applied, thanks.