On Thu, Nov 15, 2012 at 9:09 PM, Anthony Liguori <aligu...@us.ibm.com> wrote: > Stefan Hajnoczi <stefa...@redhat.com> writes: > >> The virtio-blk-data-plane cannot access memory using the usual QEMU >> functions since it executes outside the global mutex and the memory APIs >> are this time are not thread-safe. >> >> This patch introduces a virtqueue module based on the kernel's vhost >> vring code. The trick is that we map guest memory ahead of time and >> access it cheaply outside the global mutex. >> >> Once the hardware emulation code can execute outside the global mutex it >> will be possible to drop this code. >> >> Signed-off-by: Stefan Hajnoczi <stefa...@redhat.com> >> --- >> hw/Makefile.objs | 2 +- >> hw/dataplane/Makefile.objs | 3 + >> hw/dataplane/vring.c | 321 >> +++++++++++++++++++++++++++++++++++++++++++++ >> hw/dataplane/vring.h | 54 ++++++++ >> trace-events | 3 + >> 5 files changed, 382 insertions(+), 1 deletion(-) >> create mode 100644 hw/dataplane/Makefile.objs >> create mode 100644 hw/dataplane/vring.c >> create mode 100644 hw/dataplane/vring.h >> >> diff --git a/hw/Makefile.objs b/hw/Makefile.objs >> index af4ab0c..da8ef0c 100644 >> --- a/hw/Makefile.objs >> +++ b/hw/Makefile.objs >> @@ -1,4 +1,4 @@ >> -common-obj-y = usb/ ide/ >> +common-obj-y = usb/ ide/ dataplane/ >> common-obj-y += loader.o >> common-obj-$(CONFIG_VIRTIO) += virtio-console.o >> common-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o >> diff --git a/hw/dataplane/Makefile.objs b/hw/dataplane/Makefile.objs >> new file mode 100644 >> index 0000000..b58544f >> --- /dev/null >> +++ b/hw/dataplane/Makefile.objs >> @@ -0,0 +1,3 @@ >> +ifeq ($(CONFIG_VIRTIO), y) >> +common-obj-$(CONFIG_VIRTIO_BLK_DATA_PLANE) += vring.o >> +endif >> diff --git a/hw/dataplane/vring.c b/hw/dataplane/vring.c >> new file mode 100644 >> index 0000000..6aacce8 >> --- /dev/null >> +++ b/hw/dataplane/vring.c >> @@ -0,0 +1,321 @@ >> +/* Copyright 2012 Red Hat, Inc. >> + * Copyright IBM, Corp. 2012 >> + * >> + * Based on Linux vhost code: >> + * Copyright (C) 2009 Red Hat, Inc. >> + * Copyright (C) 2006 Rusty Russell IBM Corporation >> + * >> + * Author: Michael S. Tsirkin <m...@redhat.com> >> + * Stefan Hajnoczi <stefa...@redhat.com> >> + * >> + * Inspiration, some code, and most witty comments come from >> + * Documentation/virtual/lguest/lguest.c, by Rusty Russell >> + * >> + * This work is licensed under the terms of the GNU GPL, version 2. >> + */ >> + >> +#include "trace.h" >> +#include "hw/dataplane/vring.h" >> + >> +/* Map target physical address to host address >> + */ >> +static inline void *phys_to_host(Vring *vring, hwaddr phys) >> +{ >> + /* Adjust for 3.6-4 GB PCI memory range */ >> + if (phys >= 0x100000000) { >> + phys -= 0x100000000 - 0xe0000000; >> + } else if (phys >= 0xe0000000) { >> + fprintf(stderr, "phys_to_host bad physical address in " >> + "PCI range %#lx\n", phys); >> + exit(1); >> + } >> + return vring->phys_mem_zero_host_ptr + phys; >> +} > > This is too dirty to merge. This code should use a slot mechanism > similar to what vhost uses. Then it has a plausible chance of working > on other architectures. [...] > You should be able to use a MemoryListener to keep the memory slot table > up to date. You'll need to use a mutex to protect it but again since > it's mostly uncontended, that shouldn't be a problem.
Anthony, Paolo, You're right, we should use MemoryListener. Will fix for v2. Stefan