Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp407080lqo; Wed, 8 May 2024 03:38:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWuQL4WP2BhtJVrcjucKNkQzgTdYHTLTK9PE/pzqauSbRN69i4fK/on/TTU49qQFQPuXtT1lZgzFYt7dq4G6GMgxcp5rL5N13pzzCMhng== X-Google-Smtp-Source: AGHT+IGClClPw5jNO4cFLpP3Y5bE4jI8SyS3vHu761IAU583XOM+6fTTEn+Kb2RcOI4vxGxYaNqk X-Received: by 2002:a50:9f87:0:b0:56e:2b31:b111 with SMTP id 4fb4d7f45d1cf-5731d9cd3cemr1319471a12.7.1715164705920; Wed, 08 May 2024 03:38:25 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715164705; cv=pass; d=google.com; s=arc-20160816; b=Nag/3BpKDiVYQnJMD/TRLjCOiJyuwbxKqEmeKcl/7wr0pzr6qRKeHznVbahQaoWtna zqxMeihVdpHjDi90oo/1vxxgP+LOdJe2KA8RKkXcaCa8VtQmOICy53feUz2U1XVuKfsp KfKRWCothrKYhUc0kKUpd1FH9243uGXRcV3068Gfgxebj2nyrZ/LOkA90WCQILzr1gU2 mDMN7dtvueIKnZSDSfXv3a9I/2YbKejf7I6TWp0qQQ7p92w8CRHJN4IF/JrSNEuZGPV0 ToQV6ifmxdrw+UgxutbALuvGyjcXY2XMj0Yb02oUUrrGNq392b9+Aw9MCZV3vzs6Mumt 4NqA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature:message-id; bh=5eHqA7UOBweZX4Vrf/qg7QA6MfSTlCuOrDrWzen+vio=; fh=BunSfNrUVSUgMv8Ky7XbYx3tKRRASwGn74uzvOv8pZY=; b=lY3zV8ZnMtYBwInMIRP5xoZ2MPuvVze38Jg8n1ZtQ96UMqPlDdMezgIbizWQ/OZ0i9 +dQ+3FVKptH6t77N+xMH+Mmnn0AErr3+N+psKvkcS3T1DlS1xqEZ/sw0FfnX1Fb5sE05 XKYz+C7OGtZdd/A+XauuxVCUFF01xQqWl11eCPBIWJ70Vosnfa4D98gHlFpwDrXDqFG2 xINnJeN/eONeOnQofoEWmkhVMzTIypmMVuE8B5iZeTktv7Skv41os/shaZOcXcwS2LL4 7CAbJCy0B5QeCP7KxDPZwGhWPUo+F+ljktFrQRc3TH99zlRGxgS63FO7WNskBOnbRmT8 uVwA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="Edwc/9s3"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-173068-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173068-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id t23-20020a50ab57000000b0056e861a30dasi6812195edc.187.2024.05.08.03.38.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 03:38:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-173068-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="Edwc/9s3"; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-173068-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173068-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 78B8F1F23200 for ; Wed, 8 May 2024 10:38:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DD62080C16; Wed, 8 May 2024 10:38:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Edwc/9s3" Received: from out-187.mta0.migadu.com (out-187.mta0.migadu.com [91.218.175.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FDE42836D for ; Wed, 8 May 2024 10:38:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.187 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715164698; cv=none; b=j4dGOMXY/QpyoSCBLvySn06Y774J+uhkJ8V/kpZdWZRMT8Iypn8X9erCQMzoOV42g86t5WchTt9TUdLlWdV+afd1JuPXwin5PtqOjUFvM3+kbn7ZbJ1ekiVjsP5ofMSEacmDyEekECfkMMyV4uv49AdyL5h6BbP/TOutDMSkNW0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715164698; c=relaxed/simple; bh=aaYeaMdasKaspZebsx+IVEbvrwdp4b2JzRsLI+LVbtg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=qpltzl+Iy5GttO4mz6BkO5uM4EnA0NrGHWhJ08pFQdXfKCH1GNOjUvP8ilm5Zw65tjQ2/eZ9ahB6WPdrTY/BhJh7KeqcVz/6J/iCADYhCvmdg0LZbG3ePse0oqRDO+zdGPAe/o5rqVe79UGIXZHp+3oRlrOy2Yn/Kz0dSgpSVLc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Edwc/9s3; arc=none smtp.client-ip=91.218.175.187 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 Message-ID: <45bdacaf-29e9-488f-a4e8-2b5ec891cfc4@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715164692; 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=5eHqA7UOBweZX4Vrf/qg7QA6MfSTlCuOrDrWzen+vio=; b=Edwc/9s3GpdgxKvV/VSPbUnQYAZI0n2z411AMthQyYUD2RRB9vj71UInEVmevYLsEMN7Q9 NBvNXthWvS8THUBzFxoCSAUe+4KC59uC++8xQSDeh6K9DW2bgGbEK7UmW9ZsIlq8JL98KS zffZlxZFFbEyHGvDqzSgSkyg7f711+8= Date: Wed, 8 May 2024 18:37:44 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH 2/4] mm/ksm: fix ksm_zero_pages accounting Content-Language: en-US To: Andrew Morton , David Hildenbrand , Stefan Roesch , xu xin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com References: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> <20240508-b4-ksm-counters-v1-2-e2a9b13f70c5@linux.dev> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou In-Reply-To: <20240508-b4-ksm-counters-v1-2-e2a9b13f70c5@linux.dev> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 2024/5/8 17: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. > Fixes: e2942062e01d ("ksm: count all zero pages placed by KSM") Fixes: 6080d19f0704 ("ksm: add ksm zero pages for each process") > Signed-off-by: Chengming Zhou > --- > 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); > +} > + > +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..6e0dca3cecf3 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", get_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 + get_ksm_zero_pages()) * PAGE_SIZE - > ksm_rmap_items * sizeof(struct ksm_rmap_item); > > return sysfs_emit(buf, "%ld\n", general_profit); >