Skip to contentSkip to conversation

freezer: check OOM kill signal while being frozen

PATCH Superseded Cong Wang 4 participants 7 messages Aug 9, 2014 – Aug 12, 2014 Freezer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 freezer: check OOM kill signal 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 oom
B=oom_scan_process_thread()

					cgroup_freezer_freeze(B)
			...
			try_to_freeze()
			stay in D state

oom_kill_process(B)

In this case, process A triggers OOM and kernel selects 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 never care about pending signals any more. Fix this straightly by checking fatal pending signal from OOM killer, so that the frozen process will recover itself and then be killed finally.

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]>

---

kernel/freezer.c +3 -1
index aa6a8aa..c6d189d 100644
@@ -68,7 +68,9 @@ bool __refrigerator(bool check_kthr_stop)
 		spin_lock_irq(&freezer_lock);
 		current->flags |= PF_FROZEN;
 		if (!freezing(current) ||
-		    (check_kthr_stop && kthread_should_stop()))
+		    (check_kthr_stop && kthread_should_stop()) ||
+		    (test_tsk_thread_flag(current, TIF_MEMDIE) &&
+			 fatal_signal_pending(current)))
 			current->flags &= ~PF_FROZEN;
 		spin_unlock_irq(&freezer_lock);
 
#1 lore raw
Quoted 12 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 oom
B=oom_scan_process_thread()

                                        cgroup_freezer_freeze(B)
                        ...
                        try_to_freeze()
                        stay in D state

oom_kill_process(B)

Quoted 7 lines

In this case, process A triggers OOM and kernel selects 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 never care about pending signals any more. Fix this straightly by checking fatal pending signal from OOM killer, so that the frozen process will recover itself and then be killed finally.

Similar patch is posted and discussed but no conclusion.
http://marc.info/?t=137699769400004&r=1&w=1

Adding Bart,

Thank you,
Kyungmin Park

Quoted 28 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]>

---

kernel/freezer.c +3 -2
index aa6a8aa..c6d189d 100644
@@ -68,7 +68,9 @@ bool __refrigerator(bool check_kthr_stop)
                spin_lock_irq(&freezer_lock);
                current->flags |= PF_FROZEN;
                if (!freezing(current) ||
-                   (check_kthr_stop && kthread_should_stop()))
+                   (check_kthr_stop && kthread_should_stop()) ||
+                   (test_tsk_thread_flag(current, TIF_MEMDIE) &&
+                        fatal_signal_pending(current)))
                        current->flags &= ~PF_FROZEN;
                spin_unlock_irq(&freezer_lock);
 
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#2 lore raw
Quoted 4 lines · Kyungmin Park

Similar patch is posted and discussed but no conclusion.
http://marc.info/?t=137699769400004&r=1&w=1

I noticed this thread before sending my patch. I don't think we are trying to solve the same problem:

a) I only care OOM kill, not other SIGKILL cases
b) I don't set the frozen process state to TASK_KILLABLE at all

I think Bartlomiej was trying to make frozen processes killable by user-space SIGKILL, which is beyond my interest.

Thanks.

#3 lore raw
In reply to Cong Wang
Quoted 5 lines · Cong Wang
kernel/freezer.c +0 -0
index aa6a8aa..c6d189d 100644

kernel/freezer.c bool __refrigerator(bool check_kthr_stop) · line 76

Quoted 10 lines
kernel/freezer.c +3 -1
@@ -68,7 +68,9 @@ bool __refrigerator(bool check_kthr_stop)
 		spin_lock_irq(&freezer_lock);
 		current->flags |= PF_FROZEN;
 		if (!freezing(current) ||
-		    (check_kthr_stop && kthread_should_stop()))
+		    (check_kthr_stop && kthread_should_stop()) ||
+		    (test_tsk_thread_flag(current, TIF_MEMDIE) &&
+			 fatal_signal_pending(current)))
 			current->flags &= ~PF_FROZEN;
 		spin_unlock_irq(&freezer_lock);
 

All threads with TIF_MEMDIE set would have been killed, why is the check for fatal_signal_pending(current) needed?

You can also use test_thread_flag(TIF_MEMDIE) instead since you're only concerned with current.

Furthermore, that conditional is getting difficult to read. Maybe it would help to have a helper function that returns bool that determines whether PF_FROZEN should be cleared?

author
#4 lore raw
Quoted 12 lines · David Rientjes
Quoted 10 lines · Cong Wang
kernel/freezer.c +1 -1
index aa6a8aa..c6d189d 100644
@@ -68,7 +68,9 @@ bool __refrigerator(bool check_kthr_stop)
              spin_lock_irq(&freezer_lock);
              current->flags |= PF_FROZEN;
              if (!freezing(current) ||
-                 (check_kthr_stop && kthread_should_stop()))
+                 (check_kthr_stop && kthread_should_stop()) ||
Quoted 8 lines

kernel/freezer.c bool __refrigerator(bool check_kthr_stop) · line 76

Quoted 5 lines
kernel/freezer.c +2 -0
+                 (test_tsk_thread_flag(current, TIF_MEMDIE) &&
+                      fatal_signal_pending(current)))
                      current->flags &= ~PF_FROZEN;
              spin_unlock_irq(&freezer_lock);
 

All threads with TIF_MEMDIE set would have been killed, why is the check for fatal_signal_pending(current) needed?

So just checking TIF_MEMDIE is enough, right?

Quoted 4 lines

You can also use test_thread_flag(TIF_MEMDIE) instead since you're only concerned with current.

OK, will do.

Quoted 3 lines

Furthermore, that conditional is getting difficult to read. Maybe it would help to have a helper function that returns bool that determines whether PF_FROZEN should be cleared?

Makes sense.

Thanks.

#5 lore raw
In reply to Cong Wang
Quoted 10 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 oom
B=oom_scan_process_thread()

					cgroup_freezer_freeze(B)
			...
			try_to_freeze()
			stay in D state
Quoted 7 lines

oom_kill_process(B)

In this case, process A triggers OOM and kernel selects 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 never care about pending signals any more.

OK, so the system/memcg is still OOM and a new allocation/charge would trigger killer again, right? Then oom_scan_process_thread sees TIF_MEMDIE frozen task and thaw it so it can go away and die. So this shouldn't be a permanent state. Or am I missing something?

Quoted 26 lines

Fix this straightly by checking fatal pending signal from OOM killer, so that the frozen process will recover itself and then be killed finally.

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]>

---

kernel/freezer.c +3 -1
index aa6a8aa..c6d189d 100644
@@ -68,7 +68,9 @@ bool __refrigerator(bool check_kthr_stop)
 		spin_lock_irq(&freezer_lock);
 		current->flags |= PF_FROZEN;
 		if (!freezing(current) ||
-		    (check_kthr_stop && kthread_should_stop()))
+		    (check_kthr_stop && kthread_should_stop()) ||
+		    (test_tsk_thread_flag(current, TIF_MEMDIE) &&
+			 fatal_signal_pending(current)))
 			current->flags &= ~PF_FROZEN;
 		spin_unlock_irq(&freezer_lock);
 
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#6 lore raw
Quoted 6 lines · Michal Hocko

OK, so the system/memcg is still OOM and a new allocation/charge would trigger killer again, right? Then oom_scan_process_thread sees TIF_MEMDIE frozen task and thaw it so it can go away and die. So this shouldn't be a permanent state. Or am I missing something?

Good point!

Reading the code again, I think David's commit (f660daac) doesn't work any more, __thaw_task() just checks if it's frozen and then wakes it up, but the frozen task, after waking up, will check if it's freezing() 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().

This reminds me I should revert that code in oom_killer, checking TIF_MEMDIE in __refrigerator() is more correct and clear.

I will update my patch.

Thanks!