From: David Hildenbrand <david@redhat.com>
To: Donet Tom <donettom@linux.ibm.com>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Ritesh Harjani <ritesh.list@gmail.com>,
Xu Xin <xu.xin16@zte.com.cn>,
Chengming Zhou <chengming.zhou@linux.dev>,
Wei Yang <richard.weiyang@gmail.com>,
Aboorva Devarajan <aboorvad@linux.ibm.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Giorgi Tchankvetadze <giorgitchankvetadze1997@gmail.com>,
stable@vger.kernel.org
Subject: Re: [PATCH v2 1/3] mm/ksm: Fix incorrect KSM counter handling in mm_struct during fork
Date: Wed, 17 Sep 2025 12:38:32 +0200 [thread overview]
Message-ID: <6dde7c50-a222-4984-bb69-07ace724f161@redhat.com> (raw)
In-Reply-To: <4044e7623953d9f4c240d0308cf0b2fe769ee553.1757946863.git.donettom@linux.ibm.com>
On 15.09.25 17:03, Donet Tom wrote:
> Currently, the KSM-related counters in `mm_struct`, such as
> `ksm_merging_pages`, `ksm_rmap_items`, and `ksm_zero_pages`, are
> inherited by the child process during fork. This results in inconsistent
> accounting.
>
> When a process uses KSM, identical pages are merged and an rmap item is
> created for each merged page. The `ksm_merging_pages` and
> `ksm_rmap_items` counters are updated accordingly. However, after a
> fork, these counters are copied to the child while the corresponding
> rmap items are not. As a result, when the child later triggers an
> unmerge, there are no rmap items present in the child, so the counters
> remain stale, leading to incorrect accounting.
>
> A similar issue exists with `ksm_zero_pages`, which maintains both a
> global counter and a per-process counter. During fork, the per-process
> counter is inherited by the child, but the global counter is not
> incremented. Since the child also references zero pages, the global
> counter should be updated as well. Otherwise, during zero-page unmerge,
> both the global and per-process counters are decremented, causing the
> global counter to become inconsistent.
>
> To fix this, ksm_merging_pages and ksm_rmap_items are reset to 0
> during fork, and the global ksm_zero_pages counter is updated with the
> per-process ksm_zero_pages value inherited by the child. This ensures
> that KSM statistics remain accurate and reflect the activity of each
> process correctly.
>
> Fixes: 7609385337a4 ("ksm: count ksm merging pages for each process")
> Fixes: cb4df4cae4f2 ("ksm: count allocated ksm rmap_items for each process")
> Fixes: e2942062e01d ("ksm: count all zero pages placed by KSM")
> cc: stable@vger.kernel.org # v6.6
> Signed-off-by: Donet Tom <donettom@linux.ibm.com>
> ---
> include/linux/ksm.h | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/ksm.h b/include/linux/ksm.h
> index 22e67ca7cba3..067538fc4d58 100644
> --- a/include/linux/ksm.h
> +++ b/include/linux/ksm.h
> @@ -56,8 +56,14 @@ static inline long mm_ksm_zero_pages(struct mm_struct *mm)
> 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))
> + if (mm_flags_test(MMF_VM_MERGEABLE, oldmm)) {
> + long nr_ksm_zero_pages = atomic_long_read(&mm->ksm_zero_pages);
> +
> + mm->ksm_merging_pages = 0;
> + mm->ksm_rmap_items = 0;
> + atomic_long_add(nr_ksm_zero_pages, &ksm_zero_pages);
> __ksm_enter(mm);
That LGTM. KSM is all weird in combination with fork(), but that's
something for another day to improve I guess.
Acked-by: David Hildenbrand <david@redhat.com>
--
Cheers
David / dhildenb
next prev parent reply other threads:[~2025-09-17 10:38 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-15 15:03 [PATCH v2 0/3] mm/ksm: Fix incorrect accounting of KSM counters during fork Donet Tom
2025-09-15 15:03 ` [PATCH v2 1/3] mm/ksm: Fix incorrect KSM counter handling in mm_struct " Donet Tom
2025-09-15 23:42 ` Andrew Morton
2025-09-16 2:14 ` Joe Perches
2025-09-16 2:54 ` Andrew Morton
2025-09-16 4:33 ` Sasha Levin
2025-09-16 4:41 ` Andrew Morton
2025-09-16 12:45 ` Sasha Levin
2025-09-16 5:50 ` Donet Tom
2025-09-17 10:38 ` David Hildenbrand [this message]
2025-09-17 12:27 ` Chengming Zhou
2025-09-15 15:03 ` [PATCH v2 2/3] selftests/mm: Added fork inheritance test for ksm_merging_pages counter Donet Tom
2025-09-17 13:15 ` David Hildenbrand
2025-09-17 14:45 ` Donet Tom
2025-09-15 15:03 ` [PATCH v2 3/3] selftests/mm: Added fork test to verify global ksm_zero_pages counter behavior Donet Tom
2025-09-17 13:19 ` David Hildenbrand
2025-09-17 14:47 ` Donet Tom
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6dde7c50-a222-4984-bb69-07ace724f161@redhat.com \
--to=david@redhat.com \
--cc=aboorvad@linux.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=chengming.zhou@linux.dev \
--cc=donettom@linux.ibm.com \
--cc=giorgitchankvetadze1997@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=richard.weiyang@gmail.com \
--cc=ritesh.list@gmail.com \
--cc=stable@vger.kernel.org \
--cc=xu.xin16@zte.com.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).