From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 541C0C25B10 for ; Mon, 13 May 2024 06:18:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B02DD6B026F; Mon, 13 May 2024 02:17:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB3246B0270; Mon, 13 May 2024 02:17:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97A976B0271; Mon, 13 May 2024 02:17:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7AE466B026F for ; Mon, 13 May 2024 02:17:59 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CC9F71A0C77 for ; Mon, 13 May 2024 06:17:58 +0000 (UTC) X-FDA: 82112367036.20.2DFE0C9 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf22.hostedemail.com (Postfix) with ESMTP id D6D6CC000F for ; Mon, 13 May 2024 06:17:56 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DPt59pkM; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715581077; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WTzXHgcN/78bzWT5jsvUxiYaU7ZcHofB3YgYoaDgwrE=; b=YWpOCA1rTxkxhudgIIoDCH5n7BFZANJ9wP1txeJ0yFh7XNgKkL37cml5R301Txn0gtN2hu 9tgqEw7V++nPwrep37tOmMHj40UocrIsqYRMNy6gejo/3EXUOHTby9LB22V5WOVoLtVT/F nXcIrAwd59YP2VumTMZsxBMY0ppDfY8= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DPt59pkM; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of chengming.zhou@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715581077; a=rsa-sha256; cv=none; b=DF1nE3vVwFDF/vbLgkgTmNmIEQHr2ipHAm9bldQPQI2sBhm8KFnRSVbXm/yYUpFoWYqynz kvGTwIb2pYhV8PYUuuwO3yjHZm7iJa3UaAwu2FkmbWsAjvaYGRBW/MjzriXMlxok8kCWxZ 1SYT2juVcSktGHSz6su9oXdHMgCvTp0= Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715581074; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WTzXHgcN/78bzWT5jsvUxiYaU7ZcHofB3YgYoaDgwrE=; b=DPt59pkMD7wKr6zggyktQX0VSsBOPmTau18lCAfLQ9tLCeYYml9wT+bfmck1uoui805OH6 GcQ/T9zrbVbwIm0j94EBm1TzrxqWxagKKIwnSCudMsB7TOCrJgiO18894hatcewo+21OEF KAz36oogpU23s6Q8m105sPDmcrpWjw8= Date: Mon, 13 May 2024 14:17:41 +0800 MIME-Version: 1.0 Subject: Re: [PATCH v2 2/2] mm/ksm: fix ksm_zero_pages accounting To: xu xin Cc: aarcange@redhat.com, akpm@linux-foundation.org, david@redhat.com, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, shr@devkernel.io, xu.xin16@zte.com.cn, zhouchengming@bytedance.com, si.hao@zte.com.cn References: <20240513-b4-ksm-counters-v2-2-f2520183a8ca@linux.dev> <20240513060029.651050-1-xu.xin16@zte.com.cn> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou In-Reply-To: <20240513060029.651050-1-xu.xin16@zte.com.cn> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: D6D6CC000F X-Stat-Signature: r6qubjc5a8d8tqzyue3f7ns36d3dss16 X-Rspam-User: X-Rspamd-Server: rspam04 X-HE-Tag: 1715581076-336188 X-HE-Meta: U2FsdGVkX1+gVm2H/t1Ss4kMcUrt8Q2eOuTsScH86+8CTzofyFb7Y1Xtc/J8382fZUTodaAp/XhoBLnot/r3qheH2CsrSnkZAdnH2oBVqderpwiCV1DA2Nf5xnl84DN0zIhQf/xn3dMWfvU+YlYY5WtwW9mZ3O5dgL8P3KsFV/jPdc1QAbDqmfS51vD/0M8FlmEh4gKcugihWCL/JfXQBECUwtASBY6oeyaWGtJsjmzaVeUvKZyBZ1a7QR+6eZaUxlQ9qGqG76He6n9uYZrTufyeRpxCD2ui2x5/S/+Gwm4BlxUGW3BMsvitEWXtHbPVHsdX+tSmVRgmobJrOHDvQj+lt391/pMxgD4ZYoJ4swmiDnUvGtRQHety4xX7JnlIe1VU02rnX65pOK1BkU15pNpUMV5rP3mdv2JA6YAu5OXhg2lGpA3QhE4ztcygoDX1rqE2XWXH6NKOnQpI5UuBFK1/b3tXEMHE1THVivAGiMURIsJdbREiY49UWGqwPZ3RhMfUFcdNNQ7s7Ol8E4lxnUl7wnED5L1hauHf07voBP3ywA1fKXk9/rq1+md06TVDbQgLId+g0oI20MO8kjXolpmSbhSjQ1b9roIs4T5aeSx3zirxHakYG5seTeiOuis88FrhmZp6ihuahcIHb1tKn2NJMQBJqnJmFS0lcD4HZ/AJQ5l+O/yEW2VIZ2fkUIjy8VpuP/sZJgEdDj90uzbBM8Ff0Te8sfhRNG2H1hR7YYhyJOQ8oNkzAcfvg4ALvlg+FuBusYLI0gs3RSLULLacK/cxEAW4/HqGVF62GoFSnhw8aFPtyIMu+V2n28sDOxFj0Qi+7E86h2jRlTOfUchWdJwKjxi45IxIu55Ca5owKFci1i3OTqoyOd+KdfEpK9wnheGBqNu36XMf1d60Bmu6OkGxbOw5mTYx+8darQAjpkzzt+Oh7CiHlROhuQ+6oD/nzhNtrn1gITs6hQALboG 6MQ8C42m jPvzTyWmOm2PK7bWW59PFGgABq7D6Rkx1QCsgLyXqxtrRCNltoX0WARaiT8dlBCP1u8PJWqAgILLxnEYBxsMXZfbMSoESbisY3qMcin1mTl7LePbx4EI/yiVxVR2iOtXjml6b8paMjR5DpeUIJIOqiho22EpR5Vjf4HApXWkMDzUWtzVmM80j7JJ9jRdKHjQGVJhMCxrk0rwSuzZ/QgZrXTwpxJE15K8YSmDY X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 2024/5/13 14:00, xu xin 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. > > "cant protected" -> "can't be protected". Right, will fix. > > But It's better to say "where there is no any accessing protection of > ksm_zero_pages" because ksmd mutex is to protect the flag of ksm_run, not to > protect the counters of KSM. Ah, I thought the introduced ksm_zero_pages counters were protected by ksmd mutex like all other ksm counters, no? But the difference with those ksm counters is that ksm_zero_pages could be changed by the page table side operations, which can't take ksmd mutex, this is the reason why we need to use atomic variables. Thanks. > > > Anyway, The following code looks OK to me. >> >> 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. >> >> Fixes: e2942062e01d ("ksm: count all zero pages placed by KSM") >> Fixes: 6080d19f0704 ("ksm: add ksm zero pages for each process") >> Acked-by: David Hildenbrand >> Signed-off-by: Chengming Zhou >> --- >> fs/proc/base.c | 2 +- >> include/linux/ksm.h | 17 ++++++++++++++--- >> include/linux/mm_types.h | 2 +- >> mm/ksm.c | 11 +++++------ >> 4 files changed, 21 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..11690dacd986 100644 >> --- a/include/linux/ksm.h >> +++ b/include/linux/ksm.h >> @@ -33,16 +33,27 @@ 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 mm_ksm_zero_pages(struct mm_struct *mm) >> +{ >> + return atomic_long_read(&mm->ksm_zero_pages); >> +} >> + >> static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) >> { >> if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) >> diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h >> index 24323c7d0bd4..af3a0256fa93 100644 >> --- a/include/linux/mm_types.h >> +++ b/include/linux/mm_types.h >> @@ -985,7 +985,7 @@ struct mm_struct { >> * Represent how many empty pages are merged with kernel zero >> * pages when enabling KSM use_zero_pages. >> */ >> - unsigned long ksm_zero_pages; >> + atomic_long_t ksm_zero_pages; >> #endif /* CONFIG_KSM */ >> #ifdef CONFIG_LRU_GEN_WALKS_MMU >> struct { >> diff --git a/mm/ksm.c b/mm/ksm.c >> index 0f9c491552ff..6f461411d070 100644 >> --- a/mm/ksm.c >> +++ b/mm/ksm.c >> @@ -296,7 +296,7 @@ static bool ksm_use_zero_pages __read_mostly; >> static bool ksm_smart_scan = true; >> >> /* The number of zero pages which is placed by KSM */ >> -unsigned long ksm_zero_pages; >> +atomic_long_t ksm_zero_pages = ATOMIC_LONG_INIT(0); >> >> /* The number of pages that have been skipped due to "smart scanning" */ >> static unsigned long ksm_pages_skipped; >> @@ -1429,8 +1429,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, >> * the dirty bit in zero page's PTE is set. >> */ >> newpte = pte_mkdirty(pte_mkspecial(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot))); >> - ksm_zero_pages++; >> - mm->ksm_zero_pages++; >> + ksm_map_zero_page(mm); >> /* >> * We're replacing an anonymous page with a zero page, which is >> * not anonymous. We need to do proper accounting otherwise we >> @@ -3373,7 +3372,7 @@ static void wait_while_offlining(void) >> #ifdef CONFIG_PROC_FS >> long ksm_process_profit(struct mm_struct *mm) >> { >> - return (long)(mm->ksm_merging_pages + mm->ksm_zero_pages) * PAGE_SIZE - >> + return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PAGE_SIZE - >> mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); >> } >> #endif /* CONFIG_PROC_FS */ >> @@ -3662,7 +3661,7 @@ KSM_ATTR_RO(pages_skipped); >> static ssize_t ksm_zero_pages_show(struct kobject *kobj, >> struct kobj_attribute *attr, char *buf) >> { >> - return sysfs_emit(buf, "%ld\n", ksm_zero_pages); >> + return sysfs_emit(buf, "%ld\n", atomic_long_read(&ksm_zero_pages)); >> } >> KSM_ATTR_RO(ksm_zero_pages); >> >> @@ -3671,7 +3670,7 @@ static ssize_t general_profit_show(struct kobject *kobj, >> { >> long general_profit; >> >> - general_profit = (ksm_pages_sharing + ksm_zero_pages) * PAGE_SIZE - >> + general_profit = (ksm_pages_sharing + atomic_long_read(&ksm_zero_pages)) * PAGE_SIZE - >> ksm_rmap_items * sizeof(struct ksm_rmap_item); >> >> return sysfs_emit(buf, "%ld\n", general_profit); >> >> -- >> 2.45.0 >>