thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

Blame SOURCES/kvm-vhost-user-support-registering-external-host-notifie.patch

ae23c9
From 40b67cbd00745cb4aeca063619d4b8e94e926a31 Mon Sep 17 00:00:00 2001
ae23c9
From: "plai@redhat.com" <plai@redhat.com>
ae23c9
Date: Thu, 21 Jun 2018 18:54:44 +0200
ae23c9
Subject: [PATCH 165/268] vhost-user: support registering external host
ae23c9
 notifiers
ae23c9
ae23c9
RH-Author: plai@redhat.com
ae23c9
Message-id: <1529607285-9942-10-git-send-email-plai@redhat.com>
ae23c9
Patchwork-id: 80940
ae23c9
O-Subject: [RHEL7.6 PATCH BZ 1526645 09/10] vhost-user: support registering external host notifiers
ae23c9
Bugzilla: 1526645
ae23c9
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
ae23c9
RH-Acked-by: Maxime Coquelin <maxime.coquelin@redhat.com>
ae23c9
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
ae23c9
ae23c9
From: Tiwei Bie <tiwei.bie@intel.com>
ae23c9
ae23c9
This patch introduces VHOST_USER_PROTOCOL_F_HOST_NOTIFIER.
ae23c9
With this feature negotiated, vhost-user backend can register
ae23c9
memory region based host notifiers. And it will allow the guest
ae23c9
driver in the VM to notify the hardware accelerator at the
ae23c9
vhost-user backend directly.
ae23c9
ae23c9
Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
ae23c9
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
ae23c9
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
ae23c9
(cherry picked from commit 44866521bd6ea8e5152a87664dea1eee90c9438b)
ae23c9
Signed-off-by: Paul Lai <plai@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 docs/interop/vhost-user.txt    |  33 ++++++++++++
ae23c9
 hw/virtio/vhost-user.c         | 113 +++++++++++++++++++++++++++++++++++++++++
ae23c9
 include/hw/virtio/vhost-user.h |   8 +++
ae23c9
 3 files changed, 154 insertions(+)
ae23c9
ae23c9
diff --git a/docs/interop/vhost-user.txt b/docs/interop/vhost-user.txt
ae23c9
index 682a683..d51fd58 100644
ae23c9
--- a/docs/interop/vhost-user.txt
ae23c9
+++ b/docs/interop/vhost-user.txt
ae23c9
@@ -132,6 +132,16 @@ Depending on the request type, payload can be:
ae23c9
    Payload: Size bytes array holding the contents of the virtio
ae23c9
        device's configuration space
ae23c9
 
ae23c9
+ * Vring area description
ae23c9
+   -----------------------
ae23c9
+   | u64 | size | offset |
ae23c9
+   -----------------------
ae23c9
+
ae23c9
+   u64: a 64-bit integer contains vring index and flags
ae23c9
+   Size: a 64-bit size of this area
ae23c9
+   Offset: a 64-bit offset of this area from the start of the
ae23c9
+       supplied file descriptor
ae23c9
+
ae23c9
 In QEMU the vhost-user message is implemented with the following struct:
ae23c9
 
ae23c9
 typedef struct VhostUserMsg {
ae23c9
@@ -146,6 +156,7 @@ typedef struct VhostUserMsg {
ae23c9
         VhostUserLog log;
ae23c9
         struct vhost_iotlb_msg iotlb;
ae23c9
         VhostUserConfig config;
ae23c9
+        VhostUserVringArea area;
ae23c9
     };
ae23c9
 } QEMU_PACKED VhostUserMsg;
ae23c9
 
ae23c9
@@ -385,6 +396,7 @@ Protocol features
ae23c9
 #define VHOST_USER_PROTOCOL_F_PAGEFAULT      8
ae23c9
 #define VHOST_USER_PROTOCOL_F_CONFIG         9
ae23c9
 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD  10
ae23c9
+#define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER  11
ae23c9
 
ae23c9
 Master message types
ae23c9
 --------------------
ae23c9
@@ -782,6 +794,27 @@ Slave message types
ae23c9
      the VHOST_USER_NEED_REPLY flag, master must respond with zero when
ae23c9
      operation is successfully completed, or non-zero otherwise.
ae23c9
 
ae23c9
+ * VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
ae23c9
+
ae23c9
+      Id: 3
ae23c9
+      Equivalent ioctl: N/A
ae23c9
+      Slave payload: vring area description
ae23c9
+      Master payload: N/A
ae23c9
+
ae23c9
+      Sets host notifier for a specified queue. The queue index is contained
ae23c9
+      in the u64 field of the vring area description. The host notifier is
ae23c9
+      described by the file descriptor (typically it's a VFIO device fd) which
ae23c9
+      is passed as ancillary data and the size (which is mmap size and should
ae23c9
+      be the same as host page size) and offset (which is mmap offset) carried
ae23c9
+      in the vring area description. QEMU can mmap the file descriptor based
ae23c9
+      on the size and offset to get a memory range. Registering a host notifier
ae23c9
+      means mapping this memory range to the VM as the specified queue's notify
ae23c9
+      MMIO region. Slave sends this request to tell QEMU to de-register the
ae23c9
+      existing notifier if any and register the new notifier if the request is
ae23c9
+      sent with a file descriptor.
ae23c9
+      This request should be sent only when VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
ae23c9
+      protocol feature has been successfully negotiated.
ae23c9
+
ae23c9
 VHOST_USER_PROTOCOL_F_REPLY_ACK:
ae23c9
 -------------------------------
ae23c9
 The original vhost-user specification only demands replies for certain
ae23c9
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
ae23c9
index a715c5c..c30fc3d 100644
ae23c9
--- a/hw/virtio/vhost-user.c
ae23c9
+++ b/hw/virtio/vhost-user.c
ae23c9
@@ -13,6 +13,7 @@
ae23c9
 #include "hw/virtio/vhost.h"
ae23c9
 #include "hw/virtio/vhost-user.h"
ae23c9
 #include "hw/virtio/vhost-backend.h"
ae23c9
+#include "hw/virtio/virtio.h"
ae23c9
 #include "hw/virtio/virtio-net.h"
ae23c9
 #include "chardev/char-fe.h"
ae23c9
 #include "sysemu/kvm.h"
ae23c9
@@ -50,6 +51,7 @@ enum VhostUserProtocolFeature {
ae23c9
     VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
ae23c9
     VHOST_USER_PROTOCOL_F_CONFIG = 9,
ae23c9
     VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
ae23c9
+    VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
ae23c9
     VHOST_USER_PROTOCOL_F_MAX
ae23c9
 };
ae23c9
 
ae23c9
@@ -94,6 +96,7 @@ typedef enum VhostUserSlaveRequest {
ae23c9
     VHOST_USER_SLAVE_NONE = 0,
ae23c9
     VHOST_USER_SLAVE_IOTLB_MSG = 1,
ae23c9
     VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
ae23c9
+    VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
ae23c9
     VHOST_USER_SLAVE_MAX
ae23c9
 }  VhostUserSlaveRequest;
ae23c9
 
ae23c9
@@ -138,6 +141,12 @@ static VhostUserConfig c __attribute__ ((unused));
ae23c9
                                    + sizeof(c.size) \
ae23c9
                                    + sizeof(c.flags))
ae23c9
 
ae23c9
+typedef struct VhostUserVringArea {
ae23c9
+    uint64_t u64;
ae23c9
+    uint64_t size;
ae23c9
+    uint64_t offset;
ae23c9
+} VhostUserVringArea;
ae23c9
+
ae23c9
 typedef struct {
ae23c9
     VhostUserRequest request;
ae23c9
 
ae23c9
@@ -159,6 +168,7 @@ typedef union {
ae23c9
         struct vhost_iotlb_msg iotlb;
ae23c9
         VhostUserConfig config;
ae23c9
         VhostUserCryptoSession session;
ae23c9
+        VhostUserVringArea area;
ae23c9
 } VhostUserPayload;
ae23c9
 
ae23c9
 typedef struct VhostUserMsg {
ae23c9
@@ -640,9 +650,37 @@ static int vhost_user_set_vring_num(struct vhost_dev *dev,
ae23c9
     return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring);
ae23c9
 }
ae23c9
 
ae23c9
+static void vhost_user_host_notifier_restore(struct vhost_dev *dev,
ae23c9
+                                             int queue_idx)
ae23c9
+{
ae23c9
+    struct vhost_user *u = dev->opaque;
ae23c9
+    VhostUserHostNotifier *n = &u->user->notifier[queue_idx];
ae23c9
+    VirtIODevice *vdev = dev->vdev;
ae23c9
+
ae23c9
+    if (n->addr && !n->set) {
ae23c9
+        virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, true);
ae23c9
+        n->set = true;
ae23c9
+    }
ae23c9
+}
ae23c9
+
ae23c9
+static void vhost_user_host_notifier_remove(struct vhost_dev *dev,
ae23c9
+                                            int queue_idx)
ae23c9
+{
ae23c9
+    struct vhost_user *u = dev->opaque;
ae23c9
+    VhostUserHostNotifier *n = &u->user->notifier[queue_idx];
ae23c9
+    VirtIODevice *vdev = dev->vdev;
ae23c9
+
ae23c9
+    if (n->addr && n->set) {
ae23c9
+        virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, false);
ae23c9
+        n->set = false;
ae23c9
+    }
ae23c9
+}
ae23c9
+
ae23c9
 static int vhost_user_set_vring_base(struct vhost_dev *dev,
ae23c9
                                      struct vhost_vring_state *ring)
ae23c9
 {
ae23c9
+    vhost_user_host_notifier_restore(dev, ring->index);
ae23c9
+
ae23c9
     return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring);
ae23c9
 }
ae23c9
 
ae23c9
@@ -676,6 +714,8 @@ static int vhost_user_get_vring_base(struct vhost_dev *dev,
ae23c9
         .hdr.size = sizeof(msg.payload.state),
ae23c9
     };
ae23c9
 
ae23c9
+    vhost_user_host_notifier_remove(dev, ring->index);
ae23c9
+
ae23c9
     if (vhost_user_write(dev, &msg, NULL, 0) < 0) {
ae23c9
         return -1;
ae23c9
     }
ae23c9
@@ -849,6 +889,66 @@ static int vhost_user_slave_handle_config_change(struct vhost_dev *dev)
ae23c9
     return ret;
ae23c9
 }
ae23c9
 
ae23c9
+static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev *dev,
ae23c9
+                                                       VhostUserVringArea *area,
ae23c9
+                                                       int fd)
ae23c9
+{
ae23c9
+    int queue_idx = area->u64 & VHOST_USER_VRING_IDX_MASK;
ae23c9
+    size_t page_size = qemu_real_host_page_size;
ae23c9
+    struct vhost_user *u = dev->opaque;
ae23c9
+    VhostUserState *user = u->user;
ae23c9
+    VirtIODevice *vdev = dev->vdev;
ae23c9
+    VhostUserHostNotifier *n;
ae23c9
+    void *addr;
ae23c9
+    char *name;
ae23c9
+
ae23c9
+    if (!virtio_has_feature(dev->protocol_features,
ae23c9
+                            VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) ||
ae23c9
+        vdev == NULL || queue_idx >= virtio_get_num_queues(vdev)) {
ae23c9
+        return -1;
ae23c9
+    }
ae23c9
+
ae23c9
+    n = &user->notifier[queue_idx];
ae23c9
+
ae23c9
+    if (n->addr) {
ae23c9
+        virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, false);
ae23c9
+        object_unparent(OBJECT(&n->mr));
ae23c9
+        munmap(n->addr, page_size);
ae23c9
+        n->addr = NULL;
ae23c9
+    }
ae23c9
+
ae23c9
+    if (area->u64 & VHOST_USER_VRING_NOFD_MASK) {
ae23c9
+        return 0;
ae23c9
+    }
ae23c9
+
ae23c9
+    /* Sanity check. */
ae23c9
+    if (area->size != page_size) {
ae23c9
+        return -1;
ae23c9
+    }
ae23c9
+
ae23c9
+    addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
ae23c9
+                fd, area->offset);
ae23c9
+    if (addr == MAP_FAILED) {
ae23c9
+        return -1;
ae23c9
+    }
ae23c9
+
ae23c9
+    name = g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
ae23c9
+                           user, queue_idx);
ae23c9
+    memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name,
ae23c9
+                                      page_size, addr);
ae23c9
+    g_free(name);
ae23c9
+
ae23c9
+    if (virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, true)) {
ae23c9
+        munmap(addr, page_size);
ae23c9
+        return -1;
ae23c9
+    }
ae23c9
+
ae23c9
+    n->addr = addr;
ae23c9
+    n->set = true;
ae23c9
+
ae23c9
+    return 0;
ae23c9
+}
ae23c9
+
ae23c9
 static void slave_read(void *opaque)
ae23c9
 {
ae23c9
     struct vhost_dev *dev = opaque;
ae23c9
@@ -917,6 +1017,10 @@ static void slave_read(void *opaque)
ae23c9
     case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG :
ae23c9
         ret = vhost_user_slave_handle_config_change(dev);
ae23c9
         break;
ae23c9
+    case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG:
ae23c9
+        ret = vhost_user_slave_handle_vring_host_notifier(dev, &payload.area,
ae23c9
+                                                          fd[0]);
ae23c9
+        break;
ae23c9
     default:
ae23c9
         error_report("Received unexpected msg type.");
ae23c9
         ret = -EINVAL;
ae23c9
@@ -1648,6 +1752,15 @@ VhostUserState *vhost_user_init(void)
ae23c9
 
ae23c9
 void vhost_user_cleanup(VhostUserState *user)
ae23c9
 {
ae23c9
+    int i;
ae23c9
+
ae23c9
+    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
ae23c9
+        if (user->notifier[i].addr) {
ae23c9
+            object_unparent(OBJECT(&user->notifier[i].mr));
ae23c9
+            munmap(user->notifier[i].addr, qemu_real_host_page_size);
ae23c9
+            user->notifier[i].addr = NULL;
ae23c9
+        }
ae23c9
+    }
ae23c9
 }
ae23c9
 
ae23c9
 const VhostOps user_ops = {
ae23c9
diff --git a/include/hw/virtio/vhost-user.h b/include/hw/virtio/vhost-user.h
ae23c9
index eb8bc0d..fd66039 100644
ae23c9
--- a/include/hw/virtio/vhost-user.h
ae23c9
+++ b/include/hw/virtio/vhost-user.h
ae23c9
@@ -9,9 +9,17 @@
ae23c9
 #define HW_VIRTIO_VHOST_USER_H
ae23c9
 
ae23c9
 #include "chardev/char-fe.h"
ae23c9
+#include "hw/virtio/virtio.h"
ae23c9
+
ae23c9
+typedef struct VhostUserHostNotifier {
ae23c9
+    MemoryRegion mr;
ae23c9
+    void *addr;
ae23c9
+    bool set;
ae23c9
+} VhostUserHostNotifier;
ae23c9
 
ae23c9
 typedef struct VhostUserState {
ae23c9
     CharBackend *chr;
ae23c9
+    VhostUserHostNotifier notifier[VIRTIO_QUEUE_MAX];
ae23c9
 } VhostUserState;
ae23c9
 
ae23c9
 VhostUserState *vhost_user_init(void);
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9