yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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