Skip to contentSkip to conversation

Fix some bugs about HugeTLB code

PATCH Superseded Muchun Song 2 participants 10 messages Jan 14, 2021

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Fix some bugs about HugeTLB code

This patch series aims to fix some bugs and add some improvements.

Changelog since v4 -> v5:
- Squash "mm: hugetlb: retry dissolve page when hitting race" to "mm: hugetlb: fix a race between freeing and dissolving the page"

Changelog since v3 -> v4:
- Update commit log of patch #1.
- Drop "mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page"
- Add "mm: hugetlb: retry dissolve page when hitting race"

Changelog since v2 -> v3:
- Update commit log.
- Using head[3].private to indicate the page is freed in patch #3.

Changelog since v1 -> v2:
- Export set_page_huge_active() in patch #2 to fix.
- Using head[3].mapping to indicate the page is freed in patch #3.
- Flush @free_hpage_work in patch #4.

Muchun Song (5):
mm: migrate: do not migrate HugeTLB page whose refcount is one
mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
mm: hugetlb: fix a race between freeing and dissolving the page
mm: hugetlb: fix a race between isolating and freeing page
mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active

 fs/hugetlbfs/inode.c    |  3 ++-
 include/linux/hugetlb.h |  2 ++
 mm/hugetlb.c            | 50 ++++++++++++++++++++++++++++++++++++++++++++-----
 mm/migrate.c            |  6 ++++++
 4 files changed, 55 insertions(+), 6 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH v5 1/5 mm: migrate: do not migrate HugeTLB page whose refcount is one

All pages isolated for the migration have an elevated reference count and therefore seeing a reference count equal to 1 means that the last user of the page has dropped the reference and the page has became unused and there doesn't make much sense to migrate it anymore. This has been done for regular pages and this patch does the same for hugetlb pages. Although the likelyhood of the race is rather small for hugetlb pages it makes sense the two code paths in sync.

---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +6 -0
index 4385f2fb5d18..a6631c4eb6a6 100644
@@ -1279,6 +1279,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 		return -ENOSYS;
 	}
 
+	if (page_count(hpage) == 1) {
+		/* page was freed from under us. So we are done. */
+		putback_active_hugepage(hpage);
+		return MIGRATEPAGE_SUCCESS;
+	}
+
 	new_hpage = get_new_page(hpage, private);
 	if (!new_hpage)
 		return -ENOMEM;
Signature · 1 line
2.11.0
#2 lore raw

PATCH v5 2/5 mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page

In reply to Muchun Song

If a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a later isolate_huge_page failure when the page migration code would like to move that page. Such a failure would be unexpected and wrong.

Only export set_page_huge_active, just leave clear_page_huge_active as static. Because there are no external users.

Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())
---
 fs/hugetlbfs/inode.c    | 3 ++-
 include/linux/hugetlb.h | 2 ++
 mm/hugetlb.c            | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +2 -1
index b5c109703daa..21c20fd5f9ee 100644
@@ -735,9 +735,10 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
 
+		set_page_huge_active(page);
 		/*
 		 * unlock_page because locked by add_to_page_cache()
-		 * page_put due to reference from alloc_huge_page()
+		 * put_page() due to reference from alloc_huge_page()
 		 */
 		unlock_page(page);
 		put_page(page);
include/linux/hugetlb.h +2 -0
index ebca2ef02212..b5807f23caf8 100644
@@ -770,6 +770,8 @@ static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma,
 }
 #endif
 
+void set_page_huge_active(struct page *page);
+
 #else	/* CONFIG_HUGETLB_PAGE */
 struct hstate {};
 
mm/hugetlb.c +1 -1
index 1f3bf1710b66..4741d60f8955 100644
@@ -1348,7 +1348,7 @@ bool page_huge_active(struct page *page)
 }
 
 /* never called for tail page */
-static void set_page_huge_active(struct page *page)
+void set_page_huge_active(struct page *page)
 {
 	VM_BUG_ON_PAGE(!PageHeadHuge(page), page);
 	SetPagePrivate(&page[1]);
Signature · 1 line
2.11.0
#3 lore raw

PATCH v5 3/5 mm: hugetlb: fix a race between freeing and dissolving the page

In reply to Muchun Song

There is a race condition between __free_huge_page() and dissolve_free_huge_page().

CPU0: CPU1:

// page_count(page) == 1
put_page(page)
__free_huge_page(page)

                              dissolve_free_huge_page(page)
                                spin_lock(&hugetlb_lock)
                                // PageHuge(page) && !page_count(page)
                                update_and_free_page(page)
                                // page is freed to the buddy
                                spin_unlock(&hugetlb_lock)
    spin_lock(&hugetlb_lock)
    clear_page_huge_active(page)
    enqueue_huge_page(page)
    // It is wrong, the page is already freed
    spin_unlock(&hugetlb_lock)

The race windows is between put_page() and dissolve_free_huge_page().

We should make sure that the page is already on the free list when it is dissolved.

Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")
---
 mm/hugetlb.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
mm/hugetlb.c +41 -0
index 4741d60f8955..1b789d1fd06b 100644
@@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock);
 static int num_fault_mutexes;
 struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
 
+static inline bool PageHugeFreed(struct page *head)
+{
+	return page_private(head + 4) == -1UL;
+}
+
+static inline void SetPageHugeFreed(struct page *head)
+{
+	set_page_private(head + 4, -1UL);
+}
+
+static inline void ClearPageHugeFreed(struct page *head)
+{
+	set_page_private(head + 4, 0);
+}
+
 /* Forward declaration */
 static int hugetlb_acct_memory(struct hstate *h, long delta);
 
@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)
 	list_move(&page->lru, &h->hugepage_freelists[nid]);
 	h->free_huge_pages++;
 	h->free_huge_pages_node[nid]++;
+	SetPageHugeFreed(page);
 }
 
 static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)
 
 		list_move(&page->lru, &h->hugepage_activelist);
 		set_page_refcounted(page);
+		ClearPageHugeFreed(page);
 		h->free_huge_pages--;
 		h->free_huge_pages_node[nid]--;
 		return page;
@@ -1504,6 +1521,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
 	spin_lock(&hugetlb_lock);
 	h->nr_huge_pages++;
 	h->nr_huge_pages_node[nid]++;
+	ClearPageHugeFreed(page);
 	spin_unlock(&hugetlb_lock);
 }
 
@@ -1754,6 +1772,7 @@ int dissolve_free_huge_page(struct page *page)
 {
 	int rc = -EBUSY;
 
+retry:
 	/* Not to disrupt normal path by vainly holding hugetlb_lock */
 	if (!PageHuge(page))
 		return 0;
@@ -1770,6 +1789,28 @@ int dissolve_free_huge_page(struct page *page)
 		int nid = page_to_nid(head);
 		if (h->free_huge_pages - h->resv_huge_pages == 0)
 			goto out;
+
+		/*
+		 * We should make sure that the page is already on the free list
+		 * when it is dissolved.
+		 */
+		if (unlikely(!PageHugeFreed(head))) {
+			spin_unlock(&hugetlb_lock);
+
+			/*
+			 * Theoretically, we should return -EBUSY when we
+			 * encounter this race. In fact, we have a chance
+			 * to successfully dissolve the page if we do a
+			 * retry. Because the race window is quite small.
+			 * If we seize this opportunity, it is an optimization
+			 * for increasing the success rate of dissolving page.
+			 */
+			while (PageHeadHuge(head) && !PageHugeFreed(head))
+				cond_resched();
+
+			goto retry;
+		}
+
 		/*
 		 * Move PageHWPoison flag from head page to the raw error page,
 		 * which makes any subpages rather than the error page reusable.
Signature · 1 line
2.11.0
#4 lore raw

Re: [PATCH v5 3/5] mm: hugetlb: fix a race between freeing and dissolving the page

Quoted 20 lines · Muchun Song

There is a race condition between __free_huge_page() and dissolve_free_huge_page().

CPU0: CPU1:

// page_count(page) == 1
put_page(page)
__free_huge_page(page)

                              dissolve_free_huge_page(page)
                                spin_lock(&hugetlb_lock)
                                // PageHuge(page) && !page_count(page)
                                update_and_free_page(page)
                                // page is freed to the buddy
                                spin_unlock(&hugetlb_lock)
    spin_lock(&hugetlb_lock)
    clear_page_huge_active(page)
    enqueue_huge_page(page)
    // It is wrong, the page is already freed
    spin_unlock(&hugetlb_lock)
Quoted 4 lines

The race windows is between put_page() and dissolve_free_huge_page().

We should make sure that the page is already on the free list when it is dissolved.

Please describe user visible effects as suggested in
http://lkml.kernel.org/r/[email protected]

Quoted 7 lines
Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")
---
 mm/hugetlb.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

[...]

Quoted 16 lines
mm/hugetlb.c +9 -0
+retry:
 	/* Not to disrupt normal path by vainly holding hugetlb_lock */
 	if (!PageHuge(page))
 		return 0;
@@ -1770,6 +1789,28 @@ int dissolve_free_huge_page(struct page *page)
 		int nid = page_to_nid(head);
 		if (h->free_huge_pages - h->resv_huge_pages == 0)
 			goto out;
+
+		/*
+		 * We should make sure that the page is already on the free list
+		 * when it is dissolved.
+		 */
+		if (unlikely(!PageHugeFreed(head))) {
+			spin_unlock(&hugetlb_lock);
+

mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1809

Quoted 10 lines
mm/hugetlb.c +10 -0
+			/*
+			 * Theoretically, we should return -EBUSY when we
+			 * encounter this race. In fact, we have a chance
+			 * to successfully dissolve the page if we do a
+			 * retry. Because the race window is quite small.
+			 * If we seize this opportunity, it is an optimization
+			 * for increasing the success rate of dissolving page.
+			 */
+			while (PageHeadHuge(head) && !PageHugeFreed(head))
+				cond_resched();

Sorry, I should have raised that when replying to the previous version already but we have focused more on other things. Is there any special reason that you didn't simply

	if (!PageHugeFreed(head)) {
		spin_unlock(&hugetlb_lock);
		cond_resched();
		goto retry;
	}

This would be less code and a very slight advantage would be that the waiter might get blocked on the spin lock while the concurrent freeing is happening. But maybe you wanted to avoid exactly this contention? Please put your thinking into the changelog.

Quoted 9 lines
mm/hugetlb.c +4 -0
+
+			goto retry;
+		}
+
 		/*
 		 * Move PageHWPoison flag from head page to the raw error page,
 		 * which makes any subpages rather than the error page reusable.
Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw

Re: [External] Re: [PATCH v5 3/5] mm: hugetlb: fix a race between freeing and dissolving the page

Quoted 22 lines · Michal Hocko
Quoted 20 lines · Muchun Song

There is a race condition between __free_huge_page() and dissolve_free_huge_page().

CPU0: CPU1:

// page_count(page) == 1
put_page(page)
__free_huge_page(page)

                              dissolve_free_huge_page(page)
                                spin_lock(&hugetlb_lock)
                                // PageHuge(page) && !page_count(page)
                                update_and_free_page(page)
                                // page is freed to the buddy
                                spin_unlock(&hugetlb_lock)
    spin_lock(&hugetlb_lock)
    clear_page_huge_active(page)
    enqueue_huge_page(page)
    // It is wrong, the page is already freed
    spin_unlock(&hugetlb_lock)
Quoted 7 lines
Quoted 4 lines

The race windows is between put_page() and dissolve_free_huge_page().

We should make sure that the page is already on the free list when it is dissolved.

Please describe user visible effects as suggested in
http://lkml.kernel.org/r/[email protected]

Sorry forgot to update this.

Quoted 45 lines
Quoted 8 lines
Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")
---
 mm/hugetlb.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

[...]

Quoted 16 lines
mm/hugetlb.c +9 -0
+retry:
      /* Not to disrupt normal path by vainly holding hugetlb_lock */
      if (!PageHuge(page))
              return 0;
@@ -1770,6 +1789,28 @@ int dissolve_free_huge_page(struct page *page)
              int nid = page_to_nid(head);
              if (h->free_huge_pages - h->resv_huge_pages == 0)
                      goto out;
+
+             /*
+              * We should make sure that the page is already on the free list
+              * when it is dissolved.
+              */
+             if (unlikely(!PageHugeFreed(head))) {
+                     spin_unlock(&hugetlb_lock);
+

mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1809

Quoted 10 lines
mm/hugetlb.c +10 -0
+                     /*
+                      * Theoretically, we should return -EBUSY when we
+                      * encounter this race. In fact, we have a chance
+                      * to successfully dissolve the page if we do a
+                      * retry. Because the race window is quite small.
+                      * If we seize this opportunity, it is an optimization
+                      * for increasing the success rate of dissolving page.
+                      */
+                     while (PageHeadHuge(head) && !PageHugeFreed(head))
+                             cond_resched();

Sorry, I should have raised that when replying to the previous version already but we have focused more on other things. Is there any special reason that you didn't simply

        if (!PageHugeFreed(head)) {
                spin_unlock(&hugetlb_lock);
                cond_resched();
                goto retry;
        }
Quoted 4 lines

This would be less code and a very slight advantage would be that the waiter might get blocked on the spin lock while the concurrent freeing is happening. But maybe you wanted to avoid exactly this contention? Please put your thinking into the changelog.

I want to avoid the lock contention. I will add this reason to the changelog. Thanks.

Quoted 14 lines
Quoted 10 lines
mm/hugetlb.c +4 -1
+
+                     goto retry;
+             }
+
              /*
               * Move PageHWPoison flag from head page to the raw error page,
               * which makes any subpages rather than the error page reusable.
--

2.11.0

--
Michal Hocko
SUSE Labs

#6 lore raw

Re: [External] Re: [PATCH v5 3/5] mm: hugetlb: fix a race between freeing and dissolving the page

Quoted 1 line · Muchun Song

On Thu, Jan 14, 2021 at 9:20 PM Michal Hocko <[email protected]> wrote:

[...]

Quoted 32 lines
Quoted 32 lines
Quoted 12 lines
mm/hugetlb.c +8 -0
@@ -1770,6 +1789,28 @@ int dissolve_free_huge_page(struct page *page)
              int nid = page_to_nid(head);
              if (h->free_huge_pages - h->resv_huge_pages == 0)
                      goto out;
+
+             /*
+              * We should make sure that the page is already on the free list
+              * when it is dissolved.
+              */
+             if (unlikely(!PageHugeFreed(head))) {
+                     spin_unlock(&hugetlb_lock);
+

mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1809

Quoted 10 lines
mm/hugetlb.c +10 -0
+                     /*
+                      * Theoretically, we should return -EBUSY when we
+                      * encounter this race. In fact, we have a chance
+                      * to successfully dissolve the page if we do a
+                      * retry. Because the race window is quite small.
+                      * If we seize this opportunity, it is an optimization
+                      * for increasing the success rate of dissolving page.
+                      */
+                     while (PageHeadHuge(head) && !PageHugeFreed(head))
+                             cond_resched();

Sorry, I should have raised that when replying to the previous version already but we have focused more on other things. Is there any special reason that you didn't simply

        if (!PageHugeFreed(head)) {
                spin_unlock(&hugetlb_lock);
                cond_resched();
                goto retry;
        }
Quoted 7 lines
Quoted 4 lines

This would be less code and a very slight advantage would be that the waiter might get blocked on the spin lock while the concurrent freeing is happening. But maybe you wanted to avoid exactly this contention? Please put your thinking into the changelog.

I want to avoid the lock contention. I will add this reason to the changelog. Thanks.

Please also explain why it matters and whether an unintended contention is a real problem.

Signature · 2 lines
Michal Hocko
SUSE Labs
#7 lore raw

Re: [External] Re: [PATCH v5 3/5] mm: hugetlb: fix a race between freeing and dissolving the page

Quoted 41 lines · Michal Hocko
Quoted 1 line · Muchun Song

On Thu, Jan 14, 2021 at 9:20 PM Michal Hocko <[email protected]> wrote:

[...]

Quoted 37 lines
Quoted 36 lines
Quoted 12 lines
mm/hugetlb.c +8 -0
@@ -1770,6 +1789,28 @@ int dissolve_free_huge_page(struct page *page)
              int nid = page_to_nid(head);
              if (h->free_huge_pages - h->resv_huge_pages == 0)
                      goto out;
+
+             /*
+              * We should make sure that the page is already on the free list
+              * when it is dissolved.
+              */
+             if (unlikely(!PageHugeFreed(head))) {
+                     spin_unlock(&hugetlb_lock);
+

mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1809

Quoted 10 lines
mm/hugetlb.c +10 -0
+                     /*
+                      * Theoretically, we should return -EBUSY when we
+                      * encounter this race. In fact, we have a chance
+                      * to successfully dissolve the page if we do a
+                      * retry. Because the race window is quite small.
+                      * If we seize this opportunity, it is an optimization
+                      * for increasing the success rate of dissolving page.
+                      */
+                     while (PageHeadHuge(head) && !PageHugeFreed(head))
+                             cond_resched();

Sorry, I should have raised that when replying to the previous version already but we have focused more on other things. Is there any special reason that you didn't simply

        if (!PageHugeFreed(head)) {
                spin_unlock(&hugetlb_lock);
                cond_resched();
                goto retry;
        }

This would be less code and a very slight advantage would be that the waiter might get blocked on the spin lock while the concurrent freeing is happening. But maybe you wanted to avoid exactly this contention? Please put your thinking into the changelog.

Quoted 5 lines
Quoted 2 lines

I want to avoid the lock contention. I will add this reason to the changelog. Thanks.

Please also explain why it matters and whether an unintended contention is a real problem.

I have no idea about this, it is just my opinion.
I will follow your suggestion.

Quoted 3 lines

--
Michal Hocko
SUSE Labs

#8 lore raw

PATCH v5 4/5 mm: hugetlb: fix a race between isolating and freeing page

In reply to Muchun Song

There is a race between isolate_huge_page() and __free_huge_page().

CPU0: CPU1:

if (PageHuge(page))

                                            put_page(page)
                                              __free_huge_page(page)
                                                  spin_lock(&hugetlb_lock)
                                                  update_and_free_page(page)
                                                    set_compound_page_dtor(page,
                                                      NULL_COMPOUND_DTOR)
                                                  spin_unlock(&hugetlb_lock)
  isolate_huge_page(page)
    // trigger BUG_ON
    VM_BUG_ON_PAGE(!PageHead(page), page)
    spin_lock(&hugetlb_lock)
    page_huge_active(page)
      // trigger BUG_ON
      VM_BUG_ON_PAGE(!PageHuge(page), page)
    spin_unlock(&hugetlb_lock)

When we isolate a HugeTLB page on CPU0. Meanwhile, we free it to the buddy allocator on CPU1. Then, we can trigger a BUG_ON on CPU0. Because it is already freed to the buddy allocator.

Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")
---
 mm/hugetlb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/hugetlb.c +2 -2
index 1b789d1fd06b..43b7a044f248 100644
@@ -5574,9 +5574,9 @@ bool isolate_huge_page(struct page *page, struct list_head *list)
 {
 	bool ret = true;
 
-	VM_BUG_ON_PAGE(!PageHead(page), page);
 	spin_lock(&hugetlb_lock);
-	if (!page_huge_active(page) || !get_page_unless_zero(page)) {
+	if (!PageHeadHuge(page) || !page_huge_active(page) ||
+	    !get_page_unless_zero(page)) {
 		ret = false;
 		goto unlock;
 	}
Signature · 1 line
2.11.0
#9 lore raw

PATCH v5 5/5 mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active

In reply to Muchun Song

The page_huge_active() can be called from scan_movable_pages() which do not hold a reference count to the HugeTLB page. So when we call page_huge_active() from scan_movable_pages(), the HugeTLB page can be freed parallel. Then we will trigger a BUG_ON which is in the page_huge_active() when CONFIG_DEBUG_VM is enabled. Just remove the VM_BUG_ON_PAGE.

Fixes 7e1f049efb86 ("mm: hugetlb: cleanup using paeg_huge_active()")
---
 mm/hugetlb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/hugetlb.c +1 -2
index 43b7a044f248..6c5dfef682cc 100644
@@ -1360,8 +1360,7 @@ struct hstate *size_to_hstate(unsigned long size)
  */
 bool page_huge_active(struct page *page)
 {
-	VM_BUG_ON_PAGE(!PageHuge(page), page);
-	return PageHead(page) && PagePrivate(&page[1]);
+	return PageHeadHuge(page) && PagePrivate(&page[1]);
 }
 
 /* never called for tail page */
Signature · 1 line
2.11.0