tools/testing/selftests/mm/merge.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/merge.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/merge.c- Extension
.c- Size
- 48197 bytes
- Lines
- 1593
- 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.hlinux/prctl.hfcntl.hstdio.hstdlib.hunistd.hsys/mman.hsys/prctl.hsys/syscall.hsys/wait.hlinux/perf_event.hvm_util.hlinux/mman.h
Detected Declarations
function do_forkfunction sys_msealfunction sys_mseal
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include "kselftest_harness.h"
#include <linux/prctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <linux/perf_event.h>
#include "vm_util.h"
#include <linux/mman.h>
FIXTURE(merge)
{
unsigned int page_size;
char *carveout;
struct procmap_fd procmap;
};
static char *map_carveout(unsigned int page_size)
{
return mmap(NULL, 30 * page_size, PROT_NONE,
MAP_ANON | MAP_PRIVATE, -1, 0);
}
static pid_t do_fork(struct procmap_fd *procmap)
{
pid_t pid = fork();
if (pid == -1)
return -1;
if (pid != 0) {
wait(NULL);
return pid;
}
/* Reopen for child. */
if (close_procmap(procmap))
return -1;
if (open_self_procmap(procmap))
return -1;
return 0;
}
#ifdef __NR_mseal
static int sys_mseal(void *ptr, size_t len, unsigned long flags)
{
return syscall(__NR_mseal, (unsigned long)ptr, len, flags);
}
#else
static int sys_mseal(void *ptr, size_t len, unsigned long flags)
{
errno = ENOSYS;
return -1;
}
#endif
FIXTURE_SETUP(merge)
{
self->page_size = psize();
/* Carve out PROT_NONE region to map over. */
self->carveout = map_carveout(self->page_size);
ASSERT_NE(self->carveout, MAP_FAILED);
/* Setup PROCMAP_QUERY interface. */
ASSERT_EQ(open_self_procmap(&self->procmap), 0);
}
FIXTURE_TEARDOWN(merge)
{
ASSERT_EQ(munmap(self->carveout, 30 * self->page_size), 0);
/* May fail for parent of forked process. */
close_procmap(&self->procmap);
/*
* Clear unconditionally, as some tests set this. It is no issue if this
* fails (KSM may be disabled for instance).
*/
prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0);
}
FIXTURE(merge_with_fork)
{
unsigned int page_size;
char *carveout;
struct procmap_fd procmap;
Annotation
- Immediate include surface: `kselftest_harness.h`, `linux/prctl.h`, `fcntl.h`, `stdio.h`, `stdlib.h`, `unistd.h`, `sys/mman.h`, `sys/prctl.h`.
- Detected declarations: `function do_fork`, `function sys_mseal`, `function sys_mseal`.
- 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.