hugetlb: fix potential ref counting races
Subsystems (2)
v2 cover hugetlb: fix potential ref counting races
When Muchun Song brought up a potential issue with hugetlb ref counting[1], I started looking closer at the code. hugetlbfs is the only code with it's own specialized compound page destructor and taking special action when ref counts drop to zero. Potential races happen in this unique handling of ref counts. The following patches address these races when creating and destroying hugetlb pages.
These potential races have likely existed since the creation of hugetlbfs. They certainly have been around for more than 10 years. However, I am unaware of anyone actually hitting these races. It is VERY unlikely than anyone will actually hit these races, but they do exist.
I could not think of an easy (or difficult) way to force these races. Therefore, testing consisted of adding code to randomly increase ref counts in strategic places. In this way, I was able to exercise all the race handling code paths.
[1] https://lore.kernel.org/linux-mm/CAMZfGtVMn3daKrJwZMaVOGOaJU+B4dS--x_oPmGQMD=c=QNGEg@mail.gmail.com/
v1 -> v2
- Update comment in prep_compound_gigantic_page as suggested by Muchun
- Add Muchun Reviewed-by: on patch 3. Still an open question on whether the free_contig_range should be removed.
Mike Kravetz (3):
hugetlb: simplify prep_compound_gigantic_page ref count racing code
hugetlb: drop ref count earlier after page allocation
hugetlb: before freeing hugetlb page set dtor to appropriate value
mm/hugetlb.c | 139 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 106 insertions(+), 33 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/3 hugetlb: simplify prep_compound_gigantic_page ref count racing code
Code in prep_compound_gigantic_page waits for a rcu grace period if it notices a temporarily inflated ref count on a tail page. This was due to the identified potential race with speculative page cache references which could only last for a rcu grace period. This is overly complicated as this situation is VERY unlikely to ever happen. Instead, just quickly return an error.
Also, only print a warning in prep_compound_gigantic_page instead of multiple callers.
--- mm/hugetlb.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
mm/hugetlb.c +7 -10
@@ -1657,16 +1657,14 @@ static bool prep_compound_gigantic_page(struct page *page, unsigned int order)* cache adding could take a ref on a 'to be' tail page. * We need to respect any increased ref count, and only set * the ref count to zero if count is currently 1. If count -* is not 1, we call synchronize_rcu in the hope that a rcu -* grace period will cause ref count to drop and then retry. -* If count is still inflated on retry we return an error and -* must discard the pages. +* is not 1, we return an error. An error return indicates +* the set of pages can not be converted to a gigantic page. +* The caller who allocated the pages should then discard the +* pages using the appropriate free interface. */ if (!page_ref_freeze (p,1 )) { -pr_info ("HugeTLB unexpected inflated ref count on freshly allocated page\n" ); -synchronize_rcu (); -if (!page_ref_freeze (p,1 )) - goto out_error; +pr_warn ("HugeTLB page can not be used due to unexpected inflated ref count\n" ); + goto out_error; }set_page_count (p,0 );set_compound_head (p, page);
@@ -1830,7 +1828,6 @@ static struct page *alloc_fresh_huge_page(struct hstate *h, retry = true; goto retry; } -pr_warn ("HugeTLB page can not be used due to unexpected inflated ref count\n" );return NULL ; } }
@@ -2828,8 +2825,8 @@ static void __init gather_bootmem_prealloc(void)prep_new_huge_page (h, page,page_to_nid (page));put_page (page);/* add to the hugepage allocator */ }else { +/* VERY unlikely inflated ref count on a tail page */ free_gigantic_page (page,huge_page_order (h)); -pr_warn ("HugeTLB page can not be used due to unexpected inflated ref count\n" ); }/*
Signature · 1 line
2.31.1
Oscar Salvador
maintainerRe: [PATCH v2 1/3] hugetlb: simplify prep_compound_gigantic_page ref count racing code
Quoted 8 lines · Mike Kravetz
Code in
prep_compound_gigantic_pagewaits for a rcu grace period if it notices a temporarily inflated ref count on a tail page. This was due to the identified potential race with speculative page cache references which could only last for a rcu grace period. This is overly complicated as this situation is VERY unlikely to ever happen. Instead, just quickly return an error.
Also, only print a warning inprep_compound_gigantic_pageinstead of multiple callers.
The above makes sense to me.
My only question would be: gather_bootmem_prealloc() is an __init function. Can we have speculative refcounts due to e.g: pagecache at that early stage? I think we cannot, but I am not really sure.
We might be able to remove that else() in case we cannot have such scenarios.
Signature · 2 lines
Oscar Salvador SUSE L3
Re: [PATCH v2 1/3] hugetlb: simplify prep_compound_gigantic_page ref count racing code
Quoted 11 lines · Oscar Salvador
Quoted 8 lines · Mike Kravetz
Code in
prep_compound_gigantic_pagewaits for a rcu grace period if it notices a temporarily inflated ref count on a tail page. This was due to the identified potential race with speculative page cache references which could only last for a rcu grace period. This is overly complicated as this situation is VERY unlikely to ever happen. Instead, just quickly return an error.
Also, only print a warning inprep_compound_gigantic_pageinstead of multiple callers.The above makes sense to me.
Thanks for taking a look!
Quoted 3 lines
My only question would be:
gather_bootmem_prealloc()is an__initfunction. Can we have speculative refcounts due to e.g: pagecache at that early stage? I think we cannot, but I am not really sure.
I had the same thought when adding that code. We cannot have a speculative refcount this early after boot. However, further thinking below...
Quoted 3 lines
We might be able to remove that
else()in case we cannot have such scenarios.
Instead of removing the else, I think we should put a BUG_ON() just to make sure the condition never happens in the future. Otherwise, we would just abandon the gigantic page and leave memory (1GB or so) unavailable.
Even if it were possible to have speculative references this early in the boot process, the likelihood of it happening here is still VERY small. So, I would not expect a BUG_ON() to ever be hit in development or testing. Rather, with our luck it would show up in some production environment.
Since handling the race in this routine is so simple, I chose to just add the code to handle it.
Signature · 1 line
Mike Kravetz
PATCH v2 2/3 hugetlb: drop ref count earlier after page allocation
In reply to Mike KravetzWhen discussing the possibility of inflated page ref counts, Muuchun Song pointed out this potential issue [1]. It is true that any code could potentially take a reference on a compound page after allocation and before it is converted to and put into use as a hugetlb page. Specifically, this could be done by any users of get_page_unless_zero.
There are three areas of concern within hugetlb code.
1) When adding pages to the pool.
In this case, new pages are allocated added to the pool by calling put_page to invoke the hugetlb destructor (free_huge_page). If there is an inflated ref count on the page, it will not be immediately added to the free list. It will only be added to the free list when the temporary ref count is dropped. This is deemed acceptable and will not be addressed.
2) A page is allocated for immediate use normally as a surplus page or migration target. In this case, the user of the page will also hold a reference. There is no issue as this is just like normal page ref counting.
3) A page is allocated and MUST be added to the free list to satisfy a reservation. One such example is gather_surplus_pages as pointed out by Muchun in [1]. More specifically, this case covers callers of enqueue_huge_page where the page reference count must be zero. This patch covers this third case.
Three routines call enqueue_huge_page when the page reference count could potentially be inflated. They are: gather_surplus_pages, alloc_and_dissolve_huge_page and add_hugetlb_page.
add_hugetlb_page is called on error paths when a huge page can not be freed due to the inability to allocate vmemmap pages. In this case, the temporairly inflated ref count is not an issue. When the ref is dropped the appropriate action will be taken. Instead of VM_BUG_ON if the ref count does not drop to zero, simply return.
In gather_surplus_pages and alloc_and_dissolve_huge_page the caller expects a page (or pages) to be put on the free lists. In this case we must ensure there are no temporary ref counts. We do this by calling put_page_testzero() earlier and not using pages without a zero ref count. The temporary page flag (HPageTemporary) is used in such cases so that as soon as the inflated ref count is dropped the page will be freed.
[1] https://lore.kernel.org/linux-mm/CAMZfGtVMn3daKrJwZMaVOGOaJU+B4dS--x_oPmGQMD=c=QNGEg@mail.gmail.com/
--- mm/hugetlb.c | 100 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 22 deletions(-)
mm/hugetlb.c +78 -22
@@ -1072,6 +1072,8 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)int nid =page_to_nid (page);lockdep_assert_held (&hugetlb_lock); +VM_BUG_ON_PAGE (page_count (page), page); +list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++;
@@ -1399,11 +1401,20 @@ static void add_hugetlb_page(struct hstate *h, struct page *page,SetHPageVmemmapOptimized (page);/* -* This page is now managed by the hugetlb allocator and has -* no users -- drop the last reference. +* This page is about to be managed by the hugetlb allocator and +* should have no users. Drop our reference, and check for others +* just in case. */ zeroed =put_page_testzero (page); -VM_BUG_ON_PAGE (!zeroed, page); +if (!zeroed) +/* +* It is VERY unlikely soneone else has taken a ref on +* the page. In this case, we simply return as the +* hugetlb destructor (free_huge_page) will be called +* when this other ref is dropped. +*/ +return ; +arch_clear_hugepage_flags (page);enqueue_huge_page (h, page); }
@@ -2017,9 +2028,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)* Allocates a fresh surplus page from the page allocator. */ static struct page *alloc_surplus_huge_page (struct hstate *h,gfp_t gfp_mask, -int nid,nodemask_t *nmask) +int nid,nodemask_t *nmask,bool zero_ref) {struct page *page =NULL ; +bool retry = false;if (hstate_is_gigantic (h))return NULL ;
@@ -2029,6 +2041,7 @@ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask, goto out_unlock;spin_unlock_irq (&hugetlb_lock); +retry: page =alloc_fresh_huge_page (h, gfp_mask, nid, nmask,NULL );if (!page)return NULL ;
@@ -2046,11 +2059,35 @@ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask,spin_unlock_irq (&hugetlb_lock);put_page (page);return NULL ; - }else { - h->surplus_huge_pages++; - h->surplus_huge_pages_node[page_to_nid (page)]++; } +if (zero_ref) { +/* +* Caller requires a page with zero ref count. +* We will drop ref count here. If someone else is holding +* a ref, the page will be freed when they drop it. Abuse +* temporary page flag to accomplish this. +*/ +SetHPageTemporary (page); +if (!put_page_testzero (page)) { +/* +* Unexpected inflated ref count on freshly allocated +* huge. Retry once. +*/ +pr_info ("HugeTLB unexpected inflated ref count on freshly allocated page\n" ); +spin_unlock_irq (&hugetlb_lock); +if (retry) +return NULL ; + + retry = true; + goto retry; + } +ClearHPageTemporary (page); + } + + h->surplus_huge_pages++; + h->surplus_huge_pages_node[page_to_nid (page)]++; + out_unlock:spin_unlock_irq (&hugetlb_lock);
@@ -2092,7 +2129,7 @@ struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h,nodemask_t *nodemask; nid =huge_node (vma, addr, gfp_mask, &mpol, &nodemask); - page =alloc_surplus_huge_page (h, gfp_mask, nid, nodemask); + page =alloc_surplus_huge_page (h, gfp_mask, nid, nodemask, false);mpol_cond_put (mpol);return page;
@@ -2164,7 +2201,7 @@ static int gather_surplus_pages(struct hstate *h, long delta)spin_unlock_irq (&hugetlb_lock);for (i =0 ; i < needed; i++) { page =alloc_surplus_huge_page (h,htlb_alloc_mask (h), -NUMA_NO_NODE ,NULL ); +NUMA_NO_NODE ,NULL , true);if (!page) { alloc_ok = false;break ;
@@ -2205,24 +2242,20 @@ static int gather_surplus_pages(struct hstate *h, long delta)/* Free the needed pages to the hugetlb pool */ list_for_each_entry_safe (page, tmp, &surplus_list, lru) { -int zeroed; -if ((--needed) <0 )break ; -/* -* This page is now managed by the hugetlb allocator and has -* no users -- drop the buddy allocator's reference. -*/ - zeroed =put_page_testzero (page); -VM_BUG_ON_PAGE (!zeroed, page); +/* Add the page to the hugetlb allocator */ enqueue_huge_page (h, page); } free:spin_unlock_irq (&hugetlb_lock); -/* Free unnecessary surplus pages to the buddy allocator */ +/* +* Free unnecessary surplus pages to the buddy allocator. +* Pages have no ref count, call free_huge_page directly. +*/ list_for_each_entry_safe (page, tmp, &surplus_list, lru) -put_page (page); +free_huge_page (page);spin_lock_irq (&hugetlb_lock);return ret;
@@ -2531,6 +2564,7 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page, {gfp_t gfp_mask =htlb_alloc_mask (h) | __GFP_THISNODE;int nid =page_to_nid (old_page); +bool alloc_retry = false;struct page *new_page;int ret =0 ;
@@ -2541,9 +2575,30 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page,* the pool. This simplifies and let us do most of the processing * under the lock. */ +alloc_retry: new_page =alloc_buddy_huge_page (h, gfp_mask, nid,NULL ,NULL );if (!new_page)return -ENOMEM ; +/* +* If all goes well, this page will be directly added to the free +* list in the pool. For this the ref count needs to be zero. +* Attempt to drop now, and retry once if needed. It is VERY +* unlikely there is another ref on the page. +* +* If someone else has a reference to the page, it will be freed +* when they drop their ref. Abuse temporary page flag to accomplish +* this. Retry once if there is an inflated ref count. +*/ +SetHPageTemporary (new_page); +if (!put_page_testzero (new_page)) { +if (alloc_retry) +return -EBUSY ; + + alloc_retry = true; + goto alloc_retry; + } +ClearHPageTemporary (new_page); +__prep_new_huge_page (h, new_page); retry:
@@ -2583,11 +2638,10 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page,remove_hugetlb_page (h, old_page, false);/* -* Reference count trick is needed because allocator gives us -* referenced page but the pool requires pages with 0 refcount. +* Ref count on new page is already zero as it was dropped +* earlier. It can be directly added to the pool free list. */ __prep_account_new_huge_page (h, nid); -page_ref_dec (new_page);enqueue_huge_page (h, new_page);/*
@@ -2601,6 +2655,8 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page, free_new:spin_unlock_irq (&hugetlb_lock); +/* Page has a zero ref count, but needs a ref to be freed */ +set_page_refcounted (new_page);update_and_free_page (h, new_page, false);return ret;
Signature · 1 line
2.31.1
PATCH v2 3/3 hugetlb: before freeing hugetlb page set dtor to appropriate value
In reply to Mike KravetzWhen removing a hugetlb page from the pool the ref count is set to one (as the free page has no ref count) and compound page destructor is set to NULL_COMPOUND_DTOR. Since a subsequent call to free the hugetlb page will call __free_pages for non-gigantic pages and free_gigantic_page for gigantic pages the destructor is not used.
However, consider the following race with code taking a speculative reference on the page:
Thread 0 Thread 1
-------- --------remove_hugetlb_page
set_page_refcounted(page);
set_compound_page_dtor(page,
NULL_COMPOUND_DTOR);
get_page_unless_zero(page)
__update_and_free_page
__free_pages(page,
huge_page_order(h));
/* Note that __free_pages() will simply drop
the reference to the page. */
put_page(page)
__put_compound_page()
destroy_compound_page
NULL_COMPOUND_DTOR
BUG: kernel NULL pointer
dereference, address:
0000000000000000To address this race, set the dtor to the normal compound page dtor for non-gigantic pages. The dtor for gigantic pages does not matter as gigantic pages are changed from a compound page to 'just a group of pages' before freeing. Hence, the destructor is not used.
--- mm/hugetlb.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
mm/hugetlb.c +21 -1
@@ -1370,8 +1370,28 @@ static void remove_hugetlb_page(struct hstate *h, struct page *page, h->surplus_huge_pages_node[nid]--; } +/* +* Very subtle +* +* For non-gigantic pages set the destructor to the normal compound +* page dtor. This is needed in case someone takes an additional +* temporary ref to the page, and freeing is delayed until they drop +* their reference. +* +* For gigantic pages set the destructor to the null dtor. This +* destructor will never be called. Before freeing the gigantic +* page destroy_compound_gigantic_page will turn the compound page +* into a simple group of pages. After this the destructor does not +* apply. +* +* This handles the case where more than one ref is held when and +* after update_and_free_page is called. +*/ set_page_refcounted (page); -set_compound_page_dtor (page,NULL_COMPOUND_DTOR ); +if (hstate_is_gigantic (h)) +set_compound_page_dtor (page,NULL_COMPOUND_DTOR ); +else +set_compound_page_dtor (page,COMPOUND_PAGE_DTOR ); h->nr_huge_pages--; h->nr_huge_pages_node[nid]--;
Signature · 1 line
2.31.1