Support message-based DMA in vfio-user server
v5 cover Support message-based DMA in vfio-user server
This series adds basic support for message-based DMA in qemu's vfio-user server. This is useful for cases where the client does not provide file descriptors for accessing system memory via memory mappings. My motivating use case is to hook up device models as PCIe endpoints to a hardware design. This works by bridging the PCIe transaction layer to vfio-user, and the endpoint does not access memory directly, but sends memory requests TLPs to the hardware design in order to perform DMA.
Note that more work is needed to make message-based DMA work well: qemu currently breaks down DMA accesses into chunks of size 8 bytes at maximum, each of which will be handled in a separate vfio-user DMA request message. This is quite terrible for large DMA accesses, such as when nvme reads and writes page-sized blocks for example. Thus, I would like to improve qemu to be able to perform larger accesses, at least for indirect memory regions. I have something working locally, but since this will likely result in more involved surgery and discussion, I am leaving this to be addressed in a separate patch.
Changes from v1:
* Address Stefan's review comments. In particular, enforce an allocation limit and don't drop the map client callbacks given that map requests can fail when hitting size limits.
* libvfio-user version bump now included in the series.
* Tested as well on big-endian s390x. This uncovered another byte order issue in vfio-user server code that I've included a fix for.
Changes from v2:
* Add a preparatory patch to make bounce buffering an AddressSpace-specific concept.
* The total buffer size limit parameter is now per AdressSpace and can be configured for PCIDevice via a property.
* Store a magic value in first bytes of bounce buffer struct as a best effort measure to detect invalid pointers in address_space_unmap.
Changes from v3:
* libvfio-user now supports twin-socket mode which uses separate sockets for client->server and server->client commands, respectively. This addresses the concurrent command bug triggered by server->client DMA access commands. See
https://github.com/nutanix/libvfio-user/issues/279 for details.
* Add missing teardown code in do_address_space_destroy.
* Fix bounce buffer size bookkeeping race condition.
* Generate unmap notification callbacks unconditionally.
* Some cosmetic fixes.
Changes from v4:
* Fix accidentally dropped memory_region_unref, control flow restored to match previous code to simplify review.
* Some cosmetic fixes.
Mattias Nissler (5):
softmmu: Per-AddressSpace bounce buffering
softmmu: Support concurrent bounce buffers
Update subprojects/libvfio-user
vfio-user: Message-based DMA support
vfio-user: Fix config space access byte order
hw/pci/pci.c | 8 ++ hw/remote/trace-events | 2 + hw/remote/vfio-user-obj.c | 88 ++++++++++++++++++--- include/exec/cpu-common.h | 2 - include/exec/memory.h | 41 +++++++++- include/hw/pci/pci_device.h | 3 + softmmu/dma-helpers.c | 4 +- softmmu/memory.c | 8 ++ softmmu/physmem.c | 141 ++++++++++++++++++---------------- subprojects/libvfio-user.wrap | 2 +- 10 files changed, 218 insertions(+), 81 deletions(-)
Signature · 1 line
2.34.1
PATCH v5 1/5 softmmu: Per-AddressSpace bounce buffering
Instead of using a single global bounce buffer, give each AddressSpace its own bounce buffer. The MapClient callback mechanism moves to AddressSpace accordingly.
This is in preparation for generalizing bounce buffer handling further to allow multiple bounce buffers, with a total allocation limit configured per AddressSpace.
--- include/exec/cpu-common.h | 2 - include/exec/memory.h | 45 ++++++++++++++++- softmmu/dma-helpers.c | 4 +- softmmu/memory.c | 7 +++ softmmu/physmem.c | 101 ++++++++++++++++---------------------- 5 files changed, 93 insertions(+), 66 deletions(-)
include/exec/cpu-common.h +0 -2
@@ -138,8 +138,6 @@ void *cpu_physical_memory_map(hwaddr addr, bool is_write);void cpu_physical_memory_unmap (void *buffer,hwaddr len,bool is_write,hwaddr access_len); -void cpu_register_map_client (QEMUBH *bh); -void cpu_unregister_map_client (QEMUBH *bh);bool cpu_physical_memory_is_io (hwaddr phys_addr);
include/exec/memory.h +43 -2
69 lines not on this page · raw message
softmmu/dma-helpers.c +2 -2
@@ -167,7 +167,7 @@ static void dma_blk_cb(void *opaque, int ret)if (dbs->iov.size ==0 ) {trace_dma_map_wait (dbs); dbs->bh =aio_bh_new (ctx, reschedule_dma, dbs); -cpu_register_map_client (dbs->bh); +address_space_register_map_client (dbs->sg->as, dbs->bh); goto out; }
@@ -197,7 +197,7 @@ static void dma_aio_cancel(BlockAIOCB *acb) }if (dbs->bh) { -cpu_unregister_map_client (dbs->bh); +address_space_unregister_map_client (dbs->sg->as, dbs->bh);qemu_bh_delete (dbs->bh); dbs->bh =NULL ; }
softmmu/memory.c +7 -0
@@ -3105,6 +3105,9 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) as->ioeventfds =NULL ;QTAILQ_INIT (&as->listeners);QTAILQ_INSERT_TAIL (&address_spaces, as, address_spaces_link); + as->bounce.in_use = false; +qemu_mutex_init (&as->map_client_list_lock); +QLIST_INIT (&as->map_client_list); as->name =g_strdup (name ? name :"anonymous" );address_space_update_topology (as);address_space_update_ioeventfds (as);
@@ -3112,6 +3115,10 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)static void do_address_space_destroy (AddressSpace *as) { +assert (!qatomic_read (&as->bounce.in_use)); +assert (QLIST_EMPTY (&as->map_client_list)); +qemu_mutex_destroy (&as->map_client_list_lock); +assert (QTAILQ_EMPTY (&as->listeners));flatview_unref (as->current_map);
softmmu/physmem.c +41 -60
179 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v5 2/5 softmmu: Support concurrent bounce buffers
In reply to Mattias NisslerWhen DMA memory can't be directly accessed, as is the case when running the device model in a separate process without shareable DMA file descriptors, bounce buffering is used.
It is not uncommon for device models to request mapping of several DMA regions at the same time. Examples include:
* net devices, e.g. when transmitting a packet that is split across several TX descriptors (observed with igb)
* USB host controllers, when handling a packet with multiple data TRBs (observed with xhci)
Previously, qemu only provided a single bounce buffer per AddressSpace and would fail DMA map requests while the buffer was already in use. In turn, this would cause DMA failures that ultimately manifest as hardware errors from the guest perspective.
This change allocates DMA bounce buffers dynamically instead of supporting only a single buffer. Thus, multiple DMA mappings work correctly also when RAM can't be mmap()-ed.
The total bounce buffer allocation size is limited individually for each AddressSpace. The default limit is 4096 bytes, matching the previous maximum buffer size. A new x-max-bounce-buffer-size parameter is provided to configure the limit for PCI devices.
--- hw/pci/pci.c | 8 ++++ include/exec/memory.h | 14 +++---- include/hw/pci/pci_device.h | 3 ++ softmmu/memory.c | 5 ++- softmmu/physmem.c | 80 +++++++++++++++++++++++++------------ 5 files changed, 74 insertions(+), 36 deletions(-)
hw/pci/pci.c +8 -0
@@ -85,6 +85,8 @@ static Property pci_props[] = { QEMU_PCIE_ERR_UNC_MASK_BITNR, true),DEFINE_PROP_BIT ("x-pcie-ari-nextfn-1" , PCIDevice, cap_present,QEMU_PCIE_ARI_NEXTFN_1_BITNR , false), +DEFINE_PROP_SIZE ("x-max-bounce-buffer-size" , PCIDevice, + max_bounce_buffer_size,DEFAULT_MAX_BOUNCE_BUFFER_SIZE ), DEFINE_PROP_END_OF_LIST() };
@@ -1208,6 +1210,8 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, "bus master container", UINT64_MAX);address_space_init (&pci_dev->bus_master_as, &pci_dev->bus_master_container_region, pci_dev->name); + pci_dev->bus_master_as.max_bounce_buffer_size = + pci_dev->max_bounce_buffer_size;if (phase_check (PHASE_MACHINE_READY )) {pci_init_bus_master (pci_dev);
@@ -2664,6 +2668,10 @@ static void pci_device_class_init(ObjectClass *klass, void *data) k->unrealize = pci_qdev_unrealize; k->bus_type =TYPE_PCI_BUS ;device_class_set_props (k, pci_props); +object_class_property_set_description ( + klass,"x-max-bounce-buffer-size" , +"Maximum buffer size allocated for bounce buffers used for mapped " +"access to indirect DMA memory" ); }static void pci_device_class_base_init (ObjectClass *klass,void *data)
include/exec/memory.h +5 -9
@@ -1081,13 +1081,7 @@ typedef struct AddressSpaceMapClient {QLIST_ENTRY (AddressSpaceMapClient) link; } AddressSpaceMapClient; -typedef struct { -MemoryRegion *mr; -void *buffer; -hwaddr addr; -hwaddr len; -bool in_use; -}BounceBuffer ; +#define DEFAULT_MAX_BOUNCE_BUFFER_SIZE (4096)/** * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
@@ -1106,8 +1100,10 @@ struct AddressSpace { QTAILQ_HEAD(, MemoryListener) listeners;QTAILQ_ENTRY (AddressSpace) address_spaces_link; -/* Bounce buffer to use for this address space. */ -BounceBuffer bounce; +/* Maximum DMA bounce buffer size used for indirect memory map requests */ +uint64_t max_bounce_buffer_size; +/* Total size of bounce buffers currently allocated, atomically accessed */ +uint64_t bounce_buffer_size;/* List of callbacks to invoke when buffers free up */ QemuMutex map_client_list_lock; QLIST_HEAD(, AddressSpaceMapClient) map_client_list;
include/hw/pci/pci_device.h +3 -0
@@ -160,6 +160,9 @@ struct PCIDevice {/* ID of standby device in net_failover pair */ char *failover_pair_id;uint32_t acpi_index; + +/* Maximum DMA bounce buffer size used for indirect memory map requests */ +uint64_t max_bounce_buffer_size; };static inline int pci_intx (PCIDevice *pci_dev)
softmmu/memory.c +3 -2
@@ -3105,7 +3105,8 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) as->ioeventfds =NULL ;QTAILQ_INIT (&as->listeners);QTAILQ_INSERT_TAIL (&address_spaces, as, address_spaces_link); - as->bounce.in_use = false; + as->max_bounce_buffer_size =DEFAULT_MAX_BOUNCE_BUFFER_SIZE ; + as->bounce_buffer_size =0 ;qemu_mutex_init (&as->map_client_list_lock);QLIST_INIT (&as->map_client_list); as->name =g_strdup (name ? name :"anonymous" );
@@ -3115,7 +3116,7 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)static void do_address_space_destroy (AddressSpace *as) { -assert (!qatomic_read (&as->bounce.in_use)); +assert (qatomic_read (&as->bounce_buffer_size) ==0 );assert (QLIST_EMPTY (&as->map_client_list));qemu_mutex_destroy (&as->map_client_list_lock);
softmmu/physmem.c +55 -25
125 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 2/5] softmmu: Support concurrent bounce buffers
Quoted 20 lines · Mattias Nissler
When DMA memory can't be directly accessed, as is the case when running the device model in a separate process without shareable DMA file descriptors, bounce buffering is used.
It is not uncommon for device models to request mapping of several DMA regions at the same time. Examples include:
* net devices, e.g. when transmitting a packet that is split across several TX descriptors (observed with igb)
* USB host controllers, when handling a packet with multiple data TRBs (observed with xhci)Previously, qemu only provided a single bounce buffer per AddressSpace and would fail DMA map requests while the buffer was already in use. In turn, this would cause DMA failures that ultimately manifest as hardware errors from the guest perspective.
This change allocates DMA bounce buffers dynamically instead of supporting only a single buffer. Thus, multiple DMA mappings work correctly also when RAM can't be mmap()-ed.
Quoted 6 lines
The total bounce buffer allocation size is limited individually for each AddressSpace. The default limit is 4096 bytes, matching the previous maximum buffer size. A new x-max-bounce-buffer-size parameter is provided to configure the limit for PCI devices.
Signed-off-by Mattias Nissler <[email protected]>
Signature · 1 line
Peter Xu
PATCH v5 3/5 Update subprojects/libvfio-user
In reply to Mattias NisslerBrings in assorted bug fixes. The following are of particular interest with respect to message-based DMA support:
* bb308a2 "Fix address calculation for message-based DMA" Corrects a bug in DMA address calculation.
* 1569a37 "Pass server->client command over a separate socket pair" Adds support for separate sockets for either command direction, addressing a bug where libvfio-user gets confused if both client and server send commands concurrently.
--- subprojects/libvfio-user.wrap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
subprojects/libvfio-user.wrap +1 -1
@@ -1,4 +1,4 @@ [wrap-git] url = https://gitlab.com/qemu-project/libvfio-user.git -revision = 0b28d205572c80b568a1003db2c8f37ca333e4d7 +revision = 1569a37a54ecb63bd4008708c76339ccf7d06115 depth = 1
Signature · 1 line
2.34.1
PATCH v5 4/5 vfio-user: Message-based DMA support
In reply to Mattias NisslerWire up support for DMA for the case where the vfio-user client does not provide mmap()-able file descriptors, but DMA requests must be performed via the VFIO-user protocol. This installs an indirect memory region, which already works for pci_dma_{read,write}, and pci_dma_map works thanks to the existing DMA bounce buffering support.
Note that while simple scenarios work with this patch, there's a known race condition in libvfio-user that will mess up the communication channel. See https://github.com/nutanix/libvfio-user/issues/279 for details as well as a proposed fix.
--- hw/remote/trace-events | 2 + hw/remote/vfio-user-obj.c | 84 +++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 7 deletions(-)
hw/remote/trace-events +2 -0
@@ -9,6 +9,8 @@ vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg: 0x%x -> 0x%x" vfu_cfg_write(uint32_t offset, uint32_t val) "vfu: cfg: 0x%x <- 0x%x" vfu_dma_register(uint64_t gpa, size_t len) "vfu: registering GPA 0x%"PRIx64", %zu bytes" vfu_dma_unregister(uint64_t gpa) "vfu: unregistering GPA 0x%"PRIx64"" +vfu_dma_read(uint64_t gpa, size_t len) "vfu: DMA read 0x%"PRIx64", %zu bytes" +vfu_dma_write(uint64_t gpa, size_t len) "vfu: DMA write 0x%"PRIx64", %zu bytes" vfu_bar_register(int i, uint64_t addr, uint64_t size) "vfu: BAR %d: addr 0x%"PRIx64" size 0x%"PRIx64"" vfu_bar_rw_enter(const char *op, uint64_t addr) "vfu: %s request for BAR address 0x%"PRIx64"" vfu_bar_rw_exit(const char *op, uint64_t addr) "vfu: Finished %s of BAR address 0x%"PRIx64""
hw/remote/vfio-user-obj.c +77 -7
100 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v5 4/5] vfio-user: Message-based DMA support
Quoted 126 lines · Mattias Nissler
Wire up support for DMA for the case where the vfio-user client does not provide mmap()-able file descriptors, but DMA requests must be performed via the VFIO-user protocol. This installs an indirect memory region, which already works for pci_dma_{read,write}, and
pci_dma_mapworks thanks to the existing DMA bounce buffering support.Note that while simple scenarios work with this patch, there's a known race condition in libvfio-user that will mess up the communication channel. See https://github.com/nutanix/libvfio-user/issues/279 for details as well as a proposed fix.
Signed-off-by Mattias Nissler <[email protected]>---
hw/remote/trace-events | 2 +hw/remote/vfio-user-obj.c| 84 +++++++++++++++++++++++++++++++++++----2 files changed, 79 insertions(+), 7 deletions(-)hw/remote/trace-events +0
-0@@ -9,6 +9,8 @@ vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg: 0x%x -> 0x%x"
vfu_cfg_write(uint32_t offset, uint32_t val)"vfu: cfg: 0x%x <- 0x%x"vfu_dma_register(uint64_t gpa, size_t len)"vfu: registering GPA 0x%"PRIx64", %zu bytes"vfu_dma_unregister(uint64_t gpa)"vfu: unregistering GPA 0x%"PRIx64""hw/remote/trace-events +2
-0+vfu_dma_read(uint64_t gpa, size_t len) "vfu: DMA read 0x%"PRIx64", %zu bytes" +vfu_dma_write(uint64_t gpa, size_t len) "vfu: DMA write 0x%"PRIx64", %zu bytes"
vfu_bar_register(int i, uint64_t addr, uint64_t size)"vfu: BAR %d: addr 0x%"PRIx64" size 0x%"PRIx64""vfu_bar_rw_enter(const char *op, uint64_t addr)"vfu: %s request for BAR address 0x%"PRIx64""vfu_bar_rw_exit(const char *op, uint64_t addr)"vfu: Finished %s of BAR address 0x%"PRIx64""hw/remote/vfio-user-obj.c +66
-7@@ -300,6 +300,63 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,return count; } +static MemTxResult vfu_dma_read (void *opaque,hwaddr addr,uint64_t *val, +unsigned size,MemTxAttrs attrs) +{ +MemoryRegion *region = opaque; +vfu_ctx_t *vfu_ctx =VFU_OBJECT (region->owner)->vfu_ctx; +uint8_t buf[sizeof (uint64_t )]; + +trace_vfu_dma_read (region->addr + addr, size); + + g_autofree dma_sg_t *sg = g_malloc0(dma_sg_size()); +vfu_dma_addr_t vfu_addr = (vfu_dma_addr_t)(region->addr + addr); +if (vfu_addr_to_sgl (vfu_ctx, vfu_addr, size, sg,1 ,PROT_READ ) <0 || +vfu_sgl_read (vfu_ctx, sg,1 , buf) !=0 ) { +return MEMTX_ERROR ; + } + + *val =ldn_he_p (buf, size); + +return MEMTX_OK ; +} + +static MemTxResult vfu_dma_write (void *opaque,hwaddr addr,uint64_t val, +unsigned size,MemTxAttrs attrs) +{ +MemoryRegion *region = opaque; +vfu_ctx_t *vfu_ctx =VFU_OBJECT (region->owner)->vfu_ctx; +uint8_t buf[sizeof (uint64_t )]; + +trace_vfu_dma_write (region->addr + addr, size); + +stn_he_p (buf, size, val); + + g_autofree dma_sg_t *sg = g_malloc0(dma_sg_size()); +vfu_dma_addr_t vfu_addr = (vfu_dma_addr_t)(region->addr + addr); +if (vfu_addr_to_sgl (vfu_ctx, vfu_addr, size, sg,1 ,PROT_WRITE ) <0 || +vfu_sgl_write (vfu_ctx, sg,1 , buf) !=0 ) { +return MEMTX_ERROR ; + } + +return MEMTX_OK ; +} + +static const MemoryRegionOps vfu_dma_ops = { + .read_with_attrs = vfu_dma_read, + .write_with_attrs = vfu_dma_write, + .endianness =DEVICE_HOST_ENDIAN , + .valid = { + .min_access_size =1 , + .max_access_size =8 , + .unaligned = true, + }, + .impl = { + .min_access_size =1 , + .max_access_size =8 , + }, +}; +static void dma_register (vfu_ctx_t *vfu_ctx,vfu_dma_info_t *info) {VfuObject *o =vfu_get_private (vfu_ctx);@@ -308,17 +365,30 @@ static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) g_autofree char *name = NULL;struct iovec *iov = &info->iova; -if (!info->vaddr) { -return ; - } - name =g_strdup_printf ("mem-%s-%" PRIx64"" , o->device, - (uint64_t )info->vaddr); + (uint64_t )iov->iov_base); subregion =g_new0 (MemoryRegion,1 ); -memory_region_init_ram_ptr (subregion,NULL , name, - iov->iov_len, info->vaddr); +if (info->vaddr) { +memory_region_init_ram_ptr (subregion,OBJECT (o), name, + iov->iov_len, info->vaddr); + }else { +/* +* Note that I/O regions' MemoryRegionOps handle accesses of at most 8 +* bytes at a time, and larger accesses are broken down. However, +* many/most DMA accesses are larger than 8 bytes and VFIO-user can
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) · line 390
Quoted 10 lines
hw/remote/vfio-user-obj.c +10
-0+* handle large DMA accesses just fine, thus this size restriction +* unnecessarily hurts performance, in particular given that each +* access causes a round trip on the VFIO-user socket. +* +* TODO: Investigate how to plumb larger accesses through memory +* regions, possibly by amending MemoryRegionOps or by creating a new +* memory region type. +*/ +memory_region_init_io (subregion,OBJECT (o), &vfu_dma_ops, subregion, + name, iov->iov_len);
Hi Mattias,
We should update dma_unregister() to ensure we remove this subregion. dma_unregister() presently removes the RAM region, but not this one.
--
Jag
Quoted 7 lines
hw/remote/vfio-user-obj.c +1
-0+ } dma_as =pci_device_iommu_address_space (o->pci_dev);Signature · 1 line
2.34.1
Re: [PATCH v5 4/5] vfio-user: Message-based DMA support
Quoted 155 lines · Jag Raman
Quoted 3 lines
On Sep 20, 2023, at 4:06 AM, Mattias Nissler <[email protected]>
hw/remote/trace-events vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg:
Quoted 23 lines · wrote:
Wire up support for DMA for the case where the vfio-user client does not provide mmap()-able file descriptors, but DMA requests must be performed via the VFIO-user protocol. This installs an indirect memory region, which already works for pci_dma_{read,write}, and
pci_dma_mapworks thanks to the existing DMA bounce buffering support.Note that while simple scenarios work with this patch, there's a known race condition in libvfio-user that will mess up the communication channel. See https://github.com/nutanix/libvfio-user/issues/279 for details as well as a proposed fix.
Signed-off-by Mattias Nissler <[email protected]>---
hw/remote/trace-events | 2 +hw/remote/vfio-user-obj.c| 84 +++++++++++++++++++++++++++++++++++----2 files changed, 79 insertions(+), 7 deletions(-)hw/remote/trace-events +0
-0@@ -9,6 +9,8 @@ vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg:0x%x -> 0x%x"
Quoted 2 lines
vfu_cfg_write(uint32_t offset, uint32_t val)"vfu: cfg: 0x%x <- 0x%x"vfu_dma_register(uint64_t gpa, size_t len)"vfu: registering GPA0x%"PRIx64", %zu bytes"
Quoted 2 lines
vfu_dma_unregister(uint64_t gpa)"vfu: unregistering GPA 0x%"PRIx64"" +vfu_dma_read(uint64_t gpa,size_tlen) "vfu: DMA read 0x%"PRIx64", %zubytes"
Quoted 1 line
+vfu_dma_write(uint64_t gpa,
size_tlen) "vfu: DMA write 0x%"PRIx64",%zu bytes"
Quoted 1 line
vfu_bar_register(int i, uint64_t addr, uint64_t size)"vfu: BAR %d: addr0x%"PRIx64" size 0x%"PRIx64""
Quoted 1 line
vfu_bar_rw_enter(const char *op, uint64_t addr)"vfu: %s request for BARaddress 0x%"PRIx64""
Quoted 1 line
vfu_bar_rw_exit(const char *op, uint64_t addr)"vfu: Finished %s of BARaddress 0x%"PRIx64""
hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t
Quoted 5 lines
hw/remote/vfio-user-obj.c +0
-0@@ -300,6 +300,63 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t
*vfu_ctx, char * const buf,hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,
Quoted 4 lines
hw/remote/vfio-user-obj.c +1
-0return count; } +static MemTxResult vfu_dma_read(void *opaque, hwaddr addr, uint64_t*val,
hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,
Quoted 11 lines
hw/remote/vfio-user-obj.c +11
-0+ unsigned size, MemTxAttrs attrs) +{ +MemoryRegion *region = opaque; +vfu_ctx_t *vfu_ctx =VFU_OBJECT (region->owner)->vfu_ctx; +uint8_t buf[sizeof (uint64_t )]; + +trace_vfu_dma_read (region->addr + addr, size); + + g_autofree dma_sg_t *sg = g_malloc0(dma_sg_size()); +vfu_dma_addr_t vfu_addr = (vfu_dma_addr_t)(region->addr + addr); + if (vfu_addr_to_sgl(vfu_ctx, vfu_addr, size, sg, 1, PROT_READ) < 0|| > + vfu_sgl_read(vfu_ctx, sg, 1, buf) != 0) { > + return MEMTX_ERROR; > + }hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,
Quoted 7 lines
hw/remote/vfio-user-obj.c +7
-0+ + *val =ldn_he_p (buf, size); + +return MEMTX_OK ; +} + +static MemTxResult vfu_dma_write(void *opaque, hwaddr addr, uint64_tval,
hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,
Quoted 13 lines
hw/remote/vfio-user-obj.c +13
-0+ unsigned size, MemTxAttrs attrs) +{ +MemoryRegion *region = opaque; +vfu_ctx_t *vfu_ctx =VFU_OBJECT (region->owner)->vfu_ctx; +uint8_t buf[sizeof (uint64_t )]; + +trace_vfu_dma_write (region->addr + addr, size); + +stn_he_p (buf, size, val); + + g_autofree dma_sg_t *sg = g_malloc0(dma_sg_size()); +vfu_dma_addr_t vfu_addr = (vfu_dma_addr_t)(region->addr + addr); + if (vfu_addr_to_sgl(vfu_ctx, vfu_addr, size, sg, 1, PROT_WRITE) < 0|| > + vfu_sgl_write(vfu_ctx, sg, 1, buf) != 0) { > + return MEMTX_ERROR; > + }hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx,
Quoted 23 lines
hw/remote/vfio-user-obj.c +19
-0+ +return MEMTX_OK ; +} + +static const MemoryRegionOps vfu_dma_ops = { + .read_with_attrs = vfu_dma_read, + .write_with_attrs = vfu_dma_write, + .endianness =DEVICE_HOST_ENDIAN , + .valid = { + .min_access_size =1 , + .max_access_size =8 , + .unaligned = true, + }, + .impl = { + .min_access_size =1 , + .max_access_size =8 , + }, +}; +static void dma_register (vfu_ctx_t *vfu_ctx,vfu_dma_info_t *info) {VfuObject *o =vfu_get_private (vfu_ctx);@@ -308,17 +365,30 @@ static void dma_register(vfu_ctx_t *vfu_ctx,
vfu_dma_info_t*info)Quoted 11 lines
hw/remote/vfio-user-obj.c +1
-5g_autofree char *name = NULL;struct iovec *iov = &info->iova; -if (!info->vaddr) { -return ; - } - name =g_strdup_printf ("mem-%s-%" PRIx64"" , o->device, - (uint64_t )info->vaddr); + (uint64_t )iov->iov_base);hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info)
Quoted 10 lines
hw/remote/vfio-user-obj.c +6
-2subregion =g_new0 (MemoryRegion,1 ); -memory_region_init_ram_ptr (subregion,NULL , name, - iov->iov_len, info->vaddr); +if (info->vaddr) { +memory_region_init_ram_ptr (subregion,OBJECT (o), name, + iov->iov_len, info->vaddr); + }else { +/* +* Note that I/O regions' MemoryRegionOps handle accesses of at most 8
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, · line 379
Quoted 1 line
hw/remote/vfio-user-obj.c +1
-0+* bytes at a time, and larger accesses are broken down. However,
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, · line 380
Quoted 1 line
hw/remote/vfio-user-obj.c +1
-0+* many/most DMA accesses are larger than 8 bytes and VFIO-user can
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, · line 381
Quoted 1 line
hw/remote/vfio-user-obj.c +1
-0+* handle large DMA accesses just fine, thus this size restriction
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, · line 382
Quoted 1 line
hw/remote/vfio-user-obj.c +1
-0+* unnecessarily hurts performance, in particular given that each
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info)
Quoted 4 lines
hw/remote/vfio-user-obj.c +4
-0+* access causes a round trip on the VFIO-user socket. +* +* TODO: Investigate how to plumb larger accesses through memory +* regions, possibly by amending MemoryRegionOps or by creating a new
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info)
Quoted 3 lines
hw/remote/vfio-user-obj.c +3
-0+* memory region type. +*/ + memory_region_init_io(subregion, OBJECT(o), &vfu_dma_ops,
Quoted 8 lines
subregion,
hw/remote/vfio-user-obj.c static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) · line 390
Quoted 1 line
hw/remote/vfio-user-obj.c +1
-0+ name, iov->iov_len);Hi Mattias,
We should update
dma_unregister()to ensure we remove this subregion.dma_unregister()presently removes the RAM region, but not this one.
Oops. Fixing.
Quoted 13 lines
--
JagQuoted 7 lines
hw/remote/vfio-user-obj.c +1
-1+ } dma_as =pci_device_iommu_address_space (o->pci_dev); --2.34.1
PATCH v5 5/5 vfio-user: Fix config space access byte order
In reply to Mattias NisslerPCI config space is little-endian, so on a big-endian host we need to perform byte swaps for values as they are passed to and received from the generic PCI config space access machinery.
--- hw/remote/vfio-user-obj.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/remote/vfio-user-obj.c +2 -2
@@ -281,7 +281,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,while (bytes >0 ) { len = (bytes > pci_access_width) ? pci_access_width : bytes;if (is_write) { -memcpy (&val, ptr, len); + val =ldn_le_p (ptr, len);pci_host_config_write_common (o->pci_dev, offset,pci_config_size (o->pci_dev), val, len);
@@ -289,7 +289,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf, }else { val =pci_host_config_read_common (o->pci_dev, offset,pci_config_size (o->pci_dev), len); -memcpy (ptr, &val, len); +stn_le_p (ptr, len, val);trace_vfu_cfg_read (offset, val); } offset += len;
Signature · 1 line
2.34.1
Re: [PATCH v5 5/5] vfio-user: Fix config space access byte order
Quoted 38 lines · Mattias Nissler
PCI config space is little-endian, so on a big-endian host we need to perform byte swaps for values as they are passed to and received from the generic PCI config space access machinery.
Signed-off-by Mattias Nissler <[email protected]>---
hw/remote/vfio-user-obj.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)hw/remote/vfio-user-obj.c +2
-2@@ -281,7 +281,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,while (bytes >0 ) { len = (bytes > pci_access_width) ? pci_access_width : bytes;if (is_write) { -memcpy (&val, ptr, len); + val =ldn_le_p (ptr, len);pci_host_config_write_common (o->pci_dev, offset,pci_config_size (o->pci_dev), val, len);@@ -289,7 +289,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf, }else { val =pci_host_config_read_common (o->pci_dev, offset,pci_config_size (o->pci_dev), len); -memcpy (ptr, &val, len); +stn_le_p (ptr, len, val);trace_vfu_cfg_read (offset, val); } offset += len;Signature · 1 line
2.34.1
Hey,
When you tested on s390x, could you see the correct values for the config space in the Kernel? For example, were any known device's vendor and device IDs valid?
I'm asking because flatview_read_continue() / flatview_write_continue() does endianness adjustment. So, I want to confirm that the endianness adjustment in your code also makes sense from Kernel's perspective.
I'm trying to access a big-endian system, but no luck.
#0 0x0000555555b97a30 in vfio_user_io_region_read (vbasedev=0x555557802c80, index=7 '\a', off=4, size=2, data=0x7fff6cfb945c) at ../hw/vfio/user.c:1985 #1 0x0000555555b8dcfb in vfio_pci_read_config (pdev=0x555557802250, addr=4, len=2) at ../hw/vfio/pci.c:1202 #2 0x000055555599d3f9 in pci_host_config_read_common (pci_dev=0x555557802250, addr=addr@entry=4, limit=<optimized out>, limit@entry=256, len=len@entry=2) at ../hw/pci/pci_host.c:107 #3 0x000055555599d74a in pci_data_read (s=<optimized out>, addr=2147493892, len=2) at ../hw/pci/pci_host.c:143 #4 0x000055555599d84f in pci_host_data_read (opaque=<optimized out>, addr=<optimized out>, len=<optimized out>) at ../hw/pci/pci_host.c:188 #5 0x0000555555bc3c4d in memory_region_read_accessor (mr=mr@entry=0x5555569de370, addr=0, value=value@entry=0x7fff6cfb9710, size=size@entry=2, shift=0, mask=mask@entry=65535, attrs=...) at ../softmmu/memory.c:441 #6 0x0000555555bc3fce in access_with_adjusted_size (addr=addr@entry=0, value=value@entry=0x7fff6cfb9710, size=size@entry=2, access_size_min=<optimized out>, access_size_max=<optimized out>, access_fn=0x555555bc3c10 <memory_region_read_accessor>, mr=0x5555569de370, attrs=...) at ../softmmu/memory.c:569 #7 0x0000555555bc41a1 in memory_region_dispatch_read1 (attrs=..., size=2, pval=0x7fff6cfb9710, addr=<optimized out>, mr=<optimized out>) at ../softmmu/memory.c:1443 #8 0x0000555555bc41a1 in memory_region_dispatch_read (mr=mr@entry=0x5555569de370, addr=<optimized out>, pval=pval@entry=0x7fff6cfb9710, op=MO_16, attrs=attrs@entry=...) at ../softmmu/memory.c:1476 #9 0x0000555555bce13b in flatview_read_continue (fv=fv@entry=0x7fff6861e120, addr=addr@entry=3324, attrs=..., ptr=ptr@entry=0x7ffff7fdf000, len=len@entry=2, addr1=<optimized out>, l=<optimized out>, mr=0x5555569de370) at /home/github/oracle/qemu/include/qemu/host-utils.h:219
#10 0x0000555555bce2f7 in flatview_read (fv=0x7fff6861e120, addr=addr@entry=3324, attrs=attrs@entry=..., buf=buf@entry=0x7ffff7fdf000, len=len@entry=2) at ../softmmu/physmem.c:2762
#11 0x0000555555bce448 in address_space_read_full (as=0x555556671620 <address_space_io>, addr=3324, attrs=..., buf=0x7ffff7fdf000, len=2) at ../softmmu/physmem.c:2775
#12 0x0000555555bce595 in address_space_rw (as=as@entry=0x555556671620 <address_space_io>, addr=addr@entry=3324, attrs=..., attrs@entry=..., buf=<optimized out>, len=len@entry=2, is_write=is_write@entry=false) at ../softmmu/physmem.c:2803 #13 0x0000555555c1973f in kvm_handle_io (count=1, size=2, direction=<optimized out>, data=<optimized out>, attrs=..., port=3324) at ../accel/kvm/kvm-all.c:2778
#14 0x0000555555c1973f in kvm_cpu_exec (cpu=cpu@entry=0x5555569ab390) at ../accel/kvm/kvm-all.c:3029
#15 0x0000555555c1a8dd in kvm_vcpu_thread_fn (arg=arg@entry=0x5555569ab390) at ../accel/kvm/kvm-accel-ops.c:51
#16 0x0000555555d8f4fb in qemu_thread_start (args=<optimized out>) at ../util/qemu-thread-posix.c:541
#17 0x00007ffff577dea5 in start_thread () at /lib64/libpthread.so.0
#18 0x00007ffff54a6b0d in clone () at /lib64/libc.so.6
FYI, the above is the stack trace from the client. vfio_user_io_region_read() in the above sends a message to the server, and the server ends up calling either vfu_object_cfg_access() or vfu_object_bar_rw() (which also does the endianness correction) depending on the region.
--
Jag
Re: [PATCH v5 5/5] vfio-user: Fix config space access byte order
Quoted 43 lines · Jag Raman
Quoted 3 lines
On Sep 20, 2023, at 4:06 AM, Mattias Nissler <[email protected]>
hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t
Quoted 15 lines · wrote:
PCI config space is little-endian, so on a big-endian host we need to perform byte swaps for values as they are passed to and received from the generic PCI config space access machinery.
Signed-off-by Mattias Nissler <[email protected]>---
hw/remote/vfio-user-obj.c| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)hw/remote/vfio-user-obj.c +0
-0@@ -281,7 +281,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t
*vfu_ctx, char * const buf,hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t
Quoted 9 lines
hw/remote/vfio-user-obj.c +1
-1while (bytes >0 ) { len = (bytes > pci_access_width) ? pci_access_width : bytes;if (is_write) { -memcpy (&val, ptr, len); + val =ldn_le_p (ptr, len);pci_host_config_write_common (o->pci_dev, offset,pci_config_size (o->pci_dev), val, len);@@ -289,7 +289,7 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t
*vfu_ctx, char * const buf,hw/remote/vfio-user-obj.c static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf,
Quoted 3 lines
hw/remote/vfio-user-obj.c +0
-0}else { val = pci_host_config_read_common(o->pci_dev, offset,
pci_config_size(o->pci_dev), len);Quoted 8 lines
hw/remote/vfio-user-obj.c +1
-2-memcpy (ptr, &val, len); +stn_le_p (ptr, len, val);trace_vfu_cfg_read (offset, val); } offset += len; --2.34.1
Quoted 6 lines
Hey,
When you tested on s390x, could you see the correct values for the config space in the Kernel? For example, were any known device's vendor and device IDs valid?
I don't exactly remember whether I checked vendor and device IDs, but I've done something more comprehensive: I set up a qemu vfio-user server exposing an nvme device and then connected a qemu client (with the vfio-user client patches from the oracle qemu github repo). Linux running in the client probes the nvme device successfully and I've mounted a file system on it. Both qemu binaries are s390x.
Quoted 5 lines
I'm asking because
flatview_read_continue()/flatview_write_continue()does endianness adjustment. So, I want to confirm that the endianness adjustment in your code also makes sense from Kernel's perspective.
The conversion in the flatview access path is adjusting from the endianness of the memory region to what the emulated CPU needs. Since the PCI config space memory region is little-endian (see pci_host_data_le_ops), we're doing a swap there. The code I'm changing is backing the memory region, so incoming/outgoing data for writes/reads must be in little-endian to adhere to the endianness declared by the memory region.
Quoted 3 lines
I'm trying to access a big-endian system, but no luck.
Btw. I don't have access to big-endian hardware either, but it was surprisingly straightforward to make my x86 ubuntu machine run s390x binaries via multiarch + qemu user mode (qemu turtles all the way down :-D)
Quoted 63 lines
#0
0x0000555555b97a30invfio_user_io_region_read(vbasedev=0x555557802c80, index=7 '\a', off=4, size=2, data=0x7fff6cfb945c) at../hw/vfio/user.c:1985
#10x0000555555b8dcfbinvfio_pci_read_config(pdev=0x555557802250, addr=4, len=2) at../hw/vfio/pci.c:1202
#20x000055555599d3f9inpci_host_config_read_common(pci_dev=0x555557802250, addr=addr@entry=4, limit=<optimized out>, limit@entry=256, len=len@entry=2) at../hw/pci/pci_host.c:107
#30x000055555599d74ainpci_data_read(s=<optimized out>, addr=2147493892, len=2) at../hw/pci/pci_host.c:143
#40x000055555599d84finpci_host_data_read(opaque=<optimized out>, addr=<optimized out>, len=<optimized out>) at../hw/pci/pci_host.c:188#50x0000555555bc3c4dinmemory_region_read_accessor(mr=mr@entry=0x5555569de370, addr=0, value=value@entry=0x7fff6cfb9710, size=size@entry=2, shift=0, mask=mask@entry=65535, attrs=...) at../softmmu/memory.c:441
#60x0000555555bc3fceinaccess_with_adjusted_size(addr=addr@entry=0, value=value@entry=0x7fff6cfb9710, size=size@entry=2, access_size_min=<optimized out>, access_size_max=<optimized out>, access_fn=0x555555bc3c10 <memory_region_read_accessor>, mr=0x5555569de370, attrs=...) at../softmmu/memory.c:569
#70x0000555555bc41a1inmemory_region_dispatch_read1(attrs=..., size=2, pval=0x7fff6cfb9710, addr=<optimized out>, mr=<optimized out>) at../softmmu/memory.c:1443
#80x0000555555bc41a1inmemory_region_dispatch_read(mr=mr@entry=0x5555569de370, addr=<optimized out>, pval=pval@entry=0x7fff6cfb9710, op=MO_16, attrs=attrs@entry=...) at../softmmu/memory.c:1476
#90x0000555555bce13binflatview_read_continue(fv=fv@entry=0x7fff6861e120, addr=addr@entry=3324, attrs=..., ptr=ptr@entry=0x7ffff7fdf000, len=len@entry=2, addr1=<optimized out>, l=<optimized out>, mr=0x5555569de370) at/home/github/oracle/qemu/include/qemu/host-utils.h:219#100x0000555555bce2f7inflatview_read(fv=0x7fff6861e120, addr=addr@entry=3324, attrs=attrs@entry=..., buf=buf@entry=0x7ffff7fdf000, len=len@entry=2) at../softmmu/physmem.c:2762
#110x0000555555bce448inaddress_space_read_full(as=0x555556671620 <address_space_io>, addr=3324, attrs=..., buf=0x7ffff7fdf000, len=2) at../softmmu/physmem.c:2775
#120x0000555555bce595inaddress_space_rw(as=as@entry=0x555556671620 <address_space_io>, addr=addr@entry=3324, attrs=..., attrs@entry=..., buf=<optimized out>, len=len@entry=2, is_write=is_write@entry=false) at../softmmu/physmem.c:2803
#130x0000555555c1973finkvm_handle_io(count=1, size=2, direction=<optimized out>, data=<optimized out>, attrs=..., port=3324) at../accel/kvm/kvm-all.c:2778
#140x0000555555c1973finkvm_cpu_exec(cpu=cpu@entry=0x5555569ab390) at../accel/kvm/kvm-all.c:3029
#150x0000555555c1a8ddinkvm_vcpu_thread_fn(arg=arg@entry=0x5555569ab390) at../accel/kvm/kvm-accel-ops.c:51
#160x0000555555d8f4fbinqemu_thread_start(args=<optimized out>) at../util/qemu-thread-posix.c:541
#170x00007ffff577dea5instart_thread() at/lib64/libpthread.so.0#180x00007ffff54a6b0din clone () at/lib64/libc.so.6FYI, the above is the stack trace from the client.
vfio_user_io_region_read()in the above sends a message to the server, and the server ends up calling eithervfu_object_cfg_access()orvfu_object_bar_rw()(which also does the endianness correction) depending on the region.--
Jag