diff options
| author | Rafael J. Wysocki <rjw@sisk.pl> | 2007-07-17 07:03:35 -0400 |
|---|---|---|
| committer | Linus Torvalds <torvalds@woody.linux-foundation.org> | 2007-07-17 13:23:02 -0400 |
| commit | 831441862956fffa17b9801db37e6ea1650b0f69 (patch) | |
| tree | b0334921341f8f1734bdd3243de76d676329d21c | |
| parent | 787d2214c19bcc9b6ac48af0ce098277a801eded (diff) | |
Freezer: make kernel threads nonfreezable by default
Currently, the freezer treats all tasks as freezable, except for the kernel
threads that explicitly set the PF_NOFREEZE flag for themselves. This
approach is problematic, since it requires every kernel thread to either
set PF_NOFREEZE explicitly, or call try_to_freeze(), even if it doesn't
care for the freezing of tasks at all.
It seems better to only require the kernel threads that want to or need to
be frozen to use some freezer-related code and to remove any
freezer-related code from the other (nonfreezable) kernel threads, which is
done in this patch.
The patch causes all kernel threads to be nonfreezable by default (ie. to
have PF_NOFREEZE set by default) and introduces the set_freezable()
function that should be called by the freezable kernel threads in order to
unset PF_NOFREEZE. It also makes all of the currently freezable kernel
threads call set_freezable(), so it shouldn't cause any (intentional)
change of behaviour to appear. Additionally, it updates documentation to
describe the freezing of tasks more accurately.
[akpm@linux-foundation.org: build fixes]
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Acked-by: Nigel Cunningham <nigel@nigel.suspend2.net>
Cc: Pavel Machek <pavel@ucw.cz>
Cc: Oleg Nesterov <oleg@tv-sign.ru>
Cc: Gautham R Shenoy <ego@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
65 files changed, 256 insertions, 113 deletions
diff --git a/Documentation/power/freezing-of-tasks.txt b/Documentation/power/freezing-of-tasks.txt new file mode 100644 index 000000000000..af1a282c71a3 --- /dev/null +++ b/Documentation/power/freezing-of-tasks.txt | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | Freezing of tasks | ||
| 2 | (C) 2007 Rafael J. Wysocki <rjw@sisk.pl>, GPL | ||
| 3 | |||
| 4 | I. What is the freezing of tasks? | ||
| 5 | |||
| 6 | The freezing of tasks is a mechanism by which user space processes and some | ||
| 7 | kernel threads are controlled during hibernation or system-wide suspend (on some | ||
| 8 | architectures). | ||
| 9 | |||
| 10 | II. How does it work? | ||
| 11 | |||
| 12 | There are four per-task flags used for that, PF_NOFREEZE, PF_FROZEN, TIF_FREEZE | ||
| 13 | and PF_FREEZER_SKIP (the last one is auxiliary). The tasks that have | ||
| 14 | PF_NOFREEZE unset (all user space processes and some kernel threads) are | ||
| 15 | regarded as 'freezable' and treated in a special way before the system enters a | ||
| 16 | suspend state as well as before a hibernation image is created (in what follows | ||
| 17 | we only consider hibernation, but the description also applies to suspend). | ||
| 18 | |||
| 19 | 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 | sends a fake signal to each of them. A task that receives such a signal and has | ||
| 23 | TIF_FREEZE set, should react to it by calling the refrigerator() function | ||
| 24 | (defined in kernel/power/process.c), which sets the task's PF_FROZEN flag, | ||
| 25 | changes its state to TASK_UNINTERRUPTIBLE and makes it loop until PF_FROZEN is | ||
| 26 | cleared for it. Then, we say that the task is 'frozen' and therefore the set of | ||
| 27 | functions handling this mechanism is called 'the freezer' (these functions are | ||
| 28 | defined in kernel/power/process.c and include/linux/freezer.h). User space | ||
| 29 | processes are generally frozen before kernel threads. | ||
| 30 | |||
| 31 | It is not recommended to call refrigerator() directly. Instead, it is | ||
| 32 | recommended to use the try_to_freeze() function (defined in | ||
| 33 | include/linux/freezer.h), that checks the task's TIF_FREEZE flag and makes the | ||
| 34 | task enter refrigerator() if the flag is set. | ||
| 35 | |||
| 36 | For user space processes try_to_freeze() is called automatically from the | ||
| 37 | signal-handling code, but the freezable kernel threads need to call it | ||
| 38 | explicitly in suitable places. The code to do this may look like the following: | ||
| 39 | |||
| 40 | do { | ||
| 41 | hub_events(); | ||
| 42 | wait_event_interruptible(khubd_wait, | ||
| 43 | !list_empty(&hub_event_list)); | ||
| 44 | try_to_freeze(); | ||
| 45 | } while (!signal_pending(current)); | ||
| 46 | |||
| 47 | (from drivers/usb/core/hub.c::hub_thread()). | ||
| 48 | |||
| 49 | If a freezable kernel thread fails to call try_to_freeze() after the freezer has | ||
| 50 | set TIF_FREEZE for it, the freezing of tasks will fail and the entire | ||
| 51 | hibernation operation will be cancelled. For this reason, freezable kernel | ||
| 52 | threads must call try_to_freeze() somewhere. | ||
| 53 | |||
| 54 | After the system memory state has been restored from a hibernation image and | ||
| 55 | devices have been reinitialized, the function thaw_processes() is called in | ||
| 56 | order to clear the PF_FROZEN flag for each frozen task. Then, the tasks that | ||
| 57 | have been frozen leave refrigerator() and continue running. | ||
| 58 | |||
| 59 | III. Which kernel threads are freezable? | ||
| 60 | |||
| 61 | Kernel threads are not freezable by default. However, a kernel thread may clear | ||
| 62 | PF_NOFREEZE for itself by calling set_freezable() (the resetting of PF_NOFREEZE | ||
| 63 | directly is strongly discouraged). From this point it is regarded as freezable | ||
| 64 | and must call try_to_freeze() in a suitable place. | ||
| 65 | |||
| 66 | IV. Why do we do that? | ||
| 67 | |||
| 68 | Generally speaking, there is a couple of reasons to use the freezing of tasks: | ||
| 69 | |||
| 70 | 1. The principal reason is to prevent filesystems from being damaged after | ||
| 71 | hibernation. At the moment we have no simple means of checkpointing | ||
| 72 | filesystems, so if there are any modifications made to filesystem data and/or | ||
| 73 | metadata on disks, we cannot bring them back to the state from before the | ||
| 74 | modifications. At the same time each hibernation image contains some | ||
| 75 | filesystem-related information that must be consistent with the state of the | ||
| 76 | on-disk data and metadata after the system memory state has been restored from | ||
| 77 | the image (otherwise the filesystems will be damaged in a nasty way, usually | ||
| 78 | making them almost impossible to repair). We therefore freeze tasks that might | ||
| 79 | cause the on-disk filesystems' data and metadata to be modified after the | ||
| 80 | hibernation image has been created and before the system is finally powered off. | ||
