[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[XENBUS PATCH v2 1/2] Use Windows 10 APIs to inflate/deflate balloon



Currently the balloon driver allocates pages as normal memory, which
causes Windows to report the ballooned memory as kernel-allocated
instead of unavailable.

Windows 10 provides MM_ALLOCATE_AND_HOT_REMOVE to remove pages from
the physical memory pool. The removed pages can be added back with
MmAddPhysicalMemory.

Using this feature, the real VM memory capacity will be correctly
reported in Task Manager.

Balloon deflation is implemented using calls to MmAddPhysicalMemory on
contiguous PFN ranges.

Signed-off-by: Tu Dinh <ngoc-tu.dinh@xxxxxxxxxx>
---
v2: Updated commit message to say what the patch does. No code change.
---
 src/xenbus/balloon.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/src/xenbus/balloon.c b/src/xenbus/balloon.c
index 8d7e2c2..690e45f 100644
--- a/src/xenbus/balloon.c
+++ b/src/xenbus/balloon.c
@@ -194,6 +194,7 @@ BalloonSort(
         ASSERT3U(PfnArray[Index], <, PfnArray[Index + 1]);
 }
 
+__drv_requiresIRQL(PASSIVE_LEVEL)
 static PMDL
 BalloonAllocatePagesForMdl(
     IN  ULONG       Count
@@ -205,6 +206,8 @@ BalloonAllocatePagesForMdl(
     SIZE_T          TotalBytes;
     PMDL            Mdl;
 
+    ASSERT3U(KeGetCurrentIrql(), ==, PASSIVE_LEVEL);
+
     LowAddress.QuadPart = 0ull;
     HighAddress.QuadPart = ~0ull;
     SkipBytes.QuadPart = 0ull;
@@ -215,7 +218,8 @@ BalloonAllocatePagesForMdl(
                                   SkipBytes,
                                   TotalBytes,
                                   MmCached,
-                                  MM_DONT_ZERO_ALLOCATION);
+                                  MM_DONT_ZERO_ALLOCATION |
+                                  MM_ALLOCATE_AND_HOT_REMOVE);
     if (Mdl == NULL)
         goto done;
 
@@ -238,6 +242,9 @@ BalloonFreePagesFromMdl(
 {
     volatile UCHAR  *Mapping;
     ULONG           Index;
+    PPFN_NUMBER     Pfn;
+    PFN_NUMBER      RangeStart, RangeEnd;
+    NTSTATUS        Status;
 
     if (!Check)
         goto done;
@@ -280,7 +287,34 @@ BalloonFreePagesFromMdl(
     MmUnmapLockedPages((PVOID)Mapping, Mdl);
 
 done:
-    MmFreePagesFromMdl(Mdl);
+    Pfn = MmGetMdlPfnArray(Mdl);
+
+    RangeStart = 0;
+    while (RangeStart < (MmGetMdlByteCount(Mdl) >> PAGE_SHIFT)) {
+        PHYSICAL_ADDRESS    StartAddress;
+        LARGE_INTEGER       NumberOfBytes;
+
+        for (RangeEnd = RangeStart;
+             RangeEnd < (MmGetMdlByteCount(Mdl) >> PAGE_SHIFT);
+             RangeEnd++) {
+            if (Pfn[RangeEnd] != Pfn[RangeStart] + (RangeEnd - RangeStart))
+                break;
+        }
+
+        StartAddress.QuadPart = Pfn[RangeStart] << PAGE_SHIFT;
+        NumberOfBytes.QuadPart = (RangeEnd - RangeStart) << PAGE_SHIFT;
+
+        Status = MmAddPhysicalMemory(&StartAddress, &NumberOfBytes);
+        if (!NT_SUCCESS(Status)) {
+            Error("MmAddPhysicalMemory failed: %08x (PFN %llx + %llx pages)\n",
+                  Status,
+                  Pfn[RangeStart],
+                  RangeEnd - RangeStart);
+            break;
+        }
+
+        RangeStart = RangeEnd;
+    }
 }
 
 #define XENBUS_BALLOON_MIN_PAGES_PER_S 1000ull
-- 
2.49.0.windows.1



Ngoc Tu Dinh | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.