mm: migrate: do not migrate HugeTLB page whose refcount is one
Subsystems (3)
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
@@ -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
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.
--- fs/hugetlbfs/inode.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +3 -2
@@ -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
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()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())Signed-off-by Muchun Song <[email protected]>--- 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@@ -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)
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()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())Signed-off-by Muchun Song <[email protected]>--- 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@@ -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 callingset_page_huge_active()before theput_page()?When the page was allocated, it was placed on the active list (
alloc_huge_page). Therefore, thehugetlb_locklocking 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 KravetzQuoted 4 lines
fs/hugetlbfs/inode.c +0
-0}if (!(mode &FALLOC_FL_KEEP_SIZE ) && offset + len > inode->i_size)
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()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())Signed-off-by Muchun Song <[email protected]>--- 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@@ -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 callingset_page_huge_active()before theput_page()?
Quoted 7 lines
Quoted 2 lines
When the page was allocated, it was placed on the active list (
alloc_huge_page). Therefore, thehugetlb_locklocking and list movement should not be necessary.I agree with you. Because
set_page_huge_activeis not exported (static function). Only exportingset_page_huge_activeseems strange (leavingclear_page_huge_activenot 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
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()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())Signed-off-by Muchun Song <[email protected]>--- 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@@ -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 callingset_page_huge_active()before theput_page()?Quoted 7 lines
Quoted 2 lines
When the page was allocated, it was placed on the active list (
alloc_huge_page). Therefore, thehugetlb_locklocking and list movement should not be necessary.I agree with you. Because
set_page_huge_activeis not exported (static function). Only exportingset_page_huge_activeseems strange (leavingclear_page_huge_activenot 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 leaveclear_page_huge_activeas 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
PATCH 3/6 mm: hugetlb: fix a race between freeing and dissolving the page
In reply to Muchun SongThere is a race condition between __free_huge_page() and dissolve_free_huge_page().
CPU0: CPU1:
// page_count(page) == 1put_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.
--- mm/hugetlb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
mm/hugetlb.c +48 -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 ; +} + +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
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()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_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()andspin_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")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)mm/hugetlb.c +0
-0
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.
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()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_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()andspin_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")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)mm/hugetlb.c +0
-0mm/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 ifdissolve_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 KravetzQuoted 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.
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()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_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()andspin_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")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)mm/hugetlb.c +0
-0mm/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 ifdissolve_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_pageto 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 beforeput_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
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()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_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()andspin_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")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)mm/hugetlb.c +0
-0mm/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 ifdissolve_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_pageto 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 beforeput_page. But we cannot guarantee this.
Quoted 8 lines
Just FYI,
I went back and explored the option of doingset_page_huge_activewhen a page was put on the active list andclear_page_huge_activewhen put on the free list. This would be much like what you are doing with PageHugeFreed. Commitbcc54222309cwhich addedpage_huge_activeimplied that this was possible. Then I remembered a race fixed incb6acd01e2e4that required delaying the call toset_page_huge_activeinhugetlb_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 definedHUGETLB_CGROUP_MIN_ORDER.
It is fine to me. Will do. Thanks.
Quoted 2 lines
--
Mike Kravetz
Re: [External] Re: [PATCH 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
In reply to Mike KravetzQuoted 42 lines · Mike Kravetz
Quoted 40 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_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()andspin_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")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)mm/hugetlb.c +0
-0@@ -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 ifdissolve_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 KravetzQuoted 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.
PATCH 4/6 mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
In reply to Muchun SongWhen 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
@@ -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
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
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)mm/hugetlb.c +11
-5@@ -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 ; }
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_pagepath 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 KravetzQuoted 50 lines
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)mm/hugetlb.c +11
-5@@ -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 ; }
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_pagepath 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_pagereturns 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 KravetzQuoted 50 lines
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)mm/hugetlb.c +11
-5@@ -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 ; }
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_pagepath 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_pagereturns 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
Re: [PATCH 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
In reply to Muchun SongQuoted 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.Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)mm/hugetlb.c +5
-4@@ -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
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.Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)mm/hugetlb.c +5
-5@@ -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 HoriguchiQuoted 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
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.Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)mm/hugetlb.c +5
-5@@ -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_pagedo the freeing later for us? Thanks.
Ah, OK. You're right.
Thank you for the answer (and finding/fixing a few bugs).
- Naoya
PATCH 5/6 mm: hugetlb: fix a race between isolating and freeing page
In reply to Muchun SongThere 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.
--- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/hugetlb.c +2 -2
@@ -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
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_ONon CPU0. Because it is already freed to the buddy allocator.
Quoted 5 lines
Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Thanks!
Signature · 1 line
Mike Kravetz
PATCH 6/6 mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active
In reply to Muchun SongThe 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.
--- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-)
mm/hugetlb.c +0 -1
@@ -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
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 fromscan_movable_pages()which do not hold a reference count to the HugeTLB page. So when we callpage_huge_active()fromscan_movable_pages(), the HugeTLB page can be freed parallel. Then we will trigger aBUG_ONwhich is in thepage_huge_active()whenCONFIG_DEBUG_VMis enabled. Just remove theVM_BUG_ON_PAGE.
Quoted 5 lines
Fixes 7e1f049efb86 ("mm: hugetlb: cleanup using paeg_huge_active()")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-)
Thanks!
Signature · 1 line
Mike Kravetz
Quoted 7 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.
Signed-off-by Muchun Song <[email protected]>--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)
Thanks!
Signature · 1 line
Mike Kravetz
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.
Signed-off-by Muchun Song <[email protected]>--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)mm/migrate.c +0
-0@@ -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
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.
Signed-off-by Muchun Song <[email protected]>--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)mm/migrate.c +0
-0@@ -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
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.
Signed-off-by Muchun Song <[email protected]>--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)mm/migrate.c +0
-0@@ -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
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.
Signed-off-by Muchun Song <[email protected]>--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)mm/migrate.c +0
-0
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
-1new_hpage =get_new_page (hpage, private);if (!new_hpage)return -ENOMEM ; --2.11.0
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
Signed-off-by Muchun Song <[email protected]>--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)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 ; + } + 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
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