Skip to contentSkip to conversation

Return unused memory to hypervisor

PATCH Miroslav Rezanina 2 participants 2 messages Sep 16, 2009 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 1582654680.152201253087647836.JavaMail.root@zmail06.collab.prod.int.phx2.redhat.com

#1 lore raw
Quoted 15 lines

From: "Jeremy Fitzhardinge" <[email protected]>
To: "Miroslav Rezanina" <[email protected]>
Cc: [email protected], [email protected]
Sent: Friday, September 4, 2009 1:26:24 AM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna
Subject: Re: [PATCH][v2.6.29][XEN] Return unused memory to hypervisor

Quoted 1 line · Miroslav Rezanina

when running linux as XEN guest and use boot parameter mem= to set

memory lower then is assigned to guest, not used memory should be returned to hypervisor as free. This is working with kernel available on xen.org pages, but is not working with kernel 2.6.29. Comparing both kernels I found code for returning unused memory to hypervisor is missing. Following patch add this functionality to 2.6.29 kernel.

Quoted 1 line
Quoted 5 lines

Are you planning on submitting a revised patch along the lines I suggested?

Thanks,
J

General version of patch. This version checks the e820 map for holes and returns all memory that is not mapped.

Patch:
===========

arch/x86/xen/enlighten.c +56 -0
index b58e963..acc9166 100644
@@ -31,6 +31,7 @@
 #include <xen/interface/version.h>
 #include <xen/interface/physdev.h>
 #include <xen/interface/vcpu.h>
+#include <xen/interface/memory.h>
 #include <xen/features.h>
 #include <xen/page.h>
 #include <xen/hvc-console.h>
@@ -122,6 +123,59 @@ static int have_vcpu_info_placement =
 #endif
 	;
 
+void __init xen_return_unused_memory(void)
+{
+	static struct e820map holes = {
+		.nr_map = 0
+	};
+	struct xen_memory_reservation reservation = {
+		.address_bits = 0,
+		.extent_order = 0,
+		.domid        = DOMID_SELF
+	};
+	unsigned long last_end = 0;
+	int i;
+
+	for(i=0;i<e820.nr_map;i++) {
+		if (e820.map[i].addr > last_end) {
+			holes.map[holes.nr_map].addr = last_end;
+			holes.map[holes.nr_map].size =
+				e820.map[i].addr - last_end;
+			holes.nr_map++;
+		}		
+		last_end = e820.map[i].addr + e820.map[i].size;
+	}
+
+	if (last_end < PFN_PHYS((u64)xen_start_info->nr_pages)) {
+		holes.map[holes.nr_map].addr=last_end;
+		holes.map[holes.nr_map].size =
+			PFN_PHYS((u64)xen_start_info->nr_pages) - last_end;
+		holes.nr_map++;
+	}
+
+	if (holes.nr_map == 0)
+		return;
+
+	for(i=0;i<holes.nr_map;i++) {
+		unsigned long spfn = holes.map[i].addr >> PAGE_SHIFT;
+		unsigned long epfn = ((holes.map[i].addr + holes.map[i].size) >> PAGE_SHIFT);
+		int ret;
+
+		if (spfn % PAGE_SIZE != 0)
+			spfn++;
+
+		if (spfn >= epfn)
+			continue;
+
+		set_xen_guest_handle(reservation.extent_start,
+			((unsigned long *)xen_start_info->mfn_list) + spfn);
+
+		reservation.nr_extents = epfn - spfn;
+		ret = HYPERVISOR_memory_op(XENMEM_decrease_reservation,
+			&reservation);
+		BUG_ON (ret != epfn - spfn);
+	}
+}
 
 static void xen_vcpu_setup(int cpu)
 {
@@ -1057,6 +1111,8 @@ static __init void xen_post_allocator_init(void)
 	SetPagePinned(virt_to_page(level3_user_vsyscall));
 #endif
 	xen_mark_init_mm_pinned();
+
+/*	xen_return_unused_memory(); */
 }
 
 /* This is called once we have the cpu_possible_map */
Signature · 2 lines
Miroslav Rezanina
Software Engineer - Virtualization Team - XEN kernel
#2 lore raw

. snip ..

arch/x86/xen/enlighten.c static __init void xen_post_allocator_init(void) · line 1115

Quoted 8 lines

static void xen_vcpu_setup(int cpu)
{

arch/x86/xen/enlighten.c +2 -0
@@ -1057,6 +1111,8 @@ static __init void xen_post_allocator_init(void)
 	SetPagePinned(virt_to_page(level3_user_vsyscall));
 #endif
 	xen_mark_init_mm_pinned();
+
+/*	xen_return_unused_memory(); */

Should this be commented out? Or do you plan to provide another patch to enable this after extensive testing?