Blame SOURCES/kvm-util-implement-simple-iova-tree.patch

357786
From 87c2556917718a14f46ca4f9a4fc1095429866d7 Mon Sep 17 00:00:00 2001
357786
From: Peter Xu <peterx@redhat.com>
357786
Date: Mon, 3 Sep 2018 04:52:40 +0200
357786
Subject: [PATCH 25/29] util: implement simple iova tree
357786
357786
RH-Author: Peter Xu <peterx@redhat.com>
357786
Message-id: <20180903045241.6456-9-peterx@redhat.com>
357786
Patchwork-id: 82026
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 8/9] util: implement simple iova tree
357786
Bugzilla: 1623859
357786
RH-Acked-by: Xiao Wang <jasowang@redhat.com>
357786
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
357786
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
357786
357786
Introduce a simplest iova tree implementation based on GTree.
357786
357786
CC: QEMU Stable <qemu-stable@nongnu.org>
357786
Signed-off-by: Peter Xu <peterx@redhat.com>
357786
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
357786
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
357786
(cherry picked from commit eecf5eedbdc0fc04f39abcf3afeedfbf21b25ca4)
357786
Signed-off-by: Peter Xu <peterx@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 MAINTAINERS              |   6 +++
357786
 include/qemu/iova-tree.h | 134 +++++++++++++++++++++++++++++++++++++++++++++++
357786
 util/Makefile.objs       |   1 +
357786
 util/iova-tree.c         | 114 ++++++++++++++++++++++++++++++++++++++++
357786
 4 files changed, 255 insertions(+)
357786
 create mode 100644 include/qemu/iova-tree.h
357786
 create mode 100644 util/iova-tree.c
357786
357786
diff --git a/MAINTAINERS b/MAINTAINERS
357786
index 53d5216..1e32116 100644
357786
--- a/MAINTAINERS
357786
+++ b/MAINTAINERS
357786
@@ -1787,6 +1787,12 @@ F: include/sysemu/replay.h
357786
 F: docs/replay.txt
357786
 F: stubs/replay.c
357786
 
357786
+IOVA Tree
357786
+M: Peter Xu <peterx@redhat.com>
357786
+S: Maintained
357786
+F: include/qemu/iova-tree.h
357786
+F: util/iova-tree.c
357786
+
357786
 Usermode Emulation
357786
 ------------------
357786
 Overall
357786
diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h
357786
new file mode 100644
357786
index 0000000..b061932
357786
--- /dev/null
357786
+++ b/include/qemu/iova-tree.h
357786
@@ -0,0 +1,134 @@
357786
+/*
357786
+ * An very simplified iova tree implementation based on GTree.
357786
+ *
357786
+ * Copyright 2018 Red Hat, Inc.
357786
+ *
357786
+ * Authors:
357786
+ *  Peter Xu <peterx@redhat.com>
357786
+ *
357786
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
357786
+ */
357786
+#ifndef IOVA_TREE_H
357786
+#define IOVA_TREE_H
357786
+
357786
+/*
357786
+ * Currently the iova tree will only allow to keep ranges
357786
+ * information, and no extra user data is allowed for each element.  A
357786
+ * benefit is that we can merge adjacent ranges internally within the
357786
+ * tree.  It can save a lot of memory when the ranges are splitted but
357786
+ * mostly continuous.
357786
+ *
357786
+ * Note that current implementation does not provide any thread
357786
+ * protections.  Callers of the iova tree should be responsible
357786
+ * for the thread safety issue.
357786
+ */
357786
+
357786
+#include "qemu/osdep.h"
357786
+#include "exec/memory.h"
357786
+#include "exec/hwaddr.h"
357786
+
357786
+#define  IOVA_OK           (0)
357786
+#define  IOVA_ERR_INVALID  (-1) /* Invalid parameters */
357786
+#define  IOVA_ERR_OVERLAP  (-2) /* IOVA range overlapped */
357786
+
357786
+typedef struct IOVATree IOVATree;
357786
+typedef struct DMAMap {
357786
+    hwaddr iova;
357786
+    hwaddr translated_addr;
357786
+    hwaddr size;                /* Inclusive */
357786
+    IOMMUAccessFlags perm;
357786
+} QEMU_PACKED DMAMap;
357786
+typedef gboolean (*iova_tree_iterator)(DMAMap *map);
357786
+
357786
+/**
357786
+ * iova_tree_new:
357786
+ *
357786
+ * Create a new iova tree.
357786
+ *
357786
+ * Returns: the tree pointer when succeeded, or NULL if error.
357786
+ */
357786
+IOVATree *iova_tree_new(void);
357786
+
357786
+/**
357786
+ * iova_tree_insert:
357786
+ *
357786
+ * @tree: the iova tree to insert
357786
+ * @map: the mapping to insert
357786
+ *
357786
+ * Insert an iova range to the tree.  If there is overlapped
357786
+ * ranges, IOVA_ERR_OVERLAP will be returned.
357786
+ *
357786
+ * Return: 0 if succeeded, or <0 if error.
357786
+ */
357786
+int iova_tree_insert(IOVATree *tree, DMAMap *map);
357786
+
357786
+/**
357786
+ * iova_tree_remove:
357786
+ *
357786
+ * @tree: the iova tree to remove range from
357786
+ * @map: the map range to remove
357786
+ *
357786
+ * Remove mappings from the tree that are covered by the map range
357786
+ * provided.  The range does not need to be exactly what has inserted,
357786
+ * all the mappings that are included in the provided range will be
357786
+ * removed from the tree.  Here map->translated_addr is meaningless.
357786
+ *
357786
+ * Return: 0 if succeeded, or <0 if error.
357786
+ */
357786
+int iova_tree_remove(IOVATree *tree, DMAMap *map);
357786
+
357786
+/**
357786
+ * iova_tree_find:
357786
+ *
357786
+ * @tree: the iova tree to search from
357786
+ * @map: the mapping to search
357786
+ *
357786
+ * Search for a mapping in the iova tree that overlaps with the
357786
+ * mapping range specified.  Only the first found mapping will be
357786
+ * returned.
357786
+ *
357786
+ * Return: DMAMap pointer if found, or NULL if not found.  Note that
357786
+ * the returned DMAMap pointer is maintained internally.  User should
357786
+ * only read the content but never modify or free the content.  Also,
357786
+ * user is responsible to make sure the pointer is valid (say, no
357786
+ * concurrent deletion in progress).
357786
+ */
357786
+DMAMap *iova_tree_find(IOVATree *tree, DMAMap *map);
357786
+
357786
+/**
357786
+ * iova_tree_find_address:
357786
+ *
357786
+ * @tree: the iova tree to search from
357786
+ * @iova: the iova address to find
357786
+ *
357786
+ * Similar to iova_tree_find(), but it tries to find mapping with
357786
+ * range iova=iova & size=0.
357786
+ *
357786
+ * Return: same as iova_tree_find().
357786
+ */
357786
+DMAMap *iova_tree_find_address(IOVATree *tree, hwaddr iova);
357786
+
357786
+/**
357786
+ * iova_tree_foreach:
357786
+ *
357786
+ * @tree: the iova tree to iterate on
357786
+ * @iterator: the interator for the mappings, return true to stop
357786
+ *
357786
+ * Iterate over the iova tree.
357786
+ *
357786
+ * Return: 1 if found any overlap, 0 if not, <0 if error.
357786
+ */
357786
+void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator);
357786
+
357786
+/**
357786
+ * iova_tree_destroy:
357786
+ *
357786
+ * @tree: the iova tree to destroy
357786
+ *
357786
+ * Destroy an existing iova tree.
357786
+ *
357786
+ * Return: None.
357786
+ */
357786
+void iova_tree_destroy(IOVATree *tree);
357786
+
357786
+#endif
357786
diff --git a/util/Makefile.objs b/util/Makefile.objs
357786
index 728c354..e1c3fed 100644
357786
--- a/util/Makefile.objs
357786
+++ b/util/Makefile.objs
357786
@@ -47,4 +47,5 @@ util-obj-y += qht.o
357786
 util-obj-y += range.o
357786
 util-obj-y += stats64.o
357786
 util-obj-y += systemd.o
357786
+util-obj-y += iova-tree.o
357786
 util-obj-$(CONFIG_LINUX) += vfio-helpers.o
357786
diff --git a/util/iova-tree.c b/util/iova-tree.c
357786
new file mode 100644
357786
index 0000000..2d9cebf
357786
--- /dev/null
357786
+++ b/util/iova-tree.c
357786
@@ -0,0 +1,114 @@
357786
+/*
357786
+ * IOVA tree implementation based on GTree.
357786
+ *
357786
+ * Copyright 2018 Red Hat, Inc.
357786
+ *
357786
+ * Authors:
357786
+ *  Peter Xu <peterx@redhat.com>
357786
+ *
357786
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
357786
+ */
357786
+
357786
+#include <glib.h>
357786
+#include "qemu/iova-tree.h"
357786
+
357786
+struct IOVATree {
357786
+    GTree *tree;
357786
+};
357786
+
357786
+static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer data)
357786
+{
357786
+    const DMAMap *m1 = a, *m2 = b;
357786
+
357786
+    if (m1->iova > m2->iova + m2->size) {
357786
+        return 1;
357786
+    }
357786
+
357786
+    if (m1->iova + m1->size < m2->iova) {
357786
+        return -1;
357786
+    }
357786
+
357786
+    /* Overlapped */
357786
+    return 0;
357786
+}
357786
+
357786
+IOVATree *iova_tree_new(void)
357786
+{
357786
+    IOVATree *iova_tree = g_new0(IOVATree, 1);
357786
+
357786
+    /* We don't have values actually, no need to free */
357786
+    iova_tree->tree = g_tree_new_full(iova_tree_compare, NULL, g_free, NULL);
357786
+
357786
+    return iova_tree;
357786
+}
357786
+
357786
+DMAMap *iova_tree_find(IOVATree *tree, DMAMap *map)
357786
+{
357786
+    return g_tree_lookup(tree->tree, map);
357786
+}
357786
+
357786
+DMAMap *iova_tree_find_address(IOVATree *tree, hwaddr iova)
357786
+{
357786
+    DMAMap map = { .iova = iova, .size = 0 };
357786
+
357786
+    return iova_tree_find(tree, &map);
357786
+}
357786
+
357786
+static inline void iova_tree_insert_internal(GTree *gtree, DMAMap *range)
357786
+{
357786
+    /* Key and value are sharing the same range data */
357786
+    g_tree_insert(gtree, range, range);
357786
+}
357786
+
357786
+int iova_tree_insert(IOVATree *tree, DMAMap *map)
357786
+{
357786
+    DMAMap *new;
357786
+
357786
+    if (map->iova + map->size < map->iova || map->perm == IOMMU_NONE) {
357786
+        return IOVA_ERR_INVALID;
357786
+    }
357786
+
357786
+    /* We don't allow to insert range that overlaps with existings */
357786
+    if (iova_tree_find(tree, map)) {
357786
+        return IOVA_ERR_OVERLAP;
357786
+    }
357786
+
357786
+    new = g_new0(DMAMap, 1);
357786
+    memcpy(new, map, sizeof(*new));
357786
+    iova_tree_insert_internal(tree->tree, new);
357786
+
357786
+    return IOVA_OK;
357786
+}
357786
+
357786
+static gboolean iova_tree_traverse(gpointer key, gpointer value,
357786
+                                gpointer data)
357786
+{
357786
+    iova_tree_iterator iterator = data;
357786
+    DMAMap *map = key;
357786
+
357786
+    g_assert(key == value);
357786
+
357786
+    return iterator(map);
357786
+}
357786
+
357786
+void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator)
357786
+{
357786
+    g_tree_foreach(tree->tree, iova_tree_traverse, iterator);
357786
+}
357786
+
357786
+int iova_tree_remove(IOVATree *tree, DMAMap *map)
357786
+{
357786
+    DMAMap *overlap;
357786
+
357786
+    while ((overlap = iova_tree_find(tree, map))) {
357786
+        g_tree_remove(tree->tree, overlap);
357786
+    }
357786
+
357786
+    return IOVA_OK;
357786
+}
357786
+
357786
+void iova_tree_destroy(IOVATree *tree)
357786
+{
357786
+    g_tree_destroy(tree->tree);
357786
+    g_free(tree);
357786
+}
357786
-- 
357786
1.8.3.1
357786