summaryrefslogtreecommitdiff
path: root/src/oom
diff options
context:
space:
mode:
authorNick Rosbrook <nick.rosbrook@canonical.com>2022-11-28 11:13:08 -0500
committerNick Rosbrook <nick.rosbrook@canonical.com>2022-11-29 16:01:54 -0500
commit66bc4f6fb1a9354ec21c9fbe48b7b6c82c7717a2 (patch)
tree491d10d0a165ebf0200753655fc5b527107e27ab /src/oom
parentcb8d22fa8490ea19e82117b1e1ea794e3c0a4c98 (diff)
downloadsystemd-66bc4f6fb1a9354ec21c9fbe48b7b6c82c7717a2.tar.gz
oomd: only check prefix_uid when uid != 0
If the cgroup is owned by root there is no need to get prefix_uid. Only check prefix_uid when uid != 0, and then set MANAGED_OOM_PREFERENCE_NONE and return early if uid != prefix_uid.
Diffstat (limited to 'src/oom')
-rw-r--r--src/oom/oomd-util.c48
1 files changed, 27 insertions, 21 deletions
diff --git a/src/oom/oomd-util.c b/src/oom/oomd-util.c
index 70a1dc941e..7291e044eb 100644
--- a/src/oom/oomd-util.c
+++ b/src/oom/oomd-util.c
@@ -145,7 +145,7 @@ bool oomd_swap_free_below(const OomdSystemContext *ctx, int threshold_permyriad)
}
int oomd_fetch_cgroup_oom_preference(OomdCGroupContext *ctx, const char *prefix) {
- uid_t uid, prefix_uid;
+ uid_t uid;
int r;
assert(ctx);
@@ -160,28 +160,34 @@ int oomd_fetch_cgroup_oom_preference(OomdCGroupContext *ctx, const char *prefix)
if (r < 0)
return log_debug_errno(r, "Failed to get owner/group from %s: %m", ctx->path);
- r = cg_get_owner(SYSTEMD_CGROUP_CONTROLLER, prefix, &prefix_uid);
- if (r < 0)
- return log_debug_errno(r, "Failed to get owner/group from %s: %m", ctx->path);
+ if (uid != 0) {
+ uid_t prefix_uid;
- if (uid == prefix_uid || uid == 0) {
- /* Ignore most errors when reading the xattr since it is usually unset and cgroup xattrs are only used
- * as an optional feature of systemd-oomd (and the system might not even support them). */
- r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_avoid");
- if (r == -ENOMEM)
- return log_oom_debug();
- if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
- log_debug_errno(r, "Failed to get xattr user.oomd_avoid, ignoring: %m");
- ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_AVOID : ctx->preference;
+ r = cg_get_owner(SYSTEMD_CGROUP_CONTROLLER, prefix, &prefix_uid);
+ if (r < 0)
+ return log_debug_errno(r, "Failed to get owner/group from %s: %m", prefix);
- r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_omit");
- if (r == -ENOMEM)
- return log_oom_debug();
- if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
- log_debug_errno(r, "Failed to get xattr user.oomd_omit, ignoring: %m");
- ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_OMIT : ctx->preference;
- } else
- ctx->preference = MANAGED_OOM_PREFERENCE_NONE;
+ if (uid != prefix_uid) {
+ ctx->preference = MANAGED_OOM_PREFERENCE_NONE;
+ return 0;
+ }
+ }
+
+ /* Ignore most errors when reading the xattr since it is usually unset and cgroup xattrs are only used
+ * as an optional feature of systemd-oomd (and the system might not even support them). */
+ r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_avoid");
+ if (r == -ENOMEM)
+ return log_oom_debug();
+ if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
+ log_debug_errno(r, "Failed to get xattr user.oomd_avoid, ignoring: %m");
+ ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_AVOID : ctx->preference;
+
+ r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_omit");
+ if (r == -ENOMEM)
+ return log_oom_debug();
+ if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
+ log_debug_errno(r, "Failed to get xattr user.oomd_omit, ignoring: %m");
+ ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_OMIT : ctx->preference;
return 0;
}