Skip to content

Commit 6cfdd02

Browse files
committed
Merge tag 'pm-for-3.4-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
Pull power management fixes from Rafael J. Wysocki: "Fix for an issue causing hibernation to hang on systems with highmem (that practically means i386) due to broken memory management (bug introduced in 3.2, so -stable material) and PM documentation update making the freezer documentation follow the code again after some recent updates." * tag 'pm-for-3.4-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm: PM / Freezer / Docs: Update documentation about freezing of tasks PM / Hibernate: fix the number of pages used for hibernate/thaw buffering
2 parents 64f371b + 26e0f90 commit 6cfdd02

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

Documentation/power/freezing-of-tasks.txt

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,39 @@ architectures).
99

1010
II. How does it work?
1111

12-
There are four per-task flags used for that, PF_NOFREEZE, PF_FROZEN, TIF_FREEZE
12+
There are three per-task flags used for that, PF_NOFREEZE, PF_FROZEN
1313
and PF_FREEZER_SKIP (the last one is auxiliary). The tasks that have
1414
PF_NOFREEZE unset (all user space processes and some kernel threads) are
1515
regarded as 'freezable' and treated in a special way before the system enters a
1616
suspend state as well as before a hibernation image is created (in what follows
1717
we only consider hibernation, but the description also applies to suspend).
1818

1919
Namely, as the first step of the hibernation procedure the function
20-
freeze_processes() (defined in kernel/power/process.c) is called. It executes
21-
try_to_freeze_tasks() that sets TIF_FREEZE for all of the freezable tasks and
22-
either wakes them up, if they are kernel threads, or sends fake signals to them,
23-
if they are user space processes. A task that has TIF_FREEZE set, should react
24-
to it by calling the function called __refrigerator() (defined in
25-
kernel/freezer.c), which sets the task's PF_FROZEN flag, changes its state
26-
to TASK_UNINTERRUPTIBLE and makes it loop until PF_FROZEN is cleared for it.
27-
Then, we say that the task is 'frozen' and therefore the set of functions
28-
handling this mechanism is referred to as 'the freezer' (these functions are
29-
defined in kernel/power/process.c, kernel/freezer.c & include/linux/freezer.h).
30-
User space processes are generally frozen before kernel threads.
20+
freeze_processes() (defined in kernel/power/process.c) is called. A system-wide
21+
variable system_freezing_cnt (as opposed to a per-task flag) is used to indicate
22+
whether the system is to undergo a freezing operation. And freeze_processes()
23+
sets this variable. After this, it executes try_to_freeze_tasks() that sends a
24+
fake signal to all user space processes, and wakes up all the kernel threads.
25+
All freezable tasks must react to that by calling try_to_freeze(), which
26+
results in a call to __refrigerator() (defined in kernel/freezer.c), which sets
27+
the task's PF_FROZEN flag, changes its state to TASK_UNINTERRUPTIBLE and makes
28+
it loop until PF_FROZEN is cleared for it. Then, we say that the task is
29+
'frozen' and therefore the set of functions handling this mechanism is referred
30+
to as 'the freezer' (these functions are defined in kernel/power/process.c,
31+
kernel/freezer.c & include/linux/freezer.h). User space processes are generally
32+
frozen before kernel threads.
3133

3234
__refrigerator() must not be called directly. Instead, use the
3335
try_to_freeze() function (defined in include/linux/freezer.h), that checks
34-
the task's TIF_FREEZE flag and makes the task enter __refrigerator() if the
35-
flag is set.
36+
if the task is to be frozen and makes the task enter __refrigerator().
3637

3738
For user space processes try_to_freeze() is called automatically from the
3839
signal-handling code, but the freezable kernel threads need to call it
3940
explicitly in suitable places or use the wait_event_freezable() or
4041
wait_event_freezable_timeout() macros (defined in include/linux/freezer.h)
41-
that combine interruptible sleep with checking if TIF_FREEZE is set and calling
42-
try_to_freeze(). The main loop of a freezable kernel thread may look like the
43-
following one:
42+
that combine interruptible sleep with checking if the task is to be frozen and
43+
calling try_to_freeze(). The main loop of a freezable kernel thread may look
44+
like the following one:
4445

4546
set_freezable();
4647
do {
@@ -53,7 +54,7 @@ following one:
5354
(from drivers/usb/core/hub.c::hub_thread()).
5455

5556
If a freezable kernel thread fails to call try_to_freeze() after the freezer has
56-
set TIF_FREEZE for it, the freezing of tasks will fail and the entire
57+
initiated a freezing operation, the freezing of tasks will fail and the entire
5758
hibernation operation will be cancelled. For this reason, freezable kernel
5859
threads must call try_to_freeze() somewhere or use one of the
5960
wait_event_freezable() and wait_event_freezable_timeout() macros.

kernel/power/swap.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@
5151

5252
#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
5353

54+
/*
55+
* Number of free pages that are not high.
56+
*/
57+
static inline unsigned long low_free_pages(void)
58+
{
59+
return nr_free_pages() - nr_free_highpages();
60+
}
61+
62+
/*
63+
* Number of pages required to be kept free while writing the image. Always
64+
* half of all available low pages before the writing starts.
65+
*/
66+
static inline unsigned long reqd_free_pages(void)
67+
{
68+
return low_free_pages() / 2;
69+
}
70+
5471
struct swap_map_page {
5572
sector_t entries[MAP_PAGE_ENTRIES];
5673
sector_t next_swap;
@@ -72,7 +89,7 @@ struct swap_map_handle {
7289
sector_t cur_swap;
7390
sector_t first_sector;
7491
unsigned int k;
75-
unsigned long nr_free_pages, written;
92+
unsigned long reqd_free_pages;
7693
u32 crc32;
7794
};
7895

@@ -316,8 +333,7 @@ static int get_swap_writer(struct swap_map_handle *handle)
316333
goto err_rel;
317334
}
318335
handle->k = 0;
319-
handle->nr_free_pages = nr_free_pages() >> 1;
320-
handle->written = 0;
336+
handle->reqd_free_pages = reqd_free_pages();
321337
handle->first_sector = handle->cur_swap;
322338
return 0;
323339
err_rel:
@@ -352,11 +368,11 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
352368
handle->cur_swap = offset;
353369
handle->k = 0;
354370
}
355-
if (bio_chain && ++handle->written > handle->nr_free_pages) {
371+
if (bio_chain && low_free_pages() <= handle->reqd_free_pages) {
356372
error = hib_wait_on_bio_chain(bio_chain);
357373
if (error)
358374
goto out;
359-
handle->written = 0;
375+
handle->reqd_free_pages = reqd_free_pages();
360376
}
361377
out:
362378
return error;
@@ -618,7 +634,7 @@ static int save_image_lzo(struct swap_map_handle *handle,
618634
* Adjust number of free pages after all allocations have been done.
619635
* We don't want to run out of pages when writing.
620636
*/
621-
handle->nr_free_pages = nr_free_pages() >> 1;
637+
handle->reqd_free_pages = reqd_free_pages();
622638

623639
/*
624640
* Start the CRC32 thread.

0 commit comments

Comments
 (0)