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