]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
oom: make swap a soft requirement
authorAnita Zhang <the.anitazha@gmail.com>
Sun, 24 Jan 2021 06:10:42 +0000 (22:10 -0800)
committerAnita Zhang <the.anitazha@gmail.com>
Sun, 24 Jan 2021 09:59:03 +0000 (01:59 -0800)
man/systemd-oomd.service.xml
src/oom/oomd-manager.c
src/oom/oomd.c
src/oom/test-oomd-util.c

index 9cb9c6076a910d156d0e86f3b3fdfa878c9d1982..ebd2467ee2323cce3e62c932fa684388fc02fe48 100644 (file)
@@ -56,8 +56,8 @@
 
     <para>You will need a kernel compiled with PSI support. This is available in Linux 4.20 and above.</para>
 
-    <para>The system must also have swap enabled for <command>systemd-oomd</command> to function correctly. With swap
-    enabled, the system spends enough time swapping pages to let <command>systemd-oomd</command> react.
+    <para>It is highly recommended for the system to have swap enabled for <command>systemd-oomd</command> to function
+    optimally. With swap enabled, the system spends enough time swapping pages to let <command>systemd-oomd</command> react.
     Without swap, the system enters a livelocked state much more quickly and may prevent <command>systemd-oomd</command>
     from responding in a reasonable amount of time. See
     <ulink url="https://chrisdown.name/2018/01/02/in-defence-of-swap.html">"In defence of swap: common misconceptions"</ulink>
index e8ed6a52739b2d71dfd2c7aff9642b3a42e4816a..814fda51f31fddad3b730efc59da9e86ca1dcfa7 100644 (file)
@@ -6,6 +6,7 @@
 #include "cgroup-util.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "memory-util.h"
 #include "oomd-manager-bus.h"
 #include "oomd-manager.h"
 #include "path-util.h"
@@ -294,9 +295,12 @@ static int monitor_cgroup_contexts_handler(sd_event_source *s, uint64_t usec, vo
                 return log_error_errno(r, "Failed to update monitored memory pressure cgroup contexts");
 
         r = oomd_system_context_acquire("/proc/swaps", &m->system_context);
-        /* If there aren't units depending on swap actions, the only error we exit on is ENOMEM */
-        if (r == -ENOMEM || (r < 0 && !hashmap_isempty(m->monitored_swap_cgroup_contexts)))
+        /* If there aren't units depending on swap actions, the only error we exit on is ENOMEM.
+         * Allow ENOENT in the event that swap is disabled on the system. */
+        if (r == -ENOMEM || (r < 0 && r != -ENOENT && !hashmap_isempty(m->monitored_swap_cgroup_contexts)))
                 return log_error_errno(r, "Failed to acquire system context");
+        else if (r == -ENOENT)
+                zero(m->system_context);
 
         /* If we're still recovering from a kill, don't try to kill again yet */
         if (m->post_action_delay_start > 0) {
index 1b0f8ff6c402677a9a47d57ed73a74ed5b8dd991..1fbcf41492ddfc589658dba1d4f7502cd71f6b9e 100644 (file)
@@ -142,10 +142,8 @@ static int run(int argc, char *argv[]) {
                 return log_error_errno(r, "Failed to get SwapTotal from /proc/meminfo: %m");
 
         r = safe_atollu(swap, &s);
-        if (r < 0)
-                return log_error_errno(r, "Failed to parse SwapTotal from /proc/meminfo: %s: %m", swap);
-        if (s == 0)
-                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Requires swap to operate");
+        if (r < 0 || s == 0)
+                log_warning("Swap is currently not detected; memory pressure usage will be degraded");
 
         if (!is_pressure_supported())
                 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Pressure Stall Information (PSI) is not supported");
index 8143408902b97d8d42d041708b889e5267832a73..54fe2a03d14138a0345dc7f3c25f8e0b1f64a240 100644 (file)
@@ -159,6 +159,11 @@ static void test_oomd_system_context_acquire(void) {
         assert_se(ctx.swap_total == 0);
         assert_se(ctx.swap_used == 0);
 
+        assert_se(write_string_file(path, "Filename                                Type            Size    Used    Priority", WRITE_STRING_FILE_CREATE) == 0);
+        assert_se(oomd_system_context_acquire(path, &ctx) == 0);
+        assert_se(ctx.swap_total == 0);
+        assert_se(ctx.swap_used == 0);
+
         assert_se(write_string_file(path, "Filename                                Type            Size    Used    Priority\n"
                                           "/swapvol/swapfile                       file            18971644        0       -3\n"
                                           "/dev/vda2                               partition       1999868 993780  -2", WRITE_STRING_FILE_CREATE) == 0);
@@ -268,6 +273,12 @@ static void test_oomd_swap_free_below(void) {
                 .swap_used = 3310136 * 1024U,
         };
         assert_se(oomd_swap_free_below(&ctx, 20) == false);
+
+        ctx = (OomdSystemContext) {
+                .swap_total = 0,
+                .swap_used = 0,
+        };
+        assert_se(oomd_swap_free_below(&ctx, 20) == false);
 }
 
 static void test_oomd_sort_cgroups(void) {