mini-os: prepare for support of ballooning
Subsystems (3)
cover mini-os: prepare for support of ballooning
Do some cleanups, a small correction and add some basic features to lay groundwork for support of ballooning in Mini-OS.
The main visible change is the virtual memory layout: to be able to add memory to the running Mini-OS we need to have some spare areas especially after the 1:1 mapping of physical memory.
The whole series has been tested with:
- pure mini-os
- xenstore stubdom
- ioemu stubdom
- pvgrub 64 bit
pvgrub 32 bit didn't work before applying the series, it just entered the grub shell. With the series applied the behavior was exactly the same.
Juergen Gross (9):
mini-os: correct first free pfn
mini-os: remove unused alloc_contig_pages() function
mini-os: remove MM_DEBUG code
mini-os: add description of x86 memory usage
mini-os: add nr_free_pages counter
mini-os: let memory allocation fail if no free page available
mini-os: add ballooning config item
mini-os: get maximum memory size from hypervisor
mini-os: modify virtual memory layout for support of ballooning
Makefile | 2 + arch/arm/mm.c | 2 +- arch/x86/mm.c | 191 ++++---------------------------------------------- include/mm.h | 5 +- include/x86/arch_mm.h | 44 ++++++++++++ mm.c | 95 +++++++++---------------- 6 files changed, 97 insertions(+), 242 deletions(-)
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 1/9 mini-os: correct first free pfn
The first free pfn available for allocation is calculated by adding the number of page table frames to the pfn of the first page table and then the magic number 3 to account for start info page et al.
As the start info page, xenstore page and console page are allocated _before_ the page tables leaving room for these pages behind the page tables makes no sense.
--- arch/x86/mm.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/mm.c +2 -3
@@ -867,9 +867,8 @@ void arch_init_mm(unsigned long* start_pfn_p, unsigned long* max_pfn_p)printk ("stack start: %p(VA)\n" , stack);printk (" _end: %p(VA)\n" , &_end); -/* First page follows page table pages and 3 more pages (store page etc) */ - start_pfn =PFN_UP (to_phys (start_info.pt_base)) + - start_info.nr_pt_frames +3 ; +/* First page follows page table pages. */ + start_pfn =PFN_UP (to_phys (start_info.pt_base)) + start_info.nr_pt_frames; max_pfn = start_info.nr_pages;/* We need room for demand mapping and heap, clip available memory */
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 2/9 mini-os: remove unused alloc_contig_pages() function
In reply to Juergen Grossalloc_contig_pages() is never used anywhere in mini-os. Remove it.
--- arch/x86/mm.c | 142 ---------------------------------------------------------- include/mm.h | 1 - 2 files changed, 143 deletions(-)
arch/x86/mm.c +0 -142
@@ -652,148 +652,6 @@ int unmap_frames(unsigned long va, unsigned long num_frames) }/* -* Allocate pages which are contiguous in machine memory. -* Returns a VA to where they are mapped or 0 on failure. -* -* addr_bits indicates if the region has restrictions on where it is -* located. Typical values are 32 (if for example PCI devices can't access -* 64bit memory) or 0 for no restrictions. -* -* Allocated pages can be freed using the page allocators free_pages() -* function. -* -* based on Linux function xen_create_contiguous_region() -*/ -#define MAX_CONTIG_ORDER 9/* 2MB */ -unsigned long alloc_contig_pages (int order,unsigned int addr_bits) -{ -unsigned long in_va, va; -unsigned long in_frames[1UL << order], out_frames, mfn; -multicall_entry_t call[1UL << order]; -unsigned int i, num_pages =1UL << order; -int ret, exch_success; - -/* pass in num_pages 'extends' of size 1 and -* request 1 extend of size 'order */ -struct xen_memory_exchange exchange = { - .in = { - .nr_extents = num_pages, - .extent_order =0 , - .domid =DOMID_SELF - }, - .out = { - .nr_extents =1 , - .extent_order = order, - .address_bits = addr_bits, - .domid =DOMID_SELF - }, - .nr_exchanged =0 - }; - -if ( order >MAX_CONTIG_ORDER ) - { -printk ("alloc_contig_pages: order too large 0x%x > 0x%x\n" , - order,MAX_CONTIG_ORDER ); -return 0 ; - } - -/* Allocate some potentially discontiguous pages */ - in_va =alloc_pages (order); -if ( !in_va ) - { -printk ("alloc_contig_pages: could not get enough pages (order=0x%x\n" , - order); -return 0 ; - } - -/* set up arguments for exchange hyper call */ -set_xen_guest_handle (exchange.in.extent_start, in_frames); -set_xen_guest_handle (exchange.out.extent_start, &out_frames); - -/* unmap current frames, keep a list of MFNs */ -for ( i =0 ; i < num_pages; i++ ) - { -int arg =0 ; - - va = in_va + (PAGE_SIZE * i); - in_frames[i] =virt_to_mfn (va); - -/* update P2M mapping */ - phys_to_machine_mapping[virt_to_pfn (va)] =INVALID_P2M_ENTRY ; - -/* build multi call */ - call[i].op = __HYPERVISOR_update_va_mapping; - call[i].args[arg++] = va; - call[i].args[arg++] =0 ; -#ifdef __i386__ - call[i].args[arg++] =0 ; -#endif - call[i].args[arg++] =UVMF_INVLPG ; - } - - ret =HYPERVISOR_multicall (call, i); -if ( ret ) - { -printk ("Odd, update_va_mapping hypercall failed with rc=%d.\n" , ret); -return 0 ; - } - -/* try getting a contig range of MFNs */ - out_frames =virt_to_pfn (in_va);/* PFNs to populate */ - ret =HYPERVISOR_memory_op (XENMEM_exchange, &exchange); -if ( ret ) { -printk ("mem exchanged order=0x%x failed with rc=%d, nr_exchanged=%lu\n" , - order, ret, exchange.nr_exchanged); -/* we still need to return the allocated pages above to the pool -* ie. map them back into the 1:1 mapping etc. so we continue but -* in the end return the pages to the page allocator and return 0. */ - exch_success =0 ; - } -else - exch_success =1 ; - -/* map frames into 1:1 and update p2m */ -for ( i =0 ; i < num_pages; i++ ) - { -int arg =0 ; -pte_t pte; - - va = in_va + (PAGE_SIZE * i); - mfn = i < exchange.nr_exchanged ? (out_frames + i) : in_frames[i]; - pte =__pte (mfn <<PAGE_SHIFT |L1_PROT ); - -/* update P2M mapping */ - phys_to_machine_mapping[virt_to_pfn (va)] = mfn; - -/* build multi call */ - call[i].op = __HYPERVISOR_update_va_mapping; - call[i].args[arg++] = va; -#ifdef __x86_64__ - call[i].args[arg++] = (pgentry_t )pte.pte; -#else - call[i].args[arg++] = pte.pte_low; - call[i].args[arg++] = pte.pte_high; -#endif - call[i].args[arg++] =UVMF_INVLPG ; - } - ret =HYPERVISOR_multicall (call, i); -if ( ret ) - { -printk ("update_va_mapping hypercall no. 2 failed with rc=%d.\n" , ret); -return 0 ; - } - -if ( !exch_success ) - { -/* since the exchanged failed we just free the pages as well */ -free_pages ((void *) in_va, order); -return 0 ; - } - -return in_va; -} - -/* * Clear some of the bootstrap memory */ static void clear_bootstrap (void )
include/mm.h +0 -1
@@ -71,7 +71,6 @@ void do_map_frames(unsigned long addr, const unsigned long *f, unsigned long n, unsigned long stride, unsigned long increment, domid_t id, int *err, unsigned long prot);int unmap_frames (unsigned long va,unsigned long num_frames); -unsigned long alloc_contig_pages (int order,unsigned int addr_bits);#ifdef HAVE_LIBC extern unsigned long heap, brk, heap_mapped, heap_end;#endif
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 3/9 mini-os: remove MM_DEBUG code
In reply to Juergen Grossmm.c contains unused code inside #ifdef MM_DEBUG areas. Its usability is rather questionable and some parts are even wrong (e.g. print_chunks() called with nr_pages > 1000 will clobber an arbitrary stack content with a 0 byte).
Remove this code.
--- mm.c | 60 ------------------------------------------------------------ 1 file changed, 60 deletions(-)
mm.c +0 -60
@@ -42,13 +42,6 @@#include <mini-os/lib.h> #include <mini-os/xmalloc.h> -#ifdef MM_DEBUG -#define DEBUG(_f, _a...) \ - printk("MINI_OS(file=mm.c, line=%d) " _f "\n", __LINE__, ## _a) -#else -#define DEBUG(_f, _a...) ((void)0) -#endif -/********************* * ALLOCATION BITMAP * One bit per page of memory. Bit set => page is allocated.
@@ -140,59 +133,6 @@ static chunk_head_t free_tail[FREELIST_SIZE];#define round_pgdown (_p) ((_p)&PAGE_MASK)#define round_pgup (_p) (((_p)+(PAGE_SIZE-1))&PAGE_MASK) -#ifdef MM_DEBUG -/* -* Prints allocation[0/1] for @nr_pages, starting at @start -* address (virtual). -*/ -USED static void print_allocation(void *start, int nr_pages) -{ -unsigned long pfn_start =virt_to_pfn (start); -int count; -for (count =0 ; count < nr_pages; count++) -if (allocated_in_map (pfn_start + count))printk ("1" ); -else printk ("0" ); - -printk ("\n" ); -} - -/* -* Prints chunks (making them with letters) for @nr_pages starting -* at @start (virtual). -*/ -USED static void print_chunks(void *start, int nr_pages) -{ -char chunks[1001 ], current='A' ; -int order, count; -chunk_head_t *head; -unsigned long pfn_start =virt_to_pfn (start); - -memset (chunks, (int )'_' ,1000 ); -if (nr_pages >1000 ) - { -DEBUG ("Can only pring 1000 pages. Increase buffer size." ); - } - -for (order=0 ; order <FREELIST_SIZE ; order++) - { - head = free_head[order]; -while (!FREELIST_EMPTY (head)) - { -for (count =0 ; count <1UL << head->level; count++) - { -if (count +virt_to_pfn (head) - pfn_start <1000 ) - chunks[count +virt_to_pfn (head) - pfn_start] = current; - } - head = head->next; - current++; - } - } - chunks[nr_pages] ='\0' ; -printk ("%s\n" , chunks); -} -#endif - -/* * Initialise allocator, placing addresses [@min,@max] in free pool. * @min and @max are PHYSICAL addresses.
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 4/9 mini-os: add description of x86 memory usage
In reply to Juergen GrossAdd a brief description how the physical and virtual address usage looks like on x86 to include/x86/arch_mm.h
--- include/x86/arch_mm.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/x86/arch_mm.h +20 -0
@@ -36,6 +36,26 @@#endif #endif +/* +* Physical address space usage: +* +* 0..._edata: kernel text/data +* *stack : kernel stack (thread 0) +* hypervisor allocated data: p2m_list, start_info page, xenstore page, +* console page, initial page tables +* bitmap of allocated pages +* pages controlled by the page allocator +* +* +* Virtual address space usage: +* +* 1:1 mapping of physical memory starting at VA(0) +* 1 unallocated page +* demand map area (32 bits: 2 GB, 64 bits: 128 GB) for virtual allocations +* 1 unallocated page +* with libc: heap area (32 bits: 1 GB, 64 bits: 128 GB) +*/ +#define L1_FRAME 1#define L2_FRAME 2#define L3_FRAME 3
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 5/9 mini-os: add nr_free_pages counter
In reply to Juergen GrossAdd a variable holding the number of available memory pages. This will aid auto-ballooning later.
--- include/mm.h | 1 + mm.c | 6 ++++++ 2 files changed, 7 insertions(+)
include/mm.h +1 -0
@@ -42,6 +42,7 @@#define STACK_SIZE_PAGE_ORDER __STACK_SIZE_PAGE_ORDER#define STACK_SIZE __STACK_SIZE +extern unsigned long nr_free_pages;void init_mm (void );unsigned long alloc_pages (int order);
mm.c +6 -0
@@ -53,6 +53,8 @@ static unsigned long *alloc_bitmap;#define allocated_in_map (_pn) \ (alloc_bitmap[(_pn)/PAGES_PER_MAPWORD] & (1UL<<((_pn)&(PAGES_PER_MAPWORD-1)))) +unsigned long nr_free_pages; +/* * Hint regarding bitwise arithmetic in map_{alloc,free}: * -(1<<n) sets all bits >= n.
@@ -81,6 +83,8 @@ static void map_alloc(unsigned long first_page, unsigned long nr_pages)while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0UL ; alloc_bitmap[curr_idx] |= (1UL <<end_off)-1 ; } + + nr_free_pages -= nr_pages; }
@@ -93,6 +97,8 @@ static void map_free(unsigned long first_page, unsigned long nr_pages) end_idx = (first_page + nr_pages) /PAGES_PER_MAPWORD ; end_off = (first_page + nr_pages) & (PAGES_PER_MAPWORD -1 ); + nr_free_pages += nr_pages; +if ( curr_idx == end_idx ) { alloc_bitmap[curr_idx] &= -(1UL <<end_off) | ((1UL <<start_off)-1 );
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 6/9 mini-os: let memory allocation fail if no free page available
In reply to Juergen GrossInstead of panicing when no page can be allocated try to fail the memory allocation by returning NULL instead.
--- mm.c | 5 +++++ 1 file changed, 5 insertions(+)
mm.c +5 -0
@@ -335,6 +335,11 @@ void *sbrk(ptrdiff_t increment)if (new_brk > heap_mapped) {unsigned long n = (new_brk - heap_mapped +PAGE_SIZE -1 ) /PAGE_SIZE ; +if (n > nr_free_pages) { +printk ("Memory exhausted: want %ld pages, but only %ld are left\n" , + n, nr_free_pages); +return NULL ; + }do_map_zero (heap_mapped, n); heap_mapped += n *PAGE_SIZE ; }
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 7/9 mini-os: add ballooning config item
In reply to Juergen GrossAdd CONFIG_BALLOON defaulting to 'n' as a config item to Mini-OS.
--- Makefile | 2 ++ 1 file changed, 2 insertions(+)
Makefile +2 -0
@@ -33,6 +33,7 @@ CONFIG_CONSFRONT ?= y CONFIG_XENBUS ?= y CONFIG_XC ?=y CONFIG_LWIP ?= $(lwip) +CONFIG_BALLOON ?= n # Export config items as compiler directives flags-$(CONFIG_START_NETWORK) += -DCONFIG_START_NETWORK
@@ -48,6 +49,7 @@ flags-$(CONFIG_KBDFRONT) += -DCONFIG_KBDFRONT flags-$(CONFIG_FBFRONT) += -DCONFIG_FBFRONT flags-$(CONFIG_CONSFRONT) += -DCONFIG_CONSFRONT flags-$(CONFIG_XENBUS) += -DCONFIG_XENBUS +flags-$(CONFIG_BALLOON) += -DCONFIG_BALLOON DEF_CFLAGS += $(flags-y)
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 8/9 mini-os: get maximum memory size from hypervisor
In reply to Juergen GrossAdd support for obtaining the maximum memory size from the hypervisor. This will make it possible to support ballooning.
--- include/mm.h | 1 + mm.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+)
include/mm.h +1 -0
@@ -43,6 +43,7 @@#define STACK_SIZE __STACK_SIZEextern unsigned long nr_free_pages; +extern unsigned long nr_max_pages;void init_mm (void );unsigned long alloc_pages (int order);
mm.c +22 -0
@@ -54,6 +54,7 @@ static unsigned long *alloc_bitmap; (alloc_bitmap[(_pn)/PAGES_PER_MAPWORD] & (1UL<<((_pn)&(PAGES_PER_MAPWORD-1))))unsigned long nr_free_pages; +unsigned long nr_max_pages;/* * Hint regarding bitwise arithmetic in map_{alloc,free}:
@@ -350,7 +351,27 @@ void *sbrk(ptrdiff_t increment) }#endif +#ifdef CONFIG_BALLOON +static void get_max_pages (void ) +{ +long ret; +domid_t domid =DOMID_SELF ; + ret =HYPERVISOR_memory_op (XENMEM_maximum_reservation, &domid); +if (ret <0 ) { +printk ("Could not get maximum pfn\n" ); +return ; + } + + nr_max_pages = ret; +printk ("Maximum memory size: %ld pages\n" , nr_max_pages); +} +#else +static unsigned long get_max_pages (void ) +{ +return 0 ; +} +#endif void init_mm (void ) {
@@ -359,6 +380,7 @@ void init_mm(void)printk ("MM: Init\n" ); +get_max_pages ();arch_init_mm (&start_pfn, &max_pfn);/* * now we can initialise the page allocator
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH 9/9 mini-os: modify virtual memory layout for support of ballooning
In reply to Juergen GrossIn order to be able to support ballooning the virtual memory layout of Mini-OS has to be modified: instead of a (nearly) consecutive area used for physical memory mapping, on demand mappings, and heap we need enough spare place for adding new memory.
So instead of dynamically place the different regions based on found memory size locate them statically at fixed virtual addresses:
area x86-64 x86-32
------------------------------------------------------------
mapped physical memory 00000000 00000000 kernel virtual mappings 8000000000 3f000000 demand mappings 100000000000 40000000 heap 200000000000 c0000000
This will enable Mini-OS to support up to 512GB of domain memory with a 64 bit kernel and nearly 1GB with a 32 bit kernel.
The kernel virtual mappings are a new area needed for being able to grow the p2m list without having to relocate it in physical memory.
Modify the placement of the demand mappings and heap and adjust the memory layout description.
--- arch/arm/mm.c | 2 +- arch/x86/mm.c | 44 +++++++++++--------------------------------- include/mm.h | 2 +- include/x86/arch_mm.h | 34 +++++++++++++++++++++++++++++----- mm.c | 2 +- 5 files changed, 43 insertions(+), 41 deletions(-)
arch/arm/mm.c +1 -1
@@ -75,7 +75,7 @@ void arch_init_p2m(unsigned long max_pfn) { } -void arch_init_demand_mapping_area (unsigned long cur_pfn) +void arch_init_demand_mapping_area (void ) { }
arch/x86/mm.c +11 -33
62 lines not on this page · raw message
include/mm.h +1 -1
@@ -60,7 +60,7 @@ static __inline__ int get_order(unsigned long size)return order; } -void arch_init_demand_mapping_area (unsigned long max_pfn); +void arch_init_demand_mapping_area (void );void arch_init_mm (unsigned long * start_pfn_p,unsigned long * max_pfn_p);void arch_init_p2m (unsigned long max_pfn_p);
include/x86/arch_mm.h +29 -5
@@ -49,11 +49,13 @@* * Virtual address space usage: * -* 1:1 mapping of physical memory starting at VA(0) -* 1 unallocated page -* demand map area (32 bits: 2 GB, 64 bits: 128 GB) for virtual allocations -* 1 unallocated page -* with libc: heap area (32 bits: 1 GB, 64 bits: 128 GB) +* area x86-64 x86-32 +* ------------------------------------------------------------ +* mapped physical memory 00000000 00000000 +* kernel virtual mappings 8000000000 3f000000 +* demand mappings 100000000000 40000000 +* heap (with libc only) 200000000000 c0000000 +* */ #define L1_FRAME 1
@@ -81,6 +83,14 @@typedef uint64_t pgentry_t ;#endif +#define MAX_MEM_SIZE 0x3f000000UL +#define VIRT_KERNEL_AREA 0x3f000000UL +#define VIRT_DEMAND_AREA 0x40000000UL +#define VIRT_HEAP_AREA 0xc0000000UL + +#define DEMAND_MAP_PAGES 0x7ffffUL +#define HEAP_PAGES_MAX 0x3ffffUL +#elif defined(__x86_64__)#define L2_PAGETABLE_SHIFT 21
@@ -106,6 +116,20 @@ typedef uint64_t pgentry_t;typedef unsigned long pgentry_t ;#endif +#define MAX_MEM_SIZE (512ULL << 30) +#define VIRT_KERNEL_AREA 0x0000008000000000UL +#define VIRT_DEMAND_AREA 0x0000100000000000UL +#define VIRT_HEAP_AREA 0x0000200000000000UL + +#define DEMAND_MAP_PAGES 0x8000000UL +#define HEAP_PAGES_MAX 0x8000000UL + +#endif + +#ifndef HAVE_LIBC +#define HEAP_PAGES 0 +#else +#define HEAP_PAGES HEAP_PAGES_MAX#endif #define L1_MASK ((1UL << L2_PAGETABLE_SHIFT) - 1)
mm.c +1 -1
@@ -393,7 +393,7 @@ void init_mm(void)arch_init_p2m (max_pfn); -arch_init_demand_mapping_area (max_pfn); +arch_init_demand_mapping_area (); }void fini_mm (void )
Signature · 7 lines
2.6.6 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Series:
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel