2143 lines · 50.5 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later#include "basic_api.h"#include <string.h>#include <linux/memblock.h> #define EXPECTED_MEMBLOCK_REGIONS			128#define FUNC_ADD					"memblock_add"#define FUNC_RESERVE					"memblock_reserve"#define FUNC_REMOVE					"memblock_remove"#define FUNC_FREE					"memblock_free"#define FUNC_TRIM					"memblock_trim_memory" static int memblock_initialization_check(void){	PREFIX_PUSH(); 	ASSERT_NE(memblock.memory.regions, NULL);	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.max, EXPECTED_MEMBLOCK_REGIONS);	ASSERT_EQ(strcmp(memblock.memory.name, "memory"), 0); 	ASSERT_NE(memblock.reserved.regions, NULL);	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.memory.max, EXPECTED_MEMBLOCK_REGIONS);	ASSERT_EQ(strcmp(memblock.reserved.name, "reserved"), 0); 	ASSERT_EQ(memblock.bottom_up, false);	ASSERT_EQ(memblock.current_limit, MEMBLOCK_ALLOC_ANYWHERE); 	test_pass_pop(); 	return 0;} /* * A simple test that adds a memory block of a specified base address * and size to the collection of available memory regions (memblock.memory). * Expect to create a new entry. The region counter and total memory get * updated. */static int memblock_add_simple_check(void){	struct memblock_region *rgn; 	rgn = &memblock.memory.regions[0]; 	struct region r = {		.base = SZ_1G,		.size = SZ_4M	}; 	PREFIX_PUSH(); 	reset_memblock_regions();	memblock_add(r.base, r.size); 	ASSERT_EQ(rgn->base, r.base);	ASSERT_EQ(rgn->size, r.size); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, r.size); 	test_pass_pop(); 	return 0;} /* * A simple test that adds a memory block of a specified base address, size, * NUMA node and memory flags to the collection of available memory regions. * Expect to create a new entry. The region counter and total memory get * updated. */static int memblock_add_node_simple_check(void){	struct memblock_region *rgn; 	rgn = &memblock.memory.regions[0]; 	struct region r = {		.base = SZ_1M,		.size = SZ_16M	}; 	PREFIX_PUSH(); 	reset_memblock_regions();	memblock_add_node(r.base, r.size, 1, MEMBLOCK_HOTPLUG); 	ASSERT_EQ(rgn->base, r.base);	ASSERT_EQ(rgn->size, r.size);#ifdef CONFIG_NUMA	ASSERT_EQ(rgn->nid, 1);#endif	ASSERT_EQ(rgn->flags, MEMBLOCK_HOTPLUG); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, r.size); 	test_pass_pop(); 	return 0;} /* * A test that tries to add two memory blocks that don't overlap with one * another: * *  |        +--------+        +--------+  | *  |        |   r1   |        |   r2   |  | *  +--------+--------+--------+--------+--+ * * Expect to add two correctly initialized entries to the collection of * available memory regions (memblock.memory). The total size and * region counter fields get updated. */static int memblock_add_disjoint_check(void){	struct memblock_region *rgn1, *rgn2; 	rgn1 = &memblock.memory.regions[0];	rgn2 = &memblock.memory.regions[1]; 	struct region r1 = {		.base = SZ_1G,		.size = SZ_8K	};	struct region r2 = {		.base = SZ_1G + SZ_16K,		.size = SZ_8K	}; 	PREFIX_PUSH(); 	reset_memblock_regions();	memblock_add(r1.base, r1.size);	memblock_add(r2.base, r2.size); 	ASSERT_EQ(rgn1->base, r1.base);	ASSERT_EQ(rgn1->size, r1.size); 	ASSERT_EQ(rgn2->base, r2.base);	ASSERT_EQ(rgn2->size, r2.size); 	ASSERT_EQ(memblock.memory.cnt, 2);	ASSERT_EQ(memblock.memory.total_size, r1.size + r2.size); 	test_pass_pop(); 	return 0;} /* * A test that tries to add two memory blocks r1 and r2, where r2 overlaps * with the beginning of r1 (that is r1.base < r2.base + r2.size): * *  |    +----+----+------------+          | *  |    |    |r2  |   r1       |          | *  +----+----+----+------------+----------+ *       ^    ^ *       |    | *       |    r1.base *       | *       r2.base * * Expect to merge the two entries into one region that starts at r2.base * and has size of two regions minus their intersection. The total size of * the available memory is updated, and the region counter stays the same. */static int memblock_add_overlap_top_check(void){	struct memblock_region *rgn;	phys_addr_t total_size; 	rgn = &memblock.memory.regions[0]; 	struct region r1 = {		.base = SZ_512M,		.size = SZ_1G	};	struct region r2 = {		.base = SZ_256M,		.size = SZ_512M	}; 	PREFIX_PUSH(); 	total_size = (r1.base - r2.base) + r1.size; 	reset_memblock_regions();	memblock_add(r1.base, r1.size);	memblock_add(r2.base, r2.size); 	ASSERT_EQ(rgn->base, r2.base);	ASSERT_EQ(rgn->size, total_size); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, total_size); 	test_pass_pop(); 	return 0;} /* * A test that tries to add two memory blocks r1 and r2, where r2 overlaps * with the end of r1 (that is r2.base < r1.base + r1.size): * *  |  +--+------+----------+              | *  |  |  | r1   | r2       |              | *  +--+--+------+----------+--------------+ *     ^  ^ *     |  | *     |  r2.base *     | *     r1.base * * Expect to merge the two entries into one region that starts at r1.base * and has size of two regions minus their intersection. The total size of * the available memory is updated, and the region counter stays the same. */static int memblock_add_overlap_bottom_check(void){	struct memblock_region *rgn;	phys_addr_t total_size; 	rgn = &memblock.memory.regions[0]; 	struct region r1 = {		.base = SZ_128M,		.size = SZ_512M	};	struct region r2 = {		.base = SZ_256M,		.size = SZ_1G	}; 	PREFIX_PUSH(); 	total_size = (r2.base - r1.base) + r2.size; 	reset_memblock_regions();	memblock_add(r1.base, r1.size);	memblock_add(r2.base, r2.size); 	ASSERT_EQ(rgn->base, r1.base);	ASSERT_EQ(rgn->size, total_size); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, total_size); 	test_pass_pop(); 	return 0;} /* * A test that tries to add two memory blocks r1 and r2, where r2 is * within the range of r1 (that is r1.base < r2.base && * r2.base + r2.size < r1.base + r1.size): * *  |   +-------+--+-----------------------+ *  |   |       |r2|      r1               | *  +---+-------+--+-----------------------+ *      ^ *      | *      r1.base * * Expect to merge two entries into one region that stays the same. * The counter and total size of available memory are not updated. */static int memblock_add_within_check(void){	struct memblock_region *rgn; 	rgn = &memblock.memory.regions[0]; 	struct region r1 = {		.base = SZ_8M,		.size = SZ_32M	};	struct region r2 = {		.base = SZ_16M,		.size = SZ_1M	}; 	PREFIX_PUSH(); 	reset_memblock_regions();	memblock_add(r1.base, r1.size);	memblock_add(r2.base, r2.size); 	ASSERT_EQ(rgn->base, r1.base);	ASSERT_EQ(rgn->size, r1.size); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, r1.size); 	test_pass_pop(); 	return 0;} /* * A simple test that tries to add the same memory block twice. Expect * the counter and total size of available memory to not be updated. */static int memblock_add_twice_check(void){	struct region r = {		.base = SZ_16K,		.size = SZ_2M	}; 	PREFIX_PUSH(); 	reset_memblock_regions(); 	memblock_add(r.base, r.size);	memblock_add(r.base, r.size); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, r.size); 	test_pass_pop(); 	return 0;} /* * A test that tries to add two memory blocks that don't overlap with one * another and then add a third memory block in the space between the first two: * *  |        +--------+--------+--------+  | *  |        |   r1   |   r3   |   r2   |  | *  +--------+--------+--------+--------+--+ * * Expect to merge the three entries into one region that starts at r1.base * and has size of r1.size + r2.size + r3.size. The region counter and total * size of the available memory are updated. */static int memblock_add_between_check(void){	struct memblock_region *rgn;	phys_addr_t total_size; 	rgn = &memblock.memory.regions[0]; 	struct region r1 = {		.base = SZ_1G,		.size = SZ_8K	};	struct region r2 = {		.base = SZ_1G + SZ_16K,		.size = SZ_8K	};	struct region r3 = {		.base = SZ_1G + SZ_8K,		.size = SZ_8K	}; 	PREFIX_PUSH(); 	total_size = r1.size + r2.size + r3.size; 	reset_memblock_regions();	memblock_add(r1.base, r1.size);	memblock_add(r2.base, r2.size);	memblock_add(r3.base, r3.size); 	ASSERT_EQ(rgn->base, r1.base);	ASSERT_EQ(rgn->size, total_size); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, total_size); 	test_pass_pop(); 	return 0;} /* * A simple test that tries to add a memory block r when r extends past * PHYS_ADDR_MAX: * *                               +--------+ *                               |    r   | *                               +--------+ *  |                            +----+ *  |                            | rgn| *  +----------------------------+----+ * * Expect to add a memory block of size PHYS_ADDR_MAX - r.base. Expect the * total size of available memory and the counter to be updated. */static int memblock_add_near_max_check(void){	struct memblock_region *rgn;	phys_addr_t total_size; 	rgn = &memblock.memory.regions[0]; 	struct region r = {		.base = PHYS_ADDR_MAX - SZ_1M,		.size = SZ_2M	}; 	PREFIX_PUSH(); 	total_size = PHYS_ADDR_MAX - r.base; 	reset_memblock_regions();	memblock_add(r.base, r.size); 	ASSERT_EQ(rgn->base, r.base);	ASSERT_EQ(rgn->size, total_size); 	ASSERT_EQ(memblock.memory.cnt, 1);	ASSERT_EQ(memblock.memory.total_size, total_size); 	test_pass_pop(); 	return 0;} /* * A test that trying to add the 129th memory block. * Expect to trigger memblock_double_array() to double the * memblock.memory.max, find a new valid memory as * memory.regions. */static int memblock_add_many_check(void){	int i;	void *orig_region;	struct region r = {		.base = SZ_16K,		.size = SZ_16K,	};	phys_addr_t new_memory_regions_size;	phys_addr_t base, size = SZ_64;	phys_addr_t gap_size = SZ_64; 	PREFIX_PUSH(); 	reset_memblock_regions();	memblock_allow_resize(); 	dummy_physical_memory_init();	/*	 * We allocated enough memory by using dummy_physical_memory_init(), and	 * split it into small block. First we split a large enough memory block	 * as the memory region which will be choosed by memblock_double_array().	 */	base = PAGE_ALIGN(dummy_physical_memory_base());	new_memory_regions_size = PAGE_ALIGN(INIT_MEMBLOCK_REGIONS * 2 *					     sizeof(struct memblock_region));	memblock_add(base, new_memory_regions_size); 	/* This is the base of small memory block. */	base += new_memory_regions_size + gap_size; 	orig_region = memblock.memory.regions; 	for (i = 0; i < INIT_MEMBLOCK_REGIONS; i++) {		/*		 * Add these small block to fulfill the memblock. We keep a		 * gap between the nearby memory to avoid being merged.		 */		memblock_add(base, size);		base += size + gap_size; 		ASSERT_EQ(memblock.memory.cnt, i + 2);		ASSERT_EQ(memblock.memory.total_size, new_memory_regions_size +						      (i + 1) * size);	} 	/*	 * At there, memblock_double_array() has been succeed, check if it	 * update the memory.max.	 */	ASSERT_EQ(memblock.memory.max, INIT_MEMBLOCK_REGIONS * 2); 	/* memblock_double_array() will reserve the memory it used. Check it. */	ASSERT_EQ(memblock.reserved.cnt, 1);	ASSERT_EQ(memblock.reserved.total_size, new_memory_regions_size); 	/*	 * Now memblock_double_array() works fine. Let's check after the	 * double_array(), the memblock_add() still works as normal.	 */	memblock_add(r.base, r.size);	ASSERT_EQ(memblock.memory.regions[0].base, r.base);	ASSERT_EQ(memblock.memory.regions[0].size, r.size); 	ASSERT_EQ(memblock.memory.cnt, INIT_MEMBLOCK_REGIONS + 2);	ASSERT_EQ(memblock.memory.total_size, INIT_MEMBLOCK_REGIONS * size +					      new_memory_regions_size +					      r.size);	ASSERT_EQ(memblock.memory.max, INIT_MEMBLOCK_REGIONS * 2);

Lines 1–500 of 2143.