oom: implement avoid/omit xattr support

There may be situations where a cgroup should be protected from killing
or deprioritized as a candidate. In FB oomd xattrs are used to bias oomd
away from supervisor cgroups and towards worker cgroups in container
tasks. On desktops this can be used to protect important units with
unpredictable resource consumption.

The patch allows systemd-oomd to understand 2 xattrs:
"user.oomd_avoid" and "user.oomd_omit". If systemd-oomd sees these
xattrs set to 1 on a candidate cgroup (i.e. while attempting to kill something)
AND the cgroup is owned by root, it will either deprioritize the cgroup as
a candidate (avoid) or remove it completely as a candidate (omit).

Usage is restricted to root owned cgroups to prevent situations where an
unprivileged user can set their own cgroups lower in the kill priority than
another user's (and prevent them from omitting their units from
systemd-oomd killing).
This commit is contained in:
Anita Zhang
2021-02-09 02:27:40 -08:00
parent 242d75bdaa
commit 59331b8e29
8 changed files with 169 additions and 13 deletions
+33
View File
@@ -635,6 +635,20 @@ int cg_get_xattr_malloc(const char *controller, const char *path, const char *na
return r;
}
int cg_get_xattr_bool(const char *controller, const char *path, const char *name) {
_cleanup_free_ char *val = NULL;
int r;
assert(path);
assert(name);
r = cg_get_xattr_malloc(controller, path, name, &val);
if (r < 0)
return r;
return parse_boolean(val);
}
int cg_remove_xattr(const char *controller, const char *path, const char *name) {
_cleanup_free_ char *fs = NULL;
int r;
@@ -1703,6 +1717,25 @@ int cg_get_attribute_as_bool(const char *controller, const char *path, const cha
return 0;
}
int cg_get_owner(const char *controller, const char *path, uid_t *ret_uid) {
_cleanup_free_ char *f = NULL;
struct stat stats;
int r;
assert(ret_uid);
r = cg_get_path(controller, path, NULL, &f);
if (r < 0)
return r;
r = stat(f, &stats);
if (r < 0)
return -errno;
*ret_uid = stats.st_uid;
return 0;
}
int cg_get_keyed_attribute_full(
const char *controller,
const char *path,
+3
View File
@@ -212,10 +212,13 @@ int cg_get_attribute_as_uint64(const char *controller, const char *path, const c
int cg_get_attribute_as_bool(const char *controller, const char *path, const char *attribute, bool *ret);
int cg_set_access(const char *controller, const char *path, uid_t uid, gid_t gid);
int cg_get_owner(const char *controller, const char *path, uid_t *ret_uid);
int cg_set_xattr(const char *controller, const char *path, const char *name, const void *value, size_t size, int flags);
int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size);
int cg_get_xattr_malloc(const char *controller, const char *path, const char *name, char **ret);
/* Returns negative on error, and 0 or 1 on success for the bool value */
int cg_get_xattr_bool(const char *controller, const char *path, const char *name);
int cg_remove_xattr(const char *controller, const char *path, const char *name);
int cg_install_release_agent(const char *controller, const char *agent);