Do Kernel pages get swapped out?

2019-01-14 16:57发布

问题:

Pertaining to the Linux kernel, do "Kernel" pages ever get swapped out ? Also, do User space pages ever get to reside in ZONE_NORMAL ?

回答1:

No, kernel memory is unswappable.



回答2:

  1. Kernel pages are not swappable. But it can be freed.

  2. UserSpace Pages can reside in ZONE_NORMAL. Linux System Can be configured either to use HIGHMEM or not. If ZONE_HIGHMEM is configured , then the userspace processes will get its memory from the HIGHMEM else userspace processes will get memory from ZONE_NORMAL.



回答3:

Yes, under normal circumstances kernel pages (ie, memory residing in the kernel for kernel usage) are not swappable, in fact, once detected (see the pagefault handler source code), the kernel will explicitly crash itself.

See this:

http://lxr.free-electrons.com/source/arch/x86/mm/fault.c

and the function:

1205 /*
1206  * This routine handles page faults.  It determines the address,
1207  * and the problem, and then passes it off to one of the appropriate
1208  * routines.
1209  *
1210  * This function must have noinline because both callers
1211  * {,trace_}do_page_fault() have notrace on. Having this an actual function
1212  * guarantees there's a function trace entry.
1213  */
1214 static noinline void
1215 __do_page_fault(struct pt_regs *regs, unsigned long error_code,
1216                 unsigned long address)
1217 {

And the detection here:

1246          *
1247          * This verifies that the fault happens in kernel space
1248          * (error_code & 4) == 0, and that the fault was not a
1249          * protection error (error_code & 9) == 0.
1250          */
1251         if (unlikely(fault_in_kernel_space(address))) {
1252                 if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1253                         if (vmalloc_fault(address) >= 0)
1254                                 return;
1255 
1256                         if (kmemcheck_fault(regs, address, error_code))
1257                                 return;
1258                 }

But the same pagefault handler - which can detect pagefault arising from non-existent usermode memory (all hardware pagefault detection is always done in kernel) will explicitly retrieve the data from swap space if it exists, or start a memory allocation routine to give the process more memory.

Ok, that said, kernel does swap out kernel structures/memory/tasklists etc during software suspend and hibernation operation:

https://www.kernel.org/doc/Documentation/power/swsusp.txt

And during the resume phase it will restore back the kernel memory from swap file.