From nobody Wed Dec 17 14:16:31 2025 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F18B1E5B94; Tue, 7 Oct 2025 10:28:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=183.62.165.209 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759832942; cv=none; b=ny9NMAY2/V5/sjoLNxVPGJc73yugggLmpN18rvbOCGUuC/pQGjRZwTFRfn0RZBOm9H4lzJOMv8zcUtkJUhWHm3V48hl6Vc8qigoWUBpu0sAnJQxh0ndCdZl8wXyDop8XVkog3pYDI8WjCdBurb531GiHvbFyYoFWdNUJQRhS5W4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759832942; c=relaxed/simple; bh=A+YqwWb37tzwG0A3miC/nTTjWam4vJUqRM4+bA0TZYg=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=f+aGtwThl9qdXv2tpe/cFVLvGirPjnVjslr0QPDxF+QmkBJBqE1ksd1RP4sZ1xuHNanVLrGVInXxsPNoZKyrmWFhEElgcaH2UuYsx2q+VurG+abDTww+2/cRBhlbn8AjtnZf0A1z5Friea2PuySPnGL9azeF9teejNQ97vyp+uE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=183.62.165.209 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4cgsmv6XvGz4xNt6; Tue, 07 Oct 2025 18:28:47 +0800 (CST) Received: from xaxapp01.zte.com.cn ([10.88.99.176]) by mse-fl1.zte.com.cn with SMTP id 597ASIIs016509; Tue, 7 Oct 2025 18:28:18 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp01[null]) by mapi (Zmail) with MAPI id mid32; Tue, 7 Oct 2025 18:28:21 +0800 (CST) Date: Tue, 7 Oct 2025 18:28:21 +0800 (CST) X-Zmail-TransId: 2af968e4eb45fc5-6256d X-Mailer: Zmail v1.0 Message-ID: <20251007182821572h_SoFqYZXEP1mvWI4n9VL@zte.com.cn> In-Reply-To: <20251007182504440BJgK8VXRHh8TD7IGSUIY4@zte.com.cn> References: 20251007182504440BJgK8VXRHh8TD7IGSUIY4@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , , , Cc: , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjIgMS8yXSBtbS9rc206IGZpeCBleGVjL2ZvcmsgaW5oZXJpdGFuY2Ugc3VwcG9ydCBmb3IgcHJjdGw=?= X-MAIL: mse-fl1.zte.com.cn 597ASIIs016509 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.132 unknown Tue, 07 Oct 2025 18:28:47 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68E4EB5F.002/4cgsmv6XvGz4xNt6 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin Background =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The commit d7597f59d1d33 ("mm: add new api to enable ksm per process") intr= oduce MMF_VM_MERGE_ANY for mm->flags, and allow user to set it by prctl() so that= the process's VMAs are forcely scanned by ksmd. Sequently, the commit 3c6f33b72= 73a ("mm/ksm: support fork/exec for prctl") support inheritsingMMF_VM_MERGE_ANY= flag when a task calls execve(). Lastly, The commit 3a9e567ca45fb ("mm/ksm: fix ksm exec support for prctl") fixed the issue that ksmd doesn'= t scan the mm_struct with MMF_VM_MERGE_ANY by adding the mm_slot to ksm_mm_head in __bprm_mm_init(). Problem =3D=3D=3D=3D=3D=3D=3D In some extreme scenarios, however, this inheritance of MMF_VM_MERGE_ANY du= ring exec/fork can fail. For example, when the scanning frequency of ksmd is tun= ed extremely high, a process carrying MMF_VM_MERGE_ANY may still fail to pass = it to the newly exec'd process. This happens because ksm_execve() is executed too= early in the do_execve flow (prematurely adding the new mm_struct to the ksm_mm_s= lot list). As a result, before do_execve completes, ksmd may have already performed a = scan and found that this new mm_struct has no VM_MERGEABLE VMAs, thus clearing its MMF_VM_MERGE_ANY flag. Consequently, when the new program executes, the flag MMF_VM_MERGE_ANY inheritance missed. Root reason =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D The commit d7597f59d1d33 ("mm: add new api to enable ksm per process") clea= r the flag MMF_VM_MERGE_ANY when ksmd found no VM_MERGEABLE VMAs. Solution =3D=3D=3D=3D=3D=3D=3D=3D First, Don't clear MMF_VM_MERGE_ANY when ksmd found no VM_MERGEABLE VMAs, b= ecause perhaps their mm_struct has just been added to ksm_mm_slot list, and its process ha= s not yet officially started running or has not yet performed mmap/brk to allocate an= onymous VMAS. Second, recheck MMF_VM_MERGEABLE again if a process takes MMF_VM_MERGE_ANY,= and create a mm_slot and join it into ksm_scan_list again. Fixes: 3c6f33b7273a ("mm/ksm: support fork/exec for prctl") Fixes: d7597f59d1d3 ("mm: add new api to enable ksm per process") Signed-off-by: xu xin Cc: stable@vger.kernel.org Cc: Stefan Roesch Cc: David Hildenbrand Cc: Jinjiang Tu Cc: Wang Yaxin --- include/linux/ksm.h | 4 ++-- mm/ksm.c | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 067538fc4d58..c982694c987b 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -17,7 +17,7 @@ #ifdef CONFIG_KSM int ksm_madvise(struct vm_area_struct *vma, unsigned long start, unsigned long end, int advice, vm_flags_t *vm_flags); -vm_flags_t ksm_vma_flags(const struct mm_struct *mm, const struct file *fi= le, +vm_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, vm_flags_t vm_flags); int ksm_enable_merge_any(struct mm_struct *mm); int ksm_disable_merge_any(struct mm_struct *mm); @@ -103,7 +103,7 @@ bool ksm_process_mergeable(struct mm_struct *mm); #else /* !CONFIG_KSM */ -static inline vm_flags_t ksm_vma_flags(const struct mm_struct *mm, +static inline vm_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, vm_flags_t vm_flags) { return vm_flags; diff --git a/mm/ksm.c b/mm/ksm.c index 04019a15b25d..19efe3d41c75 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2617,8 +2617,14 @@ static struct ksm_rmap_item *scan_get_next_rmap_item= (struct page **page) spin_unlock(&ksm_mmlist_lock); mm_slot_free(mm_slot_cache, mm_slot); + /* + * Only clear MMF_VM_MERGEABLE. We must not clear + * MMF_VM_MERGE_ANY, because for those MMF_VM_MERGE_ANY process, + * perhaps their mm_struct has just been added to ksm_mm_slot + * list, and its process has not yet officially started running + * or has not yet performed mmap/brk to allocate anonymous VMAS. + */ mm_flags_clear(MMF_VM_MERGEABLE, mm); - mm_flags_clear(MMF_VM_MERGE_ANY, mm); mmap_read_unlock(mm); mmdrop(mm); } else { @@ -2736,12 +2742,20 @@ static int __ksm_del_vma(struct vm_area_struct *vma) * * Returns: @vm_flags possibly updated to mark mergeable. */ -vm_flags_t ksm_vma_flags(const struct mm_struct *mm, const struct file *fi= le, +vm_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, vm_flags_t vm_flags) { if (mm_flags_test(MMF_VM_MERGE_ANY, mm) && - __ksm_should_add_vma(file, vm_flags)) + __ksm_should_add_vma(file, vm_flags)) { vm_flags |=3D VM_MERGEABLE; + /* + * Generally, the flags here always include MMF_VM_MERGEABLE. + * However, in rare cases, this flag may be cleared by ksmd who + * scans a cycle without finding any mergeable vma. + */ + if (unlikely(!mm_flags_test(MMF_VM_MERGEABLE, mm))) + __ksm_enter(mm); + } return vm_flags; } --=20 2.25.1 From nobody Wed Dec 17 14:16:31 2025 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 057DB2D8DB5; Tue, 7 Oct 2025 10:29:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759832996; cv=none; b=V5EHKh7HSGDvc3lUvGYfs/Af2af9VET1ACr2S/dKDBT5pv7RZ2r9qiSl/6og/NiL2s5i8jQ8yTXgdhD5rkPDkf3IYH+ZoLqKbbJGCdKSqXqY4u+FL9NVAFudOdAE61I3y0nPcbhXox8AN8aOrpFeoSvsVdn5YLs5zAnJEYVCv98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759832996; c=relaxed/simple; bh=K9EgXTNWeloymlhQlMOub+ZTfTH5VQt0HXIU7kjBHCM=; h=Date:Message-ID:In-Reply-To:References:Mime-Version:From:To:Cc: Subject:Content-Type; b=C8W7S8BSx1Pt76ZLO1TtDIstLGFD1HbPV2SONIqT2XqmsYYYCd1HRj2PG+x3X1u94qrenNMLCxP2y79y0EV5/yEiKOxw+nvXOdJIXidXJqgcrNCSPJqs9jIiOwGKxMMfgOfrNMqlLE0or54RLozhSboPPZv9KmIUFX5mv61BNp8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4cgsp222dYz5PM34; Tue, 07 Oct 2025 18:29:46 +0800 (CST) Received: from xaxapp02.zte.com.cn ([10.88.97.241]) by mse-fl1.zte.com.cn with SMTP id 597ATXmE018488; Tue, 7 Oct 2025 18:29:33 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp04[null]) by mapi (Zmail) with MAPI id mid32; Tue, 7 Oct 2025 18:29:35 +0800 (CST) Date: Tue, 7 Oct 2025 18:29:35 +0800 (CST) X-Zmail-TransId: 2afb68e4eb8f049-615f1 X-Mailer: Zmail v1.0 Message-ID: <20251007182935207jm31wCIgLpZg5XbXQY64S@zte.com.cn> In-Reply-To: <20251007182504440BJgK8VXRHh8TD7IGSUIY4@zte.com.cn> References: 20251007182504440BJgK8VXRHh8TD7IGSUIY4@zte.com.cn Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: , , , , Cc: , , , , , , , , Subject: =?UTF-8?B?W1BBVENIIGxpbnV4LW5leHQgdjIgMi8yXSBzZWxmdGVzdHM6IHVwZGF0ZSBrc20gaW5oZXJpdGF0aW9uIHRlc3RzIGZvciBwcmN0bCBmb3JrL2V4ZWM=?= X-MAIL: mse-fl1.zte.com.cn 597ATXmE018488 X-TLS: YES X-SPF-DOMAIN: zte.com.cn X-ENVELOPE-SENDER: xu.xin16@zte.com.cn X-SPF: None X-SOURCE-IP: 10.5.228.132 unknown Tue, 07 Oct 2025 18:29:46 +0800 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 68E4EB9A.000/4cgsp222dYz5PM34 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: xu xin To reproduce the issue mentioned by [1], this add a setting of pages_to_scan and sleep_millisecs at the start of test_prctl_fork_exec(). The main change is just raise the scanning frequency of ksmd. [1] https://lore.kernel.org/all/202510012256278259zrhgATlLA2C510DMD3qI@zte.= com.cn/ Signed-off-by: xu xin --- .../selftests/mm/ksm_functional_tests.c | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/test= ing/selftests/mm/ksm_functional_tests.c index ac136f04b8d6..95afa5cfc062 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -38,6 +38,8 @@ enum ksm_merge_mode { }; static int mem_fd; +static int pages_to_scan_fd; +static int sleep_millisecs_fd; static int pagemap_fd; static size_t pagesize; @@ -493,6 +495,46 @@ static void test_prctl_fork(void) ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } +static int start_ksmd_and_set_frequency(char *pages_to_scan, char *sleep_m= s) +{ + int ksm_fd; + + ksm_fd =3D open("/sys/kernel/mm/ksm/run", O_RDWR); + if (ksm_fd < 0) + return -errno; + + if (write(ksm_fd, "1", 1) !=3D 1) + return -errno; + + if (write(pages_to_scan_fd, pages_to_scan, strlen(pages_to_scan)) <=3D 0) + return -errno; + + if (write(sleep_millisecs_fd, sleep_ms, strlen(sleep_ms)) <=3D 0) + return -errno; + + return 0; +} + +static int stop_ksmd_and_restore_frequency(void) +{ + int ksm_fd; + + ksm_fd =3D open("/sys/kernel/mm/ksm/run", O_RDWR); + if (ksm_fd < 0) + return -errno; + + if (write(ksm_fd, "2", 1) !=3D 1) + return -errno; + + if (write(pages_to_scan_fd, "100", 3) <=3D 0) + return -errno; + + if (write(sleep_millisecs_fd, "20", 2) <=3D 0) + return -errno; + + return 0; +} + static void test_prctl_fork_exec(void) { int ret, status; @@ -500,6 +542,9 @@ static void test_prctl_fork_exec(void) ksft_print_msg("[RUN] %s\n", __func__); + if (start_ksmd_and_set_frequency("2000", "0")) + ksft_test_result_fail("set ksmd's scanning frequency failed\n"); + ret =3D prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); if (ret < 0 && errno =3D=3D EINVAL) { ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); @@ -542,6 +587,11 @@ static void test_prctl_fork_exec(void) return; } + if (stop_ksmd_and_restore_frequency()) { + ksft_test_result_fail("restore ksmd frequency failed\n"); + return; + } + ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } @@ -656,6 +706,13 @@ static void init_global_file_handles(void) ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n"); if (ksm_get_self_merging_pages() < 0) ksft_exit_skip("accessing \"/proc/self/ksm_merging_pages\") failed\n"); + + pages_to_scan_fd =3D open("/sys/kernel/mm/ksm/pages_to_scan", O_RDWR); + if (pages_to_scan_fd < 0) + ksft_exit_fail_msg("opening /sys/kernel/mm/ksm/pages_to_scan failed\n"); + sleep_millisecs_fd =3D open("/sys/kernel/mm/ksm/sleep_millisecs", O_RDWR); + if (sleep_millisecs_fd < 0) + ksft_exit_fail_msg("opening /sys/kernel/mm/ksm/sleep_millisecs failed\n"= ); } int main(int argc, char **argv) --=20 2.25.1