Linux Kernel and Android sleep and wake-up

Source: Internet
Author: User
Tags linux sleep
Tags: Linux and kernel android

Version Information

Linux Kernel: v2.6.28
Android: V2.0

Introduction to sleep (suspend)

In Linux, sleep mainly involves three steps: Version Information

  • Linux Kernel: v2.6.28
  • Android: V2.0
Introduction to sleep (suspend)

In Linux, sleep mainly involves three steps:

  1. Freeze user-State processes and kernel-state tasks
  2. Call the suspend callback function of the registered device.
    • The order is the registration order.
  3. Sleep the core device and sleep the CPU into a sleep frozen process. The kernel sets all the processes in the process list to stop and saves the context of all processes. when these processes are restored, they do not know that they have been frozen, but simply continue to execute. how can we break Linux into sleep? You can read and write the Sys File/sys/power/State to enable the control system to sleep. For example
    # echo standby > /sys/power/state

    The command system goes to sleep. You can also use

    # cat /sys/power/state

    Which sleep modes are supported by the kernel.

Files related to the Linux suspend process:

You can access the Linux kernel website to obtain the source code. The file path is as follows:

  • Linux_soruce/kernel/power/Main. c
  • Linux_source/kernel/ARCH/XXX/Mach-xxx/PM. c
  • Linux_source/driver/base/power/Main. c

Next let's take a detailed look at how Linux sleep/wakes up. Let's going to see how these happens.

The user's read/write operations on/sys/power/State call main. state_store () in C. You can write strings defined in const char * const pm_state [], such as "mem", "standby ".

Then state_store () will call enter_state (). It will first check some status parameters and then synchronize the file system. The following is the code:

/**
* enter_state - Do common work of entering low-power state.
* @state: pm_state structure for state we're entering.
*
* Make sure we're the only ones trying to enter a sleep state. Fail
* if someone has beat us to it, since we don't want anything weird to
* happen when we wake up.
* Then, do the setup for suspend, enter the state, and cleaup (after
* we've woken up).
*/
static int enter_state(suspend_state_t state)
{
int error;

if (!valid_state(state))
return -ENODEV;

if (!mutex_trylock(&pm_mutex))
return -EBUSY;

printk(KERN_INFO "PM: Syncing filesystems ... ");
sys_sync();
printk("done.
");

pr_debug("PM: Preparing system for %s sleep
", pm_states[state]);
error = suspend_prepare();
if (error)
goto Unlock;

if (suspend_test(TEST_FREEZER))
goto Finish;

pr_debug("PM: Entering %s sleep
", pm_states[state]);
error = suspend_devices_and_enter(state);

Finish:
pr_debug("PM: Finishing wakeup.
");
suspend_finish();
Unlock:
mutex_unlock(&pm_mutex);
return error;
}
Prepare and freeze Processes

After entering suspend_prepare (), it will allocate a virtual terminal to suspend to output information, broadcast a system to enter suspend's notify y, and disable the user-mode helper process, then call suspend_freeze_processes () to freeze all processes. The current state of all processes will be saved here. Some processes may refuse to enter the frozen state. When such a process exists, this function will stop freezing processes and restore all frozen processes.

/**
* suspend_prepare - Do prep work before entering low-power state.
*
* This is common code that is called for each state that we're entering.
* Run suspend notifiers, allocate a console and stop all processes.
*/
static int suspend_prepare(void)
{
int error;
unsigned int free_pages;

if (!suspend_ops || !suspend_ops->enter)
return -EPERM;

pm_prepare_console();

error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
if (error)
goto Finish;

error = usermodehelper_disable();
if (error)
goto Finish;

if (suspend_freeze_processes()) {
error = -EAGAIN;
goto Thaw;
}

free_pages = global_page_state(NR_FREE_PAGES);
if (free_pages < FREE_PAGE_NUMBER) {
pr_debug("PM: free some memory
");
shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
if (nr_free_pages() < FREE_PAGE_NUMBER) {
error = -ENOMEM;
printk(KERN_ERR "PM: No enough memory
");
}
}
if (!error)
return 0;

Thaw:
suspend_thaw_processes();
usermodehelper_enable();
Finish:
pm_notifier_call_chain(PM_POST_SUSPEND);
pm_restore_console();
return error;
}
Sleep peripherals

Now, all processes (including workqueue/kthread) have stopped, and kernel-state characters may hold some semaphores when they are stopped, so if you unlock this signal in the peripherals at this time, there may be deadlocks, so you should be very careful when making the lock/unlock lock in the suspend () function of the peripherals, we recommend that you do not wait for the lock in suspend () during design. in addition, some logs cannot be output during suspend, so once a problem occurs, debugging is very difficult.

Then the kernel will try to release some memory here.

At last, we will call suspend_devices_and_enter () to sleep all external settings. In this function, if the platform registers suspend_pos (usually defined and registered in the board-level definition ), here we will call suspend_ops-> begin (), and then driver/base/power/main. in C, device_suspend ()-> dpm_suspend () will be called, and they will call the suspend () callback of the driver in sequence to sleep all devices.

When all devices sleep, suspend_ops-> prepare () will be called. This function usually makes some preparations to let the machine sleep. in Linux, non-startup CPUs in multi-core CPUs will be turned off. You can see through comments that the other CPUs will not cause race condion, and then only one CPU will be running.

Suspend_ops is an on-board power management operation, which is usually registered in the file ARCH/XXX/Mach-xxx/PM. C.

Next, suspend_enter () will be called. This function will disable arch IRQ and call device_power_down (). It will call the suspend_late () function, this function is the final function called by the system to sleep. It usually performs the final check in this function. if the check is correct, sleep all the system devices and buses and call suspend_pos-> enter () to enable the CPU to power down. at this time, it is sleep. code execution stops here.

/**
* suspend_devices_and_enter - suspend devices and enter the desired system
* sleep state.
* @state: state to enter
*/
int suspend_devices_and_enter(suspend_state_t state)
{
int error, ftrace_save;

if (!suspend_ops)
return -ENOSYS;

if (suspend_ops->begin) {
error = suspend_ops->begin(state);
if (error)
goto Close;
}
suspend_console();
ftrace_save = __ftrace_enabled_save();
suspend_test_start();
error = device_suspend(PMSG_SUSPEND);
if (error) {
printk(KERN_ERR "PM: Some devices failed to suspend
");
goto Recover_platform;
}
suspend_test_finish("suspend devices");
if (suspend_test(TEST_DEVICES))
goto Recover_platform;

if (suspend_ops->prepare) {
error = suspend_ops->prepare();
if (error)
goto Resume_devices;
}

if (suspend_test(TEST_PLATFORM))
goto Finish;

error = disable_nonboot_cpus();
if (!error && !suspend_test(TEST_CPUS))
suspend_enter(state);

enable_nonboot_cpus();
Finish:
if (suspend_ops->finish)
suspend_ops->finish();
Resume_devices:
suspend_test_start();
device_resume(PMSG_RESUME);
suspend_test_finish("resume devices");
__ftrace_enabled_restore(ftrace_save);
resume_console();
Close:
if (suspend_ops->end)
suspend_ops->end();
return error;

Recover_platform:
if (suspend_ops->recover)
suspend_ops->recover();
goto Resume_devices;
}
Resume

If the system is interrupted or other events are awakened during sleep, the following code starts to run. The wake-up sequence is the opposite to that of sleep, therefore, the system device and bus will first wake up, enable the system to be disconnected, enable non-startup CPU to be stopped during sleep, and call suspend_ops-> finish (), and in suspend_devices_and_enter () the function will also wake up each device, enable the virtual terminal, and finally call suspend_ops-> end ().

In the return to the enter_state () function, when suspend_devices_and_enter () returns, the peripherals are awakened, but the processes and tasks are still frozen. suspend_finish () is called here () to unfreeze these processes and tasks, and send a running y to indicate that the system has exited from the suspend status and wakened the terminal.

By now, all sleep and wakeup have been completed, and the system continues to run.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.