| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | #ifndef _MEMBLOCK_TEST_H |
| 3 | #define _MEMBLOCK_TEST_H |
| 4 | |
| 5 | #include <stdlib.h> |
| 6 | #include <assert.h> |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/seq_file.h> |
| 9 | #include <linux/memblock.h> |
| 10 | #include <linux/sizes.h> |
| 11 | #include <linux/printk.h> |
| 12 | #include <../selftests/kselftest.h> |
| 13 | |
| 14 | #define MEM_SIZE SZ_32K |
| 15 | #define PHYS_MEM_SIZE SZ_16M |
| 16 | #define NUMA_NODES 8 |
| 17 | |
| 18 | #define INIT_MEMBLOCK_REGIONS 128 |
| 19 | #define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS |
| 20 | |
| 21 | enum test_flags { |
| 22 | /* No special request. */ |
| 23 | TEST_F_NONE = 0x0, |
| 24 | /* Perform raw allocations (no zeroing of memory). */ |
| 25 | TEST_F_RAW = 0x1, |
| 26 | /* Perform allocations on the exact node specified. */ |
| 27 | TEST_F_EXACT = 0x2 |
| 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * ASSERT_EQ(): |
| 32 | * Check the condition |
| 33 | * @_expected == @_seen |
| 34 | * If false, print failed test message (if running with --verbose) and then |
| 35 | * assert. |
| 36 | */ |
| 37 | #define ASSERT_EQ(_expected, _seen) do { \ |
| 38 | if ((_expected) != (_seen)) \ |
| 39 | test_fail(); \ |
| 40 | assert((_expected) == (_seen)); \ |
| 41 | } while (0) |
| 42 | |
| 43 | #define ASSERT_TRUE(_seen) ASSERT_EQ(true, _seen) |
| 44 | #define ASSERT_FALSE(_seen) ASSERT_EQ(false, _seen) |
| 45 | |
| 46 | /** |
| 47 | * ASSERT_NE(): |
| 48 | * Check the condition |
| 49 | * @_expected != @_seen |
| 50 | * If false, print failed test message (if running with --verbose) and then |
| 51 | * assert. |
| 52 | */ |
| 53 | #define ASSERT_NE(_expected, _seen) do { \ |
| 54 | if ((_expected) == (_seen)) \ |
| 55 | test_fail(); \ |
| 56 | assert((_expected) != (_seen)); \ |
| 57 | } while (0) |
| 58 | |
| 59 | /** |
| 60 | * ASSERT_LT(): |
| 61 | * Check the condition |
| 62 | * @_expected < @_seen |
| 63 | * If false, print failed test message (if running with --verbose) and then |
| 64 | * assert. |
| 65 | */ |
| 66 | #define ASSERT_LT(_expected, _seen) do { \ |
| 67 | if ((_expected) >= (_seen)) \ |
| 68 | test_fail(); \ |
| 69 | assert((_expected) < (_seen)); \ |
| 70 | } while (0) |
| 71 | |
| 72 | /** |
| 73 | * ASSERT_LE(): |
| 74 | * Check the condition |
| 75 | * @_expected <= @_seen |
| 76 | * If false, print failed test message (if running with --verbose) and then |
| 77 | * assert. |
| 78 | */ |
| 79 | #define ASSERT_LE(_expected, _seen) do { \ |
| 80 | if ((_expected) > (_seen)) \ |
| 81 | test_fail(); \ |
| 82 | assert((_expected) <= (_seen)); \ |
| 83 | } while (0) |
| 84 | |
| 85 | /** |
| 86 | * ASSERT_MEM_EQ(): |
| 87 | * Check that the first @_size bytes of @_seen are all equal to @_expected. |
| 88 | * If false, print failed test message (if running with --verbose) and then |
| 89 | * assert. |
| 90 | */ |
| 91 | #define ASSERT_MEM_EQ(_seen, _expected, _size) do { \ |
| 92 | for (int _i = 0; _i < (_size); _i++) { \ |
| 93 | ASSERT_EQ(((char *)_seen)[_i], (_expected)); \ |
| 94 | } \ |
| 95 | } while (0) |
| 96 | |
| 97 | /** |
| 98 | * ASSERT_MEM_NE(): |
| 99 | * Check that none of the first @_size bytes of @_seen are equal to @_expected. |
| 100 | * If false, print failed test message (if running with --verbose) and then |
| 101 | * assert. |
| 102 | */ |
| 103 | #define ASSERT_MEM_NE(_seen, _expected, _size) do { \ |
| 104 | for (int _i = 0; _i < (_size); _i++) { \ |
| 105 | ASSERT_NE(((char *)_seen)[_i], (_expected)); \ |
| 106 | } \ |
| 107 | } while (0) |
| 108 | |
| 109 | #define PREFIX_PUSH() prefix_push(__func__) |
| 110 | |
| 111 | /* |
| 112 | * Available memory registered with memblock needs to be valid for allocs |
| 113 | * test to run. This is a convenience wrapper for memory allocated in |
| 114 | * dummy_physical_memory_init() that is later registered with memblock |
| 115 | * in setup_memblock(). |
| 116 | */ |
| 117 | struct test_memory { |
| 118 | void *base; |
| 119 | }; |
| 120 | |
| 121 | struct region { |
| 122 | phys_addr_t base; |
| 123 | phys_addr_t size; |
| 124 | }; |
| 125 | |
| 126 | static inline phys_addr_t __maybe_unused region_end(struct memblock_region *rgn) |
| 127 | { |
| 128 | return rgn->base + rgn->size; |
| 129 | } |
| 130 | |
| 131 | void reset_memblock_regions(void); |
| 132 | void reset_memblock_attributes(void); |
| 133 | void setup_memblock(void); |
| 134 | void setup_numa_memblock(const unsigned int node_fracs[]); |
| 135 | void dummy_physical_memory_init(void); |
| 136 | void dummy_physical_memory_cleanup(void); |
| 137 | phys_addr_t dummy_physical_memory_base(void); |
| 138 | void parse_args(int argc, char **argv); |
| 139 | |
| 140 | void test_fail(void); |
| 141 | void test_pass(void); |
| 142 | void test_print(const char *fmt, ...); |
| 143 | void prefix_reset(void); |
| 144 | void prefix_push(const char *prefix); |
| 145 | void prefix_pop(void); |
| 146 | |
| 147 | static inline void test_pass_pop(void) |
| 148 | { |
| 149 | test_pass(); |
| 150 | prefix_pop(); |
| 151 | } |
| 152 | |
| 153 | static inline void run_top_down(int (*func)()) |
| 154 | { |
| 155 | memblock_set_bottom_up(enable: false); |
| 156 | prefix_push(prefix: "top-down" ); |
| 157 | func(); |
| 158 | prefix_pop(); |
| 159 | } |
| 160 | |
| 161 | static inline void run_bottom_up(int (*func)()) |
| 162 | { |
| 163 | memblock_set_bottom_up(enable: true); |
| 164 | prefix_push(prefix: "bottom-up" ); |
| 165 | func(); |
| 166 | prefix_pop(); |
| 167 | } |
| 168 | |
| 169 | static inline void assert_mem_content(void *mem, int size, int flags) |
| 170 | { |
| 171 | if (flags & TEST_F_RAW) |
| 172 | ASSERT_MEM_NE(mem, 0, size); |
| 173 | else |
| 174 | ASSERT_MEM_EQ(mem, 0, size); |
| 175 | } |
| 176 | |
| 177 | #endif |
| 178 | |