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

memblock tests: update tests to check if memblock_alloc zeroed memory

Add an assert in memblock_alloc() tests where allocation is expected to
occur. The assert checks whether the entire chunk of allocated memory is
cleared.

The current memblock_alloc() tests do not check whether the allocated
memory was zeroed. memblock_alloc() should zero the allocated memory since
it is a wrapper for memblock_alloc_try_nid().
Reviewed-by: default avatarShaoqin Huang <shaoqin.huang@intel.com>
Reviewed-by: default avatarDavid Hildenbrand <david@redhat.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/83ffb941b65074f40eb14552f8bfe5b71fe50abd.1661578349.git.remckee0@gmail.com
parent 61ebea2b
...@@ -22,6 +22,8 @@ static int alloc_top_down_simple_check(void) ...@@ -22,6 +22,8 @@ static int alloc_top_down_simple_check(void)
allocated_ptr = memblock_alloc(size, SMP_CACHE_BYTES); allocated_ptr = memblock_alloc(size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, size);
ASSERT_EQ(rgn->size, size); ASSERT_EQ(rgn->size, size);
ASSERT_EQ(rgn->base, expected_start); ASSERT_EQ(rgn->base, expected_start);
...@@ -80,6 +82,8 @@ static int alloc_top_down_disjoint_check(void) ...@@ -80,6 +82,8 @@ static int alloc_top_down_disjoint_check(void)
allocated_ptr = memblock_alloc(r2_size, alignment); allocated_ptr = memblock_alloc(r2_size, alignment);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r2_size);
ASSERT_EQ(rgn1->size, r1.size); ASSERT_EQ(rgn1->size, r1.size);
ASSERT_EQ(rgn1->base, r1.base); ASSERT_EQ(rgn1->base, r1.base);
...@@ -125,6 +129,8 @@ static int alloc_top_down_before_check(void) ...@@ -125,6 +129,8 @@ static int alloc_top_down_before_check(void)
allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES); allocated_ptr = 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_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);
...@@ -173,6 +179,8 @@ static int alloc_top_down_after_check(void) ...@@ -173,6 +179,8 @@ static int alloc_top_down_after_check(void)
allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES); allocated_ptr = 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_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);
...@@ -223,6 +231,8 @@ static int alloc_top_down_second_fit_check(void) ...@@ -223,6 +231,8 @@ static int alloc_top_down_second_fit_check(void)
allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES); allocated_ptr = 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_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);
...@@ -277,6 +287,8 @@ static int alloc_in_between_generic_check(void) ...@@ -277,6 +287,8 @@ static int alloc_in_between_generic_check(void)
allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES); allocated_ptr = 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_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);
...@@ -418,6 +430,8 @@ static int alloc_limited_space_generic_check(void) ...@@ -418,6 +430,8 @@ static int alloc_limited_space_generic_check(void)
allocated_ptr = memblock_alloc(available_size, SMP_CACHE_BYTES); allocated_ptr = 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_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());
...@@ -472,6 +486,8 @@ static int alloc_bottom_up_simple_check(void) ...@@ -472,6 +486,8 @@ static int alloc_bottom_up_simple_check(void)
allocated_ptr = memblock_alloc(SZ_2, SMP_CACHE_BYTES); allocated_ptr = 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_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());
...@@ -528,6 +544,7 @@ static int alloc_bottom_up_disjoint_check(void) ...@@ -528,6 +544,7 @@ static int alloc_bottom_up_disjoint_check(void)
allocated_ptr = memblock_alloc(r2_size, alignment); allocated_ptr = memblock_alloc(r2_size, alignment);
ASSERT_NE(allocated_ptr, NULL); ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, r2_size);
ASSERT_EQ(rgn1->size, r1.size); ASSERT_EQ(rgn1->size, r1.size);
ASSERT_EQ(rgn1->base, r1.base); ASSERT_EQ(rgn1->base, r1.base);
...@@ -571,6 +588,8 @@ static int alloc_bottom_up_before_check(void) ...@@ -571,6 +588,8 @@ static int alloc_bottom_up_before_check(void)
allocated_ptr = memblock_alloc(r1_size, SMP_CACHE_BYTES); allocated_ptr = 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_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());
...@@ -618,6 +637,8 @@ static int alloc_bottom_up_after_check(void) ...@@ -618,6 +637,8 @@ static int alloc_bottom_up_after_check(void)
allocated_ptr = memblock_alloc(r2_size, SMP_CACHE_BYTES); allocated_ptr = 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_EQ(rgn->size, total_size); ASSERT_EQ(rgn->size, total_size);
ASSERT_EQ(rgn->base, r1.base); ASSERT_EQ(rgn->base, r1.base);
...@@ -669,6 +690,8 @@ static int alloc_bottom_up_second_fit_check(void) ...@@ -669,6 +690,8 @@ static int alloc_bottom_up_second_fit_check(void)
allocated_ptr = memblock_alloc(r3_size, SMP_CACHE_BYTES); allocated_ptr = 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_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);
......
...@@ -60,16 +60,23 @@ void reset_memblock_attributes(void) ...@@ -60,16 +60,23 @@ void reset_memblock_attributes(void)
memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE; memblock.current_limit = MEMBLOCK_ALLOC_ANYWHERE;
} }
static inline void fill_memblock(void)
{
memset(memory_block.base, 1, MEM_SIZE);
}
void setup_memblock(void) void setup_memblock(void)
{ {
reset_memblock_regions(); reset_memblock_regions();
memblock_add((phys_addr_t)memory_block.base, MEM_SIZE); memblock_add((phys_addr_t)memory_block.base, MEM_SIZE);
fill_memblock();
} }
void dummy_physical_memory_init(void) void dummy_physical_memory_init(void)
{ {
memory_block.base = malloc(MEM_SIZE); memory_block.base = malloc(MEM_SIZE);
assert(memory_block.base); assert(memory_block.base);
fill_memblock();
} }
void dummy_physical_memory_cleanup(void) void dummy_physical_memory_cleanup(void)
......
...@@ -51,6 +51,18 @@ ...@@ -51,6 +51,18 @@
assert((_expected) < (_seen)); \ assert((_expected) < (_seen)); \
} while (0) } while (0)
/**
* ASSERT_MEM_EQ():
* Check that the first @_size bytes of @_seen are all equal to @_expected.
* If false, print failed test message (if running with --verbose) and then
* assert.
*/
#define ASSERT_MEM_EQ(_seen, _expected, _size) do { \
for (int _i = 0; _i < (_size); _i++) { \
ASSERT_EQ(((char *)_seen)[_i], (_expected)); \
} \
} while (0)
#define PREFIX_PUSH() prefix_push(__func__) #define PREFIX_PUSH() prefix_push(__func__)
/* /*
......
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