From nobody Tue Nov 24 14:12:11 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.213 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405401; b=k5KsxPkCh8N/FFidcwT1fJ2zeInNbe8HB264jmY9BpFOeeseCIsahEM+qU9Eq6KVmG73 SW1aVw04OmkvY68Dwz37/BEwE5EXb46n6ZhJCkkpFFAGLNAjiyOL8G+i1GGntG0tlcKn5 6TcOnFT8WLs736J7aZ+WhwRnqrIZjhS+SaSz43sGShwyF+KmCFBeTHEp+y+jmVUKEFIh6 TiHo3GLE8/F40rOqU/JK3aKhxWjQGIL8ZypwPCROBBMBPBZJ1+FH0hgC3wVlxXsqc8Ola jqDV4X0kkguhEZlL+SCq3SQTxXNJx7npxlFJmbbgak4AVok4cmhAxlRwXexWqt15e2A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405401; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=u/dSSnvt29VXMuqEAAa81mXAdbrbhqEv++QT9+xRYhg=; b=Z9wtK+rr2LYgbveXz0K1jt65h3mKwSANG2R4GwJUEdgSrx8iUR5Grh2dI47F0iUshkgB q5vNRFHPk+Y1mrQKqVDzl2+AON+rRh6FdcyRv7wYbPilW8SQMHkrXG7JIGB0SGAMRxota NmI5FLQZkeRFIR6QIFRkDyu49jf5Y6qZ45D62BxuRSPqb8cYhrn3jq0Dsg8FOevybsXui 8sERGtxRWEsQPfJqbjwBEa8ODy+f9ByRbBla5JqnS/d62ShQHUecrBb6u5DqDrUazsSmI tLwIUPESG0pIyQJ2u4QI2g/aTMzHEEnhyuxYTd80u+liNhgJg8vy1b+Zl4q7K2qrWYQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=sym98uDI; arc=none smtp.remote-ip=91.218.175.213 Received: from mta0.migadu.com (out-213.mta0.migadu.com [91.218.175.213]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27EA0606A31A for ; Mon, 23 Nov 2026 03:43:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 27EA0606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=sym98uDI X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=u4K0EQqDi4+xhHfJPHjH7LQ7MYMicDcjgwVFleEBNuM=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795405399; v=1; x=1796010199; b=sym98uDIogUQEVzQ57BtWFFt9Lo0yVdauWGeKKZLwDg6jJADX1S8k4zgKh5ONHgbDFhurkMK n23ugOKub2PpV5+WNX0IZz83zQ0Q6CLMi5FsPRTiOi4sV9uGT8ZKJz8Vq0vQeAAG9l+Mf8rx+DV ZHFqO7Q4VPkh4Ai1On/OOOAg= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta12.migadu.com with ESMTPS id b23e18659fa0a669; Mon, 23 Nov 2026 03:43:19 +0000 X-Mizu-Trace-ID: b23e18659fa0a669 X-Migadu-Flow: FLOW_OUT From: "Xu Xin (vivo)" To: xu.xin@linux.dev Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev, david@kernel.org, donettom@linux.ibm.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Karl Mehltretter Subject: [PATCH v3 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork Date: Mon, 23 Nov 2026 11:43:07 +0800 Message-Id: <20261123034307.236032-1-xu.xin@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261123034136.209410-1-xu.xin@linux.dev> References: <20261123034136.209410-1-xu.xin@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" As documented in Documentation/admin-guide/mm/ksm.rst, ksm_zero_pages accounts only for zero pages actually placed by KSM. However, the current implementation wrongly accounts for forked zero-page PTEs as KSM's contribution, inflating both the child's per-mm counter and the global ksm_zero_pages counter. Originally, commit e2942062e01d ("ksm: count all zero pages placed by KSM") marked KSM-placed zero-page PTEs with the dirty bit for unmap accounting, but forgot to clear this marker during fork(). Later, to prevent ksm_might_unmap_zero_page() from underflowing the counters on child exit[1], commit 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork") kept the child's inherited per-mm counter and added it to the global counter. Instead of inflating the counters, fix the root cause by resetting the child's mm->ksm_zero_pages to 0 in ksm_fork() and clearing the KSM zero-page marker via ksm_fork_pte() when copying present PTEs in copy_present_ptes(). With the marker cleared, ksm_might_unmap_zero_page() will not decrement the counters on child exit, MADV_UNMERGEABLE in the child will not unnecessarily unshare inherited zero pages, and the global counter remains untouched. [1] https://lore.kernel.org/all/53a7bf3d-843d-4e19-b0a3-cc6852fe72c1@redhat= .com/ Fixes: 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_stru= ct during fork") Cc: stable@vger.kernel.org Tested-by: Karl Mehltretter Signed-off-by: Xu Xin (vivo) --- Changes in v3: - Rename might_clear_ksm_zero_pte() to ksm_fork_pte() and move the is_ksm_zero_pte() check into copy_present_ptes() under the !page branch, as suggested by David Hildenbrand. - Expand the comment above ksm_fork_pte() to explain why the marker is cleared on fork. - Add Tested-by tag from Karl Mehltretter. Documentation/admin-guide/mm/ksm.rst | 7 ++++++- include/linux/ksm.h | 22 +++++++++++++++++++--- mm/memory.c | 5 ++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-gui= de/mm/ksm.rst index c329ca747b8c..8968ae3f55c0 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -214,7 +214,12 @@ stable_node_dups number of duplicated KSM pages ksm_zero_pages how many zero pages that are still mapped into processes were mapp= ed by - KSM when deduplicating. + KSM when deduplicating. Note: on fork(), the child process does not + inherit the ``ksm_zero_pages`` counter, and any inherited KSM-plac= ed + zero-page mappings are converted into ordinary shared zero-page + mappings. Consequently, these mappings are neither accounted for in + the child's counter nor unshared when ``MADV_UNMERGEABLE`` is call= ed + in the child. =20 When ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` + ``ksm_zero_pages`` represents the actual number of pages saved by KSM. diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 596c04ca7326..f29aec4496d0 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -32,6 +32,17 @@ void __ksm_exit(struct mm_struct *mm); */ #define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte)) =20 +/* + * Since the inherited mappings on fork are not expected to be accounted f= or + * in the child's ksm_zero_pages counter (reset in ksm_fork()), we must + * clear KSM's dirty-bit marker on KSM-placed zero-page PTEs to avoid + * underflowing the counters on child exit. + */ +static inline pte_t ksm_fork_pte(pte_t pte) +{ + return pte_mkclean(pte); +} + extern atomic_long_t ksm_zero_pages; =20 static inline void ksm_map_zero_page(struct mm_struct *mm) @@ -57,11 +68,9 @@ static inline void ksm_fork(struct mm_struct *mm, struct= mm_struct *oldmm) { /* Adding mm to ksm is best effort on fork. */ if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) { - long nr_ksm_zero_pages =3D atomic_long_read(&mm->ksm_zero_pages); - mm->ksm_merging_pages =3D 0; mm->ksm_rmap_items =3D 0; - atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages); + atomic_long_set(&mm->ksm_zero_pages, 0); __ksm_enter(mm); } } @@ -95,6 +104,8 @@ bool ksm_process_mergeable(struct mm_struct *mm); =20 #else /* !CONFIG_KSM */ =20 +#define is_ksm_zero_pte(pte) false + static inline vma_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, vma_flags_t vma_flags) { @@ -114,6 +125,11 @@ static inline void ksm_exit(struct mm_struct *mm) { } =20 +static inline pte_t ksm_fork_pte(pte_t pte) +{ + return pte; +} + static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t p= te) { } diff --git a/mm/memory.c b/mm/memory.c index 6a23198c1c67..f0b5df879e6f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1184,8 +1184,11 @@ copy_present_ptes(struct vm_area_struct *dst_vma, st= ruct vm_area_struct *src_vma int err, nr; =20 page =3D vm_normal_page(src_vma, addr, pte); - if (unlikely(!page)) + if (unlikely(!page)) { + if (unlikely(is_ksm_zero_pte(pte))) + pte =3D ksm_fork_pte(pte); goto copy_pte; + } =20 folio =3D page_folio(page); =20 --=20 2.34.1 From nobody Tue Nov 24 14:12:11 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.216 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405427; b=ggWmDsCOigynN+v3xrHCwY+5K+hpI6NWH8Wd5DGP3RlsTnTStezvGaFFcMtMe8mD74Ec jLe+S7S26hTwJUThx7YTKP2uthxKQZAxtEwitpL/4Qgkx90a/o+EydspeU5MiLfE0qFtR 788Tfm6GEAjH8rnSUP7KDFCzTkL7g4ZjnTGgF3jHF3VLMtONTfFGwlEJ9l6Q9VDkt6twI CDLOBAogA7rJWJCzF/LSEFkAPpaoj1f7k6vraV8erWuhcSQK/mEfsdrNQ/efoLh25E4Q5 kbnAE1d2IPPIICeCrJfkhUm81IHMFhRVpWAhvRba9xT8Wx+FkOzvoD7kw81ovkz3BIQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405427; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=u/dSSnvt29VXMuqEAAa81mXAdbrbhqEv++QT9+xRYhg=; b=EKHw8zDzE9yJO34/9GW6FoAQOcduRRrl3RlAzhHUaNR2Wh4+j225ki5mpRcVOR02CjHp fsTFXlHPo6HbZiWxqhDNfvUDiNpRPKNNQgtZ+Z76DYRcPk8yxv3zixXGNnC0A8Sl5QDs5 NHHjlnnj2ZsVvXTUWLsoy2tcG27W+ucaqZC1qItcMbY7tX8fCOAnId9SaFgvENA/ox8uq ju4GMptO7mFecEiE9jqaX88qWcAIlM2ZJs8kmqDWo7emdD24lM3Wy+giriLTwEYbbetA5 I33QWPY5WnBiHhCNoqvhkC+GizjPKleDccIaXuy3H7OZykePX55Jz3UiGOGboeA92Pg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=oEIIGS9K; arc=none smtp.remote-ip=91.218.175.216 Received: from mta0.migadu.com (out-216.mta0.migadu.com [91.218.175.216]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9B35606A332 for ; Mon, 23 Nov 2026 03:43:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C9B35606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=oEIIGS9K X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=u4K0EQqDi4+xhHfJPHjH7LQ7MYMicDcjgwVFleEBNuM=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795405424; v=1; x=1796010224; b=oEIIGS9Ka5spYlhYLlfawa+ZXJ12Zna0ZEFeJEE7giXqmS3hI6VfU5abnWm9O5dKcBXdhDr3 chFS9abhBmm/TkSVGVES9Fbe9qxxhndJ2h06+g34MMv9B9xjfE5uGLlQjOAPDphORiVdJwtVHXf T8NO827T1NAkp4Q7nbd81vvk= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id cec765d6627a4390; Mon, 23 Nov 2026 03:43:44 +0000 X-Mizu-Trace-ID: cec765d6627a4390 X-Migadu-Flow: FLOW_OUT From: "Xu Xin (vivo)" To: xu.xin@linux.dev Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev, david@kernel.org, donettom@linux.ibm.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Karl Mehltretter Subject: [PATCH v3 1/3] mm/ksm: do not inherit ksm_zero_pages counter or PTE markers on fork Date: Mon, 23 Nov 2026 11:43:31 +0800 Message-Id: <20261123034331.236509-1-xu.xin@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261123034136.209410-1-xu.xin@linux.dev> References: <20261123034136.209410-1-xu.xin@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" As documented in Documentation/admin-guide/mm/ksm.rst, ksm_zero_pages accounts only for zero pages actually placed by KSM. However, the current implementation wrongly accounts for forked zero-page PTEs as KSM's contribution, inflating both the child's per-mm counter and the global ksm_zero_pages counter. Originally, commit e2942062e01d ("ksm: count all zero pages placed by KSM") marked KSM-placed zero-page PTEs with the dirty bit for unmap accounting, but forgot to clear this marker during fork(). Later, to prevent ksm_might_unmap_zero_page() from underflowing the counters on child exit[1], commit 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_struct during fork") kept the child's inherited per-mm counter and added it to the global counter. Instead of inflating the counters, fix the root cause by resetting the child's mm->ksm_zero_pages to 0 in ksm_fork() and clearing the KSM zero-page marker via ksm_fork_pte() when copying present PTEs in copy_present_ptes(). With the marker cleared, ksm_might_unmap_zero_page() will not decrement the counters on child exit, MADV_UNMERGEABLE in the child will not unnecessarily unshare inherited zero pages, and the global counter remains untouched. [1] https://lore.kernel.org/all/53a7bf3d-843d-4e19-b0a3-cc6852fe72c1@redhat= .com/ Fixes: 4d6fc29f3634 ("mm/ksm: fix incorrect KSM counter handling in mm_stru= ct during fork") Cc: stable@vger.kernel.org Tested-by: Karl Mehltretter Signed-off-by: Xu Xin (vivo) --- Changes in v3: - Rename might_clear_ksm_zero_pte() to ksm_fork_pte() and move the is_ksm_zero_pte() check into copy_present_ptes() under the !page branch, as suggested by David Hildenbrand. - Expand the comment above ksm_fork_pte() to explain why the marker is cleared on fork. - Add Tested-by tag from Karl Mehltretter. Documentation/admin-guide/mm/ksm.rst | 7 ++++++- include/linux/ksm.h | 22 +++++++++++++++++++--- mm/memory.c | 5 ++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-gui= de/mm/ksm.rst index c329ca747b8c..8968ae3f55c0 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -214,7 +214,12 @@ stable_node_dups number of duplicated KSM pages ksm_zero_pages how many zero pages that are still mapped into processes were mapp= ed by - KSM when deduplicating. + KSM when deduplicating. Note: on fork(), the child process does not + inherit the ``ksm_zero_pages`` counter, and any inherited KSM-plac= ed + zero-page mappings are converted into ordinary shared zero-page + mappings. Consequently, these mappings are neither accounted for in + the child's counter nor unshared when ``MADV_UNMERGEABLE`` is call= ed + in the child. =20 When ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` + ``ksm_zero_pages`` represents the actual number of pages saved by KSM. diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 596c04ca7326..f29aec4496d0 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -32,6 +32,17 @@ void __ksm_exit(struct mm_struct *mm); */ #define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte)) =20 +/* + * Since the inherited mappings on fork are not expected to be accounted f= or + * in the child's ksm_zero_pages counter (reset in ksm_fork()), we must + * clear KSM's dirty-bit marker on KSM-placed zero-page PTEs to avoid + * underflowing the counters on child exit. + */ +static inline pte_t ksm_fork_pte(pte_t pte) +{ + return pte_mkclean(pte); +} + extern atomic_long_t ksm_zero_pages; =20 static inline void ksm_map_zero_page(struct mm_struct *mm) @@ -57,11 +68,9 @@ static inline void ksm_fork(struct mm_struct *mm, struct= mm_struct *oldmm) { /* Adding mm to ksm is best effort on fork. */ if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) { - long nr_ksm_zero_pages =3D atomic_long_read(&mm->ksm_zero_pages); - mm->ksm_merging_pages =3D 0; mm->ksm_rmap_items =3D 0; - atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages); + atomic_long_set(&mm->ksm_zero_pages, 0); __ksm_enter(mm); } } @@ -95,6 +104,8 @@ bool ksm_process_mergeable(struct mm_struct *mm); =20 #else /* !CONFIG_KSM */ =20 +#define is_ksm_zero_pte(pte) false + static inline vma_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, vma_flags_t vma_flags) { @@ -114,6 +125,11 @@ static inline void ksm_exit(struct mm_struct *mm) { } =20 +static inline pte_t ksm_fork_pte(pte_t pte) +{ + return pte; +} + static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t p= te) { } diff --git a/mm/memory.c b/mm/memory.c index 6a23198c1c67..f0b5df879e6f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1184,8 +1184,11 @@ copy_present_ptes(struct vm_area_struct *dst_vma, st= ruct vm_area_struct *src_vma int err, nr; =20 page =3D vm_normal_page(src_vma, addr, pte); - if (unlikely(!page)) + if (unlikely(!page)) { + if (unlikely(is_ksm_zero_pte(pte))) + pte =3D ksm_fork_pte(pte); goto copy_pte; + } =20 folio =3D page_folio(page); =20 --=20 2.34.1 From nobody Tue Nov 24 14:12:11 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.224 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405545; b=KGyQIHrFw9ZtROYSvVZTlPEBCQkMhVw1ss2uDssYElanCPT6VufwMF0FQudaRl0Wj86O zdFyZLKjRbvUWQBxcXuorb1JSwkBvGQOg+4fgUQOOR+6LNcdcaAyDGUuMbYBqZXISPZUt 30Azay34XePnk8hRzQmNsxINoELzth7pPUvj+tWjIBVQPYuL49OG0pNKjTgXc+z1P3s+I YDwqucA9incVpl5fQ5NJXkGjZRBWgDuUckZxGUieHNh+uUTH8QfSTSZCUozW2JrhpJEyO kN6UH318GOoh3CAixMIjG7/Zf+UOKgtvdTl5ChpNSl+K0AlYax6pA7lr/2ijn+xhiqQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405545; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=htJ+G2m31J9SRlk7O48P5yvgT1ieAXPqPN86Pmytgdo=; b=tv16SakyGqPgRPzkA9TxVvzcSG+4yJawbS2yzFua1evuccZ48g2pPLYuGK3ZjdwXNr0l 43uBxECVAvLa2KYPHOkadC8P24oWjn5iGSwJgcb5iiKXvn1qzc3bk0njD5tCWnMbR/Urp Rsb0/qfi7Bm+/5GciqjhGwz3+6pHIPNE+BCcGb6Zamph7f59dTMidOMvFz5aapab0geTz hRHft9QKKeVVmCdEZuP1AA2RkBrZ4cN9X/zZCE6OUGMtvVZufCF0sYzxNTGDuxoL7ZgEv ANGDNpy4OEhSkzpmDhUzVsr4pqkwuDF1ehZ31MOqS/VfmUc/4yNtUUYWNiLjrePbIog== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=iampxN5t; arc=none smtp.remote-ip=91.218.175.224 Received: from mta0.migadu.com (out-224.mta0.migadu.com [91.218.175.224]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AA1C606A31A for ; Mon, 23 Nov 2026 03:45:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8AA1C606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=iampxN5t X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=AjGuHxKPho2+lTUWKKgkjdNmlALln6KLBsfucJzafsI=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795405542; v=1; x=1796010342; b=iampxN5tRwt8Jt0+JUf7XqHmjXfTDzr9pzLiB1c91Ee+hYZQ8re+GvnFmQrA1ezYSjZjvzGv f+Ba+fbDbg9B/kV6vyWdS9ON5iEY8frgB1r19U36+eonmk+dbKjVbRNkfqRRKSYjSgg+jvQojDc k0hVpQZjwuFX5P3807OrPw7w= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta10.migadu.com with ESMTPS id 7f0750ea786c6782; Mon, 23 Nov 2026 03:45:26 +0000 X-Mizu-Trace-ID: 7f0750ea786c6782 X-Migadu-Flow: FLOW_OUT From: "Xu Xin (vivo)" To: xu.xin@linux.dev Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev, david@kernel.org, donettom@linux.ibm.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org Subject: [PATCH v3 2/3] selftests/mm: fix false-positive and racy KSM fork test Date: Mon, 23 Nov 2026 11:45:17 +0800 Message-Id: <20261123034517.236914-1-xu.xin@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261123034136.209410-1-xu.xin@linux.dev> References: <20261123034136.209410-1-xu.xin@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, test_fork_ksm_merging_page_count() passes wrongly even if fork() fails to reset mm->ksm_merging_pages, because the child process calls init_global_file_handles() -> ksm_stop(), which unmerges all KSM pages across the system before reading its counters. Fix three issues in test_fork_ksm_merging_page_count(): - Drop init_global_file_handles() in the child process so it does not call ksm_stop() and mutate global KSM sysfs state (unmerging all KSM pages across the system) before reading its counters. - Use '!!' in exit(!!ksm_get_self_merging_pages()) to avoid 8-bit exit status truncation (e.g., 512 pages on 4 KiB systems truncating to 0). - Introduce ksm_pause() to pause ksmd (write "0" to /sys/kernel/mm/ksm/run) before fork(), preventing ksmd from scanning the child and incrementing its counter before it is read. Signed-off-by: Xu Xin (vivo) Reviewed-by: David Hildenbrand (Arm) --- .../testing/selftests/mm/ksm_functional_tests.c | 8 ++++++-- tools/testing/selftests/mm/vm_util.c | 16 ++++++++++++++++ tools/testing/selftests/mm/vm_util.h | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/test= ing/selftests/mm/ksm_functional_tests.c index 135191f99962..b871d5656688 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -668,10 +668,14 @@ static void test_fork_ksm_merging_page_count(void) if (map =3D=3D MAP_FAILED) return; =20 + if (ksm_pause() < 0) { + ksft_test_result_fail("Stop ksmd from running failed\n"); + goto unmap; + } + child_pid =3D fork(); if (!child_pid) { - init_global_file_handles(); - exit(ksm_get_self_merging_pages()); + exit(!!ksm_get_self_merging_pages()); } else if (child_pid < 0) { ksft_test_result_fail("fork() failed\n"); goto unmap; diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests= /mm/vm_util.c index f26f9e4bfdbe..e638475543e6 100644 --- a/tools/testing/selftests/mm/vm_util.c +++ b/tools/testing/selftests/mm/vm_util.c @@ -1080,6 +1080,22 @@ int ksm_stop(void) return ret =3D=3D 1 ? 0 : -EIO; } =20 +int ksm_pause(void) +{ + int ksm_fd; + ssize_t ret; + + ksm_fd =3D open("/sys/kernel/mm/ksm/run", O_RDWR); + if (ksm_fd < 0) + return -errno; + + ret =3D write(ksm_fd, "0", 1); + close(ksm_fd); + if (ret < 0) + return -errno; + return ret =3D=3D 1 ? 0 : -EIO; +} + int get_hardware_corrupted_size(unsigned long *val) { unsigned long size; diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests= /mm/vm_util.h index ab1325dc6580..df16973d108a 100644 --- a/tools/testing/selftests/mm/vm_util.h +++ b/tools/testing/selftests/mm/vm_util.h @@ -168,6 +168,7 @@ int ksm_set_smart_scan(bool enable); int ksm_use_zero_pages(void); int ksm_start(void); int ksm_stop(void); +int ksm_pause(void); int get_hardware_corrupted_size(unsigned long *val); int unpoison_memory(unsigned long pfn); =20 --=20 2.34.1 From nobody Tue Nov 24 14:12:11 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.229 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795405560; b=0vXErtDxKTjNFFmXVEsxA+Wnfs6eUFRZzHZOt8IE1+w3dAvrUv52+REJcM36t7+7I45A KdXUjIBk8zEuw6NL59kVMbdxYlPARumBWvHqWFDANinkMeXj+eGBjVVPW9CrkIbUnl2ph pME782TuTKn1ABhE5Gm5HjIoferUeN6u2N+Cmld5JAExALmgf/CCOwRyn1PV4yQkpeLa3 ddvuLt8DK322JK2/3WiySHhzz7SKjp1Z+55hvWZ2iI1n1ZMcih2bhpsh6IgUrbGeRCGXP BhTj8MREwa/FD0W++F7BKcdoUFUrPRlNXD3WMnXXNSIoat4p/OqEbvB4myM7c7THGsQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795405560; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-Id: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=/Qa0HZZjs7or3S/BSPmhnoxADvYGuQMQWM8HAvfk0Ro=; b=ltlg24h/f3OUo7m/pH43xd/1cJLXozfs7sjUs8xskWZr/earrqYWy9XKnf9hH6Y9qDdS qiW7wBciUMn41b1oC3WQovfPPJQtMTylQT0UOKaFAAHVW+D9a4zi6QY0/DaOr0BVUyP8x LcdveRZP8qToNDbN8G+k9ayNcUfIaQKVD66X7KkkMFT3NMxAitbs69E0e/INnv06A3HJw +Iy/770k0yYR082qV1DGd4+lLDotRH7jANP5HhunLXyEAHUHucBU7qF2dOs5svLC6R4Zr l1xEQ23wNjmVLuIAG7s4sERdehII0paqGsZDdsFlhvTyeyBQ7dYmph1Mk2S8vtx5H3Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=AZ6GIxFi; arc=none smtp.remote-ip=91.218.175.229 Received: from mta0.migadu.com (out-229.mta0.migadu.com [91.218.175.229]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6EF42606A31A for ; Mon, 23 Nov 2026 03:45:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6EF42606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=AZ6GIxFi X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=ieS6icOUU8W2/q+0+Ez1Zj6/vTJxsNmx9lTsgVr45kc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795405557; v=1; x=1796010357; b=AZ6GIxFiXyYvb3yzi1ie45t+JCI8YjlvB7igI1mXeblpvO+QZ/ykV/Alp2a/NQ1lwyR+1lqe d/L8Hf30fcByxeOHETETqcQaHFGr8nabFXdCVWMSS6E6kQ97WzY0GuAwQu59vjbqmZXD0O2/8Wp I9zJ1ylSjQZyHK+DgcMK4thI= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta12.migadu.com with ESMTPS id 1cf38e753e5e894b; Mon, 23 Nov 2026 03:45:57 +0000 X-Mizu-Trace-ID: 1cf38e753e5e894b X-Migadu-Flow: FLOW_OUT From: "Xu Xin (vivo)" To: xu.xin@linux.dev Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev, david@kernel.org, donettom@linux.ibm.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org Subject: [PATCH v3 3/3] selftests/mm: verify ksm_zero_pages is not inherited on fork Date: Mon, 23 Nov 2026 11:45:48 +0800 Message-Id: <20261123034548.237302-1-xu.xin@linux.dev> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261123034136.209410-1-xu.xin@linux.dev> References: <20261123034136.209410-1-xu.xin@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend test_fork_ksm_merging_page_count() to enable use_zero_pages and merge zero pages alongside non-zero pages before fork(), verifying that neither ksm_merging_pages nor ksm_zero_pages is inherited by the child. Signed-off-by: Xu Xin (vivo) --- .../selftests/mm/ksm_functional_tests.c | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/test= ing/selftests/mm/ksm_functional_tests.c index b871d5656688..0e128c579d5c 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -655,45 +655,71 @@ static void test_prot_none(void) munmap(map, size); } =20 -static void test_fork_ksm_merging_page_count(void) +static void test_fork_ksm_statistic_count(void) { const unsigned int size =3D 2 * MiB; + long merging_pages, zero_pages; char *map; + char *zero_map; pid_t child_pid; int status; =20 ksft_print_msg("[RUN] %s\n", __func__); =20 + if (ksm_use_zero_pages() < 0) { + ksft_test_result_skip("Enabling use_zero_pages failed\n"); + return; + } + map =3D mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERG= E_MADVISE); if (map =3D=3D MAP_FAILED) return; =20 + zero_map =3D mmap_and_merge_range(0x00, size, PROT_READ | PROT_WRITE, KSM= _MERGE_MADVISE); + if (zero_map =3D=3D MAP_FAILED) + goto unmap; + + merging_pages =3D ksm_get_self_merging_pages(); + zero_pages =3D ksm_get_self_zero_pages(); + if (merging_pages < 0 || zero_pages < 0) { + ksft_test_result_skip("read KSM stats failed\n"); + goto unmap_zero; + } + + if (!merging_pages || !zero_pages) { + ksft_test_result_fail("KSM failed to merge pages before fork\n"); + goto unmap_zero; + } + if (ksm_pause() < 0) { ksft_test_result_fail("Stop ksmd from running failed\n"); - goto unmap; + goto unmap_zero; } =20 child_pid =3D fork(); if (!child_pid) { - exit(!!ksm_get_self_merging_pages()); + merging_pages =3D ksm_get_self_merging_pages(); + zero_pages =3D ksm_get_self_zero_pages(); + exit(merging_pages !=3D 0 || zero_pages !=3D 0); } else if (child_pid < 0) { ksft_test_result_fail("fork() failed\n"); - goto unmap; + goto unmap_zero; } =20 if (waitpid(child_pid, &status, 0) < 0) { ksft_test_result_fail("waitpid() failed\n"); - goto unmap; + goto unmap_zero; } =20 status =3D WEXITSTATUS(status); if (status) { ksft_test_result_fail("ksm_merging_page in child: %d\n", status); - goto unmap; + goto unmap_zero; } =20 - ksft_test_result_pass("ksm_merging_pages is not inherited after fork\n"); - + ksft_test_result_pass("ksm_merging_pages and ksm_zero_pages are reset aft= er fork\n"); +unmap_zero: + munmap(zero_map, size); unmap: ksm_stop(); munmap(map, size); @@ -842,7 +868,7 @@ int main(int argc, char **argv) test_prctl_fork(); test_prctl_fork_exec(); test_prctl_unmerge(); - test_fork_ksm_merging_page_count(); + test_fork_ksm_statistic_count(); test_merge_aged_pairs(); =20 err =3D ksft_get_fail_cnt(); --=20 2.34.1