OSDev.org
https://forum.osdev.org/

Should Kernel Memory be swapped out?
https://forum.osdev.org/viewtopic.php?f=1&t=34098
Page 1 of 1

Author:  RyanPoint [ Fri Aug 30, 2019 11:00 am ]
Post subject:  Should Kernel Memory be swapped out?

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?

Author:  Octocontrabass [ Sat Aug 31, 2019 1:46 am ]
Post subject:  Re: Should Kernel Memory be swapped out?

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.

Author:  Korona [ Sat Aug 31, 2019 4:03 am ]
Post subject:  Re: Should Kernel Memory be swapped out?

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.

Author:  zaval [ Sat Aug 31, 2019 3:35 pm ]
Post subject:  Re: Should Kernel Memory be swapped out?

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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/