From 11b0d57c9375e24746a7f781ef3d2bca63e9d87a Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 20 Dec 2021 14:48:40 -0800 Subject: [PATCH] pkg/kubelet/cm/cgroup_manager: simplify setting hugetlb Commit 79be8be10edba made hugetlb settings optional if cgroup v2 is used and hugetlb is not available, fixing issue 92933. Note at that time this was only needed for v2, because for v1 the resources were set one-by-one, and only for supported resources. Commit d312ef7eb6730099 switched the code to using Set from runc/libcontainer cgroups manager, and expanded the check to cgroup v1 as well. Move this check earlier, to inside m.toResources, so instead of converting all hugetlb resources from ResourceConfig to libcontainers's Resources.HugetlbLimit, and then setting it to nil, we can skip the conversion entirely if hugetlb is not supported, thus not doing the work that is not needed. Signed-off-by: Kir Kolyshkin --- pkg/kubelet/cm/cgroup_manager_linux.go | 51 ++++++++++++++------------ 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/pkg/kubelet/cm/cgroup_manager_linux.go b/pkg/kubelet/cm/cgroup_manager_linux.go index 7c153703bb9..e09930e33aa 100644 --- a/pkg/kubelet/cm/cgroup_manager_linux.go +++ b/pkg/kubelet/cm/cgroup_manager_linux.go @@ -404,8 +404,34 @@ func (m *cgroupManagerImpl) toResources(resourceConfig *ResourceConfig) *libcont if resourceConfig.PidsLimit != nil { resources.PidsLimit = *resourceConfig.PidsLimit } - // if huge pages are enabled, we set them in libcontainer - // for each page size enumerated, set that value + + m.maybeSetHugetlb(resourceConfig, resources) + + // Ideally unified is used for all the resources when running on cgroup v2. + // It doesn't make difference for the memory.max limit, but for e.g. the cpu controller + // you can specify the correct setting without relying on the conversions performed by the OCI runtime. + if resourceConfig.Unified != nil && libcontainercgroups.IsCgroup2UnifiedMode() { + resources.Unified = make(map[string]string) + for k, v := range resourceConfig.Unified { + resources.Unified[k] = v + } + } + return resources +} + +func (m *cgroupManagerImpl) maybeSetHugetlb(resourceConfig *ResourceConfig, resources *libcontainerconfigs.Resources) { + // Check if hugetlb is supported. + if libcontainercgroups.IsCgroup2UnifiedMode() { + if !getSupportedUnifiedControllers().Has("hugetlb") { + klog.V(6).InfoS("Optional subsystem not supported: hugetlb") + return + } + } else if _, ok := m.subsystems.MountPoints["hugetlb"]; !ok { + klog.V(6).InfoS("Optional subsystem not supported: hugetlb") + return + } + + // For each page size enumerated, set that value. pageSizes := sets.NewString() for pageSize, limit := range resourceConfig.HugePageLimit { sizeString, err := v1helper.HugePageUnitSizeFromByteSize(pageSize) @@ -429,16 +455,6 @@ func (m *cgroupManagerImpl) toResources(resourceConfig *ResourceConfig) *libcont Limit: uint64(0), }) } - // Ideally unified is used for all the resources when running on cgroup v2. - // It doesn't make difference for the memory.max limit, but for e.g. the cpu controller - // you can specify the correct setting without relying on the conversions performed by the OCI runtime. - if resourceConfig.Unified != nil && libcontainercgroups.IsCgroup2UnifiedMode() { - resources.Unified = make(map[string]string) - for k, v := range resourceConfig.Unified { - resources.Unified[k] = v - } - } - return resources } // Update updates the cgroup with the specified Cgroup Configuration @@ -469,17 +485,6 @@ func (m *cgroupManagerImpl) Update(cgroupConfig *CgroupConfig) error { updateSystemdCgroupInfo(libcontainerCgroupConfig, cgroupConfig.Name) } - if unified { - supportedControllers := getSupportedUnifiedControllers() - if !supportedControllers.Has("hugetlb") { - resources.HugetlbLimit = nil - klog.V(6).InfoS("Optional subsystem not supported: hugetlb") - } - } else if _, ok := m.subsystems.MountPoints["hugetlb"]; !ok { - resources.HugetlbLimit = nil - klog.V(6).InfoS("Optional subsystem not supported: hugetlb") - } - manager, err := m.adapter.newManager(libcontainerCgroupConfig, paths) if err != nil { return fmt.Errorf("failed to create cgroup manager: %v", err)