memblock tests: Add skeleton of the memblock simulator
authorKarolina Drobnik <karolinadrobnik@gmail.com>
Wed, 2 Feb 2022 11:03:09 +0000 (12:03 +0100)
committerMike Rapoport <rppt@linux.ibm.com>
Sun, 20 Feb 2022 06:44:37 +0000 (08:44 +0200)
Add basic project files, together with local stubs of required headers.
Update tools/include/slab.h to include definitions used by memblock.

Signed-off-by: Karolina Drobnik <karolinadrobnik@gmail.com>
Signed-off-by: Mike Rapoport <rppt@kernel.org>
Link: https://lore.kernel.org/r/d296fceb023a04b316a31fbff9acf1e76ac684e4.1643796665.git.karolinadrobnik@gmail.com
16 files changed:
MAINTAINERS
tools/include/linux/slab.h
tools/testing/memblock/.gitignore [new file with mode: 0644]
tools/testing/memblock/Makefile [new file with mode: 0644]
tools/testing/memblock/asm/dma.h [new file with mode: 0644]
tools/testing/memblock/internal.h [new file with mode: 0644]
tools/testing/memblock/lib/slab.c [new file with mode: 0644]
tools/testing/memblock/linux/init.h [new file with mode: 0644]
tools/testing/memblock/linux/kernel.h [new file with mode: 0644]
tools/testing/memblock/linux/kmemleak.h [new file with mode: 0644]
tools/testing/memblock/linux/memory_hotplug.h [new file with mode: 0644]
tools/testing/memblock/linux/mmzone.h [new file with mode: 0644]
tools/testing/memblock/linux/printk.h [new file with mode: 0644]
tools/testing/memblock/main.c [new file with mode: 0644]
tools/testing/memblock/mmzone.c [new file with mode: 0644]
tools/testing/memblock/scripts/Makefile.include [new file with mode: 0644]

index fca970a46e77af110ed1d592f40567c82334b12d..38502bf0231a4abad30641f7ddbd6a108592eec2 100644 (file)
@@ -12423,6 +12423,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 <krzysztof.kozlowski@canonical.com>
index 07d7930d400315fcaf47c2a1ea88ec598d9cef49..f41d8a0eb1a4204bf312d89de69a066b7d2a0ae1 100644 (file)
 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);
diff --git a/tools/testing/memblock/.gitignore b/tools/testing/memblock/.gitignore
new file mode 100644 (file)
index 0000000..654338e
--- /dev/null
@@ -0,0 +1,4 @@
+main
+memblock.c
+linux/memblock.h
+asm/cmpxchg.h
diff --git a/tools/testing/memblock/Makefile b/tools/testing/memblock/Makefile
new file mode 100644 (file)
index 0000000..e43ed9d
--- /dev/null
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# Memblock simulator requires AddressSanitizer (libasan) and liburcu development
+# packages installed
+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
+EXTR_SRC = ../../../mm/memblock.c
+
+ifeq ($(BUILD), 32)
+       CFLAGS += -m32
+       LDFLAGS += -m32
+endif
+
+# Process user parameters
+include scripts/Makefile.include
+
+main: $(OFILES)
+
+$(OFILES): include
+
+include: ../../../include/linux/memblock.h ../../include/linux/*.h \
+       ../../include/asm/*.h
+
+       @mkdir -p linux
+       test -L linux/memblock.h || ln -s ../../../../include/linux/memblock.h linux/memblock.h
+       test -L asm/cmpxchg.h || ln -s ../../../arch/x86/include/asm/cmpxchg.h asm/cmpxchg.h
+
+memblock.c: $(EXTR_SRC)
+       test -L memblock.c || ln -s $(EXTR_SRC) memblock.c
+
+clean:
+       $(RM) $(TARGETS) $(OFILES) linux/memblock.h memblock.c asm/cmpxchg.h
+
+help:
+       @echo  'Memblock simulator'
+       @echo  ''
+       @echo  'Available targets:'
+       @echo  '  main            - Build the memblock simulator'
+       @echo  '  clean           - Remove generated files and symlinks in the directory'
+       @echo  ''
+       @echo  'Configuration:'
+       @echo  '  make NUMA=1               - simulate enabled NUMA'
+       @echo  '  make MOVABLE_NODE=1       - override `movable_node_is_enabled`'
+       @echo  '                              definition to simulate movable NUMA nodes'
+       @echo  '  make 32BIT_PHYS_ADDR_T=1  - Use 32 bit physical addresses'
+
+vpath %.c ../../lib
+
+.PHONY: clean include help
diff --git a/tools/testing/memblock/asm/dma.h b/tools/testing/memblock/asm/dma.h
new file mode 100644 (file)
index 0000000..13ff8e5
--- /dev/null
@@ -0,0 +1,5 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _TOOLS_DMA_H
+#define _TOOLS_DMA_H
+
+#endif
diff --git a/tools/testing/memblock/internal.h b/tools/testing/memblock/internal.h
new file mode 100644 (file)
index 0000000..94b52a8
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/lib/slab.c b/tools/testing/memblock/lib/slab.c
new file mode 100644 (file)
index 0000000..6be6020
--- /dev/null
@@ -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;
+}
diff --git a/tools/testing/memblock/linux/init.h b/tools/testing/memblock/linux/init.h
new file mode 100644 (file)
index 0000000..828e0ee
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/linux/kernel.h b/tools/testing/memblock/linux/kernel.h
new file mode 100644 (file)
index 0000000..d2f148b
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/linux/kmemleak.h b/tools/testing/memblock/linux/kmemleak.h
new file mode 100644 (file)
index 0000000..462f8c5
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/linux/memory_hotplug.h b/tools/testing/memblock/linux/memory_hotplug.h
new file mode 100644 (file)
index 0000000..4798876
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/linux/mmzone.h b/tools/testing/memblock/linux/mmzone.h
new file mode 100644 (file)
index 0000000..7c2eb5c
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/linux/printk.h b/tools/testing/memblock/linux/printk.h
new file mode 100644 (file)
index 0000000..61af424
--- /dev/null
@@ -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
diff --git a/tools/testing/memblock/main.c b/tools/testing/memblock/main.c
new file mode 100644 (file)
index 0000000..62958da
--- /dev/null
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+int main(int argc, char **argv)
+{
+       return 0;
+}
diff --git a/tools/testing/memblock/mmzone.c b/tools/testing/memblock/mmzone.c
new file mode 100644 (file)
index 0000000..7b0909e
--- /dev/null
@@ -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)
+{
+}
diff --git a/tools/testing/memblock/scripts/Makefile.include b/tools/testing/memblock/scripts/Makefile.include
new file mode 100644 (file)
index 0000000..699b0d6
--- /dev/null
@@ -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