tools/testing/memblock/tests/basic_api.c

Source file repositories/reference/linux-study-clean/tools/testing/memblock/tests/basic_api.c

File Facts

System
Linux kernel
Corpus path
tools/testing/memblock/tests/basic_api.c
Extension
.c
Size
65524 bytes
Lines
2552
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (i < skip) {
				ASSERT_EQ(memblock.reserved.cnt, i + 1);
				ASSERT_EQ(memblock.reserved.total_size, (i + 1) * MEM_SIZE);
			} else {
				ASSERT_EQ(memblock.reserved.cnt, i);
				ASSERT_EQ(memblock.reserved.total_size, i * MEM_SIZE);
			}
		}

		orig_region = memblock.reserved.regions;

		/* This reserve the 129 memory_region, and makes it double array. */
		memblock_reserve(MEMORY_BASE(skip), MEM_SIZE);

		/*
		 * This is the memory region size used by the doubled reserved.regions,
		 * and it has been reserved due to it has been used. The size is used to
		 * calculate the total_size that the memblock.reserved have now.
		 */
		new_reserved_regions_size = PAGE_ALIGN((INIT_MEMBLOCK_REGIONS * 2) *
						sizeof(struct memblock_region));
		/*
		 * The double_array() will find a free memory region as the new
		 * reserved.regions, and the used memory region will be reserved, so
		 * there will be one more region exist in the reserved memblock. And the
		 * one more reserved region's size is new_reserved_regions_size.
		 */
		ASSERT_EQ(memblock.reserved.cnt, INIT_MEMBLOCK_REGIONS + 2);
		ASSERT_EQ(memblock.reserved.total_size, (INIT_MEMBLOCK_REGIONS + 1) * MEM_SIZE +
							new_reserved_regions_size);
		ASSERT_EQ(memblock.reserved.max, INIT_MEMBLOCK_REGIONS * 2);

		/*
		 * Now memblock_double_array() works fine. Let's check after the
		 * double_array(), the memblock_reserve() still works as normal.
		 */
		memblock_reserve(r.base, r.size);
		ASSERT_EQ(memblock.reserved.regions[0].base, r.base);
		ASSERT_EQ(memblock.reserved.regions[0].size, r.size);

		ASSERT_EQ(memblock.reserved.cnt, INIT_MEMBLOCK_REGIONS + 3);
		ASSERT_EQ(memblock.reserved.total_size, (INIT_MEMBLOCK_REGIONS + 1) * MEM_SIZE +
							new_reserved_regions_size +
							r.size);
		ASSERT_EQ(memblock.reserved.max, INIT_MEMBLOCK_REGIONS * 2);

		dummy_physical_memory_cleanup();

		/*
		 * The current reserved.regions is occupying a range of memory that
		 * allocated from dummy_physical_memory_init(). After free the memory,
		 * we must not use it. So restore the origin memory region to make sure
		 * the tests can run as normal and not affected by the double array.
		 */
		memblock.reserved.regions = orig_region;
		memblock.reserved.cnt = INIT_MEMBLOCK_RESERVED_REGIONS;
	}

	test_pass_pop();

	return 0;
}

/*
 * A test that trying to reserve the 129th memory block at all locations.
 * Expect to trigger memblock_double_array() to double the
 * memblock.memory.max, find a new valid memory as reserved.regions. And make
 * sure it doesn't conflict with the range we want to reserve.
 *
 * For example, we have 128 regions in reserved and now want to reserve
 * the skipped one. Since reserved is full, memblock_double_array() would find
 * an available range in memory for the new array. We intended to put two
 * ranges in memory with one is the exact range of the skipped one. Before
 * commit 48c3b583bbdd ("mm/memblock: fix overlapping allocation when doubling
 * reserved array"), the new array would sits in the skipped range which is a
 * conflict. The expected new array should be allocated from memory.regions[0].
 *
 *           0                               1
 * memory    +-------+                       +-------+
 *           |  32K  |                       |  32K  |
 *           +-------+ ------+-------+-------+-------+
 *                   |<-32K->|<-32K->|<-32K->|
 *
 *                           0               skipped           127
 * reserved                  +-------+       .........         +-------+
 *                           |  32K  |       .  32K  .   ...   |  32K  |
 *                           +-------+-------+-------+         +-------+
 *                                   |<-32K->|
 *                                           ^
 *                                           |

Annotation

Implementation Notes