Fix some bugs about HugeTLB code
Subsystems (4)
v6 cover Fix some bugs about HugeTLB code
This patch series aims to fix some bugs and add some improvements.
Changelog since v5 -> v6:
- Simplify patch #3 and update commit changelog.
Changelog since v4 -> v5:
- Squash "mm: hugetlb: retry dissolve page when hitting race" to "mm: hugetlb: fix a race between freeing and dissolving the page"
Changelog since v3 -> v4:
- Update commit log of patch #1.
- Drop "mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page"
- Add "mm: hugetlb: retry dissolve page when hitting race"
Changelog since v2 -> v3:
- Update commit log.
- Using head[3].private to indicate the page is freed in patch #3.
Changelog since v1 -> v2:
- Export set_page_huge_active() in patch #2 to fix.
- Using head[3].mapping to indicate the page is freed in patch #3.
- Flush @free_hpage_work in patch #4.
Muchun Song (5):
mm: migrate: do not migrate HugeTLB page whose refcount is one
mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
mm: hugetlb: fix a race between freeing and dissolving the page
mm: hugetlb: fix a race between isolating and freeing page
mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active
fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 48 +++++++++++++++++++++++++++++++++++++++++++----- mm/migrate.c | 6 ++++++ 4 files changed, 53 insertions(+), 6 deletions(-)
Signature · 1 line
2.11.0
PATCH v6 1/5 mm: migrate: do not migrate HugeTLB page whose refcount is one
All pages isolated for the migration have an elevated reference count and therefore seeing a reference count equal to 1 means that the last user of the page has dropped the reference and the page has became unused and there doesn't make much sense to migrate it anymore. This has been done for regular pages and this patch does the same for hugetlb pages. Although the likelyhood of the race is rather small for hugetlb pages it makes sense the two code paths in sync.
--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/migrate.c +6 -0
@@ -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 v6 2/5 mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
In reply to Muchun SongIf a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a later isolate_huge_page failure when the page migration code would like to move that page. Such a failure would be unexpected and wrong.
Only export set_page_huge_active, just leave clear_page_huge_active as static. Because there are no external users.
--- fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +2 -1
@@ -735,9 +735,10 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,mutex_unlock (&hugetlb_fault_mutex_table[hash]); +set_page_huge_active (page);/* * unlock_page because locked by add_to_page_cache() -* page_put due to reference from alloc_huge_page() +* put_page() due to reference from alloc_huge_page() */ unlock_page (page);put_page (page);
include/linux/hugetlb.h +2 -0
@@ -770,6 +770,8 @@ static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, }#endif +void set_page_huge_active (struct page *page); +#else /* CONFIG_HUGETLB_PAGE */ struct hstate {};
mm/hugetlb.c +1 -1
@@ -1348,7 +1348,7 @@ bool page_huge_active(struct page *page) }/* never called for tail page */ -static void set_page_huge_active (struct page *page) +void set_page_huge_active (struct page *page) {VM_BUG_ON_PAGE (!PageHeadHuge (page), page);SetPagePrivate (&page[1 ]);
Signature · 1 line
2.11.0
PATCH v6 3/5 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 dissolve_free_huge_page().
We should make sure that the page is already on the free list when it is dissolved.
As a result __free_huge_page would corrupt page(s) already in the buddy allocator.
--- mm/hugetlb.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
mm/hugetlb.c +39 -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 +4 ) ==-1UL ; +} + +static inline void SetPageHugeFreed (struct page *head) +{ +set_page_private (head +4 ,-1UL ); +} + +static inline void ClearPageHugeFreed (struct page *head) +{ +set_page_private (head +4 ,0 ); +} +/* Forward declaration */ static int hugetlb_acct_memory (struct hstate *h,long delta);
@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; +SetPageHugeFreed (page); }static struct page *dequeue_huge_page_node_exact (struct hstate *h,int nid)
@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)list_move (&page->lru, &h->hugepage_activelist);set_page_refcounted (page); +ClearPageHugeFreed (page); h->free_huge_pages--; h->free_huge_pages_node[nid]--;return page;
@@ -1504,6 +1521,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)spin_lock (&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; +ClearPageHugeFreed (page);spin_unlock (&hugetlb_lock); }
@@ -1754,6 +1772,7 @@ int dissolve_free_huge_page(struct page *page) {int rc = -EBUSY ; +retry:/* Not to disrupt normal path by vainly holding hugetlb_lock */ if (!PageHuge (page))return 0 ;
@@ -1770,6 +1789,26 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; + +/* +* We should make sure that the page is already on the free list +* when it is dissolved. +*/ +if (unlikely (!PageHugeFreed (head))) { +spin_unlock (&hugetlb_lock); +cond_resched (); + +/* +* Theoretically, we should return -EBUSY when we +* encounter this race. In fact, we have a chance +* to successfully dissolve the page if we do a +* retry. Because the race window is quite small. +* If we seize this opportunity, it is an optimization +* for increasing the success rate of dissolving page. +*/ + goto retry; + } +/* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable.
Signature · 1 line
2.11.0
Re: [PATCH v6 3/5] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 25 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()anddissolve_free_huge_page().We should make sure that the page is already on the free list when it is dissolved.
Quoted 7 lines
As a result
__free_huge_pagewould corrupt page(s) already in the buddy allocator.Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>Reviewed-by Mike Kravetz <[email protected]>
LGTM,
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [PATCH v6 3/5] mm: hugetlb: fix a race between freeing and dissolving the page
In reply to Muchun SongQuoted 25 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()anddissolve_free_huge_page().We should make sure that the page is already on the free list when it is dissolved.
Quoted 7 lines
As a result
__free_huge_pagewould corrupt page(s) already in the buddy allocator.Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>Reviewed-by Mike Kravetz <[email protected]>
Thanks!
Quoted 91 lines
--- mm/hugetlb.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)mm/hugetlb.c +39
-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 +4 ) ==-1UL ; +} + +static inline void SetPageHugeFreed (struct page *head) +{ +set_page_private (head +4 ,-1UL ); +} + +static inline void ClearPageHugeFreed (struct page *head) +{ +set_page_private (head +4 ,0 ); +} +/* Forward declaration */ static int hugetlb_acct_memory (struct hstate *h,long delta);@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; +SetPageHugeFreed (page); }static struct page *dequeue_huge_page_node_exact (struct hstate *h,int nid)@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)list_move (&page->lru, &h->hugepage_activelist);set_page_refcounted (page); +ClearPageHugeFreed (page); h->free_huge_pages--; h->free_huge_pages_node[nid]--;return page;@@ -1504,6 +1521,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)spin_lock (&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; +ClearPageHugeFreed (page);spin_unlock (&hugetlb_lock); }@@ -1754,6 +1772,7 @@ int dissolve_free_huge_page(struct page *page) {int rc = -EBUSY ; +retry:/* Not to disrupt normal path by vainly holding hugetlb_lock */ if (!PageHuge (page))return 0 ;@@ -1770,6 +1789,26 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; + +/* +* We should make sure that the page is already on the free list +* when it is dissolved. +*/ +if (unlikely (!PageHugeFreed (head))) { +spin_unlock (&hugetlb_lock); +cond_resched (); + +/* +* Theoretically, we should return -EBUSY when we +* encounter this race. In fact, we have a chance +* to successfully dissolve the page if we do a +* retry. Because the race window is quite small. +* If we seize this opportunity, it is an optimization +* for increasing the success rate of dissolving page. +*/ + goto retry; + } +/* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable. Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v6 4/5 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
@@ -5572,9 +5572,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 v6 4/5] 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]>Reviewed-by Mike Kravetz <[email protected]>Acked-by Michal Hocko <[email protected]>
Signature · 2 lines
Oscar Salvador SUSE L3
PATCH v6 5/5 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 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
mm/hugetlb.c +1 -2
@@ -1360,8 +1360,7 @@ struct hstate *size_to_hstate(unsigned long size)*/ bool page_huge_active (struct page *page) { -VM_BUG_ON_PAGE (!PageHuge (page), page); -return PageHead (page) &&PagePrivate (&page[1 ]); +return PageHeadHuge (page) &&PagePrivate (&page[1 ]); }/* never called for tail page */
Signature · 1 line
2.11.0
Re: [PATCH v6 5/5] 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]>Reviewed-by Mike Kravetz <[email protected]>Acked-by Michal Hocko <[email protected]>
Signature · 2 lines
Oscar Salvador SUSE L3