Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/power/process.c - Functions for starting/stopping processes on
0004  *                           suspend transitions.
0005  *
0006  * Originally from swsusp.
0007  */
0008 
0009 #include <linux/interrupt.h>
0010 #include <linux/oom.h>
0011 #include <linux/suspend.h>
0012 #include <linux/module.h>
0013 #include <linux/sched/debug.h>
0014 #include <linux/sched/task.h>
0015 #include <linux/syscalls.h>
0016 #include <linux/freezer.h>
0017 #include <linux/delay.h>
0018 #include <linux/workqueue.h>
0019 #include <linux/kmod.h>
0020 #include <trace/events/power.h>
0021 #include <linux/cpuset.h>
0022 
0023 /*
0024  * Timeout for stopping processes
0025  */
0026 unsigned int __read_mostly freeze_timeout_msecs = 20 * MSEC_PER_SEC;
0027 
0028 static int try_to_freeze_tasks(bool user_only)
0029 {
0030     struct task_struct *g, *p;
0031     unsigned long end_time;
0032     unsigned int todo;
0033     bool wq_busy = false;
0034     ktime_t start, end, elapsed;
0035     unsigned int elapsed_msecs;
0036     bool wakeup = false;
0037     int sleep_usecs = USEC_PER_MSEC;
0038 
0039     start = ktime_get_boottime();
0040 
0041     end_time = jiffies + msecs_to_jiffies(freeze_timeout_msecs);
0042 
0043     if (!user_only)
0044         freeze_workqueues_begin();
0045 
0046     while (true) {
0047         todo = 0;
0048         read_lock(&tasklist_lock);
0049         for_each_process_thread(g, p) {
0050             if (p == current || !freeze_task(p))
0051                 continue;
0052 
0053             if (!freezer_should_skip(p))
0054                 todo++;
0055         }
0056         read_unlock(&tasklist_lock);
0057 
0058         if (!user_only) {
0059             wq_busy = freeze_workqueues_busy();
0060             todo += wq_busy;
0061         }
0062 
0063         if (!todo || time_after(jiffies, end_time))
0064             break;
0065 
0066         if (pm_wakeup_pending()) {
0067             wakeup = true;
0068             break;
0069         }
0070 
0071         /*
0072          * We need to retry, but first give the freezing tasks some
0073          * time to enter the refrigerator.  Start with an initial
0074          * 1 ms sleep followed by exponential backoff until 8 ms.
0075          */
0076         usleep_range(sleep_usecs / 2, sleep_usecs);
0077         if (sleep_usecs < 8 * USEC_PER_MSEC)
0078             sleep_usecs *= 2;
0079     }
0080 
0081     end = ktime_get_boottime();
0082     elapsed = ktime_sub(end, start);
0083     elapsed_msecs = ktime_to_ms(elapsed);
0084 
0085     if (todo) {
0086         pr_cont("\n");
0087         pr_err("Freezing of tasks %s after %d.%03d seconds "
0088                "(%d tasks refusing to freeze, wq_busy=%d):\n",
0089                wakeup ? "aborted" : "failed",
0090                elapsed_msecs / 1000, elapsed_msecs % 1000,
0091                todo - wq_busy, wq_busy);
0092 
0093         if (wq_busy)
0094             show_all_workqueues();
0095 
0096         if (!wakeup || pm_debug_messages_on) {
0097             read_lock(&tasklist_lock);
0098             for_each_process_thread(g, p) {
0099                 if (p != current && !freezer_should_skip(p)
0100                     && freezing(p) && !frozen(p))
0101                     sched_show_task(p);
0102             }
0103             read_unlock(&tasklist_lock);
0104         }
0105     } else {
0106         pr_cont("(elapsed %d.%03d seconds) ", elapsed_msecs / 1000,
0107             elapsed_msecs % 1000);
0108     }
0109 
0110     return todo ? -EBUSY : 0;
0111 }
0112 
0113 /**
0114  * freeze_processes - Signal user space processes to enter the refrigerator.
0115  * The current thread will not be frozen.  The same process that calls
0116  * freeze_processes must later call thaw_processes.
0117  *
0118  * On success, returns 0.  On failure, -errno and system is fully thawed.
0119  */
0120 int freeze_processes(void)
0121 {
0122     int error;
0123 
0124     error = __usermodehelper_disable(UMH_FREEZING);
0125     if (error)
0126         return error;
0127 
0128     /* Make sure this task doesn't get frozen */
0129     current->flags |= PF_SUSPEND_TASK;
0130 
0131     if (!pm_freezing)
0132         atomic_inc(&system_freezing_cnt);
0133 
0134     pm_wakeup_clear(0);
0135     pr_info("Freezing user space processes ... ");
0136     pm_freezing = true;
0137     error = try_to_freeze_tasks(true);
0138     if (!error) {
0139         __usermodehelper_set_disable_depth(UMH_DISABLED);
0140         pr_cont("done.");
0141     }
0142     pr_cont("\n");
0143     BUG_ON(in_atomic());
0144 
0145     /*
0146      * Now that the whole userspace is frozen we need to disable
0147      * the OOM killer to disallow any further interference with
0148      * killable tasks. There is no guarantee oom victims will
0149      * ever reach a point they go away we have to wait with a timeout.
0150      */
0151     if (!error && !oom_killer_disable(msecs_to_jiffies(freeze_timeout_msecs)))
0152         error = -EBUSY;
0153 
0154     if (error)
0155         thaw_processes();
0156     return error;
0157 }
0158 
0159 /**
0160  * freeze_kernel_threads - Make freezable kernel threads go to the refrigerator.
0161  *
0162  * On success, returns 0.  On failure, -errno and only the kernel threads are
0163  * thawed, so as to give a chance to the caller to do additional cleanups
0164  * (if any) before thawing the userspace tasks. So, it is the responsibility
0165  * of the caller to thaw the userspace tasks, when the time is right.
0166  */
0167 int freeze_kernel_threads(void)
0168 {
0169     int error;
0170 
0171     pr_info("Freezing remaining freezable tasks ... ");
0172 
0173     pm_nosig_freezing = true;
0174     error = try_to_freeze_tasks(false);
0175     if (!error)
0176         pr_cont("done.");
0177 
0178     pr_cont("\n");
0179     BUG_ON(in_atomic());
0180 
0181     if (error)
0182         thaw_kernel_threads();
0183     return error;
0184 }
0185 
0186 void thaw_processes(void)
0187 {
0188     struct task_struct *g, *p;
0189     struct task_struct *curr = current;
0190 
0191     trace_suspend_resume(TPS("thaw_processes"), 0, true);
0192     if (pm_freezing)
0193         atomic_dec(&system_freezing_cnt);
0194     pm_freezing = false;
0195     pm_nosig_freezing = false;
0196 
0197     oom_killer_enable();
0198 
0199     pr_info("Restarting tasks ... ");
0200 
0201     __usermodehelper_set_disable_depth(UMH_FREEZING);
0202     thaw_workqueues();
0203 
0204     cpuset_wait_for_hotplug();
0205 
0206     read_lock(&tasklist_lock);
0207     for_each_process_thread(g, p) {
0208         /* No other threads should have PF_SUSPEND_TASK set */
0209         WARN_ON((p != curr) && (p->flags & PF_SUSPEND_TASK));
0210         __thaw_task(p);
0211     }
0212     read_unlock(&tasklist_lock);
0213 
0214     WARN_ON(!(curr->flags & PF_SUSPEND_TASK));
0215     curr->flags &= ~PF_SUSPEND_TASK;
0216 
0217     usermodehelper_enable();
0218 
0219     schedule();
0220     pr_cont("done.\n");
0221     trace_suspend_resume(TPS("thaw_processes"), 0, false);
0222 }
0223 
0224 void thaw_kernel_threads(void)
0225 {
0226     struct task_struct *g, *p;
0227 
0228     pm_nosig_freezing = false;
0229     pr_info("Restarting kernel threads ... ");
0230 
0231     thaw_workqueues();
0232 
0233     read_lock(&tasklist_lock);
0234     for_each_process_thread(g, p) {
0235         if (p->flags & PF_KTHREAD)
0236             __thaw_task(p);
0237     }
0238     read_unlock(&tasklist_lock);
0239 
0240     schedule();
0241     pr_cont("done.\n");
0242 }