xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Fix xen crash when starting HVM guest due to missing io handler
@ 2016-05-13 17:09 suravee.suthikulpanit
  2016-05-13 17:10 ` [PATCH 1/2] x86/hvm: Add check when register " suravee.suthikulpanit
  2016-05-13 17:10 ` [PATCH 2/2] svm: iommu: Only call guest_iommu_init() after initialized HVM domain suravee.suthikulpanit
  0 siblings, 2 replies; 3+ messages in thread
From: suravee.suthikulpanit @ 2016-05-13 17:09 UTC (permalink / raw)
  To: xen-devel, george.dunlap, ian.campbell, jbeulich
  Cc: paul.durrant, Suravee Suthikulpanit

From: Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>

Hi All,

On systems with iommu v2 enabled, the hypervisor crashes when trying
to start up an HVM guest. 

Investigating shows that the guest_iommu_init() is called before the
HVM domain is initialized. It then tries to register_mmio_handler()
causing the hvm_next_io_handler() to increment the io_handler_count.
However, the registration fails silently and left the I/O handler
uninitialized.

At later time, hvm_find_io_handler() is called and iterate through
the registered handlered, but then resulting in referencing NULL
pointers.

This patch series proposes fix for this issue.

NOTE: For patch 2, since guest IOMMU emulation is still incompleted,
this change is tentative and will be verified in the future. Alterantively,
I can just simply remove the guest_iommu_init()/destroy() for now.
I will be also looking at re-enabling this feature in Xen.

Thanks,
Suravee

Suravee Suthikulpanit (2):
  x86/hvm: Add check when register io handler
  svm: iommu: Only call guest_iommu_init() after initialized HVM domain

 xen/arch/x86/hvm/intercept.c                |  8 ++++++--
 xen/arch/x86/hvm/svm/svm.c                  | 10 ++++++++++
 xen/drivers/passthrough/amd/iommu_guest.c   |  6 ++++++
 xen/drivers/passthrough/amd/pci_amd_iommu.c |  4 ----
 4 files changed, 22 insertions(+), 6 deletions(-)

-- 
1.9.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] x86/hvm: Add check when register io handler
  2016-05-13 17:09 [PATCH 0/2] Fix xen crash when starting HVM guest due to missing io handler suravee.suthikulpanit
@ 2016-05-13 17:10 ` suravee.suthikulpanit
  2016-05-13 17:10 ` [PATCH 2/2] svm: iommu: Only call guest_iommu_init() after initialized HVM domain suravee.suthikulpanit
  1 sibling, 0 replies; 3+ messages in thread
From: suravee.suthikulpanit @ 2016-05-13 17:10 UTC (permalink / raw)
  To: xen-devel, george.dunlap, ian.campbell, jbeulich
  Cc: paul.durrant, Suravee Suthikulpanit

From: Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>

At the time of registering HVM I/O handler, the HVM domain might
not have been initialized, which means the hvm_domain.io_handler
would be NULL. In the hvm_next_io_handler(), this should be checked
before returning and referencing the array. Also, the io_handler_count
should only be incremented on success.

So, this patch adds error handling in hvm_next_io_handler.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
 xen/arch/x86/hvm/intercept.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/xen/arch/x86/hvm/intercept.c b/xen/arch/x86/hvm/intercept.c
index 7096d74..13b81c9 100644
--- a/xen/arch/x86/hvm/intercept.c
+++ b/xen/arch/x86/hvm/intercept.c
@@ -248,14 +248,18 @@ int hvm_io_intercept(ioreq_t *p)
 
 struct hvm_io_handler *hvm_next_io_handler(struct domain *d)
 {
-    unsigned int i = d->arch.hvm_domain.io_handler_count++;
+    unsigned int i = d->arch.hvm_domain.io_handler_count;
 
-    if ( i == NR_IO_HANDLERS )
+    if ( !d->arch.hvm_domain.io_handler )
+        return NULL;
+
+    if ( i == NR_IO_HANDLERS - 1 )
     {
         domain_crash(d);
         return NULL;
     }
 
+    d->arch.hvm_domain.io_handler_count++;
     return &d->arch.hvm_domain.io_handler[i];
 }
 
-- 
1.9.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] svm: iommu: Only call guest_iommu_init() after initialized HVM domain
  2016-05-13 17:09 [PATCH 0/2] Fix xen crash when starting HVM guest due to missing io handler suravee.suthikulpanit
  2016-05-13 17:10 ` [PATCH 1/2] x86/hvm: Add check when register " suravee.suthikulpanit
@ 2016-05-13 17:10 ` suravee.suthikulpanit
  1 sibling, 0 replies; 3+ messages in thread
From: suravee.suthikulpanit @ 2016-05-13 17:10 UTC (permalink / raw)
  To: xen-devel, george.dunlap, ian.campbell, jbeulich
  Cc: paul.durrant, Suravee Suthikulpanit

From: Suravee Suthikulpanit <Suravee.Suthikulpanit@amd.com>

The guest_iommu_init() is currently called by the following code path:

    arch/x86/domain.c: arch_domain_create()
      ]- drivers/passthrough/iommu.c: iommu_domain_init()
        |- drivers/passthrough/amd/pci_amd_iommu.c: amd_iommu_domain_init();
          |- drivers/passthrough/amd/iommu_guest.c: guest_iommu_init()

At this point, the hvm_domain_initialised() has not been
called. So register_mmio_handler(), in guest_iommu_init(), silently fails.
This patch moves the call to guest_iommu_init/destroy() into
the svm_domain_intialise/_destroy() instead.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
 xen/arch/x86/hvm/svm/svm.c                  | 10 ++++++++++
 xen/drivers/passthrough/amd/iommu_guest.c   |  6 ++++++
 xen/drivers/passthrough/amd/pci_amd_iommu.c |  4 ----
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c
index e62dfa1..0c4affc 100644
--- a/xen/arch/x86/hvm/svm/svm.c
+++ b/xen/arch/x86/hvm/svm/svm.c
@@ -45,6 +45,7 @@
 #include <asm/hvm/support.h>
 #include <asm/hvm/io.h>
 #include <asm/hvm/emulate.h>
+#include <asm/hvm/svm/amd-iommu-proto.h>
 #include <asm/hvm/svm/asid.h>
 #include <asm/hvm/svm/svm.h>
 #include <asm/hvm/svm/vmcb.h>
@@ -1176,11 +1177,20 @@ void svm_host_osvw_init()
 
 static int svm_domain_initialise(struct domain *d)
 {
+    if ( is_hvm_domain(d) )
+        /*
+         * This requires the hvm domain to be
+         * initialized first.
+         */
+        return guest_iommu_init(d);
+
     return 0;
 }
 
 static void svm_domain_destroy(struct domain *d)
 {
+    if ( is_hvm_domain(d) )
+        guest_iommu_destroy(d);
 }
 
 static int svm_vcpu_initialise(struct vcpu *v)
diff --git a/xen/drivers/passthrough/amd/iommu_guest.c b/xen/drivers/passthrough/amd/iommu_guest.c
index b4e75ac..9f26765 100644
--- a/xen/drivers/passthrough/amd/iommu_guest.c
+++ b/xen/drivers/passthrough/amd/iommu_guest.c
@@ -891,6 +891,12 @@ int guest_iommu_init(struct domain* d)
          !has_viommu(d) )
         return 0;
 
+    if ( d->arch.hvm_domain.io_handler == NULL )
+    {
+        AMD_IOMMU_DEBUG("Error: uninitalized hvm io handler\n");
+        return 1;
+    }
+
     iommu = xzalloc(struct guest_iommu);
     if ( !iommu )
     {
diff --git a/xen/drivers/passthrough/amd/pci_amd_iommu.c b/xen/drivers/passthrough/amd/pci_amd_iommu.c
index c1c0b6b..f791618 100644
--- a/xen/drivers/passthrough/amd/pci_amd_iommu.c
+++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c
@@ -274,9 +274,6 @@ static int amd_iommu_domain_init(struct domain *d)
     hd->arch.paging_mode = is_hvm_domain(d) ?
                       IOMMU_PAGING_MODE_LEVEL_2 :
                       get_paging_mode(max_page);
-
-    guest_iommu_init(d);
-
     return 0;
 }
 
@@ -476,7 +473,6 @@ static void deallocate_iommu_page_tables(struct domain *d)
 
 static void amd_iommu_domain_destroy(struct domain *d)
 {
-    guest_iommu_destroy(d);
     deallocate_iommu_page_tables(d);
     amd_iommu_flush_all_pages(d);
 }
-- 
1.9.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-05-13 17:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-13 17:09 [PATCH 0/2] Fix xen crash when starting HVM guest due to missing io handler suravee.suthikulpanit
2016-05-13 17:10 ` [PATCH 1/2] x86/hvm: Add check when register " suravee.suthikulpanit
2016-05-13 17:10 ` [PATCH 2/2] svm: iommu: Only call guest_iommu_init() after initialized HVM domain suravee.suthikulpanit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).