Skip to content

Commit ddeb648

Browse files
committed
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>
1 parent 13e3813 commit ddeb648

5 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎Documentation/ABI/testing/sysfs-power‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,17 @@ Description:
158158
successful, will make the kernel abort a subsequent transition
159159
to a sleep state if any wakeup events are reported after the
160160
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.

‎kernel/power/hibernate.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,10 +982,33 @@ static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *att
982982

983983
power_attr(image_size);
984984

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+
9851007
static struct attribute * g[] = {
9861008
&disk_attr.attr,
9871009
&resume_attr.attr,
9881010
&image_size_attr.attr,
1011+
&reserved_size_attr.attr,
9891012
NULL,
9901013
};
9911014

‎kernel/power/main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ static int __init pm_init(void)
337337
if (error)
338338
return error;
339339
hibernate_image_size_init();
340+
hibernate_reserved_size_init();
340341
power_kobj = kobject_create_and_add("power", NULL);
341342
if (!power_kobj)
342343
return -ENOMEM;

‎kernel/power/power.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct swsusp_info {
1515

1616
#ifdef CONFIG_HIBERNATION
1717
/* kernel/power/snapshot.c */
18+
extern void __init hibernate_reserved_size_init(void);
1819
extern void __init hibernate_image_size_init(void);
1920

2021
#ifdef CONFIG_ARCH_HIBERNATION_HEADER
@@ -55,6 +56,7 @@ extern int hibernation_platform_enter(void);
5556

5657
#else /* !CONFIG_HIBERNATION */
5758

59+
static inline void hibernate_reserved_size_init(void) {}
5860
static inline void hibernate_image_size_init(void) {}
5961
#endif /* !CONFIG_HIBERNATION */
6062

@@ -72,6 +74,8 @@ static struct kobj_attribute _name##_attr = { \
7274

7375
/* Preferred image size in bytes (default 500 MB) */
7476
extern unsigned long image_size;
77+
/* Size of memory reserved for drivers (default SPARE_PAGES x PAGE_SIZE) */
78+
extern unsigned long reserved_size;
7579
extern int in_suspend;
7680
extern dev_t swsusp_resume_device;
7781
extern sector_t swsusp_resume_block;

‎kernel/power/snapshot.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ static int swsusp_page_is_free(struct page *);
4040
static void swsusp_set_page_forbidden(struct page *);
4141
static void swsusp_unset_page_forbidden(struct page *);
4242

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+
4355
/*
4456
* Preferred image size in bytes (tunable via /sys/power/image_size).
4557
* When it is set to N, the image creating code will do its best to
@@ -1263,11 +1275,13 @@ static unsigned long minimum_image_size(unsigned long saveable)
12631275
* frame in use. We also need a number of page frames to be free during
12641276
* hibernation for allocations made while saving the image and for device
12651277
* drivers, in case they need to allocate memory from their hibernation
1266-
* callbacks (these two numbers are given by PAGES_FOR_IO and SPARE_PAGES,
1267-
* respectively, both of which are rough estimates). To make this happen, we
1268-
* compute the total number of available page frames and allocate at least
1278+
* callbacks (these two numbers are given by PAGES_FOR_IO (which is a rough
1279+
* estimate) and reserverd_size divided by PAGE_SIZE (which is tunable through
1280+
* /sys/power/reserved_size, respectively). To make this happen, we compute the
1281+
* total number of available page frames and allocate at least
12691282
*
1270-
* ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2 + 2 * SPARE_PAGES
1283+
* ([page frames total] + PAGES_FOR_IO + [metadata pages]) / 2
1284+
* + 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE)
12711285
*
12721286
* of them, which corresponds to the maximum size of a hibernation image.
12731287
*
@@ -1322,7 +1336,8 @@ int hibernate_preallocate_memory(void)
13221336
count -= totalreserve_pages;
13231337

13241338
/* Compute the maximum number of saveable pages to leave in memory. */
1325-
max_size = (count - (size + PAGES_FOR_IO)) / 2 - 2 * SPARE_PAGES;
1339+
max_size = (count - (size + PAGES_FOR_IO)) / 2
1340+
- 2 * DIV_ROUND_UP(reserved_size, PAGE_SIZE);
13261341
/* Compute the desired number of image pages specified by image_size. */
13271342
size = DIV_ROUND_UP(image_size, PAGE_SIZE);
13281343
if (size > max_size)

0 commit comments

Comments
 (0)