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