From nobody Wed Dec 17 05:55:53 2025 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) (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 A10957D40E for ; Wed, 8 May 2024 09:56:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162187; cv=none; b=kjA1ph5vWjZfIBJySZ2fXNzR/WBiC+HJPn6ugmbd/CBbcfnxKbHEg64QoidHpavgXQFPgJZQYPFyKAESBgwpjs6fFjVNKLEm5n66sVUmNFHynJhNFxizx9x8bOTkmGHdU8RaJfCIykce+zbglzme2PR5jG8bsMEdGLliWHbPphg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162187; c=relaxed/simple; bh=snJN4Df6DU019UCJtAmXu+NvV52neCa6WX3l+iJA2Lw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bur26K9d9d8UQE6XtU19v6ZHesb+hxO+F24lwrlqCbop5rtQtJeNGqxcO0+8uTD4lHTKUhe9+s9v0KBT7XNm10SpLg9WZgPnqVXzWlqYKNhYFzBnDKrvEVaRkWSFg0vJ/OoJMVFBA29EtW09l/RwTKwQTqsVkJlK09XMxaue5k4= 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=C4MV5gEp; arc=none smtp.client-ip=95.215.58.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="C4MV5gEp" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715162183; 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=kkuupc/bTWDBM/qgtKXvWiw928oiixEs1ZTUcK3CiX4=; b=C4MV5gEpzMK+SjA3+P6MBs1obUWGxx4y+XHTher0dAqLtRXL2+27HAevrFHrkd1gEoHvdq MbWsBs74UJrR7m8btrI/nsWzxPm9+vpq/MJ+TFqO1TjatRLQW5jI91sxApzSnfQmrOTltl DdviWIuuyK0ImKOuOX1iDQKJ6ycJX+I= From: Chengming Zhou Date: Wed, 08 May 2024 17:55:38 +0800 Subject: [PATCH 1/4] mm/ksm: fix ksm_pages_scanned accounting Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240508-b4-ksm-counters-v1-1-e2a9b13f70c5@linux.dev> References: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> In-Reply-To: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> To: Andrew Morton , David Hildenbrand , Stefan Roesch , xu xin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1715162174; l=1280; i=chengming.zhou@linux.dev; s=20240508; h=from:subject:message-id; bh=snJN4Df6DU019UCJtAmXu+NvV52neCa6WX3l+iJA2Lw=; b=sii/wcJR6QpIF+eWSdTOUi950cTGX4hLZNmHf4BI4ooy5PMU0VGDAd1Dg/mYKZpIk3WVRH171 nEXbbd5ZrOcARiEU5K4yf9CsPeKr1ytC/0lQeZAUGjvu0Qf2mIXD4M7 X-Developer-Key: i=chengming.zhou@linux.dev; a=ed25519; pk=kx40VUetZeR6MuiqrM7kPCcGakk1md0Az5qHwb6gBdU= X-Migadu-Flow: FLOW_OUT During testing, I found ksm_pages_scanned is unchanged although the scan_get_next_rmap_item() did return valid rmap_item that is not NULL. The reason is the scan_get_next_rmap_item() will return NULL after a full scan, so ksm_do_scan() just return without accounting of the ksm_pages_scanned. Fix it by just putting ksm_pages_scanned accounting in that loop, and it will be accounted more timely if that loop would last for a long time. Signed-off-by: Chengming Zhou Acked-by: David Hildenbrand --- mm/ksm.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index e1034bf1c937..0f9c491552ff 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2753,18 +2753,16 @@ static void ksm_do_scan(unsigned int scan_npages) { struct ksm_rmap_item *rmap_item; struct page *page; - unsigned int npages =3D scan_npages; =20 - while (npages-- && likely(!freezing(current))) { + while (scan_npages-- && likely(!freezing(current))) { cond_resched(); rmap_item =3D scan_get_next_rmap_item(&page); if (!rmap_item) return; cmp_and_merge_page(page, rmap_item); put_page(page); + ksm_pages_scanned++; } - - ksm_pages_scanned +=3D scan_npages - npages; } =20 static int ksmd_should_run(void) --=20 2.45.0 From nobody Wed Dec 17 05:55:53 2025 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (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 6CDF67BAF0 for ; Wed, 8 May 2024 09:56:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162195; cv=none; b=XPzoLEneKtRbm01VwFbyb8J8dhENjnx6POcFOnBjO6CaC5ggFbF7OgADn+Z/hSxZ4QzkaE6PhXxVWE7O8UVhzmTecowTvWnaEzRYrqesipLWJDC7jY2BNYDtiZN7KK21NDW+WRHCSa9khlB0ZyVZKR01HBFzJbmSY5wy/0gvQYA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162195; c=relaxed/simple; bh=zT3260cOojJ9YkJT42xnisTrezwzaldkD24iYYJXwwo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QiiqpQCjWceF4DGQR5P28yP5td/xhrTWLJDFz+3JPFZ6Qfmu0mOGQba52Yqx0SZPGkKHMWXGNsD8FU5JdcS6LCZxgnf9WnCyT/y3zJDtyIJiSTVTMqdGA3K8qL97Lkeq2jkDlT4P7D/7goucpK+/BkJ2VB9T+jakVlNsWJjR0xo= 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=sOWryHtG; arc=none smtp.client-ip=95.215.58.180 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="sOWryHtG" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715162191; 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=2bLQ4gptvM3L1QRVzQ/kljeJWLtVefD+oipqeUeshP8=; b=sOWryHtG3kXzk2QHXqC3vWzkFP+FlHnR4lQWZKayTg4D2xGZnkjYjf+0j5aWmIgXNvd7zT 2QTe1nDeX5MKcr4QGAkBy3rzDwzo0dDxHfeOb8oJM+xxOXS9rkqgLYVByDvnSqwIxXy2bs iLs3tqru3cEhRFU15/Fg6ROIvd2+Gv8= From: Chengming Zhou Date: Wed, 08 May 2024 17:55:39 +0800 Subject: [PATCH 2/4] mm/ksm: fix ksm_zero_pages accounting Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240508-b4-ksm-counters-v1-2-e2a9b13f70c5@linux.dev> References: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> In-Reply-To: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> To: Andrew Morton , David Hildenbrand , Stefan Roesch , xu xin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1715162174; l=4898; i=chengming.zhou@linux.dev; s=20240508; h=from:subject:message-id; bh=zT3260cOojJ9YkJT42xnisTrezwzaldkD24iYYJXwwo=; b=pw9F/6MfqwjmEILvTY9Kw8kVwDgxLbZH1bNRvo778ZsSVX+2PHfvvqg43hSoNnMZZzZZRsU89 AoIeAp0psC8AQfATubxnDjo3cEl5nWUpcfboQr/FSoASz3WAK8siagH X-Developer-Key: i=chengming.zhou@linux.dev; a=ed25519; pk=kx40VUetZeR6MuiqrM7kPCcGakk1md0Az5qHwb6gBdU= X-Migadu-Flow: FLOW_OUT 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 Acked-by: David Hildenbrand --- 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, stru= ct pid_namespace *ns, mm =3D 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)) =20 -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); +} =20 static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t p= te) { 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); } } =20 +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 =3D true; =20 /* The number of zero pages which is placed by KSM */ -unsigned long ksm_zero_pages; +atomic_long_t ksm_zero_pages =3D ATOMIC_LONG_INIT(0); =20 /* 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, s= truct page *page, * the dirty bit in zero page's PTE is set. */ newpte =3D 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); =20 @@ -3671,7 +3670,7 @@ static ssize_t general_profit_show(struct kobject *ko= bj, { long general_profit; =20 - general_profit =3D (ksm_pages_sharing + ksm_zero_pages) * PAGE_SIZE - + general_profit =3D (ksm_pages_sharing + get_ksm_zero_pages()) * PAGE_SIZE= - ksm_rmap_items * sizeof(struct ksm_rmap_item); =20 return sysfs_emit(buf, "%ld\n", general_profit); --=20 2.45.0 From nobody Wed Dec 17 05:55:53 2025 Received: from out-171.mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (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 46D197C0A1 for ; Wed, 8 May 2024 09:56:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162198; cv=none; b=j+JpLm9kL0MYRR92syQ5rvF0N6YCmLlPLQ4y4Mz3y15ZrLv2yPk8Sh5q6k//XlT/+iowouL27cecPYMVsdIWIy29sW90r6iMMl3GkUU93eZovt+RU6d7sVVzRxzg9LIKI6Ko6fFfdcT2y+MgzJGHDOq498NtuQix50PiMxxRUxQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162198; c=relaxed/simple; bh=9T02FTklfVUMR7drM2v/kaRCwGZRX7z9/r7McdEQClU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=SUQskFnd3Oc3ZgkGvXrB2TL0WwTwQcq5oUhphydIiEdHLZLPld+53hbSN2My1Och/HDDyVSxsIPyzIxwEZy974WyX4tyH1Drtu6ldOiVnuMrfbRf8HJ+5NW5T4AXsWQafKmTHck2b6JPam7N4mT35lZRlE2jalROIkmoUTarnJY= 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=TxoxqQr6; arc=none smtp.client-ip=95.215.58.171 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="TxoxqQr6" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715162195; 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=7esPxFrHeeBhwH9TcjMg5ONohBvtI4PjIYPbV7umvFg=; b=TxoxqQr6IkPtfChwoT38ktZdFciaSGa3+CEFn0j3iaJM2QQ9PI0C6oA8r+CCO5OyIuLQKk 5iZps/f6cdsTPnSvWzVrdidclmBnSAFZwtFbVxwMIp3x7I4ElQut3qTe6idS+3e3QHKs8Y ZZi2Ih6oQEmzqFu75yMvT9mYIfnqKDI= From: Chengming Zhou Date: Wed, 08 May 2024 17:55:40 +0800 Subject: [PATCH 3/4] mm/ksm: union hlist_node with list_head in struct ksm_stable_node Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240508-b4-ksm-counters-v1-3-e2a9b13f70c5@linux.dev> References: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> In-Reply-To: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> To: Andrew Morton , David Hildenbrand , Stefan Roesch , xu xin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1715162174; l=892; i=chengming.zhou@linux.dev; s=20240508; h=from:subject:message-id; bh=9T02FTklfVUMR7drM2v/kaRCwGZRX7z9/r7McdEQClU=; b=YuJNdQkZ4hOohg/ChgI1eEPAZsFyNzgMZ72nhyyjbRyA7QWsyafXvdi4d09ef4lYNSfT+KoKj QrNHFhfTfVcBUsmVogPymqqloYcrkQfyOf2Imn1RNmK07HWkiZyl0Ux X-Developer-Key: i=chengming.zhou@linux.dev; a=ed25519; pk=kx40VUetZeR6MuiqrM7kPCcGakk1md0Az5qHwb6gBdU= X-Migadu-Flow: FLOW_OUT The ksm_stable_node->hlist_node is used for linking on chain node hlist, and ksm_stable_node->list_head is used for linking on migration list. We always stable_node_dup_del() first before adding it on migration list or list_del() first from migration list before adding it on stable tree. So we can union hlist_node with list_head to save some memory. Signed-off-by: Chengming Zhou --- mm/ksm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/ksm.c b/mm/ksm.c index 6e0dca3cecf3..87ffd228944c 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -162,7 +162,7 @@ struct ksm_stable_node { struct rb_node node; /* when node of stable tree */ struct { /* when listed for migration */ struct list_head *head; - struct { + union { struct hlist_node hlist_dup; struct list_head list; }; --=20 2.45.0 From nobody Wed Dec 17 05:55:53 2025 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) (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 9C8A67F470 for ; Wed, 8 May 2024 09:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162202; cv=none; b=K255udX1fGlqMXLa0csu9YpbTKgA/AqRzU9KTrLiv5mOrZaay+79ksa1dcBLop8YgiWV3mhGW1p1Fqvtb9tma9dcev1uDXEYR1Wl1LSwqCWiTZkDNBhyZrOalXHubKKpv/eaAIUUrim8ay7+TgkUVHbwBF/bB1fs+KkYEsv1DX0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715162202; c=relaxed/simple; bh=ss7DfeQ3iIOHVfPafgYrAJPDTsUQSQx+kzwMyH5OwKo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=M4+sQaJ3msLKEBFEm2BVb4/68Mtjn12yjWYfYEi533tfclgZz61dgo1o30tevf0OQ+jdnrn+5enrv4b7oy388dvX+jqUZL5iHS7JBQWLH+qYNUitKPLJDoD2YdCPJYL4uvTaTScI1611sQb30+b1z7j5ZqapEdvHZD5GpzmXFQ0= 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=kjcNAsj1; arc=none smtp.client-ip=95.215.58.170 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="kjcNAsj1" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1715162198; 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=FsicJzPzSYjb9EaWqN7pAslrOCGTYHDq5WxD1KJCe/4=; b=kjcNAsj1cuN9Enofn4z93wcbSAjYLywjhJjN77ua1sIREN682XM4/TUP7KT8khp+C1Lxo9 /sUFpeSD3NLJqtnOZwJbyj9x/lzghKhbvs6VQjYNuH7/a4A3y3fzWLWWbcQd8hcfO2GpIG /os+5isiO/1zThfvmUzmeT4jB804zKw= From: Chengming Zhou Date: Wed, 08 May 2024 17:55:41 +0800 Subject: [PATCH 4/4] mm/ksm: calculate general_profit more accurately Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240508-b4-ksm-counters-v1-4-e2a9b13f70c5@linux.dev> References: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> In-Reply-To: <20240508-b4-ksm-counters-v1-0-e2a9b13f70c5@linux.dev> To: Andrew Morton , David Hildenbrand , Stefan Roesch , xu xin Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, zhouchengming@bytedance.com, Chengming Zhou X-Developer-Signature: v=1; a=ed25519-sha256; t=1715162174; l=2381; i=chengming.zhou@linux.dev; s=20240508; h=from:subject:message-id; bh=ss7DfeQ3iIOHVfPafgYrAJPDTsUQSQx+kzwMyH5OwKo=; b=X6wGm8B+bjpFtCP8g73w6XczYKdiMwJ0vj812mxv/NkXQMbGzsqKeRNtJg3TU4ZL32DX+qm6z RzE/1RA0jgRBfO4CzfzJYEvfQ2HcOnx7ltDIHCHSXi8xkvh4KwjavFh X-Developer-Key: i=chengming.zhou@linux.dev; a=ed25519; pk=kx40VUetZeR6MuiqrM7kPCcGakk1md0Az5qHwb6gBdU= X-Migadu-Flow: FLOW_OUT The memory resource of KSM is mainly ksm_rmap_item, which has to allocate for each anon page that mm has mapped on. Another memory resource is the ksm_stable_node, which is much less than the ksm_rmap_item. We can account it easily to make general_profit calculation more accurate. This is important when max_page_sharing is limited and so we have more chained nodes. Signed-off-by: Chengming Zhou --- mm/ksm.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 87ffd228944c..a9ce17e6814d 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -267,6 +267,9 @@ static unsigned long ksm_pages_unshared; /* The number of rmap_items in use: to calculate pages_volatile */ static unsigned long ksm_rmap_items; =20 +/* The number of stable_node */ +static unsigned long ksm_stable_nodes; + /* The number of stable_node chains */ static unsigned long ksm_stable_node_chains; =20 @@ -584,12 +587,17 @@ static inline void free_rmap_item(struct ksm_rmap_ite= m *rmap_item) =20 static inline struct ksm_stable_node *alloc_stable_node(void) { + struct ksm_stable_node *node; + /* * The allocation can take too long with GFP_KERNEL when memory is under * pressure, which may lead to hung task warnings. Adding __GFP_HIGH * grants access to memory reserves, helping to avoid this problem. */ - return kmem_cache_alloc(stable_node_cache, GFP_KERNEL | __GFP_HIGH); + node =3D kmem_cache_alloc(stable_node_cache, GFP_KERNEL | __GFP_HIGH); + if (likely(node)) + ksm_stable_nodes++; + return node; } =20 static inline void free_stable_node(struct ksm_stable_node *stable_node) @@ -597,6 +605,7 @@ static inline void free_stable_node(struct ksm_stable_n= ode *stable_node) VM_BUG_ON(stable_node->rmap_hlist_len && !is_stable_node_chain(stable_node)); kmem_cache_free(stable_node_cache, stable_node); + ksm_stable_nodes--; } =20 /* @@ -3671,7 +3680,8 @@ static ssize_t general_profit_show(struct kobject *ko= bj, long general_profit; =20 general_profit =3D (ksm_pages_sharing + get_ksm_zero_pages()) * PAGE_SIZE= - - ksm_rmap_items * sizeof(struct ksm_rmap_item); + ksm_rmap_items * sizeof(struct ksm_rmap_item) - + ksm_stable_nodes * sizeof(struct ksm_stable_node); =20 return sysfs_emit(buf, "%ld\n", general_profit); } --=20 2.45.0