tools/testing/selftests/mm/process_madv.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/process_madv.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/process_madv.c- Extension
.c- Size
- 9367 bytes
- Lines
- 373
- 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
kselftest_harness.herrno.hsetjmp.hsignal.hstdbool.hstdio.hstdlib.hstring.hlinux/mman.hsys/syscall.hunistd.hsched.hvm_util.h../pidfd/pidfd.h
Detected Declarations
struct child_infofunction sys_process_madvisefunction process_madvisefunction process_madvisefunction process_madvisefunction process_madvisefunction process_madvisefunction process_madvise
Annotated Snippet
struct child_info {
pid_t pid;
void *map_addr;
} info;
huge_page_size = read_pmd_pagesize();
if (huge_page_size <= 0)
SKIP(return, "Could not determine a valid huge page size.\n");
ASSERT_EQ(pipe(pipe_info), 0);
self->child_pid = fork();
ASSERT_NE(self->child_pid, -1);
if (self->child_pid == 0) {
char *map;
size_t map_size = 2 * huge_page_size;
close(pipe_info[0]);
map = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
ASSERT_NE(map, MAP_FAILED);
/* Fault in as small pages */
for (size_t i = 0; i < map_size; i += pagesize)
map[i] = 'A';
/* Send info and pause */
info.pid = getpid();
info.map_addr = map;
ret = write(pipe_info[1], &info, sizeof(info));
ASSERT_EQ(ret, sizeof(info));
close(pipe_info[1]);
pause();
exit(0);
}
close(pipe_info[1]);
/* Receive child info */
ret = read(pipe_info[0], &info, sizeof(info));
if (ret <= 0) {
waitpid(self->child_pid, NULL, 0);
SKIP(return, "Failed to read child info from pipe.\n");
}
ASSERT_EQ(ret, sizeof(info));
close(pipe_info[0]);
self->child_pid = info.pid;
self->remote_pidfd = syscall(__NR_pidfd_open, self->child_pid, 0);
ASSERT_GE(self->remote_pidfd, 0);
vec.iov_base = info.map_addr;
vec.iov_len = huge_page_size;
ret = sys_process_madvise(self->remote_pidfd, &vec, 1, MADV_COLLAPSE,
0);
if (ret == -1) {
if (errno == EINVAL)
SKIP(return, "PROCESS_MADV_ADVISE is not supported.\n");
else if (errno == EPERM)
SKIP(return,
"No process_madvise() permissions, try running as root.\n");
return;
}
ASSERT_EQ(ret, huge_page_size);
}
/*
* Test process_madvise() with a pidfd for a process that has already
* exited to ensure correct error handling.
*/
TEST_F(process_madvise, exited_process_pidfd)
{
const unsigned long pagesize = self->page_size;
struct iovec vec;
char *map;
ssize_t ret;
map = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1,
0);
if (map == MAP_FAILED)
SKIP(return, "mmap failed, not enough memory.\n");
vec.iov_base = map;
vec.iov_len = pagesize;
Annotation
- Immediate include surface: `kselftest_harness.h`, `errno.h`, `setjmp.h`, `signal.h`, `stdbool.h`, `stdio.h`, `stdlib.h`, `string.h`.
- Detected declarations: `struct child_info`, `function sys_process_madvise`, `function process_madvise`, `function process_madvise`, `function process_madvise`, `function process_madvise`, `function process_madvise`, `function process_madvise`.
- 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.