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