Juergen Gross, on Thu 11 Aug 2016 13:06:36 +0200, wrote:
> In case of CONFIG_BALLOON the page allocator's bitmap needs some space
> to be able to grow. Remap it to kernel virtual area if the preallocated
> area isn't large enough.
>
> Signed-off-by: Juergen Gross
Reviewed-by: Samuel Thibault
> ---
> V4: - mm_bitmap* -> mm_alloc_bitmap* as requested by Samuel Thibault
>
> V3: - add assertion as requested by Samuel Thibault
> - rename functions to have mm_ prefix as requested by Samuel Thibault
> ---
> balloon.c | 18 ++
> include/balloon.h | 2 ++
> include/mm.h | 6 ++
> mm.c | 44 +++-
> 4 files changed, 49 insertions(+), 21 deletions(-)
>
> diff --git a/balloon.c b/balloon.c
> index 1ec113d..0a3342c 100644
> --- a/balloon.c
> +++ b/balloon.c
> @@ -44,3 +44,21 @@ void get_max_pages(void)
> nr_max_pages = ret;
> printk("Maximum memory size: %ld pages\n", nr_max_pages);
> }
> +
> +void mm_alloc_bitmap_remap(void)
> +{
> +unsigned long i;
> +
> +if ( mm_alloc_bitmap_size >= ((nr_max_pages + 1) >> (PAGE_SHIFT + 3)) )
> +return;
> +
> +for ( i = 0; i < mm_alloc_bitmap_size; i += PAGE_SIZE )
> +{
> +map_frame_rw(virt_kernel_area_end + i,
> + virt_to_mfn((unsigned long)(mm_alloc_bitmap) + i));
> +}
> +
> +mm_alloc_bitmap = (unsigned long *)virt_kernel_area_end;
> +virt_kernel_area_end += round_pgup((nr_max_pages + 1) >> (PAGE_SHIFT +
> 3));
> +ASSERT(virt_kernel_area_end <= VIRT_DEMAND_AREA);
> +}
> diff --git a/include/balloon.h b/include/balloon.h
> index b0d0ebf..9154f44 100644
> --- a/include/balloon.h
> +++ b/include/balloon.h
> @@ -31,11 +31,13 @@ extern unsigned long virt_kernel_area_end;
>
> void get_max_pages(void);
> void arch_remap_p2m(unsigned long max_pfn);
> +void mm_alloc_bitmap_remap(void);
>
> #else /* CONFIG_BALLOON */
>
> static inline void get_max_pages(void) { }
> static inline void arch_remap_p2m(unsigned long max_pfn) { }
> +static inline void mm_alloc_bitmap_remap(void) { }
>
> #endif /* CONFIG_BALLOON */
> #endif /* _BALLOON_H_ */
> diff --git a/include/mm.h b/include/mm.h
> index 6add683..fc3128b 100644
> --- a/include/mm.h
> +++ b/include/mm.h
> @@ -42,8 +42,14 @@
> #define STACK_SIZE_PAGE_ORDER __STACK_SIZE_PAGE_ORDER
> #define STACK_SIZE __STACK_SIZE
>
> +#define round_pgdown(_p) ((_p) & PAGE_MASK)
> +#define round_pgup(_p)(((_p) + (PAGE_SIZE - 1)) & PAGE_MASK)
> +
> extern unsigned long nr_free_pages;
>
> +extern unsigned long *mm_alloc_bitmap;
> +extern unsigned long mm_alloc_bitmap_size;
> +
> void init_mm(void);
> unsigned long alloc_pages(int order);
> #define alloc_page()alloc_pages(0)
> diff --git a/mm.c b/mm.c
> index 707a3e0..9e3a479 100644
> --- a/mm.c
> +++ b/mm.c
> @@ -48,11 +48,14 @@
> * One bit per page of memory. Bit set => page is allocated.
> */
>
> -static unsigned long *alloc_bitmap;
> +unsigned long *mm_alloc_bitmap;
> +unsigned long mm_alloc_bitmap_size;
> +
> #define PAGES_PER_MAPWORD (sizeof(unsigned long) * 8)
>
> #define allocated_in_map(_pn) \
> -(alloc_bitmap[(_pn)/PAGES_PER_MAPWORD] &
> (1UL<<((_pn)&(PAGES_PER_MAPWORD-1
> +(mm_alloc_bitmap[(_pn) / PAGES_PER_MAPWORD] & \
> + (1UL << ((_pn) & (PAGES_PER_MAPWORD - 1
>
> unsigned long nr_free_pages;
>
> @@ -61,8 +64,8 @@ unsigned long nr_free_pages;
> * -(1<= n.
> * (1< * Variable names in map_{alloc,free}:
> - * *_idx == Index into `alloc_bitmap' array.
> - * *_off == Bit offset within an element of the `alloc_bitmap' array.
> + * *_idx == Index into `mm_alloc_bitmap' array.
> + * *_off == Bit offset within an element of the `mm_alloc_bitmap' array.
> */
>
> static void map_alloc(unsigned long first_page, unsigned long nr_pages)
> @@ -76,13 +79,13 @@ static void map_alloc(unsigned long first_page, unsigned
> long nr_pages)
>
> if ( curr_idx == end_idx )
> {
> -alloc_bitmap[curr_idx] |= ((1UL< +mm_alloc_bitmap[curr_idx] |= ((1UL< }
> else
> {
> -alloc_bitmap[curr_idx] |= -(1UL< -while ( ++curr_idx < end_idx ) alloc_bitmap[curr_idx] = ~0UL;
> -alloc_bitmap[curr_idx] |= (1UL< +mm_alloc_bitmap[curr_idx] |= -(1UL< +while ( ++curr_idx < end_idx ) mm_alloc_bitmap[curr_idx] = ~0UL;
> +mm_alloc_bitmap[curr_idx] |= (1UL< }
>
> nr_free_pages -= nr_pages;
> @@ -102,13 +105,13 @@ static void map_free(unsigned long first_page, unsigned
> long nr_pages)
>
> if ( curr_idx == end_idx )
> {
> -alloc_bitmap[curr_idx] &= -(1UL< +mm_alloc_bitmap[curr_idx] &= -(1UL<