Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PM / Hibernate: Add sysfs knob to control size of memory for drivers

Martin reports that on his system hibernation occasionally fails due
to the lack of memory, because the radeon driver apparently allocates
too much of it during the device freeze stage. It turns out that the
amount of memory allocated by radeon during hibernation (and
presumably during system suspend too) depends on the utilization of
the GPU (e.g. hibernating while there are two KDE 4 sessions with
compositing enabled causes radeon to allocate more memory than for
one KDE 4 session).

In principle it should be possible to use image_size to make the
memory preallocation mechanism free enough memory for the radeon
driver, but in practice it is not easy to guess the right value
because of the way the preallocation code uses image_size. For this
reason, it seems reasonable to allow users to control the amount of
memory reserved for driver allocations made after the hibernate
preallocation, which currently is constant and amounts to 1 MB.

Introduce a new sysfs file, /sys/power/reserved_size, whose value
will be used as the amount of memory to reserve for the
post-preallocation reservations made by device drivers, in bytes.
For backwards compatibility, set its default (and initial) value to
the currently used number (1 MB).

References: https://bugzilla.kernel.org/show_bug.cgi?id=34102
Reported-and-tested-by: Martin Steigerwald <Martin@Lichtvoll.de>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>

+62 -5
+14
Documentation/ABI/testing/sysfs-power
··· 158 158 successful, will make the kernel abort a subsequent transition 159 159 to a sleep state if any wakeup events are reported after the 160 160 write has returned. 161 + 162 + What: /sys/power/reserved_size 163 + Date: May 2011 164 + Contact: Rafael J. Wysocki <rjw@sisk.pl> 165 + Description: 166 + The /sys/power/reserved_size file allows user space to control 167 + the amount of memory reserved for allocations made by device 168 + drivers during the "device freeze" stage of hibernation. It can 169 + be written a string representing a non-negative integer that 170 + will be used as the amount of memory to reserve for allocations 171 + made by device drivers' "freeze" callbacks, in bytes. 172 + 173 + Reading from this file will display the current value, which is 174 + set to 1 MB by default.
+23
kernel/power/hibernate.c
··· 982 982 983 983 power_attr(image_size); 984 984 985 + static ssize_t reserved_size_show(struct kobject *kobj, 986 + struct kobj_attribute *attr, char *buf) 987 + { 988 + return sprintf(buf, "%lu\n", reserved_size); 989 + } 990 + 991 + static ssize_t reserved_size_store(struct kobject *kobj, 992 + struct kobj_attribute *attr, 993 + const char *buf, size_t n) 994 + { 995 + unsigned long size; 996 + 997 + if (sscanf(buf, "%lu", &size) == 1) { 998 + reserved_size = size; 999 + return n; 1000 + } 1001 + 1002 + return -EINVAL; 1003 + } 1004 + 1005 + power_attr(reserved_size); 1006 + 985 1007 static struct attribute * g[] = { 986 1008 &disk_attr.attr, 987 1009 &resume_attr.attr, 988 1010 &image_size_attr.attr, 1011 + &reserved_size_attr.attr, 989 1012 NULL, 990 1013 }; 991 1014
+1
kernel/power/main.c
··· 337 337 if (error) 338 338 return error; 339 339 hibernate_image_size_init(); 340 + hibernate_reserved_size_init(); 340 341 power_kobj = kobject_create_and_add("power", NULL); 341 342 if (!power_kobj) 342 343 return -ENOMEM;
+4
kernel/power/power.h
··· 15 15 16 16 #ifdef CONFIG_HIBERNATION 17 17 /* kernel/power/snapshot.c */ 18 + extern void __init hibernate_reserved_size_init(void); 18 19 extern void __init hibernate_image_size_init(void); 19 20 20 21 #ifdef CONFIG_ARCH_HIBERNATION_HEADER ··· 56 55 57 56 #else /* !CONFIG_HIBERNATION */ 58 57 58 + static inline void hibernate_reserved_size_init(void) {} 59 59 static inline void hibernate_image_size_init(void) {} 60 60 #endif /* !CONFIG_HIBERNATION */ 61 61 ··· 74 72 75 73 /* Preferred image size in bytes (default 500 MB) */ 76 74 extern unsigned long image_size; 75 + /* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */ 76 + extern unsigned long reserved_size; 77 77 extern int in_suspend; 78 78 extern dev_t swsusp_resume_device; 79 79 extern sector_t swsusp_resume_block;
+20 -5
kernel/power/snapshot.c
··· 41 41 static void swsusp_unset_page_forbidden(struct page *); 42 42 43 43 /* 44 + * Number of bytes to reserve for memory allocations made by device drivers 45 + * from their ->freeze() and ->freeze_noirq() callbacks so that they don't 46 + * cause image creation to fail (tunable via /sys/power/reserved_size). 47 + */ 48 + unsigned long reserved_size; 49 + 50 + void __init hibernate_reserved_size_init(void) 51 + { 52 + reserved_size = SPARE_PAGES * PAGE_SIZE; 53 + } 54 + 55 + /* 44 56 * Preferred image size in bytes (tunable via /sys/power/image_size). 45 57 * When it is set to N, the image creating code will do its best to 46 58 * ensure the image size will not exceed N bytes, but if that is ··· 1275 1263 * frame in use. We also need a number of page frames to be free during 1276 1264 * hibernation for allocations made while saving the image and for device 1277 1265 * drivers, in case they need to allocate memory from their hibernation 1278 - * callbacks (these two numbers are given by PAGES_FOR_IO and SPARE_PAGES, 1279 - * respectively, both of which are rough estimates). To make this happen, we 1280 - * compute the total number of available page frames and allocate at least 1266 + * callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough 1267 + * estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through 1268 + * /sys/power/reserved_size, respectively). To make this happen, we compute the 1269 + * total number of available page frames and allocate at least 1281 1270 * 1282 - * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 + 2 * SPARE_PAGES 1271 + * ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 1272 + * + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE) 1283 1273 * 1284 1274 * of them, which corresponds to the maximum size of a hibernation image. 1285 1275 * ··· 1336 1322 count -= totalreserve_pages; 1337 1323 1338 1324 /* Compute the maximum number of saveable pages to leave in memory. */ 1339 - max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES; 1325 + max_size = (count - (size + PAGES_FOR_IO)) / 2 1326 + - 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE); 1340 1327 /* Compute the desired number of image pages specified by image_size. */ 1341 1328 size = DIV_ROUND_UP(image_size, PAGE_SIZE); 1342 1329 if (size > max_size)