Commit 6cfdd02b authored by Linus Torvalds's avatar Linus Torvalds

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
parents 64f371bc 26e0f90f
...@@ -9,7 +9,7 @@ architectures). ...@@ -9,7 +9,7 @@ architectures).
II. How does it work? II. How does it work?
There are four per-task flags used for that, PF_NOFREEZE, PF_FROZEN, TIF_FREEZE There are three per-task flags used for that, PF_NOFREEZE, PF_FROZEN
and PF_FREEZER_SKIP (the last one is auxiliary). The tasks that have and PF_FREEZER_SKIP (the last one is auxiliary). The tasks that have
PF_NOFREEZE unset (all user space processes and some kernel threads) are PF_NOFREEZE unset (all user space processes and some kernel threads) are
regarded as 'freezable' and treated in a special way before the system enters a regarded as 'freezable' and treated in a special way before the system enters a
...@@ -17,30 +17,31 @@ suspend state as well as before a hibernation image is created (in what follows ...@@ -17,30 +17,31 @@ suspend state as well as before a hibernation image is created (in what follows
we only consider hibernation, but the description also applies to suspend). we only consider hibernation, but the description also applies to suspend).
Namely, as the first step of the hibernation procedure the function Namely, as the first step of the hibernation procedure the function
freeze_processes() (defined in kernel/power/process.c) is called. It executes freeze_processes() (defined in kernel/power/process.c) is called. A system-wide
try_to_freeze_tasks() that sets TIF_FREEZE for all of the freezable tasks and variable system_freezing_cnt (as opposed to a per-task flag) is used to indicate
either wakes them up, if they are kernel threads, or sends fake signals to them, whether the system is to undergo a freezing operation. And freeze_processes()
if they are user space processes. A task that has TIF_FREEZE set, should react sets this variable. After this, it executes try_to_freeze_tasks() that sends a
to it by calling the function called __refrigerator() (defined in fake signal to all user space processes, and wakes up all the kernel threads.
kernel/freezer.c), which sets the task's PF_FROZEN flag, changes its state All freezable tasks must react to that by calling try_to_freeze(), which
to TASK_UNINTERRUPTIBLE and makes it loop until PF_FROZEN is cleared for it. results in a call to __refrigerator() (defined in kernel/freezer.c), which sets
Then, we say that the task is 'frozen' and therefore the set of functions the task's PF_FROZEN flag, changes its state to TASK_UNINTERRUPTIBLE and makes
handling this mechanism is referred to as 'the freezer' (these functions are it loop until PF_FROZEN is cleared for it. Then, we say that the task is
defined in kernel/power/process.c, kernel/freezer.c & include/linux/freezer.h). 'frozen' and therefore the set of functions handling this mechanism is referred
User space processes are generally frozen before kernel threads. to as 'the freezer' (these functions are defined in kernel/power/process.c,
kernel/freezer.c & include/linux/freezer.h). User space processes are generally
frozen before kernel threads.
__refrigerator() must not be called directly. Instead, use the __refrigerator() must not be called directly. Instead, use the
try_to_freeze() function (defined in include/linux/freezer.h), that checks try_to_freeze() function (defined in include/linux/freezer.h), that checks
the task's TIF_FREEZE flag and makes the task enter __refrigerator() if the if the task is to be frozen and makes the task enter __refrigerator().
flag is set.
For user space processes try_to_freeze() is called automatically from the For user space processes try_to_freeze() is called automatically from the
signal-handling code, but the freezable kernel threads need to call it signal-handling code, but the freezable kernel threads need to call it
explicitly in suitable places or use the wait_event_freezable() or explicitly in suitable places or use the wait_event_freezable() or
wait_event_freezable_timeout() macros (defined in include/linux/freezer.h) wait_event_freezable_timeout() macros (defined in include/linux/freezer.h)
that combine interruptible sleep with checking if TIF_FREEZE is set and calling that combine interruptible sleep with checking if the task is to be frozen and
try_to_freeze(). The main loop of a freezable kernel thread may look like the calling try_to_freeze(). The main loop of a freezable kernel thread may look
following one: like the following one:
set_freezable(); set_freezable();
do { do {
...@@ -53,7 +54,7 @@ following one: ...@@ -53,7 +54,7 @@ following one:
(from drivers/usb/core/hub.c::hub_thread()). (from drivers/usb/core/hub.c::hub_thread()).
If a freezable kernel thread fails to call try_to_freeze() after the freezer has If a freezable kernel thread fails to call try_to_freeze() after the freezer has
set TIF_FREEZE for it, the freezing of tasks will fail and the entire initiated a freezing operation, the freezing of tasks will fail and the entire
hibernation operation will be cancelled. For this reason, freezable kernel hibernation operation will be cancelled. For this reason, freezable kernel
threads must call try_to_freeze() somewhere or use one of the threads must call try_to_freeze() somewhere or use one of the
wait_event_freezable() and wait_event_freezable_timeout() macros. wait_event_freezable() and wait_event_freezable_timeout() macros.
......
...@@ -51,6 +51,23 @@ ...@@ -51,6 +51,23 @@
#define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1) #define MAP_PAGE_ENTRIES (PAGE_SIZE / sizeof(sector_t) - 1)
/*
* Number of free pages that are not high.
*/
static inline unsigned long low_free_pages(void)
{
return nr_free_pages() - nr_free_highpages();
}
/*
* Number of pages required to be kept free while writing the image. Always
* half of all available low pages before the writing starts.
*/
static inline unsigned long reqd_free_pages(void)
{
return low_free_pages() / 2;
}
struct swap_map_page { struct swap_map_page {
sector_t entries[MAP_PAGE_ENTRIES]; sector_t entries[MAP_PAGE_ENTRIES];
sector_t next_swap; sector_t next_swap;
...@@ -72,7 +89,7 @@ struct swap_map_handle { ...@@ -72,7 +89,7 @@ struct swap_map_handle {
sector_t cur_swap; sector_t cur_swap;
sector_t first_sector; sector_t first_sector;
unsigned int k; unsigned int k;
unsigned long nr_free_pages, written; unsigned long reqd_free_pages;
u32 crc32; u32 crc32;
}; };
...@@ -316,8 +333,7 @@ static int get_swap_writer(struct swap_map_handle *handle) ...@@ -316,8 +333,7 @@ static int get_swap_writer(struct swap_map_handle *handle)
goto err_rel; goto err_rel;
} }
handle->k = 0; handle->k = 0;
handle->nr_free_pages = nr_free_pages() >> 1; handle->reqd_free_pages = reqd_free_pages();
handle->written = 0;
handle->first_sector = handle->cur_swap; handle->first_sector = handle->cur_swap;
return 0; return 0;
err_rel: err_rel:
...@@ -352,11 +368,11 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf, ...@@ -352,11 +368,11 @@ static int swap_write_page(struct swap_map_handle *handle, void *buf,
handle->cur_swap = offset; handle->cur_swap = offset;
handle->k = 0; handle->k = 0;
} }
if (bio_chain && ++handle->written > handle->nr_free_pages) { if (bio_chain && low_free_pages() <= handle->reqd_free_pages) {
error = hib_wait_on_bio_chain(bio_chain); error = hib_wait_on_bio_chain(bio_chain);
if (error) if (error)
goto out; goto out;
handle->written = 0; handle->reqd_free_pages = reqd_free_pages();
} }
out: out:
return error; return error;
...@@ -618,7 +634,7 @@ static int save_image_lzo(struct swap_map_handle *handle, ...@@ -618,7 +634,7 @@ static int save_image_lzo(struct swap_map_handle *handle,
* Adjust number of free pages after all allocations have been done. * Adjust number of free pages after all allocations have been done.
* We don't want to run out of pages when writing. * We don't want to run out of pages when writing.
*/ */
handle->nr_free_pages = nr_free_pages() >> 1; handle->reqd_free_pages = reqd_free_pages();
/* /*
* Start the CRC32 thread. * Start the CRC32 thread.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment