Re: [PATCH 1/2] mm/ksm: calculate ksm_process_profit more accurately
From: xu.xin16
Date: Tue Jun 03 2025 - 23:54:03 EST
> The general_profit_show() only considers ksm_pages_sharing,
> whereas ksm_process_profit() accounts for both ksm_pages_sharing
> and ksm_pages_shared for each process. This discrepancy leads to
> the sum of ksm_process_profit() across all processes not being equal
> to general_profit_show().
>
> Fixes: 7609385337a4 ("ksm: count ksm merging pages for each process")
> Signed-off-by: Longlong Xia <xialonglong@xxxxxxxxxx>
> ---
> mm/ksm.c | 20 ++++++++------------
> 1 file changed, 8 insertions(+), 12 deletions(-)
Thanks for you report, but we'd better not change the definition of ksm_merging_pages which means
all pages involved in ksm merging (pages_sharing + pages_shared), and Do not rename the existing
of interface, which breaks user-space tools.
If we reallt want a more pricise profit of a process , I suggest:
- 1) Add a variable 'ksm_pages_sharing' in mm_struct for processes.
- 2) Refine the implementation of ksm_process_profit().
Thanks~
>
> diff --git a/mm/ksm.c b/mm/ksm.c
> index 8583fb91ef13..fa4e1618b671 100644
> --- a/mm/ksm.c
> +++ b/mm/ksm.c
> @@ -824,12 +824,10 @@ static void remove_node_from_stable_tree(struct ksm_stable_node *stable_node)
> hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
> if (rmap_item->hlist.next) {
> ksm_pages_sharing--;
> + rmap_item->mm->ksm_merging_pages--;
> trace_ksm_remove_rmap_item(stable_node->kpfn, rmap_item, rmap_item->mm);
> - } else {
> + } else
> ksm_pages_shared--;
> - }
> -
> - rmap_item->mm->ksm_merging_pages--;