From 408a3bbd76326793ea5d1cf4e0a9444a4c252d86 Mon Sep 17 00:00:00 2001 From: Anita Zhang Date: Sat, 23 Jan 2021 22:10:42 -0800 Subject: [PATCH] oom: make swap a soft requirement --- man/systemd-oomd.service.xml | 4 ++-- src/oom/oomd-manager.c | 8 ++++++-- src/oom/oomd.c | 6 ++---- src/oom/test-oomd-util.c | 11 +++++++++++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/man/systemd-oomd.service.xml b/man/systemd-oomd.service.xml index 9cb9c6076a..ebd2467ee2 100644 --- a/man/systemd-oomd.service.xml +++ b/man/systemd-oomd.service.xml @@ -56,8 +56,8 @@ You will need a kernel compiled with PSI support. This is available in Linux 4.20 and above. - The system must also have swap enabled for systemd-oomd to function correctly. With swap - enabled, the system spends enough time swapping pages to let systemd-oomd react. + It is highly recommended for the system to have swap enabled for systemd-oomd to function + optimally. With swap enabled, the system spends enough time swapping pages to let systemd-oomd react. Without swap, the system enters a livelocked state much more quickly and may prevent systemd-oomd from responding in a reasonable amount of time. See "In defence of swap: common misconceptions" diff --git a/src/oom/oomd-manager.c b/src/oom/oomd-manager.c index e8ed6a5273..814fda51f3 100644 --- a/src/oom/oomd-manager.c +++ b/src/oom/oomd-manager.c @@ -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) { diff --git a/src/oom/oomd.c b/src/oom/oomd.c index 1b0f8ff6c4..1fbcf41492 100644 --- a/src/oom/oomd.c +++ b/src/oom/oomd.c @@ -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"); diff --git a/src/oom/test-oomd-util.c b/src/oom/test-oomd-util.c index 8143408902..54fe2a03d1 100644 --- a/src/oom/test-oomd-util.c +++ b/src/oom/test-oomd-util.c @@ -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) {