From 00b3ef8a3effc32b3a765f04fed02ed4186a1d2d Mon Sep 17 00:00:00 2001 From: Liu Hua Date: Tue, 6 Jun 2023 20:36:47 +0800 Subject: [PATCH] libct/cg: skip hidden ones when parsing cgroup mounts According to [1], "If a new mount is stacked on top of a previous existing mount, at pathname P, then the parent of the new mount is the previous mount at that location" Use this strategy to skip hidden cgroup mount. 1. https://man7.org/linux/man-pages/man5/proc.5.html Signed-off-by: Liu Hua --- libcontainer/cgroups/v1_utils.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/libcontainer/cgroups/v1_utils.go b/libcontainer/cgroups/v1_utils.go index 8524c468434..c6fe7e0f0c3 100644 --- a/libcontainer/cgroups/v1_utils.go +++ b/libcontainer/cgroups/v1_utils.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strings" "sync" "syscall" @@ -165,7 +166,22 @@ func (m Mount) GetOwnCgroup(cgroups map[string]string) (string, error) { func getCgroupMountsHelper(ss map[string]bool, mounts []*mountinfo.Info, all bool) ([]Mount, error) { res := make([]Mount, 0, len(ss)) numFound := 0 + parentIDs := make([]int, 0, len(mounts)) for _, mi := range mounts { + parentIDs = append(parentIDs, mi.Parent) + } + + sort.Slice(parentIDs, func(i, j int) bool { + return parentIDs[i] < parentIDs[j] + }) + + for _, mi := range mounts { + // This mount is hidden by other mount + // at the same mountpoint, so skip it. + if sort.SearchInts(parentIDs, mi.ID) < len(parentIDs) { + continue + } + m := Mount{ Mountpoint: mi.Mountpoint, Root: mi.Root,