mm: free compound page with correct order
Subsystems (4)
PATCH v2 1/2 mm: free compound page with correct order
Compound page should be freed by put_page() or free_pages() with correct order. Not doing so will cause tail pages leaked.
The compound order can be obtained by compound_order() or use HPAGE_PMD_ORDER in our case. Some people would argue the latter is faster but I prefer the former which is more general.
--- mm/huge_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +2 -2
@@ -200,7 +200,7 @@ retry:preempt_disable ();if (cmpxchg (&huge_zero_page,NULL , zero_page)) {preempt_enable (); -__free_page (zero_page); +__free_pages (zero_page,compound_order (zero_page)); goto retry; }
@@ -232,7 +232,7 @@ static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,if (atomic_cmpxchg (&huge_zero_refcount,1 ,0 ) ==1 ) {struct page *zero_page =xchg (&huge_zero_page,NULL );BUG_ON (zero_page ==NULL ); -__free_page (zero_page); +__free_pages (zero_page,compound_order (zero_page));return HPAGE_PMD_NR ; }
Signature · 1 line
2.1.0.rc2.206.gedb03e5
PATCH v2 2/2 mm: verify compound order when freeing a page
This allows us to catch the bug fixed in the previous patch (mm: free compound page with correct order).
Here we also verify whether a page is tail page or not -- tail pages are supposed to be freed along with their head, not by themselves.
--- mm/page_alloc.c | 3 +++ 1 file changed, 3 insertions(+)
mm/page_alloc.c +3 -0
@@ -750,6 +750,9 @@ static bool free_pages_prepare(struct page *page, unsigned int order)int i;int bad =0 ; +VM_BUG_ON_PAGE (PageTail (page), page); +VM_BUG_ON_PAGE (PageHead (page) &&compound_order (page) != order, page); +trace_mm_page_free (page, order);kmemcheck_free_shadow (page, order);
Signature · 1 line
2.1.0.rc2.206.gedb03e5
Re: [PATCH v2 2/2] mm: verify compound order when freeing a page
Quoted 8 lines · Yu Zhao
This allows us to catch the bug fixed in the previous patch (mm: free compound page with correct order).
Here we also verify whether a page is tail page or not -- tail pages are supposed to be freed along with their head, not by themselves.
Reviewed-by Kirill A. Shutemov <[email protected]>
I didn't give this tag. It's okay in this case, but please do not assume that you've got Reviewed-by, unless the person said it explicitly.
Quoted 1 line
Signed-off-by Yu Zhao <[email protected]>
Your Singed-off-by should come first.
Signature · 1 line
Kirill A. Shutemov
Andrew Morton
maintainerQuoted 10 lines · On Wed, 15 Oct 2014 12:20:04 -0700 Yu Zhao <[email protected]> wrote:
Compound page should be freed by
put_page()orfree_pages()with correct order. Not doing so will cause tail pages leaked.The compound order can be obtained by
compound_order()or useHPAGE_PMD_ORDERin our case. Some people would argue the latter is faster but I prefer the former which is more general.Fixes 97ae17497e99 ("thp: implement refcounting for huge zero page")Cc [email protected] (v3.8+)
It's two years old and nobody noticed the memory leak, so presumably it happens rarely.
Quoted 10 lines
Signed-off-by Yu Zhao <[email protected]>--- mm/huge_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/huge_memory.c +0
-0
mm/huge_memory.c retry: · line 203
Quoted 5 lines
mm/huge_memory.c +1
-1@@ -200,7 +200,7 @@ retry:preempt_disable ();if (cmpxchg (&huge_zero_page,NULL , zero_page)) {preempt_enable (); -__free_page (zero_page); +__free_pages (zero_page,compound_order (zero_page));
This is rare.
mm/huge_memory.c static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, · line 235
Quoted 9 lines
mm/huge_memory.c +1
-1goto retry; }@@ -232,7 +232,7 @@ static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,if (atomic_cmpxchg (&huge_zero_refcount,1 ,0 ) ==1 ) {struct page *zero_page =xchg (&huge_zero_page,NULL );BUG_ON (zero_page ==NULL ); -__free_page (zero_page); +__free_pages (zero_page,compound_order (zero_page));
But I'm surprised that this is also rare. It makes me wonder if this code is working correctly.
mm/huge_memory.c static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, · line 237
Quoted 2 lines
mm/huge_memory.c +0
-0return HPAGE_PMD_NR ; }
Were you able to observe the leakage in practice? If so, under what circumstances?
Quoted 9 lines · Andrew Morton
mm/huge_memory.c static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, · line 235
Quoted 6 lines
mm/huge_memory.c +1
-1@@ -232,7 +232,7 @@ static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,if (atomic_cmpxchg (&huge_zero_refcount,1 ,0 ) ==1 ) {struct page *zero_page =xchg (&huge_zero_page,NULL );BUG_ON (zero_page ==NULL ); -__free_page (zero_page); +__free_pages (zero_page,compound_order (zero_page));But I'm surprised that this is also rare. It makes me wonder if this code is working correctly.
This should be rare too. To get here we need a situation when huge zero page is allocated, but not mapped and we get memory presure to trigger shrinker.
Signature · 1 line
Kirill A. Shutemov
Quoted 47 lines · Andrew Morton
Quoted 10 lines · On Wed, 15 Oct 2014 12:20:04 -0700 Yu Zhao <[email protected]> wrote:
Compound page should be freed by
put_page()orfree_pages()with correct order. Not doing so will cause tail pages leaked.The compound order can be obtained by
compound_order()or useHPAGE_PMD_ORDERin our case. Some people would argue the latter is faster but I prefer the former which is more general.Fixes 97ae17497e99 ("thp: implement refcounting for huge zero page")Cc [email protected] (v3.8+)It's two years old and nobody noticed the memory leak, so presumably it happens rarely.
Quoted 10 lines
Signed-off-by Yu Zhao <[email protected]>--- mm/huge_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/huge_memory.c +0
-0mm/huge_memory.c retry: · line 203
Quoted 5 lines
mm/huge_memory.c +1
-1@@ -200,7 +200,7 @@ retry:preempt_disable ();if (cmpxchg (&huge_zero_page,NULL , zero_page)) {preempt_enable (); -__free_page (zero_page); +__free_pages (zero_page,compound_order (zero_page));This is rare.
mm/huge_memory.c static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, · line 235
Quoted 9 lines
mm/huge_memory.c +1
-1goto retry; }@@ -232,7 +232,7 @@ static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,if (atomic_cmpxchg (&huge_zero_refcount,1 ,0 ) ==1 ) {struct page *zero_page =xchg (&huge_zero_page,NULL );BUG_ON (zero_page ==NULL ); -__free_page (zero_page); +__free_pages (zero_page,compound_order (zero_page));But I'm surprised that this is also rare. It makes me wonder if this code is working correctly.
Quoted 5 lines
mm/huge_memory.c static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, · line 237
Quoted 2 lines
mm/huge_memory.c +0
-0return HPAGE_PMD_NR ; }Were you able to observe the leakage in practice? If so, under what circumstances?
Yes, not just on our servers (the worst case we saw is 11G leaked on a 48G machine) but also on our workstations running Ubuntu based distro.
$ cat /proc/vmstat | grep thp_zero_page_alloc thp_zero_page_alloc 55 thp_zero_page_alloc_failed 0
This means there is (thp_zero_page_alloc - 1) * (2M - 4K) memory leaked.