Introduce memblock simulator
v2 cover Introduce memblock simulator
Memblock is a boot time memory allocator, which allows managing memory regions before the actual memory management is initialized. Because it is used so early during the booting process, testing and debugging it is difficult. As memblock has few kernel dependencies, it is possible to simulate its runtime behaviour in userspace after stubbing a couple of structs and functions.
This series of patches adds an initial version of a test suite for memblock, which is a part of tools/testing infrastructure. Checks included here test the basic functionality of memblock, which is memory regions management - adding/removing available regions, marking them as reserved or freeing them.
To run the tests, build the main target and run it:
$ make; ./main
A successful run produces no output. Also, It is possible to override different configuration parameters. For example, to simulate enabled NUMA, use:
$ make NUMA=1
For the full list of options, see make help.
The future work will include tests for memory allocation done via memblock_alloc_* and memblock_phys_alloc_* family of functions. It will also focus on improving the test suite by adding dedicated test targets for each group of API functions and verbose output.
For a refresher on memblock, see Boot time memory management documentation page[1] and "Getting to know memblock"[2] blog post.
Patchset structure:
* Patch 1 extracts stub definitions of kmalloc/kfree from the radix-tree testing suite, so they can be used in other tests, including memblock simulator
* Patches 2-4 update tools headers to include missing definitions needed by memblock (e.g. phys_addr_t typedef)
* Patches 5-9 add new files to tools/include, some with dummy/downsized definitions
* Patch 10 adds the basic project with local stubs that are closely connected to memblock
* Patch 11 adds a reset function, which is used as a part of the test setup. This functionality is needed, as memblock is reused in each check
* Patches 12-16 add basic test cases for the core memblock functionalities
[1] - https://www.kernel.org/doc/html/latest/core-api/boot-time-mm.html
[2] - https://insecuremode.com/post/2021/12/14/getting-to-know-memblock.html
Changes in v2:
- Fixed definition ordering in tools/include/linux/gfp.h
- Defined atomic_long_t as a struct instead of a typedef
- Deleted BIT macro from L1_CACHE_BYTES definition
- Removed SPDX information from .gitignore file
- Updated Makefile comment on the simulator dependencies
- Updated __initconst definition to be an empty macro (full definition is not required)
Karolina Drobnik (16):
tools: Move gfp.h and slab.h from radix-tree to lib tools/include: Add phys_addr_t to types.h tools/include: Add _RET_IP_ and math definitions to kernel.h tools/include: Update atomic definitions tools/include: Add mm.h file
tools/include: Add cache.h stub
tools/include: Add io.h stub
tools/include: Add pfn.h stub
tools/include: Add debugfs.h stub
memblock tests: Add skeleton of the memblock simulator memblock tests: Add memblock reset function memblock tests: Add memblock_add tests
memblock tests: Add memblock_reserve tests memblock tests: Add memblock_remove tests
memblock tests: Add memblock_add_node test memblock tests: Add memblock_free tests
MAINTAINERS | 1 +
tools/include/linux/atomic.h | 2 +
tools/include/linux/cache.h | 10 +
tools/include/linux/debugfs.h | 5 +
tools/include/linux/gfp.h | 28 +
tools/include/linux/io.h | 5 +
tools/include/linux/kernel.h | 6 +
tools/include/linux/mm.h | 42 +
tools/include/linux/pfn.h | 10 +
.../radix-tree => include}/linux/slab.h | 25 +-
tools/include/linux/types.h | 10 +
tools/lib/slab.c | 38 +
tools/testing/memblock/.gitignore | 4 +
tools/testing/memblock/Makefile | 54 ++
tools/testing/memblock/asm/dma.h | 5 +
tools/testing/memblock/internal.h | 12 +
tools/testing/memblock/lib/slab.c | 9 +
tools/testing/memblock/linux/init.h | 34 +
tools/testing/memblock/linux/kernel.h | 12 +
tools/testing/memblock/linux/kmemleak.h | 18 +
tools/testing/memblock/linux/memory_hotplug.h | 19 +
tools/testing/memblock/linux/mmzone.h | 35 +
tools/testing/memblock/linux/printk.h | 25 +
tools/testing/memblock/main.c | 8 +
tools/testing/memblock/mmzone.c | 20 +
.../testing/memblock/scripts/Makefile.include | 17 +
tools/testing/memblock/tests/basic_api.c | 896 ++++++++++++++++++
tools/testing/memblock/tests/basic_api.h | 10 +
tools/testing/memblock/tests/common.c | 27 +
tools/testing/memblock/tests/common.h | 15 +
tools/testing/radix-tree/Makefile | 3 +-
tools/testing/radix-tree/linux.c | 27 -
tools/testing/radix-tree/linux/gfp.h | 33 -
33 files changed, 1397 insertions(+), 68 deletions(-)
create mode 100644 tools/include/linux/cache.h
create mode 100644 tools/include/linux/debugfs.h
create mode 100644 tools/include/linux/io.h
create mode 100644 tools/include/linux/mm.h
create mode 100644 tools/include/linux/pfn.h
rename tools/{testing/radix-tree => include}/linux/slab.h (61%)
create mode 100644 tools/lib/slab.c
create mode 100644 tools/testing/memblock/.gitignore
create mode 100644 tools/testing/memblock/Makefile
create mode 100644 tools/testing/memblock/asm/dma.h
create mode 100644 tools/testing/memblock/internal.h
create mode 100644 tools/testing/memblock/lib/slab.c
create mode 100644 tools/testing/memblock/linux/init.h
create mode 100644 tools/testing/memblock/linux/kernel.h
create mode 100644 tools/testing/memblock/linux/kmemleak.h
create mode 100644 tools/testing/memblock/linux/memory_hotplug.h
create mode 100644 tools/testing/memblock/linux/mmzone.h
create mode 100644 tools/testing/memblock/linux/printk.h
create mode 100644 tools/testing/memblock/main.c
create mode 100644 tools/testing/memblock/mmzone.c
create mode 100644 tools/testing/memblock/scripts/Makefile.include
create mode 100644 tools/testing/memblock/tests/basic_api.c
create mode 100644 tools/testing/memblock/tests/basic_api.h
create mode 100644 tools/testing/memblock/tests/common.c
create mode 100644 tools/testing/memblock/tests/common.h
delete mode 100644 tools/testing/radix-tree/linux/gfp.h--
2.30.2
PATCH v2 1/16 tools: Move gfp.h and slab.h from radix-tree to lib
Merge radix-tree definitions from gfp.h and slab.h with these in tools/lib, so they can be used in other test suites. Fix style issues in slab.h. Update radix-tree test files.
---
tools/include/linux/gfp.h | 28 ++++++++++++++
.../radix-tree => include}/linux/slab.h | 15 ++++----
tools/lib/slab.c | 38 +++++++++++++++++++
tools/testing/radix-tree/Makefile | 3 +-
tools/testing/radix-tree/linux.c | 27 -------------
tools/testing/radix-tree/linux/gfp.h | 33 ----------------
6 files changed, 76 insertions(+), 68 deletions(-)
rename tools/{testing/radix-tree => include}/linux/slab.h (68%)
create mode 100644 tools/lib/slab.c
delete mode 100644 tools/testing/radix-tree/linux/gfp.htools/include/linux/gfp.h +28 -0
@@ -1,4 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ #ifndef _TOOLS_INCLUDE_LINUX_GFP_H#define _TOOLS_INCLUDE_LINUX_GFP_H +#include <linux/types.h> + +#define __GFP_BITS_SHIFT 26 +#define __GFP_BITS_MASK ((gfp_t)((1 << __GFP_BITS_SHIFT) - 1)) + +#define __GFP_HIGH 0x20u +#define __GFP_IO 0x40u +#define __GFP_FS 0x80u +#define __GFP_NOWARN 0x200u +#define __GFP_ZERO 0x8000u +#define __GFP_ATOMIC 0x80000u +#define __GFP_ACCOUNT 0x100000u +#define __GFP_DIRECT_RECLAIM 0x400000u +#define __GFP_KSWAPD_RECLAIM 0x2000000u + +#define __GFP_RECLAIM (__GFP_DIRECT_RECLAIM | __GFP_KSWAPD_RECLAIM) + +#define GFP_ZONEMASK 0x0fu +#define GFP_ATOMIC (__GFP_HIGH | __GFP_ATOMIC | __GFP_KSWAPD_RECLAIM) +#define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS) +#define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM) + +static inline bool gfpflags_allow_blocking (const gfp_t gfp_flags) +{ +return !!(gfp_flags & __GFP_DIRECT_RECLAIM); +} +#endif /* _TOOLS_INCLUDE_LINUX_GFP_H */
tools/include/linux/slab.h renamed from tools/testing/radix-tree/linux/slab.h +8 -7
@@ -1,20 +1,21 @@/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef SLAB_H -#define SLAB_H +#ifndef _TOOLS_SLAB_H +#define _TOOLS_SLAB_H#include <linux/types.h> #include <linux/gfp.h> -#define SLAB_HWCACHE_ALIGN 1#define SLAB_PANIC 2#define SLAB_RECLAIM_ACCOUNT 0x00020000UL/* Objects are reclaimable */ -void *kmalloc (size_t size,gfp_t ); -void kfree (void *); +#define kzalloc_node (size, flags, node) kmalloc(size, flags) + +void *kmalloc (size_t size,gfp_t gfp); +void kfree (void *p);static inline void *kzalloc (size_t size,gfp_t gfp) { -return kmalloc (size, gfp | __GFP_ZERO); +return kmalloc (size, gfp | __GFP_ZERO); }void *kmem_cache_alloc (struct kmem_cache *cachep,int flags);
@@ -24,4 +25,4 @@ struct kmem_cache *kmem_cache_create(const char *name, unsigned int size, unsigned int align, unsigned int flags, void (*ctor)(void *)); -#endif /* SLAB_H */ +#endif /* _TOOLS_SLAB_H */
tools/lib/slab.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <stdio.h> +#include <string.h> + +#include <urcu/uatomic.h> +#include <linux/slab.h> +#include <malloc.h> +#include <linux/gfp.h> + +int kmalloc_nr_allocated; +int kmalloc_verbose; + +void *kmalloc (size_t size,gfp_t gfp) +{ +void *ret; + +if (!(gfp & __GFP_DIRECT_RECLAIM)) +return NULL ; + + ret =malloc (size); +uatomic_inc (&kmalloc_nr_allocated); +if (kmalloc_verbose) +printf ("Allocating %p from malloc\n" , ret); +if (gfp & __GFP_ZERO) +memset (ret,0 , size); +return ret; +} + +void kfree (void *p) +{ +if (!p) +return ; +uatomic_dec (&kmalloc_nr_allocated); +if (kmalloc_verbose) +printf ("Freeing %p to malloc\n" , p); +free (p); +}
tools/testing/radix-tree/Makefile +2 -1
@@ -5,7 +5,8 @@ CFLAGS += -I. -I../../include -g -Og -Wall -D_LGPL_SOURCE -fsanitize=address \ LDFLAGS += -fsanitize=address -fsanitize=undefined LDLIBS+= -lpthread -lurcu TARGETS = main idr-test multiorder xarray -CORE_OFILES := xarray.o radix-tree.o idr.o linux.o test.o find_bit.o bitmap.o +CORE_OFILES := xarray.o radix-tree.o idr.o linux.o test.o find_bit.o bitmap.o \ + slab.o OFILES = main.o $(CORE_OFILES) regression1.o regression2.o regression3.o \ regression4.o tag_check.o multiorder.o idr-test.o iteration_check.o \ iteration_check_2.o benchmark.o
tools/testing/radix-tree/linux.c +0 -27
@@ -14,7 +14,6 @@int nr_allocated;int preempt_count; -int kmalloc_verbose;int test_verbose;struct kmem_cache {
@@ -78,32 +77,6 @@ void kmem_cache_free(struct kmem_cache *cachep, void *objp)pthread_mutex_unlock (&cachep->lock); } -void *kmalloc (size_t size,gfp_t gfp) -{ -void *ret; - -if (!(gfp & __GFP_DIRECT_RECLAIM)) -return NULL ; - - ret =malloc (size); -uatomic_inc (&nr_allocated); -if (kmalloc_verbose) -printf ("Allocating %p from malloc\n" , ret); -if (gfp & __GFP_ZERO) -memset (ret,0 , size); -return ret; -} - -void kfree (void *p) -{ -if (!p) -return ; -uatomic_dec (&nr_allocated); -if (kmalloc_verbose) -printf ("Freeing %p to malloc\n" , p); -free (p); -} -struct kmem_cache *kmem_cache_create (const char *name,unsigned int size,unsigned int align,unsigned int flags,void (*ctor)(void *))
tools/testing/radix-tree/linux/gfp.h deleted +0 -33
@@ -1,33 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _GFP_H -#define _GFP_H - -#include <linux/types.h> - -#define __GFP_BITS_SHIFT 26 -#define __GFP_BITS_MASK ((gfp_t)((1 << __GFP_BITS_SHIFT) - 1)) - -#define __GFP_HIGH 0x20u -#define __GFP_IO 0x40u -#define __GFP_FS 0x80u -#define __GFP_NOWARN 0x200u -#define __GFP_ZERO 0x8000u -#define __GFP_ATOMIC 0x80000u -#define __GFP_ACCOUNT 0x100000u -#define __GFP_DIRECT_RECLAIM 0x400000u -#define __GFP_KSWAPD_RECLAIM 0x2000000u - -#define __GFP_RECLAIM (__GFP_DIRECT_RECLAIM|__GFP_KSWAPD_RECLAIM) - -#define GFP_ZONEMASK 0x0fu -#define GFP_ATOMIC (__GFP_HIGH|__GFP_ATOMIC|__GFP_KSWAPD_RECLAIM) -#define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS) -#define GFP_NOWAIT (__GFP_KSWAPD_RECLAIM) - - -static inline bool gfpflags_allow_blocking (const gfp_t gfp_flags) -{ -return !!(gfp_flags & __GFP_DIRECT_RECLAIM); -} - -#endif
Signature · 1 line
2.30.2
PATCH v2 2/16 tools/include: Add phys_addr_t to types.h
In reply to Karolina DrobnikUpdate types.h file to include phys_addr_t typedef so it can be used in testing.
--- tools/include/linux/types.h | 6 ++++++ 1 file changed, 6 insertions(+)
tools/include/linux/types.h +6 -0
@@ -64,6 +64,12 @@ typedef __u64 __bitwise __be64;typedef __u16 __bitwise __sum16 ;typedef __u32 __bitwise __wsum ; +#ifdef CONFIG_PHYS_ADDR_T_64BIT +typedef u64 phys_addr_t ; +#else +typedef u32 phys_addr_t ; +#endif +typedef struct {int counter; }atomic_t ;
Signature · 1 line
2.30.2
PATCH v2 4/16 tools/include: Update atomic definitions
In reply to Karolina DrobnikAdd atomic_long_set function to atomic.h and atomic_long_t type to types.h so they can be used in testing.
--- tools/include/linux/atomic.h | 2 ++ tools/include/linux/types.h | 4 ++++ 2 files changed, 6 insertions(+)
tools/include/linux/atomic.h +2 -0
@@ -4,6 +4,8 @@#include <asm/atomic.h> +void atomic_long_set (atomic_long_t *v,long i); +/* atomic_cmpxchg_relaxed */ #ifndef atomic_cmpxchg_relaxed#define atomic_cmpxchg_relaxed atomic_cmpxchg
tools/include/linux/types.h +4 -0
@@ -74,6 +74,10 @@ typedef struct {int counter; } atomic_t; +typedef struct { +long counter; +}atomic_long_t ; +#ifndef __aligned_u64# define __aligned_u64 __u64 __attribute__((aligned(8)))#endif
Signature · 1 line
2.30.2
PATCH v2 3/16 tools/include: Add _RET_IP_ and math definitions to kernel.h
In reply to Karolina DrobnikAdd max_t, min_t and clamp functions, together with _RET_IP_ definition, so they can be used in testing.
--- tools/include/linux/kernel.h | 6 ++++++ 1 file changed, 6 insertions(+)
tools/include/linux/kernel.h +6 -0
@@ -15,6 +15,8 @@#define UINT_MAX (~0U)#endif +#define _RET_IP_ ((unsigned long)__builtin_return_address(0)) +#define PERF_ALIGN (x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)#define __PERF_ALIGN_MASK (x, mask) (((x)+(mask))&~(mask))
@@ -51,6 +53,10 @@ _min1 < _min2 ? _min1 : _min2; })#endif +#define max_t (type, x, y) max((type)x, (type)y) +#define min_t (type, x, y) min((type)x, (type)y) +#define clamp (val, lo, hi) min((typeof(val))max(val, lo), hi) +#ifndef BUG_ON #ifdef NDEBUG #define BUG_ON (cond) do { if (cond) {} } while (0)
Signature · 1 line
2.30.2
PATCH v2 5/16 tools/include: Add mm.h file
In reply to Karolina DrobnikAdd a stubbed mm.h file with dummy page-related definitions, memory alignment and physical to virtual address conversions, so they can be used in testing.
--- tools/include/linux/mm.h | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tools/include/linux/mm.h
tools/include/linux/mm.h new file +42 -0
@@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_LINUX_MM_H +#define _TOOLS_LINUX_MM_H + +#include <linux/mmzone.h> +#include <uapi/linux/const.h> + +#define PAGE_SHIFT 12 +#define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT) +#define PAGE_MASK (~(PAGE_SIZE - 1)) + +#define PHYS_ADDR_MAX (~(phys_addr_t)0) + +#define __ALIGN_KERNEL (x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1) +#define __ALIGN_KERNEL_MASK (x, mask) (((x) + (mask)) & ~(mask)) +#define ALIGN (x, a) __ALIGN_KERNEL((x), (a)) +#define ALIGN_DOWN (x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a)) + +#define PAGE_ALIGN (addr) ALIGN(addr, PAGE_SIZE) + +#define __va (x) ((void *)((unsigned long)(x))) +#define __pa (x) ((unsigned long)(x)) + +#define pfn_to_page (pfn) ((void *)((pfn) * PAGE_SIZE)) + +#define phys_to_virt phys_to_virt +static inline void *phys_to_virt (unsigned long address) +{ +return __va (address); +} + +void reserve_bootmem_region (phys_addr_t start,phys_addr_t end); + +static inline void totalram_pages_inc (void ) +{ +} + +static inline void totalram_pages_add (long count) +{ +} + +#endif
Signature · 1 line
2.30.2
PATCH v2 6/16 tools/include: Add cache.h stub
In reply to Karolina DrobnikAdd a dummy version of the cache header.
--- tools/include/linux/cache.h | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tools/include/linux/cache.h
tools/include/linux/cache.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_LINUX_CACHE_H +#define _TOOLS_LINUX_CACHE_H + +#define L1_CACHE_SHIFT 5 +#define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT) + +#define SMP_CACHE_BYTES L1_CACHE_BYTES + +#endif
Signature · 1 line
2.30.2
PATCH v2 7/16 tools/include: Add io.h stub
In reply to Karolina DrobnikAdd a dummy io.h header.
--- tools/include/linux/io.h | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tools/include/linux/io.h
tools/include/linux/io.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_IO_H +#define _TOOLS_IO_H + +#endif
Signature · 1 line
2.30.2
PATCH v2 8/16 tools/include: Add pfn.h stub
In reply to Karolina DrobnikAdd a stubbed pfn header with definitions used in testing.
--- tools/include/linux/pfn.h | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tools/include/linux/pfn.h
tools/include/linux/pfn.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_LINUX_PFN_H_ +#define _TOOLS_LINUX_PFN_H_ + +#include <linux/mm.h> + +#define PFN_UP (x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT) +#define PFN_DOWN (x) ((x) >> PAGE_SHIFT) +#define PFN_PHYS (x) ((phys_addr_t)(x) << PAGE_SHIFT) +#endif
Signature · 1 line
2.30.2
PATCH v2 9/16 tools/include: Add debugfs.h stub
In reply to Karolina DrobnikAdd a dummy debugfs.h header.
--- tools/include/linux/debugfs.h | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tools/include/linux/debugfs.h
tools/include/linux/debugfs.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_DEBUGFS_H +#define _TOOLS_DEBUGFS_H + +#endif
Signature · 1 line
2.30.2
PATCH v2 10/16 memblock tests: Add skeleton of the memblock simulator
In reply to Karolina DrobnikAdd basic project files, together with local stubs of required headers. Update tools/include/slab.h to include definitions used by memblock.
--- MAINTAINERS | 1 + tools/include/linux/slab.h | 10 ++++ tools/testing/memblock/.gitignore | 4 ++ tools/testing/memblock/Makefile | 52 +++++++++++++++++++ tools/testing/memblock/asm/dma.h | 5 ++ tools/testing/memblock/internal.h | 12 +++++ tools/testing/memblock/lib/slab.c | 9 ++++ tools/testing/memblock/linux/init.h | 34 ++++++++++++ tools/testing/memblock/linux/kernel.h | 12 +++++ tools/testing/memblock/linux/kmemleak.h | 18 +++++++ tools/testing/memblock/linux/memory_hotplug.h | 19 +++++++ tools/testing/memblock/linux/mmzone.h | 35 +++++++++++++ tools/testing/memblock/linux/printk.h | 25 +++++++++ tools/testing/memblock/main.c | 6 +++ tools/testing/memblock/mmzone.c | 20 +++++++ .../testing/memblock/scripts/Makefile.include | 17 ++++++ 16 files changed, 279 insertions(+) create mode 100644 tools/testing/memblock/.gitignore create mode 100644 tools/testing/memblock/Makefile create mode 100644 tools/testing/memblock/asm/dma.h create mode 100644 tools/testing/memblock/internal.h create mode 100644 tools/testing/memblock/lib/slab.c create mode 100644 tools/testing/memblock/linux/init.h create mode 100644 tools/testing/memblock/linux/kernel.h create mode 100644 tools/testing/memblock/linux/kmemleak.h create mode 100644 tools/testing/memblock/linux/memory_hotplug.h create mode 100644 tools/testing/memblock/linux/mmzone.h create mode 100644 tools/testing/memblock/linux/printk.h create mode 100644 tools/testing/memblock/main.c create mode 100644 tools/testing/memblock/mmzone.c create mode 100644 tools/testing/memblock/scripts/Makefile.include
MAINTAINERS +1 -0
@@ -12406,6 +12406,7 @@ S: Maintained F: Documentation/core-api/boot-time-mm.rst F: include/linux/memblock.h F: mm/memblock.c +F: tools/testing/memblock/ MEMORY CONTROLLER DRIVERS M: Krzysztof Kozlowski <[email protected]>
tools/include/linux/slab.h +10 -0
@@ -13,6 +13,16 @@void *kmalloc (size_t size,gfp_t gfp);void kfree (void *p); +bool slab_is_available (void ); + +enum slab_state { +DOWN , +PARTIAL , +PARTIAL_NODE , +UP , +FULL +}; +static inline void *kzalloc (size_t size,gfp_t gfp) {return kmalloc (size, gfp | __GFP_ZERO);
tools/testing/memblock/.gitignore new file +4 -0
@@ -0,0 +1,4 @@ +main +memblock.c +linux/memblock.h +asm/cmpxchg.h
tools/testing/memblock/Makefile new file +52 -0
52 lines not on this page · raw message
tools/testing/memblock/asm/dma.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_DMA_H +#define _TOOLS_DMA_H + +#endif
tools/testing/memblock/internal.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _MM_INTERNAL_H +#define _MM_INTERNAL_H + +struct page {}; + +void memblock_free_pages (struct page *page,unsigned long pfn, +unsigned int order) +{ +} + +#endif
tools/testing/memblock/lib/slab.c new file +9 -0
@@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/slab.h> + +enum slab_state slab_state; + +bool slab_is_available (void ) +{ +return slab_state >=UP ; +}
tools/testing/memblock/linux/init.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_INIT_H +#define _LINUX_INIT_H + +#include <linux/compiler.h> +#include <asm/export.h> +#include <linux/memory_hotplug.h> + +#define __section (section) __attribute__((__section__(section))) + +#define __initconst +#define __meminit +#define __meminitdata +#define __refdata +#define __initdata + +struct obs_kernel_param { +const char *str; +int (*setup_func)(char *st); +int early; +}; + +#define __setup_param (str, unique_id, fn, early) \ + static const char __setup_str_##unique_id[] __initconst \ + __aligned(1) = str; \ + static struct obs_kernel_param __setup_##unique_id \ + __used __section(".init.setup") \ + __aligned(__alignof__(struct obs_kernel_param)) = \ + { __setup_str_##unique_id, fn, early } + +#define early_param (str, fn) \ + __setup_param(str, fn, fn, 1) + +#endif
tools/testing/memblock/linux/kernel.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _MEMBLOCK_LINUX_KERNEL_H +#define _MEMBLOCK_LINUX_KERNEL_H + +#include <../../include/linux/kernel.h> +#include <linux/errno.h> +#include <string.h> +#include <linux/printk.h> +#include <linux/linkage.h> +#include <linux/kconfig.h> + +#endif
tools/testing/memblock/linux/kmemleak.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _KMEMLEAK_H +#define _KMEMLEAK_H + +static inline void kmemleak_free_part_phys (phys_addr_t phys,size_t size) +{ +} + +static inline void kmemleak_alloc_phys (phys_addr_t phys,size_t size, +int min_count,gfp_t gfp) +{ +} + +static inline void dump_stack (void ) +{ +} + +#endif
tools/testing/memblock/linux/memory_hotplug.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_MEMORY_HOTPLUG_H +#define _LINUX_MEMORY_HOTPLUG_H + +#include <linux/numa.h> +#include <linux/pfn.h> +#include <linux/cache.h> +#include <linux/types.h> + +static inline bool movable_node_is_enabled (void ) +{ +#ifdef MOVABLE_NODE +return true; +#else +return false; +#endif +} + +#endif
tools/testing/memblock/linux/mmzone.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_MMZONE_H +#define _TOOLS_MMZONE_H + +#include <linux/atomic.h> + +struct pglist_data *first_online_pgdat (void ); +struct pglist_data *next_online_pgdat (struct pglist_data *pgdat); + +#define for_each_online_pgdat (pgdat) \ + for (pgdat = first_online_pgdat(); \ + pgdat; \ + pgdat = next_online_pgdat(pgdat)) + +enum zone_type { + __MAX_NR_ZONES +}; + +#define MAX_NR_ZONES __MAX_NR_ZONES +#define MAX_ORDER 11 +#define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1)) + +#define pageblock_order (MAX_ORDER - 1) +#define pageblock_nr_pages BIT(pageblock_order) + +struct zone { +atomic_long_t managed_pages; +}; + +typedef struct pglist_data { +struct zone node_zones[MAX_NR_ZONES ]; + +}pg_data_t ; + +#endif
tools/testing/memblock/linux/printk.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _PRINTK_H +#define _PRINTK_H + +#include <stdio.h> +#include <asm/bug.h> + +/* +* memblock_dbg is called with u64 arguments that don't match the "%llu" +* specifier in printf. This results in warnings that cannot be fixed without +* modifying memblock.c, which we wish to avoid. As these messaged are not used +* in testing anyway, the mismatch can be ignored. +*/ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat" +#define printk printf +#pragma GCC diagnostic push + +#define pr_info printk +#define pr_debug printk +#define pr_cont printk +#define pr_err printk +#define pr_warn printk + +#endif
tools/testing/memblock/main.c new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +int main (int argc,char **argv) +{ +return 0 ; +}
tools/testing/memblock/mmzone.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include <linux/mmzone.h> + +struct pglist_data *first_online_pgdat (void ) +{ +return NULL ; +} + +struct pglist_data *next_online_pgdat (struct pglist_data *pgdat) +{ +return NULL ; +} + +void reserve_bootmem_region (phys_addr_t start,phys_addr_t end) +{ +} + +void atomic_long_set (atomic_long_t *v,long i) +{ +}
tools/testing/memblock/scripts/Makefile.include new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 +# Definitions for user-provided arguments + +# Simulate CONFIG_NUMA=y +ifeq ($(NUMA), 1) + CFLAGS += -D CONFIG_NUMA +endif + +# Simulate movable NUMA memory regions +ifeq ($(MOVABLE_NODE), 1) + CFLAGS += -D MOVABLE_NODE +endif + +# Use 32 bit physical addresses +ifeq ($(32BIT_PHYS_ADDR_T), 1) + CFLAGS += -U CONFIG_PHYS_ADDR_T_64BIT +endif
Signature · 1 line
2.30.2
PATCH v2 11/16 memblock tests: Add memblock reset function
In reply to Karolina DrobnikMemblock simulator needs to be able to reset memblock data structures between different test cases. Add a function that sets all fields to their default values.
Add a test checking if memblock is being initialized to expected values.
--- tools/testing/memblock/Makefile | 4 ++- tools/testing/memblock/main.c | 2 ++ tools/testing/memblock/tests/basic_api.c | 32 ++++++++++++++++++++++++ tools/testing/memblock/tests/basic_api.h | 10 ++++++++ tools/testing/memblock/tests/common.c | 27 ++++++++++++++++++++ tools/testing/memblock/tests/common.h | 15 +++++++++++ 6 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tools/testing/memblock/tests/basic_api.c create mode 100644 tools/testing/memblock/tests/basic_api.h create mode 100644 tools/testing/memblock/tests/common.c create mode 100644 tools/testing/memblock/tests/common.h
tools/testing/memblock/Makefile +3 -1
@@ -6,7 +6,9 @@ CFLAGS += -I. -I../../include -Wall -O2 -fsanitize=address \ -fsanitize=undefined -D CONFIG_PHYS_ADDR_T_64BIT LDFLAGS += -fsanitize=address -fsanitize=undefined TARGETS = main -OFILES = main.o memblock.o lib/slab.o mmzone.o slab.o +TEST_OFILES = tests/basic_api.o tests/common.o +DEP_OFILES = memblock.o lib/slab.o mmzone.o slab.o +OFILES = main.o $(DEP_OFILES) $(TEST_OFILES) EXTR_SRC = ../../../mm/memblock.c ifeq ($(BUILD), 32)
tools/testing/memblock/main.c +2 -0
@@ -1,6 +1,8 @@// SPDX-License-Identifier: GPL-2.0-or-later +#include "tests/basic_api.h" int main (int argc,char **argv) { +memblock_basic_checks ();return 0 ; }
tools/testing/memblock/tests/basic_api.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include <string.h> +#include <linux/memblock.h> +#include "basic_api.h" + +#define EXPECTED_MEMBLOCK_REGIONS 128 + +static int memblock_initialization_check (void ) +{ +reset_memblock (); + +assert (memblock.memory.regions); +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.max ==EXPECTED_MEMBLOCK_REGIONS ); +assert (strcmp (memblock.memory.name,"memory" ) ==0 ); + +assert (memblock.reserved.regions); +assert (memblock.reserved.cnt ==1 ); +assert (memblock.memory.max ==EXPECTED_MEMBLOCK_REGIONS ); +assert (strcmp (memblock.reserved.name,"reserved" ) ==0 ); + +assert (!memblock.bottom_up); +assert (memblock.current_limit ==MEMBLOCK_ALLOC_ANYWHERE ); + +return 0 ; +} + +int memblock_basic_checks (void ) +{ +memblock_initialization_check (); +return 0 ; +}
tools/testing/memblock/tests/basic_api.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _MEMBLOCK_BASIC_H +#define _MEMBLOCK_BASIC_H + +#include <assert.h> +#include "common.h" + +int memblock_basic_checks (void ); + +#endif
tools/testing/memblock/tests/common.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include "tests/common.h" +#include <string.h> + +#define INIT_MEMBLOCK_REGIONS 128 +#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS + +void reset_memblock (void ) +{ +memset (memblock.memory.regions,0 , + memblock.memory.cnt *sizeof (struct memblock_region )); +memset (memblock.reserved.regions,0 , + memblock.reserved.cnt *sizeof (struct memblock_region )); + + memblock.memory.cnt =1 ; + memblock.memory.max =INIT_MEMBLOCK_REGIONS ; + memblock.memory.name ="memory" ; + memblock.memory.total_size =0 ; + + memblock.reserved.cnt =1 ; + memblock.reserved.max =INIT_MEMBLOCK_RESERVED_REGIONS ; + memblock.reserved.name ="reserved" ; + memblock.reserved.total_size =0 ; + + memblock.bottom_up = false; + memblock.current_limit =MEMBLOCK_ALLOC_ANYWHERE ; +}
tools/testing/memblock/tests/common.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _MEMBLOCK_TEST_H +#define _MEMBLOCK_TEST_H + +#include <linux/types.h> +#include <linux/memblock.h> + +struct region { +phys_addr_t base; +phys_addr_t size; +}; + +void reset_memblock (void ); + +#endif
Signature · 1 line
2.30.2
PATCH v2 12/16 memblock tests: Add memblock_add tests
In reply to Karolina DrobnikAdd checks for adding a new region in different scenarios:
- The region does not overlap with existing entries
- The region overlaps with one of the previous entries: from the top (its end address is bigger than the base of the existing region) or from the bottom (its base address is smaller than the end address of one of the regions)
- The region is within an already defined region
- The same region is added twice to the collection of available memory regions
Add checks for memblock initialization to verify it sets memblock data structures to expected values.
--- tools/testing/memblock/tests/basic_api.c | 215 +++++++++++++++++++++++ 1 file changed, 215 insertions(+)
tools/testing/memblock/tests/basic_api.c +215 -0
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later #include <string.h> #include <linux/memblock.h> +#include <linux/sizes.h> #include "basic_api.h" #define EXPECTED_MEMBLOCK_REGIONS 128
@@ -25,8 +26,222 @@ static int memblock_initialization_check(void)return 0 ; } +/* +* A simple test that adds a memory block of a specified base address +* and size to the collection of available memory regions (memblock.memory). +* It checks if a new entry was created and if region counter and total memory +* were correctly updated. +*/ +static int memblock_add_simple_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.memory.regions[0 ]; + +struct region r = { + .base =SZ_1G , + .size =SZ_4M + }; + +reset_memblock (); +memblock_add (r.base, r.size); + +assert (rgn->base == r.base); +assert (rgn->size == r.size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == r.size); + +return 0 ; +} + +/* +* A test that tries to add two memory blocks that don't overlap with one another. +* It checks if two correctly initialized entries were added to the collection +* of available memory regions (memblock.memory) and if this change was reflected +* in memblock.memory's total size and region counter. +*/ +static int memblock_add_disjoint_check (void ) +{ +struct memblock_region *rgn1, *rgn2; + + rgn1 = &memblock.memory.regions[0 ]; + rgn2 = &memblock.memory.regions[1 ]; + +struct region r1 = { + .base =SZ_1G , + .size =SZ_8K + }; +struct region r2 = { + .base =SZ_1G +SZ_16K , + .size =SZ_8K + }; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_add (r2.base, r2.size); + +assert (rgn1->base == r1.base); +assert (rgn1->size == r1.size); + +assert (rgn2->base == r2.base); +assert (rgn2->size == r2.size); + +assert (memblock.memory.cnt ==2 ); +assert (memblock.memory.total_size == r1.size + r2.size); + +return 0 ; +} + +/* +* A test that tries to add two memory blocks, where the second one overlaps +* with the beginning of the first entry (that is r1.base < r2.base + r2.size). +* After this, it checks if two entries are merged into one region that starts +* at r2.base and has size of two regions minus their intersection. It also +* verifies the reported total size of the available memory and region counter. +*/ +static int memblock_add_overlap_top_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t total_size; + + rgn = &memblock.memory.regions[0 ]; + +struct region r1 = { + .base =SZ_512M , + .size =SZ_1G + }; +struct region r2 = { + .base =SZ_256M , + .size =SZ_512M + }; + + total_size = (r1.base - r2.base) + r1.size; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_add (r2.base, r2.size); + +assert (rgn->base == r2.base); +assert (rgn->size == total_size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == total_size); + +return 0 ; +} + +/* +* A test that tries to add two memory blocks, where the second one overlaps +* with the end of the first entry (that is r2.base < r1.base + r1.size). +* After this, it checks if two entries are merged into one region that starts +* at r1.base and has size of two regions minus their intersection. It verifies +* that memblock can still see only one entry and has a correct total size of +* the available memory. +*/ +static int memblock_add_overlap_bottom_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t total_size; + + rgn = &memblock.memory.regions[0 ]; + +struct region r1 = { + .base =SZ_128M , + .size =SZ_512M + }; +struct region r2 = { + .base =SZ_256M , + .size =SZ_1G + }; + + total_size = (r2.base - r1.base) + r2.size; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_add (r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == total_size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == total_size); + +return 0 ; +} + +/* +* A test that tries to add two memory blocks, where the second one is +* within the range of the first entry (that is r1.base < r2.base && +* r2.base + r2.size < r1.base + r1.size). It checks if two entries are merged +* into one region that stays the same. The counter and total size of available +* memory are expected to not be updated. +*/ +static int memblock_add_within_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.memory.regions[0 ]; + +struct region r1 = { + .base =SZ_8M , + .size =SZ_32M + }; +struct region r2 = { + .base =SZ_16M , + .size =SZ_1M + }; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_add (r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == r1.size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == r1.size); + +return 0 ; +} + +/* +* A simple test that tries to add the same memory block twice. The counter +* and total size of available memory are expected to not be updated. +*/ +static int memblock_add_twice_check (void ) +{ +struct region r = { + .base =SZ_16K , + .size =SZ_2M + }; + +reset_memblock (); + +memblock_add (r.base, r.size); +memblock_add (r.base, r.size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == r.size); + +return 0 ; +} + +static int memblock_add_checks (void ) +{ +memblock_add_simple_check (); +memblock_add_disjoint_check (); +memblock_add_overlap_top_check (); +memblock_add_overlap_bottom_check (); +memblock_add_within_check (); +memblock_add_twice_check (); + +return 0 ; +} +int memblock_basic_checks (void ) {memblock_initialization_check (); +memblock_add_checks ();return 0 ; }
Signature · 1 line
2.30.2
PATCH v2 13/16 memblock tests: Add memblock_reserve tests
In reply to Karolina DrobnikAdd checks for marking a region as reserved in different scenarios:
- The region does not overlap with existing entries
- The region overlaps with one of the previous entries: from the top (its end address is bigger than the base of the existing region) or from the bottom (its base address is smaller than the end address of one of the regions)
- The region is within an already defined region
- The same region is marked as reserved twice
--- tools/testing/memblock/tests/basic_api.c | 213 +++++++++++++++++++++++ 1 file changed, 213 insertions(+)
tools/testing/memblock/tests/basic_api.c +213 -0
@@ -239,9 +239,222 @@ static int memblock_add_checks(void)return 0 ; } +/* +* A simple test that marks a memory block of a specified base address +* and size as reserved and to the collection of reserved memory regions +* (memblock.reserved). It checks if a new entry was created and if region +* counter and total memory size were correctly updated. +*/ +static int memblock_reserve_simple_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r = { + .base =SZ_2G , + .size =SZ_128M + }; + +reset_memblock (); +memblock_reserve (r.base, r.size); + +assert (rgn->base == r.base); +assert (rgn->size == r.size); + +return 0 ; +} + +/* +* A test that tries to mark two memory blocks that don't overlap as reserved +* and checks if two entries were correctly added to the collection of reserved +* memory regions (memblock.reserved) and if this change was reflected in +* memblock.reserved's total size and region counter. +*/ +static int memblock_reserve_disjoint_check (void ) +{ +struct memblock_region *rgn1, *rgn2; + + rgn1 = &memblock.reserved.regions[0 ]; + rgn2 = &memblock.reserved.regions[1 ]; + +struct region r1 = { + .base =SZ_256M , + .size =SZ_16M + }; +struct region r2 = { + .base =SZ_512M , + .size =SZ_512M + }; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_reserve (r2.base, r2.size); + +assert (rgn1->base == r1.base); +assert (rgn1->size == r1.size); + +assert (rgn2->base == r2.base); +assert (rgn2->size == r2.size); + +assert (memblock.reserved.cnt ==2 ); +assert (memblock.reserved.total_size == r1.size + r2.size); + +return 0 ; +} + +/* +* A test that tries to mark two memory blocks as reserved, where the second one +* overlaps with the beginning of the first (that is r1.base < r2.base + r2.size). +* It checks if two entries are merged into one region that starts at r2.base +* and has size of two regions minus their intersection. The test also verifies +* that memblock can still see only one entry and has a correct total size of +* the reserved memory. +*/ +static int memblock_reserve_overlap_top_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t total_size; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r1 = { + .base =SZ_1G , + .size =SZ_1G + }; +struct region r2 = { + .base =SZ_128M , + .size =SZ_1G + }; + + total_size = (r1.base - r2.base) + r1.size; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_reserve (r2.base, r2.size); + +assert (rgn->base == r2.base); +assert (rgn->size == total_size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == total_size); + +return 0 ; +} + +/* +* A test that tries to mark two memory blocks as reserved, where the second +* one overlaps with the end of the first entry (that is r2.base < r1.base + r1.size). +* It checks if two entries are merged into one region that starts at r1.base +* and has size of two regions minus their intersection. It verifies that memblock +* can still see only one entry and has a correct total size of the reserved memory. +*/ +static int memblock_reserve_overlap_bottom_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t total_size; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r1 = { + .base =SZ_2K , + .size =SZ_128K + }; +struct region r2 = { + .base =SZ_128K , + .size =SZ_128K + }; + + total_size = (r2.base - r1.base) + r2.size; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_reserve (r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == total_size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == total_size); + +return 0 ; +} + +/* +* A test that tries to mark two memory blocks as reserved, where the second one +* is within the range of the first entry (that is (r1.base < r2.base) && +* (r2.base + r2.size < r1.base + r1.size)). It checks if two entries are merged +* into one region that stays the same. The counter and total size of available +* memory are expected to not be updated. +*/ +static int memblock_reserve_within_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r1 = { + .base =SZ_1M , + .size =SZ_8M + }; +struct region r2 = { + .base =SZ_2M , + .size =SZ_64K + }; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_reserve (r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == r1.size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == r1.size); + +return 0 ; +} + +/* +* A simple test that tries to reserve the same memory block twice. +* The region counter and total size of reserved memory are expected to not +* be updated. +*/ +static int memblock_reserve_twice_check (void ) +{ +struct region r = { + .base =SZ_16K , + .size =SZ_2M + }; + +reset_memblock (); + +memblock_reserve (r.base, r.size); +memblock_reserve (r.base, r.size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == r.size); + +return 0 ; +} + +static int memblock_reserve_checks (void ) +{ +memblock_reserve_simple_check (); +memblock_reserve_disjoint_check (); +memblock_reserve_overlap_top_check (); +memblock_reserve_overlap_bottom_check (); +memblock_reserve_within_check (); +memblock_reserve_twice_check (); + +return 0 ; +} +int memblock_basic_checks (void ) {memblock_initialization_check ();memblock_add_checks (); +memblock_reserve_checks (); +return 0 ; }
Signature · 1 line
2.30.2
PATCH v2 14/16 memblock tests: Add memblock_remove tests
In reply to Karolina DrobnikAdd checks for removing a region from available memory in different scenarios:
- The requested region matches one in the collection of available memory regions
- The requested region does not exist in memblock.memory
- The region overlaps with one of the entries: from the top (its end address is bigger than the base of the existing region) or from the bottom (its base address is smaller than the end address of one of the regions)
- The region is within an already defined region
--- tools/testing/memblock/tests/basic_api.c | 203 +++++++++++++++++++++++ 1 file changed, 203 insertions(+)
tools/testing/memblock/tests/basic_api.c +203 -0
@@ -450,11 +450,214 @@ static int memblock_reserve_checks(void)return 0 ; } +/* +* A simple test that tries to remove the first entry of the array of available +* memory regions. By "removing" a region we mean overwriting it with the next +* region in memblock.memory. To check this is the case, the test adds two memory +* blocks and verifies that the value of the latter was used to erase r1 region. +* It also checks if the region counter and total size were updated to expected +* values. +*/ +static int memblock_remove_simple_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.memory.regions[0 ]; + +struct region r1 = { + .base =SZ_2K , + .size =SZ_4K + }; +struct region r2 = { + .base =SZ_128K , + .size =SZ_4M + }; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_add (r2.base, r2.size); +memblock_remove (r1.base, r1.size); + +assert (rgn->base == r2.base); +assert (rgn->size == r2.size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == r2.size); + +return 0 ; +} + +/* +* A test that tries to remove a region that was not registered as available +* memory (i.e. has no corresponding entry in memblock.memory). It verifies +* that array, regions counter and total size were not modified. +*/ +static int memblock_remove_absent_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.memory.regions[0 ]; + +struct region r1 = { + .base =SZ_512K , + .size =SZ_4M + }; +struct region r2 = { + .base =SZ_64M , + .size =SZ_1G + }; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_remove (r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == r1.size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == r1.size); + +return 0 ; +} + +/* +* A test that tries to remove a region which overlaps with the beginning of +* the already existing entry r1 (that is r1.base < r2.base + r2.size). It +* checks if only the intersection of both regions is removed from the available +* memory pool. The test also checks if the regions counter and total size are +* updated to expected values. +*/ +static int memblock_remove_overlap_top_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t r1_end, r2_end, total_size; + + rgn = &memblock.memory.regions[0 ]; + +struct region r1 = { + .base =SZ_32M , + .size =SZ_32M + }; +struct region r2 = { + .base =SZ_16M , + .size =SZ_32M + }; + + r1_end = r1.base + r1.size; + r2_end = r2.base + r2.size; + total_size = r1_end - r2_end; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_remove (r2.base, r2.size); + +assert (rgn->base == r1.base + r2.base); +assert (rgn->size == total_size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == total_size); + +return 0 ; +} + +/* +* A test that tries to remove a region which overlaps with the end of the +* first entry (that is r2.base < r1.base + r1.size). It checks if only the +* intersection of both regions is removed from the available memory pool. +* The test also checks if the regions counter and total size are updated to +* expected values. +*/ +static int memblock_remove_overlap_bottom_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t total_size; + + rgn = &memblock.memory.regions[0 ]; + +struct region r1 = { + .base =SZ_2M , + .size =SZ_64M + }; +struct region r2 = { + .base =SZ_32M , + .size =SZ_256M + }; + + total_size = r2.base - r1.base; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_remove (r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == total_size); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == total_size); +return 0 ; +} + +/* +* A test that tries to remove a region which is within the range of the already +* existing entry (that is (r1.base < r2.base) && (r2.base + r2.size < r1.base + r1.size)). +* It checks if the region is split into two - one that ends at r2.base and second +* that starts at r2.base + size, with appropriate sizes. The test also checks if +* the region counter and total size were updated to expected values. +*/ +static int memblock_remove_within_check (void ) +{ +struct memblock_region *rgn1, *rgn2; +phys_addr_t r1_size, r2_size, total_size; + + rgn1 = &memblock.memory.regions[0 ]; + rgn2 = &memblock.memory.regions[1 ]; + +struct region r1 = { + .base =SZ_1M , + .size =SZ_32M + }; +struct region r2 = { + .base =SZ_16M , + .size =SZ_1M + }; + + r1_size = r2.base - r1.base; + r2_size = (r1.base + r1.size) - (r2.base + r2.size); + total_size = r1_size + r2_size; + +reset_memblock (); +memblock_add (r1.base, r1.size); +memblock_remove (r2.base, r2.size); + +assert (rgn1->base == r1.base); +assert (rgn1->size == r1_size); + +assert (rgn2->base == r2.base + r2.size); +assert (rgn2->size == r2_size); + +assert (memblock.memory.cnt ==2 ); +assert (memblock.memory.total_size == total_size); + +return 0 ; +} + +static int memblock_remove_checks (void ) +{ +memblock_remove_simple_check (); +memblock_remove_absent_check (); +memblock_remove_overlap_top_check (); +memblock_remove_overlap_bottom_check (); +memblock_remove_within_check (); + +return 0 ; +} +int memblock_basic_checks (void ) {memblock_initialization_check ();memblock_add_checks ();memblock_reserve_checks (); +memblock_remove_checks ();return 0 ; }
Signature · 1 line
2.30.2
PATCH v2 15/16 memblock tests: Add memblock_add_node test
In reply to Karolina DrobnikAdd a simple test for NUMA-aware variant of memblock_add function.
--- tools/testing/memblock/tests/basic_api.c | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
tools/testing/memblock/tests/basic_api.c +34 -0
@@ -55,6 +55,39 @@ static int memblock_add_simple_check(void)return 0 ; } +/* +* A simple test that adds a memory block of a specified base address, size +* NUMA node and memory flags to the collection of available memory regions. +* It checks if the new entry, region counter and total memory size have +* expected values. +*/ +static int memblock_add_node_simple_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.memory.regions[0 ]; + +struct region r = { + .base =SZ_1M , + .size =SZ_16M + }; + +reset_memblock (); +memblock_add_node (r.base, r.size,1 ,MEMBLOCK_HOTPLUG ); + +assert (rgn->base == r.base); +assert (rgn->size == r.size); +#ifdef CONFIG_NUMA +assert (rgn->nid ==1 ); +#endif +assert (rgn->flags ==MEMBLOCK_HOTPLUG ); + +assert (memblock.memory.cnt ==1 ); +assert (memblock.memory.total_size == r.size); + +return 0 ; +} +/* * A test that tries to add two memory blocks that don't overlap with one another. * It checks if two correctly initialized entries were added to the collection
@@ -230,6 +263,7 @@ static int memblock_add_twice_check(void)static int memblock_add_checks (void ) {memblock_add_simple_check (); +memblock_add_node_simple_check ();memblock_add_disjoint_check ();memblock_add_overlap_top_check ();memblock_add_overlap_bottom_check ();
Signature · 1 line
2.30.2
PATCH v2 16/16 memblock tests: Add memblock_free tests
In reply to Karolina DrobnikAdd checks for removing a region from reserved memory in different scenarios:
- The requested region matches one in the collection of reserved memory regions
- The requested region does not exist in memblock.reserved
- The region overlaps with one of the entries: from the top (its end address is bigger than the base of the existing region) or from the bottom (its base address is smaller than the end address of one of the regions)
- The region is within an already defined region
--- tools/testing/memblock/tests/basic_api.c | 199 +++++++++++++++++++++++ 1 file changed, 199 insertions(+)
tools/testing/memblock/tests/basic_api.c +199 -0
@@ -686,12 +686,211 @@ static int memblock_remove_checks(void)return 0 ; } +/* +* A simple test that tries to free a memory block that was marked earlier as +* reserved. By "freeing" a region we mean overwriting it with the next entry +* in memblock.reserved. To check this is the case, the test reserves two memory +* regions and verifies that the value of the latter was used to erase r1 region. +* The test also checks if the region counter and total size were updated. +*/ +static int memblock_free_simple_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r1 = { + .base =SZ_4M , + .size =SZ_1M + }; +struct region r2 = { + .base =SZ_8M , + .size =SZ_1M + }; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_reserve (r2.base, r2.size); +memblock_free ((void *)r1.base, r1.size); + +assert (rgn->base == r2.base); +assert (rgn->size == r2.size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == r2.size); + +return 0 ; +} + +/* +* A test that tries to free a region that was not marked as reserved (i.e. has +* no corresponding entry in memblock.reserved). It verifies that array, regions +* counter and total size were not modified. +*/ +static int memblock_free_absent_check (void ) +{ +struct memblock_region *rgn; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r1 = { + .base =SZ_2M , + .size =SZ_8K + }; +struct region r2 = { + .base =SZ_16M , + .size =SZ_128M + }; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_free ((void *)r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == r1.size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == r1.size); + +return 0 ; +} + +/* +* A test that tries to free a region which overlaps with the beginning of +* the already existing entry r1 (that is r1.base < r2.base + r2.size). It +* checks if only the intersection of both regions is freed. The test also +* checks if the regions counter and total size are updated to expected values. +*/ +static int memblock_free_overlap_top_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t total_size; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r1 = { + .base =SZ_8M , + .size =SZ_32M + }; +struct region r2 = { + .base =SZ_1M , + .size =SZ_8M + }; + + total_size = (r1.size + r1.base) - (r2.base + r2.size); + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_free ((void *)r2.base, r2.size); + +assert (rgn->base == r2.base + r2.size); +assert (rgn->size == total_size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == total_size); + +return 0 ; +} + +/* +* A test that tries to free a region which overlaps with the end of the +* first entry (that is r2.base < r1.base + r1.size). It checks if only the +* intersection of both regions is freed. The test also checks if the regions +* counter and total size are updated to expected values. +*/ +static int memblock_free_overlap_bottom_check (void ) +{ +struct memblock_region *rgn; +phys_addr_t total_size; + + rgn = &memblock.reserved.regions[0 ]; + +struct region r1 = { + .base =SZ_8M , + .size =SZ_32M + }; +struct region r2 = { + .base =SZ_32M , + .size =SZ_32M + }; + + total_size = r2.base - r1.base; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_free ((void *)r2.base, r2.size); + +assert (rgn->base == r1.base); +assert (rgn->size == total_size); + +assert (memblock.reserved.cnt ==1 ); +assert (memblock.reserved.total_size == total_size); + +return 0 ; +} + +/* +* A test that tries to free a region which is within the range of the already +* existing entry (that is (r1.base < r2.base) && (r2.base + r2.size < r1.base + r1.size)). +* It checks if the region is split into two - one that ends at r2.base and second +* that starts at r2.base + size, with appropriate sizes. It is expected that +* the region counter and total size fields were updated t reflect that change. +*/ +static int memblock_free_within_check (void ) +{ +struct memblock_region *rgn1, *rgn2; +phys_addr_t r1_size, r2_size, total_size; + + rgn1 = &memblock.reserved.regions[0 ]; + rgn2 = &memblock.reserved.regions[1 ]; + +struct region r1 = { + .base =SZ_1M , + .size =SZ_8M + }; +struct region r2 = { + .base =SZ_4M , + .size =SZ_1M + }; + + r1_size = r2.base - r1.base; + r2_size = (r1.base + r1.size) - (r2.base + r2.size); + total_size = r1_size + r2_size; + +reset_memblock (); +memblock_reserve (r1.base, r1.size); +memblock_free ((void *)r2.base, r2.size); + +assert (rgn1->base == r1.base); +assert (rgn1->size == r1_size); + +assert (rgn2->base == r2.base + r2.size); +assert (rgn2->size == r2_size); + +assert (memblock.reserved.cnt ==2 ); +assert (memblock.reserved.total_size == total_size); + +return 0 ; +} + +static int memblock_free_checks (void ) +{ +memblock_free_simple_check (); +memblock_free_absent_check (); +memblock_free_overlap_top_check (); +memblock_free_overlap_bottom_check (); +memblock_free_within_check (); + +return 0 ; +} +int memblock_basic_checks (void ) {memblock_initialization_check ();memblock_add_checks ();memblock_reserve_checks ();memblock_remove_checks (); +memblock_free_checks ();return 0 ; }
Signature · 1 line
2.30.2
Quoted 8 lines · Karolina Drobnik
Memblock is a boot time memory allocator, which allows managing memory regions before the actual memory management is initialized. Because it is used so early during the booting process, testing and debugging it is difficult. As memblock has few kernel dependencies, it is possible to simulate its runtime behaviour in userspace after stubbing a couple of structs and functions.
[...]
Applied, thanks!
[01/16] tools: Move gfp.h and slab.h from radix-tree to lib
commit: 5a198c3f9b0bf67ede95c1c065b8584b55f3f87a
[02/16] tools/include: Add phys_addr_t to types.h
commit: 4c12918f1086a824082a95f633f501c20ced63a4
[03/16] tools/include: Add _RET_IP_ and math definitions to kernel.h
commit: 826c23e10ee43cc2e65203403e4b606de6388a1f
[04/16] tools/include: Update atomic definitions
commit: 24af9a87ae817400feca949dd4bc94b5779e7481
[05/16] tools/include: Add mm.h file
commit: bad5c52beb213369fa218c88bf2a84e36cf099d9
[06/16] tools/include: Add cache.h stub
commit: f181e1e4009b5a4d52ac7a0870bfa4cb608da71d
[07/16] tools/include: Add io.h stub
commit: e4e9850709f98c6880e289dd6c364ef08a856dea
[08/16] tools/include: Add pfn.h stub
commit: e97362caf658718a63ff08fd557299c583b1f953
[09/16] tools/include: Add debugfs.h stub
commit: ab1f3ebd520caae193725043c70f5762f197241f
[10/16] memblock tests: Add skeleton of the memblock simulator
commit: 46ad27eef5220e3cda0fe263e8a9f785896f7aec
[11/16] memblock tests: Add memblock reset function
commit: d047055e609e23190c4d46899816dc32a6cc297b
[12/16] memblock tests: Add memblock_add tests
commit: c294f4708f0116a0e5a9695a471a0cafd05d2899
[13/16] memblock tests: Add memblock_reserve tests
commit: 7ee605ba936534c001c2b4b0b6f5f925d86fef6b
[14/16] memblock tests: Add memblock_remove tests
commit: ed7b7402f9c9f37fd29267ee3c9a6a3eca8871c0
[15/16] memblock tests: Add memblock_add_node test
commit: be95b2fb44340a00065f3153d75ab6c797c23740
[16/16] memblock tests: Add memblock_free tests
commit: 5b3408a4e5ff7609cc5b06da255b973ca0ab66d1Best regards,
Signature · 2 lines
Sincerely yours, Mike.