tools/testing/selftests/mm/ksm_functional_tests.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/mm/ksm_functional_tests.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/mm/ksm_functional_tests.c- Extension
.c- Size
- 18403 bytes
- Lines
- 763
- 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
stdlib.hstring.hstdbool.hstdint.hunistd.herrno.hfcntl.hsys/mman.hsys/prctl.hsys/syscall.hsys/ioctl.hsys/wait.hlinux/userfaultfd.hkselftest.hvm_util.h
Detected Declarations
enum ksm_merge_modefunction range_maps_duplicatesfunction differentlyfunction test_unmergefunction test_unmerge_zero_pagesfunction test_unmerge_discardedfunction test_unmerge_uffd_wpfunction test_prctlfunction test_child_ksmfunction test_child_ksm_errfunction test_prctl_forkfunction start_ksmd_and_set_frequencyfunction stop_ksmd_and_restore_frequencyfunction test_prctl_fork_execfunction test_prctl_unmergefunction test_prot_nonefunction test_fork_ksm_merging_page_countfunction init_global_file_handlesfunction main
Annotated Snippet
if (ret < 0 && errno == EINVAL) {
ksft_print_msg("PR_SET_MEMORY_MERGE not supported\n");
err_map = MAP_MERGE_SKIP;
goto unmap;
} else if (ret) {
ksft_print_msg("PR_SET_MEMORY_MERGE=1 failed\n");
goto unmap;
}
break;
case KSM_MERGE_MADVISE:
if (madvise(map, size, MADV_MERGEABLE)) {
ksft_print_msg("MADV_MERGEABLE failed\n");
goto unmap;
}
break;
case KSM_MERGE_NONE:
break;
}
/* Run KSM to trigger merging and wait. */
if (ksm_start() < 0) {
ksft_print_msg("Running KSM failed\n");
goto unmap;
}
/*
* Check if anything was merged at all. Ignore the zero page that is
* accounted differently (depending on kernel support).
*/
if (val && !ksm_get_self_merging_pages()) {
ksft_print_msg("No pages got merged\n");
goto unmap;
}
return map;
unmap:
munmap(map, size);
return err_map;
}
static char *mmap_and_merge_range(char val, unsigned long size, int prot,
enum ksm_merge_mode mode)
{
char *map;
char *ret = MAP_FAILED;
map = __mmap_and_merge_range(val, size, prot, mode);
if (map == MAP_MERGE_FAIL)
ksft_test_result_fail("Merging memory failed");
else if (map == MAP_MERGE_SKIP)
ksft_test_result_skip("Merging memory skipped");
else
ret = map;
return ret;
}
static void test_unmerge(void)
{
const unsigned int size = 2 * MiB;
char *map;
ksft_print_msg("[RUN] %s\n", __func__);
map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE);
if (map == MAP_FAILED)
return;
if (madvise(map, size, MADV_UNMERGEABLE)) {
ksft_test_result_fail("MADV_UNMERGEABLE failed\n");
goto unmap;
}
ksft_test_result(!range_maps_duplicates(map, size),
"Pages were unmerged\n");
unmap:
ksm_stop();
munmap(map, size);
}
static void test_unmerge_zero_pages(void)
{
const unsigned int size = 2 * MiB;
char *map;
unsigned int offs;
unsigned long pages_expected;
ksft_print_msg("[RUN] %s\n", __func__);
if (ksm_get_self_zero_pages() < 0) {
Annotation
- Immediate include surface: `stdlib.h`, `string.h`, `stdbool.h`, `stdint.h`, `unistd.h`, `errno.h`, `fcntl.h`, `sys/mman.h`.
- Detected declarations: `enum ksm_merge_mode`, `function range_maps_duplicates`, `function differently`, `function test_unmerge`, `function test_unmerge_zero_pages`, `function test_unmerge_discarded`, `function test_unmerge_uffd_wp`, `function test_prctl`, `function test_child_ksm`, `function test_child_ksm_err`.
- 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.