freezer: check OOM kill while being frozen
Subsystems (5)
PATCH v2 1/1 freezer: check OOM kill while being frozen
There is a race condition between OOM killer and freezer when they try to operate on the same process, something like below:
Process A Process B Process C
trigger page fault
then trigger oom
B=oom_scan_process_thread()
cgroup freezer freeze(A, B)
...
try_to_freeze()
stay in D stateoom_kill_process(B)
restart page fault
...
In this case, process A triggered a page fault in user-space, and the kernel page fault handler triggered OOM, then kernel selected process B as the victim, right before being killed process B was frozen by process C therefore went to D state, then kernel sent SIGKILL but it is already too late as process B will not care about pending signals any more.
David Rientjes tried to fix same issue with commit f660daac474c6f (oom: thaw threads if oom killed thread is frozen before deferring) but it doesn't work any more, because __thaw_task() just checks if it's frozen and then wakes it up, but the frozen task, after waking up, will check if freezing() is still true and continue to freeze itself if so. __thaw_task() can't make freezing() return false since it doesn't change any of these conditions, especially cgroup_freezing().
Fix this straightly by checking if the frozen process itself has been killed by OOM killer, so that the frozen process will recover itself and be killed finally.
--- kernel/freezer.c | 19 +++++++++++-------- mm/oom_kill.c | 2 -- 2 files changed, 11 insertions(+), 10 deletions(-)
kernel/freezer.c +11 -8
@@ -52,6 +52,16 @@ bool freezing_slow_path(struct task_struct *p) }EXPORT_SYMBOL (freezing_slow_path); +static bool i_should_thaw_myself (bool check_kthr_stop) +{ +if (!freezing (current) || + (check_kthr_stop &&kthread_should_stop ()) || +test_thread_flag (TIF_MEMDIE )) +return true; +else +return false; +} +/* Refrigerator is place where frozen processes are stored :-). */ bool __refrigerator (bool check_kthr_stop) {
@@ -67,8 +77,7 @@ bool __refrigerator(bool check_kthr_stop)spin_lock_irq (&freezer_lock); current->flags |=PF_FROZEN ; -if (!freezing (current) || - (check_kthr_stop &&kthread_should_stop ())) +if (i_should_thaw_myself (check_kthr_stop)) current->flags &= ~PF_FROZEN ;spin_unlock_irq (&freezer_lock);
@@ -147,12 +156,6 @@ void __thaw_task(struct task_struct *p) {unsigned long flags; -/* -* Clear freezing and kick @p if FROZEN. Clearing is guaranteed to -* be visible to @p as waking up implies wmb. Waking up inside -* freezer_lock also prevents wakeups from leaking outside -* refrigerator. -*/ spin_lock_irqsave (&freezer_lock, flags);if (frozen (p))wake_up_process (p);
mm/oom_kill.c +0 -2
@@ -266,8 +266,6 @@ enum oom_scan_t oom_scan_process_thread(struct task_struct *task,* Don't allow any other task to have access to the reserves. */ if (test_tsk_thread_flag (task,TIF_MEMDIE )) { -if (unlikely (frozen (task))) -__thaw_task (task);if (!force_kill)return OOM_SCAN_ABORT ; }
Signature · 1 line
1.8.3.1
PATCH 1/1 x86,mm: check freeze request in page fault handler
When a process triggers a page fault and kernel keeps trying to retry the fault, there is no chance for this process to be frozen, so the freeze request will always be pending.
This patch lets the page fault handler check pending freeze request and freeze current process if so.
--- arch/x86/mm/fault.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/mm/fault.c +4 -0
@@ -14,6 +14,7 @@#include <linux/hugetlb.h> /* hstate_index_to_shift */ #include <linux/prefetch.h> /* prefetchw */ #include <linux/context_tracking.h> /* exception_enter(), ... */ +#include <linux/freezer.h> /* try_to_freeze() */ #include <asm/traps.h> /* dotraplinkage, ... */ #include <asm/pgalloc.h> /* pgd_*(), ... */
@@ -885,6 +886,9 @@ mm_fault_error(struct pt_regs *regs, unsigned long error_code,up_read (¤t->mm->mmap_sem);no_context (regs, error_code, address,0 ,0 );return ; + }else if (signal_pending (current) && (error_code &PF_USER )) { +if (try_to_freeze ()) +return ; }if (fault &VM_FAULT_OOM ) {
Signature · 1 line
1.8.3.1
Re: [Patch] x86,mm: check freeze request in page fault handler
Quoted 3 lines · Cong Wang
When a process triggers a page fault and kernel keeps trying to retry the fault, there is no chance for this process to be frozen, so the freeze request will always be pending.
The retry cannot happen indefinitely, no?
Besides that the patch is broken in at least 2 ways. You are not releasing mmap_sem and this will break memcg OOM killer handling.
If a memcg is under OOM (because of hard limit) then try_charge calls mem_cgroup_oom which marks the current task with OOM information. Notably takes a reference to memcg->css. The charge fail will then gets up the pagefault stack until we get to mm_fault_error where you put the task into freezer and then returns without pagefault_out_of_memory which would handle memcg specific parts in mem_cgroup_oom_synchronize. If the task wakes up and the page fault retry succeeds (because some charges were released in the meantime) then you leak a reference to memcg->css.
Besides that the whole change would need a better justification. Why other archs do not need this?
Quoted 40 lines
This patch lets the page fault handler check pending freeze request and freeze current process if so.
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc David Rientjes <[email protected]>Cc Michal Hocko <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Cc Tejun Heo <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Cong Wang <[email protected]>--- arch/x86/mm/fault.c | 4 ++++ 1 file changed, 4 insertions(+)arch/x86/mm/fault.c +4
-0@@ -14,6 +14,7 @@#include <linux/hugetlb.h> /* hstate_index_to_shift */ #include <linux/prefetch.h> /* prefetchw */ #include <linux/context_tracking.h> /* exception_enter(), ... */ +#include <linux/freezer.h> /* try_to_freeze() */ #include <asm/traps.h> /* dotraplinkage, ... */ #include <asm/pgalloc.h> /* pgd_*(), ... */ @@ -885,6 +886,9 @@ mm_fault_error(struct pt_regs *regs, unsigned long error_code,up_read (¤t->mm->mmap_sem);no_context (regs, error_code, address,0 ,0 );return ; + }else if (signal_pending (current) && (error_code &PF_USER )) { +if (try_to_freeze ()) +return ; }if (fault &VM_FAULT_OOM ) {Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [Patch] x86,mm: check freeze request in page fault handler
Quoted 9 lines · Michal Hocko
Quoted 3 lines · Cong Wang
When a process triggers a page fault and kernel keeps trying to retry the fault, there is no chance for this process to be frozen, so the freeze request will always be pending.
The retry cannot happen indefinitely, no?
Besides that the patch is broken in at least 2 ways. You are not releasing
mmap_semand this will break memcg OOM killer handling.
Ah, it's my bad to miss the up_read().
Quoted 10 lines
If a memcg is under OOM (because of hard limit) then
try_chargecallsmem_cgroup_oomwhich marks the current task with OOM information. Notably takes a reference tomemcg->css. The charge fail will then gets up the pagefault stack until we get tomm_fault_errorwhere you put the task into freezer and then returns withoutpagefault_out_of_memorywhich would handle memcg specific parts inmem_cgroup_oom_synchronize. If the task wakes up and the page fault retry succeeds (because some charges were released in the meantime) then you leak a reference tomemcg->css.
OK, I thought skipping pagefault_out_of_memory() is safe, you are right here, thanks for explanation.
Quoted 4 lines
Besides that the whole change would need a better justification. Why other archs do not need this?
They probably need this as well, just that I only have x86 to care and test. :)
Does the following updated patch make any sense to you? If not, I will just drop it.
arch/x86/mm/fault.c +1 -0
@@ -14,6 +14,7 @@#include <linux/hugetlb.h> /* hstate_index_to_shift */ #include <linux/prefetch.h> /* prefetchw */ #include <linux/context_tracking.h> /* exception_enter(), ... */ +#include <linux/freezer.h> /* try_to_freeze() */ #include <asm/traps.h> /* dotraplinkage, ... */ #include <asm/pgalloc.h> /* pgd_*(), ... */
@@ -904,6 +905,9 @@ mm_fault_error(struct pt_regs *regs, unsigned long
error_code,
* oom-killed):
*/
pagefault_out_of_memory();+
+ if (signal_pending(current))
+ try_to_freeze();
} else {
if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
VM_FAULT_HWPOISON_LARGE))
Re: [Patch] x86,mm: check freeze request in page fault handler
On Tue 12-08-14 17:47:02, Cong Wang wrote:
[...]
Quoted 2 lines
Does the following updated patch make any sense to you? If not, I will just drop it.
Not really to be honest. I do not see what problem you are trying to fix.
Quoted 23 lines
arch/x86/mm/fault.c +4
-0@@ -14,6 +14,7 @@#include <linux/hugetlb.h> /* hstate_index_to_shift */ #include <linux/prefetch.h> /* prefetchw */ #include <linux/context_tracking.h> /* exception_enter(), ... */ +#include <linux/freezer.h> /* try_to_freeze() */ #include <asm/traps.h> /* dotraplinkage, ... */ #include <asm/pgalloc.h> /* pgd_*(), ... */ @@ -904,6 +905,9 @@ mm_fault_error(struct pt_regs *regs, unsigned longerror_code, * oom-killed): */ pagefault_out_of_memory (); + +if (signal_pending (current)) +try_to_freeze (); }else { if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
VM_FAULT_HWPOISON_LARGE))
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [Patch] x86,mm: check freeze request in page fault handler
Quoted 7 lines · Michal Hocko
On Tue 12-08-14 17:47:02, Cong Wang wrote:
[...]Quoted 2 lines
Does the following updated patch make any sense to you? If not, I will just drop it.
Not really to be honest. I do not see what problem you are trying to fix.
In my case, a page faulted process triggered OOM, kernel kept retrying this fault since without my fix oom killer couldn't kill a frozen process.
This led to that this process is always running even after we tried to freeze it,
the whole memory cgroup just stayed in FREEZING state.
Although with my fix, OOM will kill the frozen process so that the page faulted process could probably stop retrying the fault finally, the question is that is OOM
the only case blocks page fault? Looking at the code, there could be other case blocking page fault handler as well, so instead of keep retrying for unknown times
with ignoring freeze request, why not just freeze it since this is not dangerous?
Or I am missing anything?
Re: [Patch] x86,mm: check freeze request in page fault handler
Quoted 15 lines · Cong Wang
Quoted 7 lines · Michal Hocko
On Tue 12-08-14 17:47:02, Cong Wang wrote:
[...]Quoted 2 lines
Does the following updated patch make any sense to you? If not, I will just drop it.
Not really to be honest. I do not see what problem you are trying to fix.
In my case, a page faulted process triggered OOM, kernel kept retrying this fault since without my fix oom killer couldn't kill a frozen process.
This led to that this process is always running even after we tried to freeze it, the whole memory cgroup just stayed in FREEZING state.
Quoted 6 lines
Although with my fix, OOM will kill the frozen process so that the page faulted process could probably stop retrying the fault finally, the question is that is OOM the only case blocks page fault? Looking at the code, there could be other case blocking page fault handler as well, so instead of keep retrying for unknown times with ignoring freeze request, why not just freeze it since this is not dangerous?
I do not think that spreading freezing points is a good idea. The task can be blocked during page fault for many other reasons so this point doesn't solve anything IMO.
Quoted 1 line
Or I am missing anything?
Page fault shouldn't retry too many times without returning to userspace.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 22 lines · Cong Wang
There is a race condition between OOM killer and freezer when they try to operate on the same process, something like below:
Process A Process B Process C
trigger page fault
then trigger oom
B=oom_scan_process_thread()
cgroup freezerfreeze(A, B)
...
try_to_freeze()
stay in D stateoom_kill_process(B)
restart page fault
...In this case, process A triggered a page fault in user-space, and the kernel page fault handler triggered OOM, then kernel selected process B as the victim, right before being killed process B was frozen by process C therefore went to D state, then kernel sent SIGKILL but it is already too late as process B will not care about pending signals any more.
Quoted 3 lines
David Rientjes tried to fix same issue with commit
f660daac474c6f(oom: thaw threads if oom killed thread is frozen before deferring) but it doesn't work any more, because
Has it stopped working as a result of a3201227f803 (freezer: make freezing() test freeze conditions in effect instead of TIF_FREEZE)? It removes clear_freeze_flag from __thaw_task and so the OOM victim cannot escape from the refrigerator. But there were a lot of changes in that area at the time so I might be missing some subtle details.
Quoted 9 lines
__thaw_task()just checks if it's frozen and then wakes it up, but the frozen task, after waking up, will check iffreezing()is still true and continue to freeze itself if so.__thaw_task()can't makefreezing()return false since it doesn't change any of these conditions, especiallycgroup_freezing().Fix this straightly by checking if the frozen process itself has been killed by OOM killer, so that the frozen process will recover itself and be killed finally.
OK, this should work. I remember Tejun mentioned that he wanted frozen tasks to be killable but I have no idea where this went. Maybe we want to go with the OOM part now as it fixes a real bug.
Quoted 6 lines
Cc David Rientjes <[email protected]>Cc Michal Hocko <[email protected]>Cc "Rafael J. Wysocki" <[email protected]>Cc Tejun Heo <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Cong Wang <[email protected]>
I think this should go to stable as well.
Two minor notes below:
Quoted 10 lines
--- kernel/freezer.c | 19 +++++++++++-------- mm/oom_kill.c | 2 -- 2 files changed, 11 insertions(+), 10 deletions(-)kernel/freezer.c +0
-0
kernel/freezer.c bool freezing_slow_path(struct task_struct *p) · line 55
Quoted 4 lines
kernel/freezer.c +1
-0@@ -52,6 +52,16 @@ bool freezing_slow_path(struct task_struct *p) }EXPORT_SYMBOL (freezing_slow_path); +static bool i_should_thaw_myself (bool check_kthr_stop)
should_thaw_current?
Quoted 23 lines
kernel/freezer.c +10
-2+{ +if (!freezing (current) || + (check_kthr_stop &&kthread_should_stop ()) || +test_thread_flag (TIF_MEMDIE )) +return true; +else +return false; +} +/* Refrigerator is place where frozen processes are stored :-). */ bool __refrigerator (bool check_kthr_stop) {@@ -67,8 +77,7 @@ bool __refrigerator(bool check_kthr_stop)spin_lock_irq (&freezer_lock); current->flags |=PF_FROZEN ; -if (!freezing (current) || - (check_kthr_stop &&kthread_should_stop ())) +if (i_should_thaw_myself (check_kthr_stop)) current->flags &= ~PF_FROZEN ;spin_unlock_irq (&freezer_lock);
kernel/freezer.c void __thaw_task(struct task_struct *p) · line 155
Quoted 9 lines
kernel/freezer.c +0
-6@@ -147,12 +156,6 @@ void __thaw_task(struct task_struct *p) {unsigned long flags; -/* -* Clear freezing and kick @p if FROZEN. Clearing is guaranteed to -* be visible to @p as waking up implies wmb. Waking up inside -* freezer_lock also prevents wakeups from leaking outside -* refrigerator. -*/
This is an unrelated change.
Quoted 19 lines
kernel/freezer.c +0
-0spin_lock_irqsave (&freezer_lock, flags);if (frozen (p))wake_up_process (p);mm/oom_kill.c +0
-2@@ -266,8 +266,6 @@ enum oom_scan_t oom_scan_process_thread(struct task_struct *task,* Don't allow any other task to have access to the reserves. */ if (test_tsk_thread_flag (task,TIF_MEMDIE )) { -if (unlikely (frozen (task))) -__thaw_task (task);if (!force_kill)return OOM_SCAN_ABORT ; }Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 5 lines · Michal Hocko
Has it stopped working as a result of
a3201227f803(freezer: makefreezing()test freeze conditions in effect instead ofTIF_FREEZE)? It removesclear_freeze_flagfrom__thaw_taskand so the OOM victim cannot escape from the refrigerator. But there were a lot of changes in that area at the time so I might be missing some subtle details.
Probably, I don't have time to dig the history.
Quoted 7 lines
Quoted 7 lines
__thaw_task()just checks if it's frozen and then wakes it up, but the frozen task, after waking up, will check iffreezing()is still true and continue to freeze itself if so.__thaw_task()can't makefreezing()return false since it doesn't change any of these conditions, especiallycgroup_freezing().
Quoted 7 lines
Quoted 3 lines
Fix this straightly by checking if the frozen process itself has been killed by OOM killer, so that the frozen process will recover itself and be killed finally.
OK, this should work. I remember Tejun mentioned that he wanted frozen tasks to be killable but I have no idea where this went. Maybe we want to go with the OOM part now as it fixes a real bug.
I think he rejected the patch from Bart which makes frozen tasks killable.
Quoted 4 lines
Quoted 1 line
+static bool
i_should_thaw_myself(bool check_kthr_stop)
should_thaw_current?
I have no strong preference, whatever you prefer...
Quoted 4 lines
Quoted 4 lines
kernel/freezer.c +0
-0@@ -147,12 +156,6 @@ void __thaw_task(struct task_struct *p) {unsigned long flags;
Quoted 9 lines
kernel/freezer.c void __thaw_task(struct task_struct *p) · line 155
Quoted 6 lines
kernel/freezer.c +0
-6-/* -* Clear freezing and kick @p if FROZEN. Clearing is guaranteed to -* be visible to @p as waking up implies wmb. Waking up inside -* freezer_lock also prevents wakeups from leaking outside -* refrigerator. -*/ This is an unrelated change.
It is, because as I explained, __thaw_task() doesn't do what this comment says therefore needs this patch. Or you prefer to make it a separated patch?
Thanks.
Quoted 1 line · Cong Wang
On Tue, Aug 12, 2014 at 4:44 AM, Michal Hocko <[email protected]> wrote:
[...]
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
kernel/freezer.c +0
-0@@ -147,12 +156,6 @@ void __thaw_task(struct task_struct *p) {unsigned long flags;Quoted 9 lines
kernel/freezer.c void __thaw_task(struct task_struct *p) · line 155
Quoted 6 lines
kernel/freezer.c +0
-6-/* -* Clear freezing and kick @p if FROZEN. Clearing is guaranteed to -* be visible to @p as waking up implies wmb. Waking up inside -* freezer_lock also prevents wakeups from leaking outside -* refrigerator. -*/ This is an unrelated change.
Quoted 2 lines
It is, because as I explained,
__thaw_task()doesn't do what this comment says therefore needs this patch. Or you prefer to make it a separated patch?
It is outdated since a3201227f803. I would do it in a separate patch but maybe just mention that the above commit has changed the implementation.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 17 lines · Michal Hocko
Quoted 1 line · Cong Wang
On Tue, Aug 12, 2014 at 4:44 AM, Michal Hocko <[email protected]> wrote:
[...]
Quoted 14 lines
Quoted 13 lines
kernel/freezer.c void __thaw_task(struct task_struct *p) · line 155
Quoted 10 lines
kernel/freezer.c +0
-6@@ -147,12 +156,6 @@ void __thaw_task(struct task_struct *p) {unsigned long flags; -/* -* Clear freezing and kick @p if FROZEN. Clearing is guaranteed to -* be visible to @p as waking up implies wmb. Waking up inside -* freezer_lock also prevents wakeups from leaking outside -* refrigerator. -*/ This is an unrelated change.
Quoted 6 lines
Quoted 2 lines
It is, because as I explained,
__thaw_task()doesn't do what this comment says therefore needs this patch. Or you prefer to make it a separated patch?It is outdated since
a3201227f803. I would do it in a separate patch but maybe just mention that the above commit has changed the implementation.
OK, I will split this patch.