4

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

migrated from cs.stackexchange.com Jul 22 at 18:17

This question came from our site for students, researchers and practitioners of computer science.

11

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.

  • So essentially process segments can be swapped in and out, while kernel modules can be only loaded and unloaded. The difference seems to be only that when we swap out pages we save what has changed, while a kernel module is just freed when it's sure to be not used again and so no saving is done. Is it that? – Gabriele Scarlatti Jul 22 at 18:39
  • 1
    @Gabriele unloading a module releases its memory in the same way that exiting a process does: the memory is no longer needed. Swapping is used with memory pages which are still needed, don’t have their own backing store, and which the kernel decides are better off stored outside of physical memory. – Stephen Kitt Jul 22 at 18:46
  • @StephenKitt But can kernel module be unloaded during a process execution? or only when the process that uses the module terminate? – Gabriele Scarlatti Jul 22 at 18:54
  • @Gabriele I was only comparing the two, not implying that unloading a kernel module was tied to process execution. When a process exits, its memory is no longer needed and is freed. When a kernel module is unloaded, its memory is no longer needed and is freed. – Stephen Kitt Jul 22 at 19:00
  • yes I know, I just needed to see it stated :) Thanks – Gabriele Scarlatti Jul 22 at 19:06
5

Memory pages swapping (you call it "paging") and Linux kernel modules loading/unloading are two completely different functionalities.

A Linux kernel module can be loaded into the kernel - after that it becomes a part of the kernel. The goal here is to adjust the kernel to a given hardware. For example, there are hundreds of types of network cards, and there are many kernel modules, which support these cards - but on any particular machine at any given moment you'll have only one type of network card. The Linux chooses the right kernel module for this card and loads it, leaving all the other modules in the external memory.

For more information about kernel modules please read here.

  • So kernel modules aren't swapped in and out? Altough Iit seemed to me that it happens in windows, are they different (windows and linux) on that? – Gabriele Scarlatti Jul 22 at 18:26
  • Moreover the second answer to this post unix.stackexchange.com/questions/65481/… seems to imply that Linux kernel modules can be loaded and unloaded on demand, isn't this a form page swap? – Gabriele Scarlatti Jul 22 at 18:29
  • After the kernel module is loaded it becomes a part of the kernel, so it can't be swapped out. I have no idea about Windows internals – HEKTO Jul 22 at 18:29
  • Modules are typically loaded during the system boot, but there are no such restriction. No, it's not a form of swapping - the system swaps memory page by page, not module by module – HEKTO Jul 22 at 18:32
  • So, why is "paging" wrong? At least by some sources, "swapping" refers to swapping out whole programs at a time, while "paging" refers to doing essentially the same but a page at a time. – ilkkachu Jul 22 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.