Skip to contentSkip to conversation

Adding support for hyper-v enlightments hv-extcalls and hv-memory-hint (hypercall HvExtCallMemoryHeatHint)

PATCH New Shmuel Fomberg 2 participants 2 messages Oct 14 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Adding support for hyper-v enlightments hv-extcalls and hv-memory-hint (hypercall HvExtCallMemoryHeatHint)

---
 hw/hyperv/hv-gpa-range.c                      |  59 ++++
 include/hw/hyperv/hv-gpa-range.h              |  52 ++++
 .../functional/x86_64/test_hyperv_extcalls.py | 101 +++++++
 tests/unit/test-hv-gpa-range.c                | 271 ++++++++++++++++++
 4 files changed, 483 insertions(+)
 create mode 100644 hw/hyperv/hv-gpa-range.c
 create mode 100644 include/hw/hyperv/hv-gpa-range.h
 create mode 100644 tests/functional/x86_64/test_hyperv_extcalls.py
 create mode 100644 tests/unit/test-hv-gpa-range.c
hw/hyperv/hv-gpa-range.c new file +59 -0
new file mode 100644
index 0000000000..e6d3d3eb42
@@ -0,0 +1,59 @@
+/*
+ * Hyper-V extended memory heat-hint GPA range helpers.
+ *
+ * Copyright (c) 2015-2018 Virtuozzo International GmbH.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/hyperv/hv-gpa-range.h"
+#include "hw/hyperv/hyperv-proto.h"
+
+bool hv_decode_gpa_page_range(uint64_t raw, hwaddr *gpa, hwaddr *len)
+{
+    uint64_t pages = (raw & HV_GPA_PAGE_RANGE_ADDITIONAL_MASK) + 1;
+
+    if (raw & HV_GPA_PAGE_RANGE_LARGEPAGE) {
+        if (raw & HV_GPA_PAGE_RANGE_PAGE_SIZE_1GB) {
+            *gpa = raw & HV_GPA_PAGE_RANGE_1G_GPA_MASK;
+            *len = pages << 30;
+        } else {
+            *gpa = raw & HV_GPA_PAGE_RANGE_2M_GPA_MASK;
+            *len = pages << 21;
+        }
+    } else {
+        *gpa = raw & HV_GPA_PAGE_RANGE_4K_GPA_MASK;
+        *len = pages << 12;
+    }
+
+    return *len != 0;
+}
+
+bool hv_madvise_walk_step(hwaddr *gpa, hwaddr *len,
+                          hwaddr section_start, hwaddr section_size,
+                          hwaddr *out_gpa, hwaddr *out_len)
+{
+    hwaddr skip;
+
+    /*
+     * A hole with no memory region makes the section start past the
+     * cursor; a zero-size section means there is nothing left to find.
+     * Either way, stop -- do not advise or advance past the gap.
+     */
+    if (!section_size || section_start < *gpa) {
+        return false;
+    }
+
+    skip = section_start - *gpa;
+    if (skip >= *len || section_size > *len - skip) {
+        return false;
+    }
+
+    *out_gpa = section_start;
+    *out_len = section_size;
+
+    *gpa = section_start + section_size;
+    *len -= skip + section_size;
+    return true;
+}
include/hw/hyperv/hv-gpa-range.h new file +52 -0

52 lines not on this page · raw message

tests/functional/x86_64/test_hyperv_extcalls.py new file +101 -0

101 lines not on this page · raw message

tests/unit/test-hv-gpa-range.c new file +271 -0

271 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] qemu: hv_madvise_walk_step drops overlapping memory hints --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] qemu: hv_madvise_walk_step drops overlapping memory hints
--

commit e5fd145c0d62c5737370ada9533f9341dfc23d5a
Author: Shmuel Fomberg <[email protected]>

Adding support for hyper-v enlightments hv-extcalls and hv-memory-hint (hypercall HvExtCallMemoryHeatHint)

This commit introduces support for Hyper-V extended memory heat-hint GPA range helpers. It includes the API for decoding and walking memory ranges, along with corresponding unit and functional tests.

Quoted 5 lines
hw/hyperv/hv-gpa-range.c new file +0 -0
new file mode 100644
index 0000000000000..e6d3d3eb42f7b

[ ... ]

Quoted 9 lines
hw/hyperv/hv-gpa-range.c new file +9 -0
+bool hv_madvise_walk_step(hwaddr *gpa, hwaddr *len,
+                          hwaddr section_start, hwaddr section_size,
+                          hwaddr *out_gpa, hwaddr *out_len)
+{
+    hwaddr skip;
+
+    /*
+     * A hole with no memory region makes the section start past the
+     * cursor; a zero-size section means there is nothing left to find.

hw/hyperv/hv-gpa-range.c line 51

Quoted 10 lines
hw/hyperv/hv-gpa-range.c new file +10 -0
+     * Either way, stop -- do not advise or advance past the gap.
+     */
+    if (!section_size || section_start < *gpa) {
+        return false;
+    }
+
+    skip = section_start - *gpa;
+    if (skip >= *len || section_size > *len - skip) {
+        return false;
+    }

[Severity: Medium]
Does this incorrectly drop overlapping memory hints?

If memory_region_find() returns a section that extends beyond the originally requested range, section_size > *len - skip evaluates to true and the walk is aborted entirely by returning false.

Should this instead clamp the section_size to the remaining length (*len - skip) so that the valid overlapping portion is processed rather than discarded?

[ ... ]

Quoted 5 lines
tests/unit/test-hv-gpa-range.c new file +0 -0
new file mode 100644
index 0000000000000..70196f271b886

[ ... ]

tests/unit/test-hv-gpa-range.c line 194

Quoted 8 lines
tests/unit/test-hv-gpa-range.c new file +8 -0
+static void test_walk_section_past_requested_range(void)
+{
+    /* Section is larger than what's left to process -> stop, don't act */
+    hwaddr gpa = 0x1000, len = 0x1000;
+    hwaddr out_gpa = 0xdead, out_len = 0xdead;
+
+    g_assert_false(hv_madvise_walk_step(&gpa, &len, 0x1000, 0x2000,
+                                        &out_gpa, &out_len));

[Severity: Medium]
This unit test appears to strictly enforce the behavior of failing when a section extends past the requested range.

If hv_madvise_walk_step() is updated to clamp the returned size instead of failing, will this test also need to be adjusted to expect the successful clamped values?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1