common.h 4.1 KB

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