Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/mm: add fork inheritance test for ksm_merging_pages counter

Add a new selftest to verify whether the `ksm_merging_pages` counter in
`mm_struct` is not inherited by a child process after fork. This helps
ensure correctness of KSM accounting across process creation.

Link: https://lkml.kernel.org/r/e7bb17d374133bd31a3e423aa9e46e1122e74971.1758648700.git.donettom@linux.ibm.com
Signed-off-by: Donet Tom <donettom@linux.ibm.com>
Acked-by: David Hildenbrand <david@redhat.com>
Cc: Aboorva Devarajan <aboorvad@linux.ibm.com>
Cc: Chengming Zhou <chengming.zhou@linux.dev>
Cc: "Ritesh Harjani (IBM)" <ritesh.list@gmail.com>
Cc: Wei Yang <richard.weiyang@gmail.com>
Cc: xu xin <xu.xin16@zte.com.cn>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Donet Tom and committed by
Andrew Morton
08ff89b5 4d6fc29f

+42 -1
+42 -1
tools/testing/selftests/mm/ksm_functional_tests.c
··· 602 602 munmap(map, size); 603 603 } 604 604 605 + static void test_fork_ksm_merging_page_count(void) 606 + { 607 + const unsigned int size = 2 * MiB; 608 + char *map; 609 + pid_t child_pid; 610 + int status; 611 + 612 + ksft_print_msg("[RUN] %s\n", __func__); 613 + 614 + map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_MADVISE); 615 + if (map == MAP_FAILED) 616 + return; 617 + 618 + child_pid = fork(); 619 + if (!child_pid) { 620 + init_global_file_handles(); 621 + exit(ksm_get_self_merging_pages()); 622 + } else if (child_pid < 0) { 623 + ksft_test_result_fail("fork() failed\n"); 624 + goto unmap; 625 + } 626 + 627 + if (waitpid(child_pid, &status, 0) < 0) { 628 + ksft_test_result_fail("waitpid() failed\n"); 629 + goto unmap; 630 + } 631 + 632 + status = WEXITSTATUS(status); 633 + if (status) { 634 + ksft_test_result_fail("ksm_merging_page in child: %d\n", status); 635 + goto unmap; 636 + } 637 + 638 + ksft_test_result_pass("ksm_merging_pages is not inherited after fork\n"); 639 + 640 + unmap: 641 + ksm_stop(); 642 + munmap(map, size); 643 + } 644 + 605 645 static void init_global_file_handles(void) 606 646 { 607 647 mem_fd = open("/proc/self/mem", O_RDWR); ··· 660 620 661 621 int main(int argc, char **argv) 662 622 { 663 - unsigned int tests = 8; 623 + unsigned int tests = 9; 664 624 int err; 665 625 666 626 if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) { ··· 692 652 test_prctl_fork(); 693 653 test_prctl_fork_exec(); 694 654 test_prctl_unmerge(); 655 + test_fork_ksm_merging_page_count(); 695 656 696 657 err = ksft_get_fail_cnt(); 697 658 if (err)