Commit deee033e authored by Rebecca Mckeever's avatar Rebecca Mckeever Committed by Mike Rapoport

memblock tests: update alloc_api to test memblock_alloc_raw

Update memblock_alloc() tests so that they test either memblock_alloc()
or memblock_alloc_raw() depending on the value of alloc_test_flags. Run
through all the existing tests in memblock_alloc_api twice: once for
memblock_alloc() and once for memblock_alloc_raw().

When the tests run memblock_alloc(), they test that the entire memory
region is zero. When the tests run memblock_alloc_raw(), they test that
the entire memory region is nonzero. The content of the memory region is
initialized to nonzero, and we expect it to remain unchanged if running
memblock_alloc_raw().
Reviewed-by: default avatarShaoqin Huang <shaoqin.huang@intel.com>
Signed-off-by: default avatarRebecca Mckeever <remckee0@gmail.com>
Signed-off-by: default avatarMike Rapoport <rppt@linux.ibm.com>
Link: https://lore.kernel.org/r/5a7cfb2f807ee2cb53ee77f9f5c910107b253d6e.1661578349.git.remckee0@gmail.com
parent 21a233f6
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include "alloc_api.h" #include "alloc_api.h"
static int alloc_test_flags = TEST_F_NONE;
static inline const char * const get_memblock_alloc_name(int flags)
{
if (flags & TEST_F_RAW)
return "memblock_alloc_raw";
return "memblock_alloc";
}
static inline void *run_memblock_alloc(phys_addr_t size, phys_addr_t align)
{
if (alloc_test_flags & TEST_F_RAW)
return memblock_alloc_raw(size, align);
return memblock_alloc(size, align);
}
/* /*
* A simple test that tries to allocate a small memory region. * A simple test that tries to allocate a small memory region.
* Expect to allocate an aligned region near the end of the available memory. * Expect to allocate an aligned region near the end of the available memory.
...@@ -19,10 +35,10 @@ static int alloc_top_down_simple_check(void) ...@@ -19,10 +35,10 @@ static int alloc_top_down_simple_check(void)
expected_start = memblock_end_of_DRAM() - SMP_CACHE_BYTES; expected_start = memblock_end_of_DRAM() - SMP_CACHE_BYTES;
allocated_ptr = memblock_alloc(size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, size); assert_mem_content(allocated_ptr, size, alloc_test_flags);
ASSERT_EQ(rgn->size, size); ASSERT_EQ(rgn->size, size);
ASSERT_EQ(rgn->base, expected_start); ASSERT_EQ(rgn->base, expected_start);
...@@ -79,10 +95,10 @@ static int alloc_top_down_disjoint_check(void) ...@@ -79,10 +95,10 @@ static int alloc_top_down_disjoint_check(void)
memblock_reserve(r1.base, r1.size); memblock_reserve(r1.base, r1.size);
allocated_ptr = memblock_alloc(r2_size, alignment); allocated_ptr = run_memblock_alloc(r2_size, alignment);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r2_size); assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
ASSERT_EQ(rgn1->size, r1.size); ASSERT_EQ(rgn1->size, r1.size);
ASSERT_EQ(rgn1->base, r1.base); ASSERT_EQ(rgn1->base, r1.base);
...@@ -126,10 +142,10 @@ static int alloc_top_down_before_check(void) ...@@ -126,10 +142,10 @@ static int alloc_top_down_before_check(void)
memblock_reserve(memblock_end_of_DRAM() - total_size, r1_size); memblock_reserve(memblock_end_of_DRAM() - total_size, r1_size);
allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r2_size); assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
ASSERT_EQ(rgn->size, total_size); ASSERT_EQ(rgn->size, total_size);
ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - total_size); ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - total_size);
...@@ -176,10 +192,10 @@ static int alloc_top_down_after_check(void) ...@@ -176,10 +192,10 @@ static int alloc_top_down_after_check(void)
memblock_reserve(r1.base, r1.size); memblock_reserve(r1.base, r1.size);
allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r2_size); assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
ASSERT_EQ(rgn->size, total_size); ASSERT_EQ(rgn->size, total_size);
ASSERT_EQ(rgn->base, r1.base - r2_size); ASSERT_EQ(rgn->base, r1.base - r2_size);
...@@ -228,10 +244,10 @@ static int alloc_top_down_second_fit_check(void) ...@@ -228,10 +244,10 @@ static int alloc_top_down_second_fit_check(void)
memblock_reserve(r1.base, r1.size); memblock_reserve(r1.base, r1.size);
memblock_reserve(r2.base, r2.size); memblock_reserve(r2.base, r2.size);
allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r3_size); assert_mem_content(allocated_ptr, r3_size, alloc_test_flags);
ASSERT_EQ(rgn->size, r2.size + r3_size); ASSERT_EQ(rgn->size, r2.size + r3_size);
ASSERT_EQ(rgn->base, r2.base - r3_size); ASSERT_EQ(rgn->base, r2.base - r3_size);
...@@ -284,10 +300,10 @@ static int alloc_in_between_generic_check(void) ...@@ -284,10 +300,10 @@ static int alloc_in_between_generic_check(void)
memblock_reserve(r1.base, r1.size); memblock_reserve(r1.base, r1.size);
memblock_reserve(r2.base, r2.size); memblock_reserve(r2.base, r2.size);
allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r3_size); assert_mem_content(allocated_ptr, r3_size, alloc_test_flags);
ASSERT_EQ(rgn->size, total_size); ASSERT_EQ(rgn->size, total_size);
ASSERT_EQ(rgn->base, r1.base - r2.size - r3_size); ASSERT_EQ(rgn->base, r1.base - r2.size - r3_size);
...@@ -332,7 +348,7 @@ static int alloc_small_gaps_generic_check(void) ...@@ -332,7 +348,7 @@ static int alloc_small_gaps_generic_check(void)
region_end += gap_size + region_size; region_end += gap_size + region_size;
} }
allocated_ptr = memblock_alloc(region_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(region_size, SMP_CACHE_BYTES);
ASSERT_EQ(allocated_ptr, NULL); ASSERT_EQ(allocated_ptr, NULL);
...@@ -356,7 +372,7 @@ static int alloc_all_reserved_generic_check(void) ...@@ -356,7 +372,7 @@ static int alloc_all_reserved_generic_check(void)
/* Simulate full memory */ /* Simulate full memory */
memblock_reserve(memblock_start_of_DRAM(), MEM_SIZE); memblock_reserve(memblock_start_of_DRAM(), MEM_SIZE);
allocated_ptr = memblock_alloc(SZ_256, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(SZ_256, SMP_CACHE_BYTES);
ASSERT_EQ(allocated_ptr, NULL); ASSERT_EQ(allocated_ptr, NULL);
...@@ -392,7 +408,7 @@ static int alloc_no_space_generic_check(void) ...@@ -392,7 +408,7 @@ static int alloc_no_space_generic_check(void)
/* Simulate almost-full memory */ /* Simulate almost-full memory */
memblock_reserve(memblock_start_of_DRAM(), reserved_size); memblock_reserve(memblock_start_of_DRAM(), reserved_size);
allocated_ptr = memblock_alloc(SZ_1K, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(SZ_1K, SMP_CACHE_BYTES);
ASSERT_EQ(allocated_ptr, NULL); ASSERT_EQ(allocated_ptr, NULL);
...@@ -427,10 +443,10 @@ static int alloc_limited_space_generic_check(void) ...@@ -427,10 +443,10 @@ static int alloc_limited_space_generic_check(void)
/* Simulate almost-full memory */ /* Simulate almost-full memory */
memblock_reserve(memblock_start_of_DRAM(), reserved_size); memblock_reserve(memblock_start_of_DRAM(), reserved_size);
allocated_ptr = memblock_alloc(available_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(available_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, available_size); assert_mem_content(allocated_ptr, available_size, alloc_test_flags);
ASSERT_EQ(rgn->size, MEM_SIZE); ASSERT_EQ(rgn->size, MEM_SIZE);
ASSERT_EQ(rgn->base, memblock_start_of_DRAM()); ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
...@@ -457,7 +473,7 @@ static int alloc_no_memory_generic_check(void) ...@@ -457,7 +473,7 @@ static int alloc_no_memory_generic_check(void)
reset_memblock_regions(); reset_memblock_regions();
allocated_ptr = memblock_alloc(SZ_1K, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(SZ_1K, SMP_CACHE_BYTES);
ASSERT_EQ(allocated_ptr, NULL); ASSERT_EQ(allocated_ptr, NULL);
ASSERT_EQ(rgn->size, 0); ASSERT_EQ(rgn->size, 0);
...@@ -491,7 +507,7 @@ static int alloc_too_large_generic_check(void) ...@@ -491,7 +507,7 @@ static int alloc_too_large_generic_check(void)
setup_memblock(); setup_memblock();
allocated_ptr = memblock_alloc(MEM_SIZE + SZ_2, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(MEM_SIZE + SZ_2, SMP_CACHE_BYTES);
ASSERT_EQ(allocated_ptr, NULL); ASSERT_EQ(allocated_ptr, NULL);
ASSERT_EQ(rgn->size, 0); ASSERT_EQ(rgn->size, 0);
...@@ -517,10 +533,10 @@ static int alloc_bottom_up_simple_check(void) ...@@ -517,10 +533,10 @@ static int alloc_bottom_up_simple_check(void)
setup_memblock(); setup_memblock();
allocated_ptr = memblock_alloc(SZ_2, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(SZ_2, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, SZ_2); assert_mem_content(allocated_ptr, SZ_2, alloc_test_flags);
ASSERT_EQ(rgn->size, SZ_2); ASSERT_EQ(rgn->size, SZ_2);
ASSERT_EQ(rgn->base, memblock_start_of_DRAM()); ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
...@@ -575,10 +591,10 @@ static int alloc_bottom_up_disjoint_check(void) ...@@ -575,10 +591,10 @@ static int alloc_bottom_up_disjoint_check(void)
memblock_reserve(r1.base, r1.size); memblock_reserve(r1.base, r1.size);
allocated_ptr = memblock_alloc(r2_size, alignment); allocated_ptr = run_memblock_alloc(r2_size, alignment);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r2_size); assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
ASSERT_EQ(rgn1->size, r1.size); ASSERT_EQ(rgn1->size, r1.size);
ASSERT_EQ(rgn1->base, r1.base); ASSERT_EQ(rgn1->base, r1.base);
...@@ -619,10 +635,10 @@ static int alloc_bottom_up_before_check(void) ...@@ -619,10 +635,10 @@ static int alloc_bottom_up_before_check(void)
memblock_reserve(memblock_start_of_DRAM() + r1_size, r2_size); memblock_reserve(memblock_start_of_DRAM() + r1_size, r2_size);
allocated_ptr = memblock_alloc(r1_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(r1_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r1_size); assert_mem_content(allocated_ptr, r1_size, alloc_test_flags);
ASSERT_EQ(rgn->size, total_size); ASSERT_EQ(rgn->size, total_size);
ASSERT_EQ(rgn->base, memblock_start_of_DRAM()); ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
...@@ -668,10 +684,10 @@ static int alloc_bottom_up_after_check(void) ...@@ -668,10 +684,10 @@ static int alloc_bottom_up_after_check(void)
memblock_reserve(r1.base, r1.size); memblock_reserve(r1.base, r1.size);
allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r2_size); assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
ASSERT_EQ(rgn->size, total_size); ASSERT_EQ(rgn->size, total_size);
ASSERT_EQ(rgn->base, r1.base); ASSERT_EQ(rgn->base, r1.base);
...@@ -721,10 +737,10 @@ static int alloc_bottom_up_second_fit_check(void) ...@@ -721,10 +737,10 @@ static int alloc_bottom_up_second_fit_check(void)
memblock_reserve(r1.base, r1.size); memblock_reserve(r1.base, r1.size);
memblock_reserve(r2.base, r2.size); memblock_reserve(r2.base, r2.size);
allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES); allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r3_size); assert_mem_content(allocated_ptr, r3_size, alloc_test_flags);
ASSERT_EQ(rgn->size, r2.size + r3_size); ASSERT_EQ(rgn->size, r2.size + r3_size);
ASSERT_EQ(rgn->base, r2.base); ASSERT_EQ(rgn->base, r2.base);
...@@ -856,13 +872,14 @@ static int alloc_too_large_check(void) ...@@ -856,13 +872,14 @@ static int alloc_too_large_check(void)
return 0; return 0;
} }
int memblock_alloc_checks(void) static int memblock_alloc_checks_internal(int flags)
{ {
const char *func_testing = "memblock_alloc"; const char *func = get_memblock_alloc_name(flags);
alloc_test_flags = flags;
prefix_reset(); prefix_reset();
prefix_push(func_testing); prefix_push(func);
test_print("Running %s tests...\n", func_testing); test_print("Running %s tests...\n", func);
reset_memblock_attributes(); reset_memblock_attributes();
dummy_physical_memory_init(); dummy_physical_memory_init();
...@@ -886,3 +903,11 @@ int memblock_alloc_checks(void) ...@@ -886,3 +903,11 @@ int memblock_alloc_checks(void)
return 0; return 0;
} }
int memblock_alloc_checks(void)
{
memblock_alloc_checks_internal(TEST_F_NONE);
memblock_alloc_checks_internal(TEST_F_RAW);
return 0;
}
...@@ -12,6 +12,13 @@ ...@@ -12,6 +12,13 @@
#define MEM_SIZE SZ_16K #define MEM_SIZE SZ_16K
enum test_flags {
/* No special request. */
TEST_F_NONE = 0x0,
/* Perform raw allocations (no zeroing of memory). */
TEST_F_RAW = 0x1,
};
/** /**
* ASSERT_EQ(): * ASSERT_EQ():
* Check the condition * Check the condition
...@@ -63,6 +70,18 @@ ...@@ -63,6 +70,18 @@
} \ } \
} while (0) } while (0)
/**
* ASSERT_MEM_NE():
* Check that none of the first @_size bytes of @_seen are equal to @_expected.
* If false, print failed test message (if running with --verbose) and then
* assert.
*/
#define ASSERT_MEM_NE(_seen, _expected, _size) do { \
for (int _i = 0; _i < (_size); _i++) { \
ASSERT_NE(((char *)_seen)[_i], (_expected)); \
} \
} while (0)
#define PREFIX_PUSH() prefix_push(__func__) #define PREFIX_PUSH() prefix_push(__func__)
/* /*
...@@ -116,4 +135,12 @@ static inline void run_bottom_up(int (*func)()) ...@@ -116,4 +135,12 @@ static inline void run_bottom_up(int (*func)())
prefix_pop(); prefix_pop();
} }
static inline void assert_mem_content(void *mem, int size, int flags)
{
if (flags & TEST_F_RAW)
ASSERT_MEM_NE(mem, 0, size);
else
ASSERT_MEM_EQ(mem, 0, size);
}
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment