Skip to contentSkip to conversation

cgroup v2 freezer follow-up patches

PATCH Applied Roman Gushchin 2 participants 4 messages Apr 26, 2019 – May 6, 2019 cgroup

lore
Share & tools
lore
#0 lore raw

cover cgroup v2 freezer follow-up patches

Hi, Tejun!

Please, pull these two follow-up patches for the cgroup v2 freezer.

These are a fix for a spurious state transition, which could happen due to a race condition, and a cleanup of some dead code. Both patches were suggested by Oleg Nesterov.

Thank you!

Roman Gushchin (2):
cgroup: prevent spurious transition into non-frozen state
cgroup: get rid of cgroup_freezer_frozen_exit()

 include/linux/cgroup.h  |  2 +-
 kernel/cgroup/cgroup.c  |  5 ++---
 kernel/cgroup/freezer.c | 26 +++++---------------------
 kernel/signal.c         |  2 +-
 4 files changed, 9 insertions(+), 26 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 cgroup: prevent spurious transition into non-frozen state

If freezing of a cgroup races with waking of a task from the frozen state (like waiting in vfork() or in do_signal_stop()), a spurious transition of the cgroup state can happen.

The task enters cgroup_leave_frozen(true), the cgroup->nr_frozen_tasks counter decrements, and the cgroup is switched to the unfrozen state.

To prevent it, let's reserve cgroup_leave_frozen(true) for terminating processes and use cgroup_leave_frozen(false) otherwise.

To avoid busy-looping in the signal handling loop waiting for JOBCTL_TRAP_FREEZE set from the cgroup freezing path, let's do it explicitly in cgroup_leave_frozen(), if the task is going to stay frozen.

---
 kernel/cgroup/freezer.c | 16 +++++-----------
 kernel/signal.c         |  2 +-
 2 files changed, 6 insertions(+), 12 deletions(-)
kernel/cgroup/freezer.c +5 -11
index 3bfbb3c8baf3..c321e768f8d3 100644
@@ -139,19 +139,13 @@ void cgroup_leave_frozen(bool always_leave)
 		cgroup_update_frozen(cgrp);
 		WARN_ON_ONCE(!current->frozen);
 		current->frozen = false;
+	} else if (!(current->jobctl & JOBCTL_TRAP_FREEZE)) {
+		spin_lock(&current->sighand->siglock);
+		current->jobctl |= JOBCTL_TRAP_FREEZE;
+		set_thread_flag(TIF_SIGPENDING);
+		spin_unlock(&current->sighand->siglock);
 	}
 	spin_unlock_irq(&css_set_lock);
-
-	if (unlikely(current->frozen)) {
-		/*
-		 * If the task remained in the frozen state,
-		 * make sure it won't reach userspace without
-		 * entering the signal handling loop.
-		 */
-		spin_lock_irq(&current->sighand->siglock);
-		recalc_sigpending();
-		spin_unlock_irq(&current->sighand->siglock);
-	}
 }
 
 /*
kernel/signal.c +1 -1
index 095e0fc57b25..16b72f4f14df 100644
@@ -2514,7 +2514,7 @@ bool get_signal(struct ksignal *ksig)
 		 */
 		if (unlikely(cgroup_task_frozen(current))) {
 			spin_unlock_irq(&sighand->siglock);
-			cgroup_leave_frozen(true);
+			cgroup_leave_frozen(false);
 			goto relock;
 		}
 
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/2 cgroup: get rid of cgroup_freezer_frozen_exit()

In reply to Roman Gushchin

A task should never enter the exit path with the task->frozen bit set. Any frozen task must enter the signal handling loop and the only way to escape is through cgroup_leave_frozen(true), which unconditionally drops the task->frozen bit. So it means that cgroyp_freezer_frozen_exit() has zero chances to be called and has to be removed.

Let's put a WARN_ON_ONCE() instead of the cgroup_freezer_frozen_exit() call to catch any potential leak of the task's frozen bit.

---
 include/linux/cgroup.h  |  2 +-
 kernel/cgroup/cgroup.c  |  5 ++---
 kernel/cgroup/freezer.c | 10 ----------
 3 files changed, 3 insertions(+), 14 deletions(-)
include/linux/cgroup.h +1 -1
index 3e2efd412dfa..c0077adeea83 100644
@@ -889,7 +889,7 @@ void cgroup_update_frozen(struct cgroup *cgrp);
 void cgroup_freeze(struct cgroup *cgrp, bool freeze);
 void cgroup_freezer_migrate_task(struct task_struct *task, struct cgroup *src,
 				 struct cgroup *dst);
-void cgroup_freezer_frozen_exit(struct task_struct *task);
+
 static inline bool cgroup_task_freeze(struct task_struct *task)
 {
 	bool ret;
kernel/cgroup/cgroup.c +2 -3
index 57edcf398d71..622ae7452969 100644
@@ -5929,9 +5929,8 @@ void cgroup_exit(struct task_struct *tsk)
 		css_set_move_task(tsk, cset, NULL, false);
 		cset->nr_tasks--;
 
-		if (unlikely(cgroup_task_frozen(tsk)))
-			cgroup_freezer_frozen_exit(tsk);
-		else if (unlikely(cgroup_task_freeze(tsk)))
+		WARN_ON_ONCE(cgroup_task_frozen(tsk));
+		if (unlikely(cgroup_task_freeze(tsk)))
 			cgroup_update_frozen(task_dfl_cgroup(tsk));
 
 		spin_unlock_irq(&css_set_lock);
kernel/cgroup/freezer.c +0 -10
index c321e768f8d3..8cf010680678 100644
@@ -248,16 +248,6 @@ void cgroup_freezer_migrate_task(struct task_struct *task,
 	cgroup_freeze_task(task, test_bit(CGRP_FREEZE, &dst->flags));
 }
 
-void cgroup_freezer_frozen_exit(struct task_struct *task)
-{
-	struct cgroup *cgrp = task_dfl_cgroup(task);
-
-	lockdep_assert_held(&css_set_lock);
-
-	cgroup_dec_frozen_cnt(cgrp);
-	cgroup_update_frozen(cgrp);
-}
-
 void cgroup_freeze(struct cgroup *cgrp, bool freeze)
 {
 	struct cgroup_subsys_state *css;
Signature · 1 line
2.20.1
maintainer Applied
#3 lore raw
In reply to Roman Gushchin
Quoted 7 lines · Roman Gushchin

Hi, Tejun!

Please, pull these two follow-up patches for the cgroup v2 freezer.

These are a fix for a spurious state transition, which could happen due to a race condition, and a cleanup of some dead code. Both patches were suggested by Oleg Nesterov.

Applied to cgroup/for-5.2.

Thanks.

Signature · 1 line
tejun