もともと、Linux Kernelのソースコードの一部なので、GPLv2扱いになる(はずの認識)。
https://www.kernel.org/doc/html/latest/index.html
Licensing documentation
The following describes the license of the Linux kernel source code (GPLv2), how to properly mark the license of individual files in the source tree, as well as links to the full license text.
https://www.kernel.org/doc/html/latest/process/license-rules.html#kernel-licensing
Docs » Power Management » Freezing of tasks
Freezing of tasks
2007 Rafael J. Wysocki <rjw@sisk.pl>, GPL
I. What is the freezing of tasks? ( taskのfreezingとは何か?)
The freezing of tasks is a mechanism by which user space processes and some kernel threads are controlled during hibernation or system-wide suspend (on some architectures).
taskのfreezingとは、ユーザー空間プロセスといくつかのkernel threadを、hibernationもしくは(いくつかのarchitectureにおいて)system-wide syspendする間に制御する事です。
II. How does it work?(どのように動作するのか?)
There are three per-task flags used for that, PF_NOFREEZE, PF_FROZEN and PF_FREEZER_SKIP (the last one is auxiliary).
3種類のtask事のフラグが用いられます。PF_NOFREEZE, PF_FROZEN, PF_FREEZER_SKIPです(最期の1つは補助的なものです)。
The tasks that have 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 suspend state as well as before a hibernation image is created
PF_NOFREEZEが設定されていないtask(すべてのユーザー空間プロセスと、いくつかのkernel thread)は、"freezable"とみなされます。それらは、システムがhibernation imageを生成する前のsyspend stateに移行する前に、特別な方法で処理されます。
(in what follows we only consider hibernation, but the description also applies to suspend).
(以下では、hibernationについてだけ考慮していますが、suspendに対しても記載を適用できます)
.
Namely, as the first step of the hibernation procedure the function freeze_processes() (defined in kernel/power/process.c) is called.
つまり、hibernation procedureの最初のステップとして、freeze_processes()関数が呼ばれます(kernel/power/process.cに記述されていまsう)。
A system-wide variable system_freezing_cnt (as opposed to a per-task flag) is used to indicate whether the system is to undergo a freezing operation.
system-wide variableであるsystem_freezing_cntが(task単位のflagではなくて)、systemがfreezing operationを受けるかどうかを表現するために使われます。
And freeze_processes() sets this variable.
freeze_processes()は、この変数を設定ます。
After this, it executes try_to_freeze_tasks() that sends a fake signal to all user space processes, and wakes up all the kernel threads.
この後、try_to_freeze_tasks()が実行されることで、全てのユーザー空間プロセスと、全てのkernel threadに対して、疑似シグナルが送信されます。
All freezable tasks must react to that by calling try_to_freeze(), which results in a call to __refrigerator() (defined in kernel/freezer.c), which sets the task’s PF_FROZEN flag, changes its state to TASK_UNINTERRUPTIBLE and makes it loop until PF_FROZEN is cleared for it.
全てのfreezableなタスクは、try_to_freeze()を呼び出す事によって、反応しなければなりません。これによって、taskのPF_FROZEN flagが設定し、taskの状態をTASK_UNTERRRUPTIBLEに変更し、そして、PF_FROZENがクリアされるまでの間loopします。
Then, we say that the task is ‘frozen’ and therefore the set of functions handling this mechanism is referred to as ‘the freezer’
そして、taskはfrozen
されるために、このメカニズムを処理する関数の集合は、"the freezer"と称されます。
(these functions are defined in kernel/power/process.c, kernel/freezer.c & include/linux/freezer.h).
(これらの関数は、kernel/power/process.c, kernel/freezer.c と include/linux/freezer.hに記述されています)
User space processes are generally frozen before kernel threads
ユーザ-空間プロセスは、kernel threadの前に、基本的には凍結されます。.
.
__refrigerator() must not be called directly.
__refrigerator()を直接呼び出すことはできません。
Instead, use the try_to_freeze() function (defined in include/linux/freezer.h), that checks if the task is to be frozen and makes the task enter __refrigerator().
その代わりに、try_yo_freeze()関数を使ってください(include/linux/freezer.hで定義されています)これは、もし佐が統括されるべきで、taskが__refrigerator()に移行するかをチェックします。
.
For user space processes try_to_freeze() is called automatically from the signal-handling code, but the freezable kernel threads need to call it explicitly in suitable places or use the wait_event_freezable() or wait_event_freezable_timeout() macros (defined in include/linux/freezer.h) that combine interruptible sleep with checking if the task is to be frozen and calling try_to_freeze().
ユーザー空間プロセスでは、try_yo_freeze()はシグナルハンドリングコードから自動的に呼ばれます。しかし、freezable kernel threadでは、適切な場所で明示的に呼び出すか、wait_event_freezable()もしくはwait_event_freezable_timeout() macroを用いる必要があります(include/linux/freezer.hで定義)。これは、割り込み可能なスリープで、タスクが凍結するべきかを判断し、try_to_freeze()を呼び出すかどうかを判定するためのマクロです。
The main loop of a freezable kernel thread may look like the following one:
freezableカーネルスレっとのmain looopは下記のような形になります。
set_freezable();
do {
hub_events();
wait_event_freezable(khubd_wait,
!list_empty(&hub_event_list) ||
kthread_should_stop());
} while (!kthread_should_stop() || !list_empty(&hub_event_list));
(from drivers/usb/core/hub.c::hub_thread()).
If a freezable kernel thread fails to call try_to_freeze() after the freezer has initiated a freezing operation, the freezing of tasks will fail and the entire hibernation operation will be cancelled.
freezablekernel threadは、freezerがfreezing処理を初期化した後に、try_to_freeze()をよびだすと失敗します。taskのfreezingは失敗し、hibernation処理への移行はキャンセルされます。
For this reason, freezable kernel threads must call try_to_freeze() somewhere or use one of the wait_event_freezable() and wait_event_freezable_timeout() macros.
この理由から、freezable kernel threadはいずれかでtry_to_freeze()を呼び出すか、wait_event_freezable() and wait_event_freezable_timeout() macrosのいずれか1つを使わなければなりません。
.
After the system memory state has been restored from a hibernation image and devices have been reinitialized, the function thaw_processes() is called in order to clear the PF_FROZEN flag for each frozen task. Then, the tasks that have been frozen leave __refrigerator() and continue running.
システムメモリの状態がhibernation imageから復帰し、デバイスが再初期化できたら、thaw_processes()が呼ばれます。これは、それぞれの凍結済みのtaskにおいてPF_FROZEN flagをクリアします。この結果、taskは__refrigerator()から離れ、継続して処理ができます。.
Rationale behind the functions dealing with freezing and thawing of tasks
タスクのfreezingとthawingにおける関係しは以下です。
freeze_processes():
freezes only userspace tasks
ユーザー空間のtaskだけを凍結します。
freeze_kernel_threads():
freezes all tasks (including kernel threads) because we can’t freeze kernel threads without freezing userspace tasks
kernel threadを含めた、全タスクを凍結します。なぜならば、ユーザー空間タスクの凍結無し、kernel threadの凍結はできません。
thaw_kernel_threads():
thaws only kernel threads; this is particularly useful if we need to do anything special in between thawing of kernel threads and thawing of userspace tasks, or if we want to postpone the thawing of userspace tasks
kernel threadを復元します。これは、kernel threadの解凍と、ユーザー空間タスクの解凍の間で、特別なことをする場合、あるいは、ユーザー空間タスクの解凍を延期する場合に有益です。
thaw_processes():
thaws all tasks (including kernel threads) because we can’t thaw userspace tasks without thawing kernel threads
kernel threadを含めた、全タスクを回答します。なぜならば、kernel threadの解凍無しに、ユーザー空間タスクの解凍はできません。
III. Which kernel threads are freezable? (どこカーネルスレッドが凍結可能なのか?)
Kernel threads are not freezable by default.
kernel threadはデフォルトでは、freezableではありません。
However, a kernel thread may clear PF_NOFREEZE for itself by calling set_freezable() (the resetting of PF_NOFREEZE directly is not allowed).
しかし、kernel threadが、set_freezable()を呼ぶことで自分自身のPF_NOFREEZEを消すことができます。(PF_NOFREEZEを再設定を直接行うことは許可されていません)。
From this point it is regarded as freezable and must call try_to_freeze() in a suitable place.
この時点から、freezableとみなされ、適切な場所で、try_to_freeze()を呼び出さなければなりません。
.
IV. Why do we do that?(なぜこれをしなければならないのか)
Generally speaking, there is a couple of reasons to use the freezing of tasks:
一般的な言い方をすれば、taskのfreezingを使わなければならないいくつかの理由がある。
.
The principal reason is to prevent filesystems from being damaged after hibernation.
主な理由は、hibernation後のファイルシステムへの損傷を防ぐためです。
At the moment we have no simple means of checkpointing filesystems, so if there are any modifications made to filesystem data and/or metadata on disks, we cannot bring them back to the state from before the modifications.
現時点で、ファイルシステムのチェックポイントを設定する簡単な方法はありません。そのため、ディスク上のdataやmetadataに変更を加えた場合、元の状態にもどすことはできません。
At the same time each hibernation image contains some filesystem-related information that must be consistent with the state of the on-disk data and metadata after the system memory state has been restored from the image
この時、各hibernation imageには、システムメモリの状態がイメージから復元された後、ディスク上のデータとメタデータの状態と一致する必要があるファイルシステム関連の情報が含まれています
(otherwise the filesystems will be damaged in a nasty way, usually making them almost impossible to repair).
(それ以外では、ファイルシステムは面倒な方法によって損傷し、通常は復旧することがほぼ不可能となります)。
We therefore freeze tasks that might cause the on-disk filesystems’ data and metadata to be modified after the hibernation image has been created and before the system is finally powered off.
したがって、hibernation imageが作成されて、システムが最終的に電源が切られる前の、ディスク上のファイルシステムのdataとmetadataの変更する可能性のあるtaskが凍結されます。
The majority of these are user space processes, but if any of the kernel threads may cause something like this to happen, they have to be freezable.
これらの多くは、ユーザー空間プロセスですが、これを引き起こすような何かを実行するkernel threadがあれば、これらもfreezableです。
.
Next, to create the hibernation image we need to free a sufficient amount of memory (approximately 50% of available RAM) and we need to do that before devices are deactivated, because we generally need them for swapping out.
次に、hibernation imageを作るためには、十分な量のメモリを開放する必要があります(おおよそ、有効なRAMの50%)。デバイスを非アクティブ化する前にやらなければなりません。なぜならば、swapping outするためには必要になるためです。
Then, after the memory for the image has been freed, we don’t want tasks to allocate additional memory and we prevent them from doing that by freezing them earlier.
よって、imageのためのmemoryが解放された後、更なるメモリをtaskに要求されないように、より早期にfreezingすることで防ぎます。
[Of course, this also means that device drivers should not allocate substantial amounts of memory from their .suspend() callbacks before hibernation, but this is a separate issue.]
[もちろん、これはデバイスドライバ―がhibernationになる前に、それぞれのsuspend() callbackから大量のメモリを割り当てるべきでないことw意味します。これは別問題です]
.
The third reason is to prevent user space processes and some kernel threads from interfering with the suspending and resuming of devices.
三番目の理由は、ユーザー空間プロセスといくつかのkernel threadが、デバイスのsuspendとresumeを妨げないようにするためです。
A user space process running on a second CPU while we are suspending devices may, for example, be troublesome and without the freezing of tasks we would need some safeguards against race conditions that might occur in such a case.
例えば、デバイスをsuspendさせようとしているときに、2番目のCPUで動作しているユーザー空間プロセスは、面倒な場合があります。taskのfreezingをしないと、そのようなケースを引き起こす競合状態に対する保護策が必要になります。
.
Although Linus Torvalds doesn’t like the freezing of tasks, he said this in one of the discussions on LKML (http://lkml.org/lkml/2007/4/27/608):
Linus Torvalds氏はタスクの凍結を嫌っています。LKMLに関する議論の1つでこれを述べました (http://lkml.org/lkml/2007/4/27/608)
.
“RJW:> Why we freeze tasks at all or why we freeze kernel threads? (なぜすべてのタスクを、あるいは、なぜkernel threadを凍結するのか?)
Linus: In many ways, ‘at all’.(多くの場合、
すべて
)
I do realize the IO request queue issues, and that we cannot actually do s2ram with some devices in the middle of a DMA.私はIO request問題を理解しています。また、DMA途中にあるデバイスと共に、s2ramすることもできません。
So we want to be able to avoid that, there’s no question about that.
だから、これを避けるために、これについては疑問の余地はありません。
And I suspect that stopping user threads and then waiting for a sync is practically one of the easier ways to do so.
そして、ユーザースレッドを停止してから、同期のために待機する事は、実際には簡単な手段の1つです。
So in practice, the ‘at all’ may become a ‘why freeze kernel threads?’ and freezing user threads I don’t find really objectionable.”
したがって、実際には‘at all'が「なぜカーネルスレッドをフリーズするのか」になり、ユーザースレッドをフリーズすることになり、私が本当に不愉快なことだとは思いません。
.
Still, there are kernel threads that may want to be freezable.
それでも、freezableにしたいkernel threadがあります。
For example, if a kernel thread that belongs to a device driver accesses the device directly, it in principle needs to know when the device is suspended, so that it doesn’t try to access it at that time.
例えば、kernel threadがdevice driverに属し、deviceに直接アクセスする場合、デバイスがsuspendに入るときであるかを知る必要があり、その時点でデバイスにアクセスしないようにしなければなりません。
However, if the kernel thread is freezable, it will be frozen before the driver’s .suspend() callback is executed and it will be thawed after the driver’s .resume() callback has run, so it won’t be accessing the device while it’s suspended.
しかし、kernel threadがfreezableであれば、driverのsuspend() callbackが実行される前に凍結されます。そして、driverのresume() callbackが実行される前に回答されます。suspend中においてデバイスにアクセスすることが亡くなります。
Another reason for freezing tasks is to prevent user space processes from realizing that hibernation (or suspend) operation takes place.
タスクを凍結するもう1つの理由は、ユーザー空間プロセスがhibernation(もしくはsuspend)処理が行われていることに気付かないようにするためです。
Ideally, user space processes should not notice that such a system-wide operation has occurred and should continue running without any problems after the restore (or resume from suspend).
理想的には、ユーザー空間プロセスは、そのようなsystem-wide operatioが発生していることを知らずに、restore(あるいは、suspendからのresume)をした後に問題なく動作継続できるべきです。
Unfortunately, in the most general case this is quite difficult to achieve without the freezing of tasks.
残念ながら、最も一般的なケースでは、タスクをフリーズせずにこれを実現することは非常に難しいです。。
Consider, for example, a process that depends on all CPUs being online while it’s running.
たとえば、実行中にonlineになっているすべてのCPUに依存するプロセスを考えてみます。
Since we need to disable nonboot CPUs during the hibernation, if this process is not frozen, it may notice that the number of CPUs has changed and may start to work incorrectly because of that.
hibernation中に非起動CPUを停止させたい場合、そのprocessを凍結できません。それは、CPUの数が変化した事に気が付き、正しく動作できなくなります。