Skip to content

Commit f3252a2

Browse files
drobnikrppt
authored andcommitted
memblock tests: Add memblock reset function
Memblock 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. Signed-off-by: Karolina Drobnik <[email protected]> Signed-off-by: Mike Rapoport <[email protected]> Link: https://lore.kernel.org/r/8c185aa7e0dd68c2c7e937c9a06c90ae413e240f.1643796665.git.karolinadrobnik@gmail.com
1 parent 16802e5 commit f3252a2

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

tools/testing/memblock/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ CFLAGS += -I. -I../../include -Wall -O2 -fsanitize=address \
66
-fsanitize=undefined -D CONFIG_PHYS_ADDR_T_64BIT
77
LDFLAGS += -fsanitize=address -fsanitize=undefined
88
TARGETS = main
9-
OFILES = main.o memblock.o lib/slab.o mmzone.o slab.o
9+
TEST_OFILES = tests/basic_api.o tests/common.o
10+
DEP_OFILES = memblock.o lib/slab.o mmzone.o slab.o
11+
OFILES = main.o $(DEP_OFILES) $(TEST_OFILES)
1012
EXTR_SRC = ../../../mm/memblock.c
1113

1214
ifeq ($(BUILD), 32)

tools/testing/memblock/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include "tests/basic_api.h"
23

34
int main(int argc, char **argv)
45
{
6+
memblock_basic_checks();
57
return 0;
68
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include <string.h>
3+
#include <linux/memblock.h>
4+
#include "basic_api.h"
5+
6+
#define EXPECTED_MEMBLOCK_REGIONS 128
7+
8+
static int memblock_initialization_check(void)
9+
{
10+
reset_memblock();
11+
12+
assert(memblock.memory.regions);
13+
assert(memblock.memory.cnt == 1);
14+
assert(memblock.memory.max == EXPECTED_MEMBLOCK_REGIONS);
15+
assert(strcmp(memblock.memory.name, "memory") == 0);
16+
17+
assert(memblock.reserved.regions);
18+
assert(memblock.reserved.cnt == 1);
19+
assert(memblock.memory.max == EXPECTED_MEMBLOCK_REGIONS);
20+
assert(strcmp(memblock.reserved.name, "reserved") == 0);
21+
22+
assert(!memblock.bottom_up);
23+
assert(memblock.current_limit == MEMBLOCK_ALLOC_ANYWHERE);
24+
25+
return 0;
26+
}
27+
28+
int memblock_basic_checks(void)
29+
{
30+
memblock_initialization_check();
31+
return 0;
32+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
#ifndef _MEMBLOCK_BASIC_H
3+
#define _MEMBLOCK_BASIC_H
4+
5+
#include <assert.h>
6+
#include "common.h"
7+
8+
int memblock_basic_checks(void);
9+
10+
#endif

tools/testing/memblock/tests/common.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
#include "tests/common.h"
3+
#include <string.h>
4+
5+
#define INIT_MEMBLOCK_REGIONS 128
6+
#define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
7+
8+
void reset_memblock(void)
9+
{
10+
memset(memblock.memory.regions, 0,
11+
memblock.memory.cnt * sizeof(struct memblock_region));
12+
memset(memblock.reserved.regions, 0,
13+
memblock.reserved.cnt * sizeof(struct memblock_region));
14+
15+
memblock.memory.cnt = 1;
16+
memblock.memory.max = INIT_MEMBLOCK_REGIONS;
17+
memblock.memory.name = "memory";
18+
memblock.memory.total_size = 0;
19+
20+
memblock.reserved.cnt = 1;
21+
memblock.reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS;
22+
memblock.reserved.name = "reserved";
23+
memblock.reserved.total_size = 0;
24+
25+
memblock.bottom_up = false;
26+
memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
27+
}

tools/testing/memblock/tests/common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
#ifndef _MEMBLOCK_TEST_H
3+
#define _MEMBLOCK_TEST_H
4+
5+
#include <linux/types.h>
6+
#include <linux/memblock.h>
7+
8+
struct region {
9+
phys_addr_t base;
10+
phys_addr_t size;
11+
};
12+
13+
void reset_memblock(void);
14+
15+
#endif

0 commit comments

Comments
 (0)