aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/power/snapshot.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/power/snapshot.c')
-rw-r--r--kernel/power/snapshot.c99
1 files changed, 75 insertions, 24 deletions
diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c
index 5e7edfb05e66..ac7eb109f196 100644
--- a/kernel/power/snapshot.c
+++ b/kernel/power/snapshot.c
@@ -46,7 +46,12 @@ static void swsusp_unset_page_forbidden(struct page *);
46 * size will not exceed N bytes, but if that is impossible, it will 46 * size will not exceed N bytes, but if that is impossible, it will
47 * try to create the smallest image possible. 47 * try to create the smallest image possible.
48 */ 48 */
49unsigned long image_size = 500 * 1024 * 1024; 49unsigned long image_size;
50
51void __init hibernate_image_size_init(void)
52{
53 image_size = ((totalram_pages * 2) / 5) * PAGE_SIZE;
54}
50 55
51/* List of PBEs needed for restoring the pages that were allocated before 56/* List of PBEs needed for restoring the pages that were allocated before
52 * the suspend and included in the suspend image, but have also been 57 * the suspend and included in the suspend image, but have also been
@@ -1086,7 +1091,6 @@ void swsusp_free(void)
1086 buffer = NULL; 1091 buffer = NULL;
1087 alloc_normal = 0; 1092 alloc_normal = 0;
1088 alloc_highmem = 0; 1093 alloc_highmem = 0;
1089 hibernation_thaw_swap();
1090} 1094}
1091 1095
1092/* Helper functions used for the shrinking of memory. */ 1096/* Helper functions used for the shrinking of memory. */
@@ -1122,9 +1126,19 @@ static unsigned long preallocate_image_pages(unsigned long nr_pages, gfp_t mask)
1122 return nr_alloc; 1126 return nr_alloc;
1123} 1127}
1124 1128
1125static unsigned long preallocate_image_memory(unsigned long nr_pages) 1129static unsigned long preallocate_image_memory(unsigned long nr_pages,
1130 unsigned long avail_normal)
1126{ 1131{
1127 return preallocate_image_pages(nr_pages, GFP_IMAGE); 1132 unsigned long alloc;
1133
1134 if (avail_normal <= alloc_normal)
1135 return 0;
1136
1137 alloc = avail_normal - alloc_normal;
1138 if (nr_pages < alloc)
1139 alloc = nr_pages;
1140
1141 return preallocate_image_pages(alloc, GFP_IMAGE);
1128} 1142}
1129 1143
1130#ifdef CONFIG_HIGHMEM 1144#ifdef CONFIG_HIGHMEM
@@ -1170,15 +1184,22 @@ static inline unsigned long preallocate_highmem_fraction(unsigned long nr_pages,
1170 */ 1184 */
1171static void free_unnecessary_pages(void) 1185static void free_unnecessary_pages(void)
1172{ 1186{
1173 unsigned long save_highmem, to_free_normal, to_free_highmem; 1187 unsigned long save, to_free_normal, to_free_highmem;
1174 1188
1175 to_free_normal = alloc_normal - count_data_pages(); 1189 save = count_data_pages();
1176 save_highmem = count_highmem_pages(); 1190 if (alloc_normal >= save) {
1177 if (alloc_highmem > save_highmem) { 1191 to_free_normal = alloc_normal - save;
1178 to_free_highmem = alloc_highmem - save_highmem; 1192 save = 0;
1193 } else {
1194 to_free_normal = 0;
1195 save -= alloc_normal;
1196 }
1197 save += count_highmem_pages();
1198 if (alloc_highmem >= save) {
1199 to_free_highmem = alloc_highmem - save;
1179 } else { 1200 } else {
1180 to_free_highmem = 0; 1201 to_free_highmem = 0;
1181 to_free_normal -= save_highmem - alloc_highmem; 1202 to_free_normal -= save - alloc_highmem;
1182 } 1203 }
1183 1204
1184 memory_bm_position_reset(&copy_bm); 1205 memory_bm_position_reset(&copy_bm);
@@ -1259,7 +1280,7 @@ int hibernate_preallocate_memory(void)
1259{ 1280{
1260 struct zone *zone; 1281 struct zone *zone;
1261 unsigned long saveable, size, max_size, count, highmem, pages = 0; 1282 unsigned long saveable, size, max_size, count, highmem, pages = 0;
1262 unsigned long alloc, save_highmem, pages_highmem; 1283 unsigned long alloc, save_highmem, pages_highmem, avail_normal;
1263 struct timeval start, stop; 1284 struct timeval start, stop;
1264 int error; 1285 int error;
1265 1286
@@ -1296,26 +1317,38 @@ int hibernate_preallocate_memory(void)
1296 else 1317 else
1297 count += zone_page_state(zone, NR_FREE_PAGES); 1318 count += zone_page_state(zone, NR_FREE_PAGES);
1298 } 1319 }
1320 avail_normal = count;
1299 count += highmem; 1321 count += highmem;
1300 count -= totalreserve_pages; 1322 count -= totalreserve_pages;
1301 1323
1302 /* Compute the maximum number of saveable pages to leave in memory. */ 1324 /* Compute the maximum number of saveable pages to leave in memory. */
1303 max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES; 1325 max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES;
1326 /* Compute the desired number of image pages specified by image_size. */
1304 size = DIV_ROUND_UP(image_size, PAGE_SIZE); 1327 size = DIV_ROUND_UP(image_size, PAGE_SIZE);
1305 if (size > max_size) 1328 if (size > max_size)
1306 size = max_size; 1329 size = max_size;
1307 /* 1330 /*
1308 * If the maximum is not less than the current number of saveable pages 1331 * If the desired number of image pages is at least as large as the
1309 * in memory, allocate page frames for the image and we're done. 1332 * current number of saveable pages in memory, allocate page frames for
1333 * the image and we're done.
1310 */ 1334 */
1311 if (size >= saveable) { 1335 if (size >= saveable) {
1312 pages = preallocate_image_highmem(save_highmem); 1336 pages = preallocate_image_highmem(save_highmem);
1313 pages += preallocate_image_memory(saveable - pages); 1337 pages += preallocate_image_memory(saveable - pages, avail_normal);
1314 goto out; 1338 goto out;
1315 } 1339 }
1316 1340
1317 /* Estimate the minimum size of the image. */ 1341 /* Estimate the minimum size of the image. */
1318 pages = minimum_image_size(saveable); 1342 pages = minimum_image_size(saveable);
1343 /*
1344 * To avoid excessive pressure on the normal zone, leave room in it to
1345 * accommodate an image of the minimum size (unless it's already too
1346 * small, in which case don't preallocate pages from it at all).
1347 */
1348 if (avail_normal > pages)
1349 avail_normal -= pages;
1350 else
1351 avail_normal = 0;
1319 if (size < pages) 1352 if (size < pages)
1320 size = min_t(unsigned long, pages, max_size); 1353 size = min_t(unsigned long, pages, max_size);
1321 1354
@@ -1336,16 +1369,34 @@ int hibernate_preallocate_memory(void)
1336 */ 1369 */
1337 pages_highmem = preallocate_image_highmem(highmem / 2); 1370 pages_highmem = preallocate_image_highmem(highmem / 2);
1338 alloc = (count - max_size) - pages_highmem; 1371 alloc = (count - max_size) - pages_highmem;
1339 pages = preallocate_image_memory(alloc); 1372 pages = preallocate_image_memory(alloc, avail_normal);
1340 if (pages < alloc) 1373 if (pages < alloc) {
1341 goto err_out; 1374 /* We have exhausted non-highmem pages, try highmem. */
1342 size = max_size - size; 1375 alloc -= pages;
1343 alloc = size; 1376 pages += pages_highmem;
1344 size = preallocate_highmem_fraction(size, highmem, count); 1377 pages_highmem = preallocate_image_highmem(alloc);
1345 pages_highmem += size; 1378 if (pages_highmem < alloc)
1346 alloc -= size; 1379 goto err_out;
1347 pages += preallocate_image_memory(alloc); 1380 pages += pages_highmem;
1348 pages += pages_highmem; 1381 /*
1382 * size is the desired number of saveable pages to leave in
1383 * memory, so try to preallocate (all memory - size) pages.
1384 */
1385 alloc = (count - pages) - size;
1386 pages += preallocate_image_highmem(alloc);
1387 } else {
1388 /*
1389 * There are approximately max_size saveable pages at this point
1390 * and we want to reduce this number down to size.
1391 */
1392 alloc = max_size - size;
1393 size = preallocate_highmem_fraction(alloc, highmem, count);
1394 pages_highmem += size;
1395 alloc -= size;
1396 size = preallocate_image_memory(alloc, avail_normal);
1397 pages_highmem += preallocate_image_highmem(alloc - size);
1398 pages += pages_highmem + size;
1399 }
1349 1400
1350 /* 1401 /*
1351 * We only need as many page frames for the image as there are saveable 1402 * We only need as many page frames for the image as there are saveable