Linux-mm Archive mirror
 help / color / mirror / Atom feed
From: Chengming Zhou <chengming.zhou@linux.dev>
To: David Hildenbrand <david@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Stefan Roesch <shr@devkernel.io>, xu xin <xu.xin16@zte.com.cn>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	zhouchengming@bytedance.com
Subject: Re: [PATCH 2/4] mm/ksm: fix ksm_zero_pages accounting
Date: Wed, 8 May 2024 21:52:20 +0800	[thread overview]
Message-ID: <7d8ad19a-dae8-4783-8f49-227ef8ca4857@linux.dev> (raw)
In-Reply-To: <7cac6762-4486-4c42-885d-dd5715eb6ba4@redhat.com>

On 2024/5/8 20:36, David Hildenbrand wrote:
> On 08.05.24 11:55, Chengming Zhou wrote:
>> We normally ksm_zero_pages++ in ksmd when page is merged with zero page,
>> but ksm_zero_pages-- is done from page tables side, which can't protected
>> by the ksmd mutex.
>>
>> So we can read very exceptional value of ksm_zero_pages in rare cases,
>> such as -1, which is very confusing to users.
>>
>> Fix it by changing to use atomic_long_t, and the same case with the
>> mm->ksm_zero_pages.
>>
>> Signed-off-by: Chengming Zhou <chengming.zhou@linux.dev>
>> ---
>>   fs/proc/base.c           |  2 +-
>>   include/linux/ksm.h      | 22 +++++++++++++++++++---
>>   include/linux/mm_types.h |  2 +-
>>   mm/ksm.c                 | 11 +++++------
>>   4 files changed, 26 insertions(+), 11 deletions(-)
>>
>> diff --git a/fs/proc/base.c b/fs/proc/base.c
>> index 18550c071d71..72a1acd03675 100644
>> --- a/fs/proc/base.c
>> +++ b/fs/proc/base.c
>> @@ -3214,7 +3214,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
>>       mm = get_task_mm(task);
>>       if (mm) {
>>           seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
>> -        seq_printf(m, "ksm_zero_pages %lu\n", mm->ksm_zero_pages);
>> +        seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm));
>>           seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
>>           seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
>>           mmput(mm);
>> diff --git a/include/linux/ksm.h b/include/linux/ksm.h
>> index 52c63a9c5a9c..bfc2cf756b0d 100644
>> --- a/include/linux/ksm.h
>> +++ b/include/linux/ksm.h
>> @@ -33,16 +33,32 @@ void __ksm_exit(struct mm_struct *mm);
>>    */
>>   #define is_ksm_zero_pte(pte)    (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
>>   -extern unsigned long ksm_zero_pages;
>> +extern atomic_long_t ksm_zero_pages;
>> +
>> +static inline void ksm_map_zero_page(struct mm_struct *mm)
>> +{
>> +    atomic_long_inc(&ksm_zero_pages);
>> +    atomic_long_inc(&mm->ksm_zero_pages);
>> +}
>>     static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t pte)
>>   {
>>       if (is_ksm_zero_pte(pte)) {
>> -        ksm_zero_pages--;
>> -        mm->ksm_zero_pages--;
>> +        atomic_long_dec(&ksm_zero_pages);
>> +        atomic_long_dec(&mm->ksm_zero_pages);
>>       }
>>   }
>>   +static inline long get_ksm_zero_pages(void)
>> +{
>> +    return atomic_long_read(&ksm_zero_pages);
>> +}
> 
> I suggest inlining that one. The naming of the function also is a bit inconsistent staring at the others.

Good point, I will inline it.

> 
>> +
>> +static inline long mm_ksm_zero_pages(struct mm_struct *mm)
>> +{
>> +    return atomic_long_read(&mm->ksm_zero_pages);
>> +}
>> +
> 
> Apart from that LGTM
> 
> Acked-by: David Hildenbrand <david@redhat.com>
> 

Thanks!


  reply	other threads:[~2024-05-08 13:52 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-08  9:55 [PATCH 0/4] mm/ksm: fix some accounting problems Chengming Zhou
2024-05-08  9:55 ` [PATCH 1/4] mm/ksm: fix ksm_pages_scanned accounting Chengming Zhou
2024-05-08 10:35   ` Chengming Zhou
2024-05-08 12:32     ` David Hildenbrand
2024-05-08  9:55 ` [PATCH 2/4] mm/ksm: fix ksm_zero_pages accounting Chengming Zhou
2024-05-08 10:37   ` Chengming Zhou
2024-05-08 12:36   ` David Hildenbrand
2024-05-08 13:52     ` Chengming Zhou [this message]
2024-05-08  9:55 ` [PATCH 3/4] mm/ksm: union hlist_node with list_head in struct ksm_stable_node Chengming Zhou
2024-05-08  9:55 ` [PATCH 4/4] mm/ksm: calculate general_profit more accurately Chengming Zhou
2024-05-08 10:24 ` [PATCH 0/4] mm/ksm: fix some accounting problems David Hildenbrand
2024-05-08 10:28   ` Chengming Zhou

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=7d8ad19a-dae8-4783-8f49-227ef8ca4857@linux.dev \
    --to=chengming.zhou@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=david@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=shr@devkernel.io \
    --cc=xu.xin16@zte.com.cn \
    --cc=zhouchengming@bytedance.com \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).