tools/testing/selftests/mm/hugepage-vmemmap.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/hugepage-vmemmap.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/hugepage-vmemmap.c- Extension
.c- Size
- 3035 bytes
- Lines
- 135
- 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.h
Detected Declarations
function write_bytesfunction virt_to_pfnfunction check_page_flagsfunction main
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* A test case of using hugepage memory in a user application using the
* mmap system call with MAP_HUGETLB flag. Before running this program
* make sure the administrator has allocated enough default sized huge
* pages to cover the 2 MB allocation.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "vm_util.h"
#define PAGE_COMPOUND_HEAD (1UL << 15)
#define PAGE_COMPOUND_TAIL (1UL << 16)
#define PAGE_HUGE (1UL << 17)
#define HEAD_PAGE_FLAGS (PAGE_COMPOUND_HEAD | PAGE_HUGE)
#define TAIL_PAGE_FLAGS (PAGE_COMPOUND_TAIL | PAGE_HUGE)
#define PM_PFRAME_BITS 55
#define PM_PFRAME_MASK ~((1UL << PM_PFRAME_BITS) - 1)
static size_t pagesize;
static size_t maplength;
static void write_bytes(char *addr, size_t length)
{
unsigned long i;
for (i = 0; i < length; i++)
*(addr + i) = (char)i;
}
static unsigned long virt_to_pfn(void *addr)
{
int fd;
unsigned long pagemap;
fd = open("/proc/self/pagemap", O_RDONLY);
if (fd < 0)
return -1UL;
lseek(fd, (unsigned long)addr / pagesize * sizeof(pagemap), SEEK_SET);
read(fd, &pagemap, sizeof(pagemap));
close(fd);
return pagemap & ~PM_PFRAME_MASK;
}
static int check_page_flags(unsigned long pfn)
{
int fd, i;
unsigned long pageflags;
fd = open("/proc/kpageflags", O_RDONLY);
if (fd < 0)
return -1;
lseek(fd, pfn * sizeof(pageflags), SEEK_SET);
read(fd, &pageflags, sizeof(pageflags));
if ((pageflags & HEAD_PAGE_FLAGS) != HEAD_PAGE_FLAGS) {
close(fd);
printf("Head page flags (%lx) is invalid\n", pageflags);
return -1;
}
/*
* pages other than the first page must be tail and shouldn't be head;
* this also verifies kernel has correctly set the fake page_head to tail
* while hugetlb_free_vmemmap is enabled.
*/
for (i = 1; i < maplength / pagesize; i++) {
read(fd, &pageflags, sizeof(pageflags));
if ((pageflags & TAIL_PAGE_FLAGS) != TAIL_PAGE_FLAGS ||
(pageflags & HEAD_PAGE_FLAGS) == HEAD_PAGE_FLAGS) {
close(fd);
printf("Tail page flags (%lx) is invalid\n", pageflags);
return -1;
}
}
close(fd);
return 0;
}
int main(int argc, char **argv)
Annotation
- Immediate include surface: `stdlib.h`, `stdio.h`, `unistd.h`, `sys/mman.h`, `fcntl.h`, `vm_util.h`.
- Detected declarations: `function write_bytes`, `function virt_to_pfn`, `function check_page_flags`, `function main`.
- 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.