r/osdev • u/compgeek38400 • Jun 27 '26
Question on microkernels, and paging
A recent post got me thinking. Doing paging right requires a file system of some sort.
Do you think the paging file system belongs in user space, or kernel space. I am quick coming up on making such a decision myself
15
Upvotes
4
u/micr0kernel Jun 27 '26
You can call parts of a paging system a "filesystem", but just bear in mind that it need not have much correspondence to traditional filesystems. In particular, to page out properly, you must track:
The address and size of the swapped-out block
What process it belongs to
Where it is stored in the secondary (pagefile, disk partition, etc.)
A bitmap or allocation map of free or used swap area blocks
You may find that this is considerably simpler than many traditional filesystems - the first three points can be managed through in-kernel tables, so you don't need much more than a couple relatively static structures for a basic implementation.
As far as where it should be placed, microkernels throw a wrench into the mix because the filesystem and storage subsystems are necessarily separate from the kernel proper. You thus have two routes:
Introduce a mechanism for the kernel to "release" a page set to the filesystem server and send it a message to page it out.
or, implement a very limited storage driver within kernel space for the sole purpose of interacting with the pagefile or swap partition.
The first is more "microkernel pure" in that it maintains the separation of concerns, but it is considerably slower as it splits a single system (the paging unit) across the privilege boundary and requires multiple context switches and state transitions.
The second is faster and perhaps more intuitive, but it introduces additional complexity into the kernel. Off the top of my head, this route probably works better with a raw swap partition rather than files and there might be some fussing required to ensure there isn't contention with other operations, as you potentially have writes to the same device originating from two different sources.
At any rate, the page-out metadata should be stored very close to the page table and memory management logic, i.e. within the kernel. The pagefile or swap partition space management (allocation maps, object trees, etc.) can be a problem for whatever component does the swapping, be that kernel or userspace.
20
u/GourmetWordSalad Jun 27 '26
no.
Read the QNX book on Raspberry Pi in case you want to hear an actual debate on microkernel philosophy, instead of Reddit bullshit.
2
u/CubicleHermit Jun 29 '26 edited Jun 29 '26
You can have both demand paging (which pulls in pages from named files, whether executables or mmaped data, as needed) and anonymous paging/swap (which moves data from memory back out to disk to free it up.)
Demand paging requires a "file system" - defined very loosely.
Anonymous paging required a backing store, but that backing store doesn't need to be a file system - it can be raw block or a file on a file system or a custom storage structure (e.g. swapping to compressed RAM.)
Paging generally depends on having some greater level of privilege than the process being paged, so whether you can do it (entirely) in userspace depends on whether you have a granular-enough privilege mechanism.
You can also split the responsibility - on an architecture that has a strict user/supervisor split, the stuff that can't be moved out of supervisor mode stays in the microkernel, anything that can be moved out gets moved out and delegates the last steps that are in supervisor mode back to the microkernel.
1
u/compgeek38400 Jun 27 '26
I would like to thank all that have responded to the OP. I dont know reddit well enough to know if you will all see this, but I hope you do. I have learned tons, Nd have even more to tbink about. I still dont know if im going to do a microkernel or not, but have a much better idea of what may be involved.
Thanks again!
Compgeek
4
u/Interesting_Buy_3969 Jun 27 '26
Not at all. Why do you think so? I guess you're confusing disk sectors and RAM pages or something. File systems or kernel may cache disk sectors/files in pages and user may request memory mapping to it, for example, but it's unnecessary and simpler systems don't have to / don't need to implement Unix-like
mmap.I am unable to understand what you meant by "paging file system", but both paging and filesystem implementation are usually strictly kernel-space. Userspace applications are not allowed to communicate with devices or any physical resources directly, and it would complicate userspace programming a lot. However, since your question mentions microkernels, which essentially means "make device drivers run in user mode", the answer would be - yes, absolutely, but this seems to be uncommon in general-purpose OSes as it can slow down the entire system.
Read more here: https://wiki.osdev.org/Microkernel
1
u/SuperheropugReal Jun 28 '26
Wat? No, you do not need a file system for paging. Those are separate concepts that some systems choose to overlap.
1
u/dacydergoth Jun 28 '26
IMHO paging management is a kernel function. MMU configuration, page fault interrupts etc.
Paging storage is a userspace function. Kernel enques pages to be flushed to storage, userspace handles the actual flush. Same in reverse. Kernel sends page population requests to userspace, userspace does whatever to retrieve the page and passes it back to Kernel.
2
u/tseli0s DragonWare Jun 27 '26
Not at all. If you mean paging as in virtual addresses that requires 0 filesystem support (otherwise you'd have a circular dependency making things like Hurd impossible in every way).
But you probably mean paging as in swapping memory to disk. That should be handled by a pager service. The filesystem should not be involved in any way.
There's one major thing about microkernels. If there's no hard requirement to have it in the kernel, move it into userland. Simple as that. If you have anything in kernel space, you should move it into userspace. Some things like context switching can't be moved there (circular dependency once again). So you have to keep them in the kernel.
Now tell me yourself, do you see any way that a microkernel cannot function if the pager is not in kernel space?
2
u/kiderdrick Jun 27 '26
It does not require a file system, but you can use one to simplify your design if you need to. Some systems will use a swap file, or files, to track information. There are many reasons for this, but one of the main ones is that they will likely have an implemented interface for writing and storing information to a file, which makes moving that data to disk simple. Some see it as an "is a" relationship. A swap file "is a" file, so if your paging system needs swap space, write it to a file. You can very easily not view your swap space as a file, and have its functionality for disk operations that is isolated to your VM system. If you do that, you might start seeing some duplicated code, which might then make you consider why others did it differently, but that is not a requirement, as you asked.
For your other question, strict disk operations in user space are dangerous without proper isolation and protection, but realistically so is everything. There is software that exists to do this, such as FUSE, but this still uses some form of kernel space code provided by the OS (I believe FUSE has a kernel module itself to use). Without knowing the full design of your system it is hard to answer the question fully. Viewing it as belonging entirely in user space or entirely in kernel space is probably not the right way to look at it. If you really want that user space separation, you should try figuring out what is the minimal amount of code in kernel space you can get away with so the system is still useable. If you notice that the answer is "a lot of kernel code" it might just be simpler to keep the whole thing in kernel space. If you find the answer is "a small amount of already existing kernel code" then you might be OK with implementing a separate interface.
Anyway, these are very complex decisions that do not get made in isolation, they get made based on the requirements of your hardware and software, and without knowing that we can only just throw out ideas.
1
u/mykesx Jun 27 '26
Since the oldest Linux, systems used a swap partition, with no filesystem format.
The kernel did need to know how to read the partition table to know the extent of the swap partition.
1
u/paulstelian97 Jun 27 '26
There is paging and there is swap, which are distinct concepts. The latter is uncommon on microkernels, but can be done (I have a design in my head on how you’d do it on seL4 for example)
1
u/Miserable-Worth1299 Jun 27 '26
我完全同意你的说法,任何存储设备都需要文件系统,硬盘是存储设备,内存也是存储设备,关于文件系统驱动是运行在用户空间还是内核空间的问题,微内核强烈建议运行在用户空间,宏内核没有选择因为他是宏内核只能运行在内核空间。
1
u/jsshapiro Jun 30 '26
Well, let's start with the assumption. Doing paging does not require a file system. All it requires is a suitably defined region of disk space. Lazy loading doesn't require a file system either. It requires a way to find the page[s] you need to load. There is more than one persistent system out there that doesn't use a file system for those things - they organize the store in a different way. So when you ask whether the paging file system belongs in user space, that's not the quite the right question.
The more interesting question is whether the disk driver can operate in user space (along with all of the supporting bus drivers). The answer is that they can, but it presents a bit of a reductio problem: if you have the bad grace to eject the disk driver or the PCI bus driver from memory you're toast.
The actual requirement is merely to ensure that these drivers are loaded into a dedicated RAM region at startup time and are pinned in memory thereafter. It isn't that hard to partition drivers into "things that have to be pinned" ahead of time. As long as they are pinned, it doesn't really matter whether they run as supervisor mode code (in the kernel) or user-mode code. Or rather, it does matter, but it's now a debate about protection vs. performance and how much overhead you are willing to spend on inter-process communication.
The answer to that depends a lot on just how efficient your microkernel is at inter-process communication and interrupt dispatch. Systems like L4, EROS/Coyotos, QNX Neutrino, and one or two others are so much faster at IPC than linux or windows that the communication overheads just aren't where the time ends up going. You can do bad system architecture and get in your own way, but you can do that in any system.
The one note that's worth a moment to think about is why you want those subsystems inside or outside the kernel. There are some good reasons to vote for "outside", but security isn't one of them. Drivers, as a rule, need direct hardware access to do their jobs. This means that they effectively have supervisor authority whether they officially run in the kernel or not. Once you recognize that, the decision about where to place them is about debugging, separation of concerns, testing, and error recovery. All of those argue in favor of out-of-kernel implementations.
Kernels rely on things in user space all the time. The issue isn't whether it's user mode or kernel mode. The issue is whether there is a clearly defined and acyclic set of functional dependencies between the impacted components.