tools/testing/selftests/mm/hugetlb-madvise.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/hugetlb-madvise.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/hugetlb-madvise.c- Extension
.c- Size
- 9760 bytes
- Lines
- 361
- 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.
Dependency Surface
stdlib.hstdio.hunistd.hsys/mman.hfcntl.hvm_util.hkselftest.h
Detected Declarations
function write_fault_pagesfunction read_fault_pagesfunction mainfunction munmap
Annotated Snippet
if (fhp != (exp_free)) { \
printf("Unexpected number of free huge " \
"pages line %d\n", __LINE__); \
exit(1); \
} \
} while (0)
unsigned long huge_page_size;
unsigned long base_page_size;
void write_fault_pages(void *addr, unsigned long nr_pages)
{
unsigned long i;
for (i = 0; i < nr_pages; i++)
*((unsigned long *)(addr + (i * huge_page_size))) = i;
}
void read_fault_pages(void *addr, unsigned long nr_pages)
{
force_read_pages(addr, nr_pages, huge_page_size);
}
int main(int argc, char **argv)
{
unsigned long free_hugepages;
void *addr, *addr2;
int fd;
int ret;
huge_page_size = default_huge_page_size();
if (!huge_page_size) {
printf("Unable to determine huge page size, exiting!\n");
exit(1);
}
base_page_size = sysconf(_SC_PAGE_SIZE);
if (!huge_page_size) {
printf("Unable to determine base page size, exiting!\n");
exit(1);
}
free_hugepages = get_free_hugepages();
if (free_hugepages < MIN_FREE_PAGES) {
printf("Not enough free huge pages to test, exiting!\n");
exit(KSFT_SKIP);
}
fd = memfd_create(argv[0], MFD_HUGETLB);
if (fd < 0) {
perror("memfd_create() failed");
exit(1);
}
/*
* Test validity of MADV_DONTNEED addr and length arguments. mmap
* size is NR_HUGE_PAGES + 2. One page at the beginning and end of
* the mapping will be unmapped so we KNOW there is nothing mapped
* there.
*/
addr = mmap(NULL, (NR_HUGE_PAGES + 2) * huge_page_size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
-1, 0);
if (addr == MAP_FAILED) {
perror("mmap");
exit(1);
}
if (munmap(addr, huge_page_size) ||
munmap(addr + (NR_HUGE_PAGES + 1) * huge_page_size,
huge_page_size)) {
perror("munmap");
exit(1);
}
addr = addr + huge_page_size;
write_fault_pages(addr, NR_HUGE_PAGES);
validate_free_pages(free_hugepages - NR_HUGE_PAGES);
/* addr before mapping should fail */
ret = madvise(addr - base_page_size, NR_HUGE_PAGES * huge_page_size,
MADV_DONTNEED);
if (!ret) {
printf("Unexpected success of madvise call with invalid addr line %d\n",
__LINE__);
exit(1);
}
/* addr + length after mapping should fail */
ret = madvise(addr, (NR_HUGE_PAGES * huge_page_size) + base_page_size,
MADV_DONTNEED);
Annotation
- Immediate include surface: `stdlib.h`, `stdio.h`, `unistd.h`, `sys/mman.h`, `fcntl.h`, `vm_util.h`, `kselftest.h`.
- Detected declarations: `function write_fault_pages`, `function read_fault_pages`, `function main`, `function munmap`.
- 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.