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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
basic_api.hstring.hlinux/memblock.h
Detected Declarations
function memblock_initialization_checkfunction regionsfunction memblock_add_node_simple_checkfunction regionsfunction r1function r1function r1function memblock_add_twice_checkfunction memblock_add_between_checkfunction memblock_add_near_max_checkfunction memblock_double_arrayfunction memblock_add_checksfunction memblock_reserve_simple_checkfunction memblock_reserve_disjoint_checkfunction r1function r1function r1function memblock_reserve_twice_checkfunction memblock_reserve_between_checkfunction memblock_reserve_near_max_checkfunction memblock_double_arrayfunction memblock_double_arrayfunction memblock_double_arrayfunction memblock_reserve_checksfunction memblock_remove_simple_checkfunction memoryfunction memblock_remove_overlap_top_checkfunction r1function r1function memblock_remove_only_region_checkfunction memblock_remove_near_max_checkfunction memblock_remove_overlap_two_checkfunction memblock_remove_checksfunction memblock_free_simple_checkfunction memblock_free_absent_checkfunction r1function r1function r1function memblock_free_only_region_checkfunction memblock_free_near_max_checkfunction memblock_free_overlap_two_checkfunction memblock_free_checksfunction memblock_set_bottom_up_checkfunction memblock_bottom_up_checkfunction memblock_bottom_up_checksfunction memblock_trim_memory_aligned_checkfunction memblock_trim_memory_too_small_checkfunction memblock_trim_memory_unaligned_base_check
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
- Immediate include surface: `basic_api.h`, `string.h`, `linux/memblock.h`.
- Detected declarations: `function memblock_initialization_check`, `function regions`, `function memblock_add_node_simple_check`, `function regions`, `function r1`, `function r1`, `function r1`, `function memblock_add_twice_check`, `function memblock_add_between_check`, `function memblock_add_near_max_check`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.