Skip to contentSkip to conversation

Cleanup and fixup for ksm

PATCH Applied Miaohe Lin 1 participant 5 messages Mar 30, 2021

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

cover Cleanup and fixup for ksm

Hi all,
This series contains cleanups to remove unnecessary VM_BUG_ON_PAGE and dedicated macro KSM_FLAG_MASK. Also this fixes potential missing rmap_item for stable_node which would result in failed rmap_walk_ksm(). More details can be found in the respective changelogs. Thanks!

Miaohe Lin (4):
ksm: remove redundant VM_BUG_ON_PAGE() on stable_tree_search()
ksm: use GET_KSM_PAGE_NOLOCK to get ksm page in
remove_rmap_item_from_tree()
ksm: remove dedicated macro KSM_FLAG_MASK
ksm: fix potential missing rmap_item for stable_node

 mm/ksm.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
Signature · 1 line
2.19.1
author
#1 lore raw

PATCH 1/4 ksm: remove redundant VM_BUG_ON_PAGE() on stable_tree_search()

The same VM_BUG_ON_PAGE() check is already done in the callee. Remove these extra caller one to simplify code slightly.

---
 mm/ksm.c | 2 --
 1 file changed, 2 deletions(-)
mm/ksm.c +0 -2
index 1f2c62e1d797..359afb3023b4 100644
@@ -1771,7 +1771,6 @@ static struct page *stable_tree_search(struct page *page)
 	 * stable_node_dup is the dup to replace.
 	 */
 	if (stable_node_dup == stable_node) {
-		VM_BUG_ON(is_stable_node_chain(stable_node_dup));
 		VM_BUG_ON(is_stable_node_dup(stable_node_dup));
 		/* chain is missing so create it */
 		stable_node = alloc_stable_node_chain(stable_node_dup,
@@ -1785,7 +1784,6 @@ static struct page *stable_tree_search(struct page *page)
 	 * of the current nid for this page
 	 * content.
 	 */
-	VM_BUG_ON(!is_stable_node_chain(stable_node));
 	VM_BUG_ON(!is_stable_node_dup(stable_node_dup));
 	VM_BUG_ON(page_node->head != &migrate_nodes);
 	list_del(&page_node->list);
Signature · 1 line
2.19.1
author
#2 lore raw

PATCH 2/4 ksm: use GET_KSM_PAGE_NOLOCK to get ksm page in remove_rmap_item_from_tree()

In reply to Miaohe Lin

It's unnecessary to lock the page when get ksm page if we're going to remove the rmap item as page migration is irrelevant in this case. Use GET_KSM_PAGE_NOLOCK instead to save some page lock cycles.

---
 mm/ksm.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/ksm.c +1 -2
index 359afb3023b4..5650a282da3e 100644
@@ -778,12 +778,11 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
 		struct page *page;
 
 		stable_node = rmap_item->head;
-		page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
+		page = get_ksm_page(stable_node, GET_KSM_PAGE_NOLOCK);
 		if (!page)
 			goto out;
 
 		hlist_del(&rmap_item->hlist);
-		unlock_page(page);
 		put_page(page);
 
 		if (!hlist_empty(&stable_node->hlist))
Signature · 1 line
2.19.1
author
#3 lore raw

PATCH 3/4 ksm: remove dedicated macro KSM_FLAG_MASK

In reply to Miaohe Lin

The macro KSM_FLAG_MASK is used in rmap_walk_ksm() only. So we can replace ~KSM_FLAG_MASK with PAGE_MASK to remove this dedicated macro and make code more consistent because PAGE_MASK is used elsewhere in this file.

---
 mm/ksm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
mm/ksm.c +1 -3
index 5650a282da3e..f9ca6d94bf7f 100644
@@ -215,8 +215,6 @@ struct rmap_item {
 #define SEQNR_MASK	0x0ff	/* low bits of unstable tree seqnr */
 #define UNSTABLE_FLAG	0x100	/* is a node of the unstable tree */
 #define STABLE_FLAG	0x200	/* is listed from the stable tree */
-#define KSM_FLAG_MASK	(SEQNR_MASK|UNSTABLE_FLAG|STABLE_FLAG)
-				/* to mask all the flags */
 
 /* The stable and unstable tree heads */
 static struct rb_root one_stable_tree[1] = { RB_ROOT };
@@ -2631,7 +2629,7 @@ void rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc)
 			vma = vmac->vma;
 
 			/* Ignore the stable/unstable/sqnr flags */
-			addr = rmap_item->address & ~KSM_FLAG_MASK;
+			addr = rmap_item->address & PAGE_MASK;
 
 			if (addr < vma->vm_start || addr >= vma->vm_end)
 				continue;
Signature · 1 line
2.19.1
author
#4 lore raw

PATCH 4/4 ksm: fix potential missing rmap_item for stable_node

In reply to Miaohe Lin

When remove rmap_item from stable tree, STABLE_FLAG of rmap_item is cleared with head reserved. So the following scenario might happen:
For ksm page with rmap_item1:
cmp_and_merge_page
stable_node->head = &migrate_nodes; remove_rmap_item_from_tree, but head still equal to stable_node; try_to_merge_with_ksm_page failed;
return;
For the same ksm page with rmap_item2, stable node migration succeed this time. The stable_node->head does not equal to migrate_nodes now. For ksm page with rmap_item1 again:
cmp_and_merge_page
stable_node->head != &migrate_nodes && rmap_item->head == stable_node return;
We would miss the rmap_item for stable_node and might result in failed rmap_walk_ksm(). Fix this by set rmap_item->head to NULL when rmap_item is removed from stable tree.

Fixes 4146d2d673e8 ("ksm: make !merge_across_nodes migration safe")
---
 mm/ksm.c | 1 +
 1 file changed, 1 insertion(+)
mm/ksm.c +1 -0
index f9ca6d94bf7f..8609c67f04a2 100644
@@ -791,6 +791,7 @@ static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
 		stable_node->rmap_hlist_len--;
 
 		put_anon_vma(rmap_item->anon_vma);
+		rmap_item->head = NULL;
 		rmap_item->address &= PAGE_MASK;
 
 	} else if (rmap_item->address & UNSTABLE_FLAG) {
Signature · 1 line
2.19.1