Fix and improve the page allocator
Subsystems (2)
v2 cover Fix and improve the page allocator
My previous patchseries was rushed and not polished enough. Furthermore it introduced some regressions.
This patchseries fixes hopefully all the issues reported, and introduces some new features.
It also simplifies the code and hopefully makes it more readable.
Fixed:
* allocated memory is now zeroed by default
New features:
* per-allocation flags to specify not just the area (like before) but also other parameters
- dontzero flag: the allocation will not be zeroed
- fresh flag: the returned memory has never been read or written to before
I would appreciate if people could test these patches, especially on strange, unusual or exotic hardware (I tested only on s390x)
GitLab:
https://gitlab.com/imbrenda/kvm-unit-tests/-/tree/page_allocator_fixes
CI:
https://gitlab.com/imbrenda/kvm-unit-tests/-/pipelines/241819726
v1->v2
* have DONTZERO flag instead of a ZERO flag, this way there is no need for a default.
* drop the last patch, since there is no need for a default now.
* fixed a pre-existing bug that caused wrong allocations
* renamed alloc_pages_special to reserve_pages, to make it clear it is not a normal allocation. The function now returns 0 on success and -1 on failure.
* added a NULL check in vm_free; freeing a NULL pointer is now a no-op.
* fix and improve some comments
* remove a spurious return
Claudio Imbrenda (11):
lib/x86: fix page.h to include the generic header lib/list.h: add list_add_tail
lib/vmalloc: add some asserts and improvements lib/asm: Fix definitions of memory areas lib/alloc_page: fix and improve the page allocator lib/alloc.h: remove align_min from struct alloc_ops lib/alloc_page: Optimization to skip known empty freelists lib/alloc_page: rework metadata format lib/alloc: replace areas with more generic flags lib/alloc_page: Wire up FLAG_DONTZERO lib/alloc_page: Properly handle requests for fresh blocks
lib/asm-generic/memory_areas.h | 9 +- lib/arm/asm/memory_areas.h | 11 +- lib/arm64/asm/memory_areas.h | 11 +- lib/powerpc/asm/memory_areas.h | 11 +- lib/ppc64/asm/memory_areas.h | 11 +- lib/s390x/asm/memory_areas.h | 13 +- lib/x86/asm/memory_areas.h | 27 ++-- lib/x86/asm/page.h | 4 +- lib/alloc.h | 1 - lib/alloc_page.h | 76 ++++++--- lib/list.h | 9 ++ lib/alloc_page.c | 283 +++++++++++++++++++-------------- lib/alloc_phys.c | 9 +- lib/s390x/smp.c | 2 +- lib/vmalloc.c | 23 +-- 15 files changed, 284 insertions(+), 216 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/11 lib/x86: fix page.h to include the generic header
Bring x86 in line with the other architectures and include the generic header at asm-generic/page.h .
This provides the macros PAGE_SHIFT, PAGE_SIZE, PAGE_MASK, virt_to_pfn, and pfn_to_virt.
--- lib/x86/asm/page.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
lib/x86/asm/page.h +1 -3
@@ -13,9 +13,7 @@typedef unsigned long pteval_t ;typedef unsigned long pgd_t ; -#define PAGE_SHIFT 12 -#define PAGE_SIZE (_AC(1,UL) << PAGE_SHIFT) -#define PAGE_MASK (~(PAGE_SIZE-1)) +#include <asm-generic/page.h> #ifndef __ASSEMBLY__
Signature · 1 line
2.26.2
Re: [kvm-unit-tests PATCH v2 01/11] lib/x86: fix page.h to include the generic header
Quoted 7 lines · Claudio Imbrenda
Bring x86 in line with the other architectures and include the generic header at
asm-generic/page.h.
This provides the macrosPAGE_SHIFT,PAGE_SIZE,PAGE_MASK,virt_to_pfn, andpfn_to_virt.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Krish Sadhukhan <[email protected]>
Quoted 20 lines
--- lib/x86/asm/page.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)lib/x86/asm/page.h +1
-3@@ -13,9 +13,7 @@typedef unsigned long pteval_t ;typedef unsigned long pgd_t ; -#define PAGE_SHIFT 12 -#define PAGE_SIZE (_AC(1,UL) << PAGE_SHIFT) -#define PAGE_MASK (~(PAGE_SIZE-1)) +#include <asm-generic/page.h> #ifndef __ASSEMBLY__
PATCH v2 2/11 lib/list.h: add list_add_tail
In reply to Claudio ImbrendaAdd a list_add_tail wrapper function to allow adding elements to the end of a list.
--- lib/list.h | 9 +++++++++ 1 file changed, 9 insertions(+)
lib/list.h +9 -0
@@ -50,4 +50,13 @@ static inline void list_add(struct linked_list *head, struct linked_list *li) head->next = li; } +/* +* Add the given element before the given list head. +*/ +static inline void list_add_tail (struct linked_list *head,struct linked_list *li) +{ +assert (head); +list_add (head->prev, li); +} +#endif
Signature · 1 line
2.26.2
Re: [kvm-unit-tests PATCH v2 02/11] lib/list.h: add list_add_tail
Quoted 5 lines · Claudio Imbrenda
Add a
list_add_tailwrapper function to allow adding elements to the end of a list.Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Krish Sadhukhan <[email protected]>
Quoted 23 lines
--- lib/list.h | 9 +++++++++ 1 file changed, 9 insertions(+)lib/list.h +9
-0@@ -50,4 +50,13 @@ static inline void list_add(struct linked_list *head, struct linked_list *li) head->next = li; } +/* +* Add the given element before the given list head. +*/ +static inline void list_add_tail (struct linked_list *head,struct linked_list *li) +{ +assert (head); +list_add (head->prev, li); +} +#endif
PATCH v2 3/11 lib/vmalloc: add some asserts and improvements
In reply to Claudio ImbrendaAdd some asserts to make sure the state is consistent.
Simplify and improve the readability of vm_free.
If a NULL pointer is freed, no operation is performed.
--- lib/vmalloc.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
lib/vmalloc.c +13 -9
@@ -162,13 +162,16 @@ static void *vm_memalign(size_t alignment, size_t size)static void vm_free (void *mem) {struct metadata *m; -uintptr_t ptr, end; +uintptr_t ptr, page, i; +if (!mem) +return ;/* the pointer is not page-aligned, it was a single-page allocation */ if (!IS_ALIGNED ((uintptr_t )mem,PAGE_SIZE )) {assert (GET_MAGIC (mem) ==VM_MAGIC ); - ptr =virt_to_pte_phys (page_root, mem) &PAGE_MASK ; -free_page (phys_to_virt (ptr)); + page =virt_to_pte_phys (page_root, mem) &PAGE_MASK ; +assert (page); +free_page (phys_to_virt (page));return ; }
@@ -176,13 +179,14 @@ static void vm_free(void *mem) m =GET_METADATA (mem);assert (m->magic ==VM_MAGIC );assert (m->npages >0 ); +assert (m->npages <BIT_ULL (BITS_PER_LONG -PAGE_SHIFT ));/* free all the pages including the metadata page */ - ptr = (uintptr_t )mem -PAGE_SIZE ; - end = ptr + m->npages *PAGE_SIZE ; -for ( ; ptr < end; ptr +=PAGE_SIZE ) -free_page (phys_to_virt (virt_to_pte_phys (page_root, (void *)ptr))); -/* free the last one separately to avoid overflow issues */ -free_page (phys_to_virt (virt_to_pte_phys (page_root, (void *)ptr))); + ptr = (uintptr_t )m &PAGE_MASK ; +for (i =0 ; i < m->npages +1 ; i++, ptr +=PAGE_SIZE ) { + page =virt_to_pte_phys (page_root, (void *)ptr) &PAGE_MASK ; +assert (page); +free_page (phys_to_virt (page)); + } }static struct alloc_ops vmalloc_ops = {
Signature · 1 line
2.26.2
Re: [kvm-unit-tests PATCH v2 03/11] lib/vmalloc: add some asserts and improvements
Quoted 9 lines · Claudio Imbrenda
Add some asserts to make sure the state is consistent.
Simplify and improve the readability of
vm_free.If a NULL pointer is freed, no operation is performed.
Fixes 3f6fee0d4da4 ("lib/vmalloc: vmalloc support for handling allocation metadata")Signed-off-by Claudio Imbrenda <[email protected]>
Quoted 32 lines
--- lib/vmalloc.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)lib/vmalloc.c +6
-3@@ -162,13 +162,16 @@ static void *vm_memalign(size_t alignment, size_t size)static void vm_free (void *mem) {struct metadata *m; -uintptr_t ptr, end; +uintptr_t ptr, page, i; +if (!mem) +return ;/* the pointer is not page-aligned, it was a single-page allocation */ if (!IS_ALIGNED ((uintptr_t )mem,PAGE_SIZE )) {assert (GET_MAGIC (mem) ==VM_MAGIC ); - ptr =virt_to_pte_phys (page_root, mem) &PAGE_MASK ; -free_page (phys_to_virt (ptr)); + page =virt_to_pte_phys (page_root, mem) &PAGE_MASK ; +assert (page); +free_page (phys_to_virt (page));return ; }@@ -176,13 +179,14 @@ static void vm_free(void *mem) m =GET_METADATA (mem);assert (m->magic ==VM_MAGIC );assert (m->npages >0 );
lib/vmalloc.c static void vm_free(void *mem) · line 185
Quoted 10 lines
lib/vmalloc.c +3
-6+assert (m->npages <BIT_ULL (BITS_PER_LONG -PAGE_SHIFT ));/* free all the pages including the metadata page */ - ptr = (uintptr_t )mem -PAGE_SIZE ; - end = ptr + m->npages *PAGE_SIZE ; -for ( ; ptr < end; ptr +=PAGE_SIZE ) -free_page (phys_to_virt (virt_to_pte_phys (page_root, (void *)ptr))); -/* free the last one separately to avoid overflow issues */ -free_page (phys_to_virt (virt_to_pte_phys (page_root, (void *)ptr))); + ptr = (uintptr_t )m &PAGE_MASK ; +for (i =0 ; i < m->npages +1 ; i++, ptr +=PAGE_SIZE ) {
s/i < m->npages + 1/i <= m->npages/ ?
Quoted 8 lines
lib/vmalloc.c +4
-0+ page =virt_to_pte_phys (page_root, (void *)ptr) &PAGE_MASK ; +assert (page); +free_page (phys_to_virt (page)); + } }static struct alloc_ops vmalloc_ops = {
PATCH v2 4/11 lib/asm: Fix definitions of memory areas
In reply to Claudio ImbrendaFix the definitions of the memory areas.
Bring the headers in line with the rest of the asm headers, by having the appropriate #ifdef _ASM$ARCH_ guarding the headers.
--- lib/asm-generic/memory_areas.h | 9 ++++----- lib/arm/asm/memory_areas.h | 11 +++-------- lib/arm64/asm/memory_areas.h | 11 +++-------- lib/powerpc/asm/memory_areas.h | 11 +++-------- lib/ppc64/asm/memory_areas.h | 11 +++-------- lib/s390x/asm/memory_areas.h | 13 ++++++------- lib/x86/asm/memory_areas.h | 27 ++++++++++++++++----------- lib/alloc_page.h | 3 +++ lib/alloc_page.c | 4 +--- 9 files changed, 42 insertions(+), 58 deletions(-)
lib/asm-generic/memory_areas.h +4 -5
@@ -1,11 +1,10 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef __ASM_GENERIC_MEMORY_AREAS_H__ +#define __ASM_GENERIC_MEMORY_AREAS_H__#define AREA_NORMAL_PFN 0#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 1#endif
lib/arm/asm/memory_areas.h +3 -8
@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM_MEMORY_AREAS_H_ +#define _ASMARM_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif
lib/arm64/asm/memory_areas.h +3 -8
@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM64_MEMORY_AREAS_H_ +#define _ASMARM64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif
lib/powerpc/asm/memory_areas.h +3 -8
@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPOWERPC_MEMORY_AREAS_H_ +#define _ASMPOWERPC_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif
lib/ppc64/asm/memory_areas.h +3 -8
@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPPC64_MEMORY_AREAS_H_ +#define _ASMPPC64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif
lib/s390x/asm/memory_areas.h +6 -7
@@ -1,16 +1,15 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMS390X_MEMORY_AREAS_H_ +#define _ASMS390X_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN BIT(31-12) +#define AREA_NORMAL_PFN (1 << 19)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER)#define AREA_LOW_PFN 0#define AREA_LOW_NUMBER 1 -#define AREA_LOW 2 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 2#define AREA_DMA31 AREA_LOW
lib/x86/asm/memory_areas.h +16 -11
@@ -1,21 +1,26 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASM_X86_MEMORY_AREAS_H_ +#define _ASM_X86_MEMORY_AREAS_H_#define AREA_NORMAL_PFN BIT(36-12)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_PAE_HIGH_PFN BIT(32-12) -#define AREA_PAE_HIGH_NUMBER 1 -#define AREA_PAE_HIGH 2 +#define AREA_HIGH_PFN BIT(32-12) +#define AREA_HIGH_NUMBER 1 +#define AREA_HIGH (1 << AREA_HIGH_NUMBER) -#define AREA_LOW_PFN 0 +#define AREA_LOW_PFN BIT(24-12)#define AREA_LOW_NUMBER 2 -#define AREA_LOW 4 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_PAE (AREA_PAE | AREA_LOW) +#define AREA_LOWEST_PFN 0 +#define AREA_LOWEST_NUMBER 3 +#define AREA_LOWEST (1 << AREA_LOWEST_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 4 + +#define AREA_DMA24 AREA_LOWEST +#define AREA_DMA32 (AREA_LOWEST | AREA_LOW) +#define AREA_PAE36 (AREA_LOWEST | AREA_LOW | AREA_HIGH)#endif
lib/alloc_page.h +3 -0
@@ -10,6 +10,9 @@#include <asm/memory_areas.h> +#define AREA_ANY -1 +#define AREA_ANY_NUMBER 0xff +/* Returns true if the page allocator has been initialized */ bool page_alloc_initialized (void );
lib/alloc_page.c +1 -3
@@ -19,8 +19,6 @@#define NLISTS ((BITS_PER_LONG) - (PAGE_SHIFT))#define PFN (x) ((uintptr_t)(x) >> PAGE_SHIFT) -#define MAX_AREAS 6 -#define ORDER_MASK 0x3f#define ALLOC_MASK 0x40#define SPECIAL_MASK 0x80
@@ -509,7 +507,7 @@ void page_alloc_init_area(u8 n, uintptr_t base_pfn, uintptr_t top_pfn)return ; }#ifdef AREA_HIGH_PFN - __page_alloc_init_area(AREA_HIGH_NUMBER, AREA_HIGH_PFN), base_pfn, &top_pfn); +__page_alloc_init_area (AREA_HIGH_NUMBER ,AREA_HIGH_PFN , base_pfn, &top_pfn);#endif __page_alloc_init_area (AREA_NORMAL_NUMBER ,AREA_NORMAL_PFN , base_pfn, &top_pfn);#ifdef AREA_LOW_PFN
Signature · 1 line
2.26.2
Re: [kvm-unit-tests PATCH v2 04/11] lib/asm: Fix definitions of memory areas
Quoted 174 lines · Claudio Imbrenda
Fix the definitions of the memory areas.
Bring the headers in line with the rest of the asm headers, by having the appropriate #ifdef _ASM$ARCH_ guarding the headers.
Fixes d74708246bd9 ("lib/asm: Add definitions of memory areas")Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Krish Sadhukhan <[email protected]>--- lib/asm-generic/memory_areas.h | 9 ++++----- lib/arm/asm/memory_areas.h | 11 +++-------- lib/arm64/asm/memory_areas.h | 11 +++-------- lib/powerpc/asm/memory_areas.h | 11 +++-------- lib/ppc64/asm/memory_areas.h | 11 +++-------- lib/s390x/asm/memory_areas.h | 13 ++++++------- lib/x86/asm/memory_areas.h | 27 ++++++++++++++++----------- lib/alloc_page.h | 3 +++ lib/alloc_page.c | 4 +--- 9 files changed, 42 insertions(+), 58 deletions(-)lib/asm-generic/memory_areas.h +4
-5@@ -1,11 +1,10 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef __ASM_GENERIC_MEMORY_AREAS_H__ +#define __ASM_GENERIC_MEMORY_AREAS_H__#define AREA_NORMAL_PFN 0#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 1#endif lib/arm/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM_MEMORY_AREAS_H_ +#define _ASMARM_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/arm64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM64_MEMORY_AREAS_H_ +#define _ASMARM64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/powerpc/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPOWERPC_MEMORY_AREAS_H_ +#define _ASMPOWERPC_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/ppc64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPPC64_MEMORY_AREAS_H_ +#define _ASMPPC64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/s390x/asm/memory_areas.h +6
-7@@ -1,16 +1,15 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMS390X_MEMORY_AREAS_H_ +#define _ASMS390X_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN BIT(31-12) +#define AREA_NORMAL_PFN (1 << 19)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER)#define AREA_LOW_PFN 0#define AREA_LOW_NUMBER 1 -#define AREA_LOW 2 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 2#define AREA_DMA31 AREA_LOWlib/x86/asm/memory_areas.h +9
-9@@ -1,21 +1,26 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASM_X86_MEMORY_AREAS_H_ +#define _ASM_X86_MEMORY_AREAS_H_#define AREA_NORMAL_PFN BIT(36-12)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_PAE_HIGH_PFN BIT(32-12) -#define AREA_PAE_HIGH_NUMBER 1 -#define AREA_PAE_HIGH 2 +#define AREA_HIGH_PFN BIT(32-12) +#define AREA_HIGH_NUMBER 1 +#define AREA_HIGH (1 << AREA_HIGH_NUMBER) -#define AREA_LOW_PFN 0 +#define AREA_LOW_PFN BIT(24-12)#define AREA_LOW_NUMBER 2 -#define AREA_LOW 4 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_PAE (AREA_PAE | AREA_LOW) +#define AREA_LOWEST_PFN 0
lib/x86/asm/memory_areas.h line 24
Quoted 10 lines
lib/x86/asm/memory_areas.h +7
-2+#define AREA_LOWEST_NUMBER 3 +#define AREA_LOWEST (1 << AREA_LOWEST_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 4 + +#define AREA_DMA24 AREA_LOWEST +#define AREA_DMA32 (AREA_LOWEST | AREA_LOW) +#define AREA_PAE36 (AREA_LOWEST | AREA_LOW | AREA_HIGH)
This confuses the heck out of me.
The other things look ok to me though.
Quoted 39 lines
lib/x86/asm/memory_areas.h +0
-0#endif lib/alloc_page.h +3
-0@@ -10,6 +10,9 @@#include <asm/memory_areas.h> +#define AREA_ANY -1 +#define AREA_ANY_NUMBER 0xff +/* Returns true if the page allocator has been initialized */ bool page_alloc_initialized (void );lib/alloc_page.c +1
-3@@ -19,8 +19,6 @@#define NLISTS ((BITS_PER_LONG) - (PAGE_SHIFT))#define PFN (x) ((uintptr_t)(x) >> PAGE_SHIFT) -#define MAX_AREAS 6 -#define ORDER_MASK 0x3f#define ALLOC_MASK 0x40#define SPECIAL_MASK 0x80@@ -509,7 +507,7 @@ void page_alloc_init_area(u8 n, uintptr_t base_pfn, uintptr_t top_pfn)return ; }#ifdef AREA_HIGH_PFN - __page_alloc_init_area(AREA_HIGH_NUMBER, AREA_HIGH_PFN), base_pfn, &top_pfn); +__page_alloc_init_area (AREA_HIGH_NUMBER ,AREA_HIGH_PFN , base_pfn, &top_pfn);#endif __page_alloc_init_area (AREA_NORMAL_NUMBER ,AREA_NORMAL_PFN , base_pfn, &top_pfn);#ifdef AREA_LOW_PFN
Re: [kvm-unit-tests PATCH v2 04/11] lib/asm: Fix definitions of memory areas
Quoted 178 lines · On Tue, 19 Jan 2021 16:33:29 +0100 Janosch Frank <[email protected]> wrote:
Quoted 177 lines · Claudio Imbrenda
Fix the definitions of the memory areas.
Bring the headers in line with the rest of the asm headers, by having the appropriate #ifdef _ASM$ARCH_ guarding the headers.
Fixes d74708246bd9 ("lib/asm: Add definitions of memory areas")Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Krish Sadhukhan <[email protected]>--- lib/asm-generic/memory_areas.h | 9 ++++----- lib/arm/asm/memory_areas.h | 11 +++-------- lib/arm64/asm/memory_areas.h | 11 +++-------- lib/powerpc/asm/memory_areas.h | 11 +++-------- lib/ppc64/asm/memory_areas.h | 11 +++-------- lib/s390x/asm/memory_areas.h | 13 ++++++------- lib/x86/asm/memory_areas.h | 27 ++++++++++++++++----------- lib/alloc_page.h | 3 +++ lib/alloc_page.c | 4 +--- 9 files changed, 42 insertions(+), 58 deletions(-)(quoted diff) +0
-0
b/lib/asm-generic/memory_areas.hindex 927baa7..3074afe 100644lib/asm-generic/memory_areas.h +4
-5@@ -1,11 +1,10 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef __ASM_GENERIC_MEMORY_AREAS_H__ +#define __ASM_GENERIC_MEMORY_AREAS_H__#define AREA_NORMAL_PFN 0#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 1#endif lib/arm/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM_MEMORY_AREAS_H_ +#define _ASMARM_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/arm64/asm/memory_areas.hindex 927baa7..18e8ca8 100644lib/arm64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM64_MEMORY_AREAS_H_ +#define _ASMARM64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/powerpc/asm/memory_areas.hindex 927baa7..76d1738 100644lib/powerpc/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPOWERPC_MEMORY_AREAS_H_ +#define _ASMPOWERPC_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/ppc64/asm/memory_areas.hindex 927baa7..b9fd46b 100644lib/ppc64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPPC64_MEMORY_AREAS_H_ +#define _ASMPPC64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/s390x/asm/memory_areas.hindex 4856a27..827bfb3 100644lib/s390x/asm/memory_areas.h +6
-7@@ -1,16 +1,15 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMS390X_MEMORY_AREAS_H_ +#define _ASMS390X_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN BIT(31-12) +#define AREA_NORMAL_PFN (1 << 19)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER)#define AREA_LOW_PFN 0#define AREA_LOW_NUMBER 1 -#define AREA_LOW 2 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 2#define AREA_DMA31 AREA_LOWlib/x86/asm/memory_areas.h +11
-9@@ -1,21 +1,26 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASM_X86_MEMORY_AREAS_H_ +#define _ASM_X86_MEMORY_AREAS_H_#define AREA_NORMAL_PFN BIT(36-12)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_PAE_HIGH_PFN BIT(32-12) -#define AREA_PAE_HIGH_NUMBER 1 -#define AREA_PAE_HIGH 2 +#define AREA_HIGH_PFN BIT(32-12) +#define AREA_HIGH_NUMBER 1 +#define AREA_HIGH (1 << AREA_HIGH_NUMBER) -#define AREA_LOW_PFN 0 +#define AREA_LOW_PFN BIT(24-12)#define AREA_LOW_NUMBER 2 -#define AREA_LOW 4 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_PAE (AREA_PAE | AREA_LOW) +#define AREA_LOWEST_PFN 0 +#define AREA_LOWEST_NUMBER 3 +#define AREA_LOWEST (1 << AREA_LOWEST_NUMBER)
Quoted 10 lines
lib/x86/asm/memory_areas.h line 24
Quoted 7 lines
lib/x86/asm/memory_areas.h +5
-2-#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 4 + +#define AREA_DMA24 AREA_LOWEST +#define AREA_DMA32 (AREA_LOWEST | AREA_LOW) +#define AREA_PAE36 (AREA_LOWEST | AREA_LOW | AREA_HIGH)This confuses the heck out of me.
The other things look ok to me though.
if you want a page below 4G, then you don't want to restrict yourself to the area between 16M and 32G; the area below 16M is good too.
same for 64G.
Quoted 41 lines
Quoted 40 lines
lib/x86/asm/memory_areas.h +0
-0#endif lib/alloc_page.h +3
-0@@ -10,6 +10,9 @@#include <asm/memory_areas.h> +#define AREA_ANY -1 +#define AREA_ANY_NUMBER 0xff +/* Returns true if the page allocator has been initialized */ bool page_alloc_initialized (void );lib/alloc_page.c +1
-3@@ -19,8 +19,6 @@#define NLISTS ((BITS_PER_LONG) - (PAGE_SHIFT))#define PFN (x) ((uintptr_t)(x) >> PAGE_SHIFT) -#define MAX_AREAS 6 -#define ORDER_MASK 0x3f#define ALLOC_MASK 0x40#define SPECIAL_MASK 0x80@@ -509,7 +507,7 @@ void page_alloc_init_area(u8 n, uintptr_t base_pfn, uintptr_t top_pfn) return; }#ifdef AREA_HIGH_PFN -__page_alloc_init_area (AREA_HIGH_NUMBER ,AREA_HIGH_PFN ), base_pfn, &top_pfn); + __page_alloc_init_area(AREA_HIGH_NUMBER, AREA_HIGH_PFN,
base_pfn,&top_pfn); #endif
__page_alloc_init_area(AREA_NORMAL_NUMBER,AREA_NORMAL_PFN,base_pfn,&top_pfn); #ifdefAREA_LOW_PFN
Re: [kvm-unit-tests PATCH v2 04/11] lib/asm: Fix definitions of memory areas
In reply to Claudio ImbrendaHi Claudio,
Quoted 2 lines · Claudio Imbrenda
Fix the definitions of the memory areas.
The test x86/smat.flat started falling for me at this commit. I'm testing on Linux 5.7.17.
Here are the logs:
timeout -k 1s --foreground 90s /usr/bin/qemu-system-x86_64 --no-reboot -nodefaults -device pc-testdev -device isa-debug-exit,iobase=0xf4,iosize=0x4 -vnc none -serial stdio -device pci-testdev -machine accel=kvm -kernel x86/smap.flat -smp 1 -cpu host # -initrd /tmp/tmp.s4WKgsHkOh
enabling apic
paging enabled
cr0 = 80010011
cr3 = 1007000
cr4 = 20
testing without INVLPG
PASS: write to supervisor page
PASS: read from user page with AC=1
PASS: read from user page with AC=0
FAIL: write to user page with AC=1
FAIL: read from user page with AC=0
FAIL: write to user stack with AC=1
FAIL: write to user stack with AC=0
Unhandled exception 6 #UD at ip 0000000001800003
error_code=0000 rflags=00010082 cs=00000008
rax=000000000000000a rcx=00000000000003fd rdx=00000000000003f8 rbx=0000000000000000
rbp=0000000000517700 rsi=0000000000416422 rdi=0000000000000000
r8=0000000000416422 r9=00000000000003f8 r10=000000000000000d
r11=000ffffffffff000
r12=0000000000000000 r13=0000000001418700 r14=0000000000000000 r15=0000000000000000
cr0=0000000080010011 cr2=00000000015176d8 cr3=0000000001007000 cr4=0000000000200020
cr8=0000000000000000
STACK: @1800003 400368
b'0x0000000001800003: ?? ??:0'
Quoted 224 lines
Bring the headers in line with the rest of the asm headers, by having the appropriate #ifdef _ASM$ARCH_ guarding the headers.
Fixes d74708246bd9 ("lib/asm: Add definitions of memory areas")Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Krish Sadhukhan <[email protected]>--- lib/asm-generic/memory_areas.h | 9 ++++----- lib/arm/asm/memory_areas.h | 11 +++-------- lib/arm64/asm/memory_areas.h | 11 +++-------- lib/powerpc/asm/memory_areas.h | 11 +++-------- lib/ppc64/asm/memory_areas.h | 11 +++-------- lib/s390x/asm/memory_areas.h | 13 ++++++------- lib/x86/asm/memory_areas.h | 27 ++++++++++++++++----------- lib/alloc_page.h | 3 +++ lib/alloc_page.c | 4 +--- 9 files changed, 42 insertions(+), 58 deletions(-)lib/asm-generic/memory_areas.h +4
-5@@ -1,11 +1,10 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef __ASM_GENERIC_MEMORY_AREAS_H__ +#define __ASM_GENERIC_MEMORY_AREAS_H__#define AREA_NORMAL_PFN 0#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 1#endif lib/arm/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM_MEMORY_AREAS_H_ +#define _ASMARM_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/arm64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM64_MEMORY_AREAS_H_ +#define _ASMARM64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/powerpc/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPOWERPC_MEMORY_AREAS_H_ +#define _ASMPOWERPC_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/ppc64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPPC64_MEMORY_AREAS_H_ +#define _ASMPPC64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif lib/s390x/asm/memory_areas.h +6
-7@@ -1,16 +1,15 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMS390X_MEMORY_AREAS_H_ +#define _ASMS390X_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN BIT(31-12) +#define AREA_NORMAL_PFN (1 << 19)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER)#define AREA_LOW_PFN 0#define AREA_LOW_NUMBER 1 -#define AREA_LOW 2 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 2#define AREA_DMA31 AREA_LOWlib/x86/asm/memory_areas.h +16
-11@@ -1,21 +1,26 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASM_X86_MEMORY_AREAS_H_ +#define _ASM_X86_MEMORY_AREAS_H_#define AREA_NORMAL_PFN BIT(36-12)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_PAE_HIGH_PFN BIT(32-12) -#define AREA_PAE_HIGH_NUMBER 1 -#define AREA_PAE_HIGH 2 +#define AREA_HIGH_PFN BIT(32-12) +#define AREA_HIGH_NUMBER 1 +#define AREA_HIGH (1 << AREA_HIGH_NUMBER) -#define AREA_LOW_PFN 0 +#define AREA_LOW_PFN BIT(24-12)#define AREA_LOW_NUMBER 2 -#define AREA_LOW 4 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_PAE (AREA_PAE | AREA_LOW) +#define AREA_LOWEST_PFN 0 +#define AREA_LOWEST_NUMBER 3 +#define AREA_LOWEST (1 << AREA_LOWEST_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 4 + +#define AREA_DMA24 AREA_LOWEST +#define AREA_DMA32 (AREA_LOWEST | AREA_LOW) +#define AREA_PAE36 (AREA_LOWEST | AREA_LOW | AREA_HIGH)#endif lib/alloc_page.h +3
-0@@ -10,6 +10,9 @@#include <asm/memory_areas.h> +#define AREA_ANY -1 +#define AREA_ANY_NUMBER 0xff +/* Returns true if the page allocator has been initialized */ bool page_alloc_initialized (void );lib/alloc_page.c +1
-4@@ -19,8 +19,6 @@#define NLISTS ((BITS_PER_LONG) - (PAGE_SHIFT))#define PFN (x) ((uintptr_t)(x) >> PAGE_SHIFT) -#define MAX_AREAS 6 -#define ORDER_MASK 0x3f#define ALLOC_MASK 0x40#define SPECIAL_MASK 0x80@@ -509,7 +507,7 @@ void page_alloc_init_area(u8 n, uintptr_t base_pfn, uintptr_t top_pfn)return ; }#ifdef AREA_HIGH_PFN - __page_alloc_init_area(AREA_HIGH_NUMBER, AREA_HIGH_PFN), base_pfn, &top_pfn); +__page_alloc_init_area (AREA_HIGH_NUMBER ,AREA_HIGH_PFN , base_pfn, &top_pfn);#endif __page_alloc_init_area (AREA_NORMAL_NUMBER ,AREA_NORMAL_PFN , base_pfn, &top_pfn);#ifdef AREA_LOW_PFN --2.26.2
Re: [kvm-unit-tests PATCH v2 04/11] lib/asm: Fix definitions of memory areas
Quoted 8 lines · David Matlack
Hi Claudio,
Quoted 2 lines · Claudio Imbrenda
Fix the definitions of the memory areas.
The test x86/smat.flat started falling for me at this commit. I'm testing on Linux 5.7.17.
FWIW, seems like this was also happening on Travis, just before it became useless. Build #15 is still fine:
https://travis-ci.com/gitlab/kvm-unit-tests/kvm-unit-tests/builds/213551621
But build #18 reported a broken smat test:
https://travis-ci.com/gitlab/kvm-unit-tests/kvm-unit-tests/builds/213564672
The job logs say that the builders there use kernel version 5.4.0-1034-gcp
Thomas
Re: [kvm-unit-tests PATCH v2 04/11] lib/asm: Fix definitions of memory areas
In reply to David MatlackQuoted 11 lines · On Wed, 20 Jan 2021 17:23:33 -0800 David Matlack <[email protected]> wrote:
Hi Claudio,
Quoted 2 lines · Claudio Imbrenda
Fix the definitions of the memory areas.
The test x86/smat.flat started falling for me at this commit. I'm testing on Linux 5.7.17.
Here are the logs:
the test does not fail with the default configuration, maybe the default configuration should be changed?
I would have noticed the issue otherwise.
after a quick look at the testcase itself, it's quite obvious to me that it is broken, since it completely relied on the fact that the page allocator would not touch memory above 16M.
this is not the case any more.
I have fixed the testcase, I'll post a patch
Quoted 260 lines
timeout -k 1s --foreground 90s
/usr/bin/qemu-system-x86_64--no-reboot -nodefaults -device pc-testdev -device isa-debug-exit,iobase=0xf4,iosize=0x4 -vnc none -serial stdio -device pci-testdev -machine accel=kvm -kernel x86/smap.flat -smp 1 -cpu host # -initrd/tmp/tmp.s4WKgsHkOh
enabling apic
paging enabled
cr0 = 80010011
cr3 = 1007000
cr4 = 20
testing without INVLPG
PASS: write to supervisor page
PASS: read from user page with AC=1
PASS: read from user page with AC=0
FAIL: write to user page with AC=1
FAIL: read from user page with AC=0
FAIL: write to user stack with AC=1
FAIL: write to user stack with AC=0
Unhandled exception 6 #UD at ip 0000000001800003
error_code=0000 rflags=00010082 cs=00000008
rax=000000000000000a rcx=00000000000003fd rdx=00000000000003f8 rbx=0000000000000000
rbp=0000000000517700 rsi=0000000000416422 rdi=0000000000000000
r8=0000000000416422 r9=00000000000003f8 r10=000000000000000d
r11=000ffffffffff000
r12=0000000000000000 r13=0000000001418700 r14=0000000000000000 r15=0000000000000000
cr0=0000000080010011 cr2=00000000015176d8 cr3=0000000001007000 cr4=0000000000200020
cr8=0000000000000000
STACK: @1800003 400368
b'0x0000000001800003: ?? ??:0'Quoted 225 lines
Bring the headers in line with the rest of the asm headers, by having the appropriate #ifdef _ASM$ARCH_ guarding the headers.
Fixes d74708246bd9 ("lib/asm: Add definitions of memory areas")Signed-off-by Claudio Imbrenda <[email protected]>Reviewed-by Krish Sadhukhan <[email protected]>--- lib/asm-generic/memory_areas.h | 9 ++++----- lib/arm/asm/memory_areas.h | 11 +++-------- lib/arm64/asm/memory_areas.h | 11 +++-------- lib/powerpc/asm/memory_areas.h | 11 +++-------- lib/ppc64/asm/memory_areas.h | 11 +++-------- lib/s390x/asm/memory_areas.h | 13 ++++++------- lib/x86/asm/memory_areas.h | 27 ++++++++++++++++----------- lib/alloc_page.h | 3 +++ lib/alloc_page.c | 4 +--- 9 files changed, 42 insertions(+), 58 deletions(-)(quoted diff) +0
-0
b/lib/asm-generic/memory_areas.hindex 927baa7..3074afe 100644lib/asm-generic/memory_areas.h +4
-5@@ -1,11 +1,10 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef __ASM_GENERIC_MEMORY_AREAS_H__ +#define __ASM_GENERIC_MEMORY_AREAS_H__#define AREA_NORMAL_PFN 0#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 1#endif lib/arm/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM_MEMORY_AREAS_H_ +#define _ASMARM_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/arm64/asm/memory_areas.hindex 927baa7..18e8ca8 100644lib/arm64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMARM64_MEMORY_AREAS_H_ +#define _ASMARM64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/powerpc/asm/memory_areas.hindex 927baa7..76d1738 100644lib/powerpc/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPOWERPC_MEMORY_AREAS_H_ +#define _ASMPOWERPC_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/ppc64/asm/memory_areas.hindex 927baa7..b9fd46b 100644lib/ppc64/asm/memory_areas.h +3
-8@@ -1,11 +1,6 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMPPC64_MEMORY_AREAS_H_ +#define _ASMPPC64_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN 0 -#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 - -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#include <asm-generic/memory_areas.h> #endif (quoted diff) +0
-0
b/lib/s390x/asm/memory_areas.hindex 4856a27..827bfb3 100644lib/s390x/asm/memory_areas.h +6
-7@@ -1,16 +1,15 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASMS390X_MEMORY_AREAS_H_ +#define _ASMS390X_MEMORY_AREAS_H_ -#define AREA_NORMAL_PFN BIT(31-12) +#define AREA_NORMAL_PFN (1 << 19)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER)#define AREA_LOW_PFN 0#define AREA_LOW_NUMBER 1 -#define AREA_LOW 2 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 2#define AREA_DMA31 AREA_LOWlib/x86/asm/memory_areas.h +16
-11@@ -1,21 +1,26 @@ -#ifndef MEMORY_AREAS_H -#define MEMORY_AREAS_H +#ifndef _ASM_X86_MEMORY_AREAS_H_ +#define _ASM_X86_MEMORY_AREAS_H_#define AREA_NORMAL_PFN BIT(36-12)#define AREA_NORMAL_NUMBER 0 -#define AREA_NORMAL 1 +#define AREA_NORMAL (1 << AREA_NORMAL_NUMBER) -#define AREA_PAE_HIGH_PFN BIT(32-12) -#define AREA_PAE_HIGH_NUMBER 1 -#define AREA_PAE_HIGH 2 +#define AREA_HIGH_PFN BIT(32-12) +#define AREA_HIGH_NUMBER 1 +#define AREA_HIGH (1 << AREA_HIGH_NUMBER) -#define AREA_LOW_PFN 0 +#define AREA_LOW_PFN BIT(24-12)#define AREA_LOW_NUMBER 2 -#define AREA_LOW 4 +#define AREA_LOW (1 << AREA_LOW_NUMBER) -#define AREA_PAE (AREA_PAE | AREA_LOW) +#define AREA_LOWEST_PFN 0 +#define AREA_LOWEST_NUMBER 3 +#define AREA_LOWEST (1 << AREA_LOWEST_NUMBER) -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define MAX_AREAS 4 + +#define AREA_DMA24 AREA_LOWEST +#define AREA_DMA32 (AREA_LOWEST | AREA_LOW) +#define AREA_PAE36 (AREA_LOWEST | AREA_LOW | AREA_HIGH)#endif lib/alloc_page.h +3
-0@@ -10,6 +10,9 @@#include <asm/memory_areas.h> +#define AREA_ANY -1 +#define AREA_ANY_NUMBER 0xff +/* Returns true if the page allocator has been initialized */ bool page_alloc_initialized (void );lib/alloc_page.c +1
-3@@ -19,8 +19,6 @@#define NLISTS ((BITS_PER_LONG) - (PAGE_SHIFT))#define PFN (x) ((uintptr_t)(x) >> PAGE_SHIFT) -#define MAX_AREAS 6 -#define ORDER_MASK 0x3f#define ALLOC_MASK 0x40#define SPECIAL_MASK 0x80@@ -509,7 +507,7 @@ void page_alloc_init_area(u8 n, uintptr_t base_pfn, uintptr_t top_pfn) return; }#ifdef AREA_HIGH_PFN -__page_alloc_init_area (AREA_HIGH_NUMBER ,AREA_HIGH_PFN ), base_pfn, &top_pfn); + __page_alloc_init_area(AREA_HIGH_NUMBER, AREA_HIGH_PFN,
base_pfn,&top_pfn); #endif
__page_alloc_init_area(AREA_NORMAL_NUMBER,AREA_NORMAL_PFN,base_pfn,&top_pfn); #ifdefAREA_LOW_PFN
--
2.26.2
PATCH v2 5/11 lib/alloc_page: fix and improve the page allocator
In reply to Claudio ImbrendaThis patch introduces some improvements to the code, mostly readability improvements, but also some semantic details, and improvements in the documentation.
* introduce and use pfn_t to semantically tag parameters as PFNs
* remove the PFN macro, use virt_to_pfn instead
* rename area_or_metadata_contains and area_contains to area_contains_pfn and usable_area_contains_pfn respectively
* fix/improve comments in lib/alloc_page.h
* move some wrapper functions to the header
--- lib/alloc_page.h | 52 ++++++++++----- lib/alloc_page.c | 165 +++++++++++++++++++++++------------------------ 2 files changed, 118 insertions(+), 99 deletions(-)
lib/alloc_page.h +36 -16
@@ -8,6 +8,7 @@#ifndef ALLOC_PAGE_H #define ALLOC_PAGE_H 1 +#include <stdbool.h> #include <asm/memory_areas.h> #define AREA_ANY -1
@@ -23,7 +24,7 @@ bool page_alloc_initialized(void);* top_pfn is the physical frame number of the first page immediately after * the end of the area to initialize */ -void page_alloc_init_area (u8 n,uintptr_t base_pfn,uintptr_t top_pfn); +void page_alloc_init_area (u8 n,phys_addr_t base_pfn,phys_addr_t top_pfn);/* Enables the page allocator. At least one area must have been initialized */ void page_alloc_ops_enable (void );
@@ -37,9 +38,12 @@ void *memalign_pages_area(unsigned int areas, size_t alignment, size_t size);/* * Allocate aligned memory from any area. -* Equivalent to memalign_pages_area(~0, alignment, size). +* Equivalent to memalign_pages_area(AREA_ANY, alignment, size). */ -void *memalign_pages (size_t alignment,size_t size); +static inline void *memalign_pages (size_t alignment,size_t size) +{ +return memalign_pages_area (AREA_ANY , alignment, size); +}/* * Allocate naturally aligned memory from the specified areas.
@@ -48,16 +52,23 @@ void *memalign_pages(size_t alignment, size_t size);void *alloc_pages_area (unsigned int areas,unsigned int order);/* -* Allocate one page from any area. -* Equivalent to alloc_pages(0); +* Allocate naturally aligned pages from any area; the number of allocated +* pages is 1 << order. +* Equivalent to alloc_pages_area(AREA_ANY, order); */ -void *alloc_page (void ); +static inline void *alloc_pages (unsigned int order) +{ +return alloc_pages_area (AREA_ANY , order); +}/* -* Allocate naturally aligned memory from any area. -* Equivalent to alloc_pages_area(~0, order); +* Allocate one page from any area. +* Equivalent to alloc_pages(0); */ -void *alloc_pages (unsigned int order); +static inline void *alloc_page (void ) +{ +return alloc_pages (0 ); +}/* * Frees a memory block allocated with any of the memalign_pages* or
@@ -66,31 +77,40 @@ void *alloc_pages(unsigned int order);*/ void free_pages (void *mem); -/* For backwards compatibility */ +/* +* Free one page. +* Equivalent to free_pages(mem). +*/ static inline void free_page (void *mem) {return free_pages (mem); } -/* For backwards compatibility */ +/* +* Free pages by order. +* Equivalent to free_pages(mem). +*/ static inline void free_pages_by_order (void *mem,unsigned int order) {free_pages (mem); }/* -* Allocates and reserves the specified memory range if possible. -* Returns NULL in case of failure. +* Reserves the specified physical memory range if possible. +* If the specified range cannot be reserved in its entirety, no action is +* performed and -1 is returned. +* +* Returns 0 in case of success, -1 otherwise. */ -void *alloc_pages_special (uintptr_t addr,size_t npages); +int reserve_pages (phys_addr_t addr,size_t npages);/* * Frees a reserved memory range that had been reserved with -* alloc_pages_special. +* reserve_pages. * The memory range does not need to match a previous allocation * exactly, it can also be a subset, in which case only the specified * pages will be freed and unreserved. */ -void free_pages_special (uintptr_t addr,size_t npages); +void unreserve_pages (phys_addr_t addr,size_t npages);#endif
lib/alloc_page.c +82 -83
361 lines not on this page · raw message
Signature · 1 line
2.26.2
PATCH v2 6/11 lib/alloc.h: remove align_min from struct alloc_ops
In reply to Claudio ImbrendaRemove align_min from struct alloc_ops, since it is no longer used.
--- lib/alloc.h | 1 - lib/alloc_page.c | 1 - lib/alloc_phys.c | 9 +++++---- lib/vmalloc.c | 1 - 4 files changed, 5 insertions(+), 7 deletions(-)
lib/alloc.h +0 -1
@@ -25,7 +25,6 @@struct alloc_ops {void *(*memalign)(size_t alignment,size_t size);void (*free)(void *ptr); -size_t align_min; };extern struct alloc_ops *alloc_ops;
lib/alloc_page.c +0 -1
@@ -385,7 +385,6 @@ void *memalign_pages_area(unsigned int area, size_t alignment, size_t size)static struct alloc_ops page_alloc_ops = { .memalign = memalign_pages, .free = free_pages, - .align_min =PAGE_SIZE , };/*
lib/alloc_phys.c +5 -4
@@ -29,8 +29,8 @@ static phys_addr_t base, top;static void *early_memalign (size_t alignment,size_t size);static struct alloc_ops early_alloc_ops = { .memalign = early_memalign, - .align_min =DEFAULT_MINIMUM_ALIGNMENT }; +static size_t align_min;struct alloc_ops *alloc_ops = &early_alloc_ops;
@@ -39,8 +39,7 @@ void phys_alloc_show(void)int i;spin_lock (&lock); -printf ("phys_alloc minimum alignment: %#" PRIx64"\n" , - (u64 )early_alloc_ops.align_min); +printf ("phys_alloc minimum alignment: %#" PRIx64"\n" , (u64 )align_min);for (i =0 ; i < nr_regions; ++i) printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n", (u64)regions[i].base,
@@ -64,7 +63,7 @@ void phys_alloc_set_minimum_alignment(phys_addr_t align) {assert (align && !(align & (align -1 )));spin_lock (&lock); - early_alloc_ops.align_min = align; + align_min = align;spin_unlock (&lock); }
@@ -83,6 +82,8 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size, top_safe =MIN (top_safe,1ULL <<32 );assert (base < top_safe); +if (align < align_min) + align = align_min; addr =ALIGN (base, align); size += addr - base;
lib/vmalloc.c +0 -1
@@ -192,7 +192,6 @@ static void vm_free(void *mem)static struct alloc_ops vmalloc_ops = { .memalign = vm_memalign, .free = vm_free, - .align_min =PAGE_SIZE , };void __attribute__((__weak__))find_highmem (void )
Signature · 1 line
2.26.2
PATCH v2 7/11 lib/alloc_page: Optimization to skip known empty freelists
In reply to Claudio ImbrendaKeep track of the largest block order available in each area, and do not search past it when looking for free memory.
This will avoid needlessly scanning the freelists for the largest block orders, which will be empty in most cases.
--- lib/alloc_page.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
lib/alloc_page.c +11 -2
@@ -31,6 +31,8 @@ struct mem_area {pfn_t top;/* Per page metadata, each entry is a combination *_MASK and order */ u8 *page_states; +/* Highest block order available in this area */ +u8 max_order;/* One freelist for each possible block size, up to NLISTS */ struct linked_list freelists[NLISTS ]; };
@@ -104,6 +106,8 @@ static void split(struct mem_area *a, void *addr)assert (a->page_states[idx + i] == order); a->page_states[idx + i] = order -1 ; } +if ((order == a->max_order) && (is_list_empty (a->freelists + order))) + a->max_order--; order--;/* add the first half block to the appropriate free list */ list_add (a->freelists + order, addr);
@@ -127,13 +131,13 @@ static void *page_memalign_order(struct mem_area *a, u8 al, u8 sz) order = sz > al ? sz : al;/* search all free lists for some memory */ -for ( ; order <NLISTS ; order++) { +for ( ; order <= a->max_order; order++) { p = a->freelists[order].next;if (!is_list_empty (p))break ; }/* out of memory */ -if (order >=NLISTS ) +if (order > a->max_order)return NULL ;/*
@@ -201,6 +205,8 @@ static bool coalesce(struct mem_area *a, u8 order, pfn_t pfn, pfn_t pfn2) }/* finally add the newly coalesced block to the appropriate freelist */ list_add (a->freelists + order +1 ,pfn_to_virt (pfn)); +if (order +1 > a->max_order) + a->max_order = order +1 ;return true; }
@@ -438,6 +444,7 @@ static void _page_alloc_init_area(u8 n, pfn_t start_pfn, pfn_t top_pfn) a->page_states =pfn_to_virt (start_pfn); a->base = start_pfn + table_size; a->top = top_pfn; + a->max_order =0 ; npages = top_pfn - a->base;assert ((a->base - start_pfn) *PAGE_SIZE >= npages);
@@ -472,6 +479,8 @@ static void _page_alloc_init_area(u8 n, pfn_t start_pfn, pfn_t top_pfn)/* initialize the metadata and add to the freelist */ memset (a->page_states + (i - a->base), order,BIT (order));list_add (a->freelists + order,pfn_to_virt (i)); +if (order > a->max_order) + a->max_order = order; }/* finally mark the area as present */ areas_mask |=BIT (n);
Signature · 1 line
2.26.2
PATCH v2 8/11 lib/alloc_page: rework metadata format
In reply to Claudio ImbrendaThis patch changes the format of the metadata so that the metadata is now a 2-bit field instead of two separate flags.
This allows to have 4 different states for memory:
STATUS_FRESH: the memory is free and has not been touched at all since
boot (not even read from!)STATUS_FREE: the memory is free, but it is probably not fresh any moreSTATUS_ALLOCATED: the memory has been allocated and is in useSTATUS_SPECIAL: the memory has been removed from the pool of allocated
memory for some kind of special purpose according to the needs of the caller
Some macros are also introduced to test the status of a specific metadata item.
--- lib/alloc_page.c | 49 +++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-)
lib/alloc_page.c +30 -19
@@ -18,9 +18,20 @@#define IS_ALIGNED_ORDER (x,order) IS_ALIGNED((x),BIT_ULL(order))#define NLISTS ((BITS_PER_LONG) - (PAGE_SHIFT)) -#define ORDER_MASK 0x3f -#define ALLOC_MASK 0x40 -#define SPECIAL_MASK 0x80 +#define ORDER_MASK 0x3f +#define STATUS_MASK 0xc0 + +#define STATUS_FRESH 0x00 +#define STATUS_FREE 0x40 +#define STATUS_ALLOCATED 0x80 +#define STATUS_SPECIAL 0xc0 + +#define IS_FRESH (x) (((x) & STATUS_MASK) == STATUS_FRESH) +#define IS_FREE (x) (((x) & STATUS_MASK) == STATUS_FREE) +#define IS_ALLOCATED (x) (((x) & STATUS_MASK) == STATUS_ALLOCATED) +#define IS_SPECIAL (x) (((x) & STATUS_MASK) == STATUS_SPECIAL) + +#define IS_USABLE (x) (IS_FREE(x) || IS_FRESH(x))typedef phys_addr_t pfn_t ;
@@ -87,14 +98,14 @@ static inline bool usable_area_contains_pfn(struct mem_area *a, pfn_t pfn)*/ static void split (struct mem_area *a,void *addr) { -pfn_t pfn =virt_to_pfn (addr); -pfn_t i, idx; -u8 order; +pfn_t i, idx, pfn =virt_to_pfn (addr); +u8 metadata, order;assert (a &&usable_area_contains_pfn (a, pfn)); idx = pfn - a->base; - order = a->page_states[idx]; -assert (!(order & ~ORDER_MASK ) && order && (order <NLISTS )); + metadata = a->page_states[idx]; + order = metadata &ORDER_MASK ; +assert (IS_USABLE (metadata) && order && (order <NLISTS ));assert (IS_ALIGNED_ORDER (pfn, order));assert (usable_area_contains_pfn (a, pfn +BIT (order) -1 ));
@@ -103,8 +114,8 @@ static void split(struct mem_area *a, void *addr)/* update the block size for each page in the block */ for (i =0 ; i <BIT (order); i++) { -assert (a->page_states[idx + i] == order); - a->page_states[idx + i] = order -1 ; +assert (a->page_states[idx + i] == metadata); + a->page_states[idx + i] = metadata -1 ; }if ((order == a->max_order) && (is_list_empty (a->freelists + order))) a->max_order--;
@@ -149,7 +160,7 @@ static void *page_memalign_order(struct mem_area *a, u8 al, u8 sz)split (a, p);list_remove (p); -memset (a->page_states + (virt_to_pfn (p) - a->base),ALLOC_MASK | order,BIT (order)); +memset (a->page_states + (virt_to_pfn (p) - a->base),STATUS_ALLOCATED | order,BIT (order));return p; }
@@ -243,7 +254,7 @@ static void _free_pages(void *mem) order = a->page_states[p] &ORDER_MASK ;/* ensure that the first page is allocated and not special */ -assert (a->page_states[p] == (order |ALLOC_MASK )); +assert (IS_ALLOCATED (a->page_states[p]));/* ensure that the order has a sane value */ assert (order <NLISTS );/* ensure that the block is aligned properly for its size */
@@ -253,9 +264,9 @@ static void _free_pages(void *mem)for (i =0 ; i <BIT (order); i++) {/* check that all pages of the block have consistent metadata */ -assert (a->page_states[p + i] == (ALLOC_MASK | order)); +assert (a->page_states[p + i] == (STATUS_ALLOCATED | order));/* set the page as free */ - a->page_states[p + i] &= ~ALLOC_MASK ; + a->page_states[p + i] =STATUS_FREE | order; }/* provisionally add the block to the appropriate free list */ list_add (a->freelists + order, mem);
@@ -294,13 +305,13 @@ static int _reserve_one_page(pfn_t pfn)if (!a)return -1 ; i = pfn - a->base; -if (a->page_states[i] & (ALLOC_MASK |SPECIAL_MASK )) +if (!IS_USABLE (a->page_states[i]))return -1 ;while (a->page_states[i]) { mask =GENMASK_ULL (63 , a->page_states[i]);split (a,pfn_to_virt (pfn & mask)); } - a->page_states[i] =SPECIAL_MASK ; + a->page_states[i] =STATUS_SPECIAL ;return 0 ; }
@@ -312,8 +323,8 @@ static void _unreserve_one_page(pfn_t pfn) a =get_area (pfn);assert (a); i = pfn - a->base; -assert (a->page_states[i] ==SPECIAL_MASK ); - a->page_states[i] =ALLOC_MASK ; +assert (a->page_states[i] ==STATUS_SPECIAL ); + a->page_states[i] =STATUS_ALLOCATED ;_free_pages (pfn_to_virt (pfn)); }
@@ -477,7 +488,7 @@ static void _page_alloc_init_area(u8 n, pfn_t start_pfn, pfn_t top_pfn) order++;assert (order <NLISTS );/* initialize the metadata and add to the freelist */ -memset (a->page_states + (i - a->base), order,BIT (order)); +memset (a->page_states + (i - a->base),STATUS_FRESH | order,BIT (order));list_add (a->freelists + order,pfn_to_virt (i));if (order > a->max_order) a->max_order = order;
Signature · 1 line
2.26.2
PATCH v2 9/11 lib/alloc: replace areas with more generic flags
In reply to Claudio ImbrendaReplace the areas parameter with a more generic flags parameter. This allows for up to 16 allocation areas and 16 allocation flags.
This patch introduces the flags and changes the names of the funcions, subsequent patches will actually wire up the flags to do something.
The first two flags introduced are:
- FLAG_DONTZERO to ask the allocated memory not to be zeroed
- FLAG_FRESH to indicate that the allocated memory should have not been touched (READ or written to) in any way since boot.
This patch also fixes the order of arguments to consistently have alignment first and then size, thereby fixing a bug where the two values would get swapped.
--- lib/alloc_page.h | 39 ++++++++++++++++++++++----------------- lib/alloc_page.c | 16 ++++++++-------- lib/s390x/smp.c | 2 +- 3 files changed, 31 insertions(+), 26 deletions(-)
lib/alloc_page.h +22 -17
@@ -11,8 +11,13 @@#include <stdbool.h> #include <asm/memory_areas.h> -#define AREA_ANY -1 -#define AREA_ANY_NUMBER 0xff +#define AREA_ANY_NUMBER 0xff + +#define AREA_ANY 0x00000 +#define AREA_MASK 0x0ffff + +#define FLAG_DONTZERO 0x10000 +#define FLAG_FRESH 0x20000/* Returns true if the page allocator has been initialized */ bool page_alloc_initialized (void );
@@ -30,39 +35,39 @@ void page_alloc_init_area(u8 n, phys_addr_t base_pfn, phys_addr_t top_pfn);void page_alloc_ops_enable (void );/* -* Allocate aligned memory from the specified areas. -* areas is a bitmap of allowed areas +* Allocate aligned memory with the specified flags. +* flags is a bitmap of allowed areas and flags. * alignment must be a power of 2 */ -void *memalign_pages_area (unsigned int areas,size_t alignment,size_t size); +void *memalign_pages_flags (size_t alignment,size_t size,unsigned int flags);/* -* Allocate aligned memory from any area. -* Equivalent to memalign_pages_area(AREA_ANY, alignment, size). +* Allocate aligned memory from any area and with default flags. +* Equivalent to memalign_pages_flags(alignment, size, AREA_ANY). */ static inline void *memalign_pages (size_t alignment,size_t size) { -return memalign_pages_area (AREA_ANY , alignment, size); +return memalign_pages_flags (alignment, size,AREA_ANY ); }/* -* Allocate naturally aligned memory from the specified areas. -* Equivalent to memalign_pages_area(areas, 1ull << order, 1ull << order). +* Allocate 1ull << order naturally aligned pages with the specified flags. +* Equivalent to memalign_pages_flags(1ull << order, 1ull << order, flags). */ -void *alloc_pages_area (unsigned int areas,unsigned int order); +void *alloc_pages_flags (unsigned int order,unsigned int flags);/* -* Allocate naturally aligned pages from any area; the number of allocated -* pages is 1 << order. -* Equivalent to alloc_pages_area(AREA_ANY, order); +* Allocate 1ull << order naturally aligned pages from any area and with +* default flags. +* Equivalent to alloc_pages_flags(order, AREA_ANY); */ static inline void *alloc_pages (unsigned int order) { -return alloc_pages_area (AREA_ANY , order); +return alloc_pages_flags (order,AREA_ANY ); }/* -* Allocate one page from any area. +* Allocate one page from any area and with default flags. * Equivalent to alloc_pages(0); */ static inline void *alloc_page (void )
@@ -83,7 +88,7 @@ void free_pages(void *mem);*/ static inline void free_page (void *mem) { -return free_pages (mem); +free_pages (mem); }/*
lib/alloc_page.c +8 -8
@@ -361,16 +361,16 @@ void unreserve_pages(phys_addr_t addr, size_t n)spin_unlock (&lock); } -static void *page_memalign_order_area (unsigned area,u8 ord,u8 al) +static void *page_memalign_order_flags (u8 al,u8 ord,u32 flags) {void *res =NULL ; -int i; +int i, area;spin_lock (&lock); - area &= areas_mask; + area = (flags &AREA_MASK ) ? flags & areas_mask : areas_mask;for (i =0 ; !res && (i <MAX_AREAS ); i++)if (area &BIT (i)) - res =page_memalign_order (areas + i, ord, al); + res =page_memalign_order (areas + i, al, ord);spin_unlock (&lock);return res; }
@@ -379,23 +379,23 @@ static void *page_memalign_order_area(unsigned area, u8 ord, u8 al)* Allocates (1 << order) physically contiguous and naturally aligned pages. * Returns NULL if the allocation was not possible. */ -void *alloc_pages_area (unsigned int area,unsigned int order) +void *alloc_pages_flags (unsigned int order,unsigned int flags) { -return page_memalign_order_area (area, order, order); +return page_memalign_order_flags (order, order, flags); }/* * Allocates (1 << order) physically contiguous aligned pages. * Returns NULL if the allocation was not possible. */ -void *memalign_pages_area (unsigned int area,size_t alignment,size_t size) +void *memalign_pages_flags (size_t alignment,size_t size,unsigned int flags) {assert (is_power_of_2 (alignment)); alignment =get_order (PAGE_ALIGN (alignment) >>PAGE_SHIFT ); size =get_order (PAGE_ALIGN (size) >>PAGE_SHIFT );assert (alignment <NLISTS );assert (size <NLISTS ); -return page_memalign_order_area (area, size, alignment); +return page_memalign_order_flags (alignment, size, flags); }
lib/s390x/smp.c +1 -1
@@ -190,7 +190,7 @@ int smp_cpu_setup(uint16_t addr, struct psw psw)sigp_retry (cpu->addr,SIGP_INITIAL_CPU_RESET ,0 ,NULL ); - lc =alloc_pages_area (AREA_DMA31 ,1 ); + lc =alloc_pages_flags (1 ,AREA_DMA31 ); cpu->lowcore = lc;memset (lc,0 ,PAGE_SIZE *2 );sigp_retry (cpu->addr,SIGP_SET_PREFIX , (unsigned long )lc,NULL );
Signature · 1 line
2.26.2
PATCH v2 10/11 lib/alloc_page: Wire up FLAG_DONTZERO
In reply to Claudio ImbrendaMemory allocated without FLAG_DONTZERO will now be zeroed before being returned to the caller.
This means that by default all allocated memory is now zeroed, restoring the default behaviour that had been accidentally removed by a previous commit.
--- lib/alloc_page.c | 2 ++ 1 file changed, 2 insertions(+)
lib/alloc_page.c +2 -0
@@ -372,6 +372,8 @@ static void *page_memalign_order_flags(u8 al, u8 ord, u32 flags)if (area &BIT (i)) res =page_memalign_order (areas + i, al, ord);spin_unlock (&lock); +if (res && !(flags &FLAG_DONTZERO )) +memset (res,0 ,BIT (ord) *PAGE_SIZE );return res; }
Signature · 1 line
2.26.2
PATCH v2 11/11 lib/alloc_page: Properly handle requests for fresh blocks
In reply to Claudio ImbrendaUpon initialization, all memory in an area is marked as fresh. Once memory is used and freed, the freed memory is marked as free.
Free memory is always appended to the front of the freelist, meaning that fresh memory stays on the tail.
When a block of fresh memory is split, the two blocks are put on the tail of the appropriate freelist, so they can be found when needed.
When a fresh block is requested, a fresh block one order bigger is taken, the first half is put back in the free pool (on the tail), and the second half is returned. The reason behind this is that the first page of every block always contains the pointers of the freelist. Since the first page of a fresh block is actually not fresh, it cannot be returned when a fresh allocation is requested.
--- lib/alloc_page.c | 51 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-)
lib/alloc_page.c +40 -11
@@ -120,10 +120,17 @@ static void split(struct mem_area *a, void *addr)if ((order == a->max_order) && (is_list_empty (a->freelists + order))) a->max_order--; order--; -/* add the first half block to the appropriate free list */ -list_add (a->freelists + order, addr); -/* add the second half block to the appropriate free list */ -list_add (a->freelists + order,pfn_to_virt (pfn +BIT (order))); + +/* add the two half blocks to the appropriate free list */ +if (IS_FRESH (metadata)) { +/* add to the tail if the blocks are fresh */ +list_add_tail (a->freelists + order, addr); +list_add_tail (a->freelists + order,pfn_to_virt (pfn +BIT (order))); + }else { +/* add to the front if the blocks are dirty */ +list_add (a->freelists + order, addr); +list_add (a->freelists + order,pfn_to_virt (pfn +BIT (order))); + } }/*
@@ -132,21 +139,33 @@ static void split(struct mem_area *a, void *addr)* * Both parameters must be not larger than the largest allowed order */ -static void *page_memalign_order (struct mem_area *a,u8 al,u8 sz) +static void *page_memalign_order (struct mem_area *a,u8 al,u8 sz,bool fresh) {struct linked_list *p; +pfn_t idx;u8 order;assert ((al <NLISTS ) && (sz <NLISTS ));/* we need the bigger of the two as starting point */ order = sz > al ? sz : al; +/* +* we need to go one order up if we want a completely fresh block, +* since the first page contains the freelist pointers, and +* therefore it is always dirty +*/ + order += fresh;/* search all free lists for some memory */ for ( ; order <= a->max_order; order++) { - p = a->freelists[order].next; -if (!is_list_empty (p)) -break ; + p = fresh ? a->freelists[order].prev : a->freelists[order].next; +if (is_list_empty (p)) +continue ; + idx =virt_to_pfn (p) - a->base; +if (fresh && !IS_FRESH (a->page_states[idx])) +continue ; +break ; } +/* out of memory */ if (order > a->max_order)return NULL ;
@@ -160,7 +179,16 @@ static void *page_memalign_order(struct mem_area *a, u8 al, u8 sz)split (a, p);list_remove (p); -memset (a->page_states + (virt_to_pfn (p) - a->base),STATUS_ALLOCATED | order,BIT (order)); +/* We now have a block twice the size, but the first page is dirty. */ +if (fresh) { + order--; +/* Put back the first (partially dirty) half of the block */ +memset (a->page_states + idx,STATUS_FRESH | order,BIT (order)); +list_add_tail (a->freelists + order, p); + idx +=BIT (order); + p =pfn_to_virt (a->base + idx); + } +memset (a->page_states + idx,STATUS_ALLOCATED | order,BIT (order));return p; }
@@ -364,13 +392,14 @@ void unreserve_pages(phys_addr_t addr, size_t n)static void *page_memalign_order_flags (u8 al,u8 ord,u32 flags) {void *res =NULL ; -int i, area; +int i, area, fresh; + fresh = !!(flags &FLAG_FRESH );spin_lock (&lock); area = (flags &AREA_MASK ) ? flags & areas_mask : areas_mask;for (i =0 ; !res && (i <MAX_AREAS ); i++)if (area &BIT (i)) - res =page_memalign_order (areas + i, al, ord); + res =page_memalign_order (areas + i, al, ord, fresh);spin_unlock (&lock);if (res && !(flags &FLAG_DONTZERO ))memset (res,0 ,BIT (ord) *PAGE_SIZE );
Signature · 1 line
2.26.2