Skip to contentSkip to conversation

ksm: Revert "use GET_KSM_PAGE_NOLOCK to get ksm page in remove_rmap_item_from_tree()"

PATCH Applied Hugh Dickins 2 participants 4 messages May 10, 2021 – May 11, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksm: Revert "use GET_KSM_PAGE_NOLOCK to get ksm page in remove_rmap_item_from_tree()"

This reverts commit 3e96b6a2e9ad929a3230a22f4d64a74671a0720b. General Protection Fault in rmap_walk_ksm() under memory pressure:
remove_rmap_item_from_tree() needs to take page lock, of course.

---

 mm/ksm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/ksm.c +2 -1
@@ -776,11 +776,12 @@ static void remove_rmap_item_from_tree(s
 		struct page *page;
 
 		stable_node = rmap_item->head;
-		page = get_ksm_page(stable_node, GET_KSM_PAGE_NOLOCK);
+		page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
 		if (!page)
 			goto out;
 
 		hlist_del(&rmap_item->hlist);
+		unlock_page(page);
 		put_page(page);
 
 		if (!hlist_empty(&stable_node->hlist))
#1 lore raw
Quoted 4 lines · Hugh Dickins

This reverts commit 3e96b6a2e9ad929a3230a22f4d64a74671a0720b. General Protection Fault in rmap_walk_ksm() under memory pressure:
remove_rmap_item_from_tree() needs to take page lock, of course.

I'am really sorry about it! And many thanks for this bugfix! It seems rmap_walk_ksm() relies on the page lock to protect against concurrent modifications to that page's node of the stable tree. Could you please add a comment in remove_rmap_item_from_tree() to clarify this in case similar trouble again? Many thanks!

Quoted 24 lines

---

 mm/ksm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/ksm.c +2 -1
@@ -776,11 +776,12 @@ static void remove_rmap_item_from_tree(s
 		struct page *page;
 
 		stable_node = rmap_item->head;
-		page = get_ksm_page(stable_node, GET_KSM_PAGE_NOLOCK);
+		page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
 		if (!page)
 			goto out;
 
 		hlist_del(&rmap_item->hlist);
+		unlock_page(page);
 		put_page(page);
 
 		if (!hlist_empty(&stable_node->hlist))

.

#2 lore raw
Quoted 11 lines · Miaohe Lin
Quoted 4 lines · Hugh Dickins

This reverts commit 3e96b6a2e9ad929a3230a22f4d64a74671a0720b. General Protection Fault in rmap_walk_ksm() under memory pressure:
remove_rmap_item_from_tree() needs to take page lock, of course.

I'am really sorry about it! And many thanks for this bugfix! It seems rmap_walk_ksm() relies on the page lock to protect against concurrent modifications to that page's node of the stable tree. Could you please add a comment in remove_rmap_item_from_tree() to clarify this in case similar trouble again? Many thanks!

Sorry, no. Page lock is held by callers of stable_tree_append() when adding an rmap_item to the tree, and held by callers of rmap_walk_ksm() (see VM_BUG_ON_PAGE there) when walking the tree: you would surely expect some kind of locking when removing an rmap_item from the tree, and the appropriate page lock is what GET_KSM_PAGE_LOCK provided.

I do not want us to go through the kernel source adding a comment /* We really mean to take this lock: it protects against concurrency */ every time we take a lock in the kernel: you should generally assume that if a lock is taken, then the writer intended it to be taken.

There are sure to be some exceptions, where a lock is taken pointlessly:
but please look deeper before assuming that is the case.

Hugh

Quoted 23 lines
Quoted 23 lines

---

 mm/ksm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/ksm.c +2 -1
@@ -776,11 +776,12 @@ static void remove_rmap_item_from_tree(s
 		struct page *page;
 
 		stable_node = rmap_item->head;
-		page = get_ksm_page(stable_node, GET_KSM_PAGE_NOLOCK);
+		page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
 		if (!page)
 			goto out;
 
 		hlist_del(&rmap_item->hlist);
+		unlock_page(page);
 		put_page(page);
 
 		if (!hlist_empty(&stable_node->hlist))
#3 lore raw
Quoted 19 lines · Hugh Dickins
Quoted 11 lines · Miaohe Lin
Quoted 4 lines · Hugh Dickins

This reverts commit 3e96b6a2e9ad929a3230a22f4d64a74671a0720b. General Protection Fault in rmap_walk_ksm() under memory pressure:
remove_rmap_item_from_tree() needs to take page lock, of course.

I'am really sorry about it! And many thanks for this bugfix! It seems rmap_walk_ksm() relies on the page lock to protect against concurrent modifications to that page's node of the stable tree. Could you please add a comment in remove_rmap_item_from_tree() to clarify this in case similar trouble again? Many thanks!

Sorry, no. Page lock is held by callers of stable_tree_append() when adding an rmap_item to the tree, and held by callers of rmap_walk_ksm() (see VM_BUG_ON_PAGE there) when walking the tree: you would surely expect some kind of locking when removing an rmap_item from the tree, and the appropriate page lock is what GET_KSM_PAGE_LOCK provided.

Quoted 8 lines

I do not want us to go through the kernel source adding a comment /* We really mean to take this lock: it protects against concurrency */ every time we take a lock in the kernel: you should generally assume that if a lock is taken, then the writer intended it to be taken.

There are sure to be some exceptions, where a lock is taken pointlessly:
but please look deeper before assuming that is the case.

I see. I should have been more careful. Many thanks for your detailed explanation and sorry about trouble!

Quoted 27 lines

Hugh

Quoted 18 lines
Quoted 18 lines

---

 mm/ksm.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/ksm.c +1 -1
@@ -776,11 +776,12 @@ static void remove_rmap_item_from_tree(s
 		struct page *page;
 
 		stable_node = rmap_item->head;
-		page = get_ksm_page(stable_node, GET_KSM_PAGE_NOLOCK);
+		page = get_ksm_page(stable_node, GET_KSM_PAGE_LOCK);
 		if (!page)
 			goto out;
 
Quoted 5 lines

mm/ksm.c static void remove_rmap_item_from_tree(s · line 787

Quoted 5 lines
mm/ksm.c +1 -0
 		hlist_del(&rmap_item->hlist);
+		unlock_page(page);
 		put_page(page);
 
 		if (!hlist_empty(&stable_node->hlist))

.