yeahuh / rpms / qemu-kvm

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