Skip to contentSkip to conversation

mm: shrink_all_memory improvements

RFC PATCH In review Rafael J. Wysocki 4 participants 14 messages Feb 27, 2006 – Feb 28, 2006

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

cover mm: shrink_all_memory improvements

Hi,

The following two patches are designed to improve the shrink_all_memory() function used by swsusp and other pm functions.

The first patch makes shrink_all_memory() overflow-resistant. The problem is that, AFAICT, balance_pgdat(pgdat, nr_to_free, 0) may free more than nr_to_free pages, in which case nr_to_free, being unsigned, will overflow (and obviously it cannot be less than 0). Also if the argument is too big, strange things may happen.

The first patch adds a workaround of the problem that shrink_all_memory() may return 0 even if there still are some pages to free. WIth this patch applied it sometimes frees 2 times as many pages as without it on my box.

Please have a look and comment.

Greetings,
Rafael

Signature · 1 line
Beer is proof that God loves us and wants us to be happy - Benjamin Franklin
#1 lore raw

RFC PATCH 1/2 mm: make shrink_all_memory overflow-resistant

Make shrink_all_memory() overflow-resistant.

---
 include/linux/swap.h |    2 +-
 mm/vmscan.c          |    9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
mm/vmscan.c +5 -4
Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================
@@ -1785,18 +1785,19 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned int nr_pages)
 {
 	pg_data_t *pgdat;
-	unsigned long nr_to_free = nr_pages;
-	int ret = 0;
+	long long nr_to_free = nr_pages;
+	unsigned long ret = 0;
 	struct reclaim_state reclaim_state = {
 		.reclaimed_slab = 0,
 	};
 
 	current->reclaim_state = &reclaim_state;
 	for_each_pgdat(pgdat) {
-		int freed;
+		unsigned long freed;
+
 		freed = balance_pgdat(pgdat, nr_to_free, 0);
 		ret += freed;
 		nr_to_free -= freed;
include/linux/swap.h +1 -1
Index: linux-2.6.16-rc4-mm2/include/linux/swap.h
===================================================================
@@ -173,7 +173,7 @@ extern void swap_setup(void);
 
 /* linux/mm/vmscan.c */
 extern unsigned long try_to_free_pages(struct zone **, gfp_t);
-extern int shrink_all_memory(unsigned long nr_pages);
+extern unsigned long shrink_all_memory(unsigned int nr_pages);
 extern int vm_swappiness;
 
 #ifdef CONFIG_NUMA
#8 lore raw

RFC PATCH 2/2 mm: make shrink_all_memory try harder

In reply to Rafael J. Wysocki

Make shrink_all_memory() repeat the attempts to free more memory if there seems to be no pages to free.

---
 mm/vmscan.c |   22 +++++++++++++++-------
 1 files changed, 15 insertions(+), 7 deletions(-)
mm/vmscan.c +15 -7
Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================
@@ -33,6 +33,7 @@
 #include <linux/cpuset.h>
 #include <linux/notifier.h>
 #include <linux/rwsem.h>
+#include <linux/delay.h>
 
 #include <asm/tlbflush.h>
 #include <asm/div64.h>
@@ -1793,17 +1794,24 @@ unsigned long shrink_all_memory(unsigned
 	struct reclaim_state reclaim_state = {
 		.reclaimed_slab = 0,
 	};
+	int retry = 2;
 
 	current->reclaim_state = &reclaim_state;
-	for_each_pgdat(pgdat) {
-		unsigned long freed;
+	do {
+		for_each_pgdat(pgdat) {
+			unsigned long freed;
 
-		freed = balance_pgdat(pgdat, nr_to_free, 0);
-		ret += freed;
-		nr_to_free -= freed;
-		if (nr_to_free <= 0)
+			freed = balance_pgdat(pgdat, nr_to_free, 0);
+			ret += freed;
+			nr_to_free -= freed;
+			if (nr_to_free <= 0)
+				break;
+		}
+		if (ret > 0)
 			break;
-	}
+		if (retry)
+			msleep_interruptible(100);
+	} while (retry--);
 	current->reclaim_state = NULL;
 	return ret;
 }
#13 lore raw
In reply to Rafael J. Wysocki

Hi!

Quoted 9 lines

The following two patches are designed to improve the shrink_all_memory() function used by swsusp and other pm functions.

The first patch makes shrink_all_memory() overflow-resistant. The problem is that, AFAICT, balance_pgdat(pgdat, nr_to_free, 0) may free more than nr_to_free pages, in which case nr_to_free, being unsigned, will overflow (and obviously it cannot be less than 0). Also if the argument is too big, strange things may happen.

Quoted 5 lines

The first patch adds a workaround of the problem that shrink_all_memory() may return 0 even if there still are some pages to free. WIth this patch applied it sometimes frees 2 times as many pages as without it on my box.

Please have a look and comment.

ACK on the first one, Andrew, please suggest how to do the second one better.
Pavel

Signature · 1 line
Web maintainer for suspend.sf.net (www.sf.net/projects/suspend) wanted...
#2 lore raw

Re: [RFC][PATCH -mm 1/2] mm: make shrink_all_memory overflow-resistant

In reply to Rafael J. Wysocki
Quoted 14 lines · Rafael J. Wysocki

Make shrink_all_memory() overflow-resistant.

---
 include/linux/swap.h |    2 +-
 mm/vmscan.c          |    9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)

Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1788

Quoted 5 lines
mm/vmscan.c +1 -1
@@ -1785,18 +1785,19 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned int nr_pages)

What about the callers of shrink_all_memory() who currently expect it to return an int, have you checked how they will react to you changing the return type (and signedness) ?

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1791

Quoted 5 lines
mm/vmscan.c +1 -2
 {
        pg_data_t *pgdat;
-       unsigned long nr_to_free = nr_pages;
-       int ret = 0;
+       long long nr_to_free = nr_pages;

'nr_pages' passed to the function is an unsigned int, why this change? also, nr_to_free is later passed to balance_pgdat() as the second argument and balance_pgdat() expects to be passed an int.

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1799

Quoted 9 lines
mm/vmscan.c +2 -1
+       unsigned long ret = 0;
        struct reclaim_state reclaim_state = {
                .reclaimed_slab = 0,
        };
 
        current->reclaim_state = &reclaim_state;
        for_each_pgdat(pgdat) {
-               int freed;
+               unsigned long freed;

balance_pgdat() returns a plain int, so what's the point of making 'freed' an unsigned long?

Quoted 10 lines
mm/vmscan.c +1 -0
+
                freed = balance_pgdat(pgdat, nr_to_free, 0);
                ret += freed;
                nr_to_free -= freed;

Index: linux-2.6.16-rc4-mm2/include/linux/swap.h
===================================================================

include/linux/swap.h extern void swap_setup(void); · line 179

Quoted 8 lines
include/linux/swap.h +1 -1
@@ -173,7 +173,7 @@ extern void swap_setup(void);
 
 /* linux/mm/vmscan.c */
 extern unsigned long try_to_free_pages(struct zone **, gfp_t);
-extern int shrink_all_memory(unsigned long nr_pages);
+extern unsigned long shrink_all_memory(unsigned int nr_pages);
 extern int vm_swappiness;
 
 #ifdef CONFIG_NUMA

This patch may be correct or it may be wrong, I've not spend enough time staring at it and follow the code it calls and gets called by to say which, but I for one would like an explanation of why these changes are made and why they are correct.
There's a distinct lack of a changelog/explanation with this patch. Or maybe I'm just dense and can't see the obvious correctness, but if that's the case I'd still like to be enlightened :)

--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html

#3 lore raw

Re: [RFC][PATCH -mm 1/2] mm: make shrink_all_memory overflow-resistant

Quoted 10 lines · Jesper Juhl
Quoted 9 lines · Rafael J. Wysocki

Make shrink_all_memory() overflow-resistant.

---
 include/linux/swap.h |    2 +-
 mm/vmscan.c          |    9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
Quoted 14 lines

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1788

Quoted 10 lines

Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================

mm/vmscan.c +1 -1
@@ -1785,18 +1785,19 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned int nr_pages)

What about the callers of shrink_all_memory() who currently expect it to return an int, have you checked how they will react to you changing the return type (and signedness) ?

That's okay, we checked all 3 callers :-).
Pavel

Signature · 1 line
Web maintainer for suspend.sf.net (www.sf.net/projects/suspend) wanted...
#4 lore raw

Re: [RFC][PATCH -mm 1/2] mm: make shrink_all_memory overflow-resistant

Quoted 26 lines · Pavel Machek
Quoted 10 lines · Jesper Juhl
Quoted 9 lines · Rafael J. Wysocki

Make shrink_all_memory() overflow-resistant.

---
 include/linux/swap.h |    2 +-
 mm/vmscan.c          |    9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
Quoted 14 lines

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1788

Quoted 10 lines

Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================

mm/vmscan.c +1 -1
@@ -1785,18 +1785,19 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned int nr_pages)

What about the callers of shrink_all_memory() who currently expect it to return an int, have you checked how they will react to you changing the return type (and signedness) ?

Quoted 1 line

That's okay, we checked all 3 callers :-).

I'm sure you did, I'm not saying you didn't.
All I'm asking for is a short explanation of why the changes the patch makes are correct since that's not obvious to me, and I'd like to understand the patch.

--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html

#5 lore raw

Re: [RFC][PATCH -mm 1/2] mm: make shrink_all_memory overflow-resistant

In reply to Jesper Juhl

Hi,

Quoted 10 lines · Jesper Juhl
Quoted 9 lines · Rafael J. Wysocki

Make shrink_all_memory() overflow-resistant.

---
 include/linux/swap.h |    2 +-
 mm/vmscan.c          |    9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
Quoted 14 lines

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1788

Quoted 10 lines

Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================

mm/vmscan.c +1 -1
@@ -1785,18 +1785,19 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned int nr_pages)

What about the callers of shrink_all_memory() who currently expect it to return an int, have you checked how they will react to you changing the return type (and signedness) ?

AFAICT, there are not any.

Quoted 9 lines

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1791

Quoted 5 lines
mm/vmscan.c +1 -2
 {
        pg_data_t *pgdat;
-       unsigned long nr_to_free = nr_pages;
-       int ret = 0;
+       long long nr_to_free = nr_pages;

'nr_pages' passed to the function is an unsigned int, why this change? also, nr_to_free is later passed to balance_pgdat() as the second argument and balance_pgdat() expects to be passed an int.

First, the balance_pgdat() in the current -mm expects to be passed unsigned long here.

Second, say you pass 100 and balance_pgdat() returns 110. Then if nr_to_free is unsigned, it will become a huge positive number instead of a negative number, as it should be. Thus it has to be signed IMO.

It could be long, but on i386 long is the same as int.

Quoted 5 lines
Quoted 5 lines
mm/vmscan.c +1 -0
+       unsigned long ret = 0;
        struct reclaim_state reclaim_state = {
                .reclaimed_slab = 0,
        };
 
Quoted 7 lines

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1799

Quoted 4 lines
mm/vmscan.c +1 -1
        current->reclaim_state = &reclaim_state;
        for_each_pgdat(pgdat) {
-               int freed;
+               unsigned long freed;

balance_pgdat() returns a plain int, so what's the point of making 'freed' an unsigned long?

Well, at least the balance_pgdat() I'm looking at now returns unsigned long.

Quoted 16 lines
Quoted 16 lines
mm/vmscan.c +1 -0
+
                freed = balance_pgdat(pgdat, nr_to_free, 0);
                ret += freed;
                nr_to_free -= freed;

Index: linux-2.6.16-rc4-mm2/include/linux/swap.h
===================================================================

include/linux/swap.h +1 -1
@@ -173,7 +173,7 @@ extern void swap_setup(void);
 
 /* linux/mm/vmscan.c */
 extern unsigned long try_to_free_pages(struct zone **, gfp_t);
-extern int shrink_all_memory(unsigned long nr_pages);
+extern unsigned long shrink_all_memory(unsigned int nr_pages);
 extern int vm_swappiness;
 
Quoted 11 lines

include/linux/swap.h extern void swap_setup(void); · line 179

Quoted 2 lines
include/linux/swap.h +0 -0
 #ifdef CONFIG_NUMA

This patch may be correct or it may be wrong, I've not spend enough time staring at it and follow the code it calls and gets called by to say which, but I for one would like an explanation of why these changes are made and why they are correct.
There's a distinct lack of a changelog/explanation with this patch. Or maybe I'm just dense and can't see the obvious correctness, but if that's the case I'd still like to be enlightened :)

Perhaps I should have explained it in a more detailed way in the changelog.

The problem is that, AFAICT, balance_pgdat(pgdat, nr_to_free, 0) may free more than nr_to_free pages, in which case nr_to_free, being unsigned, will overflow (and obviously it cannot be less than 0).  Also if nr_pages is too big, strange things may happen (without the patch, that is).

Greetings,
Rafael

#6 lore raw

Re: [RFC][PATCH -mm 1/2] mm: make shrink_all_memory overflow-resistant

In reply to Rafael J. Wysocki
Quoted 3 lines · Rafael J. Wysocki

Make shrink_all_memory() overflow-resistant.

As you've probably noticed, I'm moving toward making all the scalars which hold counts of pages in the VM toward unsigned long. For uniformity, and because there is a small risk that a huge machine with 4k pages could actually overflow a 32-bit counter.

Quoted 11 lines
---
 include/linux/swap.h |    2 +-
 mm/vmscan.c          |    9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)

Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1788

Quoted 5 lines
mm/vmscan.c +1 -1
@@ -1785,18 +1785,19 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned int nr_pages)

So nr_pages should remain unsigned long.

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1789

Quoted 1 line
mm/vmscan.c +1 -0
+	long long nr_to_free = nr_pages;

As should nr_to_free.

The actual bug is the (unsigned <= 0) thing. So how's about this?

include/linux/swap.h renamed from include/linux/swap.h~vmscan-use-unsigned-longs-shrink_all_memory-fix +1 -1
@@ -173,7 +173,7 @@ extern void swap_setup(void);
 
 /* linux/mm/vmscan.c */
 extern unsigned long try_to_free_pages(struct zone **, gfp_t);
-extern int shrink_all_memory(unsigned long nr_pages);
+extern unsigned long shrink_all_memory(unsigned long nr_pages);
 extern int vm_swappiness;
 
 #ifdef CONFIG_NUMA
mm/vmscan.c renamed from mm/vmscan.c~vmscan-use-unsigned-longs-shrink_all_memory-fix +5 -4
@@ -1779,22 +1779,23 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned long nr_pages)
 {
 	pg_data_t *pgdat;
 	unsigned long nr_to_free = nr_pages;
-	int ret = 0;
+	unsigned long ret = 0;
 	struct reclaim_state reclaim_state = {
 		.reclaimed_slab = 0,
 	};
 
 	current->reclaim_state = &reclaim_state;
 	for_each_pgdat(pgdat) {
-		int freed;
+		unsigned long freed;
+
 		freed = balance_pgdat(pgdat, nr_to_free, 0);
 		ret += freed;
 		nr_to_free -= freed;
-		if (nr_to_free <= 0)
+		if ((long)nr_to_free <= 0)
 			break;
 	}
 	current->reclaim_state = NULL;

_

#9 lore raw

Re: [RFC][PATCH -mm 2/2] mm: make shrink_all_memory try harder

In reply to Rafael J. Wysocki
Quoted 4 lines · Rafael J. Wysocki

Make shrink_all_memory() repeat the attempts to free more memory if there seems to be no pages to free.

This description doesn't describe what the problem is, not how the patch fixes it. So I'm kinda left guessing.

Quoted 29 lines

===================================================================

mm/vmscan.c +6 -6
@@ -33,6 +33,7 @@
 #include <linux/cpuset.h>
 #include <linux/notifier.h>
 #include <linux/rwsem.h>
+#include <linux/delay.h>
 
 #include <asm/tlbflush.h>
 #include <asm/div64.h>
@@ -1793,17 +1794,24 @@ unsigned long shrink_all_memory(unsigned
 	struct reclaim_state reclaim_state = {
 		.reclaimed_slab = 0,
 	};
+	int retry = 2;
 
 	current->reclaim_state = &reclaim_state;
-	for_each_pgdat(pgdat) {
-		unsigned long freed;
+	do {
+		for_each_pgdat(pgdat) {
+			unsigned long freed;
 
-		freed = balance_pgdat(pgdat, nr_to_free, 0);
-		ret += freed;
-		nr_to_free -= freed;
-		if (nr_to_free <= 0)
+			freed = balance_pgdat(pgdat, nr_to_free, 0);

mm/vmscan.c unsigned long shrink_all_memory(unsigned · line 1813

Quoted 10 lines
mm/vmscan.c +8 -1
+			ret += freed;
+			nr_to_free -= freed;
+			if (nr_to_free <= 0)
+				break;
+		}
+		if (ret > 0)
 			break;
-	}
+		if (retry)
+			msleep_interruptible(100);

TASK_INTERRUPTIBLE sleeps won't do anything if someone has sent this
process a signal. They should be used with caution.

Something like the below, I guess. But it's hard to fix something when you don't know what you're fixing.

swsusp should call drop_pagecache() and then drop_slab() before trying to use shrink_all_memory(), btw.

mm/vmscan.c renamed from mm/vmscan.c~mm-make-shrink_all_memory-try-harder +7 -0
diff -puN mm/vmscan.c~mm-make-shrink_all_memory-try-harder mm/vmscan.c
@@ -33,6 +33,7 @@
 #include <linux/cpuset.h>
 #include <linux/notifier.h>
 #include <linux/rwsem.h>
+#include <linux/delay.h>
 
 #include <asm/tlbflush.h>
 #include <asm/div64.h>
@@ -1783,11 +1784,13 @@ unsigned long shrink_all_memory(unsigned
 	pg_data_t *pgdat;
 	unsigned long nr_to_free = nr_pages;
 	unsigned long ret = 0;
+	unsigned retry = 2;
 	struct reclaim_state reclaim_state = {
 		.reclaimed_slab = 0,
 	};
 
 	current->reclaim_state = &reclaim_state;
+repeat:
 	for_each_pgdat(pgdat) {
 		unsigned long freed;
 
@@ -1797,6 +1800,10 @@ unsigned long shrink_all_memory(unsigned
 		if ((long)nr_to_free <= 0)
 			break;
 	}
+	if (retry-- && ret < nr_pages) {
+		blk_congestion_wait(WRITE, HZ/5);
+		goto repeat;
+	}
 	current->reclaim_state = NULL;
 	return ret;
 }

_

But then, the above could be implemented by the caller, so I don't know what's going on..

#7 lore raw

Re: [RFC][PATCH -mm 1/2] mm: make shrink_all_memory overflow-resistant

In reply to Andrew Morton
Quoted 29 lines · Andrew Morton
Quoted 3 lines · Rafael J. Wysocki

Make shrink_all_memory() overflow-resistant.

As you've probably noticed, I'm moving toward making all the scalars which hold counts of pages in the VM toward unsigned long. For uniformity, and because there is a small risk that a huge machine with 4k pages could actually overflow a 32-bit counter.

Quoted 11 lines
---
 include/linux/swap.h |    2 +-
 mm/vmscan.c          |    9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)

Index: linux-2.6.16-rc4-mm2/mm/vmscan.c
===================================================================

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1788

Quoted 5 lines
mm/vmscan.c +1 -1
@@ -1785,18 +1785,19 @@ void wakeup_kswapd(struct zone *zone, in
  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
  * pages.
  */
-int shrink_all_memory(unsigned long nr_pages)
+unsigned long shrink_all_memory(unsigned int nr_pages)

So nr_pages should remain unsigned long.

Quoted 3 lines

mm/vmscan.c void wakeup_kswapd(struct zone *zone, in · line 1789

Quoted 1 line
mm/vmscan.c +1 -0
+	long long nr_to_free = nr_pages;

As should nr_to_free.

OK

Quoted 1 line

The actual bug is the (unsigned <= 0) thing. So how's about this?

I like it (if my opinion matters here ;-)). Thanks a lot.

#10 lore raw

Re: [RFC][PATCH -mm 2/2] mm: make shrink_all_memory try harder

In reply to Andrew Morton
Quoted 8 lines · Andrew Morton
Quoted 4 lines · Rafael J. Wysocki

Make shrink_all_memory() repeat the attempts to free more memory if there seems to be no pages to free.

This description doesn't describe what the problem is, not how the patch fixes it. So I'm kinda left guessing.

I have described it in the 0/0 message, but I should have repeated that in the changelog, sorry.

The probelm is that shrink_all_memory() sometimes returns 0 when there are some freeable pages left. In swsusp we stop freeing memory when shrink_all_memory() returns 0, so this is a problem to us.

Quoted 44 lines
Quoted 29 lines

===================================================================

mm/vmscan.c +6 -6
@@ -33,6 +33,7 @@
 #include <linux/cpuset.h>
 #include <linux/notifier.h>
 #include <linux/rwsem.h>
+#include <linux/delay.h>
 
 #include <asm/tlbflush.h>
 #include <asm/div64.h>
@@ -1793,17 +1794,24 @@ unsigned long shrink_all_memory(unsigned
 	struct reclaim_state reclaim_state = {
 		.reclaimed_slab = 0,
 	};
+	int retry = 2;
 
 	current->reclaim_state = &reclaim_state;
-	for_each_pgdat(pgdat) {
-		unsigned long freed;
+	do {
+		for_each_pgdat(pgdat) {
+			unsigned long freed;
 
-		freed = balance_pgdat(pgdat, nr_to_free, 0);
-		ret += freed;
-		nr_to_free -= freed;
-		if (nr_to_free <= 0)
+			freed = balance_pgdat(pgdat, nr_to_free, 0);

mm/vmscan.c unsigned long shrink_all_memory(unsigned · line 1813

Quoted 10 lines
mm/vmscan.c +8 -1
+			ret += freed;
+			nr_to_free -= freed;
+			if (nr_to_free <= 0)
+				break;
+		}
+		if (ret > 0)
 			break;
-	}
+		if (retry)
+			msleep_interruptible(100);

TASK_INTERRUPTIBLE sleeps won't do anything if someone has sent this
process a signal. They should be used with caution.

Quoted 2 lines

Something like the below, I guess. But it's hard to fix something when you don't know what you're fixing.

Sorry again.

Quoted 2 lines

swsusp should call drop_pagecache() and then drop_slab() before trying to use shrink_all_memory(), btw.

Well, sometimes we don't need to free a lot of memory.

Quoted 26 lines

diff -puN mm/vmscan.c~mm-make-shrink_all_memory-try-harder mm/vmscan.c

mm/vmscan.c renamed from mm/vmscan.c~mm-make-shrink_all_memory-try-harder +3 -0
@@ -33,6 +33,7 @@
 #include <linux/cpuset.h>
 #include <linux/notifier.h>
 #include <linux/rwsem.h>
+#include <linux/delay.h>
 
 #include <asm/tlbflush.h>
 #include <asm/div64.h>
@@ -1783,11 +1784,13 @@ unsigned long shrink_all_memory(unsigned
 	pg_data_t *pgdat;
 	unsigned long nr_to_free = nr_pages;
 	unsigned long ret = 0;
+	unsigned retry = 2;
 	struct reclaim_state reclaim_state = {
 		.reclaimed_slab = 0,
 	};
 
 	current->reclaim_state = &reclaim_state;
+repeat:
 	for_each_pgdat(pgdat) {
 		unsigned long freed;
 

mm/vmscan.c unsigned long shrink_all_memory(unsigned · line 1806

Quoted 7 lines
mm/vmscan.c renamed from mm/vmscan.c~mm-make-shrink_all_memory-try-harder +4 -0
@@ -1797,6 +1800,10 @@ unsigned long shrink_all_memory(unsigned
 		if ((long)nr_to_free <= 0)
 			break;
 	}
+	if (retry-- && ret < nr_pages) {
+		blk_congestion_wait(WRITE, HZ/5);
+		goto repeat;
+	}

I'd like to do this only if ret is 0.

Quoted 7 lines
mm/vmscan.c renamed from mm/vmscan.c~mm-make-shrink_all_memory-try-harder +0 -0
 	current->reclaim_state = NULL;
 	return ret;
 }

_

But then, the above could be implemented by the caller, so I don't know what's going on..

If 0 is returned, the caller assumes there are no more freeable pages.

#11 lore raw

Re: [RFC][PATCH -mm 2/2] mm: make shrink_all_memory try harder

Quoted 8 lines · Rafael J. Wysocki
Quoted 6 lines · Andrew Morton
Quoted 4 lines · Rafael J. Wysocki

Make shrink_all_memory() repeat the attempts to free more memory if there seems to be no pages to free.

Quoted 5 lines
Quoted 2 lines

This description doesn't describe what the problem is, not how the patch fixes it. So I'm kinda left guessing.

I have described it in the 0/0 message, but I should have repeated that in the changelog, sorry.

Actually these [patch 0/n] emails are a nuisance - some poor schmuck just has to copy-n-paste that text into the fist patch's changelog anwyay.

Quoted 4 lines
Quoted 2 lines

swsusp should call drop_pagecache() and then drop_slab() before trying to use shrink_all_memory(), btw.

Well, sometimes we don't need to free a lot of memory.

OK. But if clean pagecache and reclaimable slabs are left in memory, they'll have to be written to swap, won't they?

It could well be more efficent to restore them from swap. Slower suspend, faster resume.

Quoted 6 lines

mm/vmscan.c unsigned long shrink_all_memory(unsigned · line 1806

Quoted 4 lines
mm/vmscan.c renamed from mm/vmscan.c~mm-make-shrink_all_memory-try-harder +4 -0
+	if (retry-- && ret < nr_pages) {
+		blk_congestion_wait(WRITE, HZ/5);
+		goto repeat;
+	}

I'd like to do this only if ret is 0.

Well I figured that this was a more general approach: we were _asked_ to free that many pages. If we haven't done that yet, keep trying. Can you test that code please?

#12 lore raw

Re: [RFC][PATCH -mm 2/2] mm: make shrink_all_memory try harder

Quoted 18 lines · Andrew Morton
Quoted 8 lines · Rafael J. Wysocki
Quoted 6 lines · Andrew Morton
Quoted 4 lines · Rafael J. Wysocki

Make shrink_all_memory() repeat the attempts to free more memory if there seems to be no pages to free.

Quoted 5 lines
Quoted 2 lines

This description doesn't describe what the problem is, not how the patch fixes it. So I'm kinda left guessing.

I have described it in the 0/0 message, but I should have repeated that in the changelog, sorry.

Actually these [patch 0/n] emails are a nuisance - some poor schmuck just has to copy-n-paste that text into the fist patch's changelog anwyay.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

swsusp should call drop_pagecache() and then drop_slab() before trying to use shrink_all_memory(), btw.

Well, sometimes we don't need to free a lot of memory.

OK. But if clean pagecache and reclaimable slabs are left in memory, they'll have to be written to swap, won't they?

Yes, but then we write them more or less linearly and we can also compress them. :-)

Quoted 10 lines

It could well be more efficent to restore them from swap. Slower suspend, faster resume.

Quoted 6 lines

mm/vmscan.c unsigned long shrink_all_memory(unsigned · line 1806

Quoted 4 lines
mm/vmscan.c renamed from mm/vmscan.c~mm-make-shrink_all_memory-try-harder +4 -0
+	if (retry-- && ret < nr_pages) {
+		blk_congestion_wait(WRITE, HZ/5);
+		goto repeat;
+	}

I'd like to do this only if ret is 0.

Quoted 3 lines

Well I figured that this was a more general approach: we were _asked_ to free that many pages. If we haven't done that yet, keep trying. Can you test that code please?

So far, it works just fine. Thanks.

[Tested on 2.6.16-rc4-mm2, because -rc5-mm1 crashes on my system in a spectacular way (already reported separately).]