Skip to contentSkip to conversation

PM/Hibernate: Rework memory shrinking

RFC PATCH In review Rafael J. Wysocki 7 participants 146 messages May 6, 2009 – May 19, 2009 Page 2 of 2

Subsystems (8)
lore
Share & tools
lore
#110 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

In reply to Rafael J. Wysocki

Hi!

Quoted 8 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

[rev. 2: Take highmem into account correctly.]

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Pavel

Quoted 408 lines
---
 kernel/power/disk.c     |   15 ++-
 kernel/power/power.h    |    2 
 kernel/power/snapshot.c |  206 +++++++++++++++++++++++++++++++-----------------
 3 files changed, 149 insertions(+), 74 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +137 -69
@@ -1033,6 +1033,25 @@ copy_data_pages(struct memory_bitmap *co
 static unsigned int nr_copy_pages;
 /* Number of pages needed for saving the original pfns of the image pages */
 static unsigned int nr_meta_pages;
+/*
+ * Numbers of normal and highmem page frames allocated for hibernation image
+ * before suspending devices.
+ */
+unsigned int alloc_normal, alloc_highmem;
+/*
+ * Memory bitmap used for marking saveable pages (during hibernation) or
+ * hibernation image pages (during restore)
+ */
+static struct memory_bitmap orig_bm;
+/*
+ * Memory bitmap used during hibernation for marking allocated page frames that
+ * will contain copies of saveable pages.  During restore it is initially used
+ * for marking hibernation image pages, but then the set bits from it are
+ * duplicated in @orig_bm and it is released.  On highmem systems it is next
+ * used for marking "safe" highmem pages, but it has to be reinitialized for
+ * this purpose.
+ */
+static struct memory_bitmap copy_bm;
 
 /**
  *	swsusp_free - free pages allocated for the suspend.
@@ -1064,6 +1083,8 @@ void swsusp_free(void)
 	nr_meta_pages = 0;
 	restore_pblist = NULL;
 	buffer = NULL;
+	alloc_normal = 0;
+	alloc_highmem = 0;
 }
 
 /* Helper functions used for the shrinking of memory. */
@@ -1082,8 +1103,16 @@ static unsigned long preallocate_image_p
 	unsigned long nr_alloc = 0;
 
 	while (nr_pages > 0) {
-		if (!alloc_image_page(mask))
-			break;
+ 		struct page *page;
+
+		page = alloc_image_page(mask);
+ 		if (!page)
+ 			break;
+		memory_bm_set_bit(&copy_bm, page_to_pfn(page));
+		if (PageHighMem(page))
+			alloc_highmem++;
+		else
+			alloc_normal++;
 		nr_pages--;
 		nr_alloc++;
 	}
@@ -1144,7 +1173,47 @@ static inline unsigned long highmem_size
 #endif /* CONFIG_HIGHMEM */
 
 /**
- * swsusp_shrink_memory -  Make the kernel release as much memory as needed
+ * free_unnecessary_pages - Release preallocated pages not needed for the image
+ */
+static void free_unnecessary_pages(void)
+{
+	unsigned long save_highmem, 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;
+	} else {
+		to_free_highmem = 0;
+		to_free_normal -= save_highmem - alloc_highmem;
+	}
+
+	memory_bm_position_reset(&copy_bm);
+
+	while (to_free_normal > 0 && to_free_highmem > 0) {
+		unsigned long pfn = memory_bm_next_pfn(&copy_bm);
+		struct page *page = pfn_to_page(pfn);
+
+		if (PageHighMem(page)) {
+			if (!to_free_highmem)
+				continue;
+			to_free_highmem--;
+			alloc_highmem--;
+		} else {
+			if (!to_free_normal)
+				continue;
+			to_free_normal--;
+			alloc_normal--;
+		}
+		memory_bm_clear_bit(&copy_bm, pfn);
+		swsusp_unset_page_forbidden(page);
+		swsusp_unset_page_free(page);
+		__free_page(page);
+	}
+}
+
+/**
+ * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
  * frame in use.  We also need a number of page frames to be free during
@@ -1163,19 +1232,30 @@ static inline unsigned long highmem_size
  * pages in the system is below the requested image size or it is impossible to
  * allocate more memory, whichever happens first.
  */
-int swsusp_shrink_memory(void)
+int hibernate_preallocate_memory(void)
 {
 	struct zone *zone;
 	unsigned long saveable, size, max_size, count, highmem, pages = 0;
-	unsigned long alloc, pages_highmem;
+	unsigned long alloc, save_highmem, pages_highmem;
 	struct timeval start, stop;
-	int error = 0;
+	int error;
 
-	printk(KERN_INFO "PM: Shrinking memory... ");
+	printk(KERN_INFO "PM: Preallocating image memory... ");
 	do_gettimeofday(&start);
 
+	error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
+	if (error)
+		goto err_out;
+
+	error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
+	if (error)
+		goto err_out;
+
+	alloc_normal = 0;
+	alloc_highmem = 0;
+
 	/* Count the number of saveable data pages. */
-	highmem = count_highmem_pages();
+	save_highmem = count_highmem_pages();
 	saveable = count_data_pages();
 
 	/*
@@ -1183,7 +1263,8 @@ int swsusp_shrink_memory(void)
 	 * number of pages needed for image metadata (size).
 	 */
 	count = saveable;
-	saveable += highmem;
+	saveable += save_highmem;
+	highmem = save_highmem;
 	size = 0;
 	for_each_populated_zone(zone) {
 		size += snapshot_additional_pages(zone);
@@ -1202,10 +1283,13 @@ int swsusp_shrink_memory(void)
 		size = max_size;
 	/*
 	 * If the maximum is not less than the current number of saveable pages
-	 * in memory, we don't need to do anything more.
+	 * in memory, allocate page frames for the image and we're done.
 	 */
-	if (size >= saveable)
+	if (size >= saveable) {
+		pages = preallocate_image_highmem(save_highmem);
+		pages += preallocate_image_memory(saveable - pages);
 		goto out;
+	}
 
 	/*
 	 * Let the memory management subsystem know that we're going to need a
@@ -1226,10 +1310,8 @@ int swsusp_shrink_memory(void)
 	max_size += pages_highmem;
 	alloc = count - max_size;
 	pages = preallocate_image_memory(alloc);
-	if (pages < alloc) {
-		error = -ENOMEM;
-		goto free_out;
-	}
+	if (pages < alloc)
+		goto err_out;
 	size = max_size - size;
 	alloc = size;
 	size = preallocate_image_highmem(highmem_size(size, highmem, count));
@@ -1238,21 +1320,24 @@ int swsusp_shrink_memory(void)
 	pages += preallocate_image_memory(alloc);
 	pages += pages_highmem;
 
- free_out:
-	/* Release all of the preallocated page frames. */
-	swsusp_free();
-
-	if (error) {
-		printk(KERN_CONT "\n");
-		return error;
-	}
+	/*
+	 * We only need as many page frames for the image as there are saveable
+	 * pages in memory, but we have allocated more.  Release the excessive
+	 * ones now.
+	 */
+	free_unnecessary_pages();
 
  out:
 	do_gettimeofday(&stop);
-	printk(KERN_CONT "done (preallocated %lu free pages)\n", pages);
-	swsusp_show_speed(&start, &stop, pages, "Freed");
+	printk(KERN_CONT "done (allocated %lu pages)\n", pages);
+	swsusp_show_speed(&start, &stop, pages, "Allocated");
 
 	return 0;
+
+ err_out:
+	printk(KERN_CONT "\n");
+	swsusp_free();
+	return -ENOMEM;
 }
 
 #ifdef CONFIG_HIGHMEM
@@ -1263,7 +1348,7 @@ int swsusp_shrink_memory(void)
 
 static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
 {
-	unsigned int free_highmem = count_free_highmem_pages();
+	unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
 
 	if (free_highmem >= nr_highmem)
 		nr_highmem = 0;
@@ -1285,19 +1370,17 @@ count_pages_for_highmem(unsigned int nr_
 static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
 {
 	struct zone *zone;
-	unsigned int free = 0, meta = 0;
+	unsigned int free = alloc_normal;
 
-	for_each_zone(zone) {
-		meta += snapshot_additional_pages(zone);
+	for_each_zone(zone)
 		if (!is_highmem(zone))
 			free += zone_page_state(zone, NR_FREE_PAGES);
-	}
 
 	nr_pages += count_pages_for_highmem(nr_highmem);
-	pr_debug("PM: Normal pages needed: %u + %u + %u, available pages: %u\n",
-		nr_pages, PAGES_FOR_IO, meta, free);
+	pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
+		nr_pages, PAGES_FOR_IO, free);
 
-	return free > nr_pages + PAGES_FOR_IO + meta;
+	return free > nr_pages + PAGES_FOR_IO;
 }
 
 #ifdef CONFIG_HIGHMEM
@@ -1319,7 +1402,7 @@ static inline int get_highmem_buffer(int
  */
 
 static inline unsigned int
-alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
+alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
 {
 	unsigned int to_alloc = count_free_highmem_pages();
 
@@ -1339,7 +1422,7 @@ alloc_highmem_image_pages(struct memory_
 static inline int get_highmem_buffer(int safe_needed) { return 0; }
 
 static inline unsigned int
-alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
+alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
 #endif /* CONFIG_HIGHMEM */
 
 /**
@@ -1358,51 +1441,36 @@ static int
 swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
 		unsigned int nr_pages, unsigned int nr_highmem)
 {
-	int error;
-
-	error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
-	if (error)
-		goto Free;
-
-	error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
-	if (error)
-		goto Free;
+	int error = 0;
 
 	if (nr_highmem > 0) {
 		error = get_highmem_buffer(PG_ANY);
 		if (error)
-			goto Free;
-
-		nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
+			goto err_out;
+		if (nr_highmem > alloc_highmem) {
+			nr_highmem -= alloc_highmem;
+			nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
+		}
 	}
-	while (nr_pages-- > 0) {
-		struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
-
-		if (!page)
-			goto Free;
-
-		memory_bm_set_bit(copy_bm, page_to_pfn(page));
+	if (nr_pages > alloc_normal) {
+		nr_pages -= alloc_normal;
+		while (nr_pages-- > 0) {
+			struct page *page;
+
+			page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
+			if (!page)
+				goto err_out;
+			memory_bm_set_bit(copy_bm, page_to_pfn(page));
+		}
 	}
+
 	return 0;
 
- Free:
+ err_out:
 	swsusp_free();
-	return -ENOMEM;
+	return error;
 }
 
-/* Memory bitmap used for marking saveable pages (during suspend) or the
- * suspend image pages (during resume)
- */
-static struct memory_bitmap orig_bm;
-/* Memory bitmap used on suspend for marking allocated pages that will contain
- * the copies of saveable pages.  During resume it is initially used for
- * marking the suspend image pages, but then its set bits are duplicated in
- * @orig_bm and it is released.  Next, on systems with high memory, it may be
- * used for marking "safe" highmem pages, but it has to be reinitialized for
- * this purpose.
- */
-static struct memory_bitmap copy_bm;
-
 asmlinkage int swsusp_save(void)
 {
 	unsigned int nr_pages, nr_highmem;

Index: linux-2.6/kernel/power/power.h
===================================================================

kernel/power/power.h +1 -1
@@ -74,7 +74,7 @@ extern asmlinkage int swsusp_arch_resume
 
 extern int create_basic_memory_bitmaps(void);
 extern void free_basic_memory_bitmaps(void);
-extern int swsusp_shrink_memory(void);
+extern int hibernate_preallocate_memory(void);
 
 /**
  *	Auxiliary structure used for reading the snapshot image data and

Index: linux-2.6/kernel/power/disk.c
===================================================================

kernel/power/disk.c +11 -4
@@ -303,8 +303,8 @@ int hibernation_snapshot(int platform_mo
 	if (error)
 		return error;
 
-	/* Free memory before shutting down devices. */
-	error = swsusp_shrink_memory();
+	/* Preallocate image memory before shutting down devices. */
+	error = hibernate_preallocate_memory();
 	if (error)
 		goto Close;
 
@@ -320,6 +320,10 @@ int hibernation_snapshot(int platform_mo
 	/* Control returns here after successful restore */
 
  Resume_devices:
+	/* We may need to release the preallocated image pages here. */
+	if (error || !in_suspend)
+		swsusp_free();
+
 	device_resume(in_suspend ?
 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
 	resume_console();
@@ -593,7 +597,10 @@ int hibernate(void)
 		goto Thaw;
 
 	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
-	if (in_suspend && !error) {
+	if (error)
+		goto Thaw;
+
+	if (in_suspend) {
 		unsigned int flags = 0;
 
 		if (hibernation_mode == HIBERNATION_PLATFORM)
@@ -605,8 +612,8 @@ int hibernate(void)
 			power_down();
 	} else {
 		pr_debug("PM: Image restored successfully.\n");
-		swsusp_free();
 	}
+
  Thaw:
 	thaw_processes();
  Finish:
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#111 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

In reply to Rafael J. Wysocki

Hi!

Quoted 8 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

[rev. 2: Take highmem into account correctly.]

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Pavel

Quoted 408 lines
---
 kernel/power/disk.c     |   15 ++-
 kernel/power/power.h    |    2 
 kernel/power/snapshot.c |  206 +++++++++++++++++++++++++++++++-----------------
 3 files changed, 149 insertions(+), 74 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +137 -69
@@ -1033,6 +1033,25 @@ copy_data_pages(struct memory_bitmap *co
 static unsigned int nr_copy_pages;
 /* Number of pages needed for saving the original pfns of the image pages */
 static unsigned int nr_meta_pages;
+/*
+ * Numbers of normal and highmem page frames allocated for hibernation image
+ * before suspending devices.
+ */
+unsigned int alloc_normal, alloc_highmem;
+/*
+ * Memory bitmap used for marking saveable pages (during hibernation) or
+ * hibernation image pages (during restore)
+ */
+static struct memory_bitmap orig_bm;
+/*
+ * Memory bitmap used during hibernation for marking allocated page frames that
+ * will contain copies of saveable pages.  During restore it is initially used
+ * for marking hibernation image pages, but then the set bits from it are
+ * duplicated in @orig_bm and it is released.  On highmem systems it is next
+ * used for marking "safe" highmem pages, but it has to be reinitialized for
+ * this purpose.
+ */
+static struct memory_bitmap copy_bm;
 
 /**
  *	swsusp_free - free pages allocated for the suspend.
@@ -1064,6 +1083,8 @@ void swsusp_free(void)
 	nr_meta_pages = 0;
 	restore_pblist = NULL;
 	buffer = NULL;
+	alloc_normal = 0;
+	alloc_highmem = 0;
 }
 
 /* Helper functions used for the shrinking of memory. */
@@ -1082,8 +1103,16 @@ static unsigned long preallocate_image_p
 	unsigned long nr_alloc = 0;
 
 	while (nr_pages > 0) {
-		if (!alloc_image_page(mask))
-			break;
+ 		struct page *page;
+
+		page = alloc_image_page(mask);
+ 		if (!page)
+ 			break;
+		memory_bm_set_bit(&copy_bm, page_to_pfn(page));
+		if (PageHighMem(page))
+			alloc_highmem++;
+		else
+			alloc_normal++;
 		nr_pages--;
 		nr_alloc++;
 	}
@@ -1144,7 +1173,47 @@ static inline unsigned long highmem_size
 #endif /* CONFIG_HIGHMEM */
 
 /**
- * swsusp_shrink_memory -  Make the kernel release as much memory as needed
+ * free_unnecessary_pages - Release preallocated pages not needed for the image
+ */
+static void free_unnecessary_pages(void)
+{
+	unsigned long save_highmem, 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;
+	} else {
+		to_free_highmem = 0;
+		to_free_normal -= save_highmem - alloc_highmem;
+	}
+
+	memory_bm_position_reset(&copy_bm);
+
+	while (to_free_normal > 0 && to_free_highmem > 0) {
+		unsigned long pfn = memory_bm_next_pfn(&copy_bm);
+		struct page *page = pfn_to_page(pfn);
+
+		if (PageHighMem(page)) {
+			if (!to_free_highmem)
+				continue;
+			to_free_highmem--;
+			alloc_highmem--;
+		} else {
+			if (!to_free_normal)
+				continue;
+			to_free_normal--;
+			alloc_normal--;
+		}
+		memory_bm_clear_bit(&copy_bm, pfn);
+		swsusp_unset_page_forbidden(page);
+		swsusp_unset_page_free(page);
+		__free_page(page);
+	}
+}
+
+/**
+ * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
  * frame in use.  We also need a number of page frames to be free during
@@ -1163,19 +1232,30 @@ static inline unsigned long highmem_size
  * pages in the system is below the requested image size or it is impossible to
  * allocate more memory, whichever happens first.
  */
-int swsusp_shrink_memory(void)
+int hibernate_preallocate_memory(void)
 {
 	struct zone *zone;
 	unsigned long saveable, size, max_size, count, highmem, pages = 0;
-	unsigned long alloc, pages_highmem;
+	unsigned long alloc, save_highmem, pages_highmem;
 	struct timeval start, stop;
-	int error = 0;
+	int error;
 
-	printk(KERN_INFO "PM: Shrinking memory... ");
+	printk(KERN_INFO "PM: Preallocating image memory... ");
 	do_gettimeofday(&start);
 
+	error = memory_bm_create(&orig_bm, GFP_IMAGE, PG_ANY);
+	if (error)
+		goto err_out;
+
+	error = memory_bm_create(&copy_bm, GFP_IMAGE, PG_ANY);
+	if (error)
+		goto err_out;
+
+	alloc_normal = 0;
+	alloc_highmem = 0;
+
 	/* Count the number of saveable data pages. */
-	highmem = count_highmem_pages();
+	save_highmem = count_highmem_pages();
 	saveable = count_data_pages();
 
 	/*
@@ -1183,7 +1263,8 @@ int swsusp_shrink_memory(void)
 	 * number of pages needed for image metadata (size).
 	 */
 	count = saveable;
-	saveable += highmem;
+	saveable += save_highmem;
+	highmem = save_highmem;
 	size = 0;
 	for_each_populated_zone(zone) {
 		size += snapshot_additional_pages(zone);
@@ -1202,10 +1283,13 @@ int swsusp_shrink_memory(void)
 		size = max_size;
 	/*
 	 * If the maximum is not less than the current number of saveable pages
-	 * in memory, we don't need to do anything more.
+	 * in memory, allocate page frames for the image and we're done.
 	 */
-	if (size >= saveable)
+	if (size >= saveable) {
+		pages = preallocate_image_highmem(save_highmem);
+		pages += preallocate_image_memory(saveable - pages);
 		goto out;
+	}
 
 	/*
 	 * Let the memory management subsystem know that we're going to need a
@@ -1226,10 +1310,8 @@ int swsusp_shrink_memory(void)
 	max_size += pages_highmem;
 	alloc = count - max_size;
 	pages = preallocate_image_memory(alloc);
-	if (pages < alloc) {
-		error = -ENOMEM;
-		goto free_out;
-	}
+	if (pages < alloc)
+		goto err_out;
 	size = max_size - size;
 	alloc = size;
 	size = preallocate_image_highmem(highmem_size(size, highmem, count));
@@ -1238,21 +1320,24 @@ int swsusp_shrink_memory(void)
 	pages += preallocate_image_memory(alloc);
 	pages += pages_highmem;
 
- free_out:
-	/* Release all of the preallocated page frames. */
-	swsusp_free();
-
-	if (error) {
-		printk(KERN_CONT "\n");
-		return error;
-	}
+	/*
+	 * We only need as many page frames for the image as there are saveable
+	 * pages in memory, but we have allocated more.  Release the excessive
+	 * ones now.
+	 */
+	free_unnecessary_pages();
 
  out:
 	do_gettimeofday(&stop);
-	printk(KERN_CONT "done (preallocated %lu free pages)\n", pages);
-	swsusp_show_speed(&start, &stop, pages, "Freed");
+	printk(KERN_CONT "done (allocated %lu pages)\n", pages);
+	swsusp_show_speed(&start, &stop, pages, "Allocated");
 
 	return 0;
+
+ err_out:
+	printk(KERN_CONT "\n");
+	swsusp_free();
+	return -ENOMEM;
 }
 
 #ifdef CONFIG_HIGHMEM
@@ -1263,7 +1348,7 @@ int swsusp_shrink_memory(void)
 
 static unsigned int count_pages_for_highmem(unsigned int nr_highmem)
 {
-	unsigned int free_highmem = count_free_highmem_pages();
+	unsigned int free_highmem = count_free_highmem_pages() + alloc_highmem;
 
 	if (free_highmem >= nr_highmem)
 		nr_highmem = 0;
@@ -1285,19 +1370,17 @@ count_pages_for_highmem(unsigned int nr_
 static int enough_free_mem(unsigned int nr_pages, unsigned int nr_highmem)
 {
 	struct zone *zone;
-	unsigned int free = 0, meta = 0;
+	unsigned int free = alloc_normal;
 
-	for_each_zone(zone) {
-		meta += snapshot_additional_pages(zone);
+	for_each_zone(zone)
 		if (!is_highmem(zone))
 			free += zone_page_state(zone, NR_FREE_PAGES);
-	}
 
 	nr_pages += count_pages_for_highmem(nr_highmem);
-	pr_debug("PM: Normal pages needed: %u + %u + %u, available pages: %u\n",
-		nr_pages, PAGES_FOR_IO, meta, free);
+	pr_debug("PM: Normal pages needed: %u + %u, available pages: %u\n",
+		nr_pages, PAGES_FOR_IO, free);
 
-	return free > nr_pages + PAGES_FOR_IO + meta;
+	return free > nr_pages + PAGES_FOR_IO;
 }
 
 #ifdef CONFIG_HIGHMEM
@@ -1319,7 +1402,7 @@ static inline int get_highmem_buffer(int
  */
 
 static inline unsigned int
-alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
+alloc_highmem_pages(struct memory_bitmap *bm, unsigned int nr_highmem)
 {
 	unsigned int to_alloc = count_free_highmem_pages();
 
@@ -1339,7 +1422,7 @@ alloc_highmem_image_pages(struct memory_
 static inline int get_highmem_buffer(int safe_needed) { return 0; }
 
 static inline unsigned int
-alloc_highmem_image_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
+alloc_highmem_pages(struct memory_bitmap *bm, unsigned int n) { return 0; }
 #endif /* CONFIG_HIGHMEM */
 
 /**
@@ -1358,51 +1441,36 @@ static int
 swsusp_alloc(struct memory_bitmap *orig_bm, struct memory_bitmap *copy_bm,
 		unsigned int nr_pages, unsigned int nr_highmem)
 {
-	int error;
-
-	error = memory_bm_create(orig_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
-	if (error)
-		goto Free;
-
-	error = memory_bm_create(copy_bm, GFP_ATOMIC | __GFP_COLD, PG_ANY);
-	if (error)
-		goto Free;
+	int error = 0;
 
 	if (nr_highmem > 0) {
 		error = get_highmem_buffer(PG_ANY);
 		if (error)
-			goto Free;
-
-		nr_pages += alloc_highmem_image_pages(copy_bm, nr_highmem);
+			goto err_out;
+		if (nr_highmem > alloc_highmem) {
+			nr_highmem -= alloc_highmem;
+			nr_pages += alloc_highmem_pages(copy_bm, nr_highmem);
+		}
 	}
-	while (nr_pages-- > 0) {
-		struct page *page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
-
-		if (!page)
-			goto Free;
-
-		memory_bm_set_bit(copy_bm, page_to_pfn(page));
+	if (nr_pages > alloc_normal) {
+		nr_pages -= alloc_normal;
+		while (nr_pages-- > 0) {
+			struct page *page;
+
+			page = alloc_image_page(GFP_ATOMIC | __GFP_COLD);
+			if (!page)
+				goto err_out;
+			memory_bm_set_bit(copy_bm, page_to_pfn(page));
+		}
 	}
+
 	return 0;
 
- Free:
+ err_out:
 	swsusp_free();
-	return -ENOMEM;
+	return error;
 }
 
-/* Memory bitmap used for marking saveable pages (during suspend) or the
- * suspend image pages (during resume)
- */
-static struct memory_bitmap orig_bm;
-/* Memory bitmap used on suspend for marking allocated pages that will contain
- * the copies of saveable pages.  During resume it is initially used for
- * marking the suspend image pages, but then its set bits are duplicated in
- * @orig_bm and it is released.  Next, on systems with high memory, it may be
- * used for marking "safe" highmem pages, but it has to be reinitialized for
- * this purpose.
- */
-static struct memory_bitmap copy_bm;
-
 asmlinkage int swsusp_save(void)
 {
 	unsigned int nr_pages, nr_highmem;

Index: linux-2.6/kernel/power/power.h
===================================================================

kernel/power/power.h +1 -1
@@ -74,7 +74,7 @@ extern asmlinkage int swsusp_arch_resume
 
 extern int create_basic_memory_bitmaps(void);
 extern void free_basic_memory_bitmaps(void);
-extern int swsusp_shrink_memory(void);
+extern int hibernate_preallocate_memory(void);
 
 /**
  *	Auxiliary structure used for reading the snapshot image data and

Index: linux-2.6/kernel/power/disk.c
===================================================================

kernel/power/disk.c +11 -4
@@ -303,8 +303,8 @@ int hibernation_snapshot(int platform_mo
 	if (error)
 		return error;
 
-	/* Free memory before shutting down devices. */
-	error = swsusp_shrink_memory();
+	/* Preallocate image memory before shutting down devices. */
+	error = hibernate_preallocate_memory();
 	if (error)
 		goto Close;
 
@@ -320,6 +320,10 @@ int hibernation_snapshot(int platform_mo
 	/* Control returns here after successful restore */
 
  Resume_devices:
+	/* We may need to release the preallocated image pages here. */
+	if (error || !in_suspend)
+		swsusp_free();
+
 	device_resume(in_suspend ?
 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
 	resume_console();
@@ -593,7 +597,10 @@ int hibernate(void)
 		goto Thaw;
 
 	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
-	if (in_suspend && !error) {
+	if (error)
+		goto Thaw;
+
+	if (in_suspend) {
 		unsigned int flags = 0;
 
 		if (hibernation_mode == HIBERNATION_PLATFORM)
@@ -605,8 +612,8 @@ int hibernate(void)
 			power_down();
 	} else {
 		pr_debug("PM: Image restored successfully.\n");
-		swsusp_free();
 	}
+
  Thaw:
 	thaw_processes();
  Finish:
Signature · 8 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#112 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

Quoted 9 lines · Pavel Machek

Hi!

Quoted 7 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

Quoted 4 lines
Quoted 1 line

[rev. 2: Take highmem into account correctly.]

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Yes, in some test cases it is reported to be faster (along with [4/6], actually).

Besides, we'd like to get rid of shrink_all_memory() eventually and it is a step in this direction.

Thanks,
Rafael

#113 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

In reply to Pavel Machek
Quoted 9 lines · Pavel Machek

Hi!

Quoted 7 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

Quoted 4 lines
Quoted 1 line

[rev. 2: Take highmem into account correctly.]

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Yes, in some test cases it is reported to be faster (along with [4/6], actually).

Besides, we'd like to get rid of shrink_all_memory() eventually and it is a step in this direction.

Thanks,
Rafael

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#114 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

Quoted 12 lines · Rafael J. Wysocki
Quoted 11 lines · Pavel Machek

Hi!

Quoted 8 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

[rev. 2: Take highmem into account correctly.]

Quoted 8 lines
Quoted 2 lines

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Yes, in some test cases it is reported to be faster (along with [4/6], actually).

Besides, we'd like to get rid of shrink_all_memory() eventually and it is a step in this direction.

Ok, but maybe we should wait with applying this until we have patches that actually get us rid of shrink_all_memory? Maybe it will not be feasible for speed reasons after all, or something...
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#115 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

In reply to Rafael J. Wysocki
Quoted 12 lines · Rafael J. Wysocki
Quoted 11 lines · Pavel Machek

Hi!

Quoted 8 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

[rev. 2: Take highmem into account correctly.]

Quoted 8 lines
Quoted 2 lines

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Yes, in some test cases it is reported to be faster (along with [4/6], actually).

Besides, we'd like to get rid of shrink_all_memory() eventually and it is a step in this direction.

Ok, but maybe we should wait with applying this until we have patches that actually get us rid of shrink_all_memory? Maybe it will not be feasible for speed reasons after all, or something...
Pavel

Signature · 8 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#116 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

Quoted 16 lines · Pavel Machek
Quoted 15 lines · Rafael J. Wysocki
Quoted 13 lines · Pavel Machek

Hi!

Quoted 8 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

[rev. 2: Take highmem into account correctly.]

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Quoted 8 lines
Quoted 5 lines

Yes, in some test cases it is reported to be faster (along with [4/6], actually).

Besides, we'd like to get rid of shrink_all_memory() eventually and it is a step in this direction.

Ok, but maybe we should wait with applying this until we have patches that actually get us rid of shrink_all_memory?

Well, the $subject patch is only an optimization of top of [4/6] that you've just acked. ;-)

In fact [4/6] changes the approach to the memory shrinking and the $subject one is only to avoid freeing all of the memory we've allocated and allocating it once again later.

Quoted 1 line

Maybe it will not be feasible for speed reasons after all, or something...

At least it allows us to drop shrink_all_memory() easily for the sake of experimentation (it's sufficient to comment out just one line of code for this purpose).

Besides, after this patchset shrink_all_memory() is _only_ needed for performance, so it should be possible to get rid of it relatively quckly.

Thanks,
Rafael

#117 lore raw

Re: [PATCH 5/6] PM/Hibernate: Do not release preallocated memory unnecessarily (rev. 2)

In reply to Pavel Machek
Quoted 16 lines · Pavel Machek
Quoted 15 lines · Rafael J. Wysocki
Quoted 13 lines · Pavel Machek

Hi!

Quoted 8 lines

Since the hibernation code is now going to use allocations of memory to make enough room for the image, it can also use the page frames allocated at this stage as image page frames. The low-level hibernation code needs to be rearranged for this purpose, but it allows us to avoid freeing a great number of pages and allocating these same pages once again later, so it generally is worth doing.

[rev. 2: Take highmem into account correctly.]

I don't get it. What is advantage of this patch? It makes the code more complex... Is it supposed to be faster?

Quoted 8 lines
Quoted 5 lines

Yes, in some test cases it is reported to be faster (along with [4/6], actually).

Besides, we'd like to get rid of shrink_all_memory() eventually and it is a step in this direction.

Ok, but maybe we should wait with applying this until we have patches that actually get us rid of shrink_all_memory?

Well, the $subject patch is only an optimization of top of [4/6] that you've just acked. ;-)

In fact [4/6] changes the approach to the memory shrinking and the $subject one is only to avoid freeing all of the memory we've allocated and allocating it once again later.

Quoted 1 line

Maybe it will not be feasible for speed reasons after all, or something...

At least it allows us to drop shrink_all_memory() easily for the sake of experimentation (it's sufficient to comment out just one line of code for this purpose).

Besides, after this patchset shrink_all_memory() is _only_ needed for performance, so it should be possible to get rid of it relatively quckly.

Thanks,
Rafael

#118 lore raw

RFC PATCH 6/6 PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
kernel/power/snapshot.c +52 -4
Index: linux-2.6/kernel/power/snapshot.c
===================================================================
@@ -1213,6 +1213,49 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * The minimum size of the image is computed as
+ *
+ * ([number of saveable pages] - [number of pages we can free]) * 1.25
+ *
+ * where the second term is the sum of reclaimable slab, anonymouns pages and
+ * active/inactive file pages.
+ *
+ * NOTE: It usually turns out that we can't really free all pages reported as
+ * reclaimable slab, so the number resulting from the subtraction alone is too
+ * low.  Still, it seems reasonable to assume that this number is proportional
+ * to the total number of pages that cannot be freed, which leads to the
+ * formula above.  The coefficient of proportinality in this formula, 1.25, has
+ * been determined experimentally.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);
+
+	if (saveable <= size)
+		return saveable;
+
+	size = saveable - size;
+	size += (size >> 2);
+
+	return size;
+}
+
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1229,8 +1272,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1291,6 +1334,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1303,8 +1351,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
#119 lore raw

RFC PATCH 6/6 PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
kernel/power/snapshot.c +52 -4
Index: linux-2.6/kernel/power/snapshot.c
===================================================================
@@ -1213,6 +1213,49 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * The minimum size of the image is computed as
+ *
+ * ([number of saveable pages] - [number of pages we can free]) * 1.25
+ *
+ * where the second term is the sum of reclaimable slab, anonymouns pages and
+ * active/inactive file pages.
+ *
+ * NOTE: It usually turns out that we can't really free all pages reported as
+ * reclaimable slab, so the number resulting from the subtraction alone is too
+ * low.  Still, it seems reasonable to assume that this number is proportional
+ * to the total number of pages that cannot be freed, which leads to the
+ * formula above.  The coefficient of proportinality in this formula, 1.25, has
+ * been determined experimentally.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);
+
+	if (saveable <= size)
+		return saveable;
+
+	size = saveable - size;
+	size += (size >> 2);
+
+	return size;
+}
+
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1229,8 +1272,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1291,6 +1334,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1303,8 +1351,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
#120 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Hi!

Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 24 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

Pavel

Signature · 8 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#121 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 47 lines · Pavel Machek

Hi!

Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 24 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

Quoted 2 lines

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

The "as much memory as we can" is not well defined.

Patches [4/6] and [5/6] make hibernation use memory allocations to force some memory to be freed. However, it is not really reasonable to try to allocate until the allocation fails, because that stresses the memory management subsystem too much. It is better to predict when it fails and stop allocating at that point, which is what the patch does.

The prediction is not very precise, but I think it need not be. Even if it leaves a few pages more in memory, that won't be a disaster.

Thanks,
Rafael

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#122 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Hi!

Quoted 28 lines
Quoted 28 lines
Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 15 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

...

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

Quoted 4 lines
Quoted 2 lines

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

The "as much memory as we can" is not well defined.

Well, while (1) kmalloc(1024, GFP_KERNEL | GFP_NO_OOMKILL); is basically "as much memory as we can". I believe it is pretty well defined.

Quoted 5 lines

Patches [4/6] and [5/6] make hibernation use memory allocations to force some memory to be freed. However, it is not really reasonable to try to allocate until the allocation fails, because that stresses the memory management subsystem too much. It is better to predict when it fails and stop allocating at that point, which is what the patch does.

Why is it wrong to stress memory management? It is a computer; it can handle it. Does it take too long? Should the user just set image_size higher in such case?

Quoted 2 lines

The prediction is not very precise, but I think it need not be. Even if it leaves a few pages more in memory, that won't be a disaster.

Well, on 128MB machine, you'll fail suspend even if it would fit if code tried little harder...?
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#123 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki

Hi!

Quoted 28 lines
Quoted 28 lines
Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 15 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

...

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

Quoted 4 lines
Quoted 2 lines

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

The "as much memory as we can" is not well defined.

Well, while (1) kmalloc(1024, GFP_KERNEL | GFP_NO_OOMKILL); is basically "as much memory as we can". I believe it is pretty well defined.

Quoted 5 lines

Patches [4/6] and [5/6] make hibernation use memory allocations to force some memory to be freed. However, it is not really reasonable to try to allocate until the allocation fails, because that stresses the memory management subsystem too much. It is better to predict when it fails and stop allocating at that point, which is what the patch does.

Why is it wrong to stress memory management? It is a computer; it can handle it. Does it take too long? Should the user just set image_size higher in such case?

Quoted 2 lines

The prediction is not very precise, but I think it need not be. Even if it leaves a few pages more in memory, that won't be a disaster.

Well, on 128MB machine, you'll fail suspend even if it would fit if code tried little harder...?
Pavel

Signature · 8 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#124 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 47 lines · Pavel Machek

Hi!

Quoted 28 lines
Quoted 28 lines
Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 15 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

...

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

Quoted 4 lines
Quoted 2 lines

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

The "as much memory as we can" is not well defined.

Well, while (1) kmalloc(1024, GFP_KERNEL | GFP_NO_OOMKILL); is basically "as much memory as we can". I believe it is pretty well defined.

Quoted 8 lines
Quoted 5 lines

Patches [4/6] and [5/6] make hibernation use memory allocations to force some memory to be freed. However, it is not really reasonable to try to allocate until the allocation fails, because that stresses the memory management subsystem too much. It is better to predict when it fails and stop allocating at that point, which is what the patch does.

Why is it wrong to stress memory management? It is a computer; it can handle it. Does it take too long?

Yes.

Quoted 1 line

Should the user just set image_size higher in such case?

Yes, he should.

Quoted 5 lines
Quoted 2 lines

The prediction is not very precise, but I think it need not be. Even if it leaves a few pages more in memory, that won't be a disaster.

Well, on 128MB machine, you'll fail suspend even if it would fit if code tried little harder...?

No. Did you notice the min_t(unsigned long, pages, max_size) in the patch? It's there exactly for this purpose (although I don't think it's really going to trigger in practice). :-)

Thanks,
Rafael

#125 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Pavel Machek
Quoted 47 lines · Pavel Machek

Hi!

Quoted 28 lines
Quoted 28 lines
Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 15 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

...

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

Quoted 4 lines
Quoted 2 lines

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

The "as much memory as we can" is not well defined.

Well, while (1) kmalloc(1024, GFP_KERNEL | GFP_NO_OOMKILL); is basically "as much memory as we can". I believe it is pretty well defined.

Quoted 8 lines
Quoted 5 lines

Patches [4/6] and [5/6] make hibernation use memory allocations to force some memory to be freed. However, it is not really reasonable to try to allocate until the allocation fails, because that stresses the memory management subsystem too much. It is better to predict when it fails and stop allocating at that point, which is what the patch does.

Why is it wrong to stress memory management? It is a computer; it can handle it. Does it take too long?

Yes.

Quoted 1 line

Should the user just set image_size higher in such case?

Yes, he should.

Quoted 5 lines
Quoted 2 lines

The prediction is not very precise, but I think it need not be. Even if it leaves a few pages more in memory, that won't be a disaster.

Well, on 128MB machine, you'll fail suspend even if it would fit if code tried little harder...?

No. Did you notice the min_t(unsigned long, pages, max_size) in the patch? It's there exactly for this purpose (although I don't think it's really going to trigger in practice). :-)

Thanks,
Rafael

#126 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Pavel Machek
Quoted 47 lines · Pavel Machek

Hi!

Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 24 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

Quoted 2 lines

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

The "as much memory as we can" is not well defined.

Patches [4/6] and [5/6] make hibernation use memory allocations to force some memory to be freed. However, it is not really reasonable to try to allocate until the allocation fails, because that stresses the memory management subsystem too much. It is better to predict when it fails and stop allocating at that point, which is what the patch does.

The prediction is not very precise, but I think it need not be. Even if it leaves a few pages more in memory, that won't be a disaster.

Thanks,
Rafael

#127 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki

Hi!

Quoted 3 lines

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Why? Is freeing memory too slow?

It used to be that user controlled image size, so he was able to balance "time to save image" vs. "responsiveness of system after resume".

Does this just override user's preference when he chooses too small image size?

Quoted 24 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)
Quoted 7 lines

/**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.

I don't get it. If user sets image size as 0, we should free as much memory as we can. I just don't see why "we want to avoid... it".

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#128 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki

Hi Rafael,

Sorry for being late.

Quoted 6 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 8 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

It's a very good idea to count the numbers in a reverse way.

Quoted 6 lines

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

However, the "*1.25" looks like a hack. We should really apply more constraints to the individual components.

Quoted 34 lines
---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +21 -0
@@ -1213,6 +1213,49 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * The minimum size of the image is computed as
+ *
+ * ([number of saveable pages] - [number of pages we can free]) * 1.25
+ *
+ * where the second term is the sum of reclaimable slab, anonymouns pages and
+ * active/inactive file pages.
+ *
+ * NOTE: It usually turns out that we can't really free all pages reported as
+ * reclaimable slab, so the number resulting from the subtraction alone is too
+ * low.  Still, it seems reasonable to assume that this number is proportional
+ * to the total number of pages that cannot be freed, which leads to the
+ * formula above.  The coefficient of proportinality in this formula, 1.25, has
+ * been determined experimentally.
+ */

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Thanks,
Fengguang

Quoted 48 lines
kernel/power/snapshot.c +20 -4
+	if (saveable <= size)
+		return saveable;
+
+	size = saveable - size;
+	size += (size >> 2);
+
+	return size;
+}
+
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1229,8 +1272,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1291,6 +1334,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1303,8 +1351,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
#129 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki

Hi Rafael,

Sorry for being late.

Quoted 6 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 8 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

It's a very good idea to count the numbers in a reverse way.

Quoted 6 lines

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

However, the "*1.25" looks like a hack. We should really apply more constraints to the individual components.

Quoted 34 lines
---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +21 -0
@@ -1213,6 +1213,49 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * The minimum size of the image is computed as
+ *
+ * ([number of saveable pages] - [number of pages we can free]) * 1.25
+ *
+ * where the second term is the sum of reclaimable slab, anonymouns pages and
+ * active/inactive file pages.
+ *
+ * NOTE: It usually turns out that we can't really free all pages reported as
+ * reclaimable slab, so the number resulting from the subtraction alone is too
+ * low.  Still, it seems reasonable to assume that this number is proportional
+ * to the total number of pages that cannot be freed, which leads to the
+ * formula above.  The coefficient of proportinality in this formula, 1.25, has
+ * been determined experimentally.
+ */

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Thanks,
Fengguang

Quoted 38 lines
kernel/power/snapshot.c +18 -2
+	if (saveable <= size)
+		return saveable;
+
+	size = saveable - size;
+	size += (size >> 2);
+
+	return size;
+}
+
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1229,8 +1272,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1291,6 +1334,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.

kernel/power/snapshot.c int hibernate_preallocate_memory(void) · line 1358

Quoted 10 lines
kernel/power/snapshot.c +2 -2
@@ -1303,8 +1351,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#130 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 1 line · Wu Fengguang

Hi Rafael,

Hi,

Quoted 1 line

Sorry for being late.

No big deal.

Quoted 18 lines · Rafael J. Wysocki
Quoted 6 lines

From: Rafael J. Wysocki <[email protected]>

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 8 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

It's a very good idea to count the numbers in a reverse way.

Quoted 8 lines
Quoted 6 lines

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

However, the "*1.25" looks like a hack.

It's just an experimental value.

Quoted 50 lines

We should really apply more constraints to the individual components.

Quoted 34 lines
---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +21 -0
@@ -1213,6 +1213,49 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * The minimum size of the image is computed as
+ *
+ * ([number of saveable pages] - [number of pages we can free]) * 1.25
+ *
+ * where the second term is the sum of reclaimable slab, anonymouns pages and
+ * active/inactive file pages.
+ *
+ * NOTE: It usually turns out that we can't really free all pages reported as
+ * reclaimable slab, so the number resulting from the subtraction alone is too
+ * low.  Still, it seems reasonable to assume that this number is proportional
+ * to the total number of pages that cannot be freed, which leads to the
+ * formula above.  The coefficient of proportinality in this formula, 1.25, has
+ * been determined experimentally.
+ */

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Thanks,
Rafael

#131 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Wu Fengguang
Quoted 1 line · Wu Fengguang

Hi Rafael,

Hi,

Quoted 1 line

Sorry for being late.

No big deal.

Quoted 18 lines · Rafael J. Wysocki
Quoted 6 lines

From: Rafael J. Wysocki <[email protected]>

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 8 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

It's a very good idea to count the numbers in a reverse way.

Quoted 8 lines
Quoted 6 lines

and even that is usually impossible to free in practice, because some of the pages reported as global_page_state(NR_SLAB_RECLAIMABLE) can't in fact be freed. It turns out, however, that if the sum of the above numbers is subtracted from the number of saveable pages in the system and the result is multiplied by 1.25, we get a suitable estimate of the minimum size of the image.

However, the "*1.25" looks like a hack.

It's just an experimental value.

Quoted 50 lines

We should really apply more constraints to the individual components.

Quoted 34 lines
---
 kernel/power/snapshot.c |   56 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +21 -0
@@ -1213,6 +1213,49 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: The total number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * The minimum size of the image is computed as
+ *
+ * ([number of saveable pages] - [number of pages we can free]) * 1.25
+ *
+ * where the second term is the sum of reclaimable slab, anonymouns pages and
+ * active/inactive file pages.
+ *
+ * NOTE: It usually turns out that we can't really free all pages reported as
+ * reclaimable slab, so the number resulting from the subtraction alone is too
+ * low.  Still, it seems reasonable to assume that this number is proportional
+ * to the total number of pages that cannot be freed, which leads to the
+ * formula above.  The coefficient of proportinality in this formula, 1.25, has
+ * been determined experimentally.
+ */

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Thanks,
Rafael

#132 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 9 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

Thanks,
Fengguang

#133 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 9 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

Thanks,
Fengguang

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#134 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 20 lines · Wu Fengguang
Quoted 19 lines · Rafael J. Wysocki
Quoted 16 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;
Quoted 7 lines
Quoted 1 line

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much (unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.

That doesn't really matter, we're talking about the minimum image size.

Quoted 22 lines

- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 5 lines
Quoted 5 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

Unfortunately your formula above doesn't also work after running shrink_all_memory(<all saveable pages>), because the number given by it is still too high in that case. The resulting minimum image size is then too low.

OTOH, the number computed in accordance with my original 1.25 * (<sum>) formula is fine in all cases I have checked (it actuall would be sufficient to take 1.2 * <sum>, but the difference is not really significant).

I don't think we can derive everything directly from the statistics collected by the mm subsystem.

Thanks,
Rafael

#135 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Wu Fengguang
Quoted 20 lines · Wu Fengguang
Quoted 19 lines · Rafael J. Wysocki
Quoted 16 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;
Quoted 7 lines
Quoted 1 line

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much (unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.

That doesn't really matter, we're talking about the minimum image size.

Quoted 22 lines

- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 5 lines
Quoted 5 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

Unfortunately your formula above doesn't also work after running shrink_all_memory(<all saveable pages>), because the number given by it is still too high in that case. The resulting minimum image size is then too low.

OTOH, the number computed in accordance with my original 1.25 * (<sum>) formula is fine in all cases I have checked (it actuall would be sufficient to take 1.2 * <sum>, but the difference is not really significant).

I don't think we can derive everything directly from the statistics collected by the mm subsystem.

Thanks,
Rafael

#136 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 23 lines · Rafael J. Wysocki
Quoted 22 lines · Wu Fengguang
Quoted 20 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Quoted 7 lines
Quoted 5 lines

Also a very coarse value:
- we don't want to stress icache/dcache too much (unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.

That doesn't really matter, we're talking about the minimum image size.

Have you dropped to goal to keep the minimal working set?

OK, even we only care about the success of page allocation, NR_SLAB_RECLAIMABLE is not really all reclaimable, not even close. In my desktop, 34MB pages are actually unreclaimable:

echo 2 > /proc/sys/vm/drop_caches

Slab: 69864 kB
SReclaimable: 34052 kB

But on the other hand, updatedb can make it really huge:

        Slab:            1700852 kB
        SReclaimable:    1664456 kB
Quoted 27 lines
Quoted 27 lines

- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 9 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Quoted 7 lines
Quoted 3 lines

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

Unfortunately your formula above doesn't also work after running shrink_all_memory(<all saveable pages>), because the number given by it is still too high in that case. The resulting minimum image size is then too low.

That means more items should be preserved, hehe.

Quoted 6 lines

OTOH, the number computed in accordance with my original 1.25 * (<sum>) formula is fine in all cases I have checked (it actuall would be sufficient to take 1.2 * <sum>, but the difference is not really significant).

I don't think we can derive everything directly from the statistics collected by the mm subsystem.

I agree that the numbers only reflects a coarse outline of the lru cache contents and can grow large in abnormal situations. We shall not be too dependent on them.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#137 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki
Quoted 23 lines · Rafael J. Wysocki
Quoted 22 lines · Wu Fengguang
Quoted 20 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void) · line 1246

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Quoted 7 lines
Quoted 5 lines

Also a very coarse value:
- we don't want to stress icache/dcache too much (unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.

That doesn't really matter, we're talking about the minimum image size.

Have you dropped to goal to keep the minimal working set?

OK, even we only care about the success of page allocation, NR_SLAB_RECLAIMABLE is not really all reclaimable, not even close. In my desktop, 34MB pages are actually unreclaimable:

echo 2 > /proc/sys/vm/drop_caches

Slab: 69864 kB
SReclaimable: 34052 kB

But on the other hand, updatedb can make it really huge:

        Slab:            1700852 kB
        SReclaimable:    1664456 kB
Quoted 27 lines
Quoted 27 lines

- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 9 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Quoted 7 lines
Quoted 3 lines

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

Unfortunately your formula above doesn't also work after running shrink_all_memory(<all saveable pages>), because the number given by it is still too high in that case. The resulting minimum image size is then too low.

That means more items should be preserved, hehe.

Quoted 6 lines

OTOH, the number computed in accordance with my original 1.25 * (<sum>) formula is fine in all cases I have checked (it actuall would be sufficient to take 1.2 * <sum>, but the difference is not really significant).

I don't think we can derive everything directly from the statistics collected by the mm subsystem.

I agree that the numbers only reflects a coarse outline of the lru cache contents and can grow large in abnormal situations. We shall not be too dependent on them.

#138 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Wu Fengguang
Quoted 49 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

Review

Quoted 10 lines
(quoted diff) +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 5 lines
Quoted 5 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)
kernel/power/snapshot.c +39 -4
Index: linux-2.6/kernel/power/snapshot.c
===================================================================
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
#139 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Wu Fengguang
Quoted 49 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

Review

Quoted 10 lines
(quoted diff) +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 5 lines
Quoted 5 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

That restores the hard core working set logic in the reverse way ;)

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)
kernel/power/snapshot.c +39 -4
Index: linux-2.6/kernel/power/snapshot.c
===================================================================
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
#140 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 52 lines · Rafael J. Wysocki
Quoted 51 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void)

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 7 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

This is illogical: in previous email you complained the formula

TOTAL - MAPPED - ACTIVE_ANON - SLAB/2

gives too high number, while

TOTAL - MAPPED

in this patch is OK. (I'm not claiming the first formula to be fine.)

Quoted 2 lines

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients

It is _OK_ for the minimum image size to be higher, that margin serves as a safety margin as well as the working set size we want to preserve.

Quoted 3 lines

involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

That's OK. Because shrink_all_memory() shrinks memory in a prioritized list-after-list order.

Quoted 7 lines

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

I'd like to advocate to add "working set preservation" as another goal of this function, and I can even do with the formula in this patch :-)

That means, when one day more accurate working set estimation is possible, we can extend this function to support that goal.

Thanks,
Fengguang

Quoted 87 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +37 -2
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.

kernel/power/snapshot.c int hibernate_preallocate_memory(void) · line 1336

Quoted 10 lines
kernel/power/snapshot.c +2 -2
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#141 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 64 lines · Wu Fengguang
Quoted 52 lines · Rafael J. Wysocki
Quoted 51 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void)

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 7 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

This is illogical: in previous email you complained the formula

Quoted 7 lines

TOTAL - MAPPED - ACTIVE_ANON - SLAB/2

gives too high number, while

TOTAL - MAPPED

in this patch is OK. (I'm not claiming the first formula to be fine.)

I wasn't precise enough. :-)

The problem with the first formula is that it's not really useful when used _before_ running shrink_all_memory(), becuase it may give arbitraty result in that case (everything depends on the preceding memory usage pattern). However, if it is used _after_ running shrink_all_memory(<all saveable pages>), the resulting minimum image size is usually (most often) below the real minimum number of saveable pages that can stay in memory.

The second formula, OTOH, doesn't depend so much on the preceding memory usage pattern and therefore it seems to be suitable for computing the estimate of the minimum image size _before_ running shrink_all_memory(). Still, when used _after_ running shrink_all_memory(<all saveable pages>), it will give a number below the actual minimum number of saveable pages (ie. not a really suitable one).

Now, since we're going to get rid of shrink_all_memory() at one point, I think we should be looking for a formula suitable for using before it's called. This, IMO, the second one is just about right. :-)

Quoted 5 lines
Quoted 2 lines

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients

It is _OK_ for the minimum image size to be higher, that margin serves as a safety margin as well as the working set size we want to preserve.

I didn't say it wasn't OK. :-) It's totally fine by me.

Quoted 15 lines
Quoted 3 lines

involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

That's OK. Because shrink_all_memory() shrinks memory in a prioritized list-after-list order.

Quoted 7 lines

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 5 lines

I'd like to advocate to add "working set preservation" as another goal of this function, and I can even do with the formula in this patch :-)

That means, when one day more accurate working set estimation is possible, we can extend this function to support that goal.

OK, so do you think it's fine to go with the patch below for now?

Thanks,
Rafael

Quoted 97 lines
Quoted 97 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +39 -4
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
#142 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Wu Fengguang
Quoted 64 lines · Wu Fengguang
Quoted 52 lines · Rafael J. Wysocki
Quoted 51 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void)

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 7 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

This is illogical: in previous email you complained the formula

Quoted 7 lines

TOTAL - MAPPED - ACTIVE_ANON - SLAB/2

gives too high number, while

TOTAL - MAPPED

in this patch is OK. (I'm not claiming the first formula to be fine.)

I wasn't precise enough. :-)

The problem with the first formula is that it's not really useful when used _before_ running shrink_all_memory(), becuase it may give arbitraty result in that case (everything depends on the preceding memory usage pattern). However, if it is used _after_ running shrink_all_memory(<all saveable pages>), the resulting minimum image size is usually (most often) below the real minimum number of saveable pages that can stay in memory.

The second formula, OTOH, doesn't depend so much on the preceding memory usage pattern and therefore it seems to be suitable for computing the estimate of the minimum image size _before_ running shrink_all_memory(). Still, when used _after_ running shrink_all_memory(<all saveable pages>), it will give a number below the actual minimum number of saveable pages (ie. not a really suitable one).

Now, since we're going to get rid of shrink_all_memory() at one point, I think we should be looking for a formula suitable for using before it's called. This, IMO, the second one is just about right. :-)

Quoted 5 lines
Quoted 2 lines

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients

It is _OK_ for the minimum image size to be higher, that margin serves as a safety margin as well as the working set size we want to preserve.

I didn't say it wasn't OK. :-) It's totally fine by me.

Quoted 15 lines
Quoted 3 lines

involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

That's OK. Because shrink_all_memory() shrinks memory in a prioritized list-after-list order.

Quoted 7 lines

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 5 lines

I'd like to advocate to add "working set preservation" as another goal of this function, and I can even do with the formula in this patch :-)

That means, when one day more accurate working set estimation is possible, we can extend this function to support that goal.

OK, so do you think it's fine to go with the patch below for now?

Thanks,
Rafael

Quoted 97 lines
Quoted 97 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +39 -4
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
Acked-by
#143 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

Quoted 89 lines · Rafael J. Wysocki
Quoted 64 lines · Wu Fengguang
Quoted 52 lines · Rafael J. Wysocki
Quoted 51 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void)

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 7 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

This is illogical: in previous email you complained the formula

Quoted 7 lines

TOTAL - MAPPED - ACTIVE_ANON - SLAB/2

gives too high number, while

TOTAL - MAPPED

in this patch is OK. (I'm not claiming the first formula to be fine.)

I wasn't precise enough. :-)

The problem with the first formula is that it's not really useful when used _before_ running shrink_all_memory(), becuase it may give arbitraty result in that case (everything depends on the preceding memory usage pattern). However, if it is used _after_ running shrink_all_memory(<all saveable pages>), the resulting minimum image size is usually (most often) below the real minimum number of saveable pages that can stay in memory.

The second formula, OTOH, doesn't depend so much on the preceding memory usage pattern and therefore it seems to be suitable for computing the estimate of the minimum image size _before_ running shrink_all_memory(). Still, when used _after_ running shrink_all_memory(<all saveable pages>), it will give a number below the actual minimum number of saveable pages (ie. not a really suitable one).

Quoted 3 lines

Now, since we're going to get rid of shrink_all_memory() at one point, I think we should be looking for a formula suitable for using before it's called. This, IMO, the second one is just about right. :-)

Ah OK, thanks for the explanation!

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients

It is _OK_ for the minimum image size to be higher, that margin serves as a safety margin as well as the working set size we want to preserve.

I didn't say it wasn't OK. :-) It's totally fine by me.

Great!

Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

That's OK. Because shrink_all_memory() shrinks memory in a prioritized list-after-list order.

Quoted 7 lines

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 7 lines
Quoted 5 lines

I'd like to advocate to add "working set preservation" as another goal of this function, and I can even do with the formula in this patch :-)

That means, when one day more accurate working set estimation is possible, we can extend this function to support that goal.

OK, so do you think it's fine to go with the patch below for now?

Sure, I'm fine with it.

Thanks,
Fengguang

Quoted 89 lines
Quoted 89 lines
Quoted 89 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +37 -2
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
Quoted 8 lines
Quoted 8 lines

kernel/power/snapshot.c int hibernate_preallocate_memory(void) · line 1336

Quoted 8 lines
kernel/power/snapshot.c +2 -2
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by
#144 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki
Quoted 89 lines · Rafael J. Wysocki
Quoted 64 lines · Wu Fengguang
Quoted 52 lines · Rafael J. Wysocki
Quoted 51 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void)

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 7 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

This is illogical: in previous email you complained the formula

Quoted 7 lines

TOTAL - MAPPED - ACTIVE_ANON - SLAB/2

gives too high number, while

TOTAL - MAPPED

in this patch is OK. (I'm not claiming the first formula to be fine.)

I wasn't precise enough. :-)

The problem with the first formula is that it's not really useful when used _before_ running shrink_all_memory(), becuase it may give arbitraty result in that case (everything depends on the preceding memory usage pattern). However, if it is used _after_ running shrink_all_memory(<all saveable pages>), the resulting minimum image size is usually (most often) below the real minimum number of saveable pages that can stay in memory.

The second formula, OTOH, doesn't depend so much on the preceding memory usage pattern and therefore it seems to be suitable for computing the estimate of the minimum image size _before_ running shrink_all_memory(). Still, when used _after_ running shrink_all_memory(<all saveable pages>), it will give a number below the actual minimum number of saveable pages (ie. not a really suitable one).

Quoted 3 lines

Now, since we're going to get rid of shrink_all_memory() at one point, I think we should be looking for a formula suitable for using before it's called. This, IMO, the second one is just about right. :-)

Ah OK, thanks for the explanation!

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients

It is _OK_ for the minimum image size to be higher, that margin serves as a safety margin as well as the working set size we want to preserve.

I didn't say it wasn't OK. :-) It's totally fine by me.

Great!

Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

That's OK. Because shrink_all_memory() shrinks memory in a prioritized list-after-list order.

Quoted 7 lines

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

Quoted 7 lines
Quoted 5 lines

I'd like to advocate to add "working set preservation" as another goal of this function, and I can even do with the formula in this patch :-)

That means, when one day more accurate working set estimation is possible, we can extend this function to support that goal.

OK, so do you think it's fine to go with the patch below for now?

Sure, I'm fine with it.

Thanks,
Fengguang

Quoted 97 lines
Quoted 97 lines
Quoted 97 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +39 -4
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;
#145 lore raw

Re: [RFC][PATCH 6/6] PM/Hibernate: Do not try to allocate too much memory too hard

In reply to Rafael J. Wysocki
Quoted 52 lines · Rafael J. Wysocki
Quoted 51 lines · Wu Fengguang
Quoted 16 lines · Rafael J. Wysocki
Quoted 14 lines · Wu Fengguang

kernel/power/snapshot.c static void free_unnecessary_pages(void)

Quoted 10 lines
kernel/power/snapshot.c +10 -0
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	/* Compute the number of saveable pages we can free. */
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE);

For example, we could drop the 1.25 ratio and calculate the above reclaimable size with more meaningful constraints:

Quoted 4 lines
Quoted 2 lines
        /* slabs are not easy to reclaim */
	size = global_page_state(NR_SLAB_RECLAIMABLE) / 2;

Why 1/2?

Also a very coarse value:
- we don't want to stress icache/dcache too much
(unless they grow too large)
- my experience was that the icache/dcache are scanned in a slower pace than lru pages.
- most importantly, inside the NR_SLAB_RECLAIMABLE pages, maybe half of the pages are actually *in use* and cannot be freed:
% cat /proc/sys/fs/inode-nr
30450 16605
% cat /proc/sys/fs/dentry-state
41598 35731 45 0 0 0
See? More than half entries are in-use. Sure many of them will actually become unused when dentries are freed, but in the mean time the internal fragmentations in the slabs can go up.

Quoted 4 lines
Quoted 2 lines
        /* keep NR_ACTIVE_ANON */
	size += global_page_state(NR_INACTIVE_ANON);

Why exactly did you omit ACTIVE_ANON?

To keep the "core working set" :)

Quoted 7 lines
Quoted 6 lines
        /* keep mapped files */
	size += global_page_state(NR_ACTIVE_FILE);
	size += global_page_state(NR_INACTIVE_FILE);
        size -= global_page_state(NR_FILE_MAPPED);

That restores the hard core working set logic in the reverse way ;)

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

I think the 1/2 factor for NR_SLAB_RECLAIMABLE may be too high in some cases, but I'm going to check that.

Yes, after updatedb. In that case simple magics numbers may not help. In that case we should really first call shrink_slab() in a loop to cut down the slab pages to a sane number.

I have verified that the appended patch works reasonably well.

This is illogical: in previous email you complained the formula

TOTAL - MAPPED - ACTIVE_ANON - SLAB/2

gives too high number, while

TOTAL - MAPPED

in this patch is OK. (I'm not claiming the first formula to be fine.)

Quoted 2 lines

The value returned as the minimum image size is usually too high, but not very much (on x86_64 usually about 20%) and there are no "magic" coefficients

It is _OK_ for the minimum image size to be higher, that margin serves as a safety margin as well as the working set size we want to preserve.

Quoted 3 lines

involved any more and the computation of the minimum image size is carried out before calling shrink_all_memory() (so it's still going to be useful after we've dropped shrink_all_memory() at one point).

That's OK. Because shrink_all_memory() shrinks memory in a prioritized list-after-list order.

Quoted 7 lines

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM/Hibernate: Do not try to allocate too much memory too hard (rev. 2)

We want to avoid attempting to free too much memory too hard during hibernation, so estimate the minimum size of the image to use as the lower limit for preallocating memory.

I'd like to advocate to add "working set preservation" as another goal of this function, and I can even do with the formula in this patch :-)

That means, when one day more accurate working set estimation is possible, we can extend this function to support that goal.

Thanks,
Fengguang

Quoted 97 lines

The approach here is based on the (experimental) observation that we can't free more page frames than the sum of:

* global_page_state(NR_SLAB_RECLAIMABLE)
* global_page_state(NR_ACTIVE_ANON)
* global_page_state(NR_INACTIVE_ANON)
* global_page_state(NR_ACTIVE_FILE)
* global_page_state(NR_INACTIVE_FILE)

minus

* global_page_state(NR_FILE_MAPPED)

Namely, if this number is subtracted from the number of saveable pages in the system, we get a good estimate of the minimum reasonable size of a hibernation image.

---
 kernel/power/snapshot.c |   43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

Index: linux-2.6/kernel/power/snapshot.c
===================================================================

kernel/power/snapshot.c +39 -4
@@ -1204,6 +1204,36 @@ static void free_unnecessary_pages(void)
 }
 
 /**
+ * minimum_image_size - Estimate the minimum acceptable size of an image
+ * @saveable: Number of saveable pages in the system.
+ *
+ * We want to avoid attempting to free too much memory too hard, so estimate the
+ * minimum acceptable size of a hibernation image to use as the lower limit for
+ * preallocating memory.
+ *
+ * We assume that the minimum image size should be proportional to
+ *
+ * [number of saveable pages] - [number of pages that can be freed in theory]
+ *
+ * where the second term is the sum of (1) reclaimable slab pages, (2) active
+ * and (3) inactive anonymouns pages, (4) active and (5) inactive file pages,
+ * minus mapped file pages.
+ */
+static unsigned long minimum_image_size(unsigned long saveable)
+{
+	unsigned long size;
+
+	size = global_page_state(NR_SLAB_RECLAIMABLE)
+		+ global_page_state(NR_ACTIVE_ANON)
+		+ global_page_state(NR_INACTIVE_ANON)
+		+ global_page_state(NR_ACTIVE_FILE)
+		+ global_page_state(NR_INACTIVE_FILE)
+		- global_page_state(NR_FILE_MAPPED);
+
+	return saveable <= size ? 0 : saveable - size;
+}
+
+/**
  * hibernate_preallocate_memory - Preallocate memory for hibernation image
  *
  * To create a hibernation image it is necessary to make a copy of every page
@@ -1220,8 +1250,8 @@ static void free_unnecessary_pages(void)
  *
  * If image_size is set below the number following from the above formula,
  * the preallocation of memory is continued until the total number of saveable
- * pages in the system is below the requested image size or it is impossible to
- * allocate more memory, whichever happens first.
+ * pages in the system is below the requested image size or the minimum
+ * acceptable image size returned by minimum_image_size(), whichever is greater.
  */
 int hibernate_preallocate_memory(void)
 {
@@ -1282,6 +1312,11 @@ int hibernate_preallocate_memory(void)
 		goto out;
 	}
 
+	/* Estimate the minimum size of the image. */
+	pages = minimum_image_size(saveable);
+	if (size < pages)
+		size = min_t(unsigned long, pages, max_size);
+
 	/*
 	 * Let the memory management subsystem know that we're going to need a
 	 * large number of page frames to allocate and make it free some memory.
@@ -1294,8 +1329,8 @@ int hibernate_preallocate_memory(void)
 	 * The number of saveable pages in memory was too high, so apply some
 	 * pressure to decrease it.  First, make room for the largest possible
 	 * image and fail if that doesn't work.  Next, try to decrease the size
-	 * of the image as much as indicated by image_size using allocations
-	 * from highmem and non-highmem zones separately.
+	 * of the image as much as indicated by 'size' using allocations from
+	 * highmem and non-highmem zones separately.
 	 */
 	pages_highmem = preallocate_image_highmem(highmem / 2);
 	max_size += pages_highmem;