Skip to contentSkip to conversation

mm: free compound page with correct order

PATCH In review Yu Zhao 3 participants 6 messages Oct 15, 2014 – Oct 24, 2014

Subsystems (4)
lore
Share & tools
lore

Yu Zhao

author
#0 lore raw

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.

Fixes 97ae17497e99 ("thp: implement refcounting for huge zero page")
---
 mm/huge_memory.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +2 -2
index 74c78aa..780d12c 100644
@@ -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
author
#1 lore raw

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
index 736d8e1..5bf44e4 100644
@@ -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
maintainer
#3 lore raw
In reply to Yu Zhao
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() 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.

Fixes 97ae17497e99 ("thp: implement refcounting for huge zero page")

It's two years old and nobody noticed the memory leak, so presumably it happens rarely.

Quoted 10 lines
---
 mm/huge_memory.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 74c78aa..780d12c 100644

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 -1
 		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));

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 -0
 		return HPAGE_PMD_NR;
 	}

Were you able to observe the leakage in practice? If so, under what circumstances?

#4 lore raw
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
#2 lore raw

Re: [PATCH v2 2/2] mm: verify compound order when freeing a page

In reply to Yu Zhao
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.

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

Your Singed-off-by should come first.

Signature · 1 line
 Kirill A. Shutemov
author
#5 lore raw
In reply to Andrew Morton
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() 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.

Fixes 97ae17497e99 ("thp: implement refcounting for huge zero page")

It's two years old and nobody noticed the memory leak, so presumably it happens rarely.

Quoted 10 lines
---
 mm/huge_memory.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 74c78aa..780d12c 100644

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 -1
 		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));

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 -0
 		return 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.