linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] cgroup v2 freezer follow-up patches
@ 2019-04-26 17:59 Roman Gushchin
  2019-04-26 17:59 ` [PATCH 1/2] cgroup: prevent spurious transition into non-frozen state Roman Gushchin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Roman Gushchin @ 2019-04-26 17:59 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Oleg Nesterov, kernel-team, cgroups, linux-kernel, 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.

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(-)

-- 
2.20.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] cgroup: prevent spurious transition into non-frozen state
  2019-04-26 17:59 [PATCH 0/2] cgroup v2 freezer follow-up patches Roman Gushchin
@ 2019-04-26 17:59 ` Roman Gushchin
  2019-04-26 17:59 ` [PATCH 2/2] cgroup: get rid of cgroup_freezer_frozen_exit() Roman Gushchin
  2019-05-06 15:40 ` [PATCH 0/2] cgroup v2 freezer follow-up patches Tejun Heo
  2 siblings, 0 replies; 4+ messages in thread
From: Roman Gushchin @ 2019-04-26 17:59 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Oleg Nesterov, kernel-team, cgroups, linux-kernel, Roman Gushchin

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.

Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Roman Gushchin <guro@fb.com>
---
 kernel/cgroup/freezer.c | 16 +++++-----------
 kernel/signal.c         |  2 +-
 2 files changed, 6 insertions(+), 12 deletions(-)

diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c
index 3bfbb3c8baf3..c321e768f8d3 100644
--- a/kernel/cgroup/freezer.c
+++ b/kernel/cgroup/freezer.c
@@ -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);
-	}
 }
 
 /*
diff --git a/kernel/signal.c b/kernel/signal.c
index 095e0fc57b25..16b72f4f14df 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -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;
 		}
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] cgroup: get rid of cgroup_freezer_frozen_exit()
  2019-04-26 17:59 [PATCH 0/2] cgroup v2 freezer follow-up patches Roman Gushchin
  2019-04-26 17:59 ` [PATCH 1/2] cgroup: prevent spurious transition into non-frozen state Roman Gushchin
@ 2019-04-26 17:59 ` Roman Gushchin
  2019-05-06 15:40 ` [PATCH 0/2] cgroup v2 freezer follow-up patches Tejun Heo
  2 siblings, 0 replies; 4+ messages in thread
From: Roman Gushchin @ 2019-04-26 17:59 UTC (permalink / raw)
  To: Tejun Heo
  Cc: Oleg Nesterov, kernel-team, cgroups, linux-kernel, 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.

Suggested-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Roman Gushchin <guro@fb.com>
---
 include/linux/cgroup.h  |  2 +-
 kernel/cgroup/cgroup.c  |  5 ++---
 kernel/cgroup/freezer.c | 10 ----------
 3 files changed, 3 insertions(+), 14 deletions(-)

diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h
index 3e2efd412dfa..c0077adeea83 100644
--- a/include/linux/cgroup.h
+++ b/include/linux/cgroup.h
@@ -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;
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 57edcf398d71..622ae7452969 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -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);
diff --git a/kernel/cgroup/freezer.c b/kernel/cgroup/freezer.c
index c321e768f8d3..8cf010680678 100644
--- a/kernel/cgroup/freezer.c
+++ b/kernel/cgroup/freezer.c
@@ -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;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] cgroup v2 freezer follow-up patches
  2019-04-26 17:59 [PATCH 0/2] cgroup v2 freezer follow-up patches Roman Gushchin
  2019-04-26 17:59 ` [PATCH 1/2] cgroup: prevent spurious transition into non-frozen state Roman Gushchin
  2019-04-26 17:59 ` [PATCH 2/2] cgroup: get rid of cgroup_freezer_frozen_exit() Roman Gushchin
@ 2019-05-06 15:40 ` Tejun Heo
  2 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2019-05-06 15:40 UTC (permalink / raw)
  To: Roman Gushchin; +Cc: Oleg Nesterov, kernel-team, cgroups, linux-kernel

On Fri, Apr 26, 2019 at 10:59:43AM -0700, Roman Gushchin wrote:
> 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.

-- 
tejun

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-05-06 15:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-26 17:59 [PATCH 0/2] cgroup v2 freezer follow-up patches Roman Gushchin
2019-04-26 17:59 ` [PATCH 1/2] cgroup: prevent spurious transition into non-frozen state Roman Gushchin
2019-04-26 17:59 ` [PATCH 2/2] cgroup: get rid of cgroup_freezer_frozen_exit() Roman Gushchin
2019-05-06 15:40 ` [PATCH 0/2] cgroup v2 freezer follow-up patches Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).