Skip to contentSkip to conversation

dma: Define dma_context_memory and use in sysbus-ohci

PATCH In review Peter Maydell 6 participants 12 messages Oct 23, 2012 – Nov 18, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma: Define dma_context_memory and use in sysbus-ohci

Define a new global dma_context_memory which is a DMAContext corresponding to the global address_space_memory AddressSpace. This can be used by sysbus peripherals like sysbus-ohci which need to do DMA.

In particular, use it in the sysbus-ohci device, which fixes a segfault when attempting to use that device.

---
As suggested by Avi. I could have split this patch into one defining the new global and one actually using it, but since the hcd-ohci change would be a one-liner it didn't seem worthwhile.

 dma.h             |    5 +++++
 exec.c            |    5 +++++
 hw/usb/hcd-ohci.c |    2 +-
 3 files changed, 11 insertions(+), 1 deletion(-)
dma.h +5 -0
index 1bd6f4a..f7cf5e7 100644
@@ -68,6 +68,11 @@ struct DMAContext {
     DMAUnmapFunc *unmap;
 };
 
+/* A global DMA context corresponding to the address_space_memory
+ * AddressSpace, for sysbus devices which do DMA.
+ */
+extern DMAContext dma_context_memory;
+
 static inline void dma_barrier(DMAContext *dma, DMADirection dir)
 {
     /*
exec.c +5 -0
index 750008c..a59ed31 100644
@@ -34,6 +34,7 @@
 #include "hw/xen.h"
 #include "qemu-timer.h"
 #include "memory.h"
+#include "dma.h"
 #include "exec-memory.h"
 #if defined(CONFIG_USER_ONLY)
 #include <qemu.h>
@@ -103,6 +104,7 @@ static MemoryRegion *system_io;
 
 AddressSpace address_space_io;
 AddressSpace address_space_memory;
+DMAContext dma_context_memory;
 
 MemoryRegion io_mem_ram, io_mem_rom, io_mem_unassigned, io_mem_notdirty;
 static MemoryRegion io_mem_subpage_ram;
@@ -3276,6 +3278,9 @@ static void memory_map_init(void)
     memory_listener_register(&core_memory_listener, &address_space_memory);
     memory_listener_register(&io_memory_listener, &address_space_io);
     memory_listener_register(&tcg_memory_listener, &address_space_memory);
+
+    dma_context_init(&dma_context_memory, &address_space_memory,
+                     NULL, NULL, NULL);
 }
 
 MemoryRegion *get_system_memory(void)
hw/usb/hcd-ohci.c +1 -1
index 59c7055..eb1cb70 100644
@@ -1846,7 +1846,7 @@ static int ohci_init_pxa(SysBusDevice *dev)
 
     /* Cannot fail as we pass NULL for masterbus */
     usb_ohci_init(&s->ohci, &dev->qdev, s->num_ports, s->dma_offset, NULL, 0,
-                  NULL);
+                  &dma_context_memory);
     sysbus_init_irq(dev, &s->ohci.irq);
     sysbus_init_mmio(dev, &s->ohci.mem);
 
Signature · 1 line
1.7.9.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Peter Maydell

Define a new global dma_context_memory which is a DMAContext corresponding to the global address_space_memory AddressSpace. This can be used by sysbus peripherals like sysbus-ohci which need to do DMA.

In particular, use it in the sysbus-ohci device, which fixes a segfault when attempting to use that device.

Quoted 47 lines

---
As suggested by Avi. I could have split this patch into one defining the new global and one actually using it, but since the hcd-ohci change would be a one-liner it didn't seem worthwhile.

 dma.h             |    5 +++++
 exec.c            |    5 +++++
 hw/usb/hcd-ohci.c |    2 +-
 3 files changed, 11 insertions(+), 1 deletion(-)
dma.h +5 -0
index 1bd6f4a..f7cf5e7 100644
@@ -68,6 +68,11 @@ struct DMAContext {
     DMAUnmapFunc *unmap;
 };
 
+/* A global DMA context corresponding to the address_space_memory
+ * AddressSpace, for sysbus devices which do DMA.
+ */
+extern DMAContext dma_context_memory;
+
 static inline void dma_barrier(DMAContext *dma, DMADirection dir)
 {
     /*
exec.c +2 -0
index 750008c..a59ed31 100644
@@ -34,6 +34,7 @@
 #include "hw/xen.h"
 #include "qemu-timer.h"
 #include "memory.h"
+#include "dma.h"
 #include "exec-memory.h"
 #if defined(CONFIG_USER_ONLY)
 #include <qemu.h>
@@ -103,6 +104,7 @@ static MemoryRegion *system_io;
 
 AddressSpace address_space_io;
 AddressSpace address_space_memory;
+DMAContext dma_context_memory;
 
 MemoryRegion io_mem_ram, io_mem_rom, io_mem_unassigned, io_mem_notdirty;
 static MemoryRegion io_mem_subpage_ram;

exec.c static void memory_map_init(void) · line 3278

Quoted 1 line
exec.c +0 -0
@@ -3276,6 +3278,9 @@ static void memory_map_init(void)
     memory_listener_register(&core_memory_listener,

&address_space_memory);

exec.c static void memory_map_init(void) · line 3280

Quoted 2 lines
exec.c +0 -0
     memory_listener_register(&io_memory_listener, &address_space_io);
     memory_listener_register(&tcg_memory_listener,

&address_space_memory);

Quoted 11 lines
exec.c +3 -0
+
+    dma_context_init(&dma_context_memory, &address_space_memory,
+                     NULL, NULL, NULL);
 }
 
 MemoryRegion *get_system_memory(void)
hw/usb/hcd-ohci.c +0 -0
index 59c7055..eb1cb70 100644

hw/usb/hcd-ohci.c static int ohci_init_pxa(SysBusDevice *dev) · line 1848

Quoted 3 lines
hw/usb/hcd-ohci.c +0 -0
@@ -1846,7 +1846,7 @@ static int ohci_init_pxa(SysBusDevice *dev)
 
     /* Cannot fail as we pass NULL for masterbus */
     usb_ohci_init(&s->ohci, &dev->qdev, s->num_ports, s->dma_offset,

NULL, 0,

Quoted 9 lines
hw/usb/hcd-ohci.c +1 -2
-                  NULL);
+                  &dma_context_memory);
     sysbus_init_irq(dev, &s->ohci.irq);
     sysbus_init_mmio(dev, &s->ohci.mem);
 
--

1.7.9.5

#2 lore raw
Quoted 11 lines · Peter Crosthwaite
Quoted 9 lines · Peter Maydell

Define a new global dma_context_memory which is a DMAContext corresponding to the global address_space_memory AddressSpace. This can be used by sysbus peripherals like sysbus-ohci which need to do DMA.

In particular, use it in the sysbus-ohci device, which fixes a segfault when attempting to use that device.

Hrm. So, as I originally conceived DMAContext, a NULL context pointer means "no translation" which is to say that DMA addresses are the same as memory space addresses. Which would mean a context explicitly for this purpose should not be necessary.

Has this assumption changed with the newer memory region integrated dma context stuff?

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
#3 lore raw
Quoted 13 lines · David Gibson
Quoted 11 lines · Peter Crosthwaite
Quoted 9 lines · Peter Maydell

Define a new global dma_context_memory which is a DMAContext corresponding to the global address_space_memory AddressSpace. This can be used by sysbus peripherals like sysbus-ohci which need to do DMA.

In particular, use it in the sysbus-ohci device, which fixes a segfault when attempting to use that device.

Quoted 7 lines

Hrm. So, as I originally conceived DMAContext, a NULL context pointer means "no translation" which is to say that DMA addresses are the same as memory space addresses. Which would mean a context explicitly for this purpose should not be necessary.

Has this assumption changed with the newer memory region integrated dma context stuff?

Yes, The Segfaulting line is in dma.h:

static inline int dma_memory_rw_relaxed(DMAContext *dma, dma_addr_t addr,
void *buf, dma_addr_t len, DMADirection dir)

{
    if (!dma_has_iommu(dma)) {
        /* Fast-path for no IOMMU */
        address_space_rw(dma->as, addr, buf, len, dir ==
DMA_DIRECTION_FROM_DEVICE);
        return 0;
    } else {
        return iommu_dma_memory_rw(dma, addr, buf, len, dir);
    }
}

Dereferencing of dma->as segfaults sd dma==NULL in the cas you described.

Quoted 7 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

#4 lore raw
Quoted 40 lines · Peter Crosthwaite
Quoted 13 lines · David Gibson
Quoted 11 lines · Peter Crosthwaite
Quoted 9 lines · Peter Maydell

Define a new global dma_context_memory which is a DMAContext corresponding to the global address_space_memory AddressSpace. This can be used by sysbus peripherals like sysbus-ohci which need to do DMA.

In particular, use it in the sysbus-ohci device, which fixes a segfault when attempting to use that device.

Quoted 7 lines

Hrm. So, as I originally conceived DMAContext, a NULL context pointer means "no translation" which is to say that DMA addresses are the same as memory space addresses. Which would mean a context explicitly for this purpose should not be necessary.

Has this assumption changed with the newer memory region integrated dma context stuff?

Yes, The Segfaulting line is in dma.h:

static inline int dma_memory_rw_relaxed(DMAContext *dma, dma_addr_t addr,
void *buf, dma_addr_t len, DMADirection dir)

{
    if (!dma_has_iommu(dma)) {
        /* Fast-path for no IOMMU */
        address_space_rw(dma->as, addr, buf, len, dir ==
DMA_DIRECTION_FROM_DEVICE);
        return 0;
    } else {
        return iommu_dma_memory_rw(dma, addr, buf, len, dir);
    }
}
Quoted 1 line

Dereferencing of dma->as segfaults sd dma==NULL in the cas you described.

Ok. My inclination would be to special case that in that function, setting as to the standard memory as if !dma, but others may have a different opinion.

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
#5 lore raw

Il 26/10/2012 05:58, David Gibson ha scritto:

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

static inline int dma_memory_rw_relaxed(DMAContext *dma, dma_addr_t addr,
void *buf, dma_addr_t len, DMADirection dir)

{
    if (!dma_has_iommu(dma)) {
        /* Fast-path for no IOMMU */
        address_space_rw(dma->as, addr, buf, len, dir ==
DMA_DIRECTION_FROM_DEVICE);
        return 0;
    } else {
        return iommu_dma_memory_rw(dma, addr, buf, len, dir);
    }
}
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Dereferencing of dma->as segfaults sd dma==NULL in the cas you described.

Ok. My inclination would be to special case that in that function, setting as to the standard memory as if !dma, but others may have a different opinion.

Me too, because I'm seeing the exact same segfault with virtio-scsi. Reproducible with:

x86_64-softmmu/qemu-system-x86_64 -device virtio-scsi-pci -drive if=none,id=cd -device scsi-cd,drive=cd

(you don't even need a medium in the drive, it segfaults as soon as the BIOS probes the device).

As soon as Avi's iommu patches go in, in fact, dma->as will just be as. Even if as == NULL were to be outlawed and you'd be forced to write get_address_space_memory(), taking the pain to create dummy DMAContexts now is just not worth it.

Paolo

#6 lore raw
Quoted 4 lines · Paolo Bonzini

As soon as Avi's iommu patches go in, in fact, dma->as will just be as. Even if as == NULL were to be outlawed and you'd be forced to write get_address_space_memory(), taking the pain to create dummy DMAContexts now is just not worth it.

Personally I think it's better not to permit NULL DMAContexts or AddressSpaces here, because they're kind of a hack (in the same way that the "system address space" is kind of a hack). In real hardware you probably aren't really doing dma to that address space but to some more local bus. dma_context_memory/address_space_memory are nice and easy to search for, whereas NULL isn't. [And in general I think NULL is too easy to use; if you have to go looking for the system dma context you've been prompted to think about whether that's the right one...]

-- PMM

#7 lore raw
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Paolo Bonzini

As soon as Avi's iommu patches go in, in fact, dma->as will just be as. Even if as == NULL were to be outlawed and you'd be forced to write get_address_space_memory(), taking the pain to create dummy DMAContexts now is just not worth it.

Quoted 9 lines

Personally I think it's better not to permit NULL DMAContexts or AddressSpaces here, because they're kind of a hack (in the same way that the "system address space" is kind of a hack). In real hardware you probably aren't really doing dma to that address space but to some more local bus. dma_context_memory/address_space_memory are nice and easy to search for, whereas NULL isn't. [And in general I think NULL is too easy to use; if you have to go looking for the system dma context you've been prompted to think about whether that's the right one...]

Ping! Can we have a ruling on what the right fix for this is so we can fix these segfaults before 1.3 release, please?

thanks
-- PMM

#8 lore raw

Il 13/11/2012 12:44, Peter Maydell ha scritto:

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

As soon as Avi's iommu patches go in, in fact, dma->as will just be as. Even if as == NULL were to be outlawed and you'd be forced to write get_address_space_memory(), taking the pain to create dummy DMAContexts now is just not worth it.

Quoted 9 lines
Quoted 9 lines

Personally I think it's better not to permit NULL DMAContexts or AddressSpaces here, because they're kind of a hack (in the same way that the "system address space" is kind of a hack). In real hardware you probably aren't really doing dma to that address space but to some more local bus. dma_context_memory/address_space_memory are nice and easy to search for, whereas NULL isn't. [And in general I think NULL is too easy to use; if you have to go looking for the system dma context you've been prompted to think about whether that's the right one...]

Quoted 2 lines

Ping! Can we have a ruling on what the right fix for this is so we can fix these segfaults before 1.3 release, please?

I think this patch is already in Gerd's queue.

Paolo

#9 lore raw

Hi,

Quoted 4 lines
Quoted 2 lines

Ping! Can we have a ruling on what the right fix for this is so we can fix these segfaults before 1.3 release, please?

I think this patch is already in Gerd's queue.

It isn't. It used to be in the usb queue, but after the debate how to fix this property restarted I've dropped it again so the discussion doesn't block the usb queue.

cheers,
Gerd

#10 lore raw

Il 13/11/2012 16:21, Gerd Hoffmann ha scritto:

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

Ping! Can we have a ruling on what the right fix for this is so we can fix these segfaults before 1.3 release, please?

I think this patch is already in Gerd's queue.

It isn't. It used to be in the usb queue, but after the debate how to fix this property restarted I've dropped it again so the discussion doesn't block the usb queue.

Ok, so since this breaks virtio-scsi too, I'm adding it to the scsi queue.

Paolo

#11 lore raw
In reply to Peter Maydell
Quoted 17 lines · Peter Maydell
Quoted 6 lines · Peter Maydell
Quoted 4 lines · Paolo Bonzini

As soon as Avi's iommu patches go in, in fact, dma->as will just be as. Even if as == NULL were to be outlawed and you'd be forced to write get_address_space_memory(), taking the pain to create dummy DMAContexts now is just not worth it.

Quoted 9 lines

Personally I think it's better not to permit NULL DMAContexts or AddressSpaces here, because they're kind of a hack (in the same way that the "system address space" is kind of a hack). In real hardware you probably aren't really doing dma to that address space but to some more local bus. dma_context_memory/address_space_memory are nice and easy to search for, whereas NULL isn't. [And in general I think NULL is too easy to use; if you have to go looking for the system dma context you've been prompted to think about whether that's the right one...]

Quoted 2 lines

Ping! Can we have a ruling on what the right fix for this is so we can fix these segfaults before 1.3 release, please?

I agree with you here. Callers should be fixed to supply the proper AddressSpace.

Signature · 1 line
error compiling committee.c: too many arguments to function