Linux – Are Linux kernel modules a sort of Linux system paged pool

kernel-moduleslinux-kernelswapvirtual-memory

I always read that Linux kernel isn't pageable.
If I'm not mistaken Windows, instead, divedes system virtual memory in
a paged part (paged pool) and non-paged part (non-paged pool).
The non-paged part is mapped directly to physical memory and stay there all the time because takes care of the most important tasks kernel must accomplish, while less important portions may be not.
Linux kernel,instead, is divided into laodable modules, but no information I managed to gather on how these modules are implemented.
I don't understand if they're paged and thus you can temporarily transfer them to the disk. What I usually read is that we can "free" memory by unloading them, what is meant with this is still obscure to me.
When I writed "paged" or "pageable" along this post, I implicitly meant that you can swap out on disk these pages. I addressed this because usually Linux kernel is considered paged but it can't be swapped out

Best Answer

No part of the Linux kernel can be paged out, even parts that come from modules.

A kernel module can be loaded and (if the module supports it) can be unloaded. This always happens from an explicit request from a userland process with the init_module and delete_module system calls (normally, via the insmod or modprobe utilities for loading, and via rmmod for unloading).

Once a module is loaded, it's part of the kernel, like any other part of the kernel. In particular, there's no way to isolate the memory used by a specific module. The kernel keeps tracks of which part of the memory contains a specific module's code, but not where a module may have stored data. A module can modify any of the kernel's data structures, after all.

A module can potentially add some code to any kernel subsystem. Most modules are hardware drivers, but some aren't (e.g. they can provide security functionality, filesystems, networking functionality, etc.). If data or code used by a module could be swapped out, then the rest of the kernel would have to load it when required, which would complicate the design of the system a lot. The kernel would also need to ensure that no part of the memory that's swapped out is ever necessary to swap it back in, which is difficult. What if the swap is in a swap file on a network filesystem, and the module provides firewall functionality that is involved in communicating with the server that stores the file?

It's possible to completely unload a module because it's the module's job to provide code that ensures that the module is not needed for anything. The kernel runs the module's exit function, and only unloads the module if that function reports that the module can be safely unloaded. The exit function must free any remaining data memory that is “owned” by the module (i.e. data that the module needs, but no other part of the kernel needs), and must verify that no code in the module is registered to be called when something happens. There's no way to save a module's data to swap: a module can only be removed from RAM if it has no data left.

Related Question