Skip to contentSkip to conversation

Initial support for multi-process qemu

PATCH Superseded [email protected] 4 participants 51 messages Jun 27, 2020 – Jul 31, 2020 Arm STM32

v7 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#20 lore raw

PATCH v7 10/21 multi-process: setup memory manager for remote device

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

SyncSysMemMsg message format is defined. It is used to send file descriptors of the RAM regions to remote device.
RAM on the remote device is configured with a set of file descriptors. Old RAM regions are deleted and new regions, each with an fd, is added to the RAM.

---
 MAINTAINERS                     |  2 ++
 hw/i386/Makefile.objs           |  1 +
 hw/i386/remote-memory.c         | 58 +++++++++++++++++++++++++++++++++
 include/hw/i386/remote-memory.h | 20 ++++++++++++
 include/io/mpqemu-link.h        | 13 ++++++++
 io/mpqemu-link.c                | 13 ++++++++
 6 files changed, 107 insertions(+)
 create mode 100644 hw/i386/remote-memory.c
 create mode 100644 include/hw/i386/remote-memory.h
MAINTAINERS +2 -0
index e204b3e0c6..017c96eace 100644
@@ -2950,6 +2950,8 @@ F: include/hw/i386/remote.h
 F: io/mpqemu-link.c
 F: include/io/mpqemu-link.h
 F: hw/i386/remote-msg.c
+F: include/hw/i386/remote-memory.h
+F: hw/i386/remote-memory.c
 
 Build and test automation
 -------------------------
hw/i386/Makefile.objs +1 -0
index 83969585c1..d85537713e 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_VMMOUSE) += vmmouse.o
 obj-$(CONFIG_PC) += port92.o
 obj-$(CONFIG_MPQEMU) += remote.o
 obj-$(CONFIG_MPQEMU) += remote-msg.o
+obj-$(CONFIG_MPQEMU) += remote-memory.o
 
 obj-y += kvmvapic.o
 obj-$(CONFIG_ACPI) += acpi-common.o
hw/i386/remote-memory.c new file +58 -0

58 lines not on this page · raw message

include/hw/i386/remote-memory.h new file +20 -0
new file mode 100644
index 0000000000..e2e479bb6f
@@ -0,0 +1,20 @@
+/*
+ * Memory manager for remote device
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef REMOTE_MEMORY_H
+#define REMOTE_MEMORY_H
+
+#include "qemu/osdep.h"
+#include "exec/hwaddr.h"
+#include "io/mpqemu-link.h"
+
+void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp);
+
+#endif
include/io/mpqemu-link.h +13 -0
index 52aa89656c..c6d2b6bf8b 100644
@@ -18,6 +18,8 @@
 #include "qemu/thread.h"
 #include "io/channel.h"
 #include "io/channel-socket.h"
+#include "exec/cpu-common.h"
+#include "exec/hwaddr.h"
 
 #define REMOTE_MAX_FDS 8
 
@@ -28,13 +30,22 @@
  *
  * MPQemuCmd enum type to specify the command to be executed on the remote
  * device.
+ *
+ * SYNC_SYSMEM      Shares QEMU's RAM with remote device's RAM
  */
 typedef enum {
     INIT = 0,
+    SYNC_SYSMEM,
     RET_MSG,
     MAX = INT_MAX,
 } MPQemuCmd;
 
+typedef struct {
+    hwaddr gpas[REMOTE_MAX_FDS];
+    uint64_t sizes[REMOTE_MAX_FDS];
+    off_t offsets[REMOTE_MAX_FDS];
+} SyncSysmemMsg;
+
 /**
  * Maximum size of data2 field in the message to be transmitted.
  */
@@ -53,6 +64,7 @@ typedef enum {
  * MPQemuMsg Format of the message sent to the remote device from QEMU.
  *
  */
+
 typedef struct {
     int cmd;
     int bytestream;
@@ -60,6 +72,7 @@ typedef struct {
 
     union {
         uint64_t u64;
+        SyncSysmemMsg sync_sysmem;
     } data1;
 
     int fds[REMOTE_MAX_FDS];
io/mpqemu-link.c +13 -0
index c430b4d6a2..5887c8c6c0 100644
@@ -224,6 +224,19 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
             }
         }
     }
+     /* Verify message specific fields. */
+    switch (msg->cmd) {
+    case SYNC_SYSMEM:
+        if (msg->num_fds == 0 || msg->bytestream) {
+            return false;
+        }
+        if (msg->size != sizeof(msg->data1)) {
+            return false;
+        }
+        break;
+    default:
+        break;
+    }
 
     return true;
 }
Signature · 1 line
2.25.GIT
#1 lore raw

PATCH v7 1/21 memory: alloc RAM from file at offset

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

Allow RAM MemoryRegion to be created from an offset in a file, instead of allocating at offset of 0 by default. This is needed to synchronize RAM between QEMU & remote process.

---
 backends/hostmem-memfd.c  |  2 +-
 exec.c                    | 11 +++++++----
 hw/misc/ivshmem.c         |  3 ++-
 include/exec/memory.h     |  2 ++
 include/exec/ram_addr.h   |  2 +-
 include/qemu/mmap-alloc.h |  3 ++-
 memory.c                  |  3 ++-
 util/mmap-alloc.c         |  7 ++++---
 util/oslib-posix.c        |  2 +-
 9 files changed, 22 insertions(+), 13 deletions(-)
backends/hostmem-memfd.c +1 -1
index 1b5e4bfe0d..a862d010ab 100644
@@ -56,7 +56,7 @@ memfd_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     name = host_memory_backend_get_name(backend);
     memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend),
                                    name, backend->size,
-                                   backend->share, fd, errp);
+                                   backend->share, fd, 0, errp);
     g_free(name);
 }
 
exec.c +7 -4
index 21926dc9c7..afc42722b6 100644
@@ -1854,6 +1854,7 @@ static void *file_ram_alloc(RAMBlock *block,
                             ram_addr_t memory,
                             int fd,
                             bool truncate,
+                            off_t offset,
                             Error **errp)
 {
     void *area;
@@ -1904,7 +1905,8 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 
     area = qemu_ram_mmap(fd, memory, block->mr->align,
-                         block->flags & RAM_SHARED, block->flags & RAM_PMEM);
+                         block->flags & RAM_SHARED, block->flags & RAM_PMEM,
+                         offset);
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
@@ -2336,7 +2338,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp, bool shared)
 #ifdef CONFIG_POSIX
 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
                                  uint32_t ram_flags, int fd,
-                                 Error **errp)
+                                 off_t offset, Error **errp)
 {
     RAMBlock *new_block;
     Error *local_err = NULL;
@@ -2389,7 +2391,8 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = ram_flags;
-    new_block->host = file_ram_alloc(new_block, size, fd, !file_size, errp);
+    new_block->host = file_ram_alloc(new_block, size, fd, !file_size, offset,
+                                     errp);
     if (!new_block->host) {
         g_free(new_block);
         return NULL;
@@ -2419,7 +2422,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
         return NULL;
     }
 
-    block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, errp);
+    block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, 0, errp);
     if (!block) {
         if (created) {
             unlink(mem_path);
hw/misc/ivshmem.c +2 -1
index a8dc9b377d..b3cffbd1e0 100644
@@ -492,7 +492,8 @@ static void process_msg_shmem(IVShmemState *s, int fd, Error **errp)
 
     /* mmap the region and map into the BAR2 */
     memory_region_init_ram_from_fd(&s->server_bar2, OBJECT(s),
-                                   "ivshmem.bar2", size, true, fd, &local_err);
+                                   "ivshmem.bar2", size, true, fd, 0,
+                                   &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
         return;
include/exec/memory.h +2 -0
index 7207025bd4..a8fc1c36db 100644
@@ -902,6 +902,7 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
  * @size: size of the region.
  * @share: %true if memory must be mmaped with the MAP_SHARED flag
  * @fd: the fd to mmap.
+ * @offset: offset within the file referenced by fd
  * @errp: pointer to Error*, to store an error if it happens.
  *
  * Note that this function does not do anything to cause the data in the
@@ -913,6 +914,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
                                     uint64_t size,
                                     bool share,
                                     int fd,
+                                    ram_addr_t offset,
                                     Error **errp);
 #endif
 
include/exec/ram_addr.h +1 -1
index 7b5c24e928..ff0784a79c 100644
@@ -121,7 +121,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                    Error **errp);
 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
                                  uint32_t ram_flags, int fd,
-                                 Error **errp);
+                                 off_t offset, Error **errp);
 
 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                   MemoryRegion *mr, Error **errp);
include/qemu/mmap-alloc.h +2 -1
index e786266b92..4f579858bc 100644
@@ -25,7 +25,8 @@ void *qemu_ram_mmap(int fd,
                     size_t size,
                     size_t align,
                     bool shared,
-                    bool is_pmem);
+                    bool is_pmem,
+                    off_t start);
 
 void qemu_ram_munmap(int fd, void *ptr, size_t size);
 
memory.c +2 -1
index 9200b20130..130760bd0f 100644
@@ -1576,6 +1576,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
                                     uint64_t size,
                                     bool share,
                                     int fd,
+                                    ram_addr_t offset,
                                     Error **errp)
 {
     Error *err = NULL;
@@ -1585,7 +1586,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
     mr->destructor = memory_region_destructor_ram;
     mr->ram_block = qemu_ram_alloc_from_fd(size, mr,
                                            share ? RAM_SHARED : 0,
-                                           fd, &err);
+                                           fd, offset, &err);
     mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
     if (err) {
         mr->size = int128_zero();
util/mmap-alloc.c +4 -3
index 27dcccd8ec..a28f7025f0 100644
@@ -86,7 +86,8 @@ void *qemu_ram_mmap(int fd,
                     size_t size,
                     size_t align,
                     bool shared,
-                    bool is_pmem)
+                    bool is_pmem,
+                    off_t start)
 {
     int flags;
     int map_sync_flags = 0;
@@ -147,7 +148,7 @@ void *qemu_ram_mmap(int fd,
     offset = QEMU_ALIGN_UP((uintptr_t)guardptr, align) - (uintptr_t)guardptr;
 
     ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE,
-               flags | map_sync_flags, fd, 0);
+               flags | map_sync_flags, fd, start);
 
     if (ptr == MAP_FAILED && map_sync_flags) {
         if (errno == ENOTSUP) {
@@ -172,7 +173,7 @@ void *qemu_ram_mmap(int fd,
          * we will remove these flags to handle compatibility.
          */
         ptr = mmap(guardptr + offset, size, PROT_READ | PROT_WRITE,
-                   flags, fd, 0);
+                   flags, fd, start);
     }
 
     if (ptr == MAP_FAILED) {
util/oslib-posix.c +1 -1
index 39ddc77c85..132e7c46c6 100644
@@ -222,7 +222,7 @@ void *qemu_memalign(size_t alignment, size_t size)
 void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared)
 {
     size_t align = QEMU_VMALLOC_ALIGN;
-    void *ptr = qemu_ram_mmap(-1, size, align, shared, false);
+    void *ptr = qemu_ram_mmap(-1, size, align, shared, false, 0);
 
     if (ptr == MAP_FAILED) {
         return NULL;
Signature · 1 line
2.25.GIT
#14 lore raw

PATCH v7 7/21 multi-process: add co-routines to communicate with remote

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

process to avoid blocking the main loop during the message exchanges. To be used by proxy device.

---
 include/io/mpqemu-link.h | 16 +++++++++
 io/mpqemu-link.c         | 78 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+)
include/io/mpqemu-link.h +16 -0
index 1542e8ed07..52aa89656c 100644
@@ -17,6 +17,7 @@
 #include "qom/object.h"
 #include "qemu/thread.h"
 #include "io/channel.h"
+#include "io/channel-socket.h"
 
 #define REMOTE_MAX_FDS 8
 
@@ -30,6 +31,7 @@
  */
 typedef enum {
     INIT = 0,
+    RET_MSG,
     MAX = INT_MAX,
 } MPQemuCmd;
 
@@ -67,6 +69,20 @@ typedef struct {
     uint8_t *data2;
 } MPQemuMsg;
 
+struct MPQemuRequest {
+    MPQemuMsg *msg;
+    QIOChannelSocket *sioc;
+    Coroutine *co;
+    bool finished;
+    int error;
+    long ret;
+};
+
+typedef struct MPQemuRequest MPQemuRequest;
+
+uint64_t mpqemu_msg_send_reply_co(MPQemuMsg *msg, QIOChannel *ioc,
+                                  Error **errp);
+
 void mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc);
 int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc);
 
io/mpqemu-link.c +78 -0

90 lines not on this page · raw message

Signature · 1 line
2.25.GIT
#12 lore raw

PATCH v7 6/21 multi-process: define MPQemuMsg format and transmission functions

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

Defines MPQemuMsg, which is the message that is sent to the remote process. This message is sent over QIOChannel and is used to command the remote process to perform various tasks.

Also defined the helper functions to send and receive messages over the QIOChannel

---
 MAINTAINERS              |   2 +
 include/io/mpqemu-link.h |  75 +++++++++++++++++++
 io/Makefile.objs         |   2 +
 io/mpqemu-link.c         | 151 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 230 insertions(+)
 create mode 100644 include/io/mpqemu-link.h
 create mode 100644 io/mpqemu-link.c
MAINTAINERS +2 -0
index 83aae5b441..50a5fc53d6 100644
@@ -2947,6 +2947,8 @@ F: hw/pci-host/remote.c
 F: include/hw/pci-host/remote.h
 F: hw/i386/remote.c
 F: include/hw/i386/remote.h
+F: io/mpqemu-link.c
+F: include/io/mpqemu-link.h
 
 Build and test automation
 -------------------------
include/io/mpqemu-link.h new file +75 -0

75 lines not on this page · raw message

io/Makefile.objs +2 -0
index 9a20fce4ed..5875ab0697 100644
@@ -10,3 +10,5 @@ io-obj-y += channel-util.o
 io-obj-y += dns-resolver.o
 io-obj-y += net-listener.o
 io-obj-y += task.o
+
+io-obj-$(CONFIG_MPQEMU) += mpqemu-link.o
io/mpqemu-link.c new file +151 -0

151 lines not on this page · raw message

Signature · 1 line
2.25.GIT
#0 lore raw

v7 cover Initial support for multi-process qemu

From: Elena Ufimtseva <[email protected]>

Hello

This is the v7 of the patchset.
Thank you very much for the detailed feedback for v6. We appreciate your time.

We have addressed the latest comments and suggestions that were provided on v6 patch series and incorporated to this patchset.

This is the list of changes for v7:
- QEMU & remote process share the same binary. This allowed us to reduce the number of patches as well.

- We introduced the machine type "remote" that drives the remote process initialization.

- v7 now uses QIOChannel for communication and descriptors management.

- The remote process uses the main loop instead of a separate loop.

- Co-routines support in the QEMU Proxy-remote process communication The communication model based on co-routines needs some more work and we would like to hear your take on it.
Stefan has shared some ideas how we can proceed and we will take this to the next version after additional discussion.
We did not implement the protocol to listen and accept new connections.

There are other changes that were incorporated from the feedback we have received on v6.

We posted the Proof Of Concept patches [2] before the BoF session in 2018. Subsequently, we posted RFC v1 [3], RFC v2 [4], RFC v3 [5], RFC v4 [6], v5 [7] and v6 [8] of the patch series.
Following people contributed to this patchset:

John G Johnson <[email protected]>
Jagannathan Raman <[email protected]>
Elena Ufimtseva <[email protected]>
Kanth Ghatraju <[email protected]>
Konrad Wilk <[email protected]>

Also we would like to thank QEMU community for your help, suggestions and reviewing this large series of patches.

For the full concept writeup about QEMU multi-process, please refer to docs/devel/qemu-multiprocess.rst. Also see docs/qemu-multiprocess.txt for usage information.

We will post separate patchsets for the following improvements for the experimental Qemu multi-process:
- Live migration;
- communication channel improvements;

We welcome all your ideas, concerns, and questions for this patchset.

[1]: http://events17.linuxfoundation.org/sites/events/files/slides/KVM%20FORUM%20multi-process.pdf
[1]: https://www.youtube.com/watch?v=Kq1-coHh7lg
[2]: https://www.mail-archive.com/[email protected]/msg566538.html
[3]: https://www.mail-archive.com/[email protected]/msg602285.html
[4]: https://www.mail-archive.com/[email protected]/msg624877.html
[5]: https://www.mail-archive.com/[email protected]/msg642000.html
[6]: https://www.mail-archive.com/[email protected]/msg655118.html
[7]: https://www.mail-archive.com/[email protected]/msg682429.html
[8]: https://www.mail-archive.com/[email protected]/msg697484.html

Elena Ufimtseva (9):
multi-process: add qio channel function to transmit
multi-process: define MPQemuMsg format and transmission functions
multi-process: add co-routines to communicate with remote
multi-process: Initialize communication channel at the remote end
multi-process: introduce proxy object
multi-process: Forward PCI config space acceses to the remote process
multi-process: heartbeat messages to remote
multi-process: perform device reset in the remote process
multi-process: add configure and usage information

Jagannathan Raman (11):
memory: alloc RAM from file at offset
multi-process: Add config option for multi-process QEMU
multi-process: setup PCI host bridge for remote device
multi-process: setup a machine object for remote device process
multi-process: Initialize message handler in remote device
multi-process: setup memory manager for remote device
multi-process: Connect Proxy Object with device in the remote process
multi-process: PCI BAR read/write handling for proxy & remote
endpoints
multi-process: Synchronize remote memory
multi-process: create IOHUB object to handle irq
multi-process: Retrieve PCI info from remote process

John G Johnson (1):
multi-process: add the concept description to
docs/devel/qemu-multiprocess

 MAINTAINERS                          |  24 +
 backends/hostmem-memfd.c             |   2 +-
 configure                            |  11 +
 docs/devel/index.rst                 |   1 +
 docs/devel/multi-process.rst         | 957 +++++++++++++++++++++++++++
 docs/multi-process.rst               |  71 ++
 exec.c                               |  11 +-
 hw/Makefile.objs                     |   1 +
 hw/i386/Makefile.objs                |   3 +
 hw/i386/remote-memory.c              |  58 ++
 hw/i386/remote-msg.c                 | 301 +++++++++
 hw/i386/remote.c                     |  99 +++
 hw/misc/ivshmem.c                    |   3 +-
 hw/pci-host/Makefile.objs            |   1 +
 hw/pci-host/remote.c                 |  63 ++
 hw/pci/Makefile.objs                 |   2 +
 hw/pci/memory-sync.c                 | 214 ++++++
 hw/pci/proxy.c                       | 436 ++++++++++++
 hw/remote/Makefile.objs              |   1 +
 hw/remote/iohub.c                    | 153 +++++
 include/exec/memory.h                |   2 +
 include/exec/ram_addr.h              |   2 +-
 include/hw/i386/remote-memory.h      |  20 +
 include/hw/i386/remote.h             |  38 ++
 include/hw/pci-host/remote.h         |  34 +
 include/hw/pci/memory-sync.h         |  30 +
 include/hw/pci/pci_ids.h             |   3 +
 include/hw/pci/proxy.h               |  69 ++
 include/hw/remote/iohub.h            |  50 ++
 include/io/channel.h                 |  24 +
 include/io/mpqemu-link.h             | 140 ++++
 include/qemu/mmap-alloc.h            |   3 +-
 io/Makefile.objs                     |   2 +
 io/channel.c                         |  45 ++
 io/mpqemu-link.c                     | 277 ++++++++
 memory.c                             |   3 +-
 scripts/mpqemu-launcher-perf-mode.py |  67 ++
 scripts/mpqemu-launcher.py           |  47 ++
 util/mmap-alloc.c                    |   7 +-
 util/oslib-posix.c                   |   2 +-
 40 files changed, 3264 insertions(+), 13 deletions(-)
 create mode 100644 docs/devel/multi-process.rst
 create mode 100644 docs/multi-process.rst
 create mode 100644 hw/i386/remote-memory.c
 create mode 100644 hw/i386/remote-msg.c
 create mode 100644 hw/i386/remote.c
 create mode 100644 hw/pci-host/remote.c
 create mode 100644 hw/pci/memory-sync.c
 create mode 100644 hw/pci/proxy.c
 create mode 100644 hw/remote/Makefile.objs
 create mode 100644 hw/remote/iohub.c
 create mode 100644 include/hw/i386/remote-memory.h
 create mode 100644 include/hw/i386/remote.h
 create mode 100644 include/hw/pci-host/remote.h
 create mode 100644 include/hw/pci/memory-sync.h
 create mode 100644 include/hw/pci/proxy.h
 create mode 100644 include/hw/remote/iohub.h
 create mode 100644 include/io/mpqemu-link.h
 create mode 100644 io/mpqemu-link.c
 create mode 100644 scripts/mpqemu-launcher-perf-mode.py
 create mode 100755 scripts/mpqemu-launcher.py
Signature · 1 line
2.25.GIT
#22 lore raw

PATCH v7 11/21 multi-process: introduce proxy object

From: Elena Ufimtseva <[email protected]>

Defines a PCI Device proxy object as a child of TYPE_PCI_DEVICE.

---
 MAINTAINERS            |  2 ++
 hw/pci/Makefile.objs   |  1 +
 hw/pci/proxy.c         | 70 ++++++++++++++++++++++++++++++++++++++++++
 include/hw/pci/proxy.h | 43 ++++++++++++++++++++++++++
 4 files changed, 116 insertions(+)
 create mode 100644 hw/pci/proxy.c
 create mode 100644 include/hw/pci/proxy.h
MAINTAINERS +2 -0
index 017c96eace..b48c3114c1 100644
@@ -2952,6 +2952,8 @@ F: include/io/mpqemu-link.h
 F: hw/i386/remote-msg.c
 F: include/hw/i386/remote-memory.h
 F: hw/i386/remote-memory.c
+F: hw/pci/proxy.c
+F: include/hw/pci/proxy.h
 
 Build and test automation
 -------------------------
hw/pci/Makefile.objs +1 -0
index c78f2fb24b..515dda506c 100644
@@ -12,3 +12,4 @@ common-obj-$(CONFIG_PCI_EXPRESS) += pcie_port.o pcie_host.o
 
 common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o
 common-obj-$(CONFIG_ALL) += pci-stub.o
+obj-$(CONFIG_MPQEMU) += proxy.o
hw/pci/proxy.c new file +70 -0

70 lines not on this page · raw message

include/hw/pci/proxy.h new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.25.GIT
#30 lore raw

PATCH v7 13/21 multi-process: Forward PCI config space acceses to the remote process

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

The Proxy Object sends the PCI config space accesses as messages to the remote process over the communication channel

TODO:
Investigate if the local PCI config writes can be dropped. Without the proxy local PCI config space writes for the device, the driver in the guest times out on the probing.
We have tried to only refer to the remote for the PCI config writes, but the driver timeout in the guest forced as to left this as it is (removing local PCI config only).

---
 hw/i386/remote-msg.c     | 54 ++++++++++++++++++++++++++++++++++++++++
 hw/pci/proxy.c           | 54 ++++++++++++++++++++++++++++++++++++++++
 include/io/mpqemu-link.h |  8 ++++++
 io/mpqemu-link.c         | 14 +++++++++++
 4 files changed, 130 insertions(+)
hw/i386/remote-msg.c +54 -0
index 68f50866bb..aa5780d521 100644
@@ -11,10 +11,16 @@
 
 static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
                                     Error **errp);
+static void process_config_write(QIOChannel *ioc, PCIDevice *dev,
+                                 MPQemuMsg *msg);
+static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
+                                MPQemuMsg *msg);
 
 gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
                             gpointer opaque)
 {
+    DeviceState *dev = (DeviceState *)opaque;
+    PCIDevice *pci_dev = PCI_DEVICE(dev);
     Error *local_err = NULL;
     MPQemuMsg msg = { 0 };
 
@@ -42,6 +48,12 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case CONNECT_DEV:
         process_connect_dev_msg(&msg, ioc, &local_err);
         break;
+    case PCI_CONFIG_WRITE:
+        process_config_write(ioc, pci_dev, &msg);
+        break;
+    case PCI_CONFIG_READ:
+        process_config_read(ioc, pci_dev, &msg);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -89,3 +101,45 @@ exit:
 
     mpqemu_msg_send(&ret, com);
 }
+
+static void process_config_write(QIOChannel *ioc, PCIDevice *dev,
+                                 MPQemuMsg *msg)
+{
+    struct conf_data_msg *conf = (struct conf_data_msg *)msg->data2;
+    MPQemuMsg ret = { 0 };
+
+    if (conf->addr >= PCI_CFG_SPACE_EXP_SIZE) {
+        error_report("Bad address received when writing PCI config, pid %d",
+                     getpid());
+        ret.data1.u64 = UINT64_MAX;
+    } else {
+        pci_default_write_config(dev, conf->addr, conf->val, conf->l);
+    }
+
+    ret.cmd = RET_MSG;
+    ret.bytestream = 0;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, ioc);
+}
+
+static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
+                                MPQemuMsg *msg)
+{
+    struct conf_data_msg *conf = (struct conf_data_msg *)msg->data2;
+    MPQemuMsg ret = { 0 };
+
+    if (conf->addr >= PCI_CFG_SPACE_EXP_SIZE) {
+        error_report("Bad address received when reading PCI config, pid %d",
+                     getpid());
+        ret.data1.u64 = UINT64_MAX;
+    } else {
+        ret.data1.u64 = pci_default_read_config(dev, conf->addr, conf->l);
+    }
+
+    ret.cmd = RET_MSG;
+    ret.bytestream = 0;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, ioc);
+}
hw/pci/proxy.c +54 -0

72 lines not on this page · raw message

include/io/mpqemu-link.h +8 -0
index d620806c17..bbd6f3db44 100644
@@ -38,6 +38,8 @@ typedef enum {
     SYNC_SYSMEM,
     CONNECT_DEV,
     RET_MSG,
+    PCI_CONFIG_WRITE,
+    PCI_CONFIG_READ,
     MAX = INT_MAX,
 } MPQemuCmd;
 
@@ -47,6 +49,12 @@ typedef struct {
     off_t offsets[REMOTE_MAX_FDS];
 } SyncSysmemMsg;
 
+struct conf_data_msg {
+    uint32_t addr;
+    uint32_t val;
+    int l;
+};
+
 /**
  * Maximum size of data2 field in the message to be transmitted.
  */
io/mpqemu-link.c +14 -0
index 54df3b254e..19887611d0 100644
@@ -242,6 +242,20 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
             return false;
         }
         break;
+    case PCI_CONFIG_WRITE:
+    case PCI_CONFIG_READ:
+        if (msg->size != sizeof(struct conf_data_msg)) {
+            return false;
+        }
+        if (!msg->bytestream) {
+            return false;
+        }
+        struct conf_data_msg *conf = (struct conf_data_msg *)msg->data2;
+
+        if (conf->l != 1 && conf->l != 2 && conf->l != 4) {
+            return false;
+        }
+        break;
     default:
         break;
     }
Signature · 1 line
2.25.GIT
#24 lore raw

PATCH v7 12/21 multi-process: Connect Proxy Object with device in the remote process

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

Send a message to the remote process to connect PCI device with the corresponding Proxy object in QEMU

---
 hw/i386/remote-msg.c     | 39 +++++++++++++++++++++++++++++++++++++++
 hw/pci/proxy.c           | 28 ++++++++++++++++++++++++++++
 include/hw/pci/proxy.h   |  1 +
 include/io/mpqemu-link.h |  1 +
 io/mpqemu-link.c         |  8 ++++++++
 5 files changed, 77 insertions(+)
hw/i386/remote-msg.c +39 -0
index 58e24ab2ad..68f50866bb 100644
@@ -6,6 +6,11 @@
 #include "io/mpqemu-link.h"
 #include "qapi/error.h"
 #include "sysemu/runstate.h"
+#include "io/channel-util.h"
+#include "hw/pci/pci.h"
+
+static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
+                                    Error **errp);
 
 gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
                             gpointer opaque)
@@ -34,6 +39,9 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     }
 
     switch (msg.cmd) {
+    case CONNECT_DEV:
+        process_connect_dev_msg(&msg, ioc, &local_err);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -50,3 +58,34 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
 
     return TRUE;
 }
+
+static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
+                                    Error **errp)
+{
+    char *devid = (char *)msg->data2;
+    QIOChannel *dioc = NULL;
+    DeviceState *dev = NULL;
+    MPQemuMsg ret = { 0 };
+    int rc = 0;
+
+    g_assert(devid && (devid[msg->size - 1] == '\0'));
+
+    dev = qdev_find_recursive(sysbus_get_default(), devid);
+    if (!dev || !object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
+        rc = 0xff;
+        goto exit;
+    }
+
+    dioc = qio_channel_new_fd(msg->fds[0], errp);
+
+    qio_channel_add_watch(dioc, G_IO_IN | G_IO_HUP, mpqemu_process_msg,
+                          (void *)dev, NULL);
+
+exit:
+    ret.cmd = RET_MSG;
+    ret.bytestream = 0;
+    ret.data1.u64 = rc;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, com);
+}
hw/pci/proxy.c +28 -0
index 6d62399c52..16649ed0ec 100644
@@ -15,10 +15,38 @@
 #include "io/channel-util.h"
 #include "hw/qdev-properties.h"
 #include "monitor/monitor.h"
+#include "io/mpqemu-link.h"
 
 static void proxy_set_socket(PCIProxyDev *pdev, int fd, Error **errp)
 {
+    DeviceState *dev = DEVICE(pdev);
+    MPQemuMsg msg = { 0 };
+    int fds[2];
+    Error *local_err = NULL;
+
     pdev->com = qio_channel_new_fd(fd, errp);
+
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) {
+        error_setg(errp, "Failed to create proxy channel with fd %d", fd);
+        return;
+    }
+
+    msg.cmd = CONNECT_DEV;
+    msg.bytestream = 1;
+    msg.data2 = (uint8_t *)dev->id;
+    msg.size = strlen(dev->id) + 1;
+    msg.num_fds = 1;
+    msg.fds[0] = fds[1];
+
+    (void)mpqemu_msg_send_reply_co(&msg, pdev->com, &local_err);
+    if (local_err) {
+        error_setg(errp, "Failed to send DEV_CONNECT to the remote process");
+        close(fds[0]);
+    } else {
+        pdev->dev = qio_channel_new_fd(fds[0], errp);
+    }
+
+    close(fds[1]);
 }
 
 static Property proxy_properties[] = {
include/hw/pci/proxy.h +1 -0
index c1c7142fa2..72dd7e0944 100644
@@ -30,6 +30,7 @@ typedef struct PCIProxyDev {
     PCIDevice parent_dev;
     char *fd;
     QIOChannel *com;
+    QIOChannel *dev;
 } PCIProxyDev;
 
 typedef struct PCIProxyDevClass {
include/io/mpqemu-link.h +1 -0
index c6d2b6bf8b..d620806c17 100644
@@ -36,6 +36,7 @@
 typedef enum {
     INIT = 0,
     SYNC_SYSMEM,
+    CONNECT_DEV,
     RET_MSG,
     MAX = INT_MAX,
 } MPQemuCmd;
io/mpqemu-link.c +8 -0
index 5887c8c6c0..54df3b254e 100644
@@ -234,6 +234,14 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
             return false;
         }
         break;
+    case CONNECT_DEV:
+        if ((msg->num_fds != 1) ||
+            (msg->fds[0] == -1) ||
+            (msg->fds[0] == -1) ||
+            !msg->bytestream) {
+            return false;
+        }
+        break;
     default:
         break;
     }
Signature · 1 line
2.25.GIT
#40 lore raw

PATCH v7 18/21 multi-process: heartbeat messages to remote

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

In order to detect remote processes which are hung, the proxy periodically sends heartbeat messages to confirm if the remote process is alive. The remote process responds to this heartbeat message to confirm it is alive.

---
 hw/i386/remote-msg.c     | 14 ++++++++++
 hw/pci/proxy.c           | 58 ++++++++++++++++++++++++++++++++++++++++
 include/hw/pci/proxy.h   |  2 ++
 include/io/mpqemu-link.h |  1 +
 io/mpqemu-link.c         |  1 +
 5 files changed, 76 insertions(+)
hw/i386/remote-msg.c +14 -0
index 9379ee6442..919bddc1d5 100644
@@ -22,6 +22,7 @@ static void process_bar_write(QIOChannel *ioc, MPQemuMsg *msg, Error **errp);
 static void process_bar_read(QIOChannel *ioc, MPQemuMsg *msg, Error **errp);
 static void process_get_pci_info_msg(QIOChannel *ioc, MPQemuMsg *msg,
                                      PCIDevice *pci_dev);
+static void process_proxy_ping_msg(QIOChannel *ioc);
 
 gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
                             gpointer opaque)
@@ -76,6 +77,9 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case GET_PCI_INFO:
         process_get_pci_info_msg(ioc, &msg, pci_dev);
         break;
+    case PROXY_PING:
+        process_proxy_ping_msg(ioc);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -269,3 +273,13 @@ static void process_get_pci_info_msg(QIOChannel *ioc, MPQemuMsg *msg,
 
     mpqemu_msg_send(&ret, ioc);
 }
+
+static void process_proxy_ping_msg(QIOChannel *ioc)
+{
+    MPQemuMsg ret = { 0 };
+
+    ret.cmd = RET_MSG;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, ioc);
+}
hw/pci/proxy.c +58 -0

79 lines not on this page · raw message

include/hw/pci/proxy.h +2 -0
index e6f076ae95..037740309d 100644
@@ -53,6 +53,8 @@ struct PCIProxyDev {
     EventNotifier intr;
     EventNotifier resample;
 
+    QEMUTimer *hb_timer;
+
     ProxyMemoryRegion region[PCI_NUM_REGIONS];
 };
 
include/io/mpqemu-link.h +1 -0
index 4b96cb8ccb..676d7eb3ef 100644
@@ -44,6 +44,7 @@ typedef enum {
     BAR_READ,
     SET_IRQFD,
     GET_PCI_INFO,
+    PROXY_PING,
     MAX = INT_MAX,
 } MPQemuCmd;
 
io/mpqemu-link.c +1 -0
index d09b2a2f50..7452e55e17 100644
@@ -264,6 +264,7 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
         }
         break;
     case GET_PCI_INFO:
+    case PROXY_PING:
         if (msg->size) {
             return false;
         }
Signature · 1 line
2.25.GIT
#36 lore raw

PATCH v7 16/21 multi-process: create IOHUB object to handle irq

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

IOHUB object is added to manage PCI IRQs. It uses KVM_IRQFD ioctl to create irqfd to injecting PCI interrupts to the guest. IOHUB object forwards the irqfd to the remote process. Remote process uses this fd to directly send interrupts to the guest, bypassing QEMU.

---
 MAINTAINERS               |   2 +
 hw/Makefile.objs          |   1 +
 hw/i386/remote-msg.c      |   4 +
 hw/i386/remote.c          |  15 ++++
 hw/pci/proxy.c            |  52 +++++++++++++
 hw/remote/Makefile.objs   |   1 +
 hw/remote/iohub.c         | 153 ++++++++++++++++++++++++++++++++++++++
 include/hw/i386/remote.h  |   2 +
 include/hw/pci/pci_ids.h  |   3 +
 include/hw/pci/proxy.h    |   8 ++
 include/hw/remote/iohub.h |  50 +++++++++++++
 include/io/mpqemu-link.h  |   6 ++
 io/mpqemu-link.c          |   1 +
 13 files changed, 298 insertions(+)
 create mode 100644 hw/remote/Makefile.objs
 create mode 100644 hw/remote/iohub.c
 create mode 100644 include/hw/remote/iohub.h
MAINTAINERS +2 -0
index 38d605445e..f9ede7e094 100644
@@ -2956,6 +2956,8 @@ F: hw/pci/proxy.c
 F: include/hw/pci/proxy.h
 F: hw/pci/memory-sync.c
 F: include/hw/pci/memory-sync.h
+F: hw/remote/iohub.c
+F: include/hw/remote/iohub.h
 
 Build and test automation
 -------------------------
hw/Makefile.objs +1 -0
index 4cbe5e4e57..8caf659de0 100644
@@ -40,6 +40,7 @@ devices-dirs-$(CONFIG_MEM_DEVICE) += mem/
 devices-dirs-$(CONFIG_NUBUS) += nubus/
 devices-dirs-y += semihosting/
 devices-dirs-y += smbios/
+devices-dirs-y += remote/
 endif
 
 common-obj-y += $(devices-dirs-y)
hw/i386/remote-msg.c +4 -0
index 48b153eaae..67fee4bb57 100644
@@ -10,6 +10,7 @@
 #include "hw/pci/pci.h"
 #include "exec/memattrs.h"
 #include "hw/i386/remote-memory.h"
+#include "hw/remote/iohub.h"
 
 static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
                                     Error **errp);
@@ -67,6 +68,9 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case SYNC_SYSMEM:
         remote_sysmem_reconfig(&msg, &local_err);
         break;
+    case SET_IRQFD:
+        process_set_irqfd_msg(pci_dev, &msg);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
hw/i386/remote.c +15 -0
index 5342e884ad..8e74a6f1af 100644
@@ -17,12 +17,16 @@
 #include "qapi/error.h"
 #include "io/channel-util.h"
 #include "io/channel.h"
+#include "hw/pci/pci_host.h"
+#include "hw/remote/iohub.h"
 
 static void remote_machine_init(MachineState *machine)
 {
     MemoryRegion *system_memory, *system_io, *pci_memory;
     RemMachineState *s = REMOTE_MACHINE(machine);
     RemotePCIHost *rem_host;
+    PCIHostState *pci_host;
+    PCIDevice *pci_dev;
 
     system_memory = get_system_memory();
     system_io = get_system_io();
@@ -42,6 +46,17 @@ static void remote_machine_init(MachineState *machine)
     memory_region_add_subregion_overlap(system_memory, 0x0, pci_memory, -1);
 
     qdev_realize(DEVICE(rem_host), sysbus_get_default(), &error_fatal);
+
+    pci_host = PCI_HOST_BRIDGE(rem_host);
+    pci_dev = pci_create_simple_multifunction(pci_host->bus,
+                                              PCI_DEVFN(REMOTE_IOHUB_DEV,
+                                                        REMOTE_IOHUB_FUNC),
+                                              true, TYPE_REMOTE_IOHUB_DEVICE);
+
+    s->iohub = REMOTE_IOHUB_DEVICE(pci_dev);
+
+    pci_bus_irqs(pci_host->bus, remote_iohub_set_irq, remote_iohub_map_irq,
+                 s->iohub, REMOTE_IOHUB_NB_PIRQS);
 }
 
 static void remote_set_socket(Object *obj, const char *str, Error **errp)
hw/pci/proxy.c +52 -0

70 lines not on this page · raw message

hw/remote/Makefile.objs new file +1 -0
new file mode 100644
index 0000000000..635ce5e0ab
@@ -0,0 +1 @@
+common-obj-$(CONFIG_MPQEMU) += iohub.o
hw/remote/iohub.c new file +153 -0

153 lines not on this page · raw message

include/hw/i386/remote.h +2 -0
index c3890e57ab..6af423ab54 100644
@@ -18,12 +18,14 @@
 #include "hw/boards.h"
 #include "hw/pci-host/remote.h"
 #include "io/channel.h"
+#include "hw/remote/iohub.h"
 
 typedef struct RemMachineState {
     MachineState parent_obj;
 
     RemotePCIHost *host;
     QIOChannel *ioc;
+    RemoteIOHubState *iohub;
 } RemMachineState;
 
 #define TYPE_REMOTE_MACHINE "remote-machine"
include/hw/pci/pci_ids.h +3 -0
index 11f8ab7149..bd0c17dc78 100644
@@ -192,6 +192,9 @@
 #define PCI_DEVICE_ID_SUN_SIMBA          0x5000
 #define PCI_DEVICE_ID_SUN_SABRE          0xa000
 
+#define PCI_VENDOR_ID_ORACLE             0x108e
+#define PCI_DEVICE_ID_REMOTE_IOHUB       0xb000
+
 #define PCI_VENDOR_ID_CMD                0x1095
 #define PCI_DEVICE_ID_CMD_646            0x0646
 
include/hw/pci/proxy.h +8 -0
index a41a6aeaa5..e6f076ae95 100644
@@ -12,9 +12,12 @@
 #include "qemu/osdep.h"
 #include "qemu-common.h"
 
+#include <linux/kvm.h>
+
 #include "hw/pci/pci.h"
 #include "io/channel.h"
 #include "hw/pci/memory-sync.h"
+#include "qemu/event_notifier.h"
 
 #define TYPE_PCI_PROXY_DEV "pci-proxy-dev"
 
@@ -45,6 +48,11 @@ struct PCIProxyDev {
 
     RemoteMemSync sync;
 
+    struct kvm_irqfd irqfd;
+
+    EventNotifier intr;
+    EventNotifier resample;
+
     ProxyMemoryRegion region[PCI_NUM_REGIONS];
 };
 
include/hw/remote/iohub.h new file +50 -0

50 lines not on this page · raw message

include/io/mpqemu-link.h +6 -0
index 0422213863..a563b557ce 100644
@@ -42,6 +42,7 @@ typedef enum {
     PCI_CONFIG_READ,
     BAR_WRITE,
     BAR_READ,
+    SET_IRQFD,
     MAX = INT_MAX,
 } MPQemuCmd;
 
@@ -64,6 +65,10 @@ typedef struct {
     bool memory;
 } BarAccessMsg;
 
+typedef struct {
+    int intx;
+} SetIrqFdMsg;
+
 /**
  * Maximum size of data2 field in the message to be transmitted.
  */
@@ -92,6 +97,7 @@ typedef struct {
         uint64_t u64;
         SyncSysmemMsg sync_sysmem;
         BarAccessMsg bar_access;
+        SetIrqFdMsg set_irqfd;
     } data1;
 
     int fds[REMOTE_MAX_FDS];
io/mpqemu-link.c +1 -0
index 026e25dca4..561ac0576f 100644
@@ -258,6 +258,7 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
         break;
     case BAR_WRITE:
     case BAR_READ:
+    case SET_IRQFD:
         if (msg->size != sizeof(msg->data1)) {
             return false;
         }
Signature · 1 line
2.25.GIT
#42 lore raw

PATCH v7 19/21 multi-process: perform device reset in the remote process

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

Perform device reset in the remote process when QEMU performs device reset. This is required to reset the internal state (like registers, etc...) of emulated devices

---
 hw/i386/remote-msg.c     | 16 ++++++++++++++++
 hw/pci/proxy.c           | 20 ++++++++++++++++++++
 include/io/mpqemu-link.h |  1 +
 3 files changed, 37 insertions(+)
hw/i386/remote-msg.c +16 -0
index 919bddc1d5..63d2be4701 100644
@@ -11,6 +11,7 @@
 #include "exec/memattrs.h"
 #include "hw/i386/remote-memory.h"
 #include "hw/remote/iohub.h"
+#include "sysemu/reset.h"
 
 static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
                                     Error **errp);
@@ -23,6 +24,7 @@ static void process_bar_read(QIOChannel *ioc, MPQemuMsg *msg, Error **errp);
 static void process_get_pci_info_msg(QIOChannel *ioc, MPQemuMsg *msg,
                                      PCIDevice *pci_dev);
 static void process_proxy_ping_msg(QIOChannel *ioc);
+static void process_device_reset_msg(QIOChannel *ioc);
 
 gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
                             gpointer opaque)
@@ -80,6 +82,9 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case PROXY_PING:
         process_proxy_ping_msg(ioc);
         break;
+    case DEVICE_RESET:
+        process_device_reset_msg(ioc);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -283,3 +288,14 @@ static void process_proxy_ping_msg(QIOChannel *ioc)
 
     mpqemu_msg_send(&ret, ioc);
 }
+
+static void process_device_reset_msg(QIOChannel *ioc)
+{
+    MPQemuMsg ret = { 0 };
+
+    qemu_devices_reset();
+
+    ret.cmd = RET_MSG;
+
+    mpqemu_msg_send(&ret, ioc);
+}
hw/pci/proxy.c +20 -0
index e2e9a13287..9cba2bec65 100644
@@ -26,6 +26,7 @@
 static void probe_pci_info(PCIDevice *dev);
 static void start_hb_timer(PCIProxyDev *dev);
 static void pci_proxy_dev_exit(PCIDevice *pdev);
+static void proxy_device_reset(DeviceState *dev);
 
 static void proxy_set_socket(PCIProxyDev *pdev, int fd, Error **errp)
 {
@@ -198,6 +199,8 @@ static void pci_proxy_dev_class_init(ObjectClass *klass, void *data)
     k->config_write = pci_proxy_write_config;
     k->exit = pci_proxy_dev_exit;
 
+    dc->reset = proxy_device_reset;
+
     device_class_set_props(dc, proxy_properties);
 }
 
@@ -414,3 +417,20 @@ static void pci_proxy_dev_exit(PCIDevice *pdev)
 
     stop_hb_timer(dev);
 }
+
+static void proxy_device_reset(DeviceState *dev)
+{
+    PCIProxyDev *pdev = PCI_PROXY_DEV(dev);
+    MPQemuMsg msg = { 0 };
+    Error *local_err = NULL;
+
+    msg.bytestream = 0;
+    msg.size = sizeof(msg.data1);
+    msg.cmd = DEVICE_RESET;
+
+    (void)mpqemu_msg_send_reply_co(&msg, pdev->com, &local_err);
+    if (local_err) {
+        error_report("Failed to send DEVICE_RESET to the remote process");
+    }
+
+}
include/io/mpqemu-link.h +1 -0
index 676d7eb3ef..29dce60035 100644
@@ -45,6 +45,7 @@ typedef enum {
     SET_IRQFD,
     GET_PCI_INFO,
     PROXY_PING,
+    DEVICE_RESET,
     MAX = INT_MAX,
 } MPQemuCmd;
 
Signature · 1 line
2.25.GIT
#5 lore raw

PATCH v7 3/21 multi-process: setup PCI host bridge for remote device

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

PCI host bridge is setup for the remote device process. It is implemented using remote-pcihost object. It is an extension of the PCI host bridge setup by QEMU.
Remote-pcihost configures a PCI bus which could be used by the remote PCI device to latch on to.

---
 MAINTAINERS                  |  8 +++++
 hw/pci-host/Makefile.objs    |  1 +
 hw/pci-host/remote.c         | 63 ++++++++++++++++++++++++++++++++++++
 include/hw/pci-host/remote.h | 34 +++++++++++++++++++
 4 files changed, 106 insertions(+)
 create mode 100644 hw/pci-host/remote.c
 create mode 100644 include/hw/pci-host/remote.h
MAINTAINERS +8 -0
index 1b40446c73..e46f1960bf 100644
@@ -2938,6 +2938,14 @@ S: Maintained
 F: hw/semihosting/
 F: include/hw/semihosting/
 
+Multi-process QEMU
+M: Jagannathan Raman <[email protected]>
+M: Elena Ufimtseva <[email protected]>
+M: John G Johnson <[email protected]>
+S: Maintained
+F: hw/pci-host/remote.c
+F: include/hw/pci-host/remote.h
+
 Build and test automation
 -------------------------
 Build and test automation
hw/pci-host/Makefile.objs +1 -0
index e422e0aca0..daf900710d 100644
@@ -18,6 +18,7 @@ common-obj-$(CONFIG_XEN_IGD_PASSTHROUGH) += xen_igd_pt.o
 common-obj-$(CONFIG_PCI_EXPRESS_Q35) += q35.o
 common-obj-$(CONFIG_PCI_EXPRESS_GENERIC_BRIDGE) += gpex.o
 common-obj-$(CONFIG_PCI_EXPRESS_XILINX) += xilinx-pcie.o
+common-obj-$(CONFIG_MPQEMU) += remote.o
 
 common-obj-$(CONFIG_PCI_EXPRESS_DESIGNWARE) += designware.o
 obj-$(CONFIG_POWERNV) += pnv_phb4.o pnv_phb4_pec.o
hw/pci-host/remote.c new file +63 -0

63 lines not on this page · raw message

include/hw/pci-host/remote.h new file +34 -0
new file mode 100644
index 0000000000..3df1b53c17
@@ -0,0 +1,34 @@
+/*
+ * PCI Host for remote device
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef REMOTE_PCIHOST_H
+#define REMOTE_PCIHOST_H
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+
+#include "exec/memory.h"
+#include "hw/pci/pcie_host.h"
+
+#define TYPE_REMOTE_HOST_DEVICE "remote-pcihost"
+#define REMOTE_HOST_DEVICE(obj) \
+    OBJECT_CHECK(RemotePCIHost, (obj), TYPE_REMOTE_HOST_DEVICE)
+
+typedef struct RemotePCIHost {
+    /*< private >*/
+    PCIExpressHost parent_obj;
+    /*< public >*/
+
+    MemoryRegion *mr_pci_mem;
+    MemoryRegion *mr_sys_mem;
+    MemoryRegion *mr_sys_io;
+} RemotePCIHost;
+
+#endif
Signature · 1 line
2.25.GIT
#10 lore raw

PATCH v7 5/21 multi-process: add qio channel function to transmit

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

The entire array of the memory regions and file handlers. Will be used in the next patch.

---
 include/io/channel.h | 24 +++++++++++++++++++++++
 io/channel.c         | 45 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
include/io/channel.h +24 -0
index d4557f0930..c2e3eaeafc 100644
@@ -779,5 +779,29 @@ void qio_channel_set_aio_fd_handler(QIOChannel *ioc,
                                     IOHandler *io_read,
                                     IOHandler *io_write,
                                     void *opaque);
+/**
+ * qio_channel_writev_full_all:
+ * @ioc: the channel object
+ * @iov: the array of memory regions to write data from
+ * @niov: the length of the @iov array
+ * @fds: an array of file handles to send
+ * @nfds: number of file handles in @fds
+ * @errp: pointer to a NULL-initialized error object
+ *
+ *
+ * Behaves like qio_channel_writev_full but will attempt
+ * to send all data passed (file handles and memory regions).
+ * The function will wait for all requested data
+ * to be written, yielding from the current coroutine
+ * if required.
+ *
+ * Returns: 0 if all bytes were written, or -1 on error
+ */
+
+int qio_channel_writev_full_all(QIOChannel *ioc,
+                           const struct iovec *iov,
+                           size_t niov,
+                           int *fds, size_t nfds,
+                           Error **errp);
 
 #endif /* QIO_CHANNEL_H */
io/channel.c +45 -0

51 lines not on this page · raw message

Signature · 1 line
2.25.GIT
#3 lore raw

PATCH v7 2/21 multi-process: Add config option for multi-process QEMU

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

Add a configuration option to separate multi-process code

---
 configure | 11 +++++++++++
 1 file changed, 11 insertions(+)
configure +11 -0
index 4a22dcd563..591e3c62ba 100755
@@ -519,6 +519,7 @@ fuzzing="no"
 rng_none="no"
 secret_keyring=""
 libdaxctl=""
+mpqemu="no"
 
 supported_cpu="no"
 supported_os="no"
@@ -1631,6 +1632,10 @@ for opt do
   ;;
   --disable-libdaxctl) libdaxctl=no
   ;;
+  --enable-mpqemu) mpqemu=yes
+  ;;
+  --disable-mpqemu) mpqemu=no
+  ;;
   *)
       echo "ERROR: unknown option $opt"
       echo "Try '$0 --help' for more information"
@@ -1933,6 +1938,8 @@ disabled with --disable-FEATURE, default is enabled if available:
   xkbcommon       xkbcommon support
   rng-none        dummy RNG, avoid using /dev/(u)random and getrandom()
   libdaxctl       libdaxctl support
+  mpqemu          multi-process QEMU support
+
 
 NOTE: The object files are built at the place where configure is launched
 EOF
@@ -6999,6 +7006,7 @@ echo "fuzzing support   $fuzzing"
 echo "gdb               $gdb_bin"
 echo "rng-none          $rng_none"
 echo "Linux keyring     $secret_keyring"
+echo "multiprocess QEMU $mpqemu"
 
 if test "$supported_cpu" = "no"; then
     echo
@@ -7856,6 +7864,9 @@ fi
 if test "$sheepdog" = "yes" ; then
   echo "CONFIG_SHEEPDOG=y" >> $config_host_mak
 fi
+if test "$mpqemu" = "yes" ; then
+  echo "CONFIG_MPQEMU=y" >> $config_host_mak
+fi
 if test "$fuzzing" = "yes" ; then
   if test "$have_fuzzer" = "yes"; then
     FUZZ_LDFLAGS=" -fsanitize=address,fuzzer"
Signature · 1 line
2.25.GIT
#46 lore raw

PATCH v7 21/21 multi-process: add configure and usage information

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

---
 MAINTAINERS                          |  2 +
 docs/multi-process.rst               | 71 ++++++++++++++++++++++++++++
 scripts/mpqemu-launcher-perf-mode.py | 67 ++++++++++++++++++++++++++
 scripts/mpqemu-launcher.py           | 47 ++++++++++++++++++
 4 files changed, 187 insertions(+)
 create mode 100644 docs/multi-process.rst
 create mode 100644 scripts/mpqemu-launcher-perf-mode.py
 create mode 100755 scripts/mpqemu-launcher.py
MAINTAINERS +2 -0
index a65a5dd279..60c21d8210 100644
@@ -2959,6 +2959,8 @@ F: include/hw/pci/memory-sync.h
 F: hw/remote/iohub.c
 F: include/hw/remote/iohub.h
 F: docs/devel/multi-process.rst
+F: scripts/mpqemu-launcher.py
+F: scripts/mpqemu-launcher-perf-mode.py
 
 Build and test automation
 -------------------------
docs/multi-process.rst new file +71 -0

71 lines not on this page · raw message

scripts/mpqemu-launcher-perf-mode.py new file +67 -0

67 lines not on this page · raw message

scripts/mpqemu-launcher.py new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.25.GIT
#8 lore raw

PATCH v7 4/21 multi-process: setup a machine object for remote device process

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

remote-machine object sets up various subsystems of the remote device process. Instantiate PCI host bridge object and initialize RAM, IO & PCI memory regions.

---
 MAINTAINERS              |  2 ++
 hw/i386/Makefile.objs    |  1 +
 hw/i386/remote.c         | 64 ++++++++++++++++++++++++++++++++++++++++
 include/hw/i386/remote.h | 31 +++++++++++++++++++
 4 files changed, 98 insertions(+)
 create mode 100644 hw/i386/remote.c
 create mode 100644 include/hw/i386/remote.h
MAINTAINERS +2 -0
index e46f1960bf..83aae5b441 100644
@@ -2945,6 +2945,8 @@ M: John G Johnson <[email protected]>
 S: Maintained
 F: hw/pci-host/remote.c
 F: include/hw/pci-host/remote.h
+F: hw/i386/remote.c
+F: include/hw/i386/remote.h
 
 Build and test automation
 -------------------------
hw/i386/Makefile.objs +1 -0
index 6abc74551a..5ead266a15 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_XEN) += ../xenpv/ xen/
 obj-$(CONFIG_VMPORT) += vmport.o
 obj-$(CONFIG_VMMOUSE) += vmmouse.o
 obj-$(CONFIG_PC) += port92.o
+obj-$(CONFIG_MPQEMU) += remote.o
 
 obj-y += kvmvapic.o
 obj-$(CONFIG_ACPI) += acpi-common.o
hw/i386/remote.c new file +64 -0

64 lines not on this page · raw message

include/hw/i386/remote.h new file +31 -0
new file mode 100644
index 0000000000..d118a940be
@@ -0,0 +1,31 @@
+/*
+ * Remote machine configuration
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef REMOTE_MACHINE_H
+#define REMOTE_MACHINE_H
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+
+#include "qom/object.h"
+#include "hw/boards.h"
+#include "hw/pci-host/remote.h"
+
+typedef struct RemMachineState {
+    MachineState parent_obj;
+
+    RemotePCIHost *host;
+} RemMachineState;
+
+#define TYPE_REMOTE_MACHINE "remote-machine"
+#define REMOTE_MACHINE(obj) \
+    OBJECT_CHECK(RemMachineState, (obj), TYPE_REMOTE_MACHINE)
+
+#endif
Signature · 1 line
2.25.GIT
#16 lore raw

PATCH v7 8/21 multi-process: Initialize communication channel at the remote end

In reply to [email protected]

From: Elena Ufimtseva <[email protected]>

Initialize the common QIOChannel at remote the end

---
 hw/i386/remote.c         | 16 ++++++++++++++++
 include/hw/i386/remote.h |  2 ++
 2 files changed, 18 insertions(+)
hw/i386/remote.c +16 -0
index 4d13abe9f3..1a1becffe0 100644
@@ -15,6 +15,7 @@
 #include "exec/address-spaces.h"
 #include "exec/memory.h"
 #include "qapi/error.h"
+#include "io/channel-util.h"
 
 static void remote_machine_init(MachineState *machine)
 {
@@ -42,6 +43,20 @@ static void remote_machine_init(MachineState *machine)
     qdev_realize(DEVICE(rem_host), sysbus_get_default(), &error_fatal);
 }
 
+static void remote_set_socket(Object *obj, const char *str, Error **errp)
+{
+    RemMachineState *s = REMOTE_MACHINE(obj);
+    Error *local_err = NULL;
+    int fd = atoi(str);
+
+    s->ioc = qio_channel_new_fd(fd, &local_err);
+}
+
+static void remote_instance_init(Object *obj)
+{
+    object_property_add_str(obj, "socket", NULL, remote_set_socket);
+}
+
 static void remote_machine_class_init(ObjectClass *oc, void *data)
 {
     MachineClass *mc = MACHINE_CLASS(oc);
@@ -53,6 +68,7 @@ static const TypeInfo remote_machine = {
     .name = TYPE_REMOTE_MACHINE,
     .parent = TYPE_MACHINE,
     .instance_size = sizeof(RemMachineState),
+    .instance_init = remote_instance_init,
     .class_init = remote_machine_class_init,
 };
 
include/hw/i386/remote.h +2 -0
index d118a940be..0f8b861e7a 100644
@@ -17,11 +17,13 @@
 #include "qom/object.h"
 #include "hw/boards.h"
 #include "hw/pci-host/remote.h"
+#include "io/channel.h"
 
 typedef struct RemMachineState {
     MachineState parent_obj;
 
     RemotePCIHost *host;
+    QIOChannel *ioc;
 } RemMachineState;
 
 #define TYPE_REMOTE_MACHINE "remote-machine"
Signature · 1 line
2.25.GIT
#18 lore raw

PATCH v7 9/21 multi-process: Initialize message handler in remote device

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

Initializes the message handler function in the remote process. It is called whenever there's an event pending on QIOChannel that registers this function.

---
 MAINTAINERS              |  1 +
 hw/i386/Makefile.objs    |  1 +
 hw/i386/remote-msg.c     | 52 ++++++++++++++++++++++++++++++++++++++++
 hw/i386/remote.c         |  4 ++++
 include/hw/i386/remote.h |  3 +++
 5 files changed, 61 insertions(+)
 create mode 100644 hw/i386/remote-msg.c
MAINTAINERS +1 -0
index 50a5fc53d6..e204b3e0c6 100644
@@ -2949,6 +2949,7 @@ F: hw/i386/remote.c
 F: include/hw/i386/remote.h
 F: io/mpqemu-link.c
 F: include/io/mpqemu-link.h
+F: hw/i386/remote-msg.c
 
 Build and test automation
 -------------------------
hw/i386/Makefile.objs +1 -0
index 5ead266a15..83969585c1 100644
@@ -15,6 +15,7 @@ obj-$(CONFIG_VMPORT) += vmport.o
 obj-$(CONFIG_VMMOUSE) += vmmouse.o
 obj-$(CONFIG_PC) += port92.o
 obj-$(CONFIG_MPQEMU) += remote.o
+obj-$(CONFIG_MPQEMU) += remote-msg.o
 
 obj-y += kvmvapic.o
 obj-$(CONFIG_ACPI) += acpi-common.o
hw/i386/remote-msg.c new file +52 -0

52 lines not on this page · raw message

hw/i386/remote.c +4 -0
index 1a1becffe0..5342e884ad 100644
@@ -16,6 +16,7 @@
 #include "exec/memory.h"
 #include "qapi/error.h"
 #include "io/channel-util.h"
+#include "io/channel.h"
 
 static void remote_machine_init(MachineState *machine)
 {
@@ -50,6 +51,9 @@ static void remote_set_socket(Object *obj, const char *str, Error **errp)
     int fd = atoi(str);
 
     s->ioc = qio_channel_new_fd(fd, &local_err);
+
+    qio_channel_add_watch(s->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+                          mpqemu_process_msg, NULL, NULL);
 }
 
 static void remote_instance_init(Object *obj)
include/hw/i386/remote.h +3 -0
index 0f8b861e7a..c3890e57ab 100644
@@ -30,4 +30,7 @@ typedef struct RemMachineState {
 #define REMOTE_MACHINE(obj) \
     OBJECT_CHECK(RemMachineState, (obj), TYPE_REMOTE_MACHINE)
 
+gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
+                            gpointer opaque);
+
 #endif
Signature · 1 line
2.25.GIT
#44 lore raw

PATCH v7 20/21 multi-process: add the concept description to docs/devel/qemu-multiprocess

In reply to [email protected]

From: John G Johnson <[email protected]>

---
 MAINTAINERS                  |   1 +
 docs/devel/index.rst         |   1 +
 docs/devel/multi-process.rst | 957 +++++++++++++++++++++++++++++++++++
 3 files changed, 959 insertions(+)
 create mode 100644 docs/devel/multi-process.rst
MAINTAINERS +1 -0
index f9ede7e094..a65a5dd279 100644
@@ -2958,6 +2958,7 @@ F: hw/pci/memory-sync.c
 F: include/hw/pci/memory-sync.h
 F: hw/remote/iohub.c
 F: include/hw/remote/iohub.h
+F: docs/devel/multi-process.rst
 
 Build and test automation
 -------------------------
docs/devel/index.rst +1 -0
index bb8238c5d6..8ed4369632 100644
@@ -28,3 +28,4 @@ Contents:
    reset
    s390-dasd-ipl
    clocks
+   multi-process
docs/devel/multi-process.rst new file +957 -0

957 lines not on this page · raw message

Signature · 1 line
2.25.GIT
#34 lore raw

PATCH v7 15/21 multi-process: Synchronize remote memory

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

Add memory-listener object which is used to keep the view of the RAM in sync between QEMU and remote process.
A MemoryListener is registered for system-memory AddressSpace. The listener sends SYNC_SYSMEM message to the remote process when memory listener commits the changes to memory, the remote process receives the message and processes it in the handler for SYNC_SYSMEM message.

---
 MAINTAINERS                  |   2 +
 hw/i386/remote-msg.c         |   4 +
 hw/pci/Makefile.objs         |   1 +
 hw/pci/memory-sync.c         | 214 +++++++++++++++++++++++++++++++++++
 hw/pci/proxy.c               |   4 +
 include/hw/pci/memory-sync.h |  30 +++++
 include/hw/pci/proxy.h       |   3 +
 7 files changed, 258 insertions(+)
 create mode 100644 hw/pci/memory-sync.c
 create mode 100644 include/hw/pci/memory-sync.h
MAINTAINERS +2 -0
index b48c3114c1..38d605445e 100644
@@ -2954,6 +2954,8 @@ F: include/hw/i386/remote-memory.h
 F: hw/i386/remote-memory.c
 F: hw/pci/proxy.c
 F: include/hw/pci/proxy.h
+F: hw/pci/memory-sync.c
+F: include/hw/pci/memory-sync.h
 
 Build and test automation
 -------------------------
hw/i386/remote-msg.c +4 -0
index ffb4143736..48b153eaae 100644
@@ -9,6 +9,7 @@
 #include "io/channel-util.h"
 #include "hw/pci/pci.h"
 #include "exec/memattrs.h"
+#include "hw/i386/remote-memory.h"
 
 static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
                                     Error **errp);
@@ -63,6 +64,9 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case BAR_READ:
         process_bar_read(ioc, &msg, &local_err);
         break;
+    case SYNC_SYSMEM:
+        remote_sysmem_reconfig(&msg, &local_err);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
hw/pci/Makefile.objs +1 -0
index 515dda506c..c90acd5a6e 100644
@@ -13,3 +13,4 @@ common-obj-$(CONFIG_PCI_EXPRESS) += pcie_port.o pcie_host.o
 common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o
 common-obj-$(CONFIG_ALL) += pci-stub.o
 obj-$(CONFIG_MPQEMU) += proxy.o
+obj-$(CONFIG_MPQEMU) += memory-sync.o
hw/pci/memory-sync.c new file +214 -0

214 lines not on this page · raw message

hw/pci/proxy.c +4 -0
index fff021a06a..5ecbdd2dcf 100644
@@ -17,6 +17,8 @@
 #include "monitor/monitor.h"
 #include "io/mpqemu-link.h"
 #include "qemu/error-report.h"
+#include "hw/pci/memory-sync.h"
+#include "qom/object.h"
 
 static void proxy_set_socket(PCIProxyDev *pdev, int fd, Error **errp)
 {
@@ -68,6 +70,8 @@ static void pci_proxy_dev_realize(PCIDevice *device, Error **errp)
         }
         proxy_set_socket(dev, proxyfd, errp);
     }
+
+    configure_memory_sync(&dev->sync, dev->com);
 }
 
 static int config_op_send(PCIProxyDev *pdev, uint32_t addr, uint32_t *val,
include/hw/pci/memory-sync.h new file +30 -0
new file mode 100644
index 0000000000..3c9007f318
@@ -0,0 +1,30 @@
+/*
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef MEMORY_SYNC_H
+#define MEMORY_SYNC_H
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+
+#include "exec/memory.h"
+#include "io/channel.h"
+
+typedef struct RemoteMemSync {
+    MemoryListener listener;
+
+    int n_mr_sections;
+    MemoryRegionSection *mr_sections;
+
+    QIOChannel *ioc;
+} RemoteMemSync;
+
+void configure_memory_sync(RemoteMemSync *sync, QIOChannel *ioc);
+void deconfigure_memory_sync(RemoteMemSync *sync);
+
+#endif
include/hw/pci/proxy.h +3 -0
index 4f9f9c4e15..a41a6aeaa5 100644
@@ -14,6 +14,7 @@
 
 #include "hw/pci/pci.h"
 #include "io/channel.h"
+#include "hw/pci/memory-sync.h"
 
 #define TYPE_PCI_PROXY_DEV "pci-proxy-dev"
 
@@ -42,6 +43,8 @@ struct PCIProxyDev {
     QIOChannel *com;
     QIOChannel *dev;
 
+    RemoteMemSync sync;
+
     ProxyMemoryRegion region[PCI_NUM_REGIONS];
 };
 
Signature · 1 line
2.25.GIT
#32 lore raw

PATCH v7 14/21 multi-process: PCI BAR read/write handling for proxy & remote endpoints

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

Proxy device object implements handler for PCI BAR writes and reads. The handler uses BAR_WRITE/BAR_READ message to communicate to the remote process with the BAR address and value to be written/read. The remote process implements handler for BAR_WRITE/BAR_READ message.

---
 hw/i386/remote-msg.c     | 95 ++++++++++++++++++++++++++++++++++++++++
 hw/pci/proxy.c           | 61 ++++++++++++++++++++++++++
 include/hw/pci/proxy.h   | 16 ++++++-
 include/io/mpqemu-link.h | 10 +++++
 io/mpqemu-link.c         |  6 +++
 5 files changed, 186 insertions(+), 2 deletions(-)
hw/i386/remote-msg.c +95 -0
index aa5780d521..ffb4143736 100644
@@ -8,6 +8,7 @@
 #include "sysemu/runstate.h"
 #include "io/channel-util.h"
 #include "hw/pci/pci.h"
+#include "exec/memattrs.h"
 
 static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
                                     Error **errp);
@@ -15,6 +16,8 @@ static void process_config_write(QIOChannel *ioc, PCIDevice *dev,
                                  MPQemuMsg *msg);
 static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
                                 MPQemuMsg *msg);
+static void process_bar_write(QIOChannel *ioc, MPQemuMsg *msg, Error **errp);
+static void process_bar_read(QIOChannel *ioc, MPQemuMsg *msg, Error **errp);
 
 gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
                             gpointer opaque)
@@ -54,6 +57,12 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case PCI_CONFIG_READ:
         process_config_read(ioc, pci_dev, &msg);
         break;
+    case BAR_WRITE:
+        process_bar_write(ioc, &msg, &local_err);
+        break;
+    case BAR_READ:
+        process_bar_read(ioc, &msg, &local_err);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -143,3 +152,89 @@ static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
 
     mpqemu_msg_send(&ret, ioc);
 }
+
+static void process_bar_write(QIOChannel *ioc, MPQemuMsg *msg, Error **errp)
+{
+    BarAccessMsg *bar_access = &msg->data1.bar_access;
+    AddressSpace *as =
+        bar_access->memory ? &address_space_memory : &address_space_io;
+    MPQemuMsg ret = { 0 };
+    MemTxResult res;
+
+    if (!is_power_of_2(bar_access->size) ||
+       (bar_access->size > sizeof(uint64_t))) {
+        ret.data1.u64 = UINT64_MAX;
+        goto fail;
+    }
+
+    res = address_space_rw(as, bar_access->addr, MEMTXATTRS_UNSPECIFIED,
+                           (void *)&bar_access->val, bar_access->size,
+                           true);
+
+    if (res != MEMTX_OK) {
+        error_setg(errp, "Could not perform address space write operation,"
+                   " inaccessible address: %lx in pid %d.",
+                   bar_access->addr, getpid());
+        ret.data1.u64 = -1;
+    }
+
+fail:
+    ret.cmd = RET_MSG;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, ioc);
+}
+
+static void process_bar_read(QIOChannel *ioc, MPQemuMsg *msg, Error **errp)
+{
+    BarAccessMsg *bar_access = &msg->data1.bar_access;
+    MPQemuMsg ret = { 0 };
+    AddressSpace *as;
+    MemTxResult res;
+    uint64_t val = 0;
+
+    as = bar_access->memory ? &address_space_memory : &address_space_io;
+
+    if (!is_power_of_2(bar_access->size) ||
+       (bar_access->size > sizeof(uint64_t))) {
+        val = UINT64_MAX;
+        goto fail;
+    }
+
+    res = address_space_rw(as, bar_access->addr, MEMTXATTRS_UNSPECIFIED,
+                           (void *)&val, bar_access->size, false);
+
+    if (res != MEMTX_OK) {
+        error_setg(errp, "Could not perform address space read operation,"
+                   " inaccessible address: %lx in pid %d.",
+                   bar_access->addr, getpid());
+        val = UINT64_MAX;
+        goto fail;
+    }
+
+    switch (bar_access->size) {
+    case 8:
+        /* Nothing to do as val is already 8 bytes long */
+        break;
+    case 4:
+        val = *((uint32_t *)&val);
+        break;
+    case 2:
+        val = *((uint16_t *)&val);
+        break;
+    case 1:
+        val = *((uint8_t *)&val);
+        break;
+    default:
+        error_setg(errp, "Invalid PCI BAR read size in pid %d",
+                   getpid());
+        val = (uint64_t)-1;
+    }
+
+fail:
+    ret.cmd = RET_MSG;
+    ret.data1.u64 = val;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, ioc);
+}
hw/pci/proxy.c +61 -0

64 lines not on this page · raw message

include/hw/pci/proxy.h +14 -2
index 72dd7e0944..4f9f9c4e15 100644
@@ -26,12 +26,24 @@
 #define PCI_PROXY_DEV_GET_CLASS(obj) \
             OBJECT_GET_CLASS(PCIProxyDevClass, (obj), TYPE_PCI_PROXY_DEV)
 
-typedef struct PCIProxyDev {
+typedef struct PCIProxyDev PCIProxyDev;
+
+typedef struct ProxyMemoryRegion {
+    PCIProxyDev *dev;
+    MemoryRegion mr;
+    bool memory;
+    bool present;
+    uint8_t type;
+} ProxyMemoryRegion;
+
+struct PCIProxyDev {
     PCIDevice parent_dev;
     char *fd;
     QIOChannel *com;
     QIOChannel *dev;
-} PCIProxyDev;
+
+    ProxyMemoryRegion region[PCI_NUM_REGIONS];
+};
 
 typedef struct PCIProxyDevClass {
     PCIDeviceClass parent_class;
include/io/mpqemu-link.h +10 -0
index bbd6f3db44..0422213863 100644
@@ -40,6 +40,8 @@ typedef enum {
     RET_MSG,
     PCI_CONFIG_WRITE,
     PCI_CONFIG_READ,
+    BAR_WRITE,
+    BAR_READ,
     MAX = INT_MAX,
 } MPQemuCmd;
 
@@ -55,6 +57,13 @@ struct conf_data_msg {
     int l;
 };
 
+typedef struct {
+    hwaddr addr;
+    uint64_t val;
+    unsigned size;
+    bool memory;
+} BarAccessMsg;
+
 /**
  * Maximum size of data2 field in the message to be transmitted.
  */
@@ -82,6 +91,7 @@ typedef struct {
     union {
         uint64_t u64;
         SyncSysmemMsg sync_sysmem;
+        BarAccessMsg bar_access;
     } data1;
 
     int fds[REMOTE_MAX_FDS];
io/mpqemu-link.c +6 -0
index 19887611d0..026e25dca4 100644
@@ -256,6 +256,12 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
             return false;
         }
         break;
+    case BAR_WRITE:
+    case BAR_READ:
+        if (msg->size != sizeof(msg->data1)) {
+            return false;
+        }
+        break;
     default:
         break;
     }
Signature · 1 line
2.25.GIT
#38 lore raw

PATCH v7 17/21 multi-process: Retrieve PCI info from remote process

In reply to [email protected]

From: Jagannathan Raman <[email protected]>

Retrieve PCI configuration info about the remote device and configure the Proxy PCI object based on the returned information

---
 hw/i386/remote-msg.c     | 23 +++++++++++
 hw/pci/proxy.c           | 89 ++++++++++++++++++++++++++++++++++++++++
 include/io/mpqemu-link.h |  9 ++++
 io/mpqemu-link.c         |  5 +++
 4 files changed, 126 insertions(+)
hw/i386/remote-msg.c +23 -0
index 67fee4bb57..9379ee6442 100644
@@ -20,6 +20,8 @@ static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
                                 MPQemuMsg *msg);
 static void process_bar_write(QIOChannel *ioc, MPQemuMsg *msg, Error **errp);
 static void process_bar_read(QIOChannel *ioc, MPQemuMsg *msg, Error **errp);
+static void process_get_pci_info_msg(QIOChannel *ioc, MPQemuMsg *msg,
+                                     PCIDevice *pci_dev);
 
 gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
                             gpointer opaque)
@@ -71,6 +73,9 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case SET_IRQFD:
         process_set_irqfd_msg(pci_dev, &msg);
         break;
+    case GET_PCI_INFO:
+        process_get_pci_info_msg(ioc, &msg, pci_dev);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -246,3 +251,21 @@ fail:
 
     mpqemu_msg_send(&ret, ioc);
 }
+
+static void process_get_pci_info_msg(QIOChannel *ioc, MPQemuMsg *msg,
+                                     PCIDevice *pci_dev)
+{
+    PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
+    MPQemuMsg ret = { 0 };
+
+    ret.cmd = RET_MSG;
+
+    ret.data1.ret_pci_info.vendor_id = pc->vendor_id;
+    ret.data1.ret_pci_info.device_id = pc->device_id;
+    ret.data1.ret_pci_info.class_id = pc->class_id;
+    ret.data1.ret_pci_info.subsystem_id = pc->subsystem_id;
+
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, ioc);
+}
hw/pci/proxy.c +89 -0

113 lines not on this page · raw message

include/io/mpqemu-link.h +9 -0
index a563b557ce..4b96cb8ccb 100644
@@ -43,6 +43,7 @@ typedef enum {
     BAR_WRITE,
     BAR_READ,
     SET_IRQFD,
+    GET_PCI_INFO,
     MAX = INT_MAX,
 } MPQemuCmd;
 
@@ -69,6 +70,13 @@ typedef struct {
     int intx;
 } SetIrqFdMsg;
 
+typedef struct {
+    uint16_t vendor_id;
+    uint16_t device_id;
+    uint16_t class_id;
+    uint16_t subsystem_id;
+} RetPCIInfoMsg;
+
 /**
  * Maximum size of data2 field in the message to be transmitted.
  */
@@ -98,6 +106,7 @@ typedef struct {
         SyncSysmemMsg sync_sysmem;
         BarAccessMsg bar_access;
         SetIrqFdMsg set_irqfd;
+        RetPCIInfoMsg ret_pci_info;
     } data1;
 
     int fds[REMOTE_MAX_FDS];
io/mpqemu-link.c +5 -0
index 561ac0576f..d09b2a2f50 100644
@@ -263,6 +263,11 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
             return false;
         }
         break;
+    case GET_PCI_INFO:
+        if (msg->size) {
+            return false;
+        }
+        break;
     default:
         break;
     }
Signature · 1 line
2.25.GIT
Reviewed-by on v7 2/21
#4 lore raw

Re: [PATCH v7 02/21] multi-process: Add config option for multi-process QEMU

In reply to [email protected]
Quoted 10 lines · On Sat, Jun 27, 2020 at 10:09:24AM -0700, [email protected] wrote:

From: Jagannathan Raman <[email protected]>

Add a configuration option to separate multi-process code

---
 configure | 11 +++++++++++
 1 file changed, 11 insertions(+)
Reviewed-by on v7 1/21
#2 lore raw

Re: [PATCH v7 01/21] memory: alloc RAM from file at offset

In reply to [email protected]
Quoted 9 lines · On Sat, Jun 27, 2020 at 10:09:23AM -0700, [email protected] wrote:

From: Jagannathan Raman <[email protected]>

Allow RAM MemoryRegion to be created from an offset in a file, instead of allocating at offset of 0 by default. This is needed to synchronize RAM between QEMU & remote process.

Quoted 11 lines
---
 backends/hostmem-memfd.c  |  2 +-
 exec.c                    | 11 +++++++----
 hw/misc/ivshmem.c         |  3 ++-
 include/exec/memory.h     |  2 ++
 include/exec/ram_addr.h   |  2 +-
 include/qemu/mmap-alloc.h |  3 ++-
 memory.c                  |  3 ++-
 util/mmap-alloc.c         |  7 ++++---
 util/oslib-posix.c        |  2 +-
 9 files changed, 22 insertions(+), 13 deletions(-)
#6 lore raw

Re: [PATCH v7 03/21] multi-process: setup PCI host bridge for remote device

In reply to [email protected]
Quoted 6 lines · On Sat, Jun 27, 2020 at 10:09:25AM -0700, [email protected] wrote:
hw/pci-host/remote.c new file +0 -0
new file mode 100644
index 0000000000..5ea9af4154

hw/pci-host/remote.c line 8

Quoted 8 lines
hw/pci-host/remote.c new file +8 -0
@@ -0,0 +1,63 @@
+/*
+ * Remote PCI host device
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *

A little more detail would be nice:

Unlike PCI host devices that model physical hardware, the purpose of this PCI host is to host multi-process QEMU remote PCI devices.

Multi-process QEMU talks to a remote PCI device that runs in a separate process. In order to reuse QEMU device models in the remote process we need a PCI bus that holds the devices.

This PCI host is purely a container for PCI devices. It's fake in the sense that the guest never sees this PCI host and has no way of accessing it. It's job is just to provide the environment that QEMU PCI device models need when running in a remote process.

I think this could be restated more clearly but hopefully it communicates the purpose of hw/pci-host/remote.c. :P

hw/pci-host/remote.c line 15

Quoted 7 lines
hw/pci-host/remote.c new file +7 -0
+typedef struct RemotePCIHost {
+    /*< private >*/
+    PCIExpressHost parent_obj;
+    /*< public >*/
+
+    MemoryRegion *mr_pci_mem;
+    MemoryRegion *mr_sys_mem;

Unused? Please add mr_sys_mem if and when it is used.

#9 lore raw

Re: [PATCH v7 04/21] multi-process: setup a machine object for remote device process

In reply to [email protected]

hw/i386/remote.c line 7

Quoted 13 lines · On Sat, Jun 27, 2020 at 10:09:26AM -0700, [email protected] wrote:
hw/i386/remote.c new file +7 -0
new file mode 100644
index 0000000000..4d13abe9f3
@@ -0,0 +1,64 @@
+/*
+ * Machine for remote device
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.

A suggestion for expanding the comment:

This machine type is used by the remote device process in multi-process QEMU. QEMU device models depend on parent busses, interrupt controllers, memory regions, etc. The remote machine type offers this environment so that QEMU device models can be used as remote devices.

hw/i386/remote.c line 12

Quoted 5 lines
hw/i386/remote.c new file +5 -0
+typedef struct RemMachineState {
+    MachineState parent_obj;
+
+    RemotePCIHost *host;
+} RemMachineState;

s/RemMachineState/RemoteMachineState/ for consistency with "remote-machine" and RemotePCIHost.

Reviewed-by on v7 5/21
#11 lore raw

Re: [PATCH v7 05/21] multi-process: add qio channel function to transmit

In reply to [email protected]
Quoted 5 lines · On Sat, Jun 27, 2020 at 10:09:27AM -0700, [email protected] wrote:

From: Elena Ufimtseva <[email protected]>

The entire array of the memory regions and file handlers. Will be used in the next patch.

Quoted 7 lines
---
 include/io/channel.h | 24 +++++++++++++++++++++++
 io/channel.c         | 45 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)
#13 lore raw

Re: [PATCH v7 06/21] multi-process: define MPQemuMsg format and transmission functions

In reply to [email protected]
Quoted 13 lines · On Sat, Jun 27, 2020 at 10:09:28AM -0700, [email protected] wrote:
io/mpqemu-link.c new file +13 -0
+void mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc)
+{
+    Error *local_err = NULL;
+    struct iovec send[2];
+    int *fds = NULL;
+    size_t nfds = 0;
+
+    send[0].iov_base = msg;
+    send[0].iov_len = MPQEMU_MSG_HDR_SIZE;
+
+    send[1].iov_base = msg->bytestream ? msg->data2 : (void *)&msg->data1;
+    send[1].iov_len = msg->size;
+

io/mpqemu-link.c line 42

Quoted 10 lines
io/mpqemu-link.c new file +10 -0
+    if (msg->num_fds) {
+        nfds = msg->num_fds;
+        fds = msg->fds;
+    }
+
+    (void)qio_channel_writev_full_all(ioc, send, G_N_ELEMENTS(send), fds, nfds,
+                                      &local_err);
+    if (local_err) {
+        error_report_err(local_err);
+    }

Error propagation is missing. Is this by design, i.e. errors only need to be handled in the read path, or is the patch incomplete?

io/mpqemu-link.c line 53

Quoted 11 lines
io/mpqemu-link.c new file +11 -0
+}
+
+static int mpqemu_readv(QIOChannel *ioc, struct iovec *iov, int **fds,
+                        size_t *nfds, Error **errp)
+{
+    size_t size, len;
+
+    size = iov->iov_len;
+
+    while (size > 0) {
+        len = qio_channel_readv_full(ioc, iov, 1, fds, nfds, errp);

iovec buffers are overwritten when this loop iterates because iov is not updated after reading some data.

fds is leaked when this loop iterates multiple times and there are fds each time. fds/nfds should probably be set to 0 after the first iteration.

Quoted 22 lines
io/mpqemu-link.c new file +22 -0
+
+        if (len == QIO_CHANNEL_ERR_BLOCK) {
+            if (qemu_in_coroutine()) {
+                qio_channel_yield(ioc, G_IO_IN);
+            } else {
+                qio_channel_wait(ioc, G_IO_IN);
+            }
+            continue;
+        }
+
+        if (len <= 0) {
+            return -EIO;
+        }
+
+        size -= len;
+    }
+
+    return iov->iov_len;
+}
+
+int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc)
+{

io/mpqemu-link.c line 85

Quoted 10 lines
io/mpqemu-link.c new file +10 -0
+    Error *local_err = NULL;
+    int *fds = NULL;
+    struct iovec hdr, data;
+    size_t nfds = 0;
+
+    hdr.iov_base = g_malloc0(MPQEMU_MSG_HDR_SIZE);
+    hdr.iov_len = MPQEMU_MSG_HDR_SIZE;
+
+    if (mpqemu_readv(ioc, &hdr, &fds, &nfds, &local_err) < 0) {
+        return -EIO;

hdr.iov_base is leaked.

Why is hdr.iov_base allocated on the heap? Can you read directly into msg instead of using an extra variable?

Quoted 6 lines
io/mpqemu-link.c new file +6 -0
+    }
+
+    memcpy(msg, hdr.iov_base, hdr.iov_len);
+
+    free(hdr.iov_base);
+    if (msg->size > MPQEMU_MSG_DATA_MAX) {

io/mpqemu-link.c line 101

Quoted 10 lines
io/mpqemu-link.c new file +10 -0
+        error_report("The message size is more than MPQEMU_MSG_DATA_MAX %d",
+                     MPQEMU_MSG_DATA_MAX);
+        return -EINVAL;
+    }
+
+    data.iov_base = g_malloc0(msg->size);
+    data.iov_len = msg->size;
+
+    if (mpqemu_readv(ioc, &data, NULL, NULL, &local_err) < 0) {
+        return -EIO;

data.iov_base is leaked.

io/mpqemu-link.c line 105

Quoted 4 lines
io/mpqemu-link.c new file +4 -0
+    }
+
+    if (msg->bytestream) {
+        msg->data2 = calloc(1, msg->size);

QEMU uses glib memory allocation functions when possible. Please use g_malloc0().

The calloc() and memcpy() can be avoided like this:

msg->data2 = data.iov_base;
data.iov_base = NULL; /* the pointer has been moved, don't free it */

io/mpqemu-link.c line 108

Quoted 3 lines
io/mpqemu-link.c new file +3 -0
+        memcpy(msg->data2, data.iov_base, msg->size);
+    } else {
+        memcpy((void *)&msg->data1, data.iov_base, msg->size);

Buffer overflow when msg->size > sizeof(msg->data1). There should be a check:

  if (msg->bytestream) {
      ...
  } else {
      if (msg->size > sizeof(msg->data1)) {
          ...handle invalid size...
	  return -EIO;
      }
      memcpy(&msg->data1, data.iov_base, msg->size);

}

io/mpqemu-link.c line 111

Quoted 3 lines
io/mpqemu-link.c new file +3 -0
+    }
+
+    free(data.iov_base);

Should be g_free() since the allocation was made with g_malloc0().

io/mpqemu-link.c line 116

Quoted 5 lines
io/mpqemu-link.c new file +5 -0
+
+    if (nfds) {
+        msg->num_fds = nfds;
+        memcpy(msg->fds, fds, nfds * sizeof(int));
+    }

It's safer to move the msg->num_fds = nfds outside the if statement so that it always happens. That way num_fds is set to 0 and we don't depend on the caller initializing it to 0.

#15 lore raw

Re: [PATCH v7 07/21] multi-process: add co-routines to communicate with remote

In reply to [email protected]
Quoted 4 lines · On Sat, Jun 27, 2020 at 10:09:29AM -0700, [email protected] wrote:

From: Elena Ufimtseva <[email protected]>

process to avoid blocking the main loop during the message exchanges. To be used by proxy device.

The nested aio_poll() in this patch could be a problem. I've highlighted it here so that others skimming through this series can join the discussion without reading all my review comments:

  qemu_coroutine_enter(req.co);
  while (!req.finished) {
      aio_poll(qemu_get_aio_context(), false);

This is called from the proxy device, which means the vCPU thread. The QEMU global mutex is held here.

aio_poll() does not release the mutex, so other vCPU threads will not be able to run QEMU code until mpqemu_msg_send_reply_co() completes and the QEMU global mutex has been released again.

Our goal is to do blocking socket I/O here since our vCPU thread cannot make progress until the remote device replies. This means we can forget about QEMU's event loop and simply do blocking I/O from the vCPU thread. The QEMU global mutex can be dropped around the send/recv but a per-socket mutex is needed to protect against multiple vCPU threads interleaving requests.

I think this boils down to:

/*
* Blocking version of send+recv for the proxy device. Called from
* vCPU thread where we cannot make progress until the remote device
* has replied. Releases and re-acquires the QEMU global mutex. */
uint64_t mpqemu_msg_send_and_await_reply(MPQemuMsg *msg, QIOChannel *ioc,
Error **errp)

  {
      uint64_t ret;

      qemu_mutex_unlock_iothread();

      qemu_mutex_lock(&mplink->io_mutex);
      blocking_send(ioc, msg);
      ret = blocking_recv(ioc);
      qemu_mutex_unlock(&mplink->io_mutex);

      qemu_mutex_lock_iothread();

      return ret;

}

This is very simple! The tricky thing is combining this approach with mplink I/O that happens in the event loop. The heartbeat is one example. I don't remember the protocol details enough to know how the other message exchanges work. Code running in the event loop is not allowed to block, so it really needs to use coroutines.

Quoted 35 lines
---
 include/io/mpqemu-link.h | 16 +++++++++
 io/mpqemu-link.c         | 78 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+)
include/io/mpqemu-link.h +4 -0
index 1542e8ed07..52aa89656c 100644
@@ -17,6 +17,7 @@
 #include "qom/object.h"
 #include "qemu/thread.h"
 #include "io/channel.h"
+#include "io/channel-socket.h"
 
 #define REMOTE_MAX_FDS 8
 
@@ -30,6 +31,7 @@
  */
 typedef enum {
     INIT = 0,
+    RET_MSG,
     MAX = INT_MAX,
 } MPQemuCmd;
 
@@ -67,6 +69,20 @@ typedef struct {
     uint8_t *data2;
 } MPQemuMsg;
 
+struct MPQemuRequest {
+    MPQemuMsg *msg;

include/io/mpqemu-link.h typedef struct { · line 83

Quoted 10 lines
include/io/mpqemu-link.h +10 -0
+    QIOChannelSocket *sioc;
+    Coroutine *co;
+    bool finished;
+    int error;
+    long ret;
+};
+
+typedef struct MPQemuRequest MPQemuRequest;
+
+uint64_t mpqemu_msg_send_reply_co(MPQemuMsg *msg, QIOChannel *ioc,

This function sends a message and waits for the reply. The name confuses me, I would guess that this function sends a reply. Perhaps mpqemu_msg_send_and_await_reply() is a clearer name.

include/io/mpqemu-link.h typedef struct { · line 84

Quoted 1 line
include/io/mpqemu-link.h +1 -0
+                                  Error **errp);

Functions with "co" in their name run in coroutine context. This function does not. Please remove the _co from the name to avoid confusion.

Quoted 21 lines
include/io/mpqemu-link.h +1 -0
+
 void mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc);
 int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc);
 
io/mpqemu-link.c +2 -0
index bfc542b5fd..c430b4d6a2 100644
@@ -16,6 +16,8 @@
 #include "qapi/error.h"
 #include "qemu/iov.h"
 #include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "io/channel-socket.h"
 
 void mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc)
 {
@@ -118,6 +120,82 @@ int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc)
     return 0;
 }
 

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 132

Quoted 10 lines
io/mpqemu-link.c +10 -0
+/* Use in proxy only as it clobbers fd handlers. */
+static void coroutine_fn mpqemu_msg_send_co(void *data)
+{
+    MPQemuRequest *req = (MPQemuRequest *)data;
+    MPQemuMsg msg_reply = {0};
+    long ret = -EINVAL;
+
+    if (!req->sioc) {
+        error_report("No channel available to send command %d",
+                     req->msg->cmd);

Can this ever happen?

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 133

Quoted 1 line
io/mpqemu-link.c +1 -0
+        atomic_mb_set(&req->finished, true);

Why is atomic_mb_set() used here?

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 143

Quoted 10 lines
io/mpqemu-link.c +10 -0
+        req->error = -EINVAL;
+        return;
+    }
+
+    req->co = qemu_coroutine_self();
+    mpqemu_msg_send(req->msg, QIO_CHANNEL(req->sioc));
+
+    yield_until_fd_readable(req->sioc->fd);
+
+    ret = mpqemu_msg_recv(&msg_reply, QIO_CHANNEL(req->sioc));

mpqemu_msg_recv() already yields when called from a coroutine. Why is yield_until_fd_readable() called explicitly here?

After removing the yield_until_fd_readable() it will also be possible to use QIOChannel instead of the more specific QIOChannelSocket. That will make the code a little simpler because the QIO_CHANNEL() casts can be removed.

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 146

Quoted 3 lines
io/mpqemu-link.c +3 -0
+    if (ret < 0) {
+        error_report("ERROR: failed to get a reply for command %d, \
+                     errno %s, ret is %ld",

C doesn't support backslash string wrapping in a useful way. The backslash tells the preprocessor to wrap the line but the string will contain the leading whitespace.

This format string is equivalent to:

"ERROR: failed to get a reply for command %d, errno %s, ret is %ld"

A common way of wrapping strings is:

error_report("ERROR: failed to get a reply for command %d, "

               "errno %s, ret is %ld",

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 148

Quoted 2 lines
io/mpqemu-link.c +2 -0
+                     req->msg->cmd, strerror(errno), ret);
+        req->error = -errno;

s/errno/ret/

Quoted 6 lines
io/mpqemu-link.c +6 -0
+    } else {
+        if (!mpqemu_msg_valid(&msg_reply) || msg_reply.cmd != RET_MSG) {
+            error_report("ERROR: Invalid reply received for command %d",
+                         req->msg->cmd);
+            req->error = -EINVAL;
+        } else {

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 164

Quoted 10 lines
io/mpqemu-link.c +10 -0
+            req->ret = msg_reply.data1.u64;
+        }
+    }
+    atomic_mb_set(&req->finished, true);
+}
+
+/*
+ * Create if needed and enter co-routine to send the message to the
+ * remote channel ioc and wait for the reply.
+ * Resturns the value from the reply message, sets the error on failure.

s/Resturns/Returns/

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 175

Quoted 11 lines
io/mpqemu-link.c +11 -0
+ */
+
+uint64_t mpqemu_msg_send_reply_co(MPQemuMsg *msg, QIOChannel *ioc,
+                                  Error **errp)
+{
+    MPQemuRequest req = {0};
+    uint64_t ret = UINT64_MAX;
+
+    req.sioc = QIO_CHANNEL_SOCKET(ioc);
+    if (!req.sioc) {
+        return ret;

The doc comment says "sets the error on failure" but this error return does not set errp.

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 182

Quoted 7 lines
io/mpqemu-link.c +7 -0
+    }
+
+    req.msg = msg;
+    req.ret = 0;
+    req.finished = false;
+
+    if (!req.co) {

req.co is always NULL here because it's a local variable initialized to 0 at the beginning of this function.

io/mpqemu-link.c int mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc) · line 188

Quoted 6 lines
io/mpqemu-link.c +6 -0
+        req.co = qemu_coroutine_create(mpqemu_msg_send_co, &req);
+    }
+
+    qemu_coroutine_enter(req.co);
+    while (!req.finished) {
+        aio_poll(qemu_get_aio_context(), false);

The second argument must be true so that aio_poll() waits for activity instead of returning immediately. If it returns immediately then this loop will consume 100% CPU.

Quoted 16 lines
io/mpqemu-link.c +10 -0
+    }
+    if (req.error) {
+        error_setg(errp, "Error exchanging message with remote process, "\
+                        "socket %d, error %d", req.sioc->fd, req.error);
+    }
+    ret = req.ret;
+
+    return ret;
+}
+
 bool mpqemu_msg_valid(MPQemuMsg *msg)
 {
     if (msg->cmd >= MAX && msg->cmd < 0) {
Signature · 1 line
2.25.GIT
#17 lore raw

Re: [PATCH v7 08/21] multi-process: Initialize communication channel at the remote end

In reply to [email protected]

hw/i386/remote.c static void remote_machine_init(MachineState *machine) · line 52

Quoted 11 lines · On Sat, Jun 27, 2020 at 10:09:30AM -0700, [email protected] wrote:
hw/i386/remote.c +7 -0
@@ -42,6 +43,20 @@ static void remote_machine_init(MachineState *machine)
     qdev_realize(DEVICE(rem_host), sysbus_get_default(), &error_fatal);
 }
 
+static void remote_set_socket(Object *obj, const char *str, Error **errp)
+{
+    RemMachineState *s = REMOTE_MACHINE(obj);
+    Error *local_err = NULL;
+    int fd = atoi(str);
+
+    s->ioc = qio_channel_new_fd(fd, &local_err);

Missing error handling and local_err is leaked. errp should be set.

hw/i386/remote.c static void remote_machine_init(MachineState *machine) · line 58

Quoted 6 lines
hw/i386/remote.c +6 -0
+}
+
+static void remote_instance_init(Object *obj)
+{
+    object_property_add_str(obj, "socket", NULL, remote_set_socket);
+}

The name "socket" does not communicate the structure of the value. Is it a <host>:<port> pair, a UNIX domain socket path, a file descriptor, etc? It's common to name file descriptor arguments with an "fd" suffix (e.g. vhostfd) and that would work here too.

Please also include a help string with

  object_property_set_description(obj, property_name, help_text);

The help string when QEMU is invoked like this:

  $ qemu-system-x86_64 -M remote,\?
#19 lore raw

Re: [PATCH v7 09/21] multi-process: Initialize message handler in remote device

In reply to [email protected]
Quoted 14 lines · On Sat, Jun 27, 2020 at 10:09:31AM -0700, [email protected] wrote:
hw/i386/remote-msg.c new file +8 -0
new file mode 100644
index 0000000000..58e24ab2ad
@@ -0,0 +1,52 @@
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+
+#include "hw/i386/remote.h"
+#include "io/channel.h"
+#include "io/mpqemu-link.h"
+#include "qapi/error.h"
+#include "sysemu/runstate.h"

hw/i386/remote-msg.c line 18

Quoted 10 lines
hw/i386/remote-msg.c new file +10 -0
+
+gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
+                            gpointer opaque)
+{
+    Error *local_err = NULL;
+    MPQemuMsg msg = { 0 };
+
+    if (cond & G_IO_HUP) {
+        qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
+    }

Missing return FALSE?

hw/i386/remote-msg.c line 22

Quoted 4 lines
hw/i386/remote-msg.c new file +4 -0
+
+    if (cond & (G_IO_ERR | G_IO_NVAL)) {
+        error_setg(&local_err, "Error %d while processing message from proxy \
+                   in remote process pid=%d", errno, getpid());

This sets and leaks local_err. Should this be error_report()?

hw/i386/remote-msg.c line 34

Quoted 12 lines
hw/i386/remote-msg.c new file +12 -0
+        return FALSE;
+    }
+
+    if (mpqemu_msg_recv(&msg, ioc) < 0) {
+        return FALSE;
+    }
+
+    if (!mpqemu_msg_valid(&msg)) {
+        error_report("Received invalid message from proxy \
+                     in remote process pid=%d", getpid());
+        return TRUE;
+    }

Why return TRUE here but FALSE in previous error cases?

hw/i386/remote-msg.c line 44

Quoted 10 lines
hw/i386/remote-msg.c new file +10 -0
+
+    switch (msg.cmd) {
+    default:
+        error_setg(&local_err, "Unknown command (%d) received from proxy \
+                   in remote process pid=%d", msg.cmd, getpid());
+    }
+
+    if (msg.data2) {
+        free(msg.data2);
+    }

A mpqemu_msg_cleanup() function in mpqemu-link.h would be nice. That way the code that frees data2 can be next to the code that allocates it.

Do passed file descriptors need to be closed too (especially in cases where the command normally does not expect passed file descriptors)?

#21 lore raw

Re: [PATCH v7 10/21] multi-process: setup memory manager for remote device

In reply to [email protected]
Quoted 7 lines · On Sat, Jun 27, 2020 at 10:09:32AM -0700, [email protected] wrote:
hw/i386/remote-memory.c new file +7 -0
+void remote_sysmem_reconfig(MPQemuMsg *msg, Error **errp)
+{
+    SyncSysmemMsg *sysmem_info = &msg->data1.sync_sysmem;
+    MemoryRegion *sysmem, *subregion, *next;
+    static unsigned int suffix;
+    Error *local_err = NULL;
+    char *name;

hw/i386/remote-memory.c line 35

Quoted 10 lines
hw/i386/remote-memory.c new file +10 -0
+    int region;
+
+    sysmem = get_system_memory();
+
+    memory_region_transaction_begin();
+
+    QTAILQ_FOREACH_SAFE(subregion, &sysmem->subregions, subregions_link, next) {
+        if (subregion->ram) {
+            memory_region_del_subregion(sysmem, subregion);
+            qemu_ram_free(subregion->ram_block);

Why is qemu_ram_free() called explicitly? Normally the only caller is memory_region_destructor_ram().

MemoryRegions are reference counted. The qemu_ram_free() call should probably be replaced with memory_region_unref(subregion). This also solves the subregion memory leak.

That said, I haven't read the MemoryRegion lifecycle code so please check that what I'm suggesting is correct.

include/io/mpqemu-link.h line 47

Quoted 5 lines
include/io/mpqemu-link.h +5 -0
+typedef struct {
+    hwaddr gpas[REMOTE_MAX_FDS];
+    uint64_t sizes[REMOTE_MAX_FDS];
+    off_t offsets[REMOTE_MAX_FDS];
+} SyncSysmemMsg;

It's safer to use fixed-width types like uint64_t for external communication, but in this case we expect the client and server to match.

#23 lore raw

Re: [PATCH v7 11/21] multi-process: introduce proxy object

In reply to [email protected]
Quoted 55 lines · On Sat, Jun 27, 2020 at 10:09:33AM -0700, [email protected] wrote:

From: Elena Ufimtseva <[email protected]>

Defines a PCI Device proxy object as a child of TYPE_PCI_DEVICE.

---
 MAINTAINERS            |  2 ++
 hw/pci/Makefile.objs   |  1 +
 hw/pci/proxy.c         | 70 ++++++++++++++++++++++++++++++++++++++++++
 include/hw/pci/proxy.h | 43 ++++++++++++++++++++++++++
 4 files changed, 116 insertions(+)
 create mode 100644 hw/pci/proxy.c
 create mode 100644 include/hw/pci/proxy.h
MAINTAINERS +2 -0
index 017c96eace..b48c3114c1 100644
@@ -2952,6 +2952,8 @@ F: include/io/mpqemu-link.h
 F: hw/i386/remote-msg.c
 F: include/hw/i386/remote-memory.h
 F: hw/i386/remote-memory.c
+F: hw/pci/proxy.c
+F: include/hw/pci/proxy.h
 
 Build and test automation
 -------------------------
hw/pci/Makefile.objs +1 -0
index c78f2fb24b..515dda506c 100644
@@ -12,3 +12,4 @@ common-obj-$(CONFIG_PCI_EXPRESS) += pcie_port.o pcie_host.o
 
 common-obj-$(call lnot,$(CONFIG_PCI)) += pci-stub.o
 common-obj-$(CONFIG_ALL) += pci-stub.o
+obj-$(CONFIG_MPQEMU) += proxy.o
hw/pci/proxy.c new file +11 -0
new file mode 100644
index 0000000000..6d62399c52
@@ -0,0 +1,70 @@
+/*
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+

hw/pci/proxy.c line 21

Quoted 10 lines
hw/pci/proxy.c new file +10 -0
+#include "hw/pci/proxy.h"
+#include "hw/pci/pci.h"
+#include "qapi/error.h"
+#include "io/channel-util.h"
+#include "hw/qdev-properties.h"
+#include "monitor/monitor.h"
+
+static void proxy_set_socket(PCIProxyDev *pdev, int fd, Error **errp)
+{
+    pdev->com = qio_channel_new_fd(fd, errp);

The caller needs to close(fd) on failure:

  if (!pdev->com) {
      close(fd);
  }

hw/pci/proxy.c line 22

Quoted 1 line
hw/pci/proxy.c new file +1 -0
+}

pdev->com is never freed. It seems like hotplug should be possible eventually. Implementing ->unrealize() from the start will make that easier because it will be necessary to think through the lifecycle of this object.

hw/pci/proxy.c line 34

Quoted 12 lines
hw/pci/proxy.c new file +12 -0
+
+static Property proxy_properties[] = {
+    DEFINE_PROP_STRING("fd", PCIProxyDev, fd),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void pci_proxy_dev_realize(PCIDevice *device, Error **errp)
+{
+    PCIProxyDev *dev = PCI_PROXY_DEV(device);
+    int proxyfd;
+
+    if (dev->fd) {

Does it make sense to succeed without fd? If not then errp should be set so the caller knows .realize() failed.

hw/pci/proxy.c line 37

Quoted 3 lines
hw/pci/proxy.c new file +3 -0
+        proxyfd = monitor_fd_param(cur_mon, dev->fd, errp);
+        if (proxyfd == -1) {
+            error_prepend(errp, "proxy: unable to parse proxyfd: ");

The user-visible property name is "fd":
s/proxyfd/fd/

hw/pci/proxy.c line 44

Quoted 7 lines
hw/pci/proxy.c new file +7 -0
+typedef struct PCIProxyDevClass {
+    PCIDeviceClass parent_class;
+
+    void (*realize)(PCIProxyDev *dev, Error **errp);
+
+    char *command;
+} PCIProxyDevClass;

Neither ->realize nor ->command are used in this patch. Please drop PCIProxyDevClass for now. If these fields are used later on then they should be introduced in the patches that need them.

#25 lore raw

Re: [PATCH v7 12/21] multi-process: Connect Proxy Object with device in the remote process

In reply to [email protected]
Quoted 4 lines · On Sat, Jun 27, 2020 at 10:09:34AM -0700, [email protected] wrote:

From: Jagannathan Raman <[email protected]>

Send a message to the remote process to connect PCI device with the corresponding Proxy object in QEMU

I thought the protocol was simplified to a 1:1 device:socket model, but this patch seems to implement an N:1 model?

In a 1:1 model the CONNECT_DEV message is not necessary because each socket is already associated with a specific remote device (e.g. qemu -M remote -object mplink,dev=lsi-scsi-1,sockpath=/tmp/lsi-scsi-1.sock). Connecting to the socket already indicates which device we are talking to.

The N:1 model will work but it's more complex. There is a main socket that is used for CONNECT_DEV (anything else?) and we need to worry about the lifecycle of the per-device sockets that are passed over the main socket.

Quoted 5 lines
hw/i386/remote-msg.c +1 -0
@@ -50,3 +58,34 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
 
     return TRUE;
 }
+

hw/i386/remote-msg.c gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond, · line 71

Quoted 10 lines
hw/i386/remote-msg.c +10 -0
+static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
+                                    Error **errp)
+{
+    char *devid = (char *)msg->data2;
+    QIOChannel *dioc = NULL;
+    DeviceState *dev = NULL;
+    MPQemuMsg ret = { 0 };
+    int rc = 0;
+
+    g_assert(devid && (devid[msg->size - 1] == '\0'));

Asserts are not suitable for external input validation since a failure aborts the program and lets the client cause a denial-of-service. When there are multiple clients, one misbehaved client shouldn't be able to kill the server. Please validate devid using an if statement and set errp on failure.

Can msg->size be 0? If yes, this code accesses before the beginning of the buffer.

hw/i386/remote-msg.c gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond, · line 79

Quoted 8 lines
hw/i386/remote-msg.c +8 -0
+
+    dev = qdev_find_recursive(sysbus_get_default(), devid);
+    if (!dev || !object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
+        rc = 0xff;
+        goto exit;
+    }
+
+    dioc = qio_channel_new_fd(msg->fds[0], errp);

Missing error handling if qio_channel_new_fd() fails. We need to close(msg->fds[0]) ourselves in this case.

Quoted 24 lines
hw/i386/remote-msg.c +12 -0
+
+    qio_channel_add_watch(dioc, G_IO_IN | G_IO_HUP, mpqemu_process_msg,
+                          (void *)dev, NULL);
+
+exit:
+    ret.cmd = RET_MSG;
+    ret.bytestream = 0;
+    ret.data1.u64 = rc;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, com);
+}
hw/pci/proxy.c +1 -0
index 6d62399c52..16649ed0ec 100644
@@ -15,10 +15,38 @@
 #include "io/channel-util.h"
 #include "hw/qdev-properties.h"
 #include "monitor/monitor.h"
+#include "io/mpqemu-link.h"
 
 static void proxy_set_socket(PCIProxyDev *pdev, int fd, Error **errp)
 {

hw/pci/proxy.c line 31

Quoted 10 lines
hw/pci/proxy.c +9 -0
+    DeviceState *dev = DEVICE(pdev);
+    MPQemuMsg msg = { 0 };
+    int fds[2];
+    Error *local_err = NULL;
+
     pdev->com = qio_channel_new_fd(fd, errp);
+
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) {
+        error_setg(errp, "Failed to create proxy channel with fd %d", fd);
+        return;

pdev->com needs to be cleaned up.

io/mpqemu-link.c bool mpqemu_msg_valid(MPQemuMsg *msg) · line 240

Quoted 12 lines
io/mpqemu-link.c +4 -0
index 5887c8c6c0..54df3b254e 100644
@@ -234,6 +234,14 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
             return false;
         }
         break;
+    case CONNECT_DEV:
+        if ((msg->num_fds != 1) ||
+            (msg->fds[0] == -1) ||
+            (msg->fds[0] == -1) ||

This line is duplicated.

#31 lore raw

Re: [PATCH v7 13/21] multi-process: Forward PCI config space acceses to the remote process

In reply to [email protected]

hw/i386/remote-msg.c gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond, · line 56

Quoted 10 lines · On Sat, Jun 27, 2020 at 10:09:35AM -0700, [email protected] wrote:
hw/i386/remote-msg.c +6 -0
@@ -42,6 +48,12 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case CONNECT_DEV:
         process_connect_dev_msg(&msg, ioc, &local_err);
         break;
+    case PCI_CONFIG_WRITE:
+        process_config_write(ioc, pci_dev, &msg);
+        break;
+    case PCI_CONFIG_READ:
+        process_config_read(ioc, pci_dev, &msg);
+        break;

pci_dev is NULL when mpqemu_process_msg() is called on the main socket. This is an example of how the N:1 model complicates things. Now process_config_read/write() need to check that pci_dev is non-NULL to avoid crashing.

Quoted 5 lines
hw/i386/remote-msg.c +0 -0
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -89,3 +101,45 @@ exit:
 

hw/i386/remote-msg.c exit: · line 111

Quoted 10 lines
hw/i386/remote-msg.c +8 -0
     mpqemu_msg_send(&ret, com);
 }
+
+static void process_config_write(QIOChannel *ioc, PCIDevice *dev,
+                                 MPQemuMsg *msg)
+{
+    struct conf_data_msg *conf = (struct conf_data_msg *)msg->data2;
+    MPQemuMsg ret = { 0 };
+
+    if (conf->addr >= PCI_CFG_SPACE_EXP_SIZE) {

This check treats all devices as PCIe devices. Traditional PCI devices have a smaller config space and pci_default_write_config() has an assertion that fails on out-of-bounds writes:

  assert(addr + l <= pci_config_size(d));

Are you sure all devices are PCIe? If yes, please enforce that in the code. If no, then please fix the size check.

include/io/mpqemu-link.h typedef struct { · line 56

Quoted 5 lines
include/io/mpqemu-link.h +5 -0
+struct conf_data_msg {
+    uint32_t addr;
+    uint32_t val;
+    int l;
+};

QEMU coding style uses typedefs:

  typedef struct {
      uint32_t addr;
      uint32_t val;
      int l;
  } ConfDataMsg;
#33 lore raw

Re: [PATCH v7 14/21] multi-process: PCI BAR read/write handling for proxy & remote endpoints

In reply to [email protected]

hw/i386/remote-msg.c gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond, · line 65

Quoted 10 lines · On Sat, Jun 27, 2020 at 10:09:36AM -0700, [email protected] wrote:
hw/i386/remote-msg.c +6 -0
@@ -54,6 +57,12 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case PCI_CONFIG_READ:
         process_config_read(ioc, pci_dev, &msg);
         break;
+    case BAR_WRITE:
+        process_bar_write(ioc, &msg, &local_err);
+        break;
+    case BAR_READ:
+        process_bar_read(ioc, &msg, &local_err);
+        break;

These functions are more than BAR read/write functions, they are general address space read/write functions. This is could be a security problem in the future because the client has access to more than just the PCI device they are supposed to communicate with.

I don't have a strong opinion against leaving it as-is, but wanted to mention it because the current approach is risky as a long-term solution. The protocol message could have a uint8_t bar_index field or the remote device could validate that addr falls within the device BARs.

Quoted 15 lines
hw/i386/remote-msg.c +8 -0
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
@@ -143,3 +152,89 @@ static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
 
     mpqemu_msg_send(&ret, ioc);
 }
+
+static void process_bar_write(QIOChannel *ioc, MPQemuMsg *msg, Error **errp)
+{
+    BarAccessMsg *bar_access = &msg->data1.bar_access;
+    AddressSpace *as =
+        bar_access->memory ? &address_space_memory : &address_space_io;
+    MPQemuMsg ret = { 0 };
+    MemTxResult res;

hw/i386/remote-msg.c static void process_config_read(QIOChannel *ioc, PCIDevice *dev, · line 172

Quoted 10 lines
hw/i386/remote-msg.c +10 -0
+
+    if (!is_power_of_2(bar_access->size) ||
+       (bar_access->size > sizeof(uint64_t))) {
+        ret.data1.u64 = UINT64_MAX;
+        goto fail;
+    }
+
+    res = address_space_rw(as, bar_access->addr, MEMTXATTRS_UNSPECIFIED,
+                           (void *)&bar_access->val, bar_access->size,
+                           true);

(void *)&bar_access->val doesn't work on big-endian hosts. A uint64_t -> {uint32_t, uint16_t, uint8_t} conversion must be performed before the address_space_rw() call analogous to what process_bar_read() does.

Although it's unlikely that this will be run on big-endian hosts anytime soon, it's good practice to keep the code portable when possible.

io/mpqemu-link.c bool mpqemu_msg_valid(MPQemuMsg *msg) · line 263

Quoted 5 lines
io/mpqemu-link.c +5 -0
+    case BAR_WRITE:
+    case BAR_READ:
+        if (msg->size != sizeof(msg->data1)) {
+            return false;
+        }

Is there a check that the number of passed fds is zero somewhere?

#35 lore raw

Re: [PATCH v7 15/21] multi-process: Synchronize remote memory

In reply to [email protected]
Quoted 18 lines · On Sat, Jun 27, 2020 at 10:09:37AM -0700, [email protected] wrote:
hw/pci/memory-sync.c new file +18 -0
+static bool try_merge(RemoteMemSync *sync, MemoryRegionSection *section)
+{
+    uint64_t mrs_size, mrs_gpa, mrs_page;
+    MemoryRegionSection *prev_sec;
+    bool merged = false;
+    uintptr_t mrs_host;
+    RAMBlock *mrs_rb;
+
+    if (!sync->n_mr_sections) {
+        return false;
+    }
+
+    mrs_rb = section->mr->ram_block;
+    mrs_page = (uint64_t)qemu_ram_pagesize(mrs_rb);
+    mrs_size = int128_get64(section->size);
+    mrs_gpa = section->offset_within_address_space;
+    mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) +
+               section->offset_within_region;

hw/pci/memory-sync.c line 101

Quoted 10 lines
hw/pci/memory-sync.c new file +10 -0
+
+    if (get_fd_from_hostaddr(mrs_host, NULL) < 0) {
+        return true;
+    }
+
+    mrs_host = mrs_host & ~(mrs_page - 1);
+    mrs_gpa = mrs_gpa & ~(mrs_page - 1);
+    mrs_size = ROUND_UP(mrs_size, mrs_page);
+
+    if (sync->n_mr_sections) {

The check is unnecessary because of the if statement above:

  if (!sync->n_mr_sections) {
     return false;
  }
Quoted 9 lines
hw/pci/memory-sync.c new file +9 -0
+static void proxy_ml_commit(MemoryListener *listener)
+{
+    RemoteMemSync *sync = container_of(listener, RemoteMemSync, listener);
+    MPQemuMsg msg;
+    MemoryRegionSection section;
+    ram_addr_t offset;
+    uintptr_t host_addr;
+    int region;
+

hw/pci/memory-sync.c line 173

Quoted 10 lines
hw/pci/memory-sync.c new file +10 -0
+    memset(&msg, 0, sizeof(MPQemuMsg));
+
+    msg.cmd = SYNC_SYSMEM;
+    msg.bytestream = 0;
+    msg.num_fds = sync->n_mr_sections;
+    msg.size = sizeof(msg.data1);
+    assert(msg.num_fds <= REMOTE_MAX_FDS);
+
+    for (region = 0; region < sync->n_mr_sections; region++) {
+        section = sync->mr_sections[region];

Why is section copied here? It's conventional to take a pointer to a struct instead of copying its value.

hw/pci/memory-sync.c line 186

Quoted 13 lines
hw/pci/memory-sync.c new file +13 -0
+        msg.data1.sync_sysmem.gpas[region] =
+            section.offset_within_address_space;
+        msg.data1.sync_sysmem.sizes[region] = int128_get64(section.size);
+        host_addr = (uintptr_t)memory_region_get_ram_ptr(section.mr) +
+                    section.offset_within_region;
+        msg.fds[region] = get_fd_from_hostaddr(host_addr, &offset);
+        msg.data1.sync_sysmem.offsets[region] = offset;
+    }
+    mpqemu_msg_send(&msg, sync->ioc);
+}
+
+void deconfigure_memory_sync(RemoteMemSync *sync)
+{

Missing proxy_ml_begin() call to free sync->mr_sections[] and unref sections.

Review

Quoted 2 lines
(quoted diff) +2 -0
+    memory_listener_unregister(&sync->listener);
+}

This function isn't called. It's good practice to implement the object lifecycle from the start.

#37 lore raw

Re: [PATCH v7 16/21] multi-process: create IOHUB object to handle irq

In reply to [email protected]
Quoted 2 lines · On Sat, Jun 27, 2020 at 10:09:38AM -0700, [email protected] wrote:

From: Jagannathan Raman <[email protected]>

CCing Alex Williamson in case he has time to think about how PCI INTx interrupts should work in multi-process QEMU where the device emulation runs as a separate process. I don't know enough about interrupts to give this a full review.

Quoted 4 lines

IOHUB object is added to manage PCI IRQs. It uses KVM_IRQFD ioctl to create irqfd to injecting PCI interrupts to the guest. IOHUB object forwards the irqfd to the remote process. Remote process uses this fd to directly send interrupts to the guest, bypassing QEMU.

Each PCIDevice has exactly one legacy interrupt pin. This raises two questions:

1. Why perform interrupt mapping in the remote device program? The IOHUB object really only needs a PCIDevice -> irqfd lookup so it can set the correct EventNotifier when the device raises an interrupt.

The mapping scheme in this patch can suffer from collisions when multiple devices are attached to an IOHUB. On a physical machine that results in shared legacy interrupts, but there is no need to recreate that in the remote device program since it's a guest issue, not a device issue. When a collision occurs this patch doesn't seem to share the irqfd for both devices. It overwrites the irqfd so the older device stops getting interrupts!

Unless I'm missing something I suggest dropping the N:8 PCIDevice:pirq interrupt mapping and using a 1:1 PCIDevice:irqfd mapping.

2. Why is the intx pin number sent as part of the SetIrqFdMsg? The intx pin does not matter - each PCIDevice still just exactly one legacy interrupt pin, regardless of whether it is A, B, C, or D.

Quoted 123 lines
---
 MAINTAINERS               |   2 +
 hw/Makefile.objs          |   1 +
 hw/i386/remote-msg.c      |   4 +
 hw/i386/remote.c          |  15 ++++
 hw/pci/proxy.c            |  52 +++++++++++++
 hw/remote/Makefile.objs   |   1 +
 hw/remote/iohub.c         | 153 ++++++++++++++++++++++++++++++++++++++
 include/hw/i386/remote.h  |   2 +
 include/hw/pci/pci_ids.h  |   3 +
 include/hw/pci/proxy.h    |   8 ++
 include/hw/remote/iohub.h |  50 +++++++++++++
 include/io/mpqemu-link.h  |   6 ++
 io/mpqemu-link.c          |   1 +
 13 files changed, 298 insertions(+)
 create mode 100644 hw/remote/Makefile.objs
 create mode 100644 hw/remote/iohub.c
 create mode 100644 include/hw/remote/iohub.h
MAINTAINERS +2 -0
index 38d605445e..f9ede7e094 100644
@@ -2956,6 +2956,8 @@ F: hw/pci/proxy.c
 F: include/hw/pci/proxy.h
 F: hw/pci/memory-sync.c
 F: include/hw/pci/memory-sync.h
+F: hw/remote/iohub.c
+F: include/hw/remote/iohub.h
 
 Build and test automation
 -------------------------
hw/Makefile.objs +1 -0
index 4cbe5e4e57..8caf659de0 100644
@@ -40,6 +40,7 @@ devices-dirs-$(CONFIG_MEM_DEVICE) += mem/
 devices-dirs-$(CONFIG_NUBUS) += nubus/
 devices-dirs-y += semihosting/
 devices-dirs-y += smbios/
+devices-dirs-y += remote/
 endif
 
 common-obj-y += $(devices-dirs-y)
hw/i386/remote-msg.c +4 -0
index 48b153eaae..67fee4bb57 100644
@@ -10,6 +10,7 @@
 #include "hw/pci/pci.h"
 #include "exec/memattrs.h"
 #include "hw/i386/remote-memory.h"
+#include "hw/remote/iohub.h"
 
 static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
                                     Error **errp);
@@ -67,6 +68,9 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
     case SYNC_SYSMEM:
         remote_sysmem_reconfig(&msg, &local_err);
         break;
+    case SET_IRQFD:
+        process_set_irqfd_msg(pci_dev, &msg);
+        break;
     default:
         error_setg(&local_err, "Unknown command (%d) received from proxy \
                    in remote process pid=%d", msg.cmd, getpid());
hw/i386/remote.c +15 -0
index 5342e884ad..8e74a6f1af 100644
@@ -17,12 +17,16 @@
 #include "qapi/error.h"
 #include "io/channel-util.h"
 #include "io/channel.h"
+#include "hw/pci/pci_host.h"
+#include "hw/remote/iohub.h"
 
 static void remote_machine_init(MachineState *machine)
 {
     MemoryRegion *system_memory, *system_io, *pci_memory;
     RemMachineState *s = REMOTE_MACHINE(machine);
     RemotePCIHost *rem_host;
+    PCIHostState *pci_host;
+    PCIDevice *pci_dev;
 
     system_memory = get_system_memory();
     system_io = get_system_io();
@@ -42,6 +46,17 @@ static void remote_machine_init(MachineState *machine)
     memory_region_add_subregion_overlap(system_memory, 0x0, pci_memory, -1);
 
     qdev_realize(DEVICE(rem_host), sysbus_get_default(), &error_fatal);
+
+    pci_host = PCI_HOST_BRIDGE(rem_host);
+    pci_dev = pci_create_simple_multifunction(pci_host->bus,
+                                              PCI_DEVFN(REMOTE_IOHUB_DEV,
+                                                        REMOTE_IOHUB_FUNC),
+                                              true, TYPE_REMOTE_IOHUB_DEVICE);
+
+    s->iohub = REMOTE_IOHUB_DEVICE(pci_dev);
+
+    pci_bus_irqs(pci_host->bus, remote_iohub_set_irq, remote_iohub_map_irq,
+                 s->iohub, REMOTE_IOHUB_NB_PIRQS);
 }
 
 static void remote_set_socket(Object *obj, const char *str, Error **errp)
hw/pci/proxy.c +3 -0
index 5ecbdd2dcf..9d8559b6d4 100644
@@ -19,6 +19,9 @@
 #include "qemu/error-report.h"
 #include "hw/pci/memory-sync.h"
 #include "qom/object.h"
+#include "qemu/event_notifier.h"
+#include "sysemu/kvm.h"
+#include "util/event_notifier-posix.c"
 
 static void proxy_set_socket(PCIProxyDev *pdev, int fd, Error **errp)
 {

hw/pci/proxy.c static Property proxy_properties[] = { · line 69

Quoted 10 lines
hw/pci/proxy.c +7 -0
@@ -57,6 +60,53 @@ static Property proxy_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void proxy_intx_update(PCIDevice *pci_dev)
+{
+    PCIProxyDev *dev = PCI_PROXY_DEV(pci_dev);
+    PCIINTxRoute route;
+    int pin = pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
+
+    if (dev->irqfd.fd) {

0 is a valid file descriptor number. In practice fd 0 is standard input so this code won't encounter it, but for clarity please stick to the commonly-used -1 constant for invalid file descriptors.

hw/pci/proxy.c static Property proxy_properties[] = { · line 82

Quoted 13 lines
hw/pci/proxy.c +13 -0
+        dev->irqfd.flags = KVM_IRQFD_FLAG_DEASSIGN;
+        (void) kvm_vm_ioctl(kvm_state, KVM_IRQFD, &dev->irqfd);
+        memset(&dev->irqfd, 0, sizeof(struct kvm_irqfd));
+    }
+
+    route = pci_device_route_intx_to_irq(pci_dev, pin);
+
+    dev->irqfd.fd = event_notifier_get_fd(&dev->intr);
+    dev->irqfd.resamplefd = event_notifier_get_fd(&dev->resample);
+    dev->irqfd.gsi = route.irq;
+    dev->irqfd.flags |= KVM_IRQFD_FLAG_RESAMPLE;
+    (void) kvm_vm_ioctl(kvm_state, KVM_IRQFD, &dev->irqfd);
+}

Can this code use kvm_irqchip_add/remove_irqfd_notifier_gsi() instead of duplicating it?

hw/pci/proxy.c static Property proxy_properties[] = { · line 90

Quoted 8 lines
hw/pci/proxy.c +8 -0
+
+static void setup_irqfd(PCIProxyDev *dev)
+{
+    PCIDevice *pci_dev = PCI_DEVICE(dev);
+    MPQemuMsg msg;
+
+    event_notifier_init(&dev->intr, 0);
+    event_notifier_init(&dev->resample, 0);

Missing .unrealize() function that does cleanup.

Quoted 103 lines
hw/pci/proxy.c +21 -0
+
+    memset(&msg, 0, sizeof(MPQemuMsg));
+    msg.cmd = SET_IRQFD;
+    msg.num_fds = 2;
+    msg.fds[0] = event_notifier_get_fd(&dev->intr);
+    msg.fds[1] = event_notifier_get_fd(&dev->resample);
+    msg.data1.set_irqfd.intx =
+        pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
+    msg.size = sizeof(msg.data1);
+
+    mpqemu_msg_send(&msg, dev->dev);
+
+    memset(&dev->irqfd, 0, sizeof(struct kvm_irqfd));
+
+    proxy_intx_update(pci_dev);
+
+    pci_device_set_intx_routing_notifier(pci_dev, proxy_intx_update);
+}
+
 static void pci_proxy_dev_realize(PCIDevice *device, Error **errp)
 {
     PCIProxyDev *dev = PCI_PROXY_DEV(device);
@@ -72,6 +122,8 @@ static void pci_proxy_dev_realize(PCIDevice *device, Error **errp)
     }
 
     configure_memory_sync(&dev->sync, dev->com);
+
+    setup_irqfd(dev);
 }
 
 static int config_op_send(PCIProxyDev *pdev, uint32_t addr, uint32_t *val,
hw/remote/Makefile.objs new file +1 -0
new file mode 100644
index 0000000000..635ce5e0ab
@@ -0,0 +1 @@
+common-obj-$(CONFIG_MPQEMU) += iohub.o
hw/remote/iohub.c new file +59 -0
new file mode 100644
index 0000000000..9c6cfaecd4
@@ -0,0 +1,153 @@
+/*
+ * Remote IO Hub
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+
+#include "hw/pci/pci.h"
+#include "hw/pci/pci_ids.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/remote/iohub.h"
+#include "qemu/thread.h"
+#include "hw/boards.h"
+#include "hw/i386/remote.h"
+#include "qemu/main-loop.h"
+
+static void remote_iohub_initfn(Object *obj)
+{
+    RemoteIOHubState *iohub = REMOTE_IOHUB_DEVICE(obj);
+    int slot, intx, pirq;
+
+    memset(&iohub->irqfds, 0, sizeof(iohub->irqfds));
+    memset(&iohub->resamplefds, 0, sizeof(iohub->resamplefds));
+
+    for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
+        for (intx = 0; intx < PCI_NUM_PINS; intx++) {
+            iohub->irq_num[slot][intx] = (slot + intx) % 4 + 4;
+        }
+    }
+
+    for (pirq = 0; pirq < REMOTE_IOHUB_NB_PIRQS; pirq++) {
+        qemu_mutex_init(&iohub->irq_level_lock[pirq]);
+        iohub->irq_level[pirq] = 0;
+        event_notifier_init_fd(&iohub->irqfds[pirq], -1);
+        event_notifier_init_fd(&iohub->resamplefds[pirq], -1);
+    }
+}
+
+static void remote_iohub_class_init(ObjectClass *klass, void *data)
+{
+    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
+    k->vendor_id = PCI_VENDOR_ID_ORACLE;
+    k->device_id = PCI_DEVICE_ID_REMOTE_IOHUB;
+}
+
+static const TypeInfo remote_iohub_info = {
+    .name       = TYPE_REMOTE_IOHUB_DEVICE,
+    .parent     = TYPE_PCI_DEVICE,
+    .instance_size = sizeof(RemoteIOHubState),
+    .instance_init = remote_iohub_initfn,
+    .class_init  = remote_iohub_class_init,
+    .interfaces = (InterfaceInfo[]) {
+        { INTERFACE_CONVENTIONAL_PCI_DEVICE },

hw/remote/iohub.c line 69

Quoted 10 lines
hw/remote/iohub.c new file +10 -0
+        { }
+    }
+};
+
+static void remote_iohub_register(void)
+{
+    type_register_static(&remote_iohub_info);
+}
+
+type_init(remote_iohub_register);

Why is this a PCI device? It has no BARs, configuration space, interrupts, DMA transfers, etc. It's simpler to make this a plain old struct + functions that are used by the remote machine type.

hw/remote/iohub.c line 80

Quoted 11 lines
hw/remote/iohub.c new file +11 -0
+
+int remote_iohub_map_irq(PCIDevice *pci_dev, int intx)
+{
+    BusState *bus = qdev_get_parent_bus(&pci_dev->qdev);
+    PCIBus *pci_bus = PCI_BUS(bus);
+    PCIDevice *pci_iohub =
+        pci_bus->devices[PCI_DEVFN(REMOTE_IOHUB_DEV, REMOTE_IOHUB_FUNC)];
+    RemoteIOHubState *iohub = REMOTE_IOHUB_DEVICE(pci_iohub);
+
+    return iohub->irq_num[PCI_SLOT(pci_dev->devfn)][intx];
+}

This function can be rewritten like this:

  int remote_iohub_map_irq(PCIDevice *pci_dev, int intx)
  {
      return (PCI_SLOT(pci_dev->devfn) + intx) % 4 + 4;
  }

There is no need to access RemoteIOHubState and iohub->irq_num[] can be dropped.

Quoted 45 lines
hw/remote/iohub.c new file +45 -0
+
+/*
+ * TODO: Using lock to set the interrupt level could become a
+ *       performance bottleneck. Check if atomic arithmetic
+ *       is possible.
+ */
+void remote_iohub_set_irq(void *opaque, int pirq, int level)
+{
+    RemoteIOHubState *iohub = opaque;
+
+    assert(pirq >= 0);
+    assert(pirq < REMOTE_IOHUB_NB_PIRQS);
+
+    qemu_mutex_lock(&iohub->irq_level_lock[pirq]);
+
+    if (level) {
+        if (++iohub->irq_level[pirq] == 1) {
+            event_notifier_set(&iohub->irqfds[pirq]);
+        }
+    } else if (iohub->irq_level[pirq] > 0) {
+        iohub->irq_level[pirq]--;
+    }
+
+    qemu_mutex_unlock(&iohub->irq_level_lock[pirq]);
+}
+
+static void intr_resample_handler(void *opaque)
+{
+    ResampleToken *token = opaque;
+    RemoteIOHubState *iohub = token->iohub;
+    int pirq, s;
+
+    pirq = token->pirq;
+
+    s = event_notifier_test_and_clear(&iohub->resamplefds[pirq]);
+
+    assert(s >= 0);
+
+    qemu_mutex_lock(&iohub->irq_level_lock[pirq]);
+
+    if (iohub->irq_level[pirq]) {
+        event_notifier_set(&iohub->irqfds[pirq]);
+    }
+
+    qemu_mutex_unlock(&iohub->irq_level_lock[pirq]);

hw/remote/iohub.c line 135

Quoted 10 lines
hw/remote/iohub.c new file +10 -0
+}
+
+void process_set_irqfd_msg(PCIDevice *pci_dev, MPQemuMsg *msg)
+{
+    RemMachineState *machine = REMOTE_MACHINE(current_machine);
+    RemoteIOHubState *iohub = machine->iohub;
+    int pirq;
+
+    g_assert(msg->data1.set_irqfd.intx < 4);
+    g_assert(msg->num_fds == 2);

Please handle errors instead of using asserts that allow clients to terminate the program by sending invalid values.

hw/remote/iohub.c line 143

Quoted 8 lines
hw/remote/iohub.c new file +8 -0
+
+    pirq = remote_iohub_map_irq(pci_dev, msg->data1.set_irqfd.intx);
+
+    if (event_notifier_get_fd(&iohub->irqfds[pirq]) != -1) {
+        event_notifier_cleanup(&iohub->irqfds[pirq]);
+        event_notifier_cleanup(&iohub->resamplefds[pirq]);
+        memset(&iohub->token[pirq], 0, sizeof(ResampleToken));
+    }

Missing qemu_set_fd_handler(event_notifier_get_fd(&resamplefds[pirq]), NULL, NULL, NULL);

hw/remote/iohub.c line 153

Quoted 10 lines
hw/remote/iohub.c new file +10 -0
+
+    event_notifier_init_fd(&iohub->irqfds[pirq], msg->fds[0]);
+    event_notifier_init_fd(&iohub->resamplefds[pirq], msg->fds[1]);
+
+    iohub->token[pirq].iohub = iohub;
+    iohub->token[pirq].pirq = pirq;
+
+    qemu_set_fd_handler(msg->fds[1], intr_resample_handler, NULL,
+                        &iohub->token[pirq]);
+}

Missing event_notifier_cleanup() and qemu_set_fd_handler() calls when the mplink connection closes. The connection lifecycle isn't implemented and will leak resources.

Quoted 72 lines
include/hw/i386/remote.h +2 -0
index c3890e57ab..6af423ab54 100644
@@ -18,12 +18,14 @@
 #include "hw/boards.h"
 #include "hw/pci-host/remote.h"
 #include "io/channel.h"
+#include "hw/remote/iohub.h"
 
 typedef struct RemMachineState {
     MachineState parent_obj;
 
     RemotePCIHost *host;
     QIOChannel *ioc;
+    RemoteIOHubState *iohub;
 } RemMachineState;
 
 #define TYPE_REMOTE_MACHINE "remote-machine"
include/hw/pci/pci_ids.h +3 -0
index 11f8ab7149..bd0c17dc78 100644
@@ -192,6 +192,9 @@
 #define PCI_DEVICE_ID_SUN_SIMBA          0x5000
 #define PCI_DEVICE_ID_SUN_SABRE          0xa000
 
+#define PCI_VENDOR_ID_ORACLE             0x108e
+#define PCI_DEVICE_ID_REMOTE_IOHUB       0xb000
+
 #define PCI_VENDOR_ID_CMD                0x1095
 #define PCI_DEVICE_ID_CMD_646            0x0646
 
include/hw/pci/proxy.h +8 -0
index a41a6aeaa5..e6f076ae95 100644
@@ -12,9 +12,12 @@
 #include "qemu/osdep.h"
 #include "qemu-common.h"
 
+#include <linux/kvm.h>
+
 #include "hw/pci/pci.h"
 #include "io/channel.h"
 #include "hw/pci/memory-sync.h"
+#include "qemu/event_notifier.h"
 
 #define TYPE_PCI_PROXY_DEV "pci-proxy-dev"
 
@@ -45,6 +48,11 @@ struct PCIProxyDev {
 
     RemoteMemSync sync;
 
+    struct kvm_irqfd irqfd;
+
+    EventNotifier intr;
+    EventNotifier resample;
+
     ProxyMemoryRegion region[PCI_NUM_REGIONS];
 };
 
include/hw/remote/iohub.h new file +4 -0
new file mode 100644
index 0000000000..9aacf3e04c
@@ -0,0 +1,50 @@
+/*
+ * IO Hub for remote device
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.

include/hw/remote/iohub.h line 14

Quoted 10 lines
include/hw/remote/iohub.h new file +10 -0
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef REMOTE_IOHUB_H
+#define REMOTE_IOHUB_H
+
+#include "qemu/osdep.h"

CODING_STYLE.rst says:

Do not include "qemu/osdep.h" from header files since the .c file will have already included it.

Quoted 82 lines
include/hw/remote/iohub.h new file +36 -0
+#include "qemu-common.h"
+
+#include "hw/pci/pci.h"
+#include "qemu/event_notifier.h"
+#include "qemu/thread-posix.h"
+#include "io/mpqemu-link.h"
+
+#define REMOTE_IOHUB_NB_PIRQS    8
+
+#define REMOTE_IOHUB_DEV         31
+#define REMOTE_IOHUB_FUNC        0
+
+#define TYPE_REMOTE_IOHUB_DEVICE "remote-iohub"
+#define REMOTE_IOHUB_DEVICE(obj) \
+    OBJECT_CHECK(RemoteIOHubState, (obj), TYPE_REMOTE_IOHUB_DEVICE)
+
+typedef struct ResampleToken {
+    void *iohub;
+    int pirq;
+} ResampleToken;
+
+typedef struct RemoteIOHubState {
+    PCIDevice d;
+    uint8_t irq_num[PCI_SLOT_MAX][PCI_NUM_PINS];
+    EventNotifier irqfds[REMOTE_IOHUB_NB_PIRQS];
+    EventNotifier resamplefds[REMOTE_IOHUB_NB_PIRQS];
+    unsigned int irq_level[REMOTE_IOHUB_NB_PIRQS];
+    ResampleToken token[REMOTE_IOHUB_NB_PIRQS];
+    QemuMutex irq_level_lock[REMOTE_IOHUB_NB_PIRQS];
+} RemoteIOHubState;
+
+int remote_iohub_map_irq(PCIDevice *pci_dev, int intx);
+void remote_iohub_set_irq(void *opaque, int pirq, int level);
+void process_set_irqfd_msg(PCIDevice *pci_dev, MPQemuMsg *msg);
+
+#endif
include/io/mpqemu-link.h +6 -0
index 0422213863..a563b557ce 100644
@@ -42,6 +42,7 @@ typedef enum {
     PCI_CONFIG_READ,
     BAR_WRITE,
     BAR_READ,
+    SET_IRQFD,
     MAX = INT_MAX,
 } MPQemuCmd;
 
@@ -64,6 +65,10 @@ typedef struct {
     bool memory;
 } BarAccessMsg;
 
+typedef struct {
+    int intx;
+} SetIrqFdMsg;
+
 /**
  * Maximum size of data2 field in the message to be transmitted.
  */
@@ -92,6 +97,7 @@ typedef struct {
         uint64_t u64;
         SyncSysmemMsg sync_sysmem;
         BarAccessMsg bar_access;
+        SetIrqFdMsg set_irqfd;
     } data1;
 
     int fds[REMOTE_MAX_FDS];
io/mpqemu-link.c +1 -0
index 026e25dca4..561ac0576f 100644
@@ -258,6 +258,7 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
         break;
     case BAR_WRITE:
     case BAR_READ:
+    case SET_IRQFD:
         if (msg->size != sizeof(msg->data1)) {
             return false;
         }
Signature · 1 line
2.25.GIT
#39 lore raw

Re: [PATCH v7 17/21] multi-process: Retrieve PCI info from remote process

In reply to [email protected]
Quoted 20 lines · On Sat, Jun 27, 2020 at 10:09:39AM -0700, [email protected] wrote:
hw/pci/proxy.c +16 -0
@@ -267,3 +275,84 @@ const MemoryRegionOps proxy_mr_ops = {
         .max_access_size = 1,
     },
 };
+
+static void probe_pci_info(PCIDevice *dev)
+{
+    PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
+    DeviceClass *dc = DEVICE_CLASS(pc);
+    PCIProxyDev *pdev = PCI_PROXY_DEV(dev);
+    MPQemuMsg msg = { 0 }, ret =  { 0 };
+    uint32_t orig_val, new_val, class;
+    uint8_t type;
+    int i, size;
+    char *name;
+    Error *local_err = NULL;
+
+    msg.bytestream = 0;
+    msg.size = 0;
+    msg.cmd = GET_PCI_INFO;

hw/pci/proxy.c const MemoryRegionOps proxy_mr_ops = { · line 303

Quoted 10 lines
hw/pci/proxy.c +10 -0
+
+    ret.data1.u64  = mpqemu_msg_send_reply_co(&msg, pdev->dev, &local_err);
+    if (local_err) {
+        error_report("Error while sending GET_PCI_INFO message");
+    }
+
+    pc->vendor_id = ret.data1.ret_pci_info.vendor_id;
+    pc->device_id = ret.data1.ret_pci_info.device_id;
+    pc->class_id = ret.data1.ret_pci_info.class_id;
+    pc->subsystem_id = ret.data1.ret_pci_info.subsystem_id;

Why are the vendor/device/class/subsystem IDs read using a special GET_PCI_INFO message when they are already accessible from the PCI Configuration Space? All the other values are being read from the config space so I wonder why a special message is needed.

Quoted 17 lines
hw/pci/proxy.c +17 -0
+
+    config_op_send(pdev, PCI_CLASS_DEVICE, &class, 1, PCI_CONFIG_READ);
+    switch (class) {
+    case PCI_BASE_CLASS_BRIDGE:
+        set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
+        break;
+    case PCI_BASE_CLASS_STORAGE:
+        set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
+        break;
+    case PCI_BASE_CLASS_NETWORK:
+        set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
+        break;
+    case PCI_BASE_CLASS_INPUT:
+        set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+        break;
+    case PCI_BASE_CLASS_DISPLAY:
+        set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);

hw/pci/proxy.c const MemoryRegionOps proxy_mr_ops = { · line 330

Quoted 10 lines
hw/pci/proxy.c +10 -0
+        break;
+    case PCI_BASE_CLASS_PROCESSOR:
+        set_bit(DEVICE_CATEGORY_CPU, dc->categories);
+        break;
+    default:
+        set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+        break;
+    }
+
+    for (i = 0; i < 6; i++) {

s/6/G_N_ELEMENTS(pdev->region)/

#41 lore raw

Re: [PATCH v7 18/21] multi-process: heartbeat messages to remote

In reply to [email protected]
Quoted 10 lines · On Sat, Jun 27, 2020 at 10:09:40AM -0700, [email protected] wrote:

From: Elena Ufimtseva <[email protected]>

In order to detect remote processes which are hung, the proxy periodically sends heartbeat messages to confirm if the remote process is alive. The remote process responds to this heartbeat message to confirm it is alive.

Quoted 8 lines
---
 hw/i386/remote-msg.c     | 14 ++++++++++
 hw/pci/proxy.c           | 58 ++++++++++++++++++++++++++++++++++++++++
 include/hw/pci/proxy.h   |  2 ++
 include/io/mpqemu-link.h |  1 +
 io/mpqemu-link.c         |  1 +
 5 files changed, 76 insertions(+)

This patch seems incomplete since no action is taken when the device fails to respond. vCPU threads that access the device will still get stuck.

The simplest way to make this useful is to close the connection when a timeout occurs. Then the G_IO_HUP handler for the UNIX domain socket should perform connection cleanup. At that point there are a few choices:

1. Stop guest execution and wait for the host admin to restore the mplink so execution can resume. This is similar to how -drive rerror=stop pauses the guest when a disk I/O error is encountered.

2. Stop guest execution but defer it until this stale device is actually accessed. This maximizes guest uptime. Guests that rarely access the device may not notice at all.

3. Return 0 from MemoryRegion read operations and ignore writes. The guest continues executing but the device is broken. This is risky because device drivers inside the guest may not be ready to deal with this. The result could be data loss or corruption.

4. Raise a bus-level event. Maybe PCI error reporting can be used to offline the device.

5. Terminate the guest with an error message.

6. ?

Until the heartbeat is fully implemented and tested I suggest dropping it from this patch series. Remember the G_IO_HUP will happen anyway if the remote device process terminates.

#43 lore raw

Re: [PATCH v7 19/21] multi-process: perform device reset in the remote process

In reply to [email protected]

hw/i386/remote-msg.c static void process_proxy_ping_msg(QIOChannel *ioc) · line 296

Quoted 10 lines · On Sat, Jun 27, 2020 at 10:09:41AM -0700, [email protected] wrote:
hw/i386/remote-msg.c +6 -0
@@ -283,3 +288,14 @@ static void process_proxy_ping_msg(QIOChannel *ioc)
 
     mpqemu_msg_send(&ret, ioc);
 }
+
+static void process_device_reset_msg(QIOChannel *ioc)
+{
+    MPQemuMsg ret = { 0 };
+
+    qemu_devices_reset();

Why are all devices reset globally instead of just the PCIDevice in question?

#45 lore raw

Re: [PATCH v7 20/21] multi-process: add the concept description to docs/devel/qemu-multiprocess

In reply to [email protected]
Quoted 13 lines · On Sat, Jun 27, 2020 at 10:09:42AM -0700, [email protected] wrote:
docs/devel/multi-process.rst new file +7 -0
new file mode 100644
index 0000000000..406728854c
@@ -0,0 +1,957 @@
+Multi-process QEMU
+===================
+
+QEMU is often used as the hypervisor for virtual machines running in the
+Oracle cloud. Since one of the advantages of cloud computing is the
+ability to run many VMs from different tenants in the same cloud
+infrastructure, a guest that compromised its hypervisor could

docs/devel/multi-process.rst line 17

Quoted 10 lines
docs/devel/multi-process.rst new file +10 -0
+potentially use the hypervisor's access privileges to access data it is
+not authorized for.
+
+QEMU can be susceptible to security attacks because it is a large,
+monolithic program that provides many features to the VMs it services.
+Many of these features can be configured out of QEMU, but even a reduced
+configuration QEMU has a large amount of code a guest can potentially
+attack. Separating QEMU reduces the attack surface by aiding to
+limit each component in the system to only access the resources that
+it needs to perform its job.

This document does not reflect the functionality, internals, or syntax implemented in this patch series closely. It can still be useful as background reading for someone interested in diving into the code, but please add a disclaimer at the top to avoid confusion:

This is the design document for multi-process QEMU. It does not necessarily reflect the status of the current implementation, which may lack features or be considerably different from what is described in this document. This document is still useful as a description of the goals and general direction of this feature.

#47 lore raw

Re: [PATCH v7 21/21] multi-process: add configure and usage information

In reply to [email protected]

docs/multi-process.rst line 22

Quoted 9 lines · On Sat, Jun 27, 2020 at 10:09:43AM -0700, [email protected] wrote:

+2) Usage
+--------

docs/multi-process.rst new file +7 -0
+
+Multi-process QEMU requires an orchestrator to launch. Please refer to a
+light-weight python based orchestrator for mpqemu in
+scripts/mpqemu-launcher.py to lauch QEMU in multi-process mode.
+
+Please refer to scripts/mpqemu-launcher-perf-mode.py for running the remote
+process in performance mode.

What is performance mode?

#48 lore raw
In reply to [email protected]
Quoted 3 lines · On Sat, Jun 27, 2020 at 10:09:22AM -0700, [email protected] wrote:

From: Elena Ufimtseva <[email protected]>

This is the v7 of the patchset.

I have completed the review and left comments on the patches.

I'm glad it was possible to simplify this feature. The overall approach makes sense to me and I see how it forms the base on which VFIO-over-socket and smaller remote program builds using Kconfig can be developed.

My main concern is that the object lifecycle has not been fully implemented in the proxy and remote device. Error handling is incomplete, resources are leaked, and hot unplug does not work. Thinking through the lifecycle is very important so that additional work can build on top of this later. I have tried to point out these issues in the individual patches.

#49 lore raw
Quoted 12 lines · Stefan Hajnoczi
Quoted 3 lines · On Sat, Jun 27, 2020 at 10:09:22AM -0700, [email protected] wrote:

From: Elena Ufimtseva <[email protected]>

This is the v7 of the patchset.

I have completed the review and left comments on the patches.

I'm glad it was possible to simplify this feature. The overall approach

Hi Stefan,

We’re also with you on this. The feature looks much simpler now.

Quoted 6 lines

makes sense to me and I see how it forms the base on which VFIO-over-socket and smaller remote program builds using Kconfig can be developed.

My main concern is that the object lifecycle has not been fully implemented in the proxy and remote device. Error handling is

Thank you for your feedback on. FWIW, we did check about the unrealize() path in the object lifecycle management. We noticed that the destructor for the PCI devices (pci_qdev_unrealize()) is currently not invoking the instance specific destructor/unrealize functions. While this is not an excuse for not implementing the unrealize functions, it currently doesn’t have an impact on the hot unplug path.

You’re correct, we should implement the unrealize/destructor for the Proxy & remote objects. We’ll also look into any background for why the PCI devices don’t call instance specific destructor.

Quoted 1 line

incomplete, resources are leaked, and hot unplug does not work. Thinking

We’ll double check the resource leak issues, specifically with respect to open file destructors.

Quoted 3 lines

through the lifecycle is very important so that additional work can build on top of this later. I have tried to point out these issues in the individual patches.

We got a chance to go over your feedback. We will send out responses to them shortly.

Thank you very much!
--
Jag

#7 lore raw

Re: [PATCH v7 03/21] multi-process: setup PCI host bridge for remote device

In reply to Stefan Hajnoczi
Quoted 29 lines · Stefan Hajnoczi
Quoted 6 lines · On Sat, Jun 27, 2020 at 10:09:25AM -0700, [email protected] wrote:
hw/pci-host/remote.c new file +0 -0
new file mode 100644
index 0000000000..5ea9af4154

hw/pci-host/remote.c line 8

Quoted 8 lines
hw/pci-host/remote.c new file +8 -0
@@ -0,0 +1,63 @@
+/*
+ * Remote PCI host device
+ *
+ * Copyright © 2018, 2020 Oracle and/or its affiliates.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *

A little more detail would be nice:

Unlike PCI host devices that model physical hardware, the purpose of this PCI host is to host multi-process QEMU remote PCI devices.

Multi-process QEMU talks to a remote PCI device that runs in a separate process. In order to reuse QEMU device models in the remote process we need a PCI bus that holds the devices.

Quoted 7 lines

This PCI host is purely a container for PCI devices. It's fake in the sense that the guest never sees this PCI host and has no way of accessing it. It's job is just to provide the environment that QEMU PCI device models need when running in a remote process.

I think this could be restated more clearly but hopefully it communicates the purpose of hw/pci-host/remote.c. :P

OK, we get the theme of the message. :)

Quoted 10 lines

hw/pci-host/remote.c line 15

Quoted 8 lines
hw/pci-host/remote.c new file +7 -0
+typedef struct RemotePCIHost {
+    /*< private >*/
+    PCIExpressHost parent_obj;
+    /*< public >*/
+
+    MemoryRegion *mr_pci_mem;
+    MemoryRegion *mr_sys_mem;

Unused? Please add mr_sys_mem if and when it is used.

Got it. We will add it in the later patches that use these.

--
Jag

#50 lore raw
In reply to Jag Raman
Quoted 15 lines · Jag Raman
Quoted 8 lines · Stefan Hajnoczi

On Sat, Jun 27, 2020 at 10:09:22AM -0700, [email protected] wrote:
makes sense to me and I see how it forms the base on which VFIO-over-socket and smaller remote program builds using Kconfig can be developed.

My main concern is that the object lifecycle has not been fully implemented in the proxy and remote device. Error handling is

Thank you for your feedback on. FWIW, we did check about the unrealize() path in the object lifecycle management. We noticed that the destructor for the PCI devices (pci_qdev_unrealize()) is currently not invoking the instance specific destructor/unrealize functions. While this is not an excuse for not implementing the unrealize functions, it currently doesn’t have an impact on the hot unplug path.

Quoted 3 lines

You’re correct, we should implement the unrealize/destructor for the Proxy & remote objects. We’ll also look into any background for why the PCI devices don’t call instance specific destructor.

PCIDeviceClass->exit() is invoked by pci_qdev_unrealize(). I'm not sure why it's called "exit" instead of "unrealize" but PCI devices implement it to perform clean-up.

Stefan

#26 lore raw

Re: [PATCH v7 12/21] multi-process: Connect Proxy Object with device in the remote process

In reply to Stefan Hajnoczi
Quoted 12 lines · Stefan Hajnoczi
Quoted 4 lines · On Sat, Jun 27, 2020 at 10:09:34AM -0700, [email protected] wrote:

From: Jagannathan Raman <[email protected]>

Send a message to the remote process to connect PCI device with the corresponding Proxy object in QEMU

I thought the protocol was simplified to a 1:1 device:socket model, but this patch seems to implement an N:1 model?

Hi Stefan,

Thanks for your feedback on all the patches. We were able to address most of your feedback in this series, and we are looking forward to sending the next version out for review.

We wanted to double check with you regarding this patch alone.

The protocol is still a 1:1 device:socket model. It’s not possible for a proxy device object to send messages to arbitrary devices in the remote.

Quoted 11 lines

In a 1:1 model the CONNECT_DEV message is not necessary because each socket is already associated with a specific remote device (e.g. qemu -M remote -object mplink,dev=lsi-scsi-1,sockpath=/tmp/lsi-scsi-1.sock). Connecting to the socket already indicates which device we are talking to.

The N:1 model will work but it's more complex. There is a main socket that is used for CONNECT_DEV (anything else?) and we need to worry about the lifecycle of the per-device sockets that are passed over the main socket.

The main socket is only used for CONNECT_DEV. The CONNECT_DEV message sticks a fd to the remote device.

We are using the following command-line in the remote process:
qemu-system-x86_64 -machine remote,fd=4 -device lsi53c895a,id=lsi1 ...

The alternative approach would be to let the orchestrator to assign fds for each remote device. In this approach, we would specify an ‘fd’ for each device object like below:
qemu-system-x86_64 -machine remote -device lsi53c895a,id=lsi1,fd=4 …

The alternative approach would entail changes to the DeviceState struct and qdev_device_add() function, which we thought is not preferable. Could you please share your thoughts on this?

Thanks!
—
Jag

Quoted 89 lines
Quoted 6 lines
hw/i386/remote-msg.c +1 -0
@@ -50,3 +58,34 @@ gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond,
 
    return TRUE;
 }
+

hw/i386/remote-msg.c gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond, · line 71

Quoted 10 lines
hw/i386/remote-msg.c +10 -0
+static void process_connect_dev_msg(MPQemuMsg *msg, QIOChannel *com,
+                                    Error **errp)
+{
+    char *devid = (char *)msg->data2;
+    QIOChannel *dioc = NULL;
+    DeviceState *dev = NULL;
+    MPQemuMsg ret = { 0 };
+    int rc = 0;
+
+    g_assert(devid && (devid[msg->size - 1] == '\0'));

Asserts are not suitable for external input validation since a failure aborts the program and lets the client cause a denial-of-service. When there are multiple clients, one misbehaved client shouldn't be able to kill the server. Please validate devid using an if statement and set errp on failure.

Can msg->size be 0? If yes, this code accesses before the beginning of the buffer.

hw/i386/remote-msg.c gboolean mpqemu_process_msg(QIOChannel *ioc, GIOCondition cond, · line 79

Quoted 8 lines
hw/i386/remote-msg.c +8 -0
+
+    dev = qdev_find_recursive(sysbus_get_default(), devid);
+    if (!dev || !object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
+        rc = 0xff;
+        goto exit;
+    }
+
+    dioc = qio_channel_new_fd(msg->fds[0], errp);

Missing error handling if qio_channel_new_fd() fails. We need to close(msg->fds[0]) ourselves in this case.

Quoted 24 lines
hw/i386/remote-msg.c +12 -0
+
+    qio_channel_add_watch(dioc, G_IO_IN | G_IO_HUP, mpqemu_process_msg,
+                          (void *)dev, NULL);
+
+exit:
+    ret.cmd = RET_MSG;
+    ret.bytestream = 0;
+    ret.data1.u64 = rc;
+    ret.size = sizeof(ret.data1);
+
+    mpqemu_msg_send(&ret, com);
+}
hw/pci/proxy.c +1 -0
index 6d62399c52..16649ed0ec 100644
@@ -15,10 +15,38 @@
 #include "io/channel-util.h"
 #include "hw/qdev-properties.h"
 #include "monitor/monitor.h"
+#include "io/mpqemu-link.h"
 
 static void proxy_set_socket(PCIProxyDev *pdev, int fd, Error **errp)
 {

hw/pci/proxy.c line 31

Quoted 10 lines
hw/pci/proxy.c +9 -0
+    DeviceState *dev = DEVICE(pdev);
+    MPQemuMsg msg = { 0 };
+    int fds[2];
+    Error *local_err = NULL;
+
    pdev->com = qio_channel_new_fd(fd, errp);
+
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds)) {
+        error_setg(errp, "Failed to create proxy channel with fd %d", fd);
+        return;

pdev->com needs to be cleaned up.

io/mpqemu-link.c bool mpqemu_msg_valid(MPQemuMsg *msg) · line 240

Quoted 12 lines
io/mpqemu-link.c +4 -0
index 5887c8c6c0..54df3b254e 100644
@@ -234,6 +234,14 @@ bool mpqemu_msg_valid(MPQemuMsg *msg)
            return false;
        }
        break;
+    case CONNECT_DEV:
+        if ((msg->num_fds != 1) ||
+            (msg->fds[0] == -1) ||
+            (msg->fds[0] == -1) ||

This line is duplicated.

#27 lore raw

Re: [PATCH v7 12/21] multi-process: Connect Proxy Object with device in the remote process

Quoted 19 lines · Jag Raman
Quoted 8 lines · Stefan Hajnoczi

On Sat, Jun 27, 2020 at 10:09:34AM -0700, [email protected] wrote:
In a 1:1 model the CONNECT_DEV message is not necessary because each socket is already associated with a specific remote device (e.g. qemu -M remote -object mplink,dev=lsi-scsi-1,sockpath=/tmp/lsi-scsi-1.sock). Connecting to the socket already indicates which device we are talking to.

Quoted 4 lines

The N:1 model will work but it's more complex. There is a main socket that is used for CONNECT_DEV (anything else?) and we need to worry about the lifecycle of the per-device sockets that are passed over the main socket.

The main socket is only used for CONNECT_DEV. The CONNECT_DEV message sticks a fd to the remote device.

We are using the following command-line in the remote process:
qemu-system-x86_64 -machine remote,fd=4 -device lsi53c895a,id=lsi1 ...

Quoted 8 lines

The alternative approach would be to let the orchestrator to assign fds for each remote device. In this approach, we would specify an ‘fd’ for each device object like below:
qemu-system-x86_64 -machine remote -device lsi53c895a,id=lsi1,fd=4 …

The alternative approach would entail changes to the DeviceState struct and qdev_device_add() function, which we thought is not preferable. Could you please share your thoughts on this?

I suggest dropping multi-device support for now. It will be implemented differently with VFIO-over-socket anyway, so it's not worth investing much time into.

The main socket approach needs authentication support if multiple guests share a remote device emulation process. Otherwise guest A can access guest B's devices.

It's simpler if each device has a separate UNIX domain socket. It is not necessary to modify lsi53c895a in order to do this. Either the socket can be associated with the remote PCIe port (although I think the current code implements the older PCI Local Bus instead of PCIe) or a separate -object mpqlink,device=lsi1,fd=4 object can be defined (I think that's the syntax I've shared in the past).

For now though, just using the -machine remote,fd=4 approach is fine - but limited to 1 device.

Stefan

#28 lore raw

Re: [PATCH v7 12/21] multi-process: Connect Proxy Object with device in the remote process

Quoted 3 lines · Stefan Hajnoczi

I suggest dropping multi-device support for now. It will be implemented differently with VFIO-over-socket anyway, so it's not worth investing much time into.

I'm not sure I buy the VFIO-over-socket yet. It seems a weirdly QEMU specific IPC mechanism. However ...

Quoted 11 lines

The main socket approach needs authentication support if multiple guests share a remote device emulation process. Otherwise guest A can access guest B's devices.

It's simpler if each device has a separate UNIX domain socket. It is not necessary to modify lsi53c895a in order to do this. Either the socket can be associated with the remote PCIe port (although I think the current code implements the older PCI Local Bus instead of PCIe) or a separate -object mpqlink,device=lsi1,fd=4 object can be defined (I think that's the syntax I've shared in the past).

Quoted 4 lines

For now though, just using the -machine remote,fd=4 approach is fine - but limited to 1 device.

Stefan

I agree to all of the above. Starting with a single fd is a good idea.

Signature · 1 line
MST
#29 lore raw

Re: [PATCH v7 12/21] multi-process: Connect Proxy Object with device in the remote process

In reply to Stefan Hajnoczi
Quoted 48 lines · Stefan Hajnoczi
Quoted 19 lines · Jag Raman
Quoted 8 lines · Stefan Hajnoczi

On Sat, Jun 27, 2020 at 10:09:34AM -0700, [email protected] wrote:
In a 1:1 model the CONNECT_DEV message is not necessary because each socket is already associated with a specific remote device (e.g. qemu -M remote -object mplink,dev=lsi-scsi-1,sockpath=/tmp/lsi-scsi-1.sock). Connecting to the socket already indicates which device we are talking to.

Quoted 4 lines

The N:1 model will work but it's more complex. There is a main socket that is used for CONNECT_DEV (anything else?) and we need to worry about the lifecycle of the per-device sockets that are passed over the main socket.

The main socket is only used for CONNECT_DEV. The CONNECT_DEV message sticks a fd to the remote device.

We are using the following command-line in the remote process:
qemu-system-x86_64 -machine remote,fd=4 -device lsi53c895a,id=lsi1 ...

Quoted 8 lines

The alternative approach would be to let the orchestrator to assign fds for each remote device. In this approach, we would specify an ‘fd’ for each device object like below:
qemu-system-x86_64 -machine remote -device lsi53c895a,id=lsi1,fd=4 …

The alternative approach would entail changes to the DeviceState struct and qdev_device_add() function, which we thought is not preferable. Could you please share your thoughts on this?

I suggest dropping multi-device support for now. It will be implemented differently with VFIO-over-socket anyway, so it's not worth investing much time into.

The main socket approach needs authentication support if multiple guests share a remote device emulation process. Otherwise guest A can access guest B's devices.

It's simpler if each device has a separate UNIX domain socket. It is not necessary to modify lsi53c895a in order to do this. Either the socket can be associated with the remote PCIe port (although I think the current code implements the older PCI Local Bus instead of PCIe) or a separate -object mpqlink,device=lsi1,fd=4 object can be defined (I think that's the syntax I've shared in the past).

Quoted 2 lines

For now though, just using the -machine remote,fd=4 approach is fine - but limited to 1 device.

Hi Stefan,

Thanks for your response.

We didn’t quite get the purpose of the “object” when you proposed the command-line. But we understand it now.

We also agree to wait for the authentication protocol before enabling multiple devices in the same remote process. Therefore we have limited the number of devices to 1.

Even with one device, we still had the problem of attaching the file descriptor with a device int the remote end. The object idea you proposed seemed to fit well with connecting the fd with device. We have added this object in v8, which we just sent out.

We have dropped the main-socket/control-channel as it’s not needed anymore.

We look forward to your responses to v8.

Thank you very much!

Quoted 2 lines

Stefan