public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rjw@sisk.pl>
To: Oleg Nesterov <oleg@tv-sign.ru>
Cc: ego@in.ibm.com, akpm@osdl.org, paulmck@us.ibm.com, mingo@elte.hu,
	vatsa@in.ibm.com, dipankar@in.ibm.com,
	venkatesh.pallipadi@intel.com, linux-kernel@vger.kernel.org,
	Pavel Machek <pavel@ucw.cz>
Subject: Re: freezer problems
Date: Tue, 20 Feb 2007 19:29:01 +0100	[thread overview]
Message-ID: <200702201929.03776.rjw@sisk.pl> (raw)
In-Reply-To: <200702200132.12847.rjw@sisk.pl>

On Tuesday, 20 February 2007 01:32, Rafael J. Wysocki wrote:
> On Tuesday, 20 February 2007 01:12, Oleg Nesterov wrote:
> > On 02/20, Rafael J. Wysocki wrote:
> > >
> > > On Monday, 19 February 2007 23:41, Oleg Nesterov wrote:
> > > > On 02/19, Rafael J. Wysocki wrote:
> > > > >
> > > > > On Monday, 19 February 2007 21:23, Oleg Nesterov wrote:
> > > > > 
> > > > > > > @@ -199,6 +189,10 @@ static void thaw_tasks(int thaw_user_spa
> > > > > > >
> > > > > > >         do_each_thread(g, p) {
> > > > > > > +               if (freezer_should_skip(p))
> > > > > > > +                       cancel_freezing(p);
> > > > > > > +       } while_each_thread(g, p);
> > > > > > > +       do_each_thread(g, p) {
> > > > > > >                 if (!freezeable(p))
> > > > > > >                         continue;
> > > > > > 
> > > > > > Any reason for 2 separate do_each_thread() loops ?
> > > > > 
> > > > > Yes.  If there is a "freeze" request pending for the vfork parent (TIF_FREEZE
> > > > > set), we have to cancel it before the child is unfrozen, since otherwise the
> > > > > parent may go freezing after we try to reset PF_FROZEN for it.
> > > > 
> > > > I see, thanks... thaw_process() doesn't take TIF_FREEZE into account.
> > > > 
> > > > But doesn't this mean we have a race?
> > > > 
> > > > Suppose that try_to_freeze_tasks() failed. It does cancel_freezing() for each
> > > > process before return, but what if some thread already checked TIF_FREEZE and
> > > > (for simplicity) it is preempted before frozen_process() in refrigerator().
> > > > 
> > > > thaw_tasks() runs, ignores this task (P), returns. P gets CPU, and becomes
> > > > frozen, but nobody will thaw it.
> > > > 
> > > > No?
> > > 
> > > Well, I think this is highly theoretical.  Namely, try_to_freeze_tasks() only
> > > fails after the timeout that's currently set to 20 sec., and it yields the CPU
> > > in each iteration of the main loop.  The task in question would have to refuse
> > > being frozen for 20 sec. and then suddenly decide to freeze itself right before
> > > try_to_freeze_tasks() checks the timeout for the very last time.  Then, it
> > > would have to get preempted at this very moment and stay unfrozen at least
> > > until thaw_tasks() starts running and in fact even longer.
> > 
> > Yes, yes, it is pure theroretical,
> > 
> > > I think we may avoid this by making try_to_freeze_tasks() sleep for some time
> > > after it has reset TIF_FREEZE for all tasks in the error path, if anyone is
> > > ever able to trigger it.
> > 
> > This makes this race  (pure theroretical) ** 2  :)
> > 
> > Still. May be it make sense to introduce cancel_freezing_and_thaw() function
> > (not right now) which stops the task from sleeping in refrigirator reliably.
> 
> Hm.  In the case discussed above we have a task that's right before calling
> frozen_process(), so we can't thaw it, because it's not frozen.  It will be
> frozen just in a while, but try_to_freeze_tasks() and thaw_tasks() have no
> way to check this.
> 
> I think to close this race the refrigerator should check TIF_FREEZE and set
> PF_FROZEN _and_ reset TIF_FREEZE under a lock that would also have to be
> taken by try_to_freeze_tasks() in the beginning of the error path.  This will
> ensure that all tasks either freeze themselves before the error path in
> try_to_freeze_tasks() is executed, or remain unfrozen.
> 
> I'll try to prepare a patch to illustrate this, but right now I'm too tired to
> do it. :-)

Something like this, perhaps:

---
 include/linux/freezer.h |   10 +++-------
 kernel/power/process.c  |   18 ++++++++++++++++--
 2 files changed, 19 insertions(+), 9 deletions(-)

Index: linux-2.6.20-mm2/include/linux/freezer.h
===================================================================
--- linux-2.6.20-mm2.orig/include/linux/freezer.h
+++ linux-2.6.20-mm2/include/linux/freezer.h
@@ -58,17 +58,13 @@ static inline void frozen_process(struct
 	clear_tsk_thread_flag(p, TIF_FREEZE);
 }
 
-extern void refrigerator(void);
+extern int refrigerator(void);
 extern int freeze_processes(void);
 extern void thaw_processes(void);
 
 static inline int try_to_freeze(void)
 {
-	if (freezing(current)) {
-		refrigerator();
-		return 1;
-	} else
-		return 0;
+	return refrigerator();
 }
 
 /*
@@ -104,7 +100,7 @@ static inline void freeze(struct task_st
 static inline int thaw_process(struct task_struct *p) { return 1; }
 static inline void frozen_process(struct task_struct *p) { BUG(); }
 
-static inline void refrigerator(void) {}
+static inline int refrigerator(void) { return 0; }
 static inline int freeze_processes(void) { BUG(); return 0; }
 static inline void thaw_processes(void) {}
 
Index: linux-2.6.20-mm2/kernel/power/process.c
===================================================================
--- linux-2.6.20-mm2.orig/kernel/power/process.c
+++ linux-2.6.20-mm2/kernel/power/process.c
@@ -24,6 +24,8 @@
 #define FREEZER_KERNEL_THREADS 0
 #define FREEZER_USER_SPACE 1
 
+spinlock_t refrigerator_lock;
+
 static inline int freezeable(struct task_struct * p)
 {
 	if ((p == current) ||
@@ -34,15 +36,23 @@ static inline int freezeable(struct task
 }
 
 /* Refrigerator is place where frozen processes are stored :-). */
-void refrigerator(void)
+int refrigerator(void)
 {
 	/* Hmm, should we be allowed to suspend when there are realtime
 	   processes around? */
 	long save;
+
+	spin_lock(&refrigerator_lock);
+	if (freezing(current)) {
+		frozen_process(current);
+		spin_unlock(&refrigerator_lock);
+	} else {
+		spin_unlock(&refrigerator_lock);
+		return 0;
+	}
 	save = current->state;
 	pr_debug("%s entered refrigerator\n", current->comm);
 
-	frozen_process(current);
 	spin_lock_irq(&current->sighand->siglock);
 	recalc_sigpending(); /* We sent fake signal, clean it up */
 	spin_unlock_irq(&current->sighand->siglock);
@@ -53,6 +63,7 @@ void refrigerator(void)
 	}
 	pr_debug("%s left refrigerator\n", current->comm);
 	current->state = save;
+	return 1;
 }
 
 static inline void freeze_process(struct task_struct *p)
@@ -143,6 +154,7 @@ static unsigned int try_to_freeze_tasks(
 					"kernel threads",
 				TIMEOUT / HZ, todo);
 		read_lock(&tasklist_lock);
+		spin_lock(&refrigerator_lock);
 		do_each_thread(g, p) {
 			if (is_user_space(p) == !freeze_user_space)
 				continue;
@@ -152,6 +164,7 @@ static unsigned int try_to_freeze_tasks(
 
 			cancel_freezing(p);
 		} while_each_thread(g, p);
+		spin_unlock(&refrigerator_lock);
 		read_unlock(&tasklist_lock);
 	}
 
@@ -169,6 +182,7 @@ int freeze_processes(void)
 	unsigned int nr_unfrozen;
 
 	printk("Stopping tasks ... ");
+	spin_lock_init(&refrigerator_lock);
 	nr_unfrozen = try_to_freeze_tasks(FREEZER_USER_SPACE);
 	if (nr_unfrozen)
 		return nr_unfrozen;

  parent reply	other threads:[~2007-02-20 18:35 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-14 14:40 [RFC PATCH(Experimental) 0/4] Freezer based Cpu-hotplug Gautham R Shenoy
2007-02-14 14:42 ` [RFC PATCH(Experimental) 1/4] freezer-cpu-hotplug core Gautham R Shenoy
2007-02-14 14:43   ` [RFC PATCH(Experimental) 2/4] Revert changes to workqueue.c Gautham R Shenoy
2007-02-14 14:43     ` [RFC PATCH(Experimental) 3/4] Revert changes to sched.c and slab.c Gautham R Shenoy
2007-02-14 14:44       ` [RFC PATCH(Experimental) 4/4] Rip out lock_cpu_hotplug from linux Gautham R Shenoy
2007-02-14 14:59     ` [RFC PATCH(Experimental) 2/4] Revert changes to workqueue.c Srivatsa Vaddagiri
2007-02-14 15:24     ` Srivatsa Vaddagiri
2007-02-14 20:23       ` Oleg Nesterov
2007-02-14 20:09     ` Oleg Nesterov
2007-02-16  5:26       ` Srivatsa Vaddagiri
2007-02-16 15:33         ` Oleg Nesterov
2007-02-16 16:47           ` Srivatsa Vaddagiri
2007-02-16 18:45             ` Oleg Nesterov
2007-02-16 23:59             ` Oleg Nesterov
2007-02-17  2:29               ` Srivatsa Vaddagiri
2007-02-17 21:59                 ` Oleg Nesterov
2007-02-20 15:12                   ` Srivatsa Vaddagiri
2007-02-20 20:09                     ` Oleg Nesterov
2007-02-21  6:29                       ` Srivatsa Vaddagiri
2007-02-21 14:30                         ` Oleg Nesterov
2007-02-21 14:37                           ` Gautham R Shenoy
2007-02-21 15:53                           ` Srivatsa Vaddagiri
2007-02-14 15:31   ` [RFC PATCH(Experimental) 1/4] freezer-cpu-hotplug core Srivatsa Vaddagiri
2007-02-14 19:47   ` Oleg Nesterov
2007-02-16  6:48     ` Srivatsa Vaddagiri
2007-02-16 15:47       ` Oleg Nesterov
2007-02-14 20:22   ` Oleg Nesterov
2007-02-16  7:16     ` Srivatsa Vaddagiri
2007-02-16  8:12       ` Srivatsa Vaddagiri
2007-02-16  9:29         ` Rafael J. Wysocki
2007-02-16  9:59           ` Srivatsa Vaddagiri
2007-02-16 11:06             ` Rafael J. Wysocki
2007-02-16 19:46         ` Oleg Nesterov
2007-02-17  2:31           ` Srivatsa Vaddagiri
2007-02-17  5:32         ` Gautham R Shenoy
2007-02-17 11:19           ` Gautham R Shenoy
2007-02-16 16:06       ` Oleg Nesterov
2007-02-14 21:43 ` [RFC PATCH(Experimental) 0/4] Freezer based Cpu-hotplug Rafael J. Wysocki
2007-02-15  6:34   ` Gautham R Shenoy
2007-02-15  8:09     ` Rafael J. Wysocki
2007-02-15 12:20       ` Gautham R Shenoy
2007-02-15 13:31         ` Rafael J. Wysocki
2007-02-15 14:25           ` Gautham R Shenoy
2007-02-17 11:24             ` Rafael J. Wysocki
2007-02-17 21:34               ` Oleg Nesterov
2007-02-17 22:24                 ` Rafael J. Wysocki
2007-02-17 23:42                   ` Oleg Nesterov
2007-02-17 23:47                     ` Oleg Nesterov
2007-02-18 10:43                       ` Rafael J. Wysocki
2007-02-18 11:31                         ` Oleg Nesterov
2007-02-18 12:14                           ` Rafael J. Wysocki
2007-02-18 14:52                             ` freezer problems Oleg Nesterov
2007-02-18 15:14                               ` Rafael J. Wysocki
2007-02-18 16:19                                 ` Oleg Nesterov
2007-02-18 18:14                                   ` Rafael J. Wysocki
2007-02-18 18:56                               ` Rafael J. Wysocki
2007-02-18 22:01                                 ` Oleg Nesterov
2007-02-18 23:19                                   ` Rafael J. Wysocki
2007-02-19 20:23                                     ` Oleg Nesterov
2007-02-19 21:21                                       ` Rafael J. Wysocki
2007-02-19 22:41                                         ` Oleg Nesterov
2007-02-19 23:35                                           ` Rafael J. Wysocki
2007-02-20  0:12                                             ` Oleg Nesterov
2007-02-20  0:32                                               ` Rafael J. Wysocki
2007-02-20  0:50                                                 ` Oleg Nesterov
2007-02-20 18:28                                                   ` Rafael J. Wysocki
2007-02-20 18:29                                                 ` Rafael J. Wysocki [this message]
2007-02-21 18:14                                                   ` Paul E. McKenney
2007-02-21 18:13                                                     ` Rafael J. Wysocki
2007-02-21 18:27                                                       ` Paul E. McKenney
2007-02-21 20:03                                                       ` Oleg Nesterov
2007-02-21 20:47                                                         ` Rafael J. Wysocki
2007-02-21 21:06                                                         ` Paul E. McKenney
2007-02-21 23:10                                                           ` Rafael J. Wysocki
2007-02-22 10:47                                                             ` Oleg Nesterov
2007-02-22 11:33                                                               ` Oleg Nesterov
2007-02-22 17:03                                                               ` Rafael J. Wysocki
2007-02-22 17:44                                                                 ` Oleg Nesterov
2007-02-22 21:56                                                                   ` Rafael J. Wysocki
2007-02-23 18:15                                                                     ` Oleg Nesterov
2007-02-23  3:02                                                                 ` Gautham R Shenoy
2007-02-18 15:09                             ` [RFC PATCH(Experimental) 0/4] Freezer based Cpu-hotplug Rafael J. Wysocki
2007-02-18 16:11                               ` Oleg Nesterov
2007-02-18 18:51                                 ` Rafael J. Wysocki
2007-02-18 10:32                     ` Rafael J. Wysocki
2007-02-18 11:32                       ` Oleg Nesterov
2007-02-18 12:12                         ` Rafael J. Wysocki
2007-02-18 15:06                           ` Oleg Nesterov
2007-02-18 12:56               ` Pavel Machek
2007-02-21 14:52               ` Gautham R Shenoy
2007-02-21 19:42                 ` Pavel Machek
     [not found] ` <200702231041.17136.rjw@sisk.pl>
     [not found]   ` <20070223100817.GA10973@in.ibm.com>
     [not found]     ` <200702231115.00718.rjw@sisk.pl>
     [not found]       ` <20070223104723.GB10973@in.ibm.com>
     [not found]         ` <20070223110201.GC10973@in.ibm.com>
2007-02-23 19:03           ` freezer problems Gautham R Shenoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200702201929.03776.rjw@sisk.pl \
    --to=rjw@sisk.pl \
    --cc=akpm@osdl.org \
    --cc=dipankar@in.ibm.com \
    --cc=ego@in.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=oleg@tv-sign.ru \
    --cc=paulmck@us.ibm.com \
    --cc=pavel@ucw.cz \
    --cc=vatsa@in.ibm.com \
    --cc=venkatesh.pallipadi@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox