Skip to content

Commit

Permalink
cgroups/cgfsng: improve swap accounting support detection
Browse files Browse the repository at this point in the history
When LXCFS daemon runs in a root cgroup of cgroup2 tree,
we need to go down the tree when checking for memory.swap.current.

We already have some logic to go up the tree from LXCFS daemon's
cgroup, but it's useless when LXCFS daemon sits in the cgroup2 tree root.

Signed-off-by: Alexander Mikhalitsyn <[email protected]>
  • Loading branch information
mihalicyn committed Oct 2, 2024
1 parent c303ae2 commit 49e862b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
56 changes: 54 additions & 2 deletions src/cgroups/cgfsng.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,20 +636,72 @@ static bool cgfsng_can_use_swap(struct cgroup_ops *ops, const char *cgroup)
__do_free char *cgroup_rel = NULL, *junk_value = NULL;
const char *file;
struct hierarchy *h;
bool ret;

h = ops->get_hierarchy(ops, "memory");
if (!h)
return false;

cgroup_rel = must_make_path_relative(cgroup, NULL);
file = is_unified_hierarchy(h) ? "memory.swap.current" : "memory.memsw.usage_in_bytes";

/* For v2, we need to look at the lower levels of the hierarchy because
* no 'memory.swap.current' file exists at the root. We must search
* upwards in the hierarchy in case memory accounting is disabled via
* cgroup.subtree_control for the given cgroup itself.
*/
int ret = cgroup_walkup_to_root(ops->cgroup2_root_fd, h->fd, cgroup_rel, file, &junk_value);
return ret == 0;
if (is_cgroup2_fd(h->fd) && strcmp(cgroup, "/") == 0) {
/*
* It looks like LXCFS sits in the root cgroup,
* which means that we have to find *some* cgroup
* down the tree and check a (file) presence in there.
*
* Note, that this only needed for cgroup2.
*/

__do_close int fd = -EBADF;
__do_closedir DIR *dir = NULL;
struct dirent *dent;

fd = dup(h->fd);
if (fd < 0)
return false;

dir = fdopendir(fd);
if (!dir) {
lxcfs_error("Failed to open memory cgroup hierarchy\n");
return false;
}
/* Transfer ownership to fdopendir(). */
move_fd(fd);

ret = false;
while (((dent = readdir(dir)) != NULL)) {
if (strcmp(dent->d_name, ".") == 0 ||
strcmp(dent->d_name, "..") == 0)
continue;

if (dent->d_type == DT_DIR) {
__do_free char *path;

path = must_make_path_relative(dent->d_name, "memory.swap.current", NULL);

if (!faccessat(h->fd, path, F_OK, 0)) {
/* We found it. Exit. */
ret = true;
break;
}
}
}
} else {
/*
* We can check a (file) presence on the current
* level and go up in the cgroup tree if needed.
*/
ret = cgroup_walkup_to_root(ops->cgroup2_root_fd, h->fd, cgroup_rel, file, &junk_value) == 0;
}

return ret;
}

static int cgfsng_get_memory_stats(struct cgroup_ops *ops, const char *cgroup,
Expand Down
1 change: 1 addition & 0 deletions src/cgroups/cgroup_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extern char *cg_hybrid_get_current_cgroup(char *basecginfo,
extern char *cg_legacy_get_current_cgroup(pid_t pid, const char *controller);
extern bool mkdir_p(const char *dir, mode_t mode);
extern bool is_cgroup_fd(int fd);
extern bool is_cgroup2_fd(int fd);

static inline int openat_safe(int fd, const char *path)
{
Expand Down

0 comments on commit 49e862b

Please sign in to comment.