Skip to content

Commit 284d950

Browse files
drobnikrppt
authored andcommitted
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 <[email protected]> Signed-off-by: Mike Rapoport <[email protected]> Link: https://lore.kernel.org/r/dce115503c74a6936c44694b00014658a1bb6522.1646055639.git.karolinadrobnik@gmail.com
1 parent 2c3dacb commit 284d950

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

tools/testing/memblock/tests/basic_api.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
22
#include <string.h>
33
#include <linux/memblock.h>
4-
#include <linux/sizes.h>
54
#include "basic_api.h"
65

76
#define EXPECTED_MEMBLOCK_REGIONS 128

tools/testing/memblock/tests/basic_api.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#ifndef _MEMBLOCK_BASIC_H
33
#define _MEMBLOCK_BASIC_H
44

5-
#include <assert.h>
65
#include "common.h"
76

87
int memblock_basic_checks(void);

tools/testing/memblock/tests/common.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define INIT_MEMBLOCK_REGIONS 128
66
#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
77

8+
static struct test_memory memory_block;
9+
810
void reset_memblock_regions(void)
911
{
1012
memset(memblock.memory.regions, 0,
@@ -27,3 +29,20 @@ void reset_memblock_attributes(void)
2729
memblock.bottom_up = false;
2830
memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
2931
}
32+
33+
void setup_memblock(void)
34+
{
35+
reset_memblock_regions();
36+
memblock_add((phys_addr_t)memory_block.base, MEM_SIZE);
37+
}
38+
39+
void dummy_physical_memory_init(void)
40+
{
41+
memory_block.base = malloc(MEM_SIZE);
42+
assert(memory_block.base);
43+
}
44+
45+
void dummy_physical_memory_cleanup(void)
46+
{
47+
free(memory_block.base);
48+
}

tools/testing/memblock/tests/common.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22
#ifndef _MEMBLOCK_TEST_H
33
#define _MEMBLOCK_TEST_H
44

5+
#include <stdlib.h>
6+
#include <assert.h>
57
#include <linux/types.h>
68
#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+
};
722

823
struct region {
924
phys_addr_t base;
@@ -12,5 +27,8 @@ struct region {
1227

1328
void reset_memblock_regions(void);
1429
void reset_memblock_attributes(void);
30+
void setup_memblock(void);
31+
void dummy_physical_memory_init(void);
32+
void dummy_physical_memory_cleanup(void);
1533

1634
#endif

0 commit comments

Comments
 (0)