From 08d9fea6621b16d577f840aa8e2cffdbd50cc6d5 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Tue, 14 Nov 2023 11:58:58 -0500 Subject: [PATCH] preconfigure cpuset with required resources if we already know the cpuset, then we should preconfigure the newly created cgroup with those values, instead of using the parent's set. This prevents needless churn in the kernel as it tracks which CPUs have load balancing disabled. Signed-off-by: Peter Hunt --- src/libcrun/cgroup-internal.h | 1 + src/libcrun/cgroup-setup.c | 26 +++++++++++++++++++++++--- src/libcrun/cgroup-systemd.c | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/libcrun/cgroup-internal.h b/src/libcrun/cgroup-internal.h index 9823c2bd08..176e25e951 100644 --- a/src/libcrun/cgroup-internal.h +++ b/src/libcrun/cgroup-internal.h @@ -81,6 +81,7 @@ convert_shares_to_weight (uint64_t shares) } int initialize_cpuset_subsystem (const char *path, libcrun_error_t *err); +int initialize_cpuset_subsystem_resources (const char *path, runtime_spec_schema_config_linux_resources *resources, libcrun_error_t *err); int write_cpuset_resources (int dirfd_cpuset, int cgroup2, runtime_spec_schema_config_linux_resources_cpu *cpu, libcrun_error_t *err); diff --git a/src/libcrun/cgroup-setup.c b/src/libcrun/cgroup-setup.c index 570ad9b796..2f058b21ce 100644 --- a/src/libcrun/cgroup-setup.c +++ b/src/libcrun/cgroup-setup.c @@ -40,7 +40,7 @@ #include static int -initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char *mems, libcrun_error_t *err) +initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char *mems, runtime_spec_schema_config_linux_resources *resources, libcrun_error_t *err) { cleanup_close int dirfd = -1; cleanup_close int mems_fd = -1; @@ -95,7 +95,7 @@ initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char * return 0; path[parent_path_len] = '\0'; - ret = initialize_cpuset_subsystem_rec (path, parent_path_len, cpus, mems, err); + ret = initialize_cpuset_subsystem_rec (path, parent_path_len, cpus, mems, resources, err); path[parent_path_len] = '/'; if (UNLIKELY (ret < 0)) { @@ -104,6 +104,15 @@ initialize_cpuset_subsystem_rec (char *path, size_t path_len, char *cpus, char * } } + /* If we know the resources, use them, instead of initializing with the full set, only to revert it later. */ + if (resources && resources->cpu) + { + if (resources->cpu->cpus) + cpus = xstrdup (resources->cpu->cpus); + if (resources->cpu->mems) + mems = xstrdup (resources->cpu->mems); + } + if (cpus_fd >= 0) { b_len = TEMP_FAILURE_RETRY (write (cpus_fd, cpus, strlen (cpus))); @@ -129,7 +138,18 @@ initialize_cpuset_subsystem (const char *path, libcrun_error_t *err) char mems_buf[257]; cpus_buf[0] = mems_buf[0] = '\0'; - return initialize_cpuset_subsystem_rec (tmp_path, strlen (tmp_path), cpus_buf, mems_buf, err); + return initialize_cpuset_subsystem_rec (tmp_path, strlen (tmp_path), cpus_buf, mems_buf, NULL, err); +} + +int +initialize_cpuset_subsystem_resources (const char *path, runtime_spec_schema_config_linux_resources *resources, libcrun_error_t *err) +{ + cleanup_free char *tmp_path = xstrdup (path); + char cpus_buf[257]; + char mems_buf[257]; + + cpus_buf[0] = mems_buf[0] = '\0'; + return initialize_cpuset_subsystem_rec (tmp_path, strlen (tmp_path), cpus_buf, mems_buf, resources, err); } static int diff --git a/src/libcrun/cgroup-systemd.c b/src/libcrun/cgroup-systemd.c index 3b1155de1b..f8248241cb 100644 --- a/src/libcrun/cgroup-systemd.c +++ b/src/libcrun/cgroup-systemd.c @@ -252,7 +252,7 @@ setup_cpuset_for_systemd_v1 (runtime_spec_schema_config_linux_resources *resourc if (UNLIKELY (ret < 0)) return ret; - ret = initialize_cpuset_subsystem (cgroup_path, err); + ret = initialize_cpuset_subsystem_resources (cgroup_path, resources, err); if (UNLIKELY (ret < 0)) return ret;