Skip to contentSkip to conversation

page_frag: Recover from memory pressure

PATCH In review Matthew Wilcox (Oracle) 5 participants 9 messages Nov 5, 2020 – Nov 15, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 page_frag: Recover from memory pressure

When the machine is under extreme memory pressure, the page_frag allocator signals this to the networking stack by marking allocations with the 'pfmemalloc' flag, which causes non-essential packets to be dropped. Unfortunately, even after the machine recovers from the low memory condition, the page continues to be used by the page_frag allocator, so all allocations from this page will continue to be dropped.

Fix this by freeing and re-allocating the page instead of recycling it.

Cc Aruna Ramakrishna <[email protected]>
Cc Bert Barbe <[email protected]>
Cc Rama Nichanamatlu <[email protected]>
Cc Venkat Venkatsubra <[email protected]>
Cc Manjunath Patil <[email protected]>
Cc Joe Jin <[email protected]>
Cc SRINIVAS <[email protected]>
Fixes 79930f5892e ("net: do not deplete pfmemalloc reserve")
---
 mm/page_alloc.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/page_alloc.c +4 -0
index 778e815130a6..631546ae1c53 100644
@@ -5139,6 +5139,10 @@ void *page_frag_alloc(struct page_frag_cache *nc,
 
 		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
 			goto refill;
+		if (nc->pfmemalloc) {
+			free_the_page(page, compound_order(page));
+			goto refill;
+		}
 
 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
 		/* if size can vary use size else just use PAGE_SIZE */
Signature · 1 line
2.28.0
#1 lore raw
Quoted 28 lines · Matthew Wilcox (Oracle)

When the machine is under extreme memory pressure, the page_frag allocator signals this to the networking stack by marking allocations with the 'pfmemalloc' flag, which causes non-essential packets to be dropped. Unfortunately, even after the machine recovers from the low memory condition, the page continues to be used by the page_frag allocator, so all allocations from this page will continue to be dropped.

Quoted 1 line

Fix this by freeing and re-allocating the page instead of recycling it.

Cc Aruna Ramakrishna <[email protected]>
Cc Bert Barbe <[email protected]>
Cc Rama Nichanamatlu <[email protected]>
Cc Venkat Venkatsubra <[email protected]>
Cc Manjunath Patil <[email protected]>
Cc Joe Jin <[email protected]>
Cc SRINIVAS <[email protected]>
Fixes 79930f5892e ("net: do not deplete pfmemalloc reserve")
---
  mm/page_alloc.c | 4 ++++
  1 file changed, 4 insertions(+)
mm/page_alloc.c +0 -0
index 778e815130a6..631546ae1c53 100644

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5144

Quoted 6 lines
mm/page_alloc.c +3 -0
@@ -5139,6 +5139,10 @@ void *page_frag_alloc(struct page_frag_cache *nc,
 
  		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
  			goto refill;
+		if (nc->pfmemalloc) {
+			free_the_page(page, compound_order(page));
+			goto refill;

Theoretically the refill can fail and we return NULL while leaving nc->va pointing to a freed page, so I think you should set nc->va to NULL.

Geez, can't the same thing already happen after we sub the nc->pagecnt_bias from page ref, and last users of the page fragments then return them and dec the ref to zero and the page gets freed?

Quoted 5 lines
mm/page_alloc.c +1 -0
+		}
 
  #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
  		/* if size can vary use size else just use PAGE_SIZE */
#2 lore raw
Quoted 11 lines · Vlastimil Babka

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5143

Quoted 7 lines

+++ b/mm/page_alloc.c

mm/page_alloc.c +3 -0
@@ -5139,6 +5139,10 @@ void *page_frag_alloc(struct page_frag_cache *nc,
  		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
  			goto refill;
+		if (nc->pfmemalloc) {
+			free_the_page(page, compound_order(page));
+			goto refill;

Theoretically the refill can fail and we return NULL while leaving nc->va pointing to a freed page, so I think you should set nc->va to NULL.

Quoted 3 lines

Geez, can't the same thing already happen after we sub the nc->pagecnt_bias from page ref, and last users of the page fragments then return them and dec the ref to zero and the page gets freed?

I don't think you read __page_frag_cache_refill() closely enough ...

        if (unlikely(!page))
                page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);

        nc->va = page ? page_address(page) : NULL;
#3 lore raw
Quoted 12 lines · Matthew Wilcox
Quoted 11 lines · Vlastimil Babka

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5143

Quoted 7 lines

+++ b/mm/page_alloc.c

mm/page_alloc.c +3 -0
@@ -5139,6 +5139,10 @@ void *page_frag_alloc(struct page_frag_cache *nc,
  		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
  			goto refill;
+		if (nc->pfmemalloc) {
+			free_the_page(page, compound_order(page));
+			goto refill;

Theoretically the refill can fail and we return NULL while leaving nc->va pointing to a freed page, so I think you should set nc->va to NULL.

Quoted 5 lines
Quoted 3 lines

Geez, can't the same thing already happen after we sub the nc->pagecnt_bias from page ref, and last users of the page fragments then return them and dec the ref to zero and the page gets freed?

I don't think you read __page_frag_cache_refill() closely enough ...

Or rather not at all, sorry :) somehow I just saw "ah here we call the page allocator".

Quoted 7 lines
         if (unlikely(!page))
                 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);

         nc->va = page ? page_address(page) : NULL;
#4 lore raw
In reply to Matthew Wilcox (Oracle)
Quoted 9 lines · Matthew Wilcox (Oracle)

When the machine is under extreme memory pressure, the page_frag allocator signals this to the networking stack by marking allocations with the 'pfmemalloc' flag, which causes non-essential packets to be dropped. Unfortunately, even after the machine recovers from the low memory condition, the page continues to be used by the page_frag allocator, so all allocations from this page will continue to be dropped.

Fix this by freeing and re-allocating the page instead of recycling it.

Quoted 10 lines
Cc Aruna Ramakrishna <[email protected]>
Cc Bert Barbe <[email protected]>
Cc Rama Nichanamatlu <[email protected]>
Cc Venkat Venkatsubra <[email protected]>
Cc Manjunath Patil <[email protected]>
Cc Joe Jin <[email protected]>
Cc SRINIVAS <[email protected]>
Fixes 79930f5892e ("net: do not deplete pfmemalloc reserve")

Your patch looks fine, although this Fixes: tag seems incorrect.

79930f5892e ("net: do not deplete pfmemalloc reserve") was propagating the page pfmemalloc status into the skb, and seems correct to me.

The bug was the page_frag_alloc() was keeping a problematic page for an arbitrary period of time ?

Quoted 10 lines
---
 mm/page_alloc.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/page_alloc.c +0 -0
index 778e815130a6..631546ae1c53 100644

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5142

Quoted 4 lines
mm/page_alloc.c +1 -0
@@ -5139,6 +5139,10 @@ void *page_frag_alloc(struct page_frag_cache *nc,
 
 		if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
 			goto refill;
+		if (nc->pfmemalloc) {
                if (unlikely(nc->pfmemalloc)) {
Quoted 7 lines
mm/page_alloc.c +3 -0
+			free_the_page(page, compound_order(page));
+			goto refill;
+		}
 
 #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
 		/* if size can vary use size else just use PAGE_SIZE */
#5 lore raw
Quoted 21 lines · Eric Dumazet
Quoted 9 lines · Matthew Wilcox (Oracle)

When the machine is under extreme memory pressure, the page_frag allocator signals this to the networking stack by marking allocations with the 'pfmemalloc' flag, which causes non-essential packets to be dropped. Unfortunately, even after the machine recovers from the low memory condition, the page continues to be used by the page_frag allocator, so all allocations from this page will continue to be dropped.

Fix this by freeing and re-allocating the page instead of recycling it.

Quoted 10 lines
Cc Aruna Ramakrishna <[email protected]>
Cc Bert Barbe <[email protected]>
Cc Rama Nichanamatlu <[email protected]>
Cc Venkat Venkatsubra <[email protected]>
Cc Manjunath Patil <[email protected]>
Cc Joe Jin <[email protected]>
Cc SRINIVAS <[email protected]>
Fixes 79930f5892e ("net: do not deplete pfmemalloc reserve")
Quoted 7 lines

Your patch looks fine, although this Fixes: tag seems incorrect.

79930f5892e ("net: do not deplete pfmemalloc reserve") was propagating the page pfmemalloc status into the skb, and seems correct to me.

The bug was the page_frag_alloc() was keeping a problematic page for an arbitrary period of time ?

Isn't this the commit which unmasks the problem, though? I don't think it's the buggy commit, but if your tree doesn't have 79930f5892e, then you don't need this patch.

Or are you saying the problem dates back all the way to c93bdd0e03e8 ("netvm: allow skb allocation to use PFMEMALLOC reserves")

Quoted 3 lines

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5142

Quoted 1 line
mm/page_alloc.c +1 -0
+		if (nc->pfmemalloc) {
                if (unlikely(nc->pfmemalloc)) {

ACK. Will make the change once we've settled on an appropriate Fixes tag.

#6 lore raw
Quoted 34 lines · Matthew Wilcox
Quoted 21 lines · Eric Dumazet
Quoted 9 lines · Matthew Wilcox (Oracle)

When the machine is under extreme memory pressure, the page_frag allocator signals this to the networking stack by marking allocations with the 'pfmemalloc' flag, which causes non-essential packets to be dropped. Unfortunately, even after the machine recovers from the low memory condition, the page continues to be used by the page_frag allocator, so all allocations from this page will continue to be dropped.

Fix this by freeing and re-allocating the page instead of recycling it.

Quoted 10 lines
Cc Aruna Ramakrishna <[email protected]>
Cc Bert Barbe <[email protected]>
Cc Rama Nichanamatlu <[email protected]>
Cc Venkat Venkatsubra <[email protected]>
Cc Manjunath Patil <[email protected]>
Cc Joe Jin <[email protected]>
Cc SRINIVAS <[email protected]>
Fixes 79930f5892e ("net: do not deplete pfmemalloc reserve")
Quoted 7 lines

Your patch looks fine, although this Fixes: tag seems incorrect.

79930f5892e ("net: do not deplete pfmemalloc reserve") was propagating the page pfmemalloc status into the skb, and seems correct to me.

The bug was the page_frag_alloc() was keeping a problematic page for an arbitrary period of time ?

Isn't this the commit which unmasks the problem, though? I don't think it's the buggy commit, but if your tree doesn't have 79930f5892e, then you don't need this patch.

Quoted 8 lines

Or are you saying the problem dates back all the way to c93bdd0e03e8 ("netvm: allow skb allocation to use PFMEMALLOC reserves")

Quoted 3 lines

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5142

Quoted 1 line
mm/page_alloc.c +1 -0
+		if (nc->pfmemalloc) {
                if (unlikely(nc->pfmemalloc)) {

ACK. Will make the change once we've settled on an appropriate Fixes tag.

Which commit should I claim this fixes?

#7 lore raw
Quoted 38 lines · Matthew Wilcox
Quoted 37 lines · Matthew Wilcox
Quoted 21 lines · Eric Dumazet
Quoted 9 lines · Matthew Wilcox (Oracle)

When the machine is under extreme memory pressure, the page_frag allocator signals this to the networking stack by marking allocations with the 'pfmemalloc' flag, which causes non-essential packets to be dropped. Unfortunately, even after the machine recovers from the low memory condition, the page continues to be used by the page_frag allocator, so all allocations from this page will continue to be dropped.

Fix this by freeing and re-allocating the page instead of recycling it.

Quoted 10 lines
Cc Aruna Ramakrishna <[email protected]>
Cc Bert Barbe <[email protected]>
Cc Rama Nichanamatlu <[email protected]>
Cc Venkat Venkatsubra <[email protected]>
Cc Manjunath Patil <[email protected]>
Cc Joe Jin <[email protected]>
Cc SRINIVAS <[email protected]>
Fixes 79930f5892e ("net: do not deplete pfmemalloc reserve")
Quoted 7 lines

Your patch looks fine, although this Fixes: tag seems incorrect.

79930f5892e ("net: do not deplete pfmemalloc reserve") was propagating the page pfmemalloc status into the skb, and seems correct to me.

The bug was the page_frag_alloc() was keeping a problematic page for an arbitrary period of time ?

Isn't this the commit which unmasks the problem, though? I don't think it's the buggy commit, but if your tree doesn't have 79930f5892e, then you don't need this patch.

Or are you saying the problem dates back all the way to c93bdd0e03e8 ("netvm: allow skb allocation to use PFMEMALLOC reserves")

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5142

Quoted 1 line
mm/page_alloc.c +1 -0
+		if (nc->pfmemalloc) {
                if (unlikely(nc->pfmemalloc)) {

ACK. Will make the change once we've settled on an appropriate Fixes tag.

Which commit should I claim this fixes?

Hmm, no big deal, lets not waste time on tracking precise bug origin.

#8 lore raw

From linux-next, this patch is not in akpm branch.

According to discussion with Matthew offline, I will take the author of this patch as Matthew was providing review for patch and suggesting a better alternative.

Therefore, it will be much more easier or me to track this patch.

I will re-send the patch as v2 with:

1. change author from Matthew to Dongli
2. Add references to all prior discussions
3. Add more details to commit message so that it is much more easier to search online when this issue is encountered by other people again.
4. Add "Acked-by: Vlastimil Babka <[email protected]>".

Thank you very much!

Dongli Zhang

Quoted 51 lines · Eric Dumazet
Quoted 38 lines · Matthew Wilcox
Quoted 37 lines · Matthew Wilcox
Quoted 21 lines · Eric Dumazet
Quoted 9 lines · Matthew Wilcox (Oracle)

When the machine is under extreme memory pressure, the page_frag allocator signals this to the networking stack by marking allocations with the 'pfmemalloc' flag, which causes non-essential packets to be dropped. Unfortunately, even after the machine recovers from the low memory condition, the page continues to be used by the page_frag allocator, so all allocations from this page will continue to be dropped.

Fix this by freeing and re-allocating the page instead of recycling it.

Quoted 10 lines
Cc Aruna Ramakrishna <[email protected]>
Cc Bert Barbe <[email protected]>
Cc Rama Nichanamatlu <[email protected]>
Cc Venkat Venkatsubra <[email protected]>
Cc Manjunath Patil <[email protected]>
Cc Joe Jin <[email protected]>
Cc SRINIVAS <[email protected]>
Fixes 79930f5892e ("net: do not deplete pfmemalloc reserve")
Quoted 7 lines

Your patch looks fine, although this Fixes: tag seems incorrect.

79930f5892e ("net: do not deplete pfmemalloc reserve") was propagating the page pfmemalloc status into the skb, and seems correct to me.

The bug was the page_frag_alloc() was keeping a problematic page for an arbitrary period of time ?

Isn't this the commit which unmasks the problem, though? I don't think it's the buggy commit, but if your tree doesn't have 79930f5892e, then you don't need this patch.

Or are you saying the problem dates back all the way to c93bdd0e03e8 ("netvm: allow skb allocation to use PFMEMALLOC reserves")

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

mm/page_alloc.c void *page_frag_alloc(struct page_frag_cache *nc, · line 5142

Quoted 1 line
mm/page_alloc.c +1 -0
+		if (nc->pfmemalloc) {
                if (unlikely(nc->pfmemalloc)) {

ACK. Will make the change once we've settled on an appropriate Fixes tag.

Which commit should I claim this fixes?

Hmm, no big deal, lets not waste time on tracking precise bug origin.