memblock tests: Add memblock reset function
authorKarolina Drobnik <karolinadrobnik@gmail.com>
Wed, 2 Feb 2022 11:03:10 +0000 (12:03 +0100)
committerMike Rapoport <rppt@linux.ibm.com>
Sun, 20 Feb 2022 06:44:37 +0000 (08:44 +0200)
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 <karolinadrobnik@gmail.com>
Signed-off-by: Mike Rapoport <rppt@kernel.org>
Link: https://lore.kernel.org/r/8c185aa7e0dd68c2c7e937c9a06c90ae413e240f.1643796665.git.karolinadrobnik@gmail.com
tools/testing/memblock/Makefile
tools/testing/memblock/main.c
tools/testing/memblock/tests/basic_api.c [new file with mode: 0644]
tools/testing/memblock/tests/basic_api.h [new file with mode: 0644]
tools/testing/memblock/tests/common.c [new file with mode: 0644]
tools/testing/memblock/tests/common.h [new file with mode: 0644]

index e43ed9de9bcf3fc4313f31f866b73115ef77f0bd..29715327a2d3a0f868c99e1802f84648e92b1184 100644 (file)
@@ -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)
index 62958da35d0f8f62013165a2353876d9f73f186d..da65b0adee91e2263ea4d85a6c4c479d14eb5433 100644 (file)
@@ -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;
 }
diff --git a/tools/testing/memblock/tests/basic_api.c b/tools/testing/memblock/tests/basic_api.c
new file mode 100644 (file)
index 0000000..7f2597b
--- /dev/null
@@ -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;
+}
diff --git a/tools/testing/memblock/tests/basic_api.h b/tools/testing/memblock/tests/basic_api.h
new file mode 100644 (file)
index 0000000..1ceecfc
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/tests/common.c b/tools/testing/memblock/tests/common.c
new file mode 100644 (file)
index 0000000..03de6ea
--- /dev/null
@@ -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;
+}
diff --git a/tools/testing/memblock/tests/common.h b/tools/testing/memblock/tests/common.h
new file mode 100644 (file)
index 0000000..48efc42
--- /dev/null
@@ -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