LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 04-21-2017, 06:24 AM   #1
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, Slackware
Posts: 2,598
Blog Entries: 6

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
What happens to the kernel when you resume from hibernation?


I know that resuming from hibernation restores userland exactly as it was. But what about the kernel? The first step when I wake up my laptop is that grub appears and loads a kernel. Presumably this is a new kernel, clean of all modules, and not the one I was using before. After loading some driver modules, the kernel determines that there is a resume image in the swap partition and reloads this instead of calling init.

But the image must already include a running kernel. I remember when I first started using Linux, I was told that the kernel is always in memory. It never gets swapped out because then the swap daemon would go out with it and it couldn't get swapped in again. So does this restored kernel replace the one that booted?
 
Old 04-21-2017, 02:11 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173
By definition, the first part of the bootstrap process proceeds as it always does: the kernel is loaded into memory and completes its basic initialization.

Then(!) ... the kernel "takes a left turn at Albuquerque." It detects that it is supposed to be doing a warm-start. So, it uses saved data to re-create the process-table entries and so forth, and starts swapping those processes back in.

The "hibernate" (warm-start ...) dataset does not include nor describe the kernel itself: it doesn't need to. Rather, it contains information that is used by the kernel to re-create the user-land environment just as it was when the system was put to sleep.

Most of the magic is done by "swapping everything out," thus leveraging the existing "swapper" instead of inventing something altogether new to handle the majority of the job. As the various processes come back to life, they will be "swapped in." The only information that is "truly unique to warm-starting" is the details of process- and file-table entries (etc...), which must be saved and then re-created to accomplish the entire magic trick.

Last edited by sundialsvcs; 04-21-2017 at 02:14 PM.
 
1 members found this post helpful.
Old 04-21-2017, 02:13 PM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,843

Rep: Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601Reputation: 3601
not to speak about other things, like network (which will be initialized again, cannot be restored)
 
Old 04-21-2017, 02:49 PM   #4
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, Slackware
Posts: 2,598

Original Poster
Blog Entries: 6

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by sundialsvcs View Post
Most of the magic is done by "swapping everything out," thus leveraging the existing "swapper" instead of inventing something altogether new to handle the majority of the job. As the various processes come back to life, they will be "swapped in." The only information that is "truly unique to warm-starting" is the details of process- and file-table entries (etc...), which must be saved and then re-created to accomplish the entire magic trick.
So, if I understand you, it's not just a simple copy of core memory into one big file. Instead pages are swapped out individually just as they would be if the core was overloaded. I can see that in that case, the kernel wouldn't be included as it doesn't normally get swapped.

The reason I am interested is that I am having trouble hibernating my Samsung laptop, or rather waking it up again. Sometimes it works, sometimes I can't get the screen to function. As far as I can see from the logs, everything else comes up normally. I'm experimenting with blacklisting certain graphics modules to see if I can pin down the one which is causing the problem. I suspect it's viafb. Unfortunately I am going to need that module later on in order for the webcam to work, so I can't blacklist it permanently. I may need some hooks to remove it before hibernation and prevent it from being reloaded too early in the wakeup process. Knowing exactly what kernel image is being used (the one I booted with or a freshly-loaded one) is important to me.
 
Old 04-21-2017, 06:06 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 17,362

Rep: Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699
Read this. Also the linked freezing-of-tasks.txt.
 
1 members found this post helpful.
Old 04-22-2017, 03:26 AM   #6
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, Slackware
Posts: 2,598

Original Poster
Blog Entries: 6

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by syg00 View Post
Read this. Also the linked freezing-of-tasks.txt.
Your link is a bit out of date, but it gave me an idea. I went into my actual kernel source tree and found more current versions of a whole slew of files in Documentation/Power. I shall read them religiously.
 
Old 04-22-2017, 11:55 AM   #7
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, Slackware
Posts: 2,598

Original Poster
Blog Entries: 6

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Well, look what I found in the kernel docs!
Quote:
Originally Posted by /Linux-4.9.9/Documentation/power/devices.txt
Although in principle, the image might be loaded into memory and the
pre-hibernation memory contents restored by the boot loader, in practice this
can't be done because boot loaders aren't smart enough and there is no
established protocol for passing the necessary information. So instead, the
boot loader loads a fresh instance of the kernel, called the boot kernel, into
memory and passes control to it in the usual way. Then the boot kernel reads
the system image, restores the pre-hibernation memory contents, and passes
control to the image kernel. Thus two different kernels are involved in
resuming from hibernation. In fact, the boot kernel may be completely different
from the image kernel: a different configuration and even a different version.
This has important consequences for device drivers and their subsystems.
So Sundialsvc was wrong. There are two different kernel images: the "boot kernel" and the "image kernel", and the latter takes over from the former at the end of the resume phase. This may explain something that I noticed during my experiments. I had blacklisted viafb and then hibernated the laptop. When I resumed, the screen was screwed up as usual (symptom: colour-cycling). So I thought maybe viafb was not the problem after all. I rebooted with ctrl-alt-del as the keyboard was still working. Some time later, I hibernated again. And this time, I came out of it successfully with a functional screen. I shall need to confirm this but it does make sense in view of the above quote.

The first time around, the boot kernel would not have loaded viafb because it was blacklisted. But the image kernel would still have had it loaded. The second time, neither kernel would have contained it.
 
Old 04-22-2017, 11:55 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 17,362

Rep: Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699Reputation: 2699
Quote:
Originally Posted by hazel View Post
Your link is a bit out of date
Yeah sorry - I keep forgetting Jonathan Corbet said he was moving the doco over to the Admin Guide (using Sphinx).
 
Old 04-24-2017, 09:36 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173
Quote:
Originally Posted by hazel View Post
Well, look what I found in the kernel docs!

So Sundialsvc was wrong. There are two different kernel images: the "boot kernel" and the "image kernel", and the latter takes over from the former at the end of the resume phase.
I was trying to keep things simple for the purposes of explanation.

Although I question whether, in actual practice, the "starting" kernel would actually ever be different from the "stopping" one. (If there's any opportunity for things to change in this way, you're doing something very funky! )

Last edited by sundialsvcs; 04-24-2017 at 09:38 AM.
 
Old 04-24-2017, 10:47 AM   #10
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, Slackware
Posts: 2,598

Original Poster
Blog Entries: 6

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
I agree that using a different kernel version would be rather odd. I think that example was only given to prove a point, not because anyone would actually do it.

In my case though, there would be a difference in the list of drivers present. I shall need viafb eventually because via-camera depends on it, and I need via-camera to make the webcam work. I have established that its presence in the kernel doesn't interfere with normal booting unless fbcon is also present. (viafb+fbcon is a poisonous combination on my hardware; it prevents a console from functioning. The screen cycles through various colours and the keyboard goes dead, so you can't even reboot. You have to do a hard shutdown).

So blacklisting viafb doesn't seem to be appropriate. But viafb, even on its own, interferes with the screen when you resume from hibernation. In this case, you have a console so the keyboard works, but you can't see what you're doing so you have to reboot.

The solution obviously is to have a suspend hook that removes viafb before hibernation and a resume hook that puts it back again after resuming. That's what I intend to do when I've done a bit more testing.

Last edited by hazel; 04-24-2017 at 10:51 AM.
 
Old 04-24-2017, 11:33 AM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173
Quote:
Originally Posted by hazel View Post
The solution obviously is to have a suspend hook that removes viafb before hibernation and a resume hook that puts it back again after resuming. That's what I intend to do when I've done a bit more testing.
Indeed, drivers are a thorny spot are they "hibernate aware?" Are they capable of, say, "re-finding the device" after a hibernate/wakeup cycle? Will the process of saving and then restoring the system state work for them or against them? Only the driver author knows for sure.
 
Old 04-26-2017, 02:34 PM   #12
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, Slackware
Posts: 2,598

Original Poster
Blog Entries: 6

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Good news! My hook script works. On hibernate, it unloads viafb, then reloads it on resume, and my screen comes up perfectly. This is using console only; I haven't got X fully built yet but when I have, I'll test it again. Then on to fixing sleep, which has a different problem.

Oh I do love Linux; it's so easy to find out what is causing a problem and fix it. No way could I have done anything like this in Windows.

Last edited by hazel; 04-26-2017 at 02:36 PM.
 
Old 04-26-2017, 10:18 PM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173
Quote:
Originally Posted by hazel View Post
Oh I do love Linux; it's so easy to find out what is causing a problem and fix it. No way could I have done anything like this in Windows.
Very well said. Very well, indeed.

(Although, I must say, it probably sux to be a Microsoft hardware-support engineer!)
 
Old 04-27-2017, 10:40 AM   #14
hazel
Senior Member
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: Debian, Crux, LFS, AntiX, Slackware
Posts: 2,598

Original Poster
Blog Entries: 6

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Just a little update. I've just finished setting up X with fluxbox. I hibernated from an xterm and was able to get back to the desktop without problems. Switching between the desktop and virtual consoles also goes smoothly. But I can't yet resume from sleep.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to resume after hibernation rupeshforu Linux - Hardware 2 07-06-2013 05:42 PM
Measure the time it takes to resume after hibernation linkaran Linux - General 3 03-10-2013 09:48 AM
Network Can't be reloaded after resume from hibernation snowweb Linux - Networking 2 09-16-2012 10:53 PM
Ubuntu 11.10 - problems with hibernation, resume from it firekage Ubuntu 2 05-27-2012 05:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration