Should Kernel Memory be swapped out?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
RyanPoint
Posts: 2
Joined: Sun Aug 11, 2019 5:36 pm
Libera.chat IRC: RyanPoint

Should Kernel Memory be swapped out?

Post by RyanPoint »

Assume: 32 bit machine. Linear address is divided into 3G user, 1G kernel

As far as from what I read online, Linux is designed in a way such the kernel memory cannot be swapped out. If I understand correctly, not only the code and essential data (e.g. page struct) cannot be swapped out, but the buffer allocated in the kernel space cannot be swapped out.

I understand it is necessary to not swap out kernel code and essential data like page struct, but why can't we swap out allocated buffer temporarily and once kernel accessed the area, let the page fault handler swap back the buffer?

Is any issues with swapping out kernel memory like swapping out pages from user process that I overlook?
Octocontrabass
Member
Member
Posts: 5581
Joined: Mon Mar 25, 2013 7:01 pm

Re: Should Kernel Memory be swapped out?

Post by Octocontrabass »

The only issue is that you have to make sure you don't swap out things that you won't be able to swap back in. (For example, the drivers for the storage holding your swap file.)

In the Linux case, they probably decided the potential benefits weren't worth the amount of work it would take. Computers have a lot of RAM nowadays.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Should Kernel Memory be swapped out?

Post by Korona »

Other than code complexity, there is no reason to disallow swapping of kernel pages.

However, the code complexity is easy to underestimate. If you allow swapping out kernel pages, you need to be able to handle page faults whenever you access a page that might be swapped out. This might lead to rather complex issues. The most obvious problem is locking: the page fault handler will need to take a lock on the kernel address space. The FS will be invoked and it will also take some locks. Finally, the disk driver will be called and take locks. If your page fault happens with any of those locks already taken, you get a deadlock. For example, imagine that you have a page that only contains simple statistics of a process, such as the number of I/O operations that the process triggered. Swapping out this page will already be a bug because you will need to take a lock on the statistics page to access it but, on the other hand, the disk driver will try to take this lock to update the page.
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
User avatar
zaval
Member
Member
Posts: 658
Joined: Fri Feb 17, 2017 4:01 pm
Location: Ukraine, Bachmut
Contact:

Re: Should Kernel Memory be swapped out?

Post by zaval »

Windows is mostly pageable. making the whole kernel nonpageable is lame. your question is valid - it's a good design to make your OS as pageable as possible. this example is a good one, demonstrating, that looking at (only) linux may make poor service for your knowledge, since you may believe some mediocre approach is the only one possible. it's good that you questioned it, the other time it might slip out of eye, or how it's freaking said in english. :D
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).
Post Reply