Skip to contentSkip to conversation

mm: thp: check total_mapcount instead of page_mapcount

PATCH Superseded Yang Shi 2 participants 6 messages Apr 30, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Yang Shi

author
#0 lore raw

PATCH 1/1 mm: thp: check total_mapcount instead of page_mapcount

When debugging the bug reported by Wang Yugui [1], try_to_unmap() may return false positive for PTE-mapped THP since page_mapcount() is used to check if the THP is unmapped, but it just checks compound mapount and head page's mapcount. If the THP is PTE-mapped and head page is not mapped, it may return false positive.

Use total_mapcount() instead of page_mapcount() and do so for the VM_BUG_ON_PAGE in split_huge_page_to_list as well.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/huge_memory.c | 2 +-
 mm/rmap.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +1 -1
index 63ed6b25deaa..2122c3e853b9 100644
@@ -2718,7 +2718,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
 	}
 
 	unmap_page(head);
-	VM_BUG_ON_PAGE(compound_mapcount(head), head);
+	VM_BUG_ON_PAGE(total_mapcount(head), head);
 
 	/* block interrupt reentry in xa_lock and spinlock */
 	local_irq_disable();
mm/rmap.c +1 -1
index 693a610e181d..2e547378ab5f 100644
@@ -1777,7 +1777,7 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags)
 	else
 		rmap_walk(page, &rwc);
 
-	return !page_mapcount(page) ? true : false;
+	return !total_mapcount(page) ? true : false;
 }
 
 /**
Signature · 1 line
2.26.2
reviewer
#1 lore raw
Quoted 22 lines · Yang Shi

When debugging the bug reported by Wang Yugui [1], try_to_unmap() may return false positive for PTE-mapped THP since page_mapcount() is used to check if the THP is unmapped, but it just checks compound mapount and head page's mapcount. If the THP is PTE-mapped and head page is not mapped, it may return false positive.

Use total_mapcount() instead of page_mapcount() and do so for the VM_BUG_ON_PAGE in split_huge_page_to_list as well.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/huge_memory.c | 2 +-
 mm/rmap.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 63ed6b25deaa..2122c3e853b9 100644

mm/huge_memory.c int split_huge_page_to_list(struct page *page, struct list_head *list) · line 2721

Quoted 5 lines
mm/huge_memory.c +1 -1
@@ -2718,7 +2718,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
 	}
 
 	unmap_page(head);
-	VM_BUG_ON_PAGE(compound_mapcount(head), head);
+	VM_BUG_ON_PAGE(total_mapcount(head), head);

I am not sure about this change. The code below also checks total_mapcount(head) and returns EBUSY if the count is non-zero. This change makes the code dead. On the other hand, the change will force all mappings to the page have to be successfully unmapped all the time. I am not sure if we want to do that. Maybe it is better to just check total_mapcount() and fail the split. The same situation happens with the code change below.

Quoted 8 lines
mm/huge_memory.c +0 -0
 	/* block interrupt reentry in xa_lock and spinlock */
 	local_irq_disable();
mm/rmap.c +0 -0
index 693a610e181d..2e547378ab5f 100644

mm/rmap.c bool try_to_unmap(struct page *page, enum ttu_flags flags) · line 1781

Quoted 6 lines
mm/rmap.c +1 -1
@@ -1777,7 +1777,7 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags)
 	else
 		rmap_walk(page, &rwc);
 
-	return !page_mapcount(page) ? true : false;
+	return !total_mapcount(page) ? true : false;
 }

In unmap_page(), VM_BUG_ON_PAGE(!unmap_success, page) will force all mappings to the page have to be all unmapped, which might not be the case we want. Maybe you will want to remove the VM_BUG_ON_PAGE here, check total_mapcount() above, and fail the split if not all mappings to the pages are unmapped.

—
Best Regards,
Yan Zi

author
#2 lore raw
Quoted 27 lines · Zi Yan
Quoted 24 lines · Yang Shi

When debugging the bug reported by Wang Yugui [1], try_to_unmap() may return false positive for PTE-mapped THP since page_mapcount() is used to check if the THP is unmapped, but it just checks compound mapount and head page's mapcount. If the THP is PTE-mapped and head page is not mapped, it may return false positive.

Use total_mapcount() instead of page_mapcount() and do so for the VM_BUG_ON_PAGE in split_huge_page_to_list as well.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/huge_memory.c | 2 +-
 mm/rmap.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 63ed6b25deaa..2122c3e853b9 100644
@@ -2718,7 +2718,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
      }
 
Quoted 6 lines

mm/huge_memory.c int split_huge_page_to_list(struct page *page, struct list_head *list) · line 2721

Quoted 3 lines
mm/huge_memory.c +1 -1
      unmap_page(head);
-     VM_BUG_ON_PAGE(compound_mapcount(head), head);
+     VM_BUG_ON_PAGE(total_mapcount(head), head);

I am not sure about this change. The code below also checks total_mapcount(head) and returns EBUSY if the count is non-zero. This change makes the code dead.

It is actually dead if CONFIG_DEBUG_VM is enabled and total_mapcount is not 0 regardless of this change due to the below code, right?

if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
                        pr_alert("total_mapcount: %u, page_count(): %u\n",
                                        mapcount, count);
                        if (PageTail(page))
                                dump_page(head, NULL);
                        dump_page(page, "total_mapcount(head) > 0");
                        BUG();
                }
Quoted 4 lines

On the other hand, the change will force all mappings to the page have to be successfully unmapped all the time. I am not sure if we want to do that. Maybe it is better to just check total_mapcount() and fail the split. The same situation happens with the code change below.

IIUC, the code did force all mappings to the page to be unmapped in order to split it.

Quoted 12 lines
Quoted 12 lines
mm/huge_memory.c +0 -0
      /* block interrupt reentry in xa_lock and spinlock */
      local_irq_disable();
mm/rmap.c +0 -0
index 693a610e181d..2e547378ab5f 100644
@@ -1777,7 +1777,7 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags)
      else
              rmap_walk(page, &rwc);
 
Quoted 6 lines

mm/rmap.c bool try_to_unmap(struct page *page, enum ttu_flags flags) · line 1781

Quoted 3 lines
mm/rmap.c +1 -1
-     return !page_mapcount(page) ? true : false;
+     return !total_mapcount(page) ? true : false;
 }

In unmap_page(), VM_BUG_ON_PAGE(!unmap_success, page) will force all mappings to the page have to be all unmapped, which might not be the case we want.

AFAICT, I don't see such a case from all the callers of try_to_unmap(). Imay miss something, but I do have a hard time thinking of a usecase which can proceed safely with "not fully unmapped" page.

Quoted 8 lines

Maybe you will want to remove the VM_BUG_ON_PAGE here, check total_mapcount() above, and fail the split if not all mappings to the pages are unmapped.

—
Best Regards,
Yan Zi

reviewer
#3 lore raw
Quoted 38 lines · Yang Shi
Quoted 27 lines · Zi Yan
Quoted 24 lines · Yang Shi

When debugging the bug reported by Wang Yugui [1], try_to_unmap() may return false positive for PTE-mapped THP since page_mapcount() is used to check if the THP is unmapped, but it just checks compound mapount and head page's mapcount. If the THP is PTE-mapped and head page is not mapped, it may return false positive.

Use total_mapcount() instead of page_mapcount() and do so for the VM_BUG_ON_PAGE in split_huge_page_to_list as well.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/huge_memory.c | 2 +-
 mm/rmap.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 63ed6b25deaa..2122c3e853b9 100644
@@ -2718,7 +2718,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
      }
 
Quoted 6 lines

mm/huge_memory.c int split_huge_page_to_list(struct page *page, struct list_head *list) · line 2721

Quoted 3 lines
mm/huge_memory.c +1 -1
      unmap_page(head);
-     VM_BUG_ON_PAGE(compound_mapcount(head), head);
+     VM_BUG_ON_PAGE(total_mapcount(head), head);

I am not sure about this change. The code below also checks total_mapcount(head) and returns EBUSY if the count is non-zero. This change makes the code dead.

It is actually dead if CONFIG_DEBUG_VM is enabled and total_mapcount is not 0 regardless of this change due to the below code, right?

Quoted 8 lines
if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
                        pr_alert("total_mapcount: %u, page_count(): %u\n",
                                        mapcount, count);
                        if (PageTail(page))
                                dump_page(head, NULL);
                        dump_page(page, "total_mapcount(head) > 0");
                        BUG();
                }

Right. But with this change, mapcount will never be non-zero. The code above will be useless and can be removed.

Quoted 26 lines
Quoted 4 lines

On the other hand, the change will force all mappings to the page have to be successfully unmapped all the time. I am not sure if we want to do that. Maybe it is better to just check total_mapcount() and fail the split. The same situation happens with the code change below.

IIUC, the code did force all mappings to the page to be unmapped in order to split it.

Quoted 12 lines
Quoted 12 lines
mm/huge_memory.c +0 -0
      /* block interrupt reentry in xa_lock and spinlock */
      local_irq_disable();
mm/rmap.c +0 -0
index 693a610e181d..2e547378ab5f 100644
@@ -1777,7 +1777,7 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags)
      else
              rmap_walk(page, &rwc);
 
Quoted 6 lines

mm/rmap.c bool try_to_unmap(struct page *page, enum ttu_flags flags) · line 1781

Quoted 3 lines
mm/rmap.c +1 -1
-     return !page_mapcount(page) ? true : false;
+     return !total_mapcount(page) ? true : false;
 }

In unmap_page(), VM_BUG_ON_PAGE(!unmap_success, page) will force all mappings to the page have to be all unmapped, which might not be the case we want.

Quoted 4 lines

AFAICT, I don't see such a case from all the callers of try_to_unmap(). Imay miss something, but I do have a hard time thinking of a usecase which can proceed safely with "not fully unmapped" page.

This code change is correct, but after the change unmap_page() will fire VM_BUG_ON when not all mappings are unmapped. Along with the change above, we will have two identical VM_BUG_ONs happen one after another. We might want to remove one of them.

Also, this changes the semantics of try_to_unmap. The comment for try_to_unmap might need to be updated.

—
Best Regards,
Yan Zi

author
#4 lore raw
Quoted 50 lines · Zi Yan
Quoted 38 lines · Yang Shi
Quoted 27 lines · Zi Yan
Quoted 24 lines · Yang Shi

When debugging the bug reported by Wang Yugui [1], try_to_unmap() may return false positive for PTE-mapped THP since page_mapcount() is used to check if the THP is unmapped, but it just checks compound mapount and head page's mapcount. If the THP is PTE-mapped and head page is not mapped, it may return false positive.

Use total_mapcount() instead of page_mapcount() and do so for the VM_BUG_ON_PAGE in split_huge_page_to_list as well.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/huge_memory.c | 2 +-
 mm/rmap.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 63ed6b25deaa..2122c3e853b9 100644
@@ -2718,7 +2718,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
      }
 
Quoted 6 lines

mm/huge_memory.c int split_huge_page_to_list(struct page *page, struct list_head *list) · line 2721

Quoted 3 lines
mm/huge_memory.c +1 -1
      unmap_page(head);
-     VM_BUG_ON_PAGE(compound_mapcount(head), head);
+     VM_BUG_ON_PAGE(total_mapcount(head), head);

I am not sure about this change. The code below also checks total_mapcount(head) and returns EBUSY if the count is non-zero. This change makes the code dead.

It is actually dead if CONFIG_DEBUG_VM is enabled and total_mapcount is not 0 regardless of this change due to the below code, right?

Quoted 8 lines
if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
                        pr_alert("total_mapcount: %u, page_count(): %u\n",
                                        mapcount, count);
                        if (PageTail(page))
                                dump_page(head, NULL);
                        dump_page(page, "total_mapcount(head) > 0");
                        BUG();
                }
Quoted 2 lines

Right. But with this change, mapcount will never be non-zero. The code above will be useless and can be removed.

Yes, you are correct.

Quoted 32 lines
Quoted 27 lines
Quoted 5 lines

On the other hand, the change will force all mappings to the page have to be successfully unmapped all the time. I am not sure if we want to do that. Maybe it is better to just check total_mapcount() and fail the split. The same situation happens with the code change below.

IIUC, the code did force all mappings to the page to be unmapped in order to split it.

Quoted 12 lines
Quoted 12 lines
mm/huge_memory.c +0 -0
      /* block interrupt reentry in xa_lock and spinlock */
      local_irq_disable();
mm/rmap.c +0 -0
index 693a610e181d..2e547378ab5f 100644
@@ -1777,7 +1777,7 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags)
      else
              rmap_walk(page, &rwc);
 
Quoted 6 lines

mm/rmap.c bool try_to_unmap(struct page *page, enum ttu_flags flags) · line 1781

Quoted 3 lines
mm/rmap.c +1 -1
-     return !page_mapcount(page) ? true : false;
+     return !total_mapcount(page) ? true : false;
 }

In unmap_page(), VM_BUG_ON_PAGE(!unmap_success, page) will force all mappings to the page have to be all unmapped, which might not be the case we want.

Quoted 4 lines

AFAICT, I don't see such a case from all the callers of try_to_unmap(). Imay miss something, but I do have a hard time thinking of a usecase which can proceed safely with "not fully unmapped" page.

Quoted 4 lines

This code change is correct, but after the change unmap_page() will fire VM_BUG_ON when not all mappings are unmapped. Along with the change above, we will have two identical VM_BUG_ONs happen one after another. We might want to remove one of them.

Yes. I'd prefer keep the one after unmap_page() since it seems more obvious. Any objection?

Quoted 3 lines

Also, this changes the semantics of try_to_unmap. The comment for try_to_unmap might need to be updated.

What comment do you refer to?

Quoted 5 lines

—
Best Regards,
Yan Zi

reviewer
#5 lore raw
Quoted 88 lines · Yang Shi
Quoted 50 lines · Zi Yan
Quoted 38 lines · Yang Shi
Quoted 27 lines · Zi Yan
Quoted 24 lines · Yang Shi

When debugging the bug reported by Wang Yugui [1], try_to_unmap() may return false positive for PTE-mapped THP since page_mapcount() is used to check if the THP is unmapped, but it just checks compound mapount and head page's mapcount. If the THP is PTE-mapped and head page is not mapped, it may return false positive.

Use total_mapcount() instead of page_mapcount() and do so for the VM_BUG_ON_PAGE in split_huge_page_to_list as well.

[1] https://lore.kernel.org/linux-mm/[email protected]/

---
 mm/huge_memory.c | 2 +-
 mm/rmap.c        | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 63ed6b25deaa..2122c3e853b9 100644
@@ -2718,7 +2718,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)
      }
 
Quoted 6 lines

mm/huge_memory.c int split_huge_page_to_list(struct page *page, struct list_head *list) · line 2721

Quoted 3 lines
mm/huge_memory.c +1 -1
      unmap_page(head);
-     VM_BUG_ON_PAGE(compound_mapcount(head), head);
+     VM_BUG_ON_PAGE(total_mapcount(head), head);

I am not sure about this change. The code below also checks total_mapcount(head) and returns EBUSY if the count is non-zero. This change makes the code dead.

It is actually dead if CONFIG_DEBUG_VM is enabled and total_mapcount is not 0 regardless of this change due to the below code, right?

Quoted 8 lines
if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
                        pr_alert("total_mapcount: %u, page_count(): %u\n",
                                        mapcount, count);
                        if (PageTail(page))
                                dump_page(head, NULL);
                        dump_page(page, "total_mapcount(head) > 0");
                        BUG();
                }
Quoted 2 lines

Right. But with this change, mapcount will never be non-zero. The code above will be useless and can be removed.

Yes, you are correct.

Quoted 32 lines
Quoted 31 lines
Quoted 5 lines

On the other hand, the change will force all mappings to the page have to be successfully unmapped all the time. I am not sure if we want to do that. Maybe it is better to just check total_mapcount() and fail the split. The same situation happens with the code change below.

IIUC, the code did force all mappings to the page to be unmapped in order to split it.

Quoted 12 lines
Quoted 12 lines
mm/huge_memory.c +0 -0
      /* block interrupt reentry in xa_lock and spinlock */
      local_irq_disable();
mm/rmap.c +0 -0
index 693a610e181d..2e547378ab5f 100644
@@ -1777,7 +1777,7 @@ bool try_to_unmap(struct page *page, enum ttu_flags flags)
      else
              rmap_walk(page, &rwc);
 
Quoted 6 lines

mm/rmap.c bool try_to_unmap(struct page *page, enum ttu_flags flags) · line 1781

Quoted 3 lines
mm/rmap.c +1 -1
-     return !page_mapcount(page) ? true : false;
+     return !total_mapcount(page) ? true : false;
 }

In unmap_page(), VM_BUG_ON_PAGE(!unmap_success, page) will force all mappings to the page have to be all unmapped, which might not be the case we want.

AFAICT, I don't see such a case from all the callers of try_to_unmap(). Imay miss something, but I do have a hard time thinking of a usecase which can proceed safely with "not fully unmapped" page.

Quoted 7 lines
Quoted 4 lines

This code change is correct, but after the change unmap_page() will fire VM_BUG_ON when not all mappings are unmapped. Along with the change above, we will have two identical VM_BUG_ONs happen one after another. We might want to remove one of them.

Yes. I'd prefer keep the one after unmap_page() since it seems more obvious. Any objection?

Sounds good to me.

Quoted 6 lines
Quoted 4 lines

Also, this changes the semantics of try_to_unmap. The comment for try_to_unmap might need to be updated.

What comment do you refer to?

/**
* try_to_unmap - try to remove all page table mappings to a page

a page -> a page and the compound page it belongs to

* @page: the page to get unmapped

the page -> the page or the subpage of a compound page

* @flags: action and flags
*
* Tries to remove all the page table entries which are mapping this
* page, used in the pageout path. Caller must hold the page lock.

this page -> this page and the compound page it belongs to

Feel free to change the wording if you find better ones.

—
Best Regards,
Yan Zi