linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
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 4/4] mm/ksm: use ksm page itself if no another ksm page is found on stable tree
Date: Fri, 24 May 2024 16:56:53 +0800	[thread overview]
Message-ID: <20240524-b4-ksm-scan-optimize-v1-4-053b31bd7ab4@linux.dev> (raw)
In-Reply-To: <20240524-b4-ksm-scan-optimize-v1-0-053b31bd7ab4@linux.dev>

It's interesting that a mapped ksm page also need to stable_tree_search(),
instead of using stable_tree_insert() directly. The reason is that we have
a minor optimization for migrated ksm page that has only one mapcount, in
which case we can find another ksm page that already on the stable tree
to replace it.

But what if we can't find another shareable candidate on the stable tree?
Obviously, we should just return the ksm page itself if it has been
inserted on the tree. And we shouldn't return NULL if no another ksm page
is found on the tree, since we will still map on this ksm page but the
rmap_item will be removed out to insert on the unstable tree if we return
NULL in this case.

We can ignore the is_page_sharing_candidate() check in this case, since
max_page_sharing_bypass is set to true in cmp_and_merge_page().

Signed-off-by: Chengming Zhou <chengming.zhou@linux.dev>
---
 mm/ksm.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index f923699452ed..6dea83998258 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1940,11 +1940,8 @@ static struct page *stable_tree_search(struct page *page)
 	rb_link_node(&page_node->node, parent, new);
 	rb_insert_color(&page_node->node, root);
 out:
-	if (is_page_sharing_candidate(page_node)) {
-		folio_get(folio);
-		return &folio->page;
-	} else
-		return NULL;
+	folio_get(folio);
+	return &folio->page;
 
 replace:
 	/*
@@ -1966,10 +1963,7 @@ static struct page *stable_tree_search(struct page *page)
 			rb_replace_node(&stable_node_dup->node,
 					&page_node->node,
 					root);
-			if (is_page_sharing_candidate(page_node))
-				folio_get(folio);
-			else
-				folio = NULL;
+			folio_get(folio);
 		} else {
 			rb_erase(&stable_node_dup->node, root);
 			folio = NULL;
@@ -1982,10 +1976,7 @@ static struct page *stable_tree_search(struct page *page)
 			list_del(&page_node->list);
 			DO_NUMA(page_node->nid = nid);
 			stable_node_chain_add_dup(page_node, stable_node);
-			if (is_page_sharing_candidate(page_node))
-				folio_get(folio);
-			else
-				folio = NULL;
+			folio_get(folio);
 		} else {
 			folio = NULL;
 		}
@@ -2009,7 +2000,7 @@ static struct page *stable_tree_search(struct page *page)
 		stable_node = alloc_stable_node_chain(stable_node_dup,
 						      root);
 		if (!stable_node)
-			return NULL;
+			goto out;
 	}
 	/*
 	 * Add this stable_node dup that was

-- 
2.45.1



      parent reply	other threads:[~2024-05-24  8:57 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-24  8:56 [PATCH 0/4] mm/ksm: cmp_and_merge_page() optimizations and cleanup Chengming Zhou
2024-05-24  8:56 ` [PATCH 1/4] mm/ksm: refactor out try_to_merge_with_zero_page() Chengming Zhou
2024-05-24 15:12   ` David Hildenbrand
2024-05-27  4:36     ` Chengming Zhou
2024-05-27  7:18       ` David Hildenbrand
2024-05-27  7:41         ` Chengming Zhou
2024-05-24  8:56 ` [PATCH 2/4] mm/ksm: don't waste time searching stable tree for fast changing page Chengming Zhou
2024-05-24  8:56 ` [PATCH 3/4] mm/ksm: optimize the chain()/chain_prune() interfaces Chengming Zhou
2024-05-24  8:56 ` Chengming Zhou [this message]

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=20240524-b4-ksm-scan-optimize-v1-4-053b31bd7ab4@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