3 ms·
> Having a guard page is technically difficult in Linux since there's no virtual memory in the kernel, so you can't have an unmapped page This isn't true. The
by geofft 10y ago
> Having a guard page is technically difficult in Linux since there's no virtual memory in the kernel, so you can't have an unmapped page
This isn't true.
The last line in the article links to patches that add guard pages to Linux: https://lkml.org/lkml/2016/6/15/1064 https://lkml.org/lkml/2016/6/15/1064
You absolutely have virtual memory in the kernel—that's how the kernel gets to live at a fixed high address in every process, and that's how it gets to read things from userspace when it needs to. When a process makes a system call, the virtual memory map stays exactly the same; the CPU just switches to supervisor mode and starts using a different segment that enables accessing kernel memory.
What you don't easily have, at least on Linux, is the ability to page things out (which is sometimes colloquially called "virtual memory" thanks to some customer-facing marketing in the '90s), but that's not an inherent requirement of monolithic kernels. NT, for instance, allows paging out certain parts of kernel memory (though obviously not the parts that are needed to page things back in), at the cost of a bit of mental overhead from driver authors. And this isn't necessarily solved in microkernels or exokernels: your swap device is going to be supported by at least a few userspace drivers. If you're using a machine with full-disk encryption, your disk encryption server isn't allowed to swap.
What you also don't have on Linux is different mappings for different kernel threads (all of kernel memory is mapped in every thread, accessible by kernel code only) and memory protection between kernel threads, but you don't really need that, and that wouldn't have helped solve this problem.
> kernel stacks have to be allocated contiguously
There's no particular reason that segmented stacks (like old Go or old Rust had) couldn't be used in the kernel. They're just complicated, and both Go and Rust found that they were bad for performance.
> It's a lot easier to write than kernel code.
I'm not convinced by this. Apart from the experimental evidence that monolithic kernels have been far more featureful, there's the inherent problem that you don't really know what assumptions you get to make. You're only in mostly-userspace code; there's the example I mentioned above of the full-disk-encryption driver, which doesn't get to swap because it would be implementing its own swap device. If you want your OS to support swap files, filesystem drivers can't swap. If you have syslog over local sockets, your socket implementation can't syslog. And so forth.
What is easier (as I advocated in another comment) is to keep the monolithic kernel model, but pretend that privsep within a single kernel doesn't happen, and run it all within VMs.
- qb45 10y agoYou are mostly right, but > There's no particular reason that segmented stacks (like old Go or old Rust had) couldn't be used in the kernel. A very good reason is that Linux happens to be written in C, which just doesn't support this. So the patch you linked uses VM mappings instead. > What is easier (as I advocated in another comment) is to keep the monolithic kernel model, but pretend that privsep within a single kernel doesn't happen, and run it all within VMs. That's the exokernel model, with hypervisor being the kernel and Linux being a private "libOS" of your httpd or whatever.
- geofft 10y ago> A very good reason is that Linux happens to be written in C, which just doesn't support this. This is a tangent, but I thought that C supported this just fine as long as there was compiler and runtime support? The compiler needs to insert a check at each stack allocation (beginning of each function, or each call to alloca or equivalent), and if it switches your stack pointer, it needs a trampoline as the last frame on the new stack, to un-switch the stack pointer. And you need to teach everything that traces stacks how to keep tracing across segments. But it doesn't seem impossible. GCC and LLVM seem to have support that doesn't care what language you're using: https://gcc.gnu.org/wiki/SplitStacks https://gcc.gnu.org/wiki/SplitStacks http://llvm.org/releases/3.0/docs/SegmentedStacks.html http://llvm.org/releases/3.0/docs/SegmentedStacks.html
- qb45 10y agoFair enough. I wasn't aware that GCC actually supports this in C.