tools/testing/selftests/mm/pagemap_ioctl.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/pagemap_ioctl.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/pagemap_ioctl.c- Extension
.c- Size
- 55300 bytes
- Lines
- 1738
- 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.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hfcntl.hstring.hsys/mman.herrno.hmalloc.hvm_util.hkselftest.hlinux/types.hlinux/memfd.hlinux/userfaultfd.hlinux/fs.hsys/ioctl.hsys/stat.hmath.hasm/unistd.hpthread.hsys/resource.hassert.hsys/ipc.hsys/shm.h
Detected Declarations
function pagemap_ioctlfunction pagemap_iocfunction init_uffdfunction wp_initfunction wp_freefunction wp_addr_rangefunction userfaultfd_testsfunction get_readsfunction sanity_tests_sdfunction base_testsfunction hpage_unit_testsfunction unmapped_region_testsfunction test_simplefunction sanity_testsfunction mprotect_testsfunction get_dirty_pages_resetfunction transact_testfunction zeropfn_testsfunction main
Annotated Snippet
if (mem == (char *)-1) {
shmctl(*shmid, IPC_RMID, NULL);
ksft_exit_fail_msg("Shared memory attach failure\n");
}
} else {
mem = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_HUGETLB | MAP_PRIVATE, -1, 0);
if (mem == MAP_FAILED)
return NULL;
}
return mem;
}
int userfaultfd_tests(void)
{
long mem_size, vec_size, written, num_pages = 16;
char *mem, *vec;
mem_size = num_pages * page_size;
mem = mmap(NULL, mem_size, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (mem == MAP_FAILED)
ksft_exit_fail_msg("error nomem\n");
wp_init(mem, mem_size);
/* Change protection of pages differently */
mprotect(mem, mem_size/8, PROT_READ|PROT_WRITE);
mprotect(mem + 1 * mem_size/8, mem_size/8, PROT_READ);
mprotect(mem + 2 * mem_size/8, mem_size/8, PROT_READ|PROT_WRITE);
mprotect(mem + 3 * mem_size/8, mem_size/8, PROT_READ);
mprotect(mem + 4 * mem_size/8, mem_size/8, PROT_READ|PROT_WRITE);
mprotect(mem + 5 * mem_size/8, mem_size/8, PROT_NONE);
mprotect(mem + 6 * mem_size/8, mem_size/8, PROT_READ|PROT_WRITE);
mprotect(mem + 7 * mem_size/8, mem_size/8, PROT_READ);
wp_addr_range(mem + (mem_size/16), mem_size - 2 * (mem_size/8));
wp_addr_range(mem, mem_size);
vec_size = mem_size/page_size;
vec = calloc(vec_size, sizeof(struct page_region));
written = pagemap_ioctl(mem, mem_size, vec, 1, PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC,
vec_size - 2, PAGE_IS_WRITTEN, 0, 0, PAGE_IS_WRITTEN);
if (written < 0)
ksft_exit_fail_msg("error %ld %d %s\n", written, errno, strerror(errno));
ksft_test_result(written == 0, "%s all new pages must not be written (dirty)\n", __func__);
wp_free(mem, mem_size);
munmap(mem, mem_size);
free(vec);
return 0;
}
int get_reads(struct page_region *vec, int vec_size)
{
int i, sum = 0;
for (i = 0; i < vec_size; i++)
sum += LEN(vec[i]);
return sum;
}
int sanity_tests_sd(void)
{
unsigned long long mem_size, vec_size, i, total_pages = 0;
long ret, ret2, ret3;
int num_pages = 1000;
int total_writes, total_reads, reads, count;
struct page_region *vec, *vec2;
char *mem, *m[2];
long walk_end;
vec_size = num_pages/2;
mem_size = num_pages * page_size;
vec = calloc(vec_size, sizeof(struct page_region));
if (!vec)
ksft_exit_fail_msg("error nomem\n");
vec2 = calloc(vec_size, sizeof(struct page_region));
if (!vec2)
ksft_exit_fail_msg("error nomem\n");
mem = mmap(NULL, mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (mem == MAP_FAILED)
ksft_exit_fail_msg("error nomem\n");
Annotation
- Immediate include surface: `stdio.h`, `fcntl.h`, `string.h`, `sys/mman.h`, `errno.h`, `malloc.h`, `vm_util.h`, `kselftest.h`.
- Detected declarations: `function pagemap_ioctl`, `function pagemap_ioc`, `function init_uffd`, `function wp_init`, `function wp_free`, `function wp_addr_range`, `function userfaultfd_tests`, `function get_reads`, `function sanity_tests_sd`, `function base_tests`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.