FRET-qemu/hw/hyperv/hv-balloon-page_range_tree.c
Romain Malmain 7c3c7877d8 Update to QEMU 9.0.0 (#67)
* Update to QEMU v9.0.0

---------

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Zheyu Ma <zheyuma97@gmail.com>
Signed-off-by: Ido Plat <ido.plat@ibm.com>
Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
Signed-off-by: Gregory Price <gregory.price@memverge.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Lorenz Brun <lorenz@brun.one>
Signed-off-by: Yao Xingtao <yaoxt.fnst@fujitsu.com>
Signed-off-by: Arnaud Minier <arnaud.minier@telecom-paris.fr>
Signed-off-by: Inès Varhol <ines.varhol@telecom-paris.fr>
Signed-off-by: BALATON Zoltan <balaton@eik.bme.hu>
Signed-off-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Sven Schnelle <svens@stackframe.org>
Signed-off-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Helge Deller <deller@gmx.de>
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
Signed-off-by: Avihai Horon <avihaih@nvidia.com>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
Signed-off-by: Joonas Kankaala <joonas.a.kankaala@gmail.com>
Signed-off-by: Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
Signed-off-by: Stefan Weil <sw@weilnetz.de>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Glenn Miles <milesg@linux.ibm.com>
Signed-off-by: Oleg Sviridov <oleg.sviridov@red-soft.ru>
Signed-off-by: Artem Chernyshev <artem.chernyshev@red-soft.ru>
Signed-off-by: Yajun Wu <yajunw@nvidia.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
Signed-off-by: Lei Wang <lei4.wang@intel.com>
Signed-off-by: Wei Wang <wei.w.wang@intel.com>
Signed-off-by: Martin Hundebøll <martin@geanix.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Wafer <wafer@jaguarmicro.com>
Signed-off-by: Yuxue Liu <yuxue.liu@jaguarmicro.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Nguyen Dinh Phi <phind.uet@gmail.com>
Signed-off-by: Zack Buhman <zack@buhman.org>
Signed-off-by: Keith Packard <keithp@keithp.com>
Signed-off-by: Yuquan Wang wangyuquan1236@phytium.com.cn
Signed-off-by: Matheus Tavares Bernardino <quic_mathbern@quicinc.com>
Signed-off-by: Cindy Lu <lulu@redhat.com>
Co-authored-by: Peter Maydell <peter.maydell@linaro.org>
Co-authored-by: Fabiano Rosas <farosas@suse.de>
Co-authored-by: Peter Xu <peterx@redhat.com>
Co-authored-by: Thomas Huth <thuth@redhat.com>
Co-authored-by: Cédric Le Goater <clg@redhat.com>
Co-authored-by: Zheyu Ma <zheyuma97@gmail.com>
Co-authored-by: Ido Plat <ido.plat@ibm.com>
Co-authored-by: Ilya Leoshkevich <iii@linux.ibm.com>
Co-authored-by: Markus Armbruster <armbru@redhat.com>
Co-authored-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Co-authored-by: Paolo Bonzini <pbonzini@redhat.com>
Co-authored-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
Co-authored-by: David Hildenbrand <david@redhat.com>
Co-authored-by: Kevin Wolf <kwolf@redhat.com>
Co-authored-by: Stefan Reiter <s.reiter@proxmox.com>
Co-authored-by: Fiona Ebner <f.ebner@proxmox.com>
Co-authored-by: Gregory Price <gregory.price@memverge.com>
Co-authored-by: Lorenz Brun <lorenz@brun.one>
Co-authored-by: Yao Xingtao <yaoxt.fnst@fujitsu.com>
Co-authored-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Co-authored-by: Arnaud Minier <arnaud.minier@telecom-paris.fr>
Co-authored-by: BALATON Zoltan <balaton@eik.bme.hu>
Co-authored-by: Igor Mammedov <imammedo@redhat.com>
Co-authored-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Co-authored-by: Richard Henderson <richard.henderson@linaro.org>
Co-authored-by: Sven Schnelle <svens@stackframe.org>
Co-authored-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Co-authored-by: Helge Deller <deller@kernel.org>
Co-authored-by: Harsh Prateek Bora <harshpb@linux.ibm.com>
Co-authored-by: Benjamin Gray <bgray@linux.ibm.com>
Co-authored-by: Nicholas Piggin <npiggin@gmail.com>
Co-authored-by: Avihai Horon <avihaih@nvidia.com>
Co-authored-by: Michael Tokarev <mjt@tls.msk.ru>
Co-authored-by: Joonas Kankaala <joonas.a.kankaala@gmail.com>
Co-authored-by: Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
Co-authored-by: Stefan Weil <sw@weilnetz.de>
Co-authored-by: Dayu Liu <liu.dayu@zte.com.cn>
Co-authored-by: Zhao Liu <zhao1.liu@intel.com>
Co-authored-by: Glenn Miles <milesg@linux.vnet.ibm.com>
Co-authored-by: Artem Chernyshev <artem.chernyshev@red-soft.ru>
Co-authored-by: Yajun Wu <yajunw@nvidia.com>
Co-authored-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Co-authored-by: Pierre-Clément Tosi <ptosi@google.com>
Co-authored-by: Wei Wang <wei.w.wang@intel.com>
Co-authored-by: Martin Hundebøll <martin@geanix.com>
Co-authored-by: Michael S. Tsirkin <mst@redhat.com>
Co-authored-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Co-authored-by: Wafer <wafer@jaguarmicro.com>
Co-authored-by: lyx634449800 <yuxue.liu@jaguarmicro.com>
Co-authored-by: Gerd Hoffmann <kraxel@redhat.com>
Co-authored-by: Nguyen Dinh Phi <phind.uet@gmail.com>
Co-authored-by: Zack Buhman <zack@buhman.org>
Co-authored-by: Keith Packard <keithp@keithp.com>
Co-authored-by: Yuquan Wang <wangyuquan1236@phytium.com.cn>
Co-authored-by: Matheus Tavares Bernardino <quic_mathbern@quicinc.com>
Co-authored-by: Cindy Lu <lulu@redhat.com>
2024-05-01 16:10:20 +02:00

230 lines
6.2 KiB
C

/*
* QEMU Hyper-V Dynamic Memory Protocol driver
*
* Copyright (C) 2020-2023 Oracle and/or its affiliates.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "hv-balloon-internal.h"
#include "hv-balloon-page_range_tree.h"
/*
* temporarily avoid warnings about enhanced GTree API usage requiring a
* too recent Glib version until GLIB_VERSION_MAX_ALLOWED finally reaches
* the Glib version with this API
*/
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
/* PageRangeTree */
static gint page_range_tree_key_compare(gconstpointer leftp,
gconstpointer rightp,
gpointer user_data)
{
const uint64_t *left = leftp, *right = rightp;
if (*left < *right) {
return -1;
} else if (*left > *right) {
return 1;
} else { /* *left == *right */
return 0;
}
}
static GTreeNode *page_range_tree_insert_new(PageRangeTree tree,
uint64_t start, uint64_t count)
{
uint64_t *key = g_malloc(sizeof(*key));
PageRange *range = g_malloc(sizeof(*range));
assert(count > 0);
*key = range->start = start;
range->count = count;
return g_tree_insert_node(tree.t, key, range);
}
void hvb_page_range_tree_insert(PageRangeTree tree,
uint64_t start, uint64_t count,
uint64_t *dupcount)
{
GTreeNode *node;
bool joinable;
uint64_t intersection;
PageRange *range;
assert(!SUM_OVERFLOW_U64(start, count));
if (count == 0) {
return;
}
node = g_tree_upper_bound(tree.t, &start);
if (node) {
node = g_tree_node_previous(node);
} else {
node = g_tree_node_last(tree.t);
}
if (node) {
range = g_tree_node_value(node);
assert(range);
intersection = page_range_intersection_size(range, start, count);
joinable = page_range_joinable_right(range, start, count);
}
if (!node ||
(!intersection && !joinable)) {
/*
* !node case: the tree is empty or the very first node in the tree
* already has a higher key (the start of its range).
* the other case: there is a gap in the tree between the new range
* and the previous one.
* anyway, let's just insert the new range into the tree.
*/
node = page_range_tree_insert_new(tree, start, count);
assert(node);
range = g_tree_node_value(node);
assert(range);
} else {
/*
* the previous range in the tree either partially covers the new
* range or ends just at its beginning - extend it
*/
if (dupcount) {
*dupcount += intersection;
}
count += start - range->start;
range->count = MAX(range->count, count);
}
/* check next nodes for possible merging */
for (node = g_tree_node_next(node); node; ) {
PageRange *rangecur;
rangecur = g_tree_node_value(node);
assert(rangecur);
intersection = page_range_intersection_size(rangecur,
range->start, range->count);
joinable = page_range_joinable_left(rangecur,
range->start, range->count);
if (!intersection && !joinable) {
/* the current node is disjoint */
break;
}
if (dupcount) {
*dupcount += intersection;
}
count = rangecur->count + (rangecur->start - range->start);
range->count = MAX(range->count, count);
/* the current node was merged in, remove it */
start = rangecur->start;
node = g_tree_node_next(node);
/* no hinted removal in GTree... */
g_tree_remove(tree.t, &start);
}
}
bool hvb_page_range_tree_pop(PageRangeTree tree, PageRange *out,
uint64_t maxcount)
{
GTreeNode *node;
PageRange *range;
node = g_tree_node_last(tree.t);
if (!node) {
return false;
}
range = g_tree_node_value(node);
assert(range);
out->start = range->start;
/* can't modify range->start as it is the node key */
if (range->count > maxcount) {
out->start += range->count - maxcount;
out->count = maxcount;
range->count -= maxcount;
} else {
out->count = range->count;
/* no hinted removal in GTree... */
g_tree_remove(tree.t, &out->start);
}
return true;
}
bool hvb_page_range_tree_intree_any(PageRangeTree tree,
uint64_t start, uint64_t count)
{
GTreeNode *node;
if (count == 0) {
return false;
}
/* find the first node that can possibly intersect our range */
node = g_tree_upper_bound(tree.t, &start);
if (node) {
/*
* a NULL node below means that the very first node in the tree
* already has a higher key (the start of its range).
*/
node = g_tree_node_previous(node);
} else {
/* a NULL node below means that the tree is empty */
node = g_tree_node_last(tree.t);
}
/* node range start <= range start */
if (!node) {
/* node range start > range start */
node = g_tree_node_first(tree.t);
}
for ( ; node; node = g_tree_node_next(node)) {
PageRange *range = g_tree_node_value(node);
assert(range);
/*
* if this node starts beyond or at the end of our range so does
* every next one
*/
if (range->start >= start + count) {
break;
}
if (page_range_intersection_size(range, start, count) > 0) {
return true;
}
}
return false;
}
void hvb_page_range_tree_init(PageRangeTree *tree)
{
tree->t = g_tree_new_full(page_range_tree_key_compare, NULL,
g_free, g_free);
}
void hvb_page_range_tree_destroy(PageRangeTree *tree)
{
/* g_tree_destroy() is not NULL-safe */
if (!tree->t) {
return;
}
g_tree_destroy(tree->t);
tree->t = NULL;
}