Skip to contentSkip to conversation

vfio-pci, spapr: Allow in-kernel acceleration

RFC PATCH In review Alexey Kardashevskiy 4 participants 16 messages Mar 28, 2017 – Mar 30, 2017

lore
Share & tools
lore
#0 lore raw

cover vfio-pci, spapr: Allow in-kernel acceleration

This is my current working tree to support kernel's "powerpc/kvm/vfio: Enable in-kernel acceleration".

Please comment. Thanks.

Alexey Kardashevskiy (3):
memory: Add get_fd() hook for IOMMU MR
vfio-pci: Reorder group-to-container attaching
vfio: Enable in-kernel acceleration via VFIO KVM device

 include/exec/memory.h         |  2 ++
 include/hw/vfio/vfio-common.h |  1 +
 target/ppc/kvm_ppc.h          |  6 ++++++
 hw/ppc/spapr_iommu.c          | 12 ++++++++++++
 hw/vfio/common.c              | 34 ++++++++++++++++++++++++----------
 hw/vfio/spapr.c               | 26 ++++++++++++++++++++++++++
 target/ppc/kvm.c              |  7 ++++++-
 hw/vfio/trace-events          |  1 +
 8 files changed, 78 insertions(+), 11 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

RFC PATCH 1/3 memory: Add get_fd() hook for IOMMU MR

---
 include/exec/memory.h | 2 ++
 hw/ppc/spapr_iommu.c  | 8 ++++++++
 2 files changed, 10 insertions(+)
include/exec/memory.h +2 -0
index e39256ad03..925c10b35b 100644
@@ -174,6 +174,8 @@ struct MemoryRegionIOMMUOps {
     void (*notify_flag_changed)(MemoryRegion *iommu,
                                 IOMMUNotifierFlag old_flags,
                                 IOMMUNotifierFlag new_flags);
+    /* Returns a kernel fd for IOMMU */
+    int (*get_fd)(MemoryRegion *iommu);
 };
 
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
hw/ppc/spapr_iommu.c +8 -0
index 9e30e148d6..b61c8f053e 100644
@@ -170,6 +170,13 @@ static void spapr_tce_notify_flag_changed(MemoryRegion *iommu,
     }
 }
 
+static int spapr_tce_get_fd(MemoryRegion *iommu)
+{
+    sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
+
+    return tcet->fd;
+}
+
 static int spapr_tce_table_post_load(void *opaque, int version_id)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
@@ -251,6 +258,7 @@ static MemoryRegionIOMMUOps spapr_iommu_ops = {
     .translate = spapr_tce_translate_iommu,
     .get_min_page_size = spapr_tce_get_min_page_size,
     .notify_flag_changed = spapr_tce_notify_flag_changed,
+    .get_fd = spapr_tce_get_fd,
 };
 
 static int spapr_tce_table_realize(DeviceState *dev)
Signature · 1 line
2.11.0
#2 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 1/3] memory: Add get_fd() hook for IOMMU MR

Quoted 11 lines · On Tue, 28 Mar 2017 20:05:28 +1100 Alexey Kardashevskiy <[email protected]> wrote:
---
 include/exec/memory.h | 2 ++
 hw/ppc/spapr_iommu.c  | 8 ++++++++
 2 files changed, 10 insertions(+)
include/exec/memory.h +0 -0
index e39256ad03..925c10b35b 100644

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 178

Quoted 5 lines
include/exec/memory.h +2 -0
@@ -174,6 +174,8 @@ struct MemoryRegionIOMMUOps {
     void (*notify_flag_changed)(MemoryRegion *iommu,
                                 IOMMUNotifierFlag old_flags,
                                 IOMMUNotifierFlag new_flags);
+    /* Returns a kernel fd for IOMMU */
+    int (*get_fd)(MemoryRegion *iommu);

What if we used this as a prototype:

int (*get_fd)(IOMMUFdType type, MemoryRegion *iommu);

And then we defined:

typedef enum {
SPAPR_IOMMU_TABLE_FD = 0,
} IOMMUFdType;

Such that you're actually asking the IOMMUOps for a specific type of FD and it either has it or not, so the caller doesn't need to assume what it is they get back.

Furthermore, add:

int memory_region_iommu_get_fd(IOMMUFdType type, MemoryRegion *mr)

{
    assert(memory_region_is_iommu(mr));

    if (mr->iommu_ops && mr->iommu_ops->get_fd) {
        return mr->iommu_ops->get_fd(type, mr);
    }

    return -1;
}

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 180

Quoted 2 lines
include/exec/memory.h +0 -0
 };
 

This should be two patches, patch 1 above, patch 2 below

Quoted 6 lines
include/exec/memory.h +0 -0
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
hw/ppc/spapr_iommu.c +0 -0
index 9e30e148d6..b61c8f053e 100644

hw/ppc/spapr_iommu.c static void spapr_tce_notify_flag_changed(MemoryRegion *iommu, · line 177

Quoted 8 lines
hw/ppc/spapr_iommu.c +5 -0
@@ -170,6 +170,13 @@ static void spapr_tce_notify_flag_changed(MemoryRegion *iommu,
     }
 }
 
+static int spapr_tce_get_fd(MemoryRegion *iommu)
+{
+    sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
+
+    return tcet->fd;

This would then be:

    return type == SPAPR_IOMMU_TABLE_FD ? tcet->fd : -1;
Quoted 13 lines
hw/ppc/spapr_iommu.c +3 -0
+}
+
 static int spapr_tce_table_post_load(void *opaque, int version_id)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
@@ -251,6 +258,7 @@ static MemoryRegionIOMMUOps spapr_iommu_ops = {
     .translate = spapr_tce_translate_iommu,
     .get_min_page_size = spapr_tce_get_min_page_size,
     .notify_flag_changed = spapr_tce_notify_flag_changed,
+    .get_fd = spapr_tce_get_fd,
 };
 
 static int spapr_tce_table_realize(DeviceState *dev)
#3 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 1/3] memory: Add get_fd() hook for IOMMU MR

Quoted 22 lines · Alex Williamson
Quoted 11 lines · On Tue, 28 Mar 2017 20:05:28 +1100 Alexey Kardashevskiy <[email protected]> wrote:
---
 include/exec/memory.h | 2 ++
 hw/ppc/spapr_iommu.c  | 8 ++++++++
 2 files changed, 10 insertions(+)
include/exec/memory.h +0 -0
index e39256ad03..925c10b35b 100644

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 178

Quoted 5 lines
include/exec/memory.h +2 -0
@@ -174,6 +174,8 @@ struct MemoryRegionIOMMUOps {
     void (*notify_flag_changed)(MemoryRegion *iommu,
                                 IOMMUNotifierFlag old_flags,
                                 IOMMUNotifierFlag new_flags);
+    /* Returns a kernel fd for IOMMU */
+    int (*get_fd)(MemoryRegion *iommu);

What if we used this as a prototype:

Quoted 7 lines

int (*get_fd)(IOMMUFdType type, MemoryRegion *iommu);

And then we defined:

typedef enum {
SPAPR_IOMMU_TABLE_FD = 0,
} IOMMUFdType;

Where do I put this enum definition? include/exec/memory.h? It does not have any mention of any platform yet...

I could pass char* instead of IOMMUFdType (and pass there something like TYPE_SPAPR_TCE_TABLE), would it be any better?

Quoted 57 lines

Such that you're actually asking the IOMMUOps for a specific type of FD and it either has it or not, so the caller doesn't need to assume what it is they get back.

Furthermore, add:

int memory_region_iommu_get_fd(IOMMUFdType type, MemoryRegion *mr)

{
    assert(memory_region_is_iommu(mr));

    if (mr->iommu_ops && mr->iommu_ops->get_fd) {
        return mr->iommu_ops->get_fd(type, mr);
    }

    return -1;
}

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 180

Quoted 2 lines
include/exec/memory.h +0 -0
 };
 

This should be two patches, patch 1 above, patch 2 below

Quoted 6 lines
include/exec/memory.h +0 -0
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
hw/ppc/spapr_iommu.c +0 -0
index 9e30e148d6..b61c8f053e 100644

hw/ppc/spapr_iommu.c static void spapr_tce_notify_flag_changed(MemoryRegion *iommu, · line 177

Quoted 8 lines
hw/ppc/spapr_iommu.c +5 -0
@@ -170,6 +170,13 @@ static void spapr_tce_notify_flag_changed(MemoryRegion *iommu,
     }
 }
 
+static int spapr_tce_get_fd(MemoryRegion *iommu)
+{
+    sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
+
+    return tcet->fd;

This would then be:

    return type == SPAPR_IOMMU_TABLE_FD ? tcet->fd : -1;
Quoted 13 lines
hw/ppc/spapr_iommu.c +3 -0
+}
+
 static int spapr_tce_table_post_load(void *opaque, int version_id)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
@@ -251,6 +258,7 @@ static MemoryRegionIOMMUOps spapr_iommu_ops = {
     .translate = spapr_tce_translate_iommu,
     .get_min_page_size = spapr_tce_get_min_page_size,
     .notify_flag_changed = spapr_tce_notify_flag_changed,
+    .get_fd = spapr_tce_get_fd,
 };
 
 static int spapr_tce_table_realize(DeviceState *dev)
Signature · 1 line
Alexey
#4 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 1/3] memory: Add get_fd() hook for IOMMU MR

Quoted 27 lines · On Wed, 29 Mar 2017 12:41:01 +1100 Alexey Kardashevskiy <[email protected]> wrote:
Quoted 26 lines · Alex Williamson
Quoted 11 lines · On Tue, 28 Mar 2017 20:05:28 +1100 Alexey Kardashevskiy <[email protected]> wrote:
---
 include/exec/memory.h | 2 ++
 hw/ppc/spapr_iommu.c  | 8 ++++++++
 2 files changed, 10 insertions(+)
include/exec/memory.h +0 -0
index e39256ad03..925c10b35b 100644

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 178

Quoted 5 lines
include/exec/memory.h +2 -0
@@ -174,6 +174,8 @@ struct MemoryRegionIOMMUOps {
     void (*notify_flag_changed)(MemoryRegion *iommu,
                                 IOMMUNotifierFlag old_flags,
                                 IOMMUNotifierFlag new_flags);
+    /* Returns a kernel fd for IOMMU */
+    int (*get_fd)(MemoryRegion *iommu);  

What if we used this as a prototype:

int (*get_fd)(IOMMUFdType type, MemoryRegion *iommu);

And then we defined:

Quoted 7 lines
Quoted 3 lines

typedef enum {
SPAPR_IOMMU_TABLE_FD = 0,
} IOMMUFdType;

Where do I put this enum definition? include/exec/memory.h? It does not have any mention of any platform yet...

I would assume memory.h, yes. It seems like the enum is just an abstraction, what does "get fd" mean generically to an IOMMU MemoryRegion? How can anyone else ever implement that callback if the initial user is assuming that the returned fd is a specific, yet unspecified type. If the API is "give me an fd for this type of thing" then the IOMMU driver can either provide it or indicate that type is not supported. There's really no platform knowledge at the memory API level, it's just a type of thing that means something to the driver providing the MemoryRegionIOMMUOps and the caller.

Quoted 2 lines

I could pass char* instead of IOMMUFdType (and pass there something like TYPE_SPAPR_TCE_TABLE), would it be any better?

Gack, an enum seems so much cleaner than requiring a strcmp. Thanks,

Alex

Quoted 58 lines
Quoted 56 lines

Such that you're actually asking the IOMMUOps for a specific type of FD and it either has it or not, so the caller doesn't need to assume what it is they get back.

Furthermore, add:

int memory_region_iommu_get_fd(IOMMUFdType type, MemoryRegion *mr)

{
    assert(memory_region_is_iommu(mr));

    if (mr->iommu_ops && mr->iommu_ops->get_fd) {
        return mr->iommu_ops->get_fd(type, mr);
    }

    return -1;
}

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 180

Quoted 2 lines
include/exec/memory.h +0 -0
 };
 

This should be two patches, patch 1 above, patch 2 below

Quoted 6 lines
include/exec/memory.h +0 -0
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
hw/ppc/spapr_iommu.c +0 -0
index 9e30e148d6..b61c8f053e 100644

hw/ppc/spapr_iommu.c static void spapr_tce_notify_flag_changed(MemoryRegion *iommu, · line 177

Quoted 8 lines
hw/ppc/spapr_iommu.c +5 -0
@@ -170,6 +170,13 @@ static void spapr_tce_notify_flag_changed(MemoryRegion *iommu,
     }
 }
 
+static int spapr_tce_get_fd(MemoryRegion *iommu)
+{
+    sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
+
+    return tcet->fd;  

This would then be:

    return type == SPAPR_IOMMU_TABLE_FD ? tcet->fd : -1;
Quoted 13 lines
hw/ppc/spapr_iommu.c +3 -0
+}
+
 static int spapr_tce_table_post_load(void *opaque, int version_id)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
@@ -251,6 +258,7 @@ static MemoryRegionIOMMUOps spapr_iommu_ops = {
     .translate = spapr_tce_translate_iommu,
     .get_min_page_size = spapr_tce_get_min_page_size,
     .notify_flag_changed = spapr_tce_notify_flag_changed,
+    .get_fd = spapr_tce_get_fd,
 };
 
 static int spapr_tce_table_realize(DeviceState *dev)  
#5 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 1/3] memory: Add get_fd() hook for IOMMU MR

Quoted 13 lines · Alex Williamson
Quoted 4 lines
Quoted 4 lines

What if we used this as a prototype:

int (*get_fd)(IOMMUFdType type, MemoryRegion *iommu);

Quoted 8 lines
Quoted 5 lines

And then we defined:

typedef enum {
SPAPR_IOMMU_TABLE_FD = 0,
} IOMMUFdType;

Where do I put this enum definition? include/exec/memory.h? It does not have any mention of any platform yet...

Quoted 9 lines

I would assume memory.h, yes. It seems like the enum is just an abstraction, what does "get fd" mean generically to an IOMMU MemoryRegion? How can anyone else ever implement that callback if the initial user is assuming that the returned fd is a specific, yet unspecified type. If the API is "give me an fd for this type of thing" then the IOMMU driver can either provide it or indicate that type is not supported. There's really no platform knowledge at the memory API level, it's just a type of thing that means something to the driver providing the MemoryRegionIOMMUOps and the caller.

I think we should move to a QOM hierarchy like

AbstractMemoryRegion

      MemoryRegion                  << adds MemoryRegionOps
      IOMMUMemoryRegion             << adds MemoryRegionIOMMUOps
        sPAPR_IOMMUMemoryRegion     << adds get_fd

but for now I'm okay with Alex's proposal.

Paolo

#6 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 1/3] memory: Add get_fd() hook for IOMMU MR

Quoted 26 lines · Paolo Bonzini
Quoted 13 lines · Alex Williamson
Quoted 4 lines
Quoted 4 lines

What if we used this as a prototype:

int (*get_fd)(IOMMUFdType type, MemoryRegion *iommu);

Quoted 8 lines
Quoted 5 lines

And then we defined:

typedef enum {
SPAPR_IOMMU_TABLE_FD = 0,
} IOMMUFdType;

Where do I put this enum definition? include/exec/memory.h? It does not have any mention of any platform yet...

Quoted 9 lines

I would assume memory.h, yes. It seems like the enum is just an abstraction, what does "get fd" mean generically to an IOMMU MemoryRegion? How can anyone else ever implement that callback if the initial user is assuming that the returned fd is a specific, yet unspecified type. If the API is "give me an fd for this type of thing" then the IOMMU driver can either provide it or indicate that type is not supported. There's really no platform knowledge at the memory API level, it's just a type of thing that means something to the driver providing the MemoryRegionIOMMUOps and the caller.

Quoted 6 lines

I think we should move to a QOM hierarchy like

AbstractMemoryRegion

      MemoryRegion                  << adds MemoryRegionOps
      IOMMUMemoryRegion             << adds MemoryRegionIOMMUOps
        sPAPR_IOMMUMemoryRegion     << adds get_fd

Right, this makes more sense to me than the enum as well.

Quoted 1 line

but for now I'm okay with Alex's proposal.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#7 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 1/3] memory: Add get_fd() hook for IOMMU MR

In reply to Alex Williamson
Quoted 22 lines · Alex Williamson
Quoted 11 lines · On Tue, 28 Mar 2017 20:05:28 +1100 Alexey Kardashevskiy <[email protected]> wrote:
---
 include/exec/memory.h | 2 ++
 hw/ppc/spapr_iommu.c  | 8 ++++++++
 2 files changed, 10 insertions(+)
include/exec/memory.h +0 -0
index e39256ad03..925c10b35b 100644

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 178

Quoted 5 lines
include/exec/memory.h +2 -0
@@ -174,6 +174,8 @@ struct MemoryRegionIOMMUOps {
     void (*notify_flag_changed)(MemoryRegion *iommu,
                                 IOMMUNotifierFlag old_flags,
                                 IOMMUNotifierFlag new_flags);
+    /* Returns a kernel fd for IOMMU */
+    int (*get_fd)(MemoryRegion *iommu);

What if we used this as a prototype:

Quoted 7 lines

int (*get_fd)(IOMMUFdType type, MemoryRegion *iommu);

And then we defined:

typedef enum {
SPAPR_IOMMU_TABLE_FD = 0,
} IOMMUFdType;

Are we expecting any new types of fd? Maybe it would be simpler just to name this spapr_tce_fd() or something more specific, and only generalize if we really need it for another fd type.

Quoted 57 lines

Such that you're actually asking the IOMMUOps for a specific type of FD and it either has it or not, so the caller doesn't need to assume what it is they get back.

Furthermore, add:

int memory_region_iommu_get_fd(IOMMUFdType type, MemoryRegion *mr)

{
    assert(memory_region_is_iommu(mr));

    if (mr->iommu_ops && mr->iommu_ops->get_fd) {
        return mr->iommu_ops->get_fd(type, mr);
    }

    return -1;
}

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 180

Quoted 2 lines
include/exec/memory.h +0 -0
 };
 

This should be two patches, patch 1 above, patch 2 below

Quoted 6 lines
include/exec/memory.h +0 -0
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
hw/ppc/spapr_iommu.c +0 -0
index 9e30e148d6..b61c8f053e 100644

hw/ppc/spapr_iommu.c static void spapr_tce_notify_flag_changed(MemoryRegion *iommu, · line 177

Quoted 8 lines
hw/ppc/spapr_iommu.c +5 -0
@@ -170,6 +170,13 @@ static void spapr_tce_notify_flag_changed(MemoryRegion *iommu,
     }
 }
 
+static int spapr_tce_get_fd(MemoryRegion *iommu)
+{
+    sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
+
+    return tcet->fd;

This would then be:

    return type == SPAPR_IOMMU_TABLE_FD ? tcet->fd : -1;
Quoted 13 lines
hw/ppc/spapr_iommu.c +3 -0
+}
+
 static int spapr_tce_table_post_load(void *opaque, int version_id)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
@@ -251,6 +258,7 @@ static MemoryRegionIOMMUOps spapr_iommu_ops = {
     .translate = spapr_tce_translate_iommu,
     .get_min_page_size = spapr_tce_get_min_page_size,
     .notify_flag_changed = spapr_tce_notify_flag_changed,
+    .get_fd = spapr_tce_get_fd,
 };
 
 static int spapr_tce_table_realize(DeviceState *dev)
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#8 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 1/3] memory: Add get_fd() hook for IOMMU MR

Quoted 27 lines · David Gibson
Quoted 26 lines · Alex Williamson
Quoted 11 lines · On Tue, 28 Mar 2017 20:05:28 +1100 Alexey Kardashevskiy <[email protected]> wrote:
---
 include/exec/memory.h | 2 ++
 hw/ppc/spapr_iommu.c  | 8 ++++++++
 2 files changed, 10 insertions(+)
include/exec/memory.h +0 -0
index e39256ad03..925c10b35b 100644

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 178

Quoted 5 lines
include/exec/memory.h +2 -0
@@ -174,6 +174,8 @@ struct MemoryRegionIOMMUOps {
     void (*notify_flag_changed)(MemoryRegion *iommu,
                                 IOMMUNotifierFlag old_flags,
                                 IOMMUNotifierFlag new_flags);
+    /* Returns a kernel fd for IOMMU */
+    int (*get_fd)(MemoryRegion *iommu);

What if we used this as a prototype:

int (*get_fd)(IOMMUFdType type, MemoryRegion *iommu);

And then we defined:

Quoted 7 lines
Quoted 3 lines

typedef enum {
SPAPR_IOMMU_TABLE_FD = 0,
} IOMMUFdType;

Are we expecting any new types of fd? Maybe it would be simpler just to name this spapr_tce_fd() or something more specific, and only generalize if we really need it for another fd type.

So far we managed to keep VFIO and sPAPR-IOMMU relatively separate - they do not include each others headers and interact via memory regions and kernel uapi interface. The only direct connection between VFIO and sPAPR at all is vfio_eeh_as_ok/vfio_eeh_as_op which is rather workaround. I like this separation tbh.

Quoted 59 lines
Quoted 58 lines

Such that you're actually asking the IOMMUOps for a specific type of FD and it either has it or not, so the caller doesn't need to assume what it is they get back.

Furthermore, add:

int memory_region_iommu_get_fd(IOMMUFdType type, MemoryRegion *mr)

{
    assert(memory_region_is_iommu(mr));

    if (mr->iommu_ops && mr->iommu_ops->get_fd) {
        return mr->iommu_ops->get_fd(type, mr);
    }

    return -1;
}

include/exec/memory.h struct MemoryRegionIOMMUOps { · line 180

Quoted 2 lines
include/exec/memory.h +0 -0
 };
 

This should be two patches, patch 1 above, patch 2 below

Quoted 6 lines
include/exec/memory.h +0 -0
 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
hw/ppc/spapr_iommu.c +0 -0
index 9e30e148d6..b61c8f053e 100644

hw/ppc/spapr_iommu.c static void spapr_tce_notify_flag_changed(MemoryRegion *iommu, · line 177

Quoted 8 lines
hw/ppc/spapr_iommu.c +5 -0
@@ -170,6 +170,13 @@ static void spapr_tce_notify_flag_changed(MemoryRegion *iommu,
     }
 }
 
+static int spapr_tce_get_fd(MemoryRegion *iommu)
+{
+    sPAPRTCETable *tcet = container_of(iommu, sPAPRTCETable, iommu);
+
+    return tcet->fd;

This would then be:

    return type == SPAPR_IOMMU_TABLE_FD ? tcet->fd : -1;
Quoted 13 lines
hw/ppc/spapr_iommu.c +3 -0
+}
+
 static int spapr_tce_table_post_load(void *opaque, int version_id)
 {
     sPAPRTCETable *tcet = SPAPR_TCE_TABLE(opaque);
@@ -251,6 +258,7 @@ static MemoryRegionIOMMUOps spapr_iommu_ops = {
     .translate = spapr_tce_translate_iommu,
     .get_min_page_size = spapr_tce_get_min_page_size,
     .notify_flag_changed = spapr_tce_notify_flag_changed,
+    .get_fd = spapr_tce_get_fd,
 };
 
 static int spapr_tce_table_realize(DeviceState *dev)
Signature · 1 line
Alexey
#9 lore raw

RFC PATCH 2/3 vfio-pci: Reorder group-to-container attaching

In reply to Alexey Kardashevskiy

At the moment VFIO PCI device initialization works as follows:
vfio_realize
vfio_get_group
vfio_connect_container
register memory listeners (1) update QEMU groups lists
vfio_kvm_device_add_group

Then (example for pseries) the machine reset hook triggers region_add() for all regions where listeners from (1) are listening:

ppc_spapr_reset
spapr_phb_reset
spapr_tce_table_enable
memory_region_add_subregion
vfio_listener_region_add
vfio_spapr_create_window

This scheme works fine until we need to handle VFIO PCI device hotplug _and_ we want to enable in-kernel acceleration on, i.e. after PCI hotplug we need a place to call
ioctl(vfio_kvm_device_fd, KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE). Since the ioctl needs a LIOBN fd (from sPAPRTCETable) and a IOMMU group fd (from VFIOGroup), vfio_listener_region_add() seems to be the only place for this ioctl().

However this only works during boot time because the machine reset happens strictly after all devices are finalized. When hotplug happens, vfio_listener_region_add() is called when a memory listener is registered but when this happens:
1. new group is not added to the container->group_list yet;
2. VFIO KVM device is unaware of the new IOMMU group.

This moves bits around to have all necessary VFIO infrastructure in place for both initial startup and hotplug cases.

---
 hw/vfio/common.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/vfio/common.c +11 -10
index f3ba9b9007..c75c7594d5 100644
@@ -1086,6 +1086,16 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
         goto free_container_exit;
     }
 
+    container->initialized = true;
+
+    vfio_kvm_device_add_group(group);
+
+    QLIST_INIT(&container->group_list);
+    QLIST_INSERT_HEAD(&space->containers, container, next);
+
+    group->container = container;
+    QLIST_INSERT_HEAD(&container->group_list, group, container_next);
+
     container->listener = vfio_memory_listener;
 
     memory_listener_register(&container->listener, container->space->as);
@@ -1097,16 +1107,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
         goto listener_release_exit;
     }
 
-    container->initialized = true;
-
-    QLIST_INIT(&container->group_list);
-    QLIST_INSERT_HEAD(&space->containers, container, next);
-
-    group->container = container;
-    QLIST_INSERT_HEAD(&container->group_list, group, container_next);
-
     return 0;
 listener_release_exit:
+    vfio_kvm_device_del_group(group);
     vfio_listener_release(container);
 
 free_container_exit:
@@ -1210,8 +1213,6 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
 
     QLIST_INSERT_HEAD(&vfio_group_list, group, next);
 
-    vfio_kvm_device_add_group(group);
-
     return group;
 
 close_fd_exit:
Signature · 1 line
2.11.0
#10 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 2/3] vfio-pci: Reorder group-to-container attaching

Quoted 46 lines · On Tue, 28 Mar 2017 20:05:29 +1100 Alexey Kardashevskiy <[email protected]> wrote:

At the moment VFIO PCI device initialization works as follows:
vfio_realize
vfio_get_group
vfio_connect_container
register memory listeners (1) update QEMU groups lists
vfio_kvm_device_add_group

Then (example for pseries) the machine reset hook triggers region_add() for all regions where listeners from (1) are listening:

ppc_spapr_reset
spapr_phb_reset
spapr_tce_table_enable
memory_region_add_subregion
vfio_listener_region_add
vfio_spapr_create_window

This scheme works fine until we need to handle VFIO PCI device hotplug _and_ we want to enable in-kernel acceleration on, i.e. after PCI hotplug we need a place to call
ioctl(vfio_kvm_device_fd, KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE). Since the ioctl needs a LIOBN fd (from sPAPRTCETable) and a IOMMU group fd (from VFIOGroup), vfio_listener_region_add() seems to be the only place for this ioctl().

However this only works during boot time because the machine reset happens strictly after all devices are finalized. When hotplug happens, vfio_listener_region_add() is called when a memory listener is registered but when this happens:
1. new group is not added to the container->group_list yet;
2. VFIO KVM device is unaware of the new IOMMU group.

This moves bits around to have all necessary VFIO infrastructure in place for both initial startup and hotplug cases.

---
 hw/vfio/common.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/vfio/common.c +0 -0
index f3ba9b9007..c75c7594d5 100644

hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1089

Quoted 4 lines
hw/vfio/common.c +1 -0
@@ -1086,6 +1086,16 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
         goto free_container_exit;
     }
 
+    container->initialized = true;

This ignores the purpose of this variable, which is to make runtime mapping faults fatal, but device realize time faults simply cause the device to be rejected. This cannot be moved above memory_listener_register().

Quoted 17 lines
hw/vfio/common.c +9 -1
+
+    vfio_kvm_device_add_group(group);
+
+    QLIST_INIT(&container->group_list);
+    QLIST_INSERT_HEAD(&space->containers, container, next);
+
+    group->container = container;
+    QLIST_INSERT_HEAD(&container->group_list, group, container_next);
+
     container->listener = vfio_memory_listener;
 
     memory_listener_register(&container->listener, container->space->as);
@@ -1097,16 +1107,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
         goto listener_release_exit;
     }
 
-    container->initialized = true;

hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1112

Quoted 10 lines
hw/vfio/common.c +1 -7
-
-    QLIST_INIT(&container->group_list);
-    QLIST_INSERT_HEAD(&space->containers, container, next);
-
-    group->container = container;
-    QLIST_INSERT_HEAD(&container->group_list, group, container_next);
-
     return 0;
 listener_release_exit:
+    vfio_kvm_device_del_group(group);

Where's the QLIST cleanup?

Quoted 12 lines
hw/vfio/common.c +0 -2
     vfio_listener_release(container);
 
 free_container_exit:
@@ -1210,8 +1213,6 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
 
     QLIST_INSERT_HEAD(&vfio_group_list, group, next);
 
-    vfio_kvm_device_add_group(group);
-
     return group;
 
 close_fd_exit:
#11 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 2/3] vfio-pci: Reorder group-to-container attaching

Quoted 52 lines · Alex Williamson
Quoted 49 lines · On Tue, 28 Mar 2017 20:05:29 +1100 Alexey Kardashevskiy <[email protected]> wrote:

At the moment VFIO PCI device initialization works as follows:
vfio_realize
vfio_get_group
vfio_connect_container
register memory listeners (1) update QEMU groups lists
vfio_kvm_device_add_group

Then (example for pseries) the machine reset hook triggers region_add() for all regions where listeners from (1) are listening:

ppc_spapr_reset
spapr_phb_reset
spapr_tce_table_enable
memory_region_add_subregion
vfio_listener_region_add
vfio_spapr_create_window

This scheme works fine until we need to handle VFIO PCI device hotplug _and_ we want to enable in-kernel acceleration on, i.e. after PCI hotplug we need a place to call
ioctl(vfio_kvm_device_fd, KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE). Since the ioctl needs a LIOBN fd (from sPAPRTCETable) and a IOMMU group fd (from VFIOGroup), vfio_listener_region_add() seems to be the only place for this ioctl().

However this only works during boot time because the machine reset happens strictly after all devices are finalized. When hotplug happens, vfio_listener_region_add() is called when a memory listener is registered but when this happens:
1. new group is not added to the container->group_list yet;
2. VFIO KVM device is unaware of the new IOMMU group.

This moves bits around to have all necessary VFIO infrastructure in place for both initial startup and hotplug cases.

---
 hw/vfio/common.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
hw/vfio/common.c +0 -0
index f3ba9b9007..c75c7594d5 100644
@@ -1086,6 +1086,16 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
         goto free_container_exit;
     }
 
Quoted 6 lines

hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1089

Quoted 1 line
hw/vfio/common.c +1 -0
+    container->initialized = true;

This ignores the purpose of this variable, which is to make runtime mapping faults fatal, but device realize time faults simply cause the device to be rejected. This cannot be moved above memory_listener_register().

Apart from that, the rest of the code motion looks ok, though. Even if it weren't for the hotplug case, have the container/group more-or-less initialized before registering the listener makes more logical sense to me.

Quoted 17 lines
Quoted 17 lines
hw/vfio/common.c +9 -0
+
+    vfio_kvm_device_add_group(group);
+
+    QLIST_INIT(&container->group_list);
+    QLIST_INSERT_HEAD(&space->containers, container, next);
+
+    group->container = container;
+    QLIST_INSERT_HEAD(&container->group_list, group, container_next);
+
     container->listener = vfio_memory_listener;
 
     memory_listener_register(&container->listener, container->space->as);
@@ -1097,16 +1107,9 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,
         goto listener_release_exit;
     }
 
Quoted 14 lines

hw/vfio/common.c static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, · line 1112

Quoted 11 lines
hw/vfio/common.c +1 -8
-    container->initialized = true;
-
-    QLIST_INIT(&container->group_list);
-    QLIST_INSERT_HEAD(&space->containers, container, next);
-
-    group->container = container;
-    QLIST_INSERT_HEAD(&container->group_list, group, container_next);
-
     return 0;
 listener_release_exit:
+    vfio_kvm_device_del_group(group);

Where's the QLIST cleanup?

Moving it does introduce more intermediate cleanup cases which need to be handled, though..

Quoted 15 lines
Quoted 14 lines
hw/vfio/common.c +0 -2
     vfio_listener_release(container);
 
 free_container_exit:
@@ -1210,8 +1213,6 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp)
 
     QLIST_INSERT_HEAD(&vfio_group_list, group, next);
 
-    vfio_kvm_device_add_group(group);
-
     return group;
 
 close_fd_exit:
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#12 lore raw

RFC PATCH 3/3 vfio: Enable in-kernel acceleration via VFIO KVM device

In reply to Alexey Kardashevskiy

This enables in-kernel acceleration of TCE update requests via VFIO KVM device.

---
 include/hw/vfio/vfio-common.h |  1 +
 target/ppc/kvm_ppc.h          |  6 ++++++
 hw/ppc/spapr_iommu.c          |  4 ++++
 hw/vfio/common.c              | 13 +++++++++++++
 hw/vfio/spapr.c               | 26 ++++++++++++++++++++++++++
 target/ppc/kvm.c              |  7 ++++++-
 hw/vfio/trace-events          |  1 +
 7 files changed, 57 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +1 -0
index c582de18c9..ee8c96cc4a 100644
@@ -175,6 +175,7 @@ extern const MemoryListener vfio_prereg_listener;
 int vfio_spapr_create_window(VFIOContainer *container,
                              MemoryRegionSection *section,
                              hwaddr *pgsize);
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd);
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space);
 
target/ppc/kvm_ppc.h +6 -0
index f48243d13f..ce7327a4e0 100644
@@ -46,6 +46,7 @@ void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
 int kvmppc_remove_spapr_tce(void *table, int pfd, uint32_t window_size);
 int kvmppc_reset_htab(int shift_hint);
 uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift);
+bool kvmppc_has_cap_spapr_vfio(void);
 #endif /* !CONFIG_USER_ONLY */
 bool kvmppc_has_cap_epr(void);
 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function);
@@ -216,6 +217,11 @@ static inline bool kvmppc_is_mem_backend_page_size_ok(char *obj_path)
     return true;
 }
 
+static inline bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return false;
+}
+
 #endif /* !CONFIG_USER_ONLY */
 
 static inline bool kvmppc_has_cap_epr(void)
hw/ppc/spapr_iommu.c +4 -0
index b61c8f053e..fc23d81645 100644
@@ -293,6 +293,10 @@ void spapr_tce_set_need_vfio(sPAPRTCETable *tcet, bool need_vfio)
 
     tcet->need_vfio = need_vfio;
 
+    if (!need_vfio || (tcet->fd != -1 && kvmppc_has_cap_spapr_vfio())) {
+        return;
+    }
+
     oldtable = tcet->table;
 
     tcet->table = spapr_tce_alloc_table(tcet->liobn,
hw/vfio/common.c +13 -0
index c75c7594d5..9aaf861904 100644
@@ -440,6 +440,19 @@ static void vfio_listener_region_add(MemoryListener *listener,
             goto fail;
         }
 
+#ifdef CONFIG_KVM
+        if (kvm_enabled() && section->mr->iommu_ops->get_fd) {
+            VFIOGroup *group;
+            int tablefd =  section->mr->iommu_ops->get_fd(section->mr);
+
+            if (tablefd != -1) {
+                QLIST_FOREACH(group, &container->group_list, container_next) {
+                    vfio_spapr_notify_kvm(vfio_kvm_device_fd,
+                                          group->fd, tablefd);
+                }
+            }
+        }
+#endif
         vfio_host_win_add(container, section->offset_within_address_space,
                           section->offset_within_address_space +
                           int128_get64(section->size) - 1, pgsize);
hw/vfio/spapr.c +26 -0
index 4409bcc0d7..dffef3bd5f 100644
@@ -17,6 +17,9 @@
 #include "hw/hw.h"
 #include "qemu/error-report.h"
 #include "trace.h"
+#ifdef CONFIG_KVM
+#include "linux/kvm.h"
+#endif
 
 static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section)
 {
@@ -187,6 +190,29 @@ int vfio_spapr_create_window(VFIOContainer *container,
     return 0;
 }
 
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd)
+{
+#ifdef CONFIG_KVM
+    struct kvm_vfio_spapr_tce param = {
+        .groupfd = groupfd,
+        .tablefd = tablefd
+    };
+    struct kvm_device_attr attr = {
+        .group = KVM_DEV_VFIO_GROUP,
+        .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
+        .addr = (uint64_t)(unsigned long)&param,
+    };
+
+    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
+        error_report("vfio: failed to setup fd %d for a group with fd %d: %s",
+                     param.tablefd, param.groupfd, strerror(errno));
+        return -errno;
+    }
+    trace_vfio_spapr_notify_kvm(groupfd, tablefd);
+#endif
+    return 0;
+}
+
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space)
 {
target/ppc/kvm.c +6 -1
index 560ce655c7..bca5fe7329 100644
@@ -131,7 +131,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
     cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
     cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
-    cap_spapr_vfio = false;
+    cap_spapr_vfio = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
     cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
     cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
     cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
@@ -2416,6 +2416,11 @@ bool kvmppc_has_cap_mmu_hash_v3(void)
     return cap_mmu_hash_v3;
 }
 
+bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return cap_spapr_vfio;
+}
+
 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
 {
     ObjectClass *oc = OBJECT_CLASS(pcc);
hw/vfio/trace-events +1 -0
index 2561c6d31a..084a92f7c2 100644
@@ -123,3 +123,4 @@ vfio_prereg_register(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"P
 vfio_prereg_unregister(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"PRIx64" ret=%d"
 vfio_spapr_create_window(int ps, uint64_t ws, uint64_t off) "pageshift=0x%x winsize=0x%"PRIx64" offset=0x%"PRIx64
 vfio_spapr_remove_window(uint64_t off) "offset=%"PRIx64
+vfio_spapr_notify_kvm(int groupfd, int tablefd) "Attached groupfd %d to liobn fd %d"
Signature · 1 line
2.11.0
#13 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 3/3] vfio: Enable in-kernel acceleration via VFIO KVM device

include/hw/vfio/vfio-common.h

Quoted 18 lines · On Tue, 28 Mar 2017 20:05:30 +1100 Alexey Kardashevskiy <[email protected]> wrote:

This enables in-kernel acceleration of TCE update requests via VFIO KVM device.

---
 include/hw/vfio/vfio-common.h |  1 +
 target/ppc/kvm_ppc.h          |  6 ++++++
 hw/ppc/spapr_iommu.c          |  4 ++++
 hw/vfio/common.c              | 13 +++++++++++++
 hw/vfio/spapr.c               | 26 ++++++++++++++++++++++++++
 target/ppc/kvm.c              |  7 ++++++-
 hw/vfio/trace-events          |  1 +
 7 files changed, 57 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +0 -0
index c582de18c9..ee8c96cc4a 100644

Two patches intermixed here again it seems. I'll refer to them as "A" and "B". Seems easy to split at the file level.

Patch "B"

target/ppc/kvm_ppc.h

Quoted 12 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -175,6 +175,7 @@ extern const MemoryListener vfio_prereg_listener;
 int vfio_spapr_create_window(VFIOContainer *container,
                              MemoryRegionSection *section,
                              hwaddr *pgsize);
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd);
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space);
 
target/ppc/kvm_ppc.h +0 -0
index f48243d13f..ce7327a4e0 100644

Patch "A"

hw/ppc/spapr_iommu.c

Quoted 24 lines
target/ppc/kvm_ppc.h +6 -0
@@ -46,6 +46,7 @@ void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
 int kvmppc_remove_spapr_tce(void *table, int pfd, uint32_t window_size);
 int kvmppc_reset_htab(int shift_hint);
 uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift);
+bool kvmppc_has_cap_spapr_vfio(void);
 #endif /* !CONFIG_USER_ONLY */
 bool kvmppc_has_cap_epr(void);
 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function);
@@ -216,6 +217,11 @@ static inline bool kvmppc_is_mem_backend_page_size_ok(char *obj_path)
     return true;
 }
 
+static inline bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return false;
+}
+
 #endif /* !CONFIG_USER_ONLY */
 
 static inline bool kvmppc_has_cap_epr(void)
hw/ppc/spapr_iommu.c +0 -0
index b61c8f053e..fc23d81645 100644

Patch "A"

hw/vfio/common.c

Quoted 15 lines
hw/ppc/spapr_iommu.c +4 -0
@@ -293,6 +293,10 @@ void spapr_tce_set_need_vfio(sPAPRTCETable *tcet, bool need_vfio)
 
     tcet->need_vfio = need_vfio;
 
+    if (!need_vfio || (tcet->fd != -1 && kvmppc_has_cap_spapr_vfio())) {
+        return;
+    }
+
     oldtable = tcet->table;
 
     tcet->table = spapr_tce_alloc_table(tcet->liobn,
hw/vfio/common.c +0 -0
index c75c7594d5..9aaf861904 100644

Patch "B"

hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 443

Quoted 5 lines
hw/vfio/common.c +1 -0
@@ -440,6 +440,19 @@ static void vfio_listener_region_add(MemoryListener *listener,
             goto fail;
         }
 
+#ifdef CONFIG_KVM

I don't think we need this just for kvm_enabled(), do we?

hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 446

Quoted 3 lines
hw/vfio/common.c +3 -0
+        if (kvm_enabled() && section->mr->iommu_ops->get_fd) {
+            VFIOGroup *group;
+            int tablefd =  section->mr->iommu_ops->get_fd(section->mr);

This would change to

    tablefd=memory_region_iommu_get_fd(SPAPR_IOMMU_TABLE_FD,section->mr);

hw/vfio/spapr.c

Quoted 16 lines
hw/vfio/common.c +9 -0
+
+            if (tablefd != -1) {
+                QLIST_FOREACH(group, &container->group_list, container_next) {
+                    vfio_spapr_notify_kvm(vfio_kvm_device_fd,
+                                          group->fd, tablefd);
+                }
+            }
+        }
+#endif
         vfio_host_win_add(container, section->offset_within_address_space,
                           section->offset_within_address_space +
                           int128_get64(section->size) - 1, pgsize);
hw/vfio/spapr.c +0 -0
index 4409bcc0d7..dffef3bd5f 100644

Patch "B"

target/ppc/kvm.c

Quoted 44 lines
hw/vfio/spapr.c +26 -0
@@ -17,6 +17,9 @@
 #include "hw/hw.h"
 #include "qemu/error-report.h"
 #include "trace.h"
+#ifdef CONFIG_KVM
+#include "linux/kvm.h"
+#endif
 
 static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section)
 {
@@ -187,6 +190,29 @@ int vfio_spapr_create_window(VFIOContainer *container,
     return 0;
 }
 
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd)
+{
+#ifdef CONFIG_KVM
+    struct kvm_vfio_spapr_tce param = {
+        .groupfd = groupfd,
+        .tablefd = tablefd
+    };
+    struct kvm_device_attr attr = {
+        .group = KVM_DEV_VFIO_GROUP,
+        .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
+        .addr = (uint64_t)(unsigned long)&param,
+    };
+
+    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
+        error_report("vfio: failed to setup fd %d for a group with fd %d: %s",
+                     param.tablefd, param.groupfd, strerror(errno));
+        return -errno;
+    }
+    trace_vfio_spapr_notify_kvm(groupfd, tablefd);
+#endif
+    return 0;
+}
+
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space)
 {
target/ppc/kvm.c +0 -0
index 560ce655c7..bca5fe7329 100644

Patch "A"

hw/vfio/trace-events

Quoted 25 lines
target/ppc/kvm.c +6 -1
@@ -131,7 +131,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
     cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
     cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
-    cap_spapr_vfio = false;
+    cap_spapr_vfio = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
     cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
     cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
     cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
@@ -2416,6 +2416,11 @@ bool kvmppc_has_cap_mmu_hash_v3(void)
     return cap_mmu_hash_v3;
 }
 
+bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return cap_spapr_vfio;
+}
+
 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
 {
     ObjectClass *oc = OBJECT_CLASS(pcc);
hw/vfio/trace-events +0 -0
index 2561c6d31a..084a92f7c2 100644

Patch "B"

Quoted 5 lines
hw/vfio/trace-events +1 -0
@@ -123,3 +123,4 @@ vfio_prereg_register(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"P
 vfio_prereg_unregister(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"PRIx64" ret=%d"
 vfio_spapr_create_window(int ps, uint64_t ws, uint64_t off) "pageshift=0x%x winsize=0x%"PRIx64" offset=0x%"PRIx64
 vfio_spapr_remove_window(uint64_t off) "offset=%"PRIx64
+vfio_spapr_notify_kvm(int groupfd, int tablefd) "Attached groupfd %d to liobn fd %d"
#14 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 3/3] vfio: Enable in-kernel acceleration via VFIO KVM device

Quoted 69 lines · Alex Williamson

include/hw/vfio/vfio-common.h

Quoted 18 lines · On Tue, 28 Mar 2017 20:05:30 +1100 Alexey Kardashevskiy <[email protected]> wrote:

This enables in-kernel acceleration of TCE update requests via VFIO KVM device.

---
 include/hw/vfio/vfio-common.h |  1 +
 target/ppc/kvm_ppc.h          |  6 ++++++
 hw/ppc/spapr_iommu.c          |  4 ++++
 hw/vfio/common.c              | 13 +++++++++++++
 hw/vfio/spapr.c               | 26 ++++++++++++++++++++++++++
 target/ppc/kvm.c              |  7 ++++++-
 hw/vfio/trace-events          |  1 +
 7 files changed, 57 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +0 -0
index c582de18c9..ee8c96cc4a 100644

Two patches intermixed here again it seems. I'll refer to them as "A" and "B". Seems easy to split at the file level.

Patch "B"

target/ppc/kvm_ppc.h

Quoted 12 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -175,6 +175,7 @@ extern const MemoryListener vfio_prereg_listener;
 int vfio_spapr_create_window(VFIOContainer *container,
                              MemoryRegionSection *section,
                              hwaddr *pgsize);
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd);
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space);
 
target/ppc/kvm_ppc.h +0 -0
index f48243d13f..ce7327a4e0 100644

Patch "A"

hw/ppc/spapr_iommu.c

Quoted 24 lines
target/ppc/kvm_ppc.h +6 -0
@@ -46,6 +46,7 @@ void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
 int kvmppc_remove_spapr_tce(void *table, int pfd, uint32_t window_size);
 int kvmppc_reset_htab(int shift_hint);
 uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift);
+bool kvmppc_has_cap_spapr_vfio(void);
 #endif /* !CONFIG_USER_ONLY */
 bool kvmppc_has_cap_epr(void);
 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function);
@@ -216,6 +217,11 @@ static inline bool kvmppc_is_mem_backend_page_size_ok(char *obj_path)
     return true;
 }
 
+static inline bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return false;
+}
+
 #endif /* !CONFIG_USER_ONLY */
 
 static inline bool kvmppc_has_cap_epr(void)
hw/ppc/spapr_iommu.c +0 -0
index b61c8f053e..fc23d81645 100644

Patch "A"

Quoted 7 lines

hw/ppc/spapr_iommu.c void spapr_tce_set_need_vfio(sPAPRTCETable *tcet, bool need_vfio) · line 298

Quoted 7 lines
hw/ppc/spapr_iommu.c +3 -0
@@ -293,6 +293,10 @@ void spapr_tce_set_need_vfio(sPAPRTCETable *tcet, bool need_vfio)
 
     tcet->need_vfio = need_vfio;
 
+    if (!need_vfio || (tcet->fd != -1 && kvmppc_has_cap_spapr_vfio())) {
+        return;
+    }

Separation to "A" and "B" makes sense most of the time, however this bit being put into "A" will look at the capability and change the behaviour effectively disabling TCE requests handling in the kernel as vfio_spapr_notify_kvm() only appears in "B". Bad for bisectability.

I could swap "A" and "B", this way vfio_spapr_notify_kvm() would fail but thing would keep working.

Quoted 11 lines

hw/vfio/common.c

Quoted 8 lines
hw/ppc/spapr_iommu.c +1 -0
+
     oldtable = tcet->table;
 
     tcet->table = spapr_tce_alloc_table(tcet->liobn,
hw/vfio/common.c +0 -0
index c75c7594d5..9aaf861904 100644

Patch "B"

Quoted 7 lines

hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 443

Quoted 5 lines
hw/vfio/common.c +1 -0
@@ -440,6 +440,19 @@ static void vfio_listener_region_add(MemoryListener *listener,
             goto fail;
         }
 
+#ifdef CONFIG_KVM

I don't think we need this just for kvm_enabled(), do we?

We do for vfio_kvm_device_fd - this one is defined under #ifdef.

Quoted 109 lines

hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 446

Quoted 4 lines
hw/vfio/common.c +3 -0
+        if (kvm_enabled() && section->mr->iommu_ops->get_fd) {
+            VFIOGroup *group;
+            int tablefd =  section->mr->iommu_ops->get_fd(section->mr);

This would change to

    tablefd=memory_region_iommu_get_fd(SPAPR_IOMMU_TABLE_FD,section->mr);

hw/vfio/spapr.c

Quoted 16 lines
hw/vfio/common.c +9 -0
+
+            if (tablefd != -1) {
+                QLIST_FOREACH(group, &container->group_list, container_next) {
+                    vfio_spapr_notify_kvm(vfio_kvm_device_fd,
+                                          group->fd, tablefd);
+                }
+            }
+        }
+#endif
         vfio_host_win_add(container, section->offset_within_address_space,
                           section->offset_within_address_space +
                           int128_get64(section->size) - 1, pgsize);
hw/vfio/spapr.c +0 -0
index 4409bcc0d7..dffef3bd5f 100644

Patch "B"

target/ppc/kvm.c

Quoted 44 lines
hw/vfio/spapr.c +26 -0
@@ -17,6 +17,9 @@
 #include "hw/hw.h"
 #include "qemu/error-report.h"
 #include "trace.h"
+#ifdef CONFIG_KVM
+#include "linux/kvm.h"
+#endif
 
 static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section)
 {
@@ -187,6 +190,29 @@ int vfio_spapr_create_window(VFIOContainer *container,
     return 0;
 }
 
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd)
+{
+#ifdef CONFIG_KVM
+    struct kvm_vfio_spapr_tce param = {
+        .groupfd = groupfd,
+        .tablefd = tablefd
+    };
+    struct kvm_device_attr attr = {
+        .group = KVM_DEV_VFIO_GROUP,
+        .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
+        .addr = (uint64_t)(unsigned long)&param,
+    };
+
+    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
+        error_report("vfio: failed to setup fd %d for a group with fd %d: %s",
+                     param.tablefd, param.groupfd, strerror(errno));
+        return -errno;
+    }
+    trace_vfio_spapr_notify_kvm(groupfd, tablefd);
+#endif
+    return 0;
+}
+
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space)
 {
target/ppc/kvm.c +0 -0
index 560ce655c7..bca5fe7329 100644

Patch "A"

hw/vfio/trace-events

Quoted 25 lines
target/ppc/kvm.c +6 -1
@@ -131,7 +131,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
     cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
     cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
-    cap_spapr_vfio = false;
+    cap_spapr_vfio = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
     cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
     cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
     cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
@@ -2416,6 +2416,11 @@ bool kvmppc_has_cap_mmu_hash_v3(void)
     return cap_mmu_hash_v3;
 }
 
+bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return cap_spapr_vfio;
+}
+
 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
 {
     ObjectClass *oc = OBJECT_CLASS(pcc);
hw/vfio/trace-events +0 -0
index 2561c6d31a..084a92f7c2 100644

Patch "B"

Quoted 5 lines
hw/vfio/trace-events +1 -0
@@ -123,3 +123,4 @@ vfio_prereg_register(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"P
 vfio_prereg_unregister(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"PRIx64" ret=%d"
 vfio_spapr_create_window(int ps, uint64_t ws, uint64_t off) "pageshift=0x%x winsize=0x%"PRIx64" offset=0x%"PRIx64
 vfio_spapr_remove_window(uint64_t off) "offset=%"PRIx64
+vfio_spapr_notify_kvm(int groupfd, int tablefd) "Attached groupfd %d to liobn fd %d"
Signature · 1 line
Alexey
#15 lore raw

Re: [Qemu-devel] [RFC PATCH qemu 3/3] vfio: Enable in-kernel acceleration via VFIO KVM device

In reply to Alexey Kardashevskiy
Quoted 72 lines · Alexey Kardashevskiy

This enables in-kernel acceleration of TCE update requests via VFIO KVM device.

---
 include/hw/vfio/vfio-common.h |  1 +
 target/ppc/kvm_ppc.h          |  6 ++++++
 hw/ppc/spapr_iommu.c          |  4 ++++
 hw/vfio/common.c              | 13 +++++++++++++
 hw/vfio/spapr.c               | 26 ++++++++++++++++++++++++++
 target/ppc/kvm.c              |  7 ++++++-
 hw/vfio/trace-events          |  1 +
 7 files changed, 57 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +1 -0
index c582de18c9..ee8c96cc4a 100644
@@ -175,6 +175,7 @@ extern const MemoryListener vfio_prereg_listener;
 int vfio_spapr_create_window(VFIOContainer *container,
                              MemoryRegionSection *section,
                              hwaddr *pgsize);
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd);
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space);
 
target/ppc/kvm_ppc.h +6 -0
index f48243d13f..ce7327a4e0 100644
@@ -46,6 +46,7 @@ void *kvmppc_create_spapr_tce(uint32_t liobn, uint32_t page_shift,
 int kvmppc_remove_spapr_tce(void *table, int pfd, uint32_t window_size);
 int kvmppc_reset_htab(int shift_hint);
 uint64_t kvmppc_rma_size(uint64_t current_size, unsigned int hash_shift);
+bool kvmppc_has_cap_spapr_vfio(void);
 #endif /* !CONFIG_USER_ONLY */
 bool kvmppc_has_cap_epr(void);
 int kvmppc_define_rtas_kernel_token(uint32_t token, const char *function);
@@ -216,6 +217,11 @@ static inline bool kvmppc_is_mem_backend_page_size_ok(char *obj_path)
     return true;
 }
 
+static inline bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return false;
+}
+
 #endif /* !CONFIG_USER_ONLY */
 
 static inline bool kvmppc_has_cap_epr(void)
hw/ppc/spapr_iommu.c +4 -0
index b61c8f053e..fc23d81645 100644
@@ -293,6 +293,10 @@ void spapr_tce_set_need_vfio(sPAPRTCETable *tcet, bool need_vfio)
 
     tcet->need_vfio = need_vfio;
 
+    if (!need_vfio || (tcet->fd != -1 && kvmppc_has_cap_spapr_vfio())) {
+        return;
+    }
+
     oldtable = tcet->table;
 
     tcet->table = spapr_tce_alloc_table(tcet->liobn,
hw/vfio/common.c +0 -0
index c75c7594d5..9aaf861904 100644
@@ -440,6 +440,19 @@ static void vfio_listener_region_add(MemoryListener *listener,
             goto fail;
         }

hw/vfio/common.c static void vfio_listener_region_add(MemoryListener *listener, · line 451

Quoted 10 lines
hw/vfio/common.c +9 -0
 
+#ifdef CONFIG_KVM
+        if (kvm_enabled() && section->mr->iommu_ops->get_fd) {
+            VFIOGroup *group;
+            int tablefd =  section->mr->iommu_ops->get_fd(section->mr);
+
+            if (tablefd != -1) {
+                QLIST_FOREACH(group, &container->group_list, container_next) {
+                    vfio_spapr_notify_kvm(vfio_kvm_device_fd,
+                                          group->fd, tablefd);

This is only going to make sense if we have both PAPR-style TCE tables on the guest and TCE-based IOMMU backend on the host. In which case wouldn't it make more sense to explicitly verify that, and upcast, rather than adding a new vaguely-specified get_fd hook.

Quoted 85 lines
hw/vfio/common.c +4 -0
+                }
+            }
+        }
+#endif
         vfio_host_win_add(container, section->offset_within_address_space,
                           section->offset_within_address_space +
                           int128_get64(section->size) - 1, pgsize);
hw/vfio/spapr.c +26 -0
index 4409bcc0d7..dffef3bd5f 100644
@@ -17,6 +17,9 @@
 #include "hw/hw.h"
 #include "qemu/error-report.h"
 #include "trace.h"
+#ifdef CONFIG_KVM
+#include "linux/kvm.h"
+#endif
 
 static bool vfio_prereg_listener_skipped_section(MemoryRegionSection *section)
 {
@@ -187,6 +190,29 @@ int vfio_spapr_create_window(VFIOContainer *container,
     return 0;
 }
 
+int vfio_spapr_notify_kvm(int vfio_kvm_device_fd, int groupfd, int tablefd)
+{
+#ifdef CONFIG_KVM
+    struct kvm_vfio_spapr_tce param = {
+        .groupfd = groupfd,
+        .tablefd = tablefd
+    };
+    struct kvm_device_attr attr = {
+        .group = KVM_DEV_VFIO_GROUP,
+        .attr = KVM_DEV_VFIO_GROUP_SET_SPAPR_TCE,
+        .addr = (uint64_t)(unsigned long)&param,
+    };
+
+    if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
+        error_report("vfio: failed to setup fd %d for a group with fd %d: %s",
+                     param.tablefd, param.groupfd, strerror(errno));
+        return -errno;
+    }
+    trace_vfio_spapr_notify_kvm(groupfd, tablefd);
+#endif
+    return 0;
+}
+
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space)
 {
target/ppc/kvm.c +6 -1
index 560ce655c7..bca5fe7329 100644
@@ -131,7 +131,7 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     cap_spapr_tce = kvm_check_extension(s, KVM_CAP_SPAPR_TCE);
     cap_spapr_tce_64 = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_64);
     cap_spapr_multitce = kvm_check_extension(s, KVM_CAP_SPAPR_MULTITCE);
-    cap_spapr_vfio = false;
+    cap_spapr_vfio = kvm_check_extension(s, KVM_CAP_SPAPR_TCE_VFIO);
     cap_one_reg = kvm_check_extension(s, KVM_CAP_ONE_REG);
     cap_hior = kvm_check_extension(s, KVM_CAP_PPC_HIOR);
     cap_epr = kvm_check_extension(s, KVM_CAP_PPC_EPR);
@@ -2416,6 +2416,11 @@ bool kvmppc_has_cap_mmu_hash_v3(void)
     return cap_mmu_hash_v3;
 }
 
+bool kvmppc_has_cap_spapr_vfio(void)
+{
+    return cap_spapr_vfio;
+}
+
 static PowerPCCPUClass *ppc_cpu_get_family_class(PowerPCCPUClass *pcc)
 {
     ObjectClass *oc = OBJECT_CLASS(pcc);
hw/vfio/trace-events +1 -0
index 2561c6d31a..084a92f7c2 100644
@@ -123,3 +123,4 @@ vfio_prereg_register(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"P
 vfio_prereg_unregister(uint64_t va, uint64_t size, int ret) "va=%"PRIx64" size=%"PRIx64" ret=%d"
 vfio_spapr_create_window(int ps, uint64_t ws, uint64_t off) "pageshift=0x%x winsize=0x%"PRIx64" offset=0x%"PRIx64
 vfio_spapr_remove_window(uint64_t off) "offset=%"PRIx64
+vfio_spapr_notify_kvm(int groupfd, int tablefd) "Attached groupfd %d to liobn fd %d"
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson