mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: linux-kernel@vger.kernel.org, stable@kernel.org
Cc: stable-review@kernel.org, torvalds@linux-foundation.org,
	akpm@linux-foundation.org, alan@lxorguk.ukuu.org.uk,
	"Rafael J. Wysocki" <rjw@sisk.pl>
Subject: [27/35] PM / Hibernate: Avoid hitting OOM during preallocation of memory
Date: Fri, 08 Jul 2011 16:00:43 -0700	[thread overview]
Message-ID: <20110708230112.213623202@clark.kroah.org> (raw)
In-Reply-To: <20110708230122.GA18807@kroah.com>

2.6.33-longterm review patch.  If anyone has any objections, please let us know.

------------------

From: "Rafael J. Wysocki" <rjw@sisk.pl>

commit 6715045ddc7472a22be5e49d4047d2d89b391f45 upstream.

There is a problem in hibernate_preallocate_memory() that it calls
preallocate_image_memory() with an argument that may be greater than
the total number of available non-highmem memory pages.  If that's
the case, the OOM condition is guaranteed to trigger, which in turn
can cause significant slowdown to occur during hibernation.

To avoid that, make preallocate_image_memory() adjust its argument
before calling preallocate_image_pages(), so that the total number of
saveable non-highem pages left is not less than the minimum size of
a hibernation image.  Change hibernate_preallocate_memory() to try to
allocate from highmem if the number of pages allocated by
preallocate_image_memory() is too low.

Modify free_unnecessary_pages() to take all possible memory
allocation patterns into account.

Reported-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Tested-by: M. Vefa Bicakci <bicave@superonline.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 kernel/power/snapshot.c |   85 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 65 insertions(+), 20 deletions(-)

--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -1120,9 +1120,19 @@ static unsigned long preallocate_image_p
 	return nr_alloc;
 }
 
-static unsigned long preallocate_image_memory(unsigned long nr_pages)
+static unsigned long preallocate_image_memory(unsigned long nr_pages,
+					      unsigned long avail_normal)
 {
-	return preallocate_image_pages(nr_pages, GFP_IMAGE);
+	unsigned long alloc;
+
+	if (avail_normal <= alloc_normal)
+		return 0;
+
+	alloc = avail_normal - alloc_normal;
+	if (nr_pages < alloc)
+		alloc = nr_pages;
+
+	return preallocate_image_pages(alloc, GFP_IMAGE);
 }
 
 #ifdef CONFIG_HIGHMEM
@@ -1168,15 +1178,22 @@ static inline unsigned long preallocate_
  */
 static void free_unnecessary_pages(void)
 {
-	unsigned long save_highmem, to_free_normal, to_free_highmem;
+	unsigned long save, to_free_normal, to_free_highmem;
 
-	to_free_normal = alloc_normal - count_data_pages();
-	save_highmem = count_highmem_pages();
-	if (alloc_highmem > save_highmem) {
-		to_free_highmem = alloc_highmem - save_highmem;
+	save = count_data_pages();
+	if (alloc_normal >= save) {
+		to_free_normal = alloc_normal - save;
+		save = 0;
+	} else {
+		to_free_normal = 0;
+		save -= alloc_normal;
+	}
+	save += count_highmem_pages();
+	if (alloc_highmem >= save) {
+		to_free_highmem = alloc_highmem - save;
 	} else {
 		to_free_highmem = 0;
-		to_free_normal -= save_highmem - alloc_highmem;
+		to_free_normal -= save - alloc_highmem;
 	}
 
 	memory_bm_position_reset(&copy_bm);
@@ -1257,7 +1274,7 @@ int hibernate_preallocate_memory(void)
 {
 	struct zone *zone;
 	unsigned long saveable, size, max_size, count, highmem, pages = 0;
-	unsigned long alloc, save_highmem, pages_highmem;
+	unsigned long alloc, save_highmem, pages_highmem, avail_normal;
 	struct timeval start, stop;
 	int error;
 
@@ -1294,6 +1311,7 @@ int hibernate_preallocate_memory(void)
 		else
 			count += zone_page_state(zone, NR_FREE_PAGES);
 	}
+	avail_normal = count;
 	count += highmem;
 	count -= totalreserve_pages;
 
@@ -1308,12 +1326,21 @@ int hibernate_preallocate_memory(void)
 	 */
 	if (size >= saveable) {
 		pages = preallocate_image_highmem(save_highmem);
-		pages += preallocate_image_memory(saveable - pages);
+		pages += preallocate_image_memory(saveable - pages, avail_normal);
 		goto out;
 	}
 
 	/* Estimate the minimum size of the image. */
 	pages = minimum_image_size(saveable);
+	/*
+	 * To avoid excessive pressure on the normal zone, leave room in it to
+	 * accommodate an image of the minimum size (unless it's already too
+	 * small, in which case don't preallocate pages from it at all).
+	 */
+	if (avail_normal > pages)
+		avail_normal -= pages;
+	else
+		avail_normal = 0;
 	if (size < pages)
 		size = min_t(unsigned long, pages, max_size);
 
@@ -1334,16 +1361,34 @@ int hibernate_preallocate_memory(void)
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	alloc = (count - max_size) - pages_highmem;
-	pages = preallocate_image_memory(alloc);
-	if (pages < alloc)
-		goto err_out;
-	size = max_size - size;
-	alloc = size;
-	size = preallocate_highmem_fraction(size, highmem, count);
-	pages_highmem += size;
-	alloc -= size;
-	pages += preallocate_image_memory(alloc);
-	pages += pages_highmem;
+	pages = preallocate_image_memory(alloc, avail_normal);
+	if (pages < alloc) {
+		/* We have exhausted non-highmem pages, try highmem. */
+		alloc -= pages;
+		pages += pages_highmem;
+		pages_highmem = preallocate_image_highmem(alloc);
+		if (pages_highmem < alloc)
+			goto err_out;
+		pages += pages_highmem;
+		/*
+		 * size is the desired number of saveable pages to leave in
+		 * memory, so try to preallocate (all memory - size) pages.
+		 */
+		alloc = (count - pages) - size;
+		pages += preallocate_image_highmem(alloc);
+	} else {
+		/*
+		 * There are approximately max_size saveable pages at this point
+		 * and we want to reduce this number down to size.
+		 */
+		alloc = max_size - size;
+		size = preallocate_highmem_fraction(alloc, highmem, count);
+		pages_highmem += size;
+		alloc -= size;
+		size = preallocate_image_memory(alloc, avail_normal);
+		pages_highmem += preallocate_image_highmem(alloc - size);
+		pages += pages_highmem + size;
+	}
 
 	/*
 	 * We only need as many page frames for the image as there are saveable



  parent reply	other threads:[~2011-07-09  6:04 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-08 23:01 [00/35] 2.6.33.16-longterm review Greg KH
2011-07-08 23:00 ` [01/35] ksm: fix NULL pointer dereference in scan_get_next_rmap_item() Greg KH
2011-07-08 23:00 ` [02/35] migrate: dont account swapcache as shmem Greg KH
2011-07-08 23:00 ` [03/35] xen: partially revert "xen: set max_pfn_mapped to the last pfn mapped" Greg KH
2011-07-08 23:00 ` [04/35] clocksource: Make watchdog robust vs. interruption Greg KH
2011-07-08 23:00 ` [05/35] TTY: ldisc, do not close until there are readers Greg KH
2011-07-08 23:00 ` [06/35] xhci: Reject double add of active endpoints Greg KH
2011-07-08 23:00 ` [07/35] PM: Free memory bitmaps if opening /dev/snapshot fails Greg KH
2011-07-08 23:00 ` [08/35] ath5k: fix memory leak when fewer than N_PD_CURVES are in use Greg KH
2011-07-08 23:00 ` [09/35] ath9k: Fix suspend/resume when no interface is UP Greg KH
2011-07-08 23:00 ` [10/35] mm: fix negative commitlimit when gigantic hugepages are allocated Greg KH
2011-07-08 23:00 ` [11/35] [media] uvcvideo: Remove buffers from the queues when freeing Greg KH
2011-07-08 23:00 ` [12/35] watchdog: mtx1-wdt: request gpio before using it Greg KH
2011-07-08 23:00 ` [13/35] debugobjects: Fix boot crash when kmemleak and debugobjects enabled Greg KH
2011-07-08 23:00 ` [14/35] cfq-iosched: fix locking around ioc->ioc_data assignment Greg KH
2011-07-08 23:00 ` [15/35] cfq-iosched: fix a rcu warning Greg KH
2011-07-08 23:00 ` [16/35] i2c-taos-evm: Fix log messages Greg KH
2011-07-08 23:00 ` [17/35] md: avoid endless recovery loop when waiting for fail device to complete Greg KH
2011-07-08 23:00 ` [18/35] SUNRPC: Ensure the RPC client only quits on fatal signals Greg KH
2011-07-08 23:00 ` [19/35] 6pack,mkiss: fix lock inconsistency Greg KH
2011-07-08 23:00 ` [20/35] taskstats: dont allow duplicate entries in listener mode Greg KH
2011-07-08 23:00 ` [21/35] USB: dont let errors prevent system sleep Greg KH
2011-07-08 23:00 ` [22/35] USB: dont let the hub driver " Greg KH
2011-07-08 23:00 ` [23/35] uml: fix CONFIG_STATIC_LINK=y build failure with newer glibc Greg KH
2011-07-08 23:00 ` [24/35] um: os-linux/mem.c needs sys/stat.h Greg KH
2011-07-08 23:00 ` [25/35] netlink: Make nlmsg_find_attr take a const nlmsghdr* Greg KH
2011-07-08 23:00 ` [26/35] inet_diag: fix inet_diag_bc_audit() Greg KH
2011-07-08 23:00 ` Greg KH [this message]
2011-07-08 23:00 ` [28/35] PM / Hibernate: Fix free_unnecessary_pages() Greg KH
2011-07-08 23:00 ` [29/35] bug.h: Add WARN_RATELIMIT Greg KH
2011-07-08 23:00 ` [30/35] net: filter: Use WARN_RATELIMIT Greg KH
2011-07-08 23:00 ` [31/35] af_packet: prevent information leak Greg KH
2011-07-08 23:00 ` [32/35] net/ipv4: Check for mistakenly passed in non-IPv4 address Greg KH
2011-07-08 23:00 ` [33/35] ipv6/udp: Use the correct variable to determine non-blocking condition Greg KH
2011-07-08 23:00 ` [34/35] udp/recvmsg: Clear MSG_TRUNC flag when starting over for a new packet Greg KH
2011-07-08 23:00 ` [35/35] mm: prevent concurrent unmap_mapping_range() on the same inode Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2011-07-08 22:53 [00/35] 2.6.32.43-longterm review Greg KH
2011-07-08 22:50 ` [27/35] PM / Hibernate: Avoid hitting OOM during preallocation of memory Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110708230112.213623202@clark.kroah.org \
    --to=gregkh@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rjw@sisk.pl \
    --cc=stable-review@kernel.org \
    --cc=stable@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome