From: Chengming Zhou <chengming.zhou@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>,
david@redhat.com, aarcange@redhat.com, hughd@google.com,
shr@devkernel.io
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
zhouchengming@bytedance.com,
Chengming Zhou <chengming.zhou@linux.dev>
Subject: [PATCH v2 2/3] mm/ksm: don't waste time searching stable tree for fast changing page
Date: Fri, 21 Jun 2024 15:54:30 +0800 [thread overview]
Message-ID: <20240621-b4-ksm-scan-optimize-v2-2-1c328aa9e30b@linux.dev> (raw)
In-Reply-To: <20240621-b4-ksm-scan-optimize-v2-0-1c328aa9e30b@linux.dev>
The code flow in cmp_and_merge_page() is suboptimal for handling the
ksm page and non-ksm page at the same time. For example:
- ksm page
1. Mostly just return if this ksm page is not migrated and this rmap_item
has been on the rmap hlist. Or we have to fix this rmap_item mapping.
2. But we absolutely don't need to checksum for this ksm page, since it
can't change.
- non-ksm page
1. First don't need to waste time searching stable tree if fast changing.
2. Should try to merge with zero page before search the stable tree.
3. Then search stable tree to find mergeable ksm page.
This patch optimizes the code flow so the handling differences between
ksm page and non-ksm page become clearer and more efficient too.
Signed-off-by: Chengming Zhou <chengming.zhou@linux.dev>
---
mm/ksm.c | 32 +++++++++++++++++---------------
1 file changed, 17 insertions(+), 15 deletions(-)
diff --git a/mm/ksm.c b/mm/ksm.c
index 1427abd18627..2cf836fb1367 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -2370,6 +2370,23 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
*/
if (!is_page_sharing_candidate(stable_node))
max_page_sharing_bypass = true;
+ } else {
+ remove_rmap_item_from_tree(rmap_item);
+
+ /*
+ * If the hash value of the page has changed from the last time
+ * we calculated it, this page is changing frequently: therefore we
+ * don't want to insert it in the unstable tree, and we don't want
+ * to waste our time searching for something identical to it there.
+ */
+ checksum = calc_checksum(page);
+ if (rmap_item->oldchecksum != checksum) {
+ rmap_item->oldchecksum = checksum;
+ return;
+ }
+
+ if (!try_to_merge_with_zero_page(rmap_item, page))
+ return;
}
/* We first start with searching the page inside the stable tree */
@@ -2400,21 +2417,6 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
return;
}
- /*
- * If the hash value of the page has changed from the last time
- * we calculated it, this page is changing frequently: therefore we
- * don't want to insert it in the unstable tree, and we don't want
- * to waste our time searching for something identical to it there.
- */
- checksum = calc_checksum(page);
- if (rmap_item->oldchecksum != checksum) {
- rmap_item->oldchecksum = checksum;
- return;
- }
-
- if (!try_to_merge_with_zero_page(rmap_item, page))
- return;
-
tree_rmap_item =
unstable_tree_search_insert(rmap_item, page, &tree_page);
if (tree_rmap_item) {
--
2.45.2
next prev parent reply other threads:[~2024-06-21 7:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-21 7:54 [PATCH v2 0/3] mm/ksm: cmp_and_merge_page() optimizations and cleanup Chengming Zhou
2024-06-21 7:54 ` [PATCH v2 1/3] mm/ksm: refactor out try_to_merge_with_zero_page() Chengming Zhou
2024-06-21 7:54 ` Chengming Zhou [this message]
2024-06-21 7:54 ` [PATCH v2 3/3] mm/ksm: optimize the chain()/chain_prune() interfaces Chengming Zhou
2024-06-22 1:09 ` Andrew Morton
2024-06-28 23:59 ` [PATCH v2 0/3] mm/ksm: cmp_and_merge_page() optimizations and cleanup Andrew Morton
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=20240621-b4-ksm-scan-optimize-v2-2-1c328aa9e30b@linux.dev \
--to=chengming.zhou@linux.dev \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=hughd@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=shr@devkernel.io \
--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 NNTP newsgroup(s).