debuggers.hg

changeset 22275:1385b15e168f

xl: Implement PCI passthrough force removal

This fixes two errors with removing PCI devices from HVM domains. The
first error is that the handling of "pci-rem" device-model command is
erroneously implemented in qemu and difficult (impossible?) to get
right.

For example, during domain shutdown there can be a race where the guest
OS unloads it's drivers and perhaps even shuts down PCI subsystem before
the pci-rem command has been received by qemu. This means that no OS is
present to write to the port which causes the dm command to be
acknowledged.

We fix this by implementing a 'force removal' option to
libxl_device_pci_remove which is always set to 1 during guest shutdown.
It can be optionally enabled on the xl command line for other occasions.

The second error is that if a guest OS doesn't respond to the SCI
interrupt and therefore the pci-rem dm command, which can happen if the
guest OS has no ACPI PCI hotplug support, then device removal bails with
an error but only AFTER removing the device from xenstore. This means
that xenstore gets in to an inconsistent state where an assigned device
also appears to be assignable.

This is fixed by moving xenstore device removal to occur only after the
device has really been removed.

Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
committer: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Gianni Tedesco <gianni.tedesco@citrix.com>
date Wed Oct 06 17:38:15 2010 +0100 (2010-10-06)
parents 02e199c96ece
children a33886146b45
files tools/libxl/libxl.h tools/libxl/libxl_pci.c tools/libxl/xl_cmdimpl.c
line diff
     1.1 --- a/tools/libxl/libxl.h	Wed Oct 06 11:00:19 2010 +0100
     1.2 +++ b/tools/libxl/libxl.h	Wed Oct 06 17:38:15 2010 +0100
     1.3 @@ -406,7 +406,7 @@ int libxl_device_vfb_clean_shutdown(libx
     1.4  int libxl_device_vfb_hard_shutdown(libxl_ctx *ctx, uint32_t domid);
     1.5  
     1.6  int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev);
     1.7 -int libxl_device_pci_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev);
     1.8 +int libxl_device_pci_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int force);
     1.9  int libxl_device_pci_shutdown(libxl_ctx *ctx, uint32_t domid);
    1.10  int libxl_device_pci_list_assigned(libxl_ctx *ctx, libxl_device_pci **list, uint32_t domid, int *num);
    1.11  int libxl_device_pci_list_assignable(libxl_ctx *ctx, libxl_device_pci **list, int *num);
     2.1 --- a/tools/libxl/libxl_pci.c	Wed Oct 06 11:00:19 2010 +0100
     2.2 +++ b/tools/libxl/libxl_pci.c	Wed Oct 06 17:38:15 2010 +0100
     2.3 @@ -841,7 +841,8 @@ out:
     2.4      return rc;
     2.5  }
     2.6  
     2.7 -static int do_pci_remove(libxl__gc *gc, uint32_t domid, libxl_device_pci *pcidev)
     2.8 +static int do_pci_remove(libxl__gc *gc, uint32_t domid,
     2.9 +                         libxl_device_pci *pcidev, int force)
    2.10  {
    2.11      libxl_ctx *ctx = libxl__gc_owner(gc);
    2.12      libxl_device_pci *assigned;
    2.13 @@ -858,8 +859,6 @@ static int do_pci_remove(libxl__gc *gc, 
    2.14          }
    2.15      }
    2.16  
    2.17 -    libxl_device_pci_remove_xenstore(gc, domid, pcidev);
    2.18 -
    2.19      hvm = libxl__domain_is_hvm(ctx, domid);
    2.20      if (hvm) {
    2.21          if (libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL) < 0) {
    2.22 @@ -878,7 +877,12 @@ static int do_pci_remove(libxl__gc *gc, 
    2.23              xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
    2.24              if (libxl__wait_for_device_model(ctx, domid, "pci-removed", NULL, NULL) < 0) {
    2.25                  LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Device Model didn't respond in time");
    2.26 -                return ERROR_FAIL;
    2.27 +                /* This depends on guest operating system acknowledging the
    2.28 +                 * SCI, if it doesn't respond in time then we may wish to 
    2.29 +                 * force the removal.
    2.30 +                 */
    2.31 +                if ( !force )
    2.32 +                    return ERROR_FAIL;
    2.33              }
    2.34          }
    2.35          path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
    2.36 @@ -924,7 +928,7 @@ skip1:
    2.37          if ((fscanf(f, "%u", &irq) == 1) && irq) {
    2.38              rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
    2.39              if (rc < 0) {
    2.40 -                LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
    2.41 +                LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_physdev_unmap_pirq irq=%d", irq);
    2.42              }
    2.43              rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
    2.44              if (rc < 0) {
    2.45 @@ -948,13 +952,16 @@ out:
    2.46      stubdomid = libxl_get_stubdom_id(ctx, domid);
    2.47      if (stubdomid != 0) {
    2.48          libxl_device_pci pcidev_s = *pcidev;
    2.49 -        libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
    2.50 +        libxl_device_pci_remove(ctx, stubdomid, &pcidev_s, force);
    2.51      }
    2.52  
    2.53 +    libxl_device_pci_remove_xenstore(gc, domid, pcidev);
    2.54 +
    2.55      return 0;
    2.56  }
    2.57  
    2.58 -int libxl_device_pci_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
    2.59 +int libxl_device_pci_remove(libxl_ctx *ctx, uint32_t domid,
    2.60 +                            libxl_device_pci *pcidev, int force)
    2.61  {
    2.62      libxl__gc gc = LIBXL_INIT_GC(ctx);
    2.63      unsigned int orig_vdev, pfunc_mask;
    2.64 @@ -980,7 +987,7 @@ int libxl_device_pci_remove(libxl_ctx *c
    2.65              }else{
    2.66                  pcidev->vdevfn = orig_vdev;
    2.67              }
    2.68 -            if ( do_pci_remove(&gc, domid, pcidev) )
    2.69 +            if ( do_pci_remove(&gc, domid, pcidev, force) )
    2.70                  rc = ERROR_FAIL;
    2.71          }
    2.72      }
    2.73 @@ -1049,7 +1056,11 @@ int libxl_device_pci_shutdown(libxl_ctx 
    2.74      if ( rc )
    2.75          return rc;
    2.76      for (i = 0; i < num; i++) {
    2.77 -        if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
    2.78 +        /* Force remove on shutdown since, on HVM, qemu will not always
    2.79 +         * respond to SCI interrupt because the guest kernel has shut down the
    2.80 +         * devices by the time we even get here!
    2.81 +         */
    2.82 +        if (libxl_device_pci_remove(ctx, domid, pcidevs + i, 1) < 0)
    2.83              return ERROR_FAIL;
    2.84      }
    2.85      free(pcidevs);
     3.1 --- a/tools/libxl/xl_cmdimpl.c	Wed Oct 06 11:00:19 2010 +0100
     3.2 +++ b/tools/libxl/xl_cmdimpl.c	Wed Oct 06 17:38:15 2010 +0100
     3.3 @@ -2262,7 +2262,7 @@ int main_pcilist(int argc, char **argv)
     3.4      return 0;
     3.5  }
     3.6  
     3.7 -static void pcidetach(char *dom, char *bdf)
     3.8 +static void pcidetach(char *dom, char *bdf, int force)
     3.9  {
    3.10      libxl_device_pci pcidev;
    3.11  
    3.12 @@ -2273,20 +2273,24 @@ static void pcidetach(char *dom, char *b
    3.13          fprintf(stderr, "pci-detach: malformed BDF specification \"%s\"\n", bdf);
    3.14          exit(2);
    3.15      }
    3.16 -    libxl_device_pci_remove(&ctx, domid, &pcidev);
    3.17 +    libxl_device_pci_remove(&ctx, domid, &pcidev, force);
    3.18      libxl_device_pci_destroy(&pcidev);
    3.19  }
    3.20  
    3.21  int main_pcidetach(int argc, char **argv)
    3.22  {
    3.23      int opt;
    3.24 +    int force = 0;
    3.25      char *domname = NULL, *bdf = NULL;
    3.26  
    3.27 -    while ((opt = getopt(argc, argv, "h")) != -1) {
    3.28 +    while ((opt = getopt(argc, argv, "hf")) != -1) {
    3.29          switch (opt) {
    3.30          case 'h':
    3.31              help("pci-detach");
    3.32              return 0;
    3.33 +        case 'f':
    3.34 +            force = 1;
    3.35 +            break;
    3.36          default:
    3.37              fprintf(stderr, "option not supported\n");
    3.38              break;
    3.39 @@ -2300,7 +2304,7 @@ int main_pcidetach(int argc, char **argv
    3.40      domname = argv[optind];
    3.41      bdf = argv[optind + 1];
    3.42  
    3.43 -    pcidetach(domname, bdf);
    3.44 +    pcidetach(domname, bdf, force);
    3.45      return 0;
    3.46  }
    3.47  static void pciattach(char *dom, char *bdf, char *vs)