Skip to contentSkip to conversation

mm: migrate: do not migrate HugeTLB page whose refcount is one

PATCH Superseded Muchun Song 6 participants 31 messages Jan 4, 2021 – Jan 6, 2021

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

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

If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.

---
 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
#1 lore raw

PATCH 2/6 hugetlbfs: fix cannot migrate the fallocated HugeTLB page

Because we only can isolate a active page via isolate_huge_page() and hugetlbfs_fallocate() forget to mark it as active, we cannot isolate and migrate those pages.

Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())
---
 fs/hugetlbfs/inode.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +3 -2
index b5c109703daa..2aceb085d202 100644
@@ -737,10 +737,11 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		/*
 		 * unlock_page because locked by add_to_page_cache()
-		 * page_put due to reference from alloc_huge_page()
+		 * put_page() (which is in the putback_active_hugepage())
+		 * due to reference from alloc_huge_page()
 		 */
 		unlock_page(page);
-		put_page(page);
+		putback_active_hugepage(page);
 	}
 
 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
Signature · 1 line
2.11.0
#2 lore raw

Re: [PATCH 2/6] hugetlbfs: fix cannot migrate the fallocated HugeTLB page

Quoted 9 lines · Muchun Song

Because we only can isolate a active page via isolate_huge_page() and hugetlbfs_fallocate() forget to mark it as active, we cannot isolate and migrate those pages.

Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())
---
 fs/hugetlbfs/inode.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Good catch. This is indeed an issue.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 741

Quoted 12 lines
fs/hugetlbfs/inode.c +2 -1
index b5c109703daa..2aceb085d202 100644
@@ -737,10 +737,11 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
 		/*
 		 * unlock_page because locked by add_to_page_cache()
-		 * page_put due to reference from alloc_huge_page()
+		 * put_page() (which is in the putback_active_hugepage())
+		 * due to reference from alloc_huge_page()

Thanks for fixing the comment.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 744

Quoted 4 lines
fs/hugetlbfs/inode.c +1 -1
 		 */
 		unlock_page(page);
-		put_page(page);
+		putback_active_hugepage(page);

I'm curious why you used putback_active_hugepage() here instead of simply calling set_page_huge_active() before the put_page()?

When the page was allocated, it was placed on the active list (alloc_huge_page). Therefore, the hugetlb_lock locking and list movement should not be necessary.

Signature · 1 line
Mike Kravetz
Quoted 4 lines
fs/hugetlbfs/inode.c +0 -0
 	}
 
 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
#3 lore raw

Re: [External] Re: [PATCH 2/6] hugetlbfs: fix cannot migrate the fallocated HugeTLB page

Quoted 34 lines · Mike Kravetz
Quoted 9 lines · Muchun Song

Because we only can isolate a active page via isolate_huge_page() and hugetlbfs_fallocate() forget to mark it as active, we cannot isolate and migrate those pages.

Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())
---
 fs/hugetlbfs/inode.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Good catch. This is indeed an issue.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 741

Quoted 12 lines
fs/hugetlbfs/inode.c +2 -1
index b5c109703daa..2aceb085d202 100644
@@ -737,10 +737,11 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
              /*
               * unlock_page because locked by add_to_page_cache()
-              * page_put due to reference from alloc_huge_page()
+              * put_page() (which is in the putback_active_hugepage())
+              * due to reference from alloc_huge_page()

Thanks for fixing the comment.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 744

Quoted 4 lines
fs/hugetlbfs/inode.c +1 -1
               */
              unlock_page(page);
-             put_page(page);
+             putback_active_hugepage(page);
Quoted 5 lines

I'm curious why you used putback_active_hugepage() here instead of simply calling set_page_huge_active() before the put_page()?

When the page was allocated, it was placed on the active list (alloc_huge_page). Therefore, the hugetlb_lock locking and list movement should not be necessary.

I agree with you. Because set_page_huge_active is not exported (static function). Only exporting set_page_huge_active seems strange (leaving clear_page_huge_active not export). This is just my opinion. What's yours, Mike?

Thanks.

Quoted 8 lines

--
Mike Kravetz

Quoted 4 lines
fs/hugetlbfs/inode.c +0 -0
      }
 
      if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
#4 lore raw

Re: [External] Re: [PATCH 2/6] hugetlbfs: fix cannot migrate the fallocated HugeTLB page

Quoted 38 lines · Muchun Song
Quoted 37 lines · Mike Kravetz
Quoted 9 lines · Muchun Song

Because we only can isolate a active page via isolate_huge_page() and hugetlbfs_fallocate() forget to mark it as active, we cannot isolate and migrate those pages.

Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())
---
 fs/hugetlbfs/inode.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Good catch. This is indeed an issue.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 741

Quoted 12 lines
fs/hugetlbfs/inode.c +2 -1
index b5c109703daa..2aceb085d202 100644
@@ -737,10 +737,11 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
              /*
               * unlock_page because locked by add_to_page_cache()
-              * page_put due to reference from alloc_huge_page()
+              * put_page() (which is in the putback_active_hugepage())
+              * due to reference from alloc_huge_page()

Thanks for fixing the comment.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 744

Quoted 4 lines
fs/hugetlbfs/inode.c +1 -1
               */
              unlock_page(page);
-             put_page(page);
+             putback_active_hugepage(page);

I'm curious why you used putback_active_hugepage() here instead of simply calling set_page_huge_active() before the put_page()?

Quoted 7 lines
Quoted 2 lines

When the page was allocated, it was placed on the active list (alloc_huge_page). Therefore, the hugetlb_lock locking and list movement should not be necessary.

I agree with you. Because set_page_huge_active is not exported (static function). Only exporting set_page_huge_active seems strange (leaving clear_page_huge_active not export). This is just my opinion. What's yours, Mike?

I'm thinking that we should export (make external) set_page_huge_active. We can leave clear_page_huge_active as static and just add something to the commit log noting that there are no external users.

My primary reason for doing this is to eliminate the extra and unnecessary per-page lock/unlock cycle. I believe there are some applications that use fallocate to pre-allocate very large hugetlbfs files. They may notice the extra overhead.

Signature · 1 line
Mike Kravetz
#5 lore raw

Re: [External] Re: [PATCH 2/6] hugetlbfs: fix cannot migrate the fallocated HugeTLB page

Quoted 48 lines · Mike Kravetz
Quoted 38 lines · Muchun Song
Quoted 37 lines · Mike Kravetz
Quoted 9 lines · Muchun Song

Because we only can isolate a active page via isolate_huge_page() and hugetlbfs_fallocate() forget to mark it as active, we cannot isolate and migrate those pages.

Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())
---
 fs/hugetlbfs/inode.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Good catch. This is indeed an issue.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 741

Quoted 12 lines
fs/hugetlbfs/inode.c +2 -1
index b5c109703daa..2aceb085d202 100644
@@ -737,10 +737,11 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
 
              /*
               * unlock_page because locked by add_to_page_cache()
-              * page_put due to reference from alloc_huge_page()
+              * put_page() (which is in the putback_active_hugepage())
+              * due to reference from alloc_huge_page()

Thanks for fixing the comment.

fs/hugetlbfs/inode.c static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, · line 744

Quoted 4 lines
fs/hugetlbfs/inode.c +1 -1
               */
              unlock_page(page);
-             put_page(page);
+             putback_active_hugepage(page);

I'm curious why you used putback_active_hugepage() here instead of simply calling set_page_huge_active() before the put_page()?

Quoted 7 lines
Quoted 2 lines

When the page was allocated, it was placed on the active list (alloc_huge_page). Therefore, the hugetlb_lock locking and list movement should not be necessary.

I agree with you. Because set_page_huge_active is not exported (static function). Only exporting set_page_huge_active seems strange (leaving clear_page_huge_active not export). This is just my opinion. What's yours, Mike?

Quoted 8 lines

I'm thinking that we should export (make external) set_page_huge_active. We can leave clear_page_huge_active as static and just add something to the commit log noting that there are no external users.

My primary reason for doing this is to eliminate the extra and unnecessary per-page lock/unlock cycle. I believe there are some applications that use fallocate to pre-allocate very large hugetlbfs files. They may notice the extra overhead.

Agree. Will do in the next version. Thanks.

Quoted 2 lines

--
Mike Kravetz

#6 lore raw

PATCH 3/6 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 spin_lock() which is in the __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 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
mm/hugetlb.c +48 -0
index 1f3bf1710b66..72608008f8b4 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) == -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);
 }
 
@@ -1770,6 +1788,36 @@ 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;
+
+		/*
+		 * 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)
+		 *     enqueue_huge_page(page)
+		 *     // It is wrong, the page is already freed
+		 *     spin_unlock(&hugetlb_lock)
+		 *
+		 * The race window is between put_page() and spin_lock() which
+		 * is in the __free_huge_page().
+		 *
+		 * We should make sure that the page is already on the free list
+		 * when it is dissolved.
+		 */
+		if (unlikely(!PageHugeFreed(head)))
+			goto out;
+
 		/*
 		 * 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
#7 lore raw

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

Quoted 37 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)

The race windows is between put_page() and spin_lock() which is in the __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 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..72608008f8b4 100644

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 84

Quoted 6 lines
mm/hugetlb.c +3 -0
@@ -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) == -1UL;
	return page_private(head + 4) == -1UL;

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 95

Quoted 11 lines
mm/hugetlb.c +11 -0
+}
+
+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);
+}

It is unfortunate that we can not use some existing value like page_huge_active() to determine if dissolve_free_huge_page() should proceed with freeing the page to buddy. If the existing check,

	if (!page_count(page)) {

was changed to

	if (!page_count(page) && !page_huge_active(page)) {

the race window would be shrunk. However, the most straight forward way to fully close the window is with the approach taken here.

Quoted 45 lines
mm/hugetlb.c +17 -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);
 }
 
@@ -1770,6 +1788,36 @@ 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;
+
+		/*
+		 * 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)

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

Quoted 10 lines
mm/hugetlb.c +10 -0
+		 *                                 update_and_free_page(page)
+		 *                                 // page is freed to the buddy
+		 *                                 spin_unlock(&hugetlb_lock)
+		 *     spin_lock(&hugetlb_lock)
+		 *     enqueue_huge_page(page)
+		 *     // It is wrong, the page is already freed
+		 *     spin_unlock(&hugetlb_lock)
+		 *
+		 * The race window is between put_page() and spin_lock() which
+		 * is in the __free_huge_page().

IMO, the description of the race condition in the commit message is sufficient. It does not need to be here in the code. The below comment should be sufficient.

Signature · 1 line
Mike Kravetz
Quoted 11 lines
mm/hugetlb.c +7 -0
+		 *
+		 * We should make sure that the page is already on the free list
+		 * when it is dissolved.
+		 */
+		if (unlikely(!PageHugeFreed(head)))
+			goto out;
+
 		/*
 		 * Move PageHWPoison flag from head page to the raw error page,
 		 * which makes any subpages rather than the error page reusable.
#8 lore raw

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

Quoted 64 lines · Mike Kravetz
Quoted 37 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)

The race windows is between put_page() and spin_lock() which is in the __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 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..72608008f8b4 100644

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 84

Quoted 6 lines
mm/hugetlb.c +3 -0
@@ -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) == -1UL;
        return page_private(head + 4) == -1UL;

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 95

Quoted 11 lines
mm/hugetlb.c +11 -0
+}
+
+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);
+}

It is unfortunate that we can not use some existing value like page_huge_active() to determine if dissolve_free_huge_page() should proceed with freeing the page to buddy. If the existing check,

Quoted 8 lines
        if (!page_count(page)) {

was changed to

        if (!page_count(page) && !page_huge_active(page)) {

the race window would be shrunk. However, the most straight forward way to fully close the window is with the approach taken here.

I also thought about this fix. But this is not enough. Because we just call put_page to free the HugeTLB page without setting activeness in some place (e.g. error handling routines).

If we use page_huge_active, we should set activeness before put_page. But we cannot guarantee this.

Thanks.

Quoted 52 lines
Quoted 52 lines
mm/hugetlb.c +23 -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);
 }
 
@@ -1770,6 +1788,36 @@ 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;
+
+             /*
+              * 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)
+              *     enqueue_huge_page(page)
+              *     // It is wrong, the page is already freed
Quoted 8 lines

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

Quoted 4 lines
mm/hugetlb.c +4 -0
+              *     spin_unlock(&hugetlb_lock)
+              *
+              * The race window is between put_page() and spin_lock() which
+              * is in the __free_huge_page().

IMO, the description of the race condition in the commit message is sufficient. It does not need to be here in the code. The below comment should be sufficient.

Thanks. Will do.

Quoted 15 lines

--
Mike Kravetz

Quoted 11 lines
mm/hugetlb.c +7 -0
+              *
+              * We should make sure that the page is already on the free list
+              * when it is dissolved.
+              */
+             if (unlikely(!PageHugeFreed(head)))
+                     goto out;
+
              /*
               * Move PageHWPoison flag from head page to the raw error page,
               * which makes any subpages rather than the error page reusable.
#9 lore raw

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

Quoted 74 lines · Muchun Song
Quoted 64 lines · Mike Kravetz
Quoted 37 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)

The race windows is between put_page() and spin_lock() which is in the __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 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..72608008f8b4 100644

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 84

Quoted 6 lines
mm/hugetlb.c +3 -0
@@ -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) == -1UL;
        return page_private(head + 4) == -1UL;

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 95

Quoted 11 lines
mm/hugetlb.c +11 -0
+}
+
+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);
+}

It is unfortunate that we can not use some existing value like page_huge_active() to determine if dissolve_free_huge_page() should proceed with freeing the page to buddy. If the existing check,

Quoted 8 lines
        if (!page_count(page)) {

was changed to

        if (!page_count(page) && !page_huge_active(page)) {

the race window would be shrunk. However, the most straight forward way to fully close the window is with the approach taken here.

Quoted 7 lines

I also thought about this fix. But this is not enough. Because we just call put_page to free the HugeTLB page without setting activeness in some place (e.g. error handling routines).

If we use page_huge_active, we should set activeness before put_page. But we cannot guarantee this.

Just FYI,
I went back and explored the option of doing set_page_huge_active when a page was put on the active list and clear_page_huge_active when put on the free list. This would be much like what you are doing with PageHugeFreed. Commit bcc54222309c which added page_huge_active implied that this was possible. Then I remembered a race fixed in cb6acd01e2e4 that required delaying the call to set_page_huge_active in hugetlb_no_page. So, such a scheme would not work.

Also,
It seems we could use head[3].mapping for PageHugeFreed ? Not much of an advantage. It does not add another tail page needed to store page metadata. And, this fits within the already defined HUGETLB_CGROUP_MIN_ORDER.

Signature · 1 line
Mike Kravetz
#10 lore raw

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

Quoted 84 lines · Mike Kravetz
Quoted 74 lines · Muchun Song
Quoted 64 lines · Mike Kravetz
Quoted 37 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)

The race windows is between put_page() and spin_lock() which is in the __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 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..72608008f8b4 100644

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 84

Quoted 6 lines
mm/hugetlb.c +3 -0
@@ -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) == -1UL;
        return page_private(head + 4) == -1UL;

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 95

Quoted 11 lines
mm/hugetlb.c +11 -0
+}
+
+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);
+}

It is unfortunate that we can not use some existing value like page_huge_active() to determine if dissolve_free_huge_page() should proceed with freeing the page to buddy. If the existing check,

Quoted 8 lines
        if (!page_count(page)) {

was changed to

        if (!page_count(page) && !page_huge_active(page)) {

the race window would be shrunk. However, the most straight forward way to fully close the window is with the approach taken here.

Quoted 7 lines

I also thought about this fix. But this is not enough. Because we just call put_page to free the HugeTLB page without setting activeness in some place (e.g. error handling routines).

If we use page_huge_active, we should set activeness before put_page. But we cannot guarantee this.

Quoted 8 lines

Just FYI,
I went back and explored the option of doing set_page_huge_active when a page was put on the active list and clear_page_huge_active when put on the free list. This would be much like what you are doing with PageHugeFreed. Commit bcc54222309c which added page_huge_active implied that this was possible. Then I remembered a race fixed in cb6acd01e2e4 that required delaying the call to set_page_huge_active in hugetlb_no_page. So, such a scheme would not work.

Sounds like a tortuous story. :)

Quoted 6 lines

Also,
It seems we could use head[3].mapping for PageHugeFreed ? Not much of an advantage. It does not add another tail page needed to store page metadata. And, this fits within the already defined HUGETLB_CGROUP_MIN_ORDER.

It is fine to me. Will do. Thanks.

Quoted 2 lines

--
Mike Kravetz

#11 lore raw

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

In reply to Mike Kravetz
Quoted 42 lines · Mike Kravetz
Quoted 40 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)

The race windows is between put_page() and spin_lock() which is in the __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 | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
mm/hugetlb.c +0 -0
index 1f3bf1710b66..72608008f8b4 100644
@@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock);
 static int num_fault_mutexes;
 struct mutex *hugetlb_fault_mutex_table ____cacheline_aligned_in_smp;
 
Quoted 5 lines

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 84

Quoted 3 lines
mm/hugetlb.c +3 -0
+static inline bool PageHugeFreed(struct page *head)
+{
+     return page_private(head) == -1UL;
        return page_private(head + 4) == -1UL;

Very thanks. It's my mistake when rebasing. Will
update in the next version.

Quoted 100 lines

mm/hugetlb.c DEFINE_SPINLOCK(hugetlb_lock); · line 95

Quoted 12 lines
mm/hugetlb.c +11 -0
+}
+
+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);
+}

It is unfortunate that we can not use some existing value like page_huge_active() to determine if dissolve_free_huge_page() should proceed with freeing the page to buddy. If the existing check,

        if (!page_count(page)) {

was changed to

        if (!page_count(page) && !page_huge_active(page)) {

the race window would be shrunk. However, the most straight forward way to fully close the window is with the approach taken here.

Quoted 45 lines
mm/hugetlb.c +17 -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);
 }
 
@@ -1770,6 +1788,36 @@ 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;
+
+             /*
+              * 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)

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

Quoted 10 lines
mm/hugetlb.c +10 -0
+              *                                 update_and_free_page(page)
+              *                                 // page is freed to the buddy
+              *                                 spin_unlock(&hugetlb_lock)
+              *     spin_lock(&hugetlb_lock)
+              *     enqueue_huge_page(page)
+              *     // It is wrong, the page is already freed
+              *     spin_unlock(&hugetlb_lock)
+              *
+              * The race window is between put_page() and spin_lock() which
+              * is in the __free_huge_page().

IMO, the description of the race condition in the commit message is sufficient. It does not need to be here in the code. The below comment should be sufficient.

--
Mike Kravetz

Quoted 11 lines
mm/hugetlb.c +7 -0
+              *
+              * We should make sure that the page is already on the free list
+              * when it is dissolved.
+              */
+             if (unlikely(!PageHugeFreed(head)))
+                     goto out;
+
              /*
               * Move PageHWPoison flag from head page to the raw error page,
               * which makes any subpages rather than the error page reusable.
#12 lore raw

PATCH 4/6 mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

In reply to Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

---
 mm/hugetlb.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
mm/hugetlb.c +11 -5
index 72608008f8b4..db00ae375d2a 100644
@@ -1763,10 +1763,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  * nothing for in-use hugepages and non-hugepages.
  * This function returns values like below:
  *
- *  -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
- *          (allocated or reserved.)
- *       0: successfully dissolved free hugepages or the page is not a
- *          hugepage (considered as already dissolved)
+ *  -EAGAIN: race with __free_huge_page() and can do a retry
+ *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
+ *           (allocated or reserved.)
+ *       0:  successfully dissolved free hugepages or the page is not a
+ *           hugepage (considered as already dissolved)
  */
 int dissolve_free_huge_page(struct page *page)
 {
@@ -1815,8 +1816,10 @@ int dissolve_free_huge_page(struct page *page)
 		 * We should make sure that the page is already on the free list
 		 * when it is dissolved.
 		 */
-		if (unlikely(!PageHugeFreed(head)))
+		if (unlikely(!PageHugeFreed(head))) {
+			rc = -EAGAIN;
 			goto out;
+		}
 
 		/*
 		 * Move PageHWPoison flag from head page to the raw error page,
@@ -1857,7 +1860,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
 
 	for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
 		page = pfn_to_page(pfn);
+retry:
 		rc = dissolve_free_huge_page(page);
+		if (rc == -EAGAIN)
+			goto retry;
 		if (rc)
 			break;
 	}
Signature · 1 line
2.11.0
#13 lore raw

Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

Quoted 2 lines · Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

In general, I agree that the race window is small. However, worst case would be if the freeing of the page is put on a work queue. Is it acceptable to keep retrying in that case? In addition, the 'Free some vmemmap' series may slow the free_huge_page path even more.

In these worst case scenarios, I am not sure we want to just spin retrying.

Signature · 1 line
Mike Kravetz
Quoted 50 lines
---
 mm/hugetlb.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
mm/hugetlb.c +11 -5
index 72608008f8b4..db00ae375d2a 100644
@@ -1763,10 +1763,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  * nothing for in-use hugepages and non-hugepages.
  * This function returns values like below:
  *
- *  -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
- *          (allocated or reserved.)
- *       0: successfully dissolved free hugepages or the page is not a
- *          hugepage (considered as already dissolved)
+ *  -EAGAIN: race with __free_huge_page() and can do a retry
+ *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
+ *           (allocated or reserved.)
+ *       0:  successfully dissolved free hugepages or the page is not a
+ *           hugepage (considered as already dissolved)
  */
 int dissolve_free_huge_page(struct page *page)
 {
@@ -1815,8 +1816,10 @@ int dissolve_free_huge_page(struct page *page)
 		 * We should make sure that the page is already on the free list
 		 * when it is dissolved.
 		 */
-		if (unlikely(!PageHugeFreed(head)))
+		if (unlikely(!PageHugeFreed(head))) {
+			rc = -EAGAIN;
 			goto out;
+		}
 
 		/*
 		 * Move PageHWPoison flag from head page to the raw error page,
@@ -1857,7 +1860,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
 
 	for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
 		page = pfn_to_page(pfn);
+retry:
 		rc = dissolve_free_huge_page(page);
+		if (rc == -EAGAIN)
+			goto retry;
 		if (rc)
 			break;
 	}
#14 lore raw

Re: [External] Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

Quoted 9 lines · Mike Kravetz
Quoted 2 lines · Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

In general, I agree that the race window is small. However, worst case would be if the freeing of the page is put on a work queue. Is it acceptable to keep retrying in that case? In addition, the 'Free some vmemmap' series may slow the free_huge_page path even more.

I also consider the 'Free some vmemmap' series case. In my next version series, I will flush the work before dissolve_free_huge_page returns when encountering this race. So the retry is acceptable. Right?

Thanks.

Quoted 56 lines

In these worst case scenarios, I am not sure we want to just spin retrying.

--
Mike Kravetz

Quoted 50 lines
---
 mm/hugetlb.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
mm/hugetlb.c +11 -5
index 72608008f8b4..db00ae375d2a 100644
@@ -1763,10 +1763,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  * nothing for in-use hugepages and non-hugepages.
  * This function returns values like below:
  *
- *  -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
- *          (allocated or reserved.)
- *       0: successfully dissolved free hugepages or the page is not a
- *          hugepage (considered as already dissolved)
+ *  -EAGAIN: race with __free_huge_page() and can do a retry
+ *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
+ *           (allocated or reserved.)
+ *       0:  successfully dissolved free hugepages or the page is not a
+ *           hugepage (considered as already dissolved)
  */
 int dissolve_free_huge_page(struct page *page)
 {
@@ -1815,8 +1816,10 @@ int dissolve_free_huge_page(struct page *page)
               * We should make sure that the page is already on the free list
               * when it is dissolved.
               */
-             if (unlikely(!PageHugeFreed(head)))
+             if (unlikely(!PageHugeFreed(head))) {
+                     rc = -EAGAIN;
                      goto out;
+             }
 
              /*
               * Move PageHWPoison flag from head page to the raw error page,
@@ -1857,7 +1860,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
 
      for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
              page = pfn_to_page(pfn);
+retry:
              rc = dissolve_free_huge_page(page);
+             if (rc == -EAGAIN)
+                     goto retry;
              if (rc)
                      break;
      }
#15 lore raw

Re: [External] Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

Quoted 12 lines · Muchun Song
Quoted 9 lines · Mike Kravetz
Quoted 2 lines · Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

In general, I agree that the race window is small. However, worst case would be if the freeing of the page is put on a work queue. Is it acceptable to keep retrying in that case? In addition, the 'Free some vmemmap' series may slow the free_huge_page path even more.

Quoted 4 lines

I also consider the 'Free some vmemmap' series case. In my next version series, I will flush the work before dissolve_free_huge_page returns when encountering this race. So the retry is acceptable. Right?

Hi Mike,

How about flushing the @free_hpage_work when
encountering this race?

Quoted 59 lines

Thanks.

Quoted 56 lines

In these worst case scenarios, I am not sure we want to just spin retrying.

--
Mike Kravetz

Quoted 50 lines
---
 mm/hugetlb.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
mm/hugetlb.c +11 -5
index 72608008f8b4..db00ae375d2a 100644
@@ -1763,10 +1763,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  * nothing for in-use hugepages and non-hugepages.
  * This function returns values like below:
  *
- *  -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
- *          (allocated or reserved.)
- *       0: successfully dissolved free hugepages or the page is not a
- *          hugepage (considered as already dissolved)
+ *  -EAGAIN: race with __free_huge_page() and can do a retry
+ *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
+ *           (allocated or reserved.)
+ *       0:  successfully dissolved free hugepages or the page is not a
+ *           hugepage (considered as already dissolved)
  */
 int dissolve_free_huge_page(struct page *page)
 {
@@ -1815,8 +1816,10 @@ int dissolve_free_huge_page(struct page *page)
               * We should make sure that the page is already on the free list
               * when it is dissolved.
               */
-             if (unlikely(!PageHugeFreed(head)))
+             if (unlikely(!PageHugeFreed(head))) {
+                     rc = -EAGAIN;
                      goto out;
+             }
 
              /*
               * Move PageHWPoison flag from head page to the raw error page,
@@ -1857,7 +1860,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
 
      for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
              page = pfn_to_page(pfn);
+retry:
              rc = dissolve_free_huge_page(page);
+             if (rc == -EAGAIN)
+                     goto retry;
              if (rc)
                      break;
      }
#16 lore raw

Re: [External] Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

Quoted 18 lines · Muchun Song
Quoted 12 lines · Muchun Song
Quoted 9 lines · Mike Kravetz
Quoted 2 lines · Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

In general, I agree that the race window is small. However, worst case would be if the freeing of the page is put on a work queue. Is it acceptable to keep retrying in that case? In addition, the 'Free some vmemmap' series may slow the free_huge_page path even more.

Quoted 4 lines

I also consider the 'Free some vmemmap' series case. In my next version series, I will flush the work before dissolve_free_huge_page returns when encountering this race. So the retry is acceptable. Right?

Quoted 4 lines

Hi Mike,

How about flushing the @free_hpage_work when
encountering this race?

Flushing should be fine.

The more I think about it, the more I think spinning to retry is not going to be an issue. Why? As you mentioned the race window is quite small. It just makes me a bit nervous to retry in a tight loop.

Signature · 1 line
Mike Kravetz
#17 lore raw

Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

In reply to Muchun Song
Quoted 29 lines · Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

---
 mm/hugetlb.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
mm/hugetlb.c +5 -4
index 72608008f8b4..db00ae375d2a 100644
@@ -1763,10 +1763,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  * nothing for in-use hugepages and non-hugepages.
  * This function returns values like below:
  *
- *  -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
- *          (allocated or reserved.)
- *       0: successfully dissolved free hugepages or the page is not a
- *          hugepage (considered as already dissolved)
+ *  -EAGAIN: race with __free_huge_page() and can do a retry
+ *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
+ *           (allocated or reserved.)
+ *       0:  successfully dissolved free hugepages or the page is not a
+ *           hugepage (considered as already dissolved)
  */
 int dissolve_free_huge_page(struct page *page)
 {

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

Quoted 8 lines
mm/hugetlb.c +3 -1
@@ -1815,8 +1816,10 @@ int dissolve_free_huge_page(struct page *page)
 		 * We should make sure that the page is already on the free list
 		 * when it is dissolved.
 		 */
-		if (unlikely(!PageHugeFreed(head)))
+		if (unlikely(!PageHugeFreed(head))) {
+			rc = -EAGAIN;
 			goto out;
+		}

If dissolve_free_huge_page() races with __free_huge_page() and we detect it with this check, the hugepage is expected to be freed or dissolved by __free_huge_page(), so is it enough just to return with rc = 0 without retrying?

Thanks,
Naoya Horiguchi

Quoted 17 lines
mm/hugetlb.c +3 -0
 		/*
 		 * Move PageHWPoison flag from head page to the raw error page,
@@ -1857,7 +1860,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
 
 	for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
 		page = pfn_to_page(pfn);
+retry:
 		rc = dissolve_free_huge_page(page);
+		if (rc == -EAGAIN)
+			goto retry;
 		if (rc)
 			break;
 	}
Signature · 1 line
2.11.0
#18 lore raw

Re: [External] Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

Quoted 35 lines · HORIGUCHI NAOYA(堀口 直也)
Quoted 33 lines · Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

---
 mm/hugetlb.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
mm/hugetlb.c +5 -5
index 72608008f8b4..db00ae375d2a 100644
@@ -1763,10 +1763,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  * nothing for in-use hugepages and non-hugepages.
  * This function returns values like below:
  *
- *  -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
- *          (allocated or reserved.)
- *       0: successfully dissolved free hugepages or the page is not a
- *          hugepage (considered as already dissolved)
+ *  -EAGAIN: race with __free_huge_page() and can do a retry
+ *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
+ *           (allocated or reserved.)
+ *       0:  successfully dissolved free hugepages or the page is not a
+ *           hugepage (considered as already dissolved)
  */
 int dissolve_free_huge_page(struct page *page)
 {
@@ -1815,8 +1816,10 @@ int dissolve_free_huge_page(struct page *page)
               * We should make sure that the page is already on the free list
               * when it is dissolved.
               */
-             if (unlikely(!PageHugeFreed(head)))
Quoted 8 lines

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

Quoted 4 lines
mm/hugetlb.c +3 -0
+             if (unlikely(!PageHugeFreed(head))) {
+                     rc = -EAGAIN;
                      goto out;
+             }

If dissolve_free_huge_page() races with __free_huge_page() and we detect it with this check, the hugepage is expected to be freed or dissolved by __free_huge_page(), so is it enough just to return with rc = 0 without retrying?

The dissolve_free_huge_page() aims to free the page to the buddy allocator not the hugepage pool. So it is not enough just to return 0, right? Or did you mean that we set the page temporary here and let the __free_huge_page do the freeing later for us? Thanks.

Quoted 21 lines

Thanks,
Naoya Horiguchi

Quoted 17 lines
mm/hugetlb.c +3 -1
              /*
               * Move PageHWPoison flag from head page to the raw error page,
@@ -1857,7 +1860,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
 
      for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << minimum_order) {
              page = pfn_to_page(pfn);
+retry:
              rc = dissolve_free_huge_page(page);
+             if (rc == -EAGAIN)
+                     goto retry;
              if (rc)
                      break;
      }
--

2.11.0

#19 lore raw

Re: [External] Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page

Quoted 46 lines · Muchun Song
Quoted 35 lines · HORIGUCHI NAOYA(堀口 直也)
Quoted 33 lines · Muchun Song

When dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.

---
 mm/hugetlb.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
mm/hugetlb.c +5 -5
index 72608008f8b4..db00ae375d2a 100644
@@ -1763,10 +1763,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  * nothing for in-use hugepages and non-hugepages.
  * This function returns values like below:
  *
- *  -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
- *          (allocated or reserved.)
- *       0: successfully dissolved free hugepages or the page is not a
- *          hugepage (considered as already dissolved)
+ *  -EAGAIN: race with __free_huge_page() and can do a retry
+ *  -EBUSY:  failed to dissolved free hugepages or the hugepage is in-use
+ *           (allocated or reserved.)
+ *       0:  successfully dissolved free hugepages or the page is not a
+ *           hugepage (considered as already dissolved)
  */
 int dissolve_free_huge_page(struct page *page)
 {
@@ -1815,8 +1816,10 @@ int dissolve_free_huge_page(struct page *page)
               * We should make sure that the page is already on the free list
               * when it is dissolved.
               */
-             if (unlikely(!PageHugeFreed(head)))
Quoted 8 lines

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

Quoted 4 lines
mm/hugetlb.c +3 -0
+             if (unlikely(!PageHugeFreed(head))) {
+                     rc = -EAGAIN;
                      goto out;
+             }

If dissolve_free_huge_page() races with __free_huge_page() and we detect it with this check, the hugepage is expected to be freed or dissolved by __free_huge_page(), so is it enough just to return with rc = 0 without retrying?

Quoted 4 lines

The dissolve_free_huge_page() aims to free the page to the buddy allocator not the hugepage pool. So it is not enough just to return 0, right? Or did you mean that we set the page temporary here and let the __free_huge_page do the freeing later for us? Thanks.

Ah, OK. You're right.
Thank you for the answer (and finding/fixing a few bugs).

- Naoya

#20 lore raw

PATCH 5/6 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 db00ae375d2a..5c2f64f53177 100644
@@ -5587,9 +5587,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
Reviewed-by on v1 5/6
#21 lore raw

Re: [PATCH 5/6] mm: hugetlb: fix a race between isolating and freeing page

Quoted 25 lines · 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.

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

Thanks!

Signature · 1 line
Mike Kravetz
#22 lore raw

PATCH 6/6 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 | 1 -
 1 file changed, 1 deletion(-)
mm/hugetlb.c +0 -1
index 5c2f64f53177..4c8631114e88 100644
@@ -1360,7 +1360,6 @@ 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]);
 }
 
Signature · 1 line
2.11.0
Reviewed-by on v1 6/6
#23 lore raw

Re: [PATCH 6/6] mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active

Quoted 7 lines · 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.

Quoted 5 lines
Fixes 7e1f049efb86 ("mm: hugetlb: cleanup using paeg_huge_active()")
---
 mm/hugetlb.c | 1 -
 1 file changed, 1 deletion(-)

Thanks!

Signature · 1 line
Mike Kravetz
#25 lore raw
In reply to Muchun Song
Quoted 16 lines · Muchun Song

If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.

---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +0 -0
index 4385f2fb5d18..a6631c4eb6a6 100644
@@ -1279,6 +1279,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
 		return -ENOSYS;
 	}

mm/migrate.c static int unmap_and_move_huge_page(new_page_t get_new_page, · line 1290

Quoted 10 lines
mm/migrate.c +6 -0
 
+	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;

This series seems to fix quite some important cases (thanks). Do we want to cc stable some/all?

Signature · 3 lines
Thanks,

David / dhildenb
#26 lore raw
Quoted 18 lines · David Hildenbrand
Quoted 16 lines · Muchun Song

If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.

---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +0 -0
index 4385f2fb5d18..a6631c4eb6a6 100644
@@ -1279,6 +1279,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
              return -ENOSYS;
      }
 
Quoted 13 lines

mm/migrate.c static int unmap_and_move_huge_page(new_page_t get_new_page, · line 1290

Quoted 10 lines
mm/migrate.c +6 -0
+     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;

This series seems to fix quite some important cases (thanks). Do we want to cc stable some/all?

For this particular one, I don't think so IMHO. It is an optimization rather than a bug fix.

Quoted 7 lines

--
Thanks,

David / dhildenb

#27 lore raw
Quoted 33 lines · Yang Shi
Quoted 18 lines · David Hildenbrand
Quoted 16 lines · Muchun Song

If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.

---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +0 -0
index 4385f2fb5d18..a6631c4eb6a6 100644
@@ -1279,6 +1279,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
              return -ENOSYS;
      }
 
Quoted 13 lines

mm/migrate.c static int unmap_and_move_huge_page(new_page_t get_new_page, · line 1290

Quoted 10 lines
mm/migrate.c +6 -0
+     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;

This series seems to fix quite some important cases (thanks). Do we want to cc stable some/all?

Quoted 2 lines

For this particular one, I don't think so IMHO. It is an optimization rather than a bug fix.

I'm rather referring to the actual fixes (I received no cover letter so I picked the first patch in the series)

Signature · 3 lines
Thanks,

David / dhildenb
Acked-by on v1 1/6
#28 lore raw
In reply to Muchun Song
Quoted 14 lines · Muchun Song

If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.

---
 mm/migrate.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/migrate.c +0 -0
index 4385f2fb5d18..a6631c4eb6a6 100644

mm/migrate.c static int unmap_and_move_huge_page(new_page_t get_new_page, · line 1287

Quoted 9 lines
mm/migrate.c +6 -0
@@ -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;
+       }
+
Quoted 7 lines
mm/migrate.c +0 -1
        new_hpage = get_new_page(hpage, private);
        if (!new_hpage)
                return -ENOMEM;
--

2.11.0

#29 lore raw
In reply to Muchun Song
Quoted 2 lines · Muchun Song

If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.

Is this common enough that it would warrant the explicit check for each migration?

Quoted 24 lines
---
 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
Signature · 2 lines
Michal Hocko
SUSE Labs
#30 lore raw
Quoted 6 lines · Michal Hocko
Quoted 2 lines · Muchun Song

If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.

Is this common enough that it would warrant the explicit check for each migration?

Ups, just noticed that you have posted a newer version. I will follow up there.

Signature · 2 lines
Michal Hocko
SUSE Labs