From: Juergen Gross <jgross@suse.com>
To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>,
samuel.thibault@ens-lyon.org, wei.liu2@citrix.com
Subject: [PATCH v2 17/18] mini-os: add support for ballooning up
Date: Fri, 5 Aug 2016 19:36:01 +0200 [thread overview]
Message-ID: <1470418562-11234-18-git-send-email-jgross@suse.com> (raw)
In-Reply-To: <1470418562-11234-1-git-send-email-jgross@suse.com>
Add support for ballooning the domain up by a specified amount of
pages. Following steps are performed:
- extending the p2m map
- extending the page allocator's bitmap
- getting new memory pages from the hypervisor
- adding the memory at the current end of guest memory
Signed-off-by: Juergen Gross <jgross@suse.com>
---
arch/arm/balloon.c | 9 ++++++
arch/x86/balloon.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
balloon.c | 64 +++++++++++++++++++++++++++++++++++++
include/balloon.h | 5 +++
mm.c | 4 +++
5 files changed, 176 insertions(+)
diff --git a/arch/arm/balloon.c b/arch/arm/balloon.c
index a76db3b..fbb8007 100644
--- a/arch/arm/balloon.c
+++ b/arch/arm/balloon.c
@@ -27,4 +27,13 @@
unsigned long virt_kernel_area_end; /* TODO: find a virtual area */
+int arch_expand_p2m(unsigned long max_pfn)
+{
+ return 0;
+}
+
+void arch_pfn_add(unsigned long pfn, unsigned long mfn)
+{
+}
+
#endif
diff --git a/arch/x86/balloon.c b/arch/x86/balloon.c
index db37e8f..3d7692f 100644
--- a/arch/x86/balloon.c
+++ b/arch/x86/balloon.c
@@ -23,6 +23,7 @@
#include <mini-os/os.h>
#include <mini-os/balloon.h>
+#include <mini-os/errno.h>
#include <mini-os/lib.h>
#include <mini-os/mm.h>
@@ -30,9 +31,36 @@
unsigned long virt_kernel_area_end = VIRT_KERNEL_AREA;
+static void p2m_invalidate(unsigned long *list, unsigned long start_idx)
+{
+ unsigned long idx;
+
+ for ( idx = start_idx; idx < P2M_ENTRIES; idx++ )
+ list[idx] = INVALID_P2M_ENTRY;
+}
+
+static inline unsigned long *p2m_l3list(void)
+{
+ return mfn_to_virt(HYPERVISOR_shared_info->arch.pfn_to_mfn_frame_list_list);
+}
+
+static inline unsigned long *p2m_to_virt(unsigned long p2m)
+{
+ return ( p2m == INVALID_P2M_ENTRY ) ? NULL : mfn_to_virt(p2m);
+}
+
void arch_remap_p2m(unsigned long max_pfn)
{
unsigned long pfn;
+ unsigned long *l3_list, *l2_list, *l1_list;
+
+ l3_list = p2m_l3list();
+ l2_list = p2m_to_virt(l3_list[L3_P2M_IDX(max_pfn - 1)]);
+ l1_list = p2m_to_virt(l2_list[L2_P2M_IDX(max_pfn - 1)]);
+
+ p2m_invalidate(l3_list, L3_P2M_IDX(max_pfn - 1) + 1);
+ p2m_invalidate(l2_list, L2_P2M_IDX(max_pfn - 1) + 1);
+ p2m_invalidate(l1_list, L1_P2M_IDX(max_pfn - 1) + 1);
if ( p2m_pages(nr_max_pages) <= p2m_pages(max_pfn) )
return;
@@ -49,4 +77,70 @@ void arch_remap_p2m(unsigned long max_pfn)
virt_kernel_area_end += PAGE_SIZE * p2m_pages(nr_max_pages);
}
+int arch_expand_p2m(unsigned long max_pfn)
+{
+ unsigned long pfn;
+ unsigned long *l1_list, *l2_list, *l3_list;
+
+ p2m_chk_pfn(max_pfn - 1);
+ l3_list = p2m_l3list();
+
+ for ( pfn = (HYPERVISOR_shared_info->arch.max_pfn + P2M_MASK) & ~P2M_MASK;
+ pfn < max_pfn; pfn += P2M_ENTRIES )
+ {
+ l2_list = p2m_to_virt(l3_list[L3_P2M_IDX(pfn)]);
+ if ( !l2_list )
+ {
+ l2_list = (unsigned long*)alloc_page();
+ if ( !l2_list )
+ return -ENOMEM;
+ p2m_invalidate(l2_list, 0);
+ l3_list[L3_P2M_IDX(pfn)] = virt_to_mfn(l2_list);
+ }
+ l1_list = p2m_to_virt(l2_list[L2_P2M_IDX(pfn)]);
+ if ( !l1_list )
+ {
+ l1_list = (unsigned long*)alloc_page();
+ if ( !l1_list )
+ return -ENOMEM;
+ p2m_invalidate(l1_list, 0);
+ l2_list[L2_P2M_IDX(pfn)] = virt_to_mfn(l1_list);
+
+ if ( map_frame_rw((unsigned long)(phys_to_machine_mapping + pfn),
+ l2_list[L2_P2M_IDX(pfn)]) )
+ return -ENOMEM;
+ }
+ }
+
+ HYPERVISOR_shared_info->arch.max_pfn = max_pfn;
+
+ /* Make sure the new last page can be mapped. */
+ if ( !need_pgt((unsigned long)pfn_to_virt(max_pfn - 1)) )
+ return -ENOMEM;
+
+ return 0;
+}
+
+void arch_pfn_add(unsigned long pfn, unsigned long mfn)
+{
+ mmu_update_t mmu_updates[1];
+ pgentry_t *pgt;
+ int rc;
+
+ phys_to_machine_mapping[pfn] = mfn;
+
+ pgt = need_pgt((unsigned long)pfn_to_virt(pfn));
+ ASSERT(pgt);
+ mmu_updates[0].ptr = virt_to_mach(pgt) | MMU_NORMAL_PT_UPDATE;
+ mmu_updates[0].val = (pgentry_t)(mfn << PAGE_SHIFT) |
+ _PAGE_PRESENT | _PAGE_RW;
+ rc = HYPERVISOR_mmu_update(mmu_updates, 1, NULL, DOMID_SELF);
+ if ( rc < 0 )
+ {
+ printk("ERROR: build_pagetable(): PTE could not be updated\n");
+ printk(" mmu_update failed with rc=%d\n", rc);
+ do_exit();
+ }
+}
+
#endif
diff --git a/balloon.c b/balloon.c
index 75b87c8..30e571c 100644
--- a/balloon.c
+++ b/balloon.c
@@ -23,11 +23,13 @@
#include <mini-os/os.h>
#include <mini-os/balloon.h>
+#include <mini-os/errno.h>
#include <mini-os/lib.h>
#include <xen/xen.h>
#include <xen/memory.h>
unsigned long nr_max_pages;
+unsigned long nr_mem_pages;
void get_max_pages(void)
{
@@ -61,3 +63,65 @@ void alloc_bitmap_remap(void)
alloc_bitmap = (unsigned long *)virt_kernel_area_end;
virt_kernel_area_end += round_pgup((nr_max_pages + 1) >> (PAGE_SHIFT + 3));
}
+
+#define N_BALLOON_FRAMES 64
+static unsigned long balloon_frames[N_BALLOON_FRAMES];
+
+int balloon_up(unsigned long n_pages)
+{
+ unsigned long page, pfn;
+ int rc;
+ struct xen_memory_reservation reservation = {
+ .address_bits = 0,
+ .extent_order = 0,
+ .domid = DOMID_SELF
+ };
+
+ if ( n_pages > nr_max_pages - nr_mem_pages )
+ n_pages = nr_max_pages - nr_mem_pages;
+ if ( n_pages > N_BALLOON_FRAMES )
+ n_pages = N_BALLOON_FRAMES;
+
+ /* Resize alloc_bitmap if necessary. */
+ if ( alloc_bitmap_size * 8 < nr_mem_pages + n_pages )
+ {
+ page = alloc_page();
+ if ( !page )
+ return -ENOMEM;
+
+ memset((void *)page, ~0, PAGE_SIZE);
+ if ( map_frame_rw((unsigned long)alloc_bitmap + alloc_bitmap_size,
+ virt_to_mfn(page)) )
+ {
+ free_page((void *)page);
+ return -ENOMEM;
+ }
+
+ alloc_bitmap_size += PAGE_SIZE;
+ }
+
+ rc = arch_expand_p2m(nr_mem_pages + n_pages);
+ if ( rc )
+ return rc;
+
+ /* Get new memory from hypervisor. */
+ for ( pfn = 0; pfn < n_pages; pfn++ )
+ {
+ balloon_frames[pfn] = nr_mem_pages + pfn;
+ }
+ set_xen_guest_handle(reservation.extent_start, balloon_frames);
+ reservation.nr_extents = n_pages;
+ rc = HYPERVISOR_memory_op(XENMEM_populate_physmap, &reservation);
+ if ( rc <= 0 )
+ return rc;
+
+ for ( pfn = 0; pfn < rc; pfn++ )
+ {
+ arch_pfn_add(nr_mem_pages + pfn, balloon_frames[pfn]);
+ free_page(pfn_to_virt(nr_mem_pages + pfn));
+ }
+
+ nr_mem_pages += rc;
+
+ return rc;
+}
diff --git a/include/balloon.h b/include/balloon.h
index 0e2340b..8bf77e5 100644
--- a/include/balloon.h
+++ b/include/balloon.h
@@ -28,10 +28,15 @@
extern unsigned long nr_max_pages;
extern unsigned long virt_kernel_area_end;
+extern unsigned long nr_mem_pages;
void get_max_pages(void);
+int balloon_up(unsigned long n_pages);
+
void arch_remap_p2m(unsigned long max_pfn);
void alloc_bitmap_remap(void);
+int arch_expand_p2m(unsigned long max_pfn);
+void arch_pfn_add(unsigned long pfn, unsigned long mfn);
#else /* CONFIG_BALLOON */
diff --git a/mm.c b/mm.c
index 13bb3e5..092c5f9 100644
--- a/mm.c
+++ b/mm.c
@@ -381,6 +381,10 @@ void init_mm(void)
arch_init_p2m(max_pfn);
arch_init_demand_mapping_area();
+
+#ifdef CONFIG_BALLOON
+ nr_mem_pages = max_pfn;
+#endif
}
void fini_mm(void)
--
2.6.6
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel
next prev parent reply other threads:[~2016-08-05 17:36 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-05 17:35 [PATCH v2 00/18] mini-os: support of auto-ballooning Juergen Gross
2016-08-05 17:35 ` [PATCH v2 01/18] mini-os: correct first free pfn Juergen Gross
2016-08-10 20:02 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 02/18] mini-os: remove unused alloc_contig_pages() function Juergen Gross
2016-08-10 20:02 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 03/18] mini-os: remove MM_DEBUG code Juergen Gross
2016-08-10 20:03 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 04/18] mini-os: add description of x86 memory usage Juergen Gross
2016-08-10 20:04 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 05/18] mini-os: add nr_free_pages counter Juergen Gross
2016-08-10 20:05 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 06/18] mini-os: let memory allocation fail if no free page available Juergen Gross
2016-08-10 20:06 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 07/18] mini-os: add ballooning config item Juergen Gross
2016-08-10 20:10 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 08/18] mini-os: get maximum memory size from hypervisor Juergen Gross
2016-08-10 20:11 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 09/18] mini-os: modify virtual memory layout for support of ballooning Juergen Gross
2016-08-10 20:16 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 10/18] mini-os: remove unused mem_test() function Juergen Gross
2016-08-10 20:17 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 11/18] mini-os: add checks for out of memory Juergen Gross
2016-08-10 20:18 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 12/18] mini-os: don't allocate new pages for level 1 p2m tree Juergen Gross
2016-08-10 20:24 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 13/18] mini-os: add function to map one frame Juergen Gross
2016-08-10 20:26 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 14/18] mini-os: move p2m related macros to header file Juergen Gross
2016-08-10 20:29 ` Samuel Thibault
2016-08-05 17:35 ` [PATCH v2 15/18] mini-os: remap p2m list in case of ballooning Juergen Gross
2016-08-10 20:35 ` Samuel Thibault
2016-08-10 20:41 ` Samuel Thibault
2016-08-11 4:04 ` Juergen Gross
2016-08-05 17:36 ` [PATCH v2 16/18] mini-os: map page allocator's bitmap to virtual kernel area for ballooning Juergen Gross
2016-08-10 20:45 ` Samuel Thibault
2016-08-11 4:05 ` Juergen Gross
2016-08-05 17:36 ` Juergen Gross [this message]
2016-08-10 21:02 ` [PATCH v2 17/18] mini-os: add support for ballooning up Samuel Thibault
2016-08-11 5:50 ` Juergen Gross
2016-08-05 17:36 ` [PATCH v2 18/18] mini-os: balloon up in case of oom Juergen Gross
2016-08-10 21:07 ` Samuel Thibault
2016-08-11 5:51 ` Juergen Gross
2016-08-10 21:07 ` [PATCH v2 00/18] mini-os: support of auto-ballooning Samuel Thibault
2016-08-11 5:52 ` Juergen Gross
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1470418562-11234-18-git-send-email-jgross@suse.com \
--to=jgross@suse.com \
--cc=minios-devel@lists.xenproject.org \
--cc=samuel.thibault@ens-lyon.org \
--cc=wei.liu2@citrix.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).