Skip to contentSkip to conversation

libxl_pci: check that host device is assignable before adding to the domain

PATCH In review Doug Magee 3 participants 8 messages Jan 16, 2012 – Jan 18, 2012

lore
Share & tools
lore

Doug Magee

author
#0 lore raw

PATCH 1/1 libxl_pci: check that host device is assignable before adding to the domain

Previously, on ..._pci_add, libxl only checks that a device is not assigned to another domain. This quick patch checks that the device is also owned by pciback, otherwise the call fails.

tools/libxl/libxl_pci.c +37 -3
diff -r 5b2676ac1321 -r 301cc006677f tools/libxl/libxl_pci.c
@@ -796,6 +796,9 @@ int libxl__device_pci_add(libxl__gc *gc,
     libxl_device_pci *assigned;
     int num_assigned, i, rc;
     int stubdomid = 0;
+    struct dirent *de;
+    DIR *dir;
+    int assignable = 0;
 
     rc = get_all_assigned_devices(gc, &assigned, &num_assigned);
     if ( rc ) {
@@ -809,6 +812,35 @@ int libxl__device_pci_add(libxl__gc *gc,
         goto out;
     }
 
+    dir = opendir(SYSFS_PCIBACK_DRIVER);
+    if ( NULL == dir ) {
+        if ( errno == ENOENT ) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Looks like pciback driver not loaded");
+        }else{
+            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER);
+        }
+        rc = ERROR_FAIL;
+        goto out_closedir;
+    }
+
+    while( (de = readdir(dir)) ) {
+        unsigned dom, bus, dev, func;
+        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
+            continue;
+        if ( dom == pcidev->domain && bus == pcidev->bus &&
+              dev == pcidev->dev && func == pcidev->func ) {
+            assignable = 1;
+            break;
+        }
+    }
+
+    if ( !assignable ) {
+        rc = ERROR_FAIL;
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "PCI device not owned by pciback");
+        goto out_closedir;
+    }
+
+
     libxl__device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
 
     stubdomid = libxl_get_stubdom_id(ctx, domid);
@@ -817,7 +849,7 @@ int libxl__device_pci_add(libxl__gc *gc,
         /* stubdomain is always running by now, even at create time */
         rc = do_pci_add(gc, stubdomid, &pcidev_s, 0);
         if ( rc )
-            goto out;
+            goto out_closedir;
     }
 
     orig_vdev = pcidev->vdevfn & ~7U;
@@ -826,11 +858,11 @@ int libxl__device_pci_add(libxl__gc *gc,
         if ( !(pcidev->vdevfn >> 3) ) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Must specify a v-slot for multi-function devices");
             rc = ERROR_INVAL;
-            goto out;
+            goto out_closedir;
         }
         if ( pci_multifunction_check(gc, pcidev, &pfunc_mask) ) {
             rc = ERROR_FAIL;
-            goto out;
+            goto out_closedir;
         }
         pcidev->vfunc_mask &= pfunc_mask;
         /* so now vfunc_mask == pfunc_mask */
@@ -855,6 +887,8 @@ int libxl__device_pci_add(libxl__gc *gc,
         }
     }
 
+out_closedir:
+    closedir(dir);
 out:
     return rc;
 }
#1 lore raw
Quoted 3 lines · Doug Magee

Previously, on ..._pci_add, libxl only checks that a device is not assigned to another domain. This quick patch checks that the device is also owned by pciback, otherwise the call fails.

Thanks Doug. Would this be better done by adding a call to libxl_device_pci_list_assignable and looking for the device in it? In fact from the looks of things this could replace the existing call to get_all_assigned_devices from .._pci_add since libxl_device_pci_list_assignable already omits devices which are assigned to another domain.

Ian.

Quoted 87 lines

diff -r 5b2676ac1321 -r 301cc006677f tools/libxl/libxl_pci.c

tools/libxl/libxl_pci.c +37 -3
@@ -796,6 +796,9 @@ int libxl__device_pci_add(libxl__gc *gc,
     libxl_device_pci *assigned;
     int num_assigned, i, rc;
     int stubdomid = 0;
+    struct dirent *de;
+    DIR *dir;
+    int assignable = 0;
 
     rc = get_all_assigned_devices(gc, &assigned, &num_assigned);
     if ( rc ) {
@@ -809,6 +812,35 @@ int libxl__device_pci_add(libxl__gc *gc,
         goto out;
     }
 
+    dir = opendir(SYSFS_PCIBACK_DRIVER);
+    if ( NULL == dir ) {
+        if ( errno == ENOENT ) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Looks like pciback driver not loaded");
+        }else{
+            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER);
+        }
+        rc = ERROR_FAIL;
+        goto out_closedir;
+    }
+
+    while( (de = readdir(dir)) ) {
+        unsigned dom, bus, dev, func;
+        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
+            continue;
+        if ( dom == pcidev->domain && bus == pcidev->bus &&
+              dev == pcidev->dev && func == pcidev->func ) {
+            assignable = 1;
+            break;
+        }
+    }
+
+    if ( !assignable ) {
+        rc = ERROR_FAIL;
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "PCI device not owned by pciback");
+        goto out_closedir;
+    }
+
+
     libxl__device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
 
     stubdomid = libxl_get_stubdom_id(ctx, domid);
@@ -817,7 +849,7 @@ int libxl__device_pci_add(libxl__gc *gc,
         /* stubdomain is always running by now, even at create time */
         rc = do_pci_add(gc, stubdomid, &pcidev_s, 0);
         if ( rc )
-            goto out;
+            goto out_closedir;
     }
 
     orig_vdev = pcidev->vdevfn & ~7U;
@@ -826,11 +858,11 @@ int libxl__device_pci_add(libxl__gc *gc,
         if ( !(pcidev->vdevfn >> 3) ) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Must specify a v-slot for multi-function devices");
             rc = ERROR_INVAL;
-            goto out;
+            goto out_closedir;
         }
         if ( pci_multifunction_check(gc, pcidev, &pfunc_mask) ) {
             rc = ERROR_FAIL;
-            goto out;
+            goto out_closedir;
         }
         pcidev->vfunc_mask &= pfunc_mask;
         /* so now vfunc_mask == pfunc_mask */
@@ -855,6 +887,8 @@ int libxl__device_pci_add(libxl__gc *gc,
         }
     }
 
+out_closedir:
+    closedir(dir);
 out:
     return rc;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw

That was my first thought, but this was also the first thing in libxl I touched and wasn't sure what would happen if I ended up nesting GC_INITs. If it's safe to do then I can call libxl_device_pci_list_assignable, otherwise I'd have to pull the meat out and put it in another function. What's the best way to do it?

Doug

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 4:47 AM
To: [email protected]
Cc: [email protected]; Ian Jackson; Stefano Stabellini
Subject: Re: [Xen-devel] [PATCH] libxl_pci: check that host device is assignable before adding to the domain

Quoted 3 lines · Doug Magee

Previously, on ..._pci_add, libxl only checks that a device is not assigned to another domain. This quick patch checks that the device is also owned by pciback, otherwise the call fails.

Thanks Doug. Would this be better done by adding a call to libxl_device_pci_list_assignable and looking for the device in it? In fact from the looks of things this could replace the existing call to get_all_assigned_devices from .._pci_add since libxl_device_pci_list_assignable already omits devices which are assigned to another domain.

Ian.

Quoted 87 lines

diff -r 5b2676ac1321 -r 301cc006677f tools/libxl/libxl_pci.c

tools/libxl/libxl_pci.c +37 -3
@@ -796,6 +796,9 @@ int libxl__device_pci_add(libxl__gc *gc,
     libxl_device_pci *assigned;
     int num_assigned, i, rc;
     int stubdomid = 0;
+    struct dirent *de;
+    DIR *dir;
+    int assignable = 0;
 
     rc = get_all_assigned_devices(gc, &assigned, &num_assigned);
     if ( rc ) {
@@ -809,6 +812,35 @@ int libxl__device_pci_add(libxl__gc *gc,
         goto out;
     }
 
+    dir = opendir(SYSFS_PCIBACK_DRIVER);
+    if ( NULL == dir ) {
+        if ( errno == ENOENT ) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Looks like pciback driver not loaded");
+        }else{
+            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER);
+        }
+        rc = ERROR_FAIL;
+        goto out_closedir;
+    }
+
+    while( (de = readdir(dir)) ) {
+        unsigned dom, bus, dev, func;
+        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
+            continue;
+        if ( dom == pcidev->domain && bus == pcidev->bus &&
+              dev == pcidev->dev && func == pcidev->func ) {
+            assignable = 1;
+            break;
+        }
+    }
+
+    if ( !assignable ) {
+        rc = ERROR_FAIL;
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "PCI device not owned by pciback");
+        goto out_closedir;
+    }
+
+
     libxl__device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
 
     stubdomid = libxl_get_stubdom_id(ctx, domid);
@@ -817,7 +849,7 @@ int libxl__device_pci_add(libxl__gc *gc,
         /* stubdomain is always running by now, even at create time */
         rc = do_pci_add(gc, stubdomid, &pcidev_s, 0);
         if ( rc )
-            goto out;
+            goto out_closedir;
     }
 
     orig_vdev = pcidev->vdevfn & ~7U;
@@ -826,11 +858,11 @@ int libxl__device_pci_add(libxl__gc *gc,
         if ( !(pcidev->vdevfn >> 3) ) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Must specify a v-slot for multi-function devices");
             rc = ERROR_INVAL;
-            goto out;
+            goto out_closedir;
         }
         if ( pci_multifunction_check(gc, pcidev, &pfunc_mask) ) {
             rc = ERROR_FAIL;
-            goto out;
+            goto out_closedir;
         }
         pcidev->vfunc_mask &= pfunc_mask;
         /* so now vfunc_mask == pfunc_mask */
@@ -855,6 +887,8 @@ int libxl__device_pci_add(libxl__gc *gc,
         }
     }
 
+out_closedir:
+    closedir(dir);
 out:
     return rc;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#3 lore raw

Please don't top post.

Quoted 5 lines · On Tue, 2012-01-17 at 14:05 +0000, [email protected] wrote:

That was my first thought, but this was also the first thing in libxl I touched and wasn't sure what would happen if I ended up nesting GC_INITs. If it's safe to do then I can call libxl_device_pci_list_assignable, otherwise I'd have to pull the meat out and put it in another function. What's the best way to do it?

You can use libxl__gc_owner(gc) (or the helpful CTX macro) to get a ctx from a gc which you use to call an externally visible function from within libxl -- it'll do the right thing (TM).

Ian.

Quoted 113 lines

Doug

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 4:47 AM
To: [email protected]
Cc: [email protected]; Ian Jackson; Stefano Stabellini
Subject: Re: [Xen-devel] [PATCH] libxl_pci: check that host device is assignable before adding to the domain

Quoted 3 lines · Doug Magee

Previously, on ..._pci_add, libxl only checks that a device is not assigned to another domain. This quick patch checks that the device is also owned by pciback, otherwise the call fails.

Thanks Doug. Would this be better done by adding a call to libxl_device_pci_list_assignable and looking for the device in it? In fact from the looks of things this could replace the existing call to get_all_assigned_devices from .._pci_add since libxl_device_pci_list_assignable already omits devices which are assigned to another domain.

Ian.

Quoted 87 lines

diff -r 5b2676ac1321 -r 301cc006677f tools/libxl/libxl_pci.c

tools/libxl/libxl_pci.c +37 -3
@@ -796,6 +796,9 @@ int libxl__device_pci_add(libxl__gc *gc,
     libxl_device_pci *assigned;
     int num_assigned, i, rc;
     int stubdomid = 0;
+    struct dirent *de;
+    DIR *dir;
+    int assignable = 0;
 
     rc = get_all_assigned_devices(gc, &assigned, &num_assigned);
     if ( rc ) {
@@ -809,6 +812,35 @@ int libxl__device_pci_add(libxl__gc *gc,
         goto out;
     }
 
+    dir = opendir(SYSFS_PCIBACK_DRIVER);
+    if ( NULL == dir ) {
+        if ( errno == ENOENT ) {
+            LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Looks like pciback driver not loaded");
+        }else{
+            LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Couldn't open %s", SYSFS_PCIBACK_DRIVER);
+        }
+        rc = ERROR_FAIL;
+        goto out_closedir;
+    }
+
+    while( (de = readdir(dir)) ) {
+        unsigned dom, bus, dev, func;
+        if ( sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) != 4 )
+            continue;
+        if ( dom == pcidev->domain && bus == pcidev->bus &&
+              dev == pcidev->dev && func == pcidev->func ) {
+            assignable = 1;
+            break;
+        }
+    }
+
+    if ( !assignable ) {
+        rc = ERROR_FAIL;
+        LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "PCI device not owned by pciback");
+        goto out_closedir;
+    }
+
+
     libxl__device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
 
     stubdomid = libxl_get_stubdom_id(ctx, domid);
@@ -817,7 +849,7 @@ int libxl__device_pci_add(libxl__gc *gc,
         /* stubdomain is always running by now, even at create time */
         rc = do_pci_add(gc, stubdomid, &pcidev_s, 0);
         if ( rc )
-            goto out;
+            goto out_closedir;
     }
 
     orig_vdev = pcidev->vdevfn & ~7U;
@@ -826,11 +858,11 @@ int libxl__device_pci_add(libxl__gc *gc,
         if ( !(pcidev->vdevfn >> 3) ) {
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Must specify a v-slot for multi-function devices");
             rc = ERROR_INVAL;
-            goto out;
+            goto out_closedir;
         }
         if ( pci_multifunction_check(gc, pcidev, &pfunc_mask) ) {
             rc = ERROR_FAIL;
-            goto out;
+            goto out_closedir;
         }
         pcidev->vfunc_mask &= pfunc_mask;
         /* so now vfunc_mask == pfunc_mask */
@@ -855,6 +887,8 @@ int libxl__device_pci_add(libxl__gc *gc,
         }
     }
 
+out_closedir:
+    closedir(dir);
 out:
     return rc;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw
Quoted 17 lines

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 9:16 AM
To: [email protected]
Cc: [email protected]; Ian Jackson; Stefano Stabellini
Subject: RE: [Xen-devel] [PATCH] libxl_pci: check that host device is assignable before adding to the domain

Please don't top post.

Quoted 5 lines · On Tue, 2012-01-17 at 14:05 +0000, [email protected] wrote:

That was my first thought, but this was also the first thing in libxl I touched and wasn't sure what would happen if I ended up nesting GC_INITs. If it's safe to do then I can call libxl_device_pci_list_assignable, otherwise I'd have to pull the meat out and put it in another function. What's the best way to do it?

Quoted 3 lines

You can use libxl__gc_owner(gc) (or the helpful CTX macro) to get a ctx from a gc which you use to call an externally visible function from within libxl -- it'll do the right thing (TM).

Cool, I'll send an updated patch along today.

Doug

Quoted 3 lines

Ian.

#5 lore raw
Quoted 17 lines · On Tue, Jan 17, 2012 at 09:24:39AM -0500, [email protected] wrote:
Quoted 17 lines

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 9:16 AM
To: [email protected]
Cc: [email protected]; Ian Jackson; Stefano Stabellini
Subject: RE: [Xen-devel] [PATCH] libxl_pci: check that host device is assignable before adding to the domain

Please don't top post.

Quoted 5 lines · On Tue, 2012-01-17 at 14:05 +0000, [email protected] wrote:

That was my first thought, but this was also the first thing in libxl I touched and wasn't sure what would happen if I ended up nesting GC_INITs. If it's safe to do then I can call libxl_device_pci_list_assignable, otherwise I'd have to pull the meat out and put it in another function. What's the best way to do it?

Quoted 5 lines
Quoted 3 lines

You can use libxl__gc_owner(gc) (or the helpful CTX macro) to get a ctx from a gc which you use to call an externally visible function from within libxl -- it'll do the right thing (TM).

Cool, I'll send an updated patch along today.

You might want to check for 'xen-pciback', 'pciback' and 'pci-stub' as they are valid.

Quoted 10 lines

Doug

Quoted 3 lines

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#6 lore raw
Quoted 16 lines

-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 3:13 PM
To: [email protected]
Cc: Ian Campbell; [email protected]; Ian Jackson; Stefano Stabellini
Subject: Re: [Xen-devel] [PATCH] libxl_pci: check that host device is assignable before adding to the domain

Quoted 6 lines · On Tue, Jan 17, 2012 at 09:24:39AM -0500, [email protected] wrote:
Quoted 6 lines

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 9:16 AM
To: [email protected]
Cc: [email protected]; Ian Jackson; Stefano Stabellini
Subject: RE: [Xen-devel] [PATCH] libxl_pci: check that host device

Quoted 9 lines

is

Quoted 6 lines
Quoted 6 lines

assignable before adding to the domain

Please don't top post.

Quoted 1 line · On Tue, 2012-01-17 at 14:05 +0000, [email protected] wrote:

That was my first thought, but this was also the first thing in

libxl

Quoted 1 line
Quoted 1 line
Quoted 1 line

I touched and wasn't sure what would happen if I ended up

nesting

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

GC_INITs. If it's safe to do then I can call libxl_device_pci_list_assignable, otherwise I'd have to pull the

meat

Quoted 1 line
Quoted 1 line
Quoted 1 line

out and put it in another function. What's the best way to do

it?

Quoted 2 lines
Quoted 2 lines

You can use libxl__gc_owner(gc) (or the helpful CTX macro) to get

a

Quoted 2 lines

ctx

Quoted 1 line
Quoted 1 line

from a gc which you use to call an externally visible function

from

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

within libxl -- it'll do the right thing (TM).

Cool, I'll send an updated patch along today.

You might want to check for 'xen-pciback', 'pciback' and 'pci-stub' as they are valid.

Agreed. I posted an updated patch that calls ..._list_assignable instead of looping through the directory itself. I'll work on a separate patch to update that function to check all applicable places.

In the 3.2.1 kernel I'm using, the module name is xen-pcipack, but the sysfs directory is just pciback. Is there an instance where the directory is called xen-pcipack?

Quoted 10 lines
Quoted 10 lines

Doug

Quoted 3 lines

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#7 lore raw
Quoted 44 lines · On Tue, Jan 17, 2012 at 05:02:08PM -0500, [email protected] wrote:
Quoted 16 lines

-----Original Message-----
From: Konrad Rzeszutek Wilk [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 3:13 PM
To: [email protected]
Cc: Ian Campbell; [email protected]; Ian Jackson; Stefano Stabellini
Subject: Re: [Xen-devel] [PATCH] libxl_pci: check that host device is assignable before adding to the domain

Quoted 6 lines · On Tue, Jan 17, 2012 at 09:24:39AM -0500, [email protected] wrote:
Quoted 6 lines

-----Original Message-----
From: Ian Campbell [mailto:[email protected]]
Sent: Tuesday, January 17, 2012 9:16 AM
To: [email protected]
Cc: [email protected]; Ian Jackson; Stefano Stabellini
Subject: RE: [Xen-devel] [PATCH] libxl_pci: check that host device

Quoted 9 lines

is

Quoted 6 lines
Quoted 6 lines

assignable before adding to the domain

Please don't top post.

Quoted 1 line · On Tue, 2012-01-17 at 14:05 +0000, [email protected] wrote:

That was my first thought, but this was also the first thing in

libxl

Quoted 1 line
Quoted 1 line
Quoted 1 line

I touched and wasn't sure what would happen if I ended up

nesting

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

GC_INITs. If it's safe to do then I can call libxl_device_pci_list_assignable, otherwise I'd have to pull the

meat

Quoted 1 line
Quoted 1 line
Quoted 1 line

out and put it in another function. What's the best way to do

it?

Quoted 2 lines
Quoted 2 lines

You can use libxl__gc_owner(gc) (or the helpful CTX macro) to get

a

Quoted 2 lines

ctx

Quoted 1 line
Quoted 1 line

from a gc which you use to call an externally visible function

from

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

within libxl -- it'll do the right thing (TM).

Cool, I'll send an updated patch along today.

You might want to check for 'xen-pciback', 'pciback' and 'pci-stub' as they are valid.

Quoted 7 lines

Agreed. I posted an updated patch that calls ..._list_assignable instead of looping through the directory itself. I'll work on a separate patch to update that function to check all applicable places.

In the 3.2.1 kernel I'm using, the module name is xen-pcipack, but the sysfs directory is just pciback. Is there an instance where the directory is called xen-pcipack?

Not yet. I was hoping that somebody would create a patch in the toolstack for it so that I can rename it to 'xen-pciback'.

So more of the 'this will happen in Q2 2012'