]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: lock in all memory pages when mlockall() is utilized, and on demand
authorMike Yuan <me@yhndnzj.com>
Sat, 10 Jan 2026 19:46:25 +0000 (20:46 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 15 Jan 2026 12:56:34 +0000 (13:56 +0100)
When employing MCL_FUTURE we don't actually want it to impose
immediate population of malloc()-ed pages. Hence let's set
MCL_ONFAULT everywhere.

Additionally, specify MCL_CURRENT to ensure future memory allocations
on already mapped pages are covered too. (Addresses
https://github.com/systemd/systemd/pull/40319#discussion_r2693726196)

Note that in shutdown the mlockall() is done to avoid keeping swap space
busy, hence a dedicated call w/ MCL_CURRENT and w/o MCL_ONFAULT is made.

src/cryptenroll/cryptenroll.c
src/cryptsetup/cryptsetup.c
src/shutdown/shutdown.c

index b2c291a2bd425f67c88103ec4988c3ef2e2ab011..ed66b6258f5210975b991b2a4c9482cb3ffb7427 100644 (file)
@@ -850,7 +850,7 @@ static int run(int argc, char *argv[]) {
                 return r;
 
         /* A delicious drop of snake oil */
-        (void) mlockall(MCL_FUTURE);
+        (void) mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT);
 
         cryptsetup_enable_logging(NULL);
 
index e61f4b4606ac5853f5c04e09372537c4efeee381..d18e9fe7d2f2534f52d781413c27b3557621a027 100644 (file)
@@ -2582,7 +2582,7 @@ static int verb_attach(int argc, char *argv[], void *userdata) {
                   volume, source, strempty(arg_type), strempty(arg_cipher));
 
         /* A delicious drop of snake oil */
-        (void) mlockall(MCL_FUTURE);
+        (void) mlockall(MCL_CURRENT|MCL_FUTURE|MCL_ONFAULT);
 
         if (key_file && arg_keyfile_erase)
                 destroy_key_file = key_file; /* let's get this baby erased when we leave */
index 75872f2eda87a5969cbf1ed39af464592d98433b..e282aa9116ff69bf5104d298ca1ffeb459168962 100644 (file)
@@ -402,7 +402,8 @@ int main(int argc, char *argv[]) {
         init_watchdog();
 
         /* Lock us into memory */
-        (void) mlockall(MCL_CURRENT|MCL_FUTURE);
+        (void) mlockall(MCL_FUTURE|MCL_ONFAULT);
+        (void) mlockall(MCL_CURRENT);
 
         /* We need to make mounts private so that we can MS_MOVE in unmount_all(). Kernel does not allow
          * MS_MOVE when parent mountpoints have shared propagation. */