Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

memblock tests: Add simulation of physical memory

Allocation functions that return virtual addresses (with an exception
of _raw variant) clear the allocated memory after reserving it. This
requires valid memory ranges in memblock.memory.

Introduce memory_block variable to store memory that can be registered
with memblock data structure. Move assert.h and size.h includes to common.h
to share them between the test files.

Signed-off-by: Karolina Drobnik <karolinadrobnik@gmail.com>
Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
Link: https://lore.kernel.org/r/dce115503c74a6936c44694b00014658a1bb6522.1646055639.git.karolinadrobnik@gmail.com

authored by

Karolina Drobnik and committed by
Mike Rapoport
284d950d 2c3dacba

+37 -2
-1
tools/testing/memblock/tests/basic_api.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 #include <string.h> 3 3 #include <linux/memblock.h> 4 - #include <linux/sizes.h> 5 4 #include "basic_api.h" 6 5 7 6 #define EXPECTED_MEMBLOCK_REGIONS 128
-1
tools/testing/memblock/tests/basic_api.h
··· 2 2 #ifndef _MEMBLOCK_BASIC_H 3 3 #define _MEMBLOCK_BASIC_H 4 4 5 - #include <assert.h> 6 5 #include "common.h" 7 6 8 7 int memblock_basic_checks(void);
+19
tools/testing/memblock/tests/common.c
··· 5 5 #define INIT_MEMBLOCK_REGIONS 128 6 6 #define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS 7 7 8 + static struct test_memory memory_block; 9 + 8 10 void reset_memblock_regions(void) 9 11 { 10 12 memset(memblock.memory.regions, 0, ··· 28 26 memblock.reserved.name = "reserved"; 29 27 memblock.bottom_up = false; 30 28 memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; 29 + } 30 + 31 + void setup_memblock(void) 32 + { 33 + reset_memblock_regions(); 34 + memblock_add((phys_addr_t)memory_block.base, MEM_SIZE); 35 + } 36 + 37 + void dummy_physical_memory_init(void) 38 + { 39 + memory_block.base = malloc(MEM_SIZE); 40 + assert(memory_block.base); 41 + } 42 + 43 + void dummy_physical_memory_cleanup(void) 44 + { 45 + free(memory_block.base); 31 46 }
+18
tools/testing/memblock/tests/common.h
··· 2 2 #ifndef _MEMBLOCK_TEST_H 3 3 #define _MEMBLOCK_TEST_H 4 4 5 + #include <stdlib.h> 6 + #include <assert.h> 5 7 #include <linux/types.h> 6 8 #include <linux/memblock.h> 9 + #include <linux/sizes.h> 10 + 11 + #define MEM_SIZE SZ_16K 12 + 13 + /* 14 + * Available memory registered with memblock needs to be valid for allocs 15 + * test to run. This is a convenience wrapper for memory allocated in 16 + * dummy_physical_memory_init() that is later registered with memblock 17 + * in setup_memblock(). 18 + */ 19 + struct test_memory { 20 + void *base; 21 + }; 7 22 8 23 struct region { 9 24 phys_addr_t base; ··· 27 12 28 13 void reset_memblock_regions(void); 29 14 void reset_memblock_attributes(void); 15 + void setup_memblock(void); 16 + void dummy_physical_memory_init(void); 17 + void dummy_physical_memory_cleanup(void); 30 18 31 19 #endif