mm: Always initialise folio->_deferred_list
Subsystems (5)
PATCH 1/1 mm: Always initialise folio->_deferred_list
For compound pages which are at least order-2 (and hence have a deferred_list), initialise it and then we can check at free that the page is not part of a deferred list. We recently found this useful to rule out a source of corruption.
--- mm/huge_memory.c | 2 -- mm/internal.h | 2 ++ mm/memcontrol.c | 2 ++ mm/page_alloc.c | 9 +++++---- 4 files changed, 9 insertions(+), 6 deletions(-)
mm/huge_memory.c +0 -2
@@ -792,8 +792,6 @@ void folio_prep_large_rmappable(struct folio *folio) {if (!folio || !folio_test_large (folio))return ; -if (folio_order (folio) >1 ) -INIT_LIST_HEAD (&folio->_deferred_list);folio_set_large_rmappable (folio); }
mm/internal.h +2 -0
@@ -525,6 +525,8 @@ static inline void prep_compound_head(struct page *page, unsigned int order)atomic_set (&folio->_entire_mapcount,-1 );atomic_set (&folio->_nr_pages_mapped,0 );atomic_set (&folio->_pincount,0 ); +if (order >1 ) +INIT_LIST_HEAD (&folio->_deferred_list); }static inline void prep_compound_tail (struct page *head,int tail_idx)
mm/memcontrol.c +2 -0
@@ -7483,6 +7483,8 @@ static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug)struct obj_cgroup *objcg;VM_BUG_ON_FOLIO (folio_test_lru (folio), folio); +VM_BUG_ON_FOLIO (folio_order (folio) >1 && + !list_empty (&folio->_deferred_list), folio);/* * Nobody should be changing or seriously looking at
mm/page_alloc.c +5 -4
@@ -1006,10 +1006,11 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page) }break ;case 2 : -/* -* the second tail page: ->mapping is -* deferred_list.next -- ignore value. -*/ +/* the second tail page: deferred_list overlaps ->mapping */ +if (unlikely (!list_empty (&folio->_deferred_list))) { +bad_page (page,"on deferred list" ); + goto out; + }break ;default :if (page->mapping !=TAIL_MAPPING ) {
Signature · 1 line
2.43.0
Quoted 59 lines · Matthew Wilcox (Oracle)
For compound pages which are at least order-2 (and hence have a
deferred_list), initialise it and then we can check at free that the page is not part of a deferred list. We recently found this useful to rule out a source of corruption.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>--- mm/huge_memory.c | 2 -- mm/internal.h | 2 ++ mm/memcontrol.c | 2 ++ mm/page_alloc.c | 9 +++++---- 4 files changed, 9 insertions(+), 6 deletions(-)mm/huge_memory.c +0
-2@@ -792,8 +792,6 @@ void folio_prep_large_rmappable(struct folio *folio) {if (!folio || !folio_test_large (folio))return ; -if (folio_order (folio) >1 ) -INIT_LIST_HEAD (&folio->_deferred_list);folio_set_large_rmappable (folio); }mm/internal.h +2
-0@@ -525,6 +525,8 @@ static inline void prep_compound_head(struct page *page, unsigned int order)atomic_set (&folio->_entire_mapcount,-1 );atomic_set (&folio->_nr_pages_mapped,0 );atomic_set (&folio->_pincount,0 ); +if (order >1 ) +INIT_LIST_HEAD (&folio->_deferred_list); }static inline void prep_compound_tail (struct page *head,int tail_idx)mm/memcontrol.c +2
-0@@ -7483,6 +7483,8 @@ static void uncharge_folio(struct folio *folio, struct uncharge_gather *ug)struct obj_cgroup *objcg;VM_BUG_ON_FOLIO (folio_test_lru (folio), folio); +VM_BUG_ON_FOLIO (folio_order (folio) >1 && + !list_empty (&folio->_deferred_list), folio);/* * Nobody should be changing or seriously looking at mm/page_alloc.c +0
-0@@ -1006,10 +1006,11 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page) }break ;
mm/page_alloc.c static int free_tail_page_prepare(struct page *head_page, struct page *page) · line 1013
Quoted 10 lines
mm/page_alloc.c +5
-4case 2 : -/* -* the second tail page: ->mapping is -* deferred_list.next -- ignore value. -*/ +/* the second tail page: deferred_list overlaps ->mapping */ +if (unlikely (!list_empty (&folio->_deferred_list))) { +bad_page (page,"on deferred list" ); + goto out; + }
IIRC, hugetlb might overwrite this with _hugetlb_subpool? Isn't that a problem we have to handle?
Signature · 3 lines
Cheers, David / dhildenb
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Matthew Wilcox (Oracle)
+++
b/mm/page_alloc.cmm/page_alloc.c +0
-4@@ -1006,10 +1006,11 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page) }break ;case 2 : -/* -* the second tail page: ->mapping is -* deferred_list.next -- ignore value. -*/
Quoted 8 lines
mm/page_alloc.c static int free_tail_page_prepare(struct page *head_page, struct page *page) · line 1013
Quoted 5 lines
mm/page_alloc.c +5
-0+/* the second tail page: deferred_list overlaps ->mapping */ +if (unlikely (!list_empty (&folio->_deferred_list))) { +bad_page (page,"on deferred list" ); + goto out; + }IIRC, hugetlb might overwrite this with
_hugetlb_subpool? Isn't that a problem we have to handle?
Ah yes, you're right. I think this should do the trick?
+++ b/mm/hugetlb.c
@@ -1796,6 +1796,7 @@ static void __update_and_free_hugetlb_folio(struct hstate *h,
destroy_compound_gigantic_folio(folio, huge_page_order(h));
free_gigantic_folio(folio, huge_page_order(h));
} else {
+ INIT_LIST_HEAD(&folio->_deferred_list);
__free_pages(&folio->page, huge_page_order(h));
}}
(that should also be a folio_put(), IMO)
Quoted 24 lines · Matthew Wilcox
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Matthew Wilcox (Oracle)
+++
b/mm/page_alloc.cmm/page_alloc.c +0
-4@@ -1006,10 +1006,11 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page) }break ;case 2 : -/* -* the second tail page: ->mapping is -* deferred_list.next -- ignore value. -*/ Quoted 8 lines
mm/page_alloc.c static int free_tail_page_prepare(struct page *head_page, struct page *page) · line 1013
Quoted 5 lines
mm/page_alloc.c +5
-0+/* the second tail page: deferred_list overlaps ->mapping */ +if (unlikely (!list_empty (&folio->_deferred_list))) { +bad_page (page,"on deferred list" ); + goto out; + }IIRC, hugetlb might overwrite this with
_hugetlb_subpool? Isn't that a problem we have to handle?Ah yes, you're right. I think this should do the trick?
+++
b/mm/hugetlb.c
Review static void __update_and_free_hugetlb_folio(struct hstate *h, · line 1800
Quoted 5 lines
(quoted diff) +1
-0@@ -1796,6 +1796,7 @@ static void __update_and_free_hugetlb_folio(struct hstate *h, destroy_compound_gigantic_folio(folio, huge_page_order(h)); free_gigantic_folio(folio, huge_page_order(h)); } else { + INIT_LIST_HEAD(&folio->_deferred_list); __free_pages(&folio->page, huge_page_order(h));
Heh, __free_pages() says:
"This function can free multi-page allocations that are not compound pages". I suspect that means "can also free compound pages", but it's confusing.
Especially, I thought we recently learned that free hugetlb folios do have a refcount of 0? Confusing.
Anyhow, I suspect your change does the trick and agree that __free_pages() might need a replacement.
... but it's confusing.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 7 lines · David Hildenbrand
Quoted 1 line · Matthew Wilcox
__free_pages(&folio->page, huge_page_order(h));Heh,
__free_pages()says:"This function can free multi-page allocations that are not compound pages". I suspect that means "can also free compound pages", but it's confusing.
Sorry. I wrote that documentation and I was focused on one thing, but failed to think of the other thing ... it can indeed free compound pages. I'll add this:
* If the pages were allocated with __GFP_COMP, prefer using put_page()
* to using this function. Also prefer to call put_page() rather than
* calling __free_page() or __free_pages(page, 0).
Quoted 2 lines
Especially, I thought we recently learned that free hugetlb folios do have a refcount of 0? Confusing.
Yes, that is confusing. This function wouldn't work if the refcount were 0 (put_page_testzero() would complain). Ah, here we go:
In __folio_put(), we know that refcount is 0. __folio_put_large() -> destroy_large_folio -> free_huge_folio (refcount still 0 here, indeed it asserts it)
If we add it back to the pool, the refcount stays at 0. But if we're removing it, we call __remove_hugetlb_folio() which calls folio_ref_unfreeze().
Quoted 9 lines · Matthew Wilcox
Quoted 7 lines · David Hildenbrand
Quoted 1 line · Matthew Wilcox
__free_pages(&folio->page, huge_page_order(h));Heh,
__free_pages()says:"This function can free multi-page allocations that are not compound pages". I suspect that means "can also free compound pages", but it's confusing.
Quoted 7 lines
Sorry. I wrote that documentation and I was focused on one thing, but failed to think of the other thing ... it can indeed free compound pages. I'll add this:
* If the pages were allocated with
__GFP_COMP, prefer usingput_page()
* to using this function. Also prefer to callput_page()rather than
* calling__free_page()or__free_pages(page, 0).
Sounds good.
Quoted 7 lines
Quoted 3 lines
Especially, I thought we recently learned that free hugetlb folios do have a refcount of 0? Confusing.
Yes, that is confusing. This function wouldn't work if the refcount were 0 (
put_page_testzero()would complain). Ah, here we go:
Quoted 6 lines
In
__folio_put(), we know that refcount is 0.__folio_put_large()->destroy_large_folio->free_huge_folio(refcount still 0 here, indeed it asserts it)
If we add it back to the pool, the refcount stays at 0. But if we're removing it, we call__remove_hugetlb_folio()which callsfolio_ref_unfreeze().
Ah, that's the case I was missing. The joy of hugetlb :)
As a side note: hugetlb.c still talks about "destructor" and "dtor". I assume that dates back to the times where we identified hugetlb folios by a deconstrucor in the compound page.
Signature · 3 lines
Cheers, David / dhildenb