Suppress warning on resume attempt of explicitly frozen processes
Subsystems (3)
cover Suppress warning on resume attempt of explicitly frozen processes
Hi,
The following patches disable warning message for thawing attempts of processes that belong to a cgroup that has been intentionally frozen. This happens in the following example situation:
1. A cgroup is frozen by writing "FROZEN" to freezer.state cgroupfs file.
2. System goes to suspend.
3. System resumes from suspension.
4. The cgroup is thawed by writing "TWAWED" to freezer.state cgroupfs file.
On 3, kernel tries to thaw the frozen processes. However, the processes of the freezing cgroup should not be thawed in that step, but rather in 4, when the user requests it. That is the case and kernel skips thawing of the processes but it also warns about that it tried to thaw a process that is in freezing state. It seems that the warning is not necessary for the case explained above and the thawing of a process of freezing cgroup should be just skipped. This exact scenario is common in ChromeOS, where a separate daemon is used to freeze processes belonging to certain cgroups in specific order before systems go to suspend and then waking them up after system resumes.
The first patch of the series adds a function to check for the reason why the task is freezing. That function is then used by the second patch to suppress the warning for freezing cgroup's processes.
An alternative would be to simply add an "if (cgroup_freezing())" statement directly in __thaw_task(). However, cgroup_freezing() does not check if freezer is active (what the freezing() function does before calling cgroup_freezing()). That would still work as the freezer is actually active when __thaw_task() is called, but it would be more of a hack than a clean solution. By having a dedicated function for obtaining the task's freezing reason it can be done in a more clean way.
I would appreciate any comments or remarks.
Regards,
Michal
Michal Dubiel (2):
kernel/freezer: Add routine to read task's freezing reason kernel/freezer: Don't warn when thawing freezing cgroup processes
include/linux/freezer.h | 17 +++++++++++ kernel/freezer.c | 30 ++++++++++++++------ 2 files changed, 39 insertions(+), 8 deletions(-)
--
2.39.2.722.g9855ee24e9-goog
PATCH 1/2 kernel/freezer: Add routine to read task's freezing reason
There are few reasons why a task is freezing. It can be due to system suspend, it can be due to explicit user request, etc.
For most cases, the actual reason is not important, but there are some that it is useful to distinguish between what actually caused the task to freeze.
One example is when user requests freezing of tasks belonging to a certain cgroup. If for any reason, system goes suspend and resume when the cgroup is frozen, the already frozen tasks should not be thawed but remain frozen to ensure the suspend/resume cycle transparency. The system should not warn about skipping thawing of such processes as it is normal situation.
The proposed function allows to obtain information about the freezing reason and let the calling code decide what to do accordingly.
--- include/linux/freezer.h | 17 +++++++++++++++ kernel/freezer.c | 23 ++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-)
include/linux/freezer.h +17 -0
@@ -28,6 +28,23 @@ extern bool frozen(struct task_struct *p);extern bool freezing_slow_path (struct task_struct *p); +extern enum freezing_reason freezing_reason_slow_path (struct task_struct *p); + +enum freezing_reason { +FREEZING_NONE =0 , +FREEZING_CGROUP = (1 <<0 ), +FREEZING_PM = (1 <<1 ), +FREEZING_PM_NOSIG = (1 <<2 ), +}; + +static inline enum freezing_reason freezing_reason (struct task_struct *p) +{ +if (static_branch_unlikely (&freezer_active)) +return freezing_reason_slow_path (p); + +return FREEZING_NONE ; +} +/* * Check if there is a request to freeze a process */
kernel/freezer.c +16 -7
@@ -26,6 +26,21 @@ bool pm_nosig_freezing;/* protects freezing and frozen transitions */ static DEFINE_SPINLOCK (freezer_lock); +enum freezing_reason freezing_reason_slow_path (struct task_struct *p) +{ +if (pm_nosig_freezing) +return FREEZING_PM_NOSIG ; + +if (cgroup_freezing (p)) +return FREEZING_CGROUP ; + +if (pm_freezing && !(p->flags &PF_KTHREAD )) +return FREEZING_PM ; + +return FREEZING_NONE ; +} +EXPORT_SYMBOL (freezing_reason_slow_path); +/** * freezing_slow_path - slow path for testing whether a task needs to be frozen * @p: task to be tested
@@ -43,13 +58,7 @@ bool freezing_slow_path(struct task_struct *p)if (test_tsk_thread_flag (p,TIF_MEMDIE ))return false; -if (pm_nosig_freezing ||cgroup_freezing (p)) -return true; - -if (pm_freezing && !(p->flags &PF_KTHREAD )) -return true; - -return false; +return (bool )freezing_reason_slow_path (p); }EXPORT_SYMBOL (freezing_slow_path);
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
Rafael J. Wysocki
maintainerRe: [PATCH 1/2] kernel/freezer: Add routine to read task's freezing reason
Quoted 3 lines · Michal Dubiel
There are few reasons why a task is freezing. It can be due to system suspend, it can be due to explicit user request, etc.
Actually, there are two of them, either it is cgroup freezing, or it is freezing on system suspend AFAICS.
Quoted 4 lines
For most cases, the actual reason is not important, but there are some that it is useful to distinguish between what actually caused the task to freeze.
Quoted 8 lines
One example is when user requests freezing of tasks belonging to a certain cgroup. If for any reason, system goes suspend and resume when the cgroup is frozen, the already frozen tasks should not be thawed but remain frozen to ensure the suspend/resume cycle transparency. The system should not warn about skipping thawing of such processes as it is normal situation.
The proposed function allows to obtain information about the freezing reason and let the calling code decide what to do accordingly.
I honestly don't think that this new function is necessary.
Moreover, it would be better to combine this patch with the other one in the series.
Quoted 46 lines
Signed-off-by Michal Dubiel <[email protected]>--- include/linux/freezer.h | 17 +++++++++++++++ kernel/freezer.c | 23 ++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-)include/linux/freezer.h +17
-0@@ -28,6 +28,23 @@ extern bool frozen(struct task_struct *p);extern bool freezing_slow_path (struct task_struct *p); +extern enum freezing_reason freezing_reason_slow_path (struct task_struct *p); + +enum freezing_reason { +FREEZING_NONE =0 , +FREEZING_CGROUP = (1 <<0 ), +FREEZING_PM = (1 <<1 ), +FREEZING_PM_NOSIG = (1 <<2 ), +}; + +static inline enum freezing_reason freezing_reason (struct task_struct *p) +{ +if (static_branch_unlikely (&freezer_active)) +return freezing_reason_slow_path (p); + +return FREEZING_NONE ; +} +/* * Check if there is a request to freeze a process */ kernel/freezer.c +4
-0@@ -26,6 +26,21 @@ bool pm_nosig_freezing;/* protects freezing and frozen transitions */ static DEFINE_SPINLOCK (freezer_lock); +enum freezing_reason freezing_reason_slow_path (struct task_struct *p) +{ +if (pm_nosig_freezing) +return FREEZING_PM_NOSIG ;
kernel/freezer.c bool pm_nosig_freezing; · line 42
Quoted 10 lines
kernel/freezer.c +10
-0+ +if (cgroup_freezing (p)) +return FREEZING_CGROUP ; + +if (pm_freezing && !(p->flags &PF_KTHREAD )) +return FREEZING_PM ; + +return FREEZING_NONE ; +} +EXPORT_SYMBOL (freezing_reason_slow_path);
Why do you need to export this and why is it not EXPORT_SYMBOL_GPL()?
Quoted 6 lines
kernel/freezer.c +1
-0+/** * freezing_slow_path - slow path for testing whether a task needs to be frozen * @p: task to be tested @@ -43,13 +58,7 @@ bool freezing_slow_path(struct task_struct *p)if (test_tsk_thread_flag (p,TIF_MEMDIE ))
kernel/freezer.c bool freezing_slow_path(struct task_struct *p) · line 61
Quoted 10 lines
kernel/freezer.c +1
-7return false; -if (pm_nosig_freezing ||cgroup_freezing (p)) -return true; - -if (pm_freezing && !(p->flags &PF_KTHREAD )) -return true; - -return false; +return (bool )freezing_reason_slow_path (p);
This is the only call site of this function, so the explicit conversion to bool like this doesn't make any sense.
It should return bool instead.
Quoted 4 lines
kernel/freezer.c +0
-1}EXPORT_SYMBOL (freezing_slow_path); --
Re: [PATCH 1/2] kernel/freezer: Add routine to read task's freezing reason
Quoted 19 lines · Rafael J. Wysocki
Quoted 3 lines · Michal Dubiel
There are few reasons why a task is freezing. It can be due to system suspend, it can be due to explicit user request, etc.
Actually, there are two of them, either it is cgroup freezing, or it is freezing on system suspend AFAICS.
Quoted 10 lines
For most cases, the actual reason is not important, but there are some that it is useful to distinguish between what actually caused the task to freeze.
One example is when user requests freezing of tasks belonging to a certain cgroup. If for any reason, system goes suspend and resume when the cgroup is frozen, the already frozen tasks should not be thawed but remain frozen to ensure the suspend/resume cycle transparency. The system should not warn about skipping thawing of such processes as it is normal situation.
Quoted 4 lines
Quoted 2 lines
The proposed function allows to obtain information about the freezing reason and let the calling code decide what to do accordingly.
I honestly don't think that this new function is necessary.
This function (freezing_reason) is used in the second patch to differentiate between the freezing reason and suppress warnings for the tasks explicitly requested for freezing by the user.
Quoted 2 lines
Moreover, it would be better to combine this patch with the other one in the series.
I thought that separation into two patches makes sense as the first introduces a new, generic function in the kernel/freezer and the second one suppresses unnecessary warnings. The second patch is doing that by using the function introduced in the first patch, but that function is more generic and can potentially be used elsewhere so I thought there is a functional separation here, thus 2 patches. Nevertheless, if you prefer a single patch that is perfectly fine, just let me know I will squash the patches.
Quoted 42 lines
Quoted 42 lines
Signed-off-by Michal Dubiel <[email protected]>--- include/linux/freezer.h | 17 +++++++++++++++ kernel/freezer.c | 23 ++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-)include/linux/freezer.h +17
-0@@ -28,6 +28,23 @@ extern bool frozen(struct task_struct *p);extern bool freezing_slow_path (struct task_struct *p); +extern enum freezing_reason freezing_reason_slow_path (struct task_struct *p); + +enum freezing_reason { +FREEZING_NONE =0 , +FREEZING_CGROUP = (1 <<0 ), +FREEZING_PM = (1 <<1 ), +FREEZING_PM_NOSIG = (1 <<2 ), +}; + +static inline enum freezing_reason freezing_reason (struct task_struct *p) +{ +if (static_branch_unlikely (&freezer_active)) +return freezing_reason_slow_path (p); + +return FREEZING_NONE ; +} +/* * Check if there is a request to freeze a process */ kernel/freezer.c +0
-0@@ -26,6 +26,21 @@ bool pm_nosig_freezing;/* protects freezing and frozen transitions */ static DEFINE_SPINLOCK (freezer_lock);
Quoted 16 lines
Quoted 4 lines
kernel/freezer.c +4
-0+enum freezing_reason freezing_reason_slow_path (struct task_struct *p) +{ +if (pm_nosig_freezing) +return FREEZING_PM_NOSIG ;kernel/freezer.c bool pm_nosig_freezing; · line 42
Quoted 10 lines
kernel/freezer.c +10
-0+ +if (cgroup_freezing (p)) +return FREEZING_CGROUP ; + +if (pm_freezing && !(p->flags &PF_KTHREAD )) +return FREEZING_PM ; + +return FREEZING_NONE ; +} +EXPORT_SYMBOL (freezing_reason_slow_path);Why do you need to export this and why is it not
EXPORT_SYMBOL_GPL()?
I considered that function a part of the kernel/freezer external interface, similarly as the existing freezing_slow_path() is. Both the existing freezing_slow_path() and the newly introduced freezing_reason_slow_path() are currently not used outside of kernel/freezer.c by any intree modules, but they potentially could be and since the existing freezing_slow_path() already had been exported I opted for being consistent and export (using the same EXPORT_SYMBOL() as the rest of the entire file) freezing_reason_slow_path() as well. However, if you think that's not necessary or wrong I am happy to remove that. Please let me know.
Quoted 20 lines
Quoted 6 lines
kernel/freezer.c +1
-0+/** * freezing_slow_path - slow path for testing whether a task needs to be frozen * @p: task to be tested @@ -43,13 +58,7 @@ bool freezing_slow_path(struct task_struct *p)if (test_tsk_thread_flag (p,TIF_MEMDIE ))kernel/freezer.c bool freezing_slow_path(struct task_struct *p) · line 61
Quoted 10 lines
kernel/freezer.c +1
-7return false; -if (pm_nosig_freezing ||cgroup_freezing (p)) -return true; - -if (pm_freezing && !(p->flags &PF_KTHREAD )) -return true; - -return false; +return (bool )freezing_reason_slow_path (p);This is the only call site of this function, so the explicit conversion to bool like this doesn't make any sense.
Quoted 1 line
It should return bool instead.
I am not sure if I got the comment right, please correct me if that's the case. I introduced freezeing_reason_slow_path() explicitly to return more information than just bool (freezing, not freezing) in order to be able to differentiate between the causes of freeze (please note there is another call site of the function in freezeing_reason() which uses that additional information). This call of the function here in freezing_slow_path() indeed does not need that additional info. However, in order to not duplicate identical code that checks for freezing we can call the freezing_reason_slow_path() and just return whether it is freezing or not regardless of the actual reason. That way the freezing_slow_path() functionality is not altered in any way and we don't introduce code duplication.
Quoted 4 lines
Quoted 4 lines
kernel/freezer.c +0
-1}EXPORT_SYMBOL (freezing_slow_path); --
PATCH 2/2 kernel/freezer: Don't warn when thawing freezing cgroup processes
In reply to Michal DubielCurrently, kernel warns when __thaw_task is called on a task that is freezing for whatever the reason.
However, this can produce unnecessary warning in situations when some cgroup has been frozen by writing FREEZE to /sys/fs/cgroup/freezer/*/freezer.state, system suspends and then resumes.
Such situation happens for example in ChromeOS, where we have daemon for freezing cgroups in certain order before suspending the system and then thawing them back after system resumes.
The freezing_reason() can be used to obtain the reason why the task is freezing and don't warn for tasks explicitly requested to freeze by the user.
--- kernel/freezer.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/freezer.c +6 -1
@@ -203,9 +203,14 @@ static int __set_task_special(struct task_struct *p, void *arg)void __thaw_task (struct task_struct *p) {unsigned long flags, flags2; +enum freezing_reason reason;spin_lock_irqsave (&freezer_lock, flags); -if (WARN_ON_ONCE (freezing (p))) + + reason =freezing_reason (p); +if (reason ==FREEZING_CGROUP ) + goto unlock; +else if (WARN_ON_ONCE (reason !=FREEZING_NONE )) goto unlock;if (lock_task_sighand (p, &flags2)) {
Signature · 1 line
2.39.2.722.g9855ee24e9-goog
Quoted 10 lines · Michal Dubiel
Hi,
The following patches disable warning message for thawing attempts of processes that belong to a cgroup that has been intentionally frozen. This happens in the following example situation:
1. A cgroup is frozen by writing "FROZEN" to freezer.state cgroupfs file.
2. System goes to suspend.
3. System resumes from suspension.
4. The cgroup is thawed by writing "TWAWED" to freezer.state cgroupfs file.
Quoted 2 lines
On 3, kernel tries to thaw the frozen processes. However, the processes of the freezing cgroup should not be thawed in that step, but rather in 4,
Isn't the core issue here that processes frozen from user space and tasks frozen due to a PM transition are not in the same state? That is instead of inventing substates for a specific state you should have a new state?
Regards
Oliver
Quoted 12 lines · Oliver Neukum
Quoted 10 lines · Michal Dubiel
Hi,
The following patches disable warning message for thawing attempts of processes that belong to a cgroup that has been intentionally frozen. This happens in the following example situation:
1. A cgroup is frozen by writing "FROZEN" to freezer.state cgroupfs file.
2. System goes to suspend.
3. System resumes from suspension.
4. The cgroup is thawed by writing "TWAWED" to freezer.state cgroupfs file.
Quoted 7 lines
Quoted 2 lines
On 3, kernel tries to thaw the frozen processes. However, the processes of the freezing cgroup should not be thawed in that step, but rather in 4,
Isn't the core issue here that processes frozen from user space and tasks frozen due to a PM transition are not in the same state? That is instead of inventing substates for a specific state you should have a new state?
Please correct me if I misunderstood your comment. I believe that effectively we already have a separate states for task freezing due to PM transition and due to userspace requests and the kernel is able to differentiate between the two (e.g. there is cgroup_freezing() function to check for that).
The issue was that this information was not used when thawing the processes on the system resume. The used freezing() function returns true for tasks that are freezing for any reason. I've proposed another function that returns information about the reason why the task is freezing so that the information can be used and acted accordingly when needed, without breaking the existing freezing() function operation.
Regards,
Michal
Quoted 7 lines · Michal Dubiel
Quoted 4 lines · Oliver Neukum
Isn't the core issue here that processes frozen from user space and tasks frozen due to a PM transition are not in the same state? That is instead of inventing substates for a specific state you should have a new state?
Quoted 5 lines
Please correct me if I misunderstood your comment. I believe that effectively we already have a separate states for task freezing due to PM transition and due to userspace requests and the kernel is able to differentiate between the two (e.g. there is
cgroup_freezing()function to check for that).
Correct. Hence there should be two states also formally.
Regards
Oliver
Quoted 11 lines · Oliver Neukum
Quoted 7 lines · Michal Dubiel
Quoted 4 lines · Oliver Neukum
Isn't the core issue here that processes frozen from user space and tasks frozen due to a PM transition are not in the same state? That is instead of inventing substates for a specific state you should have a new state?
Quoted 7 lines
Quoted 5 lines
Please correct me if I misunderstood your comment. I believe that effectively we already have a separate states for task freezing due to PM transition and due to userspace requests and the kernel is able to differentiate between the two (e.g. there is
cgroup_freezing()function to check for that).Correct. Hence there should be two states also formally.
Do you suggest introducing another TASK_* define for task_struct's __state field, which would differentiate between userspace and pm transition initiated freezes?
Currently, the cgroup freezing state is encapsulated in kernel/cgroup/ sources. I believe it is right to keep the cgroup state internals there, and expose only an interface like cgroup_freezing() to other modules. This is what the freezing() function from freezer.h does. it checks cgroup_freezing() and other reasons like pm_freezing to return information whether a task is freezing or not. I've only extended that a bit to have an option to get information about the reason causing the freezing in addition to a boolean freezing/not freezing info existing currently.
Regards,
Michal