Freezer update (updated)
Subsystems (10)
cover Freezer update (updated)
Hi,
This is the second "release" of my latest freezer update. I have updated the patches to take your comments into account (thanks a lot for them) and changed their ordering.
The patches in these series update the freezer to eliminate some existing shortcomings, so I'm still considering them as 2.6.23 material.
The patches do the following:
* update the freezer documentation to describe, previously omitted, important reason for freezing tasks (ie. memory shrinking)
* remove sys_sync() from the freezer and make the suspend/hibernation code invoke it explicitly
* prevent new tasks from inheriting TIF_FREEZE set from the parents
* introduce freezer-firendly wrappers around wait_event_interruptible() and wait_event_interruptible_timeout()
* prevent the freezer from sending signals to kernel threads
* increase the verbosity of the freezer slightly
* make the freezer use the freezing timeout more efficiently
The details are in the changelogs.
Comments welcome.
Greetings,
Rafael
Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth
RFC PATCH 1/7 Freezer: Document relationship with memory shrinking
From: Rafael J. Wysocki <[email protected]>
One important reason to freeze tasks, which is that we don't want them to allocate memory after freeing it for the hibernation image, has not been documented. Fix it.
--- Documentation/power/freezing-of-tasks.txt | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
Documentation/power/freezing-of-tasks.txt +11 -2
@@ -81,7 +81,16 @@ hibernation image has been created and b The majority of these are user space processes, but if any of the kernel threads may cause something like this to happen, they have to be freezable. -2. The second reason is to prevent user space processes and some kernel threads +2. Next, to create the hibernation image we need to free a sufficient amount of +memory (approximately 50% of available RAM) and we need to do that before +devices are deactivated, because we generally need them for swapping out. Then, +after the memory for the image has been freed, we don't want tasks to allocate +additional memory and we prevent them from doing that by freezing them earlier. +[Of course, this also means that device drivers should not allocate substantial +amounts of memory from their .suspend() callbacks before hibernation, but this +is e separate issue.] + +3. The third reason is to prevent user space processes and some kernel threads from interfering with the suspending and resuming of devices. A user space process running on a second CPU while we are suspending devices may, for example, be troublesome and without the freezing of tasks we would need some
@@ -111,7 +120,7 @@ frozen before the driver's .suspend() ca thawed after the driver's .resume() callback has run, so it won't be accessing the device while it's suspended. -3. Another reason for freezing tasks is to prevent user space processes from +4. Another reason for freezing tasks is to prevent user space processes from realizing that hibernation (or suspend) operation takes place. Ideally, user space processes should not notice that such a system-wide operation has occurred and should continue running without any problems after the restore (or resume
RFC PATCH 2/7 Freezer: Do not sync filesystems from freeze_processes
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
The syncing of filesystems from within the freezer is generally not needed. Also, if there's an ext3 filesystem loopback-mounted from a FUSE one, the syncing results in writes to it and deadlocks. Similarly, it will deadlock if FUSE implements sync.
Change freeze_processes() so that it doesn't execute sys_sync() and make the suspend and hibernation code paths sync filesystems independently of the freezer.
--- kernel/power/disk.c | 4 ++++ kernel/power/main.c | 6 ++++++ kernel/power/process.c | 1 - kernel/power/user.c | 4 ++++ 4 files changed, 14 insertions(+), 1 deletion(-)
kernel/power/disk.c +4 -0
@@ -297,6 +297,10 @@ int hibernate(void)if (error) goto Exit; +printk ("Syncing filesystems ... " ); +sys_sync (); +printk ("done.\n" ); + error =prepare_processes ();if (error) goto Finish;
kernel/power/main.c +6 -0
@@ -20,6 +20,7 @@#include <linux/resume-trace.h> #include <linux/freezer.h> #include <linux/vmstat.h> +#include <linux/syscalls.h> #include "power.h"
@@ -234,9 +235,14 @@ static int enter_state(suspend_state_t sif (!valid_state (state))return -ENODEV ; +if (!mutex_trylock (&pm_mutex))return -EBUSY ; +printk ("Syncing filesystems ... " ); +sys_sync (); +printk ("done.\n" ); +pr_debug ("PM: Preparing system for %s sleep\n" , pm_states[state]);if ((error =suspend_prepare ())) goto Unlock;
kernel/power/process.c +0 -1
@@ -191,7 +191,6 @@ int freeze_processes(void)if (error)return error; -sys_sync (); error =try_to_freeze_tasks (FREEZER_KERNEL_THREADS );if (error)return error;
kernel/power/user.c +4 -0
@@ -153,6 +153,10 @@ static int snapshot_ioctl(struct inode *mutex_lock (&pm_mutex); error =pm_notifier_call_chain (PM_HIBERNATION_PREPARE );if (!error) { +printk ("Syncing filesystems ... " ); +sys_sync (); +printk ("done.\n" ); + error =freeze_processes ();if (error)thaw_processes ();
RFC PATCH 3/7 Freezer: Prevent new tasks from inheriting TIF_FREEZE set
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Tasks should go to the refrigerator only if explicitly requested to do that by the freezer and not as a result of inheriting the TIF_FREEZE flag set from the parent. Make it happen.
--- kernel/fork.c | 1 + 1 file changed, 1 insertion(+)
kernel/fork.c +1 -0
@@ -932,6 +932,7 @@ static inline void copy_flags(unsigned lif (!(clone_flags &CLONE_PTRACE )) p->ptrace =0 ; p->flags = new_flags; +clear_freeze_flag (p); }asmlinkage long sys_set_tid_address (int __user *tidptr)
RFC PATCH 4/7 Freezer: Introduce freezer-firendly waiting macros
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Introduce freezer-friendly wrappers around wait_event_interruptible() and wait_event_interruptible_timeout(), originally defined in <linux/wait.h>, to be used in freezable kernel threads. Make some of the freezable kernel threads use them.
This is necessary for the freezer to stop sending signals to kernel threads, which is implemented in the next patch.
--- drivers/input/gameport/gameport.c | 3 -- drivers/input/serio/serio.c | 3 -- drivers/input/touchscreen/ucb1400_ts.c | 3 -- drivers/media/dvb/dvb-core/dvb_frontend.c | 3 +- drivers/usb/core/hub.c | 3 -- drivers/usb/storage/usb.c | 5 --- include/linux/freezer.h | 40 ++++++++++++++++++++++++++++-- 7 files changed, 45 insertions(+), 15 deletions(-)
include/linux/freezer.h +38 -2
@@ -4,6 +4,7 @@#define FREEZER_H_INCLUDED #include <linux/sched.h> +#include <linux/wait.h> #ifdef CONFIG_PM /*
@@ -126,7 +127,35 @@ static inline void set_freezable(void) current->flags &= ~PF_NOFREEZE; } -#else +/* +* Freezer-friendly wrappers around wait_event_interruptible() and +* wait_event_interruptible_timeout(), originally defined in <linux/wait.h> +*/ + +#define wait_event_freezable (wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + try_to_freeze(); \ + } while (!(condition)); \ + __ret; \ +}) + + +#define wait_event_freezable_timeout (wq, condition, timeout) \ +({ \ + long __ret = timeout; \ + do { \ + __ret = wait_event_interruptible_timeout(wq, \ + (condition) || freezing(current), \ + __ret); \ + try_to_freeze(); \ + } while (!(condition)); \ + __ret; \ +}) +#else /* CONFIG_PM */ static inline int frozen (struct task_struct *p) {return 0 ; }static inline int freezing (struct task_struct *p) {return 0 ; }static inline void set_freeze_flag (struct task_struct *p) {}
@@ -143,6 +172,13 @@ static inline void freezer_do_not_count(static inline void freezer_count (void ) {}static inline int freezer_should_skip (struct task_struct *p) {return 0 ; }static inline void set_freezable (void ) {} -#endif + +#define wait_event_freezable (wq, condition) \ + wait_event_interruptible(wq, condition) + +#define wait_event_freezable_timeout (wq, condition, timeout) \ + wait_event_interruptible_timeout(wq, condition, timeout) + +#endif /* CONFIG_PM */ #endif /* FREEZER_H_INCLUDED */
drivers/input/gameport/gameport.c +1 -2
@@ -448,9 +448,8 @@ static int gameport_thread(void *nothingset_freezable ();do {gameport_handle_event (); -wait_event_interruptible (gameport_wait, +wait_event_freezable (gameport_wait,kthread_should_stop () || !list_empty (&gameport_event_list)); -try_to_freeze (); }while (!kthread_should_stop ());printk (KERN_DEBUG "gameport: kgameportd exiting\n" );
drivers/input/serio/serio.c +1 -2
@@ -387,9 +387,8 @@ static int serio_thread(void *nothing)set_freezable ();do {serio_handle_event (); -wait_event_interruptible (serio_wait, +wait_event_freezable (serio_wait,kthread_should_stop () || !list_empty (&serio_event_list)); -try_to_freeze (); }while (!kthread_should_stop ());printk (KERN_DEBUG "serio: kseriod exiting\n" );
drivers/input/touchscreen/ucb1400_ts.c +1 -2
@@ -334,10 +334,9 @@ static int ucb1400_ts_thread(void *_ucb) timeout =msecs_to_jiffies (10 ); } -wait_event_interruptible_timeout (ucb->ts_wait, +wait_event_freezable_timeout (ucb->ts_wait, ucb->irq_pending || ucb->ts_restart ||kthread_should_stop (), timeout); -try_to_freeze (); }/* Send the "pen off" if we are stopping with the pen still active */
drivers/media/dvb/dvb-core/dvb_frontend.c +2 -1
@@ -528,7 +528,8 @@ static int dvb_frontend_thread(void *datup (&fepriv->sem);/* is locked when we enter the thread... */ restart: timeout =wait_event_interruptible_timeout (fepriv->wait_queue, -dvb_frontend_should_wakeup (fe) ||kthread_should_stop (), +dvb_frontend_should_wakeup (fe) ||kthread_should_stop () + ||freezing (current), fepriv->delay);if (kthread_should_stop () ||dvb_frontend_is_exiting (fe)) {
drivers/usb/core/hub.c +1 -2
@@ -2729,10 +2729,9 @@ static int hub_thread(void *__unused)set_freezable ();do {hub_events (); -wait_event_interruptible (khubd_wait, +wait_event_freezable (khubd_wait, !list_empty (&hub_event_list) ||kthread_should_stop ()); -try_to_freeze (); }while (!kthread_should_stop () || !list_empty (&hub_event_list));pr_debug ("%s: khubd exiting\n" , usbcore_name);
drivers/usb/storage/usb.c +1 -4
@@ -913,12 +913,9 @@ static int usb_stor_scan_thread(void * _if (delay_use >0 ) {printk (KERN_DEBUG "usb-storage: waiting for device " "to settle before scanning\n" ); -retry: -wait_event_interruptible_timeout (us->delay_wait, +wait_event_freezable_timeout (us->delay_wait,test_bit (US_FLIDX_DISCONNECTING , &us->flags), delay_use *HZ ); -if (try_to_freeze ()) - goto retry; }/* If the device is still connected, perform the scanning */
RFC PATCH 5/7 Freezer: Do not send signals to kernel threads (updated)
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
The freezer should not send signals to kernel threads, since that may lead to subtle problems. In particular, commit b74d0deb968e1f85942f17080eace015ce3c332c has changed recalc_sigpending_tsk() so that it doesn't clear TIF_SIGPENDING. For this reason, if the freezer continues to send fake signals to kernel threads and the freezing of kernel threads fails, some of them may be running with TIF_SIGPENDING set forever.
Accordingly, recalc_sigpending_tsk() shouldn't set the task's TIF_SIGPENDING flag if TIF_FREEZE is set.
--- Documentation/power/freezing-of-tasks.txt | 31 +++++--- kernel/power/process.c | 107 ++++++++++++++++++++---------- kernel/signal.c | 1 3 files changed, 93 insertions(+), 46 deletions(-)
kernel/power/process.c +74 -33
@@ -75,21 +75,79 @@ void refrigerator(void)__set_current_state (save); } -static void freeze_task (struct task_struct *p) +static void fake_signal_wake_up (struct task_struct *p,int resume) {unsigned long flags; -if (!freezing (p)) { +spin_lock_irqsave (&p->sighand->siglock, flags); +signal_wake_up (p, resume); +spin_unlock_irqrestore (&p->sighand->siglock, flags); +} + +static void send_fake_signal (struct task_struct *p) +{ +if (p->state ==TASK_STOPPED ) +force_sig_specific (SIGSTOP , p); +fake_signal_wake_up (p, p->state ==TASK_STOPPED ); +} + +static int has_mm (struct task_struct *p) +{ +return (p->mm && !(p->flags &PF_BORROWED_MM )); +} + +/** +* freeze_task - send a freeze request to given task +* @p: task to send the request to +* @with_mm_only: if set, the request will only be sent if the task has its +* own mm +* Return value: 0, if @with_mm_only is set and the task has no mm of its +* own or the task is frozen, 1, otherwise +* +* The freeze request is sent by seting the tasks's TIF_FREEZE flag and +* either sending a fake signal to it or waking it up, depending on whether +* or not it has its own mm (ie. it is a user land task). If @with_mm_only +* is set and the task has no mm of its own (ie. it is a kernel thread), +* its TIF_FREEZE flag should not be set. +* +* The task_lock() is necessary to prevent races with exit_mm() or +* use_mm()/unuse_mm() from occuring. +*/ +static int freeze_task (struct task_struct *p,int with_mm_only) +{ +int ret =1 ; + +task_lock (p); +if (freezing (p)) { +if (has_mm (p)) { +if (!signal_pending (p)) +fake_signal_wake_up (p,0 ); + }else { +if (with_mm_only) + ret =0 ; +else +wake_up_state (p,TASK_INTERRUPTIBLE ); + } + }else {rmb (); -if (!frozen (p)) { -set_freeze_flag (p); -if (p->state ==TASK_STOPPED ) -force_sig_specific (SIGSTOP , p); -spin_lock_irqsave (&p->sighand->siglock, flags); -signal_wake_up (p, p->state ==TASK_STOPPED ); -spin_unlock_irqrestore (&p->sighand->siglock, flags); +if (frozen (p)) { + ret =0 ; + }else { +if (has_mm (p)) { +set_freeze_flag (p); +send_fake_signal (p); + }else { +if (with_mm_only) { + ret =0 ; + }else { +set_freeze_flag (p); +wake_up_state (p,TASK_INTERRUPTIBLE ); + } + } } } +task_unlock (p); +return ret; }static void cancel_freezing (struct task_struct *p)
@@ -119,31 +177,14 @@ static int try_to_freeze_tasks(int freezif (frozen (p) || !freezeable (p))continue ; -if (freeze_user_space) { -if (p->state ==TASK_TRACED && -frozen (p->parent)) { -cancel_freezing (p); -continue ; - } -/* -* Kernel threads should not have TIF_FREEZE set -* at this point, so we must ensure that either -* p->mm is not NULL *and* PF_BORROWED_MM is -* unset, or TIF_FRREZE is left unset. -* The task_lock() is necessary to prevent races -* with exit_mm() or use_mm()/unuse_mm() from -* occuring. -*/ -task_lock (p); -if (!p->mm || (p->flags &PF_BORROWED_MM )) { -task_unlock (p); -continue ; - } -freeze_task (p); -task_unlock (p); - }else { -freeze_task (p); +if (p->state ==TASK_TRACED &&frozen (p->parent)) { +cancel_freezing (p); +continue ; } + +if (!freeze_task (p, freeze_user_space)) +continue ; +if (!freezer_should_skip (p)) todo++; }while_each_thread (g, p);
kernel/signal.c +0 -1
@@ -99,7 +99,6 @@ static inline int has_pending_signals(sistatic int recalc_sigpending_tsk (struct task_struct *t) {if (t->signal->group_stop_count >0 || - (freezing (t)) ||PENDING (&t->pending, &t->blocked) ||PENDING (&t->signal->shared_pending, &t->blocked)) {set_tsk_thread_flag (t,TIF_SIGPENDING );
Documentation/power/freezing-of-tasks.txt +19 -12
@@ -19,12 +19,13 @@ we only consider hibernation, but the de Namely, as the first step of the hibernation procedure the function freeze_processes() (defined in kernel/power/process.c) is called. It executes try_to_freeze_tasks() that sets TIF_FREEZE for all of the freezable tasks and -sends a fake signal to each of them. A task that receives such a signal and has -TIF_FREEZE set, should react to it by calling the refrigerator() function -(defined in kernel/power/process.c), which sets the task's PF_FROZEN flag, -changes its state to TASK_UNINTERRUPTIBLE and makes it loop until PF_FROZEN is -cleared for it. Then, we say that the task is 'frozen' and therefore the set of -functions handling this mechanism is called 'the freezer' (these functions are +either wakes them up, if they are kernel threads, or sends fake signals to them, +if they are user space processes. A task that has TIF_FREEZE set, should react +to it by calling the function called refrigerator() (defined in +kernel/power/process.c), which sets the task's PF_FROZEN flag, changes its state +to TASK_UNINTERRUPTIBLE and makes it loop until PF_FROZEN is cleared for it. +Then, we say that the task is 'frozen' and therefore the set of functions +handling this mechanism is referred to as 'the freezer' (these functions are defined in kernel/power/process.c and include/linux/freezer.h). User space processes are generally frozen before kernel threads.
@@ -35,21 +36,27 @@ task enter refrigerator() if the flag is For user space processes try_to_freeze() is called automatically from the signal-handling code, but the freezable kernel threads need to call it -explicitly in suitable places. The code to do this may look like the following: +explicitly in suitable places or use the wait_event_freezable() or +wait_event_freezable_timeout() macros (defined in include/linux/freezer.h) +that combine interruptible sleep with checking if TIF_FREEZE is set and calling +try_to_freeze(). The main loop of a freezable kernel thread may look like the +following one: + set_freezable(); do { hub_events(); - wait_event_interruptible(khubd_wait, - !list_empty(&hub_event_list)); - try_to_freeze(); - } while (!signal_pending(current)); + wait_event_freezable(khubd_wait, + !list_empty(&hub_event_list) || + kthread_should_stop()); + } while (!kthread_should_stop() || !list_empty(&hub_event_list)); (from drivers/usb/core/hub.c::hub_thread()). If a freezable kernel thread fails to call try_to_freeze() after the freezer has set TIF_FREEZE for it, the freezing of tasks will fail and the entire hibernation operation will be cancelled. For this reason, freezable kernel -threads must call try_to_freeze() somewhere. +threads must call try_to_freeze() somewhere or use one of the +wait_event_freezable() and wait_event_freezable_timeout() macros. After the system memory state has been restored from a hibernation image and devices have been reinitialized, the function thaw_processes() is called in
RFC PATCH 6/7 Freezer: Be more verbose
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
Increase the freezer's verbosity a bit, so that it's easier to read problem reports related to it.
--- kernel/power/process.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-)
kernel/power/process.c +10 -7
@@ -225,20 +225,23 @@ static int try_to_freeze_tasks(int freez*/ int freeze_processes (void ) { -int error; +int error =0 ; -printk ("Stopping tasks ... " ); +printk ("Freezing user space processes ... " ); error =try_to_freeze_tasks (FREEZER_USER_SPACE );if (error) -return error; + goto Exit; +printk ("done.\n" ); +printk ("Freezing remaining freezable tasks ... " ); error =try_to_freeze_tasks (FREEZER_KERNEL_THREADS );if (error) -return error; - -printk ("done.\n" ); + goto Exit; +printk ("done." ); + Exit:BUG_ON (in_atomic ()); -return 0 ; +printk ("\n" ); +return error; }static void thaw_tasks (int thaw_user_space)
RFC PATCH 7/7 Freezer: Use freezing timeout more efficiently
In reply to Rafael J. WysockiFrom: Rafael J. Wysocki <[email protected]>
There is the problem with try_to_freeze_tasks() that it always loops until the timeout expires, even if it is certain to fail much earlier. Namely, if there are uninterruptible tasks waiting for some frozen tasks to let them continue, try_to_freeze_tasks() will certainly fail and it shouldn't waste time in that cases.
To detect such situations, we can check if the number of tasks that haven't frozen yet changes between subsequent iterations of the main loop in try_to_freeze_tasks(). If this number hasn't been changing for sufficiently long time (say, 250 ms), then most probably some uninterruptible tasks are blocked by some frozen tasks and we should break out of this stalemate. Thus, it seems reasonable to thaw the tasks that have already been frozen without clearing the freeze requests of the tasks that are refusing to freeze. This way, if these tasks are really blocked by the frozen ones, they will get extra chance to freeze themselves after we have thawed the other tasks and before we request those tasks to freeze again. Next, the freezing loop can be repeated and so on, until all tasks are frozen or the timeout expires.
--- kernel/power/process.c | 84 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 14 deletions(-)
kernel/power/process.c +70 -14
@@ -14,10 +14,11 @@#include <linux/syscalls.h> #include <linux/freezer.h> -/* -* Timeout for stopping processes -*/ -#define TIMEOUT (20 * HZ) +/* Timeout for the freezing of tasks */ +#define TIMEOUT (10 * HZ) + +/* Timeout for breaking the freezing loop if stalemate state is detected */ +#define BREAK_TIMEOUT (HZ / 4)#define FREEZER_KERNEL_THREADS 0#define FREEZER_USER_SPACE 1
@@ -163,14 +164,31 @@ static void cancel_freezing(struct task_ } } +/** +* try_to_freeze_tasks - send freeze requests to all tasks and wait for +* for them to enter the refrigerator +* @freeze_user_space - if set, only tasks that have mm of their own are +* requested to freeze +* +* If this function fails, thaw_tasks() must be called to do the cleanup. +*/ +static int try_to_freeze_tasks (int freeze_user_space) {struct task_struct *g, *p; -unsigned long end_time; -unsigned int todo; +unsigned long end_time, break_time; +unsigned int todo, prev_todo; +unsigned int i =0 ; +char *tick ="-\\|/" ; + +printk (" " ); end_time = jiffies +TIMEOUT ; + Repeat: + todo =0 ; + break_time =0 ; do { + prev_todo = todo; todo = 0; read_lock(&tasklist_lock); do_each_thread(g, p) {
@@ -189,23 +207,61 @@ static int try_to_freeze_tasks(int freez todo++; }while_each_thread (g, p);read_unlock (&tasklist_lock); +yield ();/* Yield is okay here */ +if (time_after (jiffies, end_time))break ; + +/* +* Check if we are making any progress. If we aren't, it's +* better to break out of this. +*/ +if (todo == prev_todo) { +if (!break_time) + break_time = jiffies +BREAK_TIMEOUT ; +else if (time_after (jiffies, break_time)) +break ; + }else { + break_time =0 ; + } }while (todo); +if (todo && freeze_user_space && !time_after (jiffies, end_time)) { +/* +* Some tasks have not been able to freeze. They might be stuck +* in TASK_UNINTERRUPTIBLE waiting for the frozen tasks. Try to +* thaw the tasks that have frozen without clearing the freeze +* requests of the remaining tasks and repeat. +*/ +read_lock (&tasklist_lock); + do_each_thread(g, p) { +if (frozen (p)) { + p->flags &= ~PF_FROZEN ; +wake_up_process (p); + } + }while_each_thread (g, p); +read_unlock (&tasklist_lock); + +yield (); + +printk ("\b%c" , tick[i++%4 ]); + + goto Repeat; + } + +printk ("\b" ); +if (todo) { -/* This does not unfreeze processes that are already frozen -* (we have slightly ugly calling convention in that respect, -* and caller must call thaw_processes() if something fails), -* but it cleans up leftover PF_FREEZE requests. +/* +* The freezing of tasks has failed. List the tasks that have +* refused to freeze. This also clears all pending freeze +* requests. */ printk ("\n" ); -printk (KERN_ERR "Freezing of %s timed out after %d seconds " -"(%d tasks refusing to freeze):\n" , - freeze_user_space ?"user space " :"tasks " , -TIMEOUT /HZ , todo); +printk (KERN_ERR "Freezing of tasks has failed.\n" );show_state (); +printk (KERN_ERR "Tasks that have not been frozen:\n" );read_lock (&tasklist_lock); do_each_thread(g, p) {task_lock (p);
Re: [RFC][PATCH -mm 3/7] Freezer: Prevent new tasks from inheriting TIF_FREEZE set
In reply to Rafael J. WysockiHi.
Quoted 3 lines · On Thursday 12 July 2007 08:12:03 Rafael J. Wysocki wrote:
From: Rafael J. Wysocki <[email protected]>
Tasks should go to the refrigerator only if explicitly requested to do that
by
Quoted 1 line
the freezer and not as a result of inheriting the
TIF_FREEZEflag set from
the
Quoted 11 lines
parent. �Make it happen.
Signed-off-by Rafael J. Wysocki <[email protected]>Acked-by Pavel Machek <[email protected]>--- kernel/fork.c | 1 + 1 file changed, 1 insertion(+)Index:
linux-2.6.22-rc6-mm1/kernel/fork.c
===================================================================
---linux-2.6.22-rc6-mm1.orig/kernel/fork.c2007-07-11 20:48:04.000000000
+0200
kernel/fork.c static inline void copy_flags(unsigned l · line 936
Quoted 7 lines
+++
linux-2.6.22-rc6-mm1/kernel/fork.c2007-07-11 20:50:42.000000000 +0200kernel/fork.c +1
-0@@ -932,6 +932,7 @@ static inline void copy_flags(unsigned lif (!(clone_flags &CLONE_PTRACE )) p->ptrace =0 ; p->flags = new_flags; +clear_freeze_flag (p); }
Regards,
Nigel
Signature · 2 lines
See http://www.tuxonice.net for Howtos, FAQs, mailing lists, wiki and bugzilla info.
Re: [RFC][PATCH -mm 2/7] Freezer: Do not sync filesystems from freeze_processes
In reply to Rafael J. WysockiQuoted 7 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
The syncing of filesystems from within the freezer is generally not needed. Also, if there's an ext3 filesystem loopback-mounted from a FUSE one, the syncing results in writes to it and deadlocks. Similarly, it will deadlock if FUSE implements sync.
Quoted 5 lines
Change
freeze_processes()so that it doesn't executesys_sync()and make the suspend and hibernation code paths sync filesystems independently of the freezer.Signed-off-by Rafael J. Wysocki <[email protected]>
ACK. Nice to see the fuse problem to disappear.
Pavel
Quoted 5 lines
Index:
linux-2.6.22-rc6-mm1/kernel/power/user.c
===================================================================
kernel/power/user.c static int snapshot_ioctl(struct inode * · line 162
Quoted 10 lines
kernel/power/user.c +4
-0@@ -153,6 +153,10 @@ static int snapshot_ioctl(struct inode *mutex_lock (&pm_mutex); error =pm_notifier_call_chain (PM_HIBERNATION_PREPARE );if (!error) { +printk ("Syncing filesystems ... " ); +sys_sync (); +printk ("done.\n" ); + error =freeze_processes ();if (error)thaw_processes ();
I see this is back-compatible, but can we do the sync in userspace, instead?
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 4/7] Freezer: Introduce freezer-firendly waiting macros
In reply to Rafael J. WysockiHi!
Quoted 11 lines
From: Rafael J. Wysocki <[email protected]>
Introduce freezer-friendly wrappers around
wait_event_interruptible()andwait_event_interruptible_timeout(), originally defined in <linux/wait.h>, to be used in freezable kernel threads. Make some of the freezable kernel threads use them.This is necessary for the freezer to stop sending signals to kernel threads, which is implemented in the next patch.
Signed-off-by Rafael J. Wysocki <[email protected]>
ACK, but...
Quoted 15 lines
include/linux/freezer.h +15
-0+/* +* Freezer-friendly wrappers around wait_event_interruptible() and +* wait_event_interruptible_timeout(), originally defined in <linux/wait.h> +*/ + +#define wait_event_freezable (wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + try_to_freeze(); \ + } while (!(condition)); \ + __ret; \ +})
...
drivers/media/dvb/dvb-core/dvb_frontend.c static int dvb_frontend_thread(void *dat · line 533
Quoted 12 lines
Index:
linux-2.6.22-rc6-mm1/drivers/media/dvb/dvb-core/dvb_frontend.c
===================================================================drivers/media/dvb/dvb-core/dvb_frontend.c +2
-1@@ -528,7 +528,8 @@ static int dvb_frontend_thread(void *datup (&fepriv->sem);/* is locked when we enter the thread... */ restart: timeout =wait_event_interruptible_timeout (fepriv->wait_queue, -dvb_frontend_should_wakeup (fe) ||kthread_should_stop (), +dvb_frontend_should_wakeup (fe) ||kthread_should_stop () + ||freezing (current), fepriv->delay);
Should this use the new helper? This change does not seem to match the rest.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 6/7] Freezer: Be more verbose
In reply to Rafael J. WysockiQuoted 7 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Increase the freezer's verbosity a bit, so that it's easier to read problem reports related to it.
Signed-off-by Rafael J. Wysocki <[email protected]>Acked-by Pavel Machek <[email protected]>
Tiny nit:
kernel/power/process.c static int try_to_freeze_tasks(int freez · line 228
Quoted 4 lines
int
freeze_processes(void)
{kernel/power/process.c +1
-1-int error; +int error =0 ;
You still did not kill that = 0 :-).
kernel/power/process.c static int try_to_freeze_tasks(int freez · line 231
Quoted 3 lines
kernel/power/process.c +1
-1-printk ("Stopping tasks ... " ); +printk ("Freezing user space processes ... " ); error =try_to_freeze_tasks (FREEZER_USER_SPACE );
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 4/7] Freezer: Introduce freezer-firendly waiting macros
In reply to Rafael J. Wysockiinclude/linux/freezer.h static inline void set_freezable(void) · line 144
Quoted 11 lines · Rafael J. Wysocki
include/linux/freezer.h +10
-0+#define wait_event_freezable (wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + try_to_freeze(); \ + } while (!(condition)); \ + __ret; \ +})
This is still not right in general, I believe.
It will spin if the caller is signalled. You changed the freezer, but kthread can use signals anyway.
It returns only when "condition" is true, so the "__ret" is meaningless.
Oleg.
RFC PATCH 4/7 Freezer: Introduce freezer-firendly waiting macros (updated)
Quoted 15 lines · Oleg Nesterov
Review
Quoted 11 lines · Rafael J. Wysocki
(quoted diff) +10
-0+#define wait_event_freezable(wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + try_to_freeze(); \ + } while (!(condition)); \ + __ret; \ +})This is still not right in general, I believe.
Quoted 4 lines
It will spin if the caller is signalled. You changed the freezer, but kthread can use signals anyway.
It returns only when "condition" is true, so the "
__ret" is meaningless.
Yes, this is a mistake. The second one is also wrong. :-(
Please see the fixed patch below.
Greetings,
Rafael
---
From: Rafael J. Wysocki <[email protected]>
Introduce freezer-friendly wrappers around wait_event_interruptible() and wait_event_interruptible_timeout(), originally defined in <linux/wait.h>, to be used in freezable kernel threads. Make some of the freezable kernel threads use them.
This is necessary for the freezer to stop sending signals to kernel threads, which is implemented in the next patch.
--- drivers/input/gameport/gameport.c | 3 -- drivers/input/serio/serio.c | 3 -- drivers/input/touchscreen/ucb1400_ts.c | 3 -- drivers/media/dvb/dvb-core/dvb_frontend.c | 3 +- drivers/usb/core/hub.c | 3 -- drivers/usb/storage/usb.c | 5 --- include/linux/freezer.h | 42 ++++++++++++++++++++++++++++-- 7 files changed, 47 insertions(+), 15 deletions(-)
include/linux/freezer.h +40 -2
@@ -4,6 +4,7 @@#define FREEZER_H_INCLUDED #include <linux/sched.h> +#include <linux/wait.h> #ifdef CONFIG_PM /*
@@ -126,7 +127,37 @@ static inline void set_freezable(void) current->flags &= ~PF_NOFREEZE; } -#else +/* +* Freezer-friendly wrappers around wait_event_interruptible() and +* wait_event_interruptible_timeout(), originally defined in <linux/wait.h> +*/ + +#define wait_event_freezable (wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + if (!__ret && !freezing(current)) \ + break; \ + else if (!(condition)) \ + __ret = -ERESTARTSYS; \ + } while (try_to_freeze()); \ + __ret; \ +}) + + +#define wait_event_freezable_timeout (wq, condition, timeout) \ +({ \ + long __ret = timeout; \ + do { \ + __ret = wait_event_interruptible_timeout(wq, \ + (condition) || freezing(current), \ + __ret); \ + } while (try_to_freeze()); \ + __ret; \ +}) +#else /* CONFIG_PM */ static inline int frozen (struct task_struct *p) {return 0 ; }static inline int freezing (struct task_struct *p) {return 0 ; }static inline void set_freeze_flag (struct task_struct *p) {}
@@ -143,6 +174,13 @@ static inline void freezer_do_not_count(static inline void freezer_count (void ) {}static inline int freezer_should_skip (struct task_struct *p) {return 0 ; }static inline void set_freezable (void ) {} -#endif + +#define wait_event_freezable (wq, condition) \ + wait_event_interruptible(wq, condition) + +#define wait_event_freezable_timeout (wq, condition, timeout) \ + wait_event_interruptible_timeout(wq, condition, timeout) + +#endif /* CONFIG_PM */ #endif /* FREEZER_H_INCLUDED */
drivers/input/gameport/gameport.c +1 -2
@@ -448,9 +448,8 @@ static int gameport_thread(void *nothingset_freezable ();do {gameport_handle_event (); -wait_event_interruptible (gameport_wait, +wait_event_freezable (gameport_wait,kthread_should_stop () || !list_empty (&gameport_event_list)); -try_to_freeze (); }while (!kthread_should_stop ());printk (KERN_DEBUG "gameport: kgameportd exiting\n" );
drivers/input/serio/serio.c +1 -2
@@ -387,9 +387,8 @@ static int serio_thread(void *nothing)set_freezable ();do {serio_handle_event (); -wait_event_interruptible (serio_wait, +wait_event_freezable (serio_wait,kthread_should_stop () || !list_empty (&serio_event_list)); -try_to_freeze (); }while (!kthread_should_stop ());printk (KERN_DEBUG "serio: kseriod exiting\n" );
drivers/input/touchscreen/ucb1400_ts.c +1 -2
@@ -334,10 +334,9 @@ static int ucb1400_ts_thread(void *_ucb) timeout =msecs_to_jiffies (10 ); } -wait_event_interruptible_timeout (ucb->ts_wait, +wait_event_freezable_timeout (ucb->ts_wait, ucb->irq_pending || ucb->ts_restart ||kthread_should_stop (), timeout); -try_to_freeze (); }/* Send the "pen off" if we are stopping with the pen still active */
drivers/media/dvb/dvb-core/dvb_frontend.c +2 -1
@@ -528,7 +528,8 @@ static int dvb_frontend_thread(void *datup (&fepriv->sem);/* is locked when we enter the thread... */ restart: timeout =wait_event_interruptible_timeout (fepriv->wait_queue, -dvb_frontend_should_wakeup (fe) ||kthread_should_stop (), +dvb_frontend_should_wakeup (fe) ||kthread_should_stop () + ||freezing (current), fepriv->delay);if (kthread_should_stop () ||dvb_frontend_is_exiting (fe)) {
drivers/usb/core/hub.c +1 -2
@@ -2729,10 +2729,9 @@ static int hub_thread(void *__unused)set_freezable ();do {hub_events (); -wait_event_interruptible (khubd_wait, +wait_event_freezable (khubd_wait, !list_empty (&hub_event_list) ||kthread_should_stop ()); -try_to_freeze (); }while (!kthread_should_stop () || !list_empty (&hub_event_list));pr_debug ("%s: khubd exiting\n" , usbcore_name);
drivers/usb/storage/usb.c +1 -4
@@ -913,12 +913,9 @@ static int usb_stor_scan_thread(void * _if (delay_use >0 ) {printk (KERN_DEBUG "usb-storage: waiting for device " "to settle before scanning\n" ); -retry: -wait_event_interruptible_timeout (us->delay_wait, +wait_event_freezable_timeout (us->delay_wait,test_bit (US_FLIDX_DISCONNECTING , &us->flags), delay_use *HZ ); -if (try_to_freeze ()) - goto retry; }/* If the device is still connected, perform the scanning */
Re: [RFC][PATCH -mm 4/7] Freezer: Introduce freezer-firendly waiting macros
In reply to Pavel MachekQuoted 34 lines · Pavel Machek
Hi!
Quoted 11 lines
From: Rafael J. Wysocki <[email protected]>
Introduce freezer-friendly wrappers around
wait_event_interruptible()andwait_event_interruptible_timeout(), originally defined in <linux/wait.h>, to be used in freezable kernel threads. Make some of the freezable kernel threads use them.This is necessary for the freezer to stop sending signals to kernel threads, which is implemented in the next patch.
Signed-off-by Rafael J. Wysocki <[email protected]>ACK, but...
Quoted 15 lines
include/linux/freezer.h +15
-0+/* +* Freezer-friendly wrappers around wait_event_interruptible() and +* wait_event_interruptible_timeout(), originally defined in <linux/wait.h> +*/ + +#define wait_event_freezable (wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + try_to_freeze(); \ + } while (!(condition)); \ + __ret; \ +})...
Quoted 14 lines
drivers/media/dvb/dvb-core/dvb_frontend.c static int dvb_frontend_thread(void *dat · line 533
Quoted 12 lines
Index:
linux-2.6.22-rc6-mm1/drivers/media/dvb/dvb-core/dvb_frontend.c
===================================================================drivers/media/dvb/dvb-core/dvb_frontend.c +2
-1@@ -528,7 +528,8 @@ static int dvb_frontend_thread(void *datup (&fepriv->sem);/* is locked when we enter the thread... */ restart: timeout =wait_event_interruptible_timeout (fepriv->wait_queue, -dvb_frontend_should_wakeup (fe) ||kthread_should_stop (), +dvb_frontend_should_wakeup (fe) ||kthread_should_stop () + ||freezing (current), fepriv->delay);Should this use the new helper? This change does not seem to match the rest.
Yes, because it uses something like the new helper already, but with some code in between., so I decided to add the freezing(current) only to it.
BTW, please see the updated patch in the reply to Oleg (the macros have been fixed).
Greetings,
Rafael
Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth
RFC PATCH 7/7 Freezer: Use freezing timeout more efficiently (updated)
In reply to Rafael J. Wysocki[The previous version didn't pass some tests.]
---
From: Rafael J. Wysocki <[email protected]>
There is the problem with try_to_freeze_tasks() that it always loops until the timeout expires, even if it is certain to fail much earlier. Namely, if there are uninterruptible tasks waiting for some frozen tasks to let them continue, try_to_freeze_tasks() will certainly fail and it shouldn't waste time in that cases.
To detect such situations, we can check if the number of tasks that haven't frozen yet changes between subsequent iterations of the main loop in try_to_freeze_tasks(). If this number hasn't been changing for sufficiently long time (say, 250 ms), then most probably some uninterruptible tasks are blocked by some frozen tasks and we should break out of this stalemate. Thus, it seems reasonable to thaw the tasks that have already been frozen without clearing the freeze requests of the tasks that are refusing to freeze. This way, if these tasks are really blocked by the frozen ones, they will get extra chance to freeze themselves after we have thawed the other tasks and before we request those tasks to freeze again. Next, the freezing loop can be repeated and so on, until all tasks are frozen or the timeout expires.
--- kernel/power/process.c | 86 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 14 deletions(-)
kernel/power/process.c +72 -14
@@ -14,10 +14,11 @@#include <linux/syscalls.h> #include <linux/freezer.h> -/* -* Timeout for stopping processes -*/ -#define TIMEOUT (20 * HZ) +/* Timeout for the freezing of tasks */ +#define TIMEOUT (10 * HZ) + +/* Timeout for breaking the freezing loop if stalemate state is detected */ +#define BREAK_TIMEOUT (HZ / 4)#define FREEZER_KERNEL_THREADS 0#define FREEZER_USER_SPACE 1
@@ -163,14 +164,31 @@ static void cancel_freezing(struct task_ } } +/** +* try_to_freeze_tasks - send freeze requests to all tasks and wait for +* for them to enter the refrigerator +* @freeze_user_space - if set, only tasks that have mm of their own are +* requested to freeze +* +* If this function fails, thaw_tasks() must be called to do the cleanup. +*/ +static int try_to_freeze_tasks (int freeze_user_space) {struct task_struct *g, *p; -unsigned long end_time; -unsigned int todo; +unsigned long end_time, break_time; +unsigned int todo, prev_todo; +unsigned int i =0 ; +char *tick ="-\\|/" ; + +printk (" " ); end_time = jiffies +TIMEOUT ; + Repeat: + todo =0 ; + break_time =0 ; do { + prev_todo = todo; todo = 0; read_lock(&tasklist_lock); do_each_thread(g, p) {
@@ -189,23 +207,63 @@ static int try_to_freeze_tasks(int freez todo++; }while_each_thread (g, p);read_unlock (&tasklist_lock); +yield ();/* Yield is okay here */ +if (time_after (jiffies, end_time))break ; + +if (freeze_user_space) { +/* +* Check if we are making any progress. If we aren't, +* it's better to break out of this. +*/ +if (todo == prev_todo) { +if (!break_time) + break_time = jiffies +BREAK_TIMEOUT ; +else if (time_after (jiffies, break_time)) +break ; + }else { + break_time =0 ; + } + } }while (todo); +if (todo && freeze_user_space && !time_after (jiffies, end_time)) { +/* +* Some tasks have not been able to freeze. They might be stuck +* in TASK_UNINTERRUPTIBLE waiting for the frozen tasks. Try to +* thaw the tasks that have frozen without clearing the freeze +* requests of the remaining tasks and repeat. +*/ +read_lock (&tasklist_lock); + do_each_thread(g, p) { +if (frozen (p)) { + p->flags &= ~PF_FROZEN ; +wake_up_process (p); + } + }while_each_thread (g, p); +read_unlock (&tasklist_lock); + +yield (); + +printk ("\b%c" , tick[i++%4 ]); + + goto Repeat; + } + +printk ("\b" ); +if (todo) { -/* This does not unfreeze processes that are already frozen -* (we have slightly ugly calling convention in that respect, -* and caller must call thaw_processes() if something fails), -* but it cleans up leftover PF_FREEZE requests. +/* +* The freezing of tasks has failed. List the tasks that have +* refused to freeze. This also clears all pending freeze +* requests. */ printk ("\n" ); -printk (KERN_ERR "Freezing of %s timed out after %d seconds " -"(%d tasks refusing to freeze):\n" , - freeze_user_space ?"user space " :"tasks " , -TIMEOUT /HZ , todo); +printk (KERN_ERR "Freezing of tasks has failed.\n" );show_state (); +printk (KERN_ERR "Tasks that have not been frozen:\n" );read_lock (&tasklist_lock); do_each_thread(g, p) {task_lock (p);
RFC PATCH 6/7 Freezer: Be more verbose (updated)
In reply to Pavel MachekQuoted 9 lines · Pavel Machek
Quoted 7 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Increase the freezer's verbosity a bit, so that it's easier to read problem reports related to it.
Signed-off-by Rafael J. Wysocki <[email protected]>Acked-by Pavel Machek <[email protected]>
Quoted 8 lines
Tiny nit:
Review
Quoted 4 lines
int
freeze_processes(void)
{(quoted diff) +1
-1- int error; + int error = 0;You still did not kill that = 0 :-).
Yes, absolutely.
Updated patch follows.
Greetings,
Rafael
---
From: Rafael J. Wysocki <[email protected]>
Increase the freezer's verbosity a bit, so that it's easier to read problem reports related to it.
--- kernel/power/process.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
kernel/power/process.c +9 -6
@@ -227,18 +227,21 @@ int freeze_processes(void) {int error; -printk ("Stopping tasks ... " ); +printk ("Freezing user space processes ... " ); error =try_to_freeze_tasks (FREEZER_USER_SPACE );if (error) -return error; + goto Exit; +printk ("done.\n" ); +printk ("Freezing remaining freezable tasks ... " ); error =try_to_freeze_tasks (FREEZER_KERNEL_THREADS );if (error) -return error; - -printk ("done.\n" ); + goto Exit; +printk ("done." ); + Exit:BUG_ON (in_atomic ()); -return 0 ; +printk ("\n" ); +return error; }static void thaw_tasks (int thaw_user_space)
RFC PATCH 4/7 Freezer: Introduce freezer-firendly waiting macros (updated 2x)
In reply to Rafael J. WysockiQuoted 16 lines · Rafael J. Wysocki
Quoted 15 lines · Oleg Nesterov
Review
Quoted 11 lines · Rafael J. Wysocki
(quoted diff) +10
-0+#define wait_event_freezable(wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + try_to_freeze(); \ + } while (!(condition)); \ + __ret; \ +})This is still not right in general, I believe.
Quoted 8 lines
Quoted 4 lines
It will spin if the caller is signalled. You changed the freezer, but kthread can use signals anyway.
It returns only when "condition" is true, so the "
__ret" is meaningless.Yes, this is a mistake. The second one is also wrong. :-(
Please see the fixed patch below.
Ugh, there still is a mistake in there, sorry.
[--snip--]
Review
Quoted 8 lines
(quoted diff) +8
-0+ +#define wait_event_freezable(wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + if (!__ret && !freezing(current)) \
There should be (__ret && !freezing(current)) here (ie. if we've been woken up by a signal and this is not the freezing signal, break out of the loop.
Below is the corrected version.
Greetings,
Rafael
---
From: Rafael J. Wysocki <[email protected]>
Introduce freezer-friendly wrappers around wait_event_interruptible() and wait_event_interruptible_timeout(), originally defined in <linux/wait.h>, to be used in freezable kernel threads. Make some of the freezable kernel threads use them.
This is necessary for the freezer to stop sending signals to kernel threads, which is implemented in the next patch.
--- drivers/input/gameport/gameport.c | 3 -- drivers/input/serio/serio.c | 3 -- drivers/input/touchscreen/ucb1400_ts.c | 3 -- drivers/media/dvb/dvb-core/dvb_frontend.c | 3 +- drivers/usb/core/hub.c | 3 -- drivers/usb/storage/usb.c | 5 --- include/linux/freezer.h | 42 ++++++++++++++++++++++++++++-- 7 files changed, 47 insertions(+), 15 deletions(-)
include/linux/freezer.h +40 -2
@@ -4,6 +4,7 @@#define FREEZER_H_INCLUDED #include <linux/sched.h> +#include <linux/wait.h> #ifdef CONFIG_PM /*
@@ -126,7 +127,37 @@ static inline void set_freezable(void) current->flags &= ~PF_NOFREEZE; } -#else +/* +* Freezer-friendly wrappers around wait_event_interruptible() and +* wait_event_interruptible_timeout(), originally defined in <linux/wait.h> +*/ + +#define wait_event_freezable (wq, condition) \ +({ \ + int __ret; \ + do { \ + __ret = wait_event_interruptible(wq, \ + (condition) || freezing(current)); \ + if (__ret && !freezing(current)) \ + break; \ + else if (!(condition)) \ + __ret = -ERESTARTSYS; \ + } while (try_to_freeze()); \ + __ret; \ +}) + + +#define wait_event_freezable_timeout (wq, condition, timeout) \ +({ \ + long __ret = timeout; \ + do { \ + __ret = wait_event_interruptible_timeout(wq, \ + (condition) || freezing(current), \ + __ret); \ + } while (try_to_freeze()); \ + __ret; \ +}) +#else /* CONFIG_PM */ static inline int frozen (struct task_struct *p) {return 0 ; }static inline int freezing (struct task_struct *p) {return 0 ; }static inline void set_freeze_flag (struct task_struct *p) {}
@@ -143,6 +174,13 @@ static inline void freezer_do_not_count(static inline void freezer_count (void ) {}static inline int freezer_should_skip (struct task_struct *p) {return 0 ; }static inline void set_freezable (void ) {} -#endif + +#define wait_event_freezable (wq, condition) \ + wait_event_interruptible(wq, condition) + +#define wait_event_freezable_timeout (wq, condition, timeout) \ + wait_event_interruptible_timeout(wq, condition, timeout) + +#endif /* CONFIG_PM */ #endif /* FREEZER_H_INCLUDED */
drivers/input/gameport/gameport.c +1 -2
@@ -448,9 +448,8 @@ static int gameport_thread(void *nothingset_freezable ();do {gameport_handle_event (); -wait_event_interruptible (gameport_wait, +wait_event_freezable (gameport_wait,kthread_should_stop () || !list_empty (&gameport_event_list)); -try_to_freeze (); }while (!kthread_should_stop ());printk (KERN_DEBUG "gameport: kgameportd exiting\n" );
drivers/input/serio/serio.c +1 -2
@@ -387,9 +387,8 @@ static int serio_thread(void *nothing)set_freezable ();do {serio_handle_event (); -wait_event_interruptible (serio_wait, +wait_event_freezable (serio_wait,kthread_should_stop () || !list_empty (&serio_event_list)); -try_to_freeze (); }while (!kthread_should_stop ());printk (KERN_DEBUG "serio: kseriod exiting\n" );
drivers/input/touchscreen/ucb1400_ts.c +1 -2
@@ -334,10 +334,9 @@ static int ucb1400_ts_thread(void *_ucb) timeout =msecs_to_jiffies (10 ); } -wait_event_interruptible_timeout (ucb->ts_wait, +wait_event_freezable_timeout (ucb->ts_wait, ucb->irq_pending || ucb->ts_restart ||kthread_should_stop (), timeout); -try_to_freeze (); }/* Send the "pen off" if we are stopping with the pen still active */
drivers/media/dvb/dvb-core/dvb_frontend.c +2 -1
@@ -528,7 +528,8 @@ static int dvb_frontend_thread(void *datup (&fepriv->sem);/* is locked when we enter the thread... */ restart: timeout =wait_event_interruptible_timeout (fepriv->wait_queue, -dvb_frontend_should_wakeup (fe) ||kthread_should_stop (), +dvb_frontend_should_wakeup (fe) ||kthread_should_stop () + ||freezing (current), fepriv->delay);if (kthread_should_stop () ||dvb_frontend_is_exiting (fe)) {
drivers/usb/core/hub.c +1 -2
@@ -2729,10 +2729,9 @@ static int hub_thread(void *__unused)set_freezable ();do {hub_events (); -wait_event_interruptible (khubd_wait, +wait_event_freezable (khubd_wait, !list_empty (&hub_event_list) ||kthread_should_stop ()); -try_to_freeze (); }while (!kthread_should_stop () || !list_empty (&hub_event_list));pr_debug ("%s: khubd exiting\n" , usbcore_name);
drivers/usb/storage/usb.c +1 -4
@@ -913,12 +913,9 @@ static int usb_stor_scan_thread(void * _if (delay_use >0 ) {printk (KERN_DEBUG "usb-storage: waiting for device " "to settle before scanning\n" ); -retry: -wait_event_interruptible_timeout (us->delay_wait, +wait_event_freezable_timeout (us->delay_wait,test_bit (US_FLIDX_DISCONNECTING , &us->flags), delay_use *HZ ); -if (try_to_freeze ()) - goto retry; }/* If the device is still connected, perform the scanning */
Re: [RFC][PATCH -mm 7/7] Freezer: Use freezing timeout more efficiently
In reply to Rafael J. WysockiHi!
Quoted 6 lines
There is the problem with
try_to_freeze_tasks()that it always loops until the timeout expires, even if it is certain to fail much earlier. Namely, if there are uninterruptible tasks waiting for some frozen tasks to let them continue,try_to_freeze_tasks()will certainly fail and it shouldn't waste time in that cases.
Quoted 11 lines
To detect such situations, we can check if the number of tasks that haven't frozen yet changes between subsequent iterations of the main loop in
try_to_freeze_tasks(). If this number hasn't been changing for sufficiently long time (say, 250 ms), then most probably some uninterruptible tasks are blocked by some frozen tasks and we should break out of this stalemate. Thus, it seems reasonable to thaw the tasks that have already been frozen without clearing the freeze requests of the tasks that are refusing to freeze. This way, if these tasks are really blocked by the frozen ones, they will get extra chance to freeze themselves after we have thawed the other tasks and before we request those tasks to freeze again. Next, the freezing loop can be repeated and so on, until all tasks are frozen or the timeout expires.
I still don't quite like this patch... it is kind of trick we should not have to play. Can we at least get confirmation it helps in the FUSE case?
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
Re: [RFC][PATCH -mm 7/7] Freezer: Use freezing timeout more efficiently
Quoted 20 lines · Pavel Machek
Hi!
Quoted 6 lines
There is the problem with
try_to_freeze_tasks()that it always loops until the timeout expires, even if it is certain to fail much earlier. Namely, if there are uninterruptible tasks waiting for some frozen tasks to let them continue,try_to_freeze_tasks()will certainly fail and it shouldn't waste time in that cases.Quoted 11 lines
To detect such situations, we can check if the number of tasks that haven't frozen yet changes between subsequent iterations of the main loop in
try_to_freeze_tasks(). If this number hasn't been changing for sufficiently long time (say, 250 ms), then most probably some uninterruptible tasks are blocked by some frozen tasks and we should break out of this stalemate. Thus, it seems reasonable to thaw the tasks that have already been frozen without clearing the freeze requests of the tasks that are refusing to freeze. This way, if these tasks are really blocked by the frozen ones, they will get extra chance to freeze themselves after we have thawed the other tasks and before we request those tasks to freeze again. Next, the freezing loop can be repeated and so on, until all tasks are frozen or the timeout expires.
Quoted 3 lines
I still don't quite like this patch... it is kind of trick we should not have to play. Can we at least get confirmation it helps in the FUSE case?
Well, anyone having problems with FUSE vs the freezer please check. :-)
I think I'll send the series to Andrew without this patch. Would that be OK?
Greetings,
Rafael
Signature · 1 line
"Premature optimization is the root of all evil." - Donald Knuth