2733 lines · 79.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later#include "alloc_nid_api.h" static int alloc_nid_test_flags = TEST_F_NONE; /* * contains the fraction of MEM_SIZE contained in each node in basis point * units (one hundredth of 1% or 1/10000) */static const unsigned int node_fractions[] = {	2500, /* 1/4  */	 625, /* 1/16 */	1250, /* 1/8  */	1250, /* 1/8  */	 625, /* 1/16 */	 625, /* 1/16 */	2500, /* 1/4  */	 625, /* 1/16 */}; static inline const char * const get_memblock_alloc_nid_name(int flags){	if (flags & TEST_F_EXACT)		return "memblock_alloc_exact_nid_raw";	if (flags & TEST_F_RAW)		return "memblock_alloc_try_nid_raw";	return "memblock_alloc_try_nid";} static inline void *run_memblock_alloc_nid(phys_addr_t size,					   phys_addr_t align,					   phys_addr_t min_addr,					   phys_addr_t max_addr, int nid){	assert(!(alloc_nid_test_flags & TEST_F_EXACT) ||	       (alloc_nid_test_flags & TEST_F_RAW));	/*	 * TEST_F_EXACT should be checked before TEST_F_RAW since	 * memblock_alloc_exact_nid_raw() performs raw allocations.	 */	if (alloc_nid_test_flags & TEST_F_EXACT)		return memblock_alloc_exact_nid_raw(size, align, min_addr,						    max_addr, nid);	if (alloc_nid_test_flags & TEST_F_RAW)		return memblock_alloc_try_nid_raw(size, align, min_addr,						  max_addr, nid);	return memblock_alloc_try_nid(size, align, min_addr, max_addr, nid);} /* * A simple test that tries to allocate a memory region within min_addr and * max_addr range: * *        +                   + *   |    +       +-----------+      | *   |    |       |    rgn    |      | *   +----+-------+-----------+------+ *        ^                   ^ *        |                   | *        min_addr           max_addr * * Expect to allocate a region that ends at max_addr. */static int alloc_nid_top_down_simple_check(void){	struct memblock_region *rgn = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	phys_addr_t size = SZ_128;	phys_addr_t min_addr;	phys_addr_t max_addr;	phys_addr_t rgn_end; 	PREFIX_PUSH();	setup_memblock(); 	min_addr = memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2;	max_addr = min_addr + SZ_512; 	allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE);	rgn_end = rgn->base + rgn->size; 	ASSERT_NE(allocated_ptr, NULL);	assert_mem_content(allocated_ptr, size, alloc_nid_test_flags); 	ASSERT_EQ(rgn->size, size);	ASSERT_EQ(rgn->base, max_addr - size);	ASSERT_EQ(rgn_end, max_addr); 	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.reserved.total_size, size); 	test_pass_pop(); 	return 0;} /* * A simple test that tries to allocate a memory region within min_addr and * max_addr range, where the end address is misaligned: * *         +       +            + *  |      +       +---------+  +    | *  |      |       |   rgn   |  |    | *  +------+-------+---------+--+----+ *         ^       ^            ^ *         |       |            | *       min_add   |            max_addr *                 | *                 Aligned address *                 boundary * * Expect to allocate an aligned region that ends before max_addr. */static int alloc_nid_top_down_end_misaligned_check(void){	struct memblock_region *rgn = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	phys_addr_t size = SZ_128;	phys_addr_t misalign = SZ_2;	phys_addr_t min_addr;	phys_addr_t max_addr;	phys_addr_t rgn_end; 	PREFIX_PUSH();	setup_memblock(); 	min_addr = memblock_start_of_DRAM() + SMP_CACHE_BYTES * 2;	max_addr = min_addr + SZ_512 + misalign; 	allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE);	rgn_end = rgn->base + rgn->size; 	ASSERT_NE(allocated_ptr, NULL);	assert_mem_content(allocated_ptr, size, alloc_nid_test_flags); 	ASSERT_EQ(rgn->size, size);	ASSERT_EQ(rgn->base, max_addr - size - misalign);	ASSERT_LT(rgn_end, max_addr); 	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.reserved.total_size, size); 	test_pass_pop(); 	return 0;} /* * A simple test that tries to allocate a memory region, which spans over the * min_addr and max_addr range: * *         +               + *  |      +---------------+       | *  |      |      rgn      |       | *  +------+---------------+-------+ *         ^               ^ *         |               | *         min_addr        max_addr * * Expect to allocate a region that starts at min_addr and ends at * max_addr, given that min_addr is aligned. */static int alloc_nid_exact_address_generic_check(void){	struct memblock_region *rgn = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	phys_addr_t size = SZ_1K;	phys_addr_t min_addr;	phys_addr_t max_addr;	phys_addr_t rgn_end; 	PREFIX_PUSH();	setup_memblock(); 	min_addr = memblock_start_of_DRAM() + SMP_CACHE_BYTES;	max_addr = min_addr + size; 	allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE);	rgn_end = rgn->base + rgn->size; 	ASSERT_NE(allocated_ptr, NULL);	assert_mem_content(allocated_ptr, size, alloc_nid_test_flags); 	ASSERT_EQ(rgn->size, size);	ASSERT_EQ(rgn->base, min_addr);	ASSERT_EQ(rgn_end, max_addr); 	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.reserved.total_size, size); 	test_pass_pop(); 	return 0;} /* * A test that tries to allocate a memory region, which can't fit into * min_addr and max_addr range: * *           +          +     + *  |        +----------+-----+    | *  |        |   rgn    +     |    | *  +--------+----------+-----+----+ *           ^          ^     ^ *           |          |     | *           Aligned    |    max_addr *           address    | *           boundary   min_add * * Expect to drop the lower limit and allocate a memory region which * ends at max_addr (if the address is aligned). */static int alloc_nid_top_down_narrow_range_check(void){	struct memblock_region *rgn = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	phys_addr_t size = SZ_256;	phys_addr_t min_addr;	phys_addr_t max_addr; 	PREFIX_PUSH();	setup_memblock(); 	min_addr = memblock_start_of_DRAM() + SZ_512;	max_addr = min_addr + SMP_CACHE_BYTES; 	allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE); 	ASSERT_NE(allocated_ptr, NULL);	assert_mem_content(allocated_ptr, size, alloc_nid_test_flags); 	ASSERT_EQ(rgn->size, size);	ASSERT_EQ(rgn->base, max_addr - size); 	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.reserved.total_size, size); 	test_pass_pop(); 	return 0;} /* * A test that tries to allocate a memory region, which can't fit into * min_addr and max_addr range, with the latter being too close to the beginning * of the available memory: * *   +-------------+ *   |     new     | *   +-------------+ *         +       + *         |       +              | *         |       |              | *         +-------+--------------+ *         ^       ^ *         |       | *         |       max_addr *         | *         min_addr * * Expect no allocation to happen. */static int alloc_nid_low_max_generic_check(void){	void *allocated_ptr = NULL;	phys_addr_t size = SZ_1K;	phys_addr_t min_addr;	phys_addr_t max_addr; 	PREFIX_PUSH();	setup_memblock(); 	min_addr = memblock_start_of_DRAM();	max_addr = min_addr + SMP_CACHE_BYTES; 	allocated_ptr = run_memblock_alloc_nid(size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE); 	ASSERT_EQ(allocated_ptr, NULL); 	test_pass_pop(); 	return 0;} /* * A test that tries to allocate a memory region within min_addr min_addr range, * with min_addr being so close that it's next to an allocated region: * *          +                        + *  |       +--------+---------------| *  |       |   r1   |      rgn      | *  +-------+--------+---------------+ *          ^                        ^ *          |                        | *          min_addr                 max_addr * * Expect a merge of both regions. Only the region size gets updated. */static int alloc_nid_min_reserved_generic_check(void){	struct memblock_region *rgn = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	phys_addr_t r1_size = SZ_128;	phys_addr_t r2_size = SZ_64;	phys_addr_t total_size = r1_size + r2_size;	phys_addr_t min_addr;	phys_addr_t max_addr;	phys_addr_t reserved_base; 	PREFIX_PUSH();	setup_memblock(); 	max_addr = memblock_end_of_DRAM();	min_addr = max_addr - r2_size;	reserved_base = min_addr - r1_size; 	memblock_reserve_kern(reserved_base, r1_size); 	allocated_ptr = run_memblock_alloc_nid(r2_size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE); 	ASSERT_NE(allocated_ptr, NULL);	assert_mem_content(allocated_ptr, r2_size, alloc_nid_test_flags); 	ASSERT_EQ(rgn->size, total_size);	ASSERT_EQ(rgn->base, reserved_base); 	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.reserved.total_size, total_size); 	test_pass_pop(); 	return 0;} /* * A test that tries to allocate a memory region within min_addr and max_addr, * with max_addr being so close that it's next to an allocated region: * *             +             + *  |          +-------------+--------| *  |          |     rgn     |   r1   | *  +----------+-------------+--------+ *             ^             ^ *             |             | *             min_addr      max_addr * * Expect a merge of regions. Only the region size gets updated. */static int alloc_nid_max_reserved_generic_check(void){	struct memblock_region *rgn = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	phys_addr_t r1_size = SZ_64;	phys_addr_t r2_size = SZ_128;	phys_addr_t total_size = r1_size + r2_size;	phys_addr_t min_addr;	phys_addr_t max_addr; 	PREFIX_PUSH();	setup_memblock(); 	max_addr = memblock_end_of_DRAM() - r1_size;	min_addr = max_addr - r2_size; 	memblock_reserve_kern(max_addr, r1_size); 	allocated_ptr = run_memblock_alloc_nid(r2_size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE); 	ASSERT_NE(allocated_ptr, NULL);	assert_mem_content(allocated_ptr, r2_size, alloc_nid_test_flags); 	ASSERT_EQ(rgn->size, total_size);	ASSERT_EQ(rgn->base, min_addr); 	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.reserved.total_size, total_size); 	test_pass_pop(); 	return 0;} /* * A test that tries to allocate memory within min_addr and max_add range, when * there are two reserved regions at the borders, with a gap big enough to fit * a new region: * *                +           + *  |    +--------+   +-------+------+  | *  |    |   r2   |   |  rgn  |  r1  |  | *  +----+--------+---+-------+------+--+ *                ^           ^ *                |           | *                min_addr    max_addr * * Expect to merge the new region with r1. The second region does not get * updated. The total size field gets updated. */ static int alloc_nid_top_down_reserved_with_space_check(void){	struct memblock_region *rgn1 = &memblock.reserved.regions[1];	struct memblock_region *rgn2 = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	struct region r1, r2;	phys_addr_t r3_size = SZ_64;	phys_addr_t gap_size = SMP_CACHE_BYTES;	phys_addr_t total_size;	phys_addr_t max_addr;	phys_addr_t min_addr; 	PREFIX_PUSH();	setup_memblock(); 	r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;	r1.size = SMP_CACHE_BYTES; 	r2.size = SZ_128;	r2.base = r1.base - (r3_size + gap_size + r2.size); 	total_size = r1.size + r2.size + r3_size;	min_addr = r2.base + r2.size;	max_addr = r1.base; 	memblock_reserve_kern(r1.base, r1.size);	memblock_reserve_kern(r2.base, r2.size); 	allocated_ptr = run_memblock_alloc_nid(r3_size, SMP_CACHE_BYTES,					       min_addr, max_addr,					       NUMA_NO_NODE); 	ASSERT_NE(allocated_ptr, NULL);	assert_mem_content(allocated_ptr, r3_size, alloc_nid_test_flags); 	ASSERT_EQ(rgn1->size, r1.size + r3_size);	ASSERT_EQ(rgn1->base, max_addr - r3_size); 	ASSERT_EQ(rgn2->size, r2.size);	ASSERT_EQ(rgn2->base, r2.base); 	ASSERT_EQ(memblock.reserved.cnt, 2);	ASSERT_EQ(memblock.reserved.total_size, total_size); 	test_pass_pop(); 	return 0;} /* * A test that tries to allocate memory within min_addr and max_add range, when * there are two reserved regions at the borders, with a gap of a size equal to * the size of the new region: * *                 +        + *  |     +--------+--------+--------+     | *  |     |   r2   |   r3   |   r1   |     | *  +-----+--------+--------+--------+-----+ *                 ^        ^ *                 |        | *                 min_addr max_addr * * Expect to merge all of the regions into one. The region counter and total * size fields get updated. */static int alloc_nid_reserved_full_merge_generic_check(void){	struct memblock_region *rgn = &memblock.reserved.regions[0];	void *allocated_ptr = NULL;	struct region r1, r2;	phys_addr_t r3_size = SZ_64;	phys_addr_t total_size;	phys_addr_t max_addr;	phys_addr_t min_addr; 	PREFIX_PUSH();	setup_memblock(); 	r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES * 2;	r1.size = SMP_CACHE_BYTES; 	r2.size = SZ_128;	r2.base = r1.base - (r3_size + r2.size); 	total_size = r1.size + r2.size + r3_size;	min_addr = r2.base + r2.size;	max_addr = r1.base;

Lines 1–500 of 2733.