[PATCH 05/10] mm/ksm: use folio in stable_tree_search

alexs
2024-06-04T04:21:46Z

From: "Alex Shi (tencent)" <alexs@kernel.org>

The stable_tree_search() function is actually using folio internal, just
pass the folio parameter from cmp_and_merge_page().

And in cmp_and_merge_page(), the parameter 'page' is actually a folio
too, so use folio to save a few compound check in necessnary context.

Signed-off-by: Alex Shi (tencent) <alexs@kernel.org>
---
mm/ksm.c | 36 ++++++++++++++++++------------------
1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/mm/ksm.c b/mm/ksm.c
index 2e4790842515..f68779651841 100644
--- a/mm/ksm.c
+++ b/mm/ksm.c
@@ -1825,7 +1825,7 @@ static __always_inline struct folio *chain(struct ksm_stable_node **s_n_d,
* This function returns the stable tree node of identical content if found,
* NULL otherwise.
*/
-static struct page *stable_tree_search(struct page *page)
+static struct folio *stable_tree_search(struct folio *folio)
{
int nid;
struct rb_root *root;
@@ -1833,14 +1833,12 @@ static struct page *stable_tree_search(struct page *page)
struct rb_node *parent;
struct ksm_stable_node *stable_node, *stable_node_dup, *stable_node_any;
struct ksm_stable_node *page_node;
- struct folio *folio;

- folio = page_folio(page);
page_node = folio_stable_node(folio);
if (page_node && page_node->head != &migrate_nodes) {
/* ksm page forked */
folio_get(folio);
- return &folio->page;
+ return folio;
}

nid = get_kpfn_nid(folio_pfn(folio));
@@ -1907,7 +1905,7 @@ static struct page *stable_tree_search(struct page *page)
goto again;
}

- ret = memcmp_pages(page, &tree_folio->page);
+ ret = memcmp_pages(folio_page(folio, 0), &tree_folio->page);
folio_put(tree_folio);

parent = *new;
@@ -1973,7 +1971,7 @@ static struct page *stable_tree_search(struct page *page)
folio_put(tree_folio);
goto replace;
}
- return &tree_folio->page;
+ return tree_folio;
}
}

@@ -1987,7 +1985,7 @@ static struct page *stable_tree_search(struct page *page)
out:
if (is_page_sharing_candidate(page_node)) {
folio_get(folio);
- return &folio->page;
+ return folio;
} else
return NULL;

@@ -2037,7 +2035,7 @@ static struct page *stable_tree_search(struct page *page)
}
stable_node_dup->head = &migrate_nodes;
list_add(&stable_node_dup->list, stable_node_dup->head);
- return &folio->page;
+ return folio;

chain_append:
/* stable_node_dup could be null if it reached the limit */
@@ -2324,6 +2322,7 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
unsigned int checksum;
int err;
bool max_page_sharing_bypass = false;
+ struct folio *folio, *kfolio;

stable_node = page_stable_node(page);
if (stable_node) {
@@ -2346,30 +2345,31 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite
}

/* We first start with searching the page inside the stable tree */
- kpage = stable_tree_search(page);
- if (kpage == page && rmap_item->head == stable_node) {
- put_page(kpage);
+ folio = page_folio(page);
+ kfolio = stable_tree_search(folio);
+ if (kfolio == folio && rmap_item->head == stable_node) {
+ folio_put(kfolio);
return;
}

remove_rmap_item_from_tree(rmap_item);

- if (kpage) {
- if (PTR_ERR(kpage) == -EBUSY)
+ if (kfolio) {
+ if (kfolio == ERR_PTR(-EBUSY))
return;

- err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
+ err = try_to_merge_with_ksm_page(rmap_item, page, folio_page(kfolio, 0));
if (!err) {
/*
* The page was successfully merged:
* add its rmap_item to the stable tree.
*/
- lock_page(kpage);
- stable_tree_append(rmap_item, page_stable_node(kpage),
+ folio_lock(kfolio);
+ stable_tree_append(rmap_item, folio_stable_node(kfolio),
max_page_sharing_bypass);
- unlock_page(kpage);
+ folio_unlock(kfolio);
}
- put_page(kpage);
+ folio_put(kfolio);
return;
}

--
2.43.0