Skip to content

Commit

Permalink
Merge pull request #1216 from giuseppe/fix-domain-threaded-parent
Browse files Browse the repository at this point in the history
cgroups: fix creating cgroup under "domain threaded"
  • Loading branch information
flouthoc authored May 16, 2023
2 parents 1af41ed + c56c3c4 commit ab4b251
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/libcrun/cgroup-setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ enter_cgroup_v2 (pid_t pid, pid_t init_pid, const char *path, bool create_if_mis
{
crun_error_release (err);

ret = make_cgroup_threaded (path, err);
ret = maybe_make_cgroup_threaded (path, err);
if (UNLIKELY (ret < 0))
return ret;

Expand Down
56 changes: 33 additions & 23 deletions src/libcrun/cgroup-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ libcrun_cgroups_create_symlinks (int dirfd, libcrun_error_t *err)
}

int
make_cgroup_threaded (const char *path, libcrun_error_t *err)
maybe_make_cgroup_threaded (const char *path, libcrun_error_t *err)
{
cleanup_free char *cgroup_path_type = NULL;
const char *const threaded = "threaded";
cleanup_free char *content = NULL;
cleanup_free char *buffer = NULL;
const char *parent;
size_t size;
char *it;
int ret;

path = consume_slashes (path);
Expand All @@ -87,23 +92,29 @@ make_cgroup_threaded (const char *path, libcrun_error_t *err)
if (UNLIKELY (ret < 0))
return ret;

ret = write_file (cgroup_path_type, threaded, strlen (threaded), err);
if (UNLIKELY (ret < 0 && errno == EOPNOTSUPP))
ret = read_all_file (cgroup_path_type, &content, &size, err);
if (UNLIKELY (ret < 0))
return ret;

if (size > 0)
{
cleanup_free char *buffer = xstrdup (path);
const char *parent = consume_slashes (dirname (buffer));
if (parent[0])
{
crun_error_release (err);
it = content + size - 1;
while (*it == '\n' && it > content)
*it-- = '\0';
}

ret = make_cgroup_threaded (parent, err);
if (ret < 0)
return ret;
if (strcmp (content, "domain") == 0 || strcmp (content, "domain threaded") == 0)
return 0;

return write_file (cgroup_path_type, threaded, strlen (threaded), err);
}
buffer = xstrdup (path);
parent = consume_slashes (dirname (buffer));
if (parent[0] && strcmp (parent, "."))
{
ret = maybe_make_cgroup_threaded (parent, err);
if (ret < 0)
return ret;
}
return ret;
return write_file (cgroup_path_type, threaded, strlen (threaded), err);
}

int
Expand Down Expand Up @@ -138,7 +149,7 @@ move_process_to_cgroup (pid_t pid, const char *subsystem, const char *path, libc

crun_error_release (err);

ret = make_cgroup_threaded (path, err);
ret = maybe_make_cgroup_threaded (path, err);
if (UNLIKELY (ret < 0))
return ret;

Expand Down Expand Up @@ -788,6 +799,13 @@ write_controller_file (const char *path, int controllers_to_enable, libcrun_erro

crun_error_release (err);

if (e == EOPNOTSUPP)
{
ret = maybe_make_cgroup_threaded (path, err);
if (UNLIKELY (ret < 0))
return ret;
}

/* It seems the kernel can return EBUSY when a process was moved to a sub-cgroup
and the controllers are enabled in its parent cgroup. Retry a few times when
it happens. */
Expand All @@ -811,14 +829,6 @@ write_controller_file (const char *path, int controllers_to_enable, libcrun_erro

if (e == EBUSY)
repeat = true;
else if (e == EOPNOTSUPP)
{
ret = make_cgroup_threaded (path, err);
if (UNLIKELY (ret < 0))
return ret;

repeat = true;
}

continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcrun/cgroup-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ int libcrun_get_cgroup_mode (libcrun_error_t *err);

int libcrun_get_cgroup_dirfd (struct libcrun_cgroup_status *status, const char *sub_cgroup, libcrun_error_t *err);

int make_cgroup_threaded (const char *path, libcrun_error_t *err);
int maybe_make_cgroup_threaded (const char *path, libcrun_error_t *err);

#endif

0 comments on commit ab4b251

Please sign in to comment.