From 4796f49ca31dd3fa44b9d3c06b28c123bae5b67f Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Tue, 26 Jul 2022 15:36:47 -0400 Subject: [PATCH] libct/intelrdt: elide parsing mountinfo The intelrdt package only needs to parse mountinfo to find the mount point of the resctrl filesystem. Users are generally going to mount the resctrl filesystem to the pre-created /sys/fs/resctrl directory, so there is a common case where mountinfo parsing is not required. Optimize for the common case with a fast path which checks both for the existence of the /sys/fs/resctrl directory and whether the resctrl filesystem was mounted to that path using a single statfs syscall. Signed-off-by: Cory Snider (cherry picked from commit c156bde7cc9bb721f7110b987992d7f9c8b951a0) Signed-off-by: Kir Kolyshkin --- libcontainer/intelrdt/intelrdt.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/libcontainer/intelrdt/intelrdt.go b/libcontainer/intelrdt/intelrdt.go index 82bfd32859d..4cd9ffc108f 100644 --- a/libcontainer/intelrdt/intelrdt.go +++ b/libcontainer/intelrdt/intelrdt.go @@ -224,7 +224,7 @@ func featuresInit() { }) } -// Return the mount point path of Intel RDT "resource control" filesystem. +// findIntelRdtMountpointDir returns the mount point of the Intel RDT "resource control" filesystem. func findIntelRdtMountpointDir() (string, error) { mi, err := mountinfo.GetMounts(func(m *mountinfo.Info) (bool, bool) { // similar to mountinfo.FSTypeFilter but stops after the first match @@ -250,24 +250,32 @@ var ( rootOnce sync.Once ) +// The kernel creates this (empty) directory if resctrl is supported by the +// hardware and kernel. The user is responsible for mounting the resctrl +// filesystem, and they could mount it somewhere else if they wanted to. +const defaultResctrlMountpoint = "/sys/fs/resctrl" + // Root returns the Intel RDT "resource control" filesystem mount point. func Root() (string, error) { rootOnce.Do(func() { - // If resctrl is available, kernel creates this directory. - if unix.Access("/sys/fs/resctrl", unix.F_OK) != nil { - intelRdtRootErr = errNotFound + // Does this system support resctrl? + var statfs unix.Statfs_t + if err := unix.Statfs(defaultResctrlMountpoint, &statfs); err != nil { + if errors.Is(err, unix.ENOENT) { + err = errNotFound + } + intelRdtRootErr = err return } - // NB: ideally, we could just do statfs and RDTGROUP_SUPER_MAGIC check, but - // we have to parse mountinfo since we're also interested in mount options. - root, err := findIntelRdtMountpointDir() - if err != nil { - intelRdtRootErr = err + // Has the resctrl fs been mounted to the default mount point? + if statfs.Type == unix.RDTGROUP_SUPER_MAGIC { + intelRdtRoot = defaultResctrlMountpoint return } - intelRdtRoot = root + // The resctrl fs could have been mounted somewhere nonstandard. + intelRdtRoot, intelRdtRootErr = findIntelRdtMountpointDir() }) return intelRdtRoot, intelRdtRootErr