Skip to content

Commit 891fcc6

Browse files
committed
libct/intelrdt: elide parsing mountinfo
The intelrdt package needs to parse mountinfo for two things: to find the mountpoint of the resctrl filesystem, and to check if the user enabled the MBA Software Controller. Users are generally going to mount the resctrl filesystem to the pre-created /sys/fs/resctrl directory and neither libcontainer nor the rest of runc need to know whether the MBA Software Controller is enabled, 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 it using a single statfs syscall. Signed-off-by: Cory Snider <[email protected]>
1 parent 9039d8a commit 891fcc6

File tree

1 file changed

+57
-37
lines changed

1 file changed

+57
-37
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ var (
181181
catEnabled bool
182182
// The flag to indicate if Intel RDT/MBA is enabled
183183
mbaEnabled bool
184-
// The flag to indicate if Intel RDT/MBA Software Controller is enabled
185-
mbaScEnabled bool
186184

187185
// For Intel RDT initialization
188186
initOnce sync.Once
@@ -207,12 +205,7 @@ func featuresInit() {
207205
if _, err := os.Stat(filepath.Join(root, "info", "L3")); err == nil {
208206
catEnabled = true
209207
}
210-
if mbaScEnabled {
211-
// We confirm MBA Software Controller is enabled in step 1,
212-
// MBA should be enabled because MBA Software Controller
213-
// depends on MBA
214-
mbaEnabled = true
215-
} else if _, err := os.Stat(filepath.Join(root, "info", "MB")); err == nil {
208+
if _, err := os.Stat(filepath.Join(root, "info", "MB")); err == nil {
216209
mbaEnabled = true
217210
}
218211
if _, err := os.Stat(filepath.Join(root, "info", "L3_MON")); err != nil {
@@ -231,28 +224,36 @@ func featuresInit() {
231224
})
232225
}
233226

234-
// Return the mount point path of Intel RDT "resource control" filesystem.
235-
func findIntelRdtMountpointDir() (string, error) {
236-
mi, err := mountinfo.GetMounts(func(m *mountinfo.Info) (bool, bool) {
237-
// similar to mountinfo.FSTypeFilter but stops after the first match
238-
if m.FSType == "resctrl" {
239-
return false, true // don't skip, stop
227+
// for findResctrlMountinfo() use only.
228+
var (
229+
resctrlMountinfo *mountinfo.Info
230+
findMountinfoErr error
231+
findMountinfoOnce sync.Once
232+
)
233+
234+
// findResctrlMountinfo returns the mount info for the mounted Intel RDT "resource control" filesystem.
235+
func findResctrlMountinfo() (*mountinfo.Info, error) {
236+
findMountinfoOnce.Do(func() {
237+
mi, err := mountinfo.GetMounts(func(m *mountinfo.Info) (bool, bool) {
238+
// similar to mountinfo.FSTypeFilter but stops after the first match
239+
if m.FSType == "resctrl" {
240+
return false, true // don't skip, stop
241+
}
242+
return true, false // skip, keep going
243+
})
244+
if err != nil {
245+
findMountinfoErr = err
246+
return
247+
}
248+
if len(mi) < 1 {
249+
findMountinfoErr = errNotFound
250+
return
240251
}
241-
return true, false // skip, keep going
242-
})
243-
if err != nil {
244-
return "", err
245-
}
246-
if len(mi) < 1 {
247-
return "", errNotFound
248-
}
249252

250-
// Check if MBA Software Controller is enabled through mount option "-o mba_MBps"
251-
if strings.Contains(","+mi[0].VFSOptions+",", ",mba_MBps,") {
252-
mbaScEnabled = true
253-
}
253+
resctrlMountinfo = mi[0]
254+
})
254255

255-
return mi[0].Mountpoint, nil
256+
return resctrlMountinfo, findMountinfoErr
256257
}
257258

258259
// For Root() use only.
@@ -262,24 +263,37 @@ var (
262263
rootOnce sync.Once
263264
)
264265

266+
// The kernel creates this (empty) directory if resctrl is supported by the
267+
// hardware and kernel. The user is responsible for mounting the resctrl
268+
// filesystem, and they could mount it somewhere else if they wanted to.
269+
const defaultResctrlMountpoint = "/sys/fs/resctrl"
270+
265271
// Root returns the Intel RDT "resource control" filesystem mount point.
266272
func Root() (string, error) {
267273
rootOnce.Do(func() {
268-
// If resctrl is available, kernel creates this directory.
269-
if unix.Access("/sys/fs/resctrl", unix.F_OK) != nil {
270-
intelRdtRootErr = errNotFound
274+
// Does this system support resctrl?
275+
var statfs unix.Statfs_t
276+
if err := unix.Statfs(defaultResctrlMountpoint, &statfs); err != nil {
277+
if errors.Is(err, unix.ENOENT) {
278+
err = errNotFound
279+
}
280+
intelRdtRootErr = err
281+
return
282+
}
283+
284+
// Has the resctrl fs been mounted to the default mount point?
285+
if statfs.Type == unix.RDTGROUP_SUPER_MAGIC {
286+
intelRdtRoot = defaultResctrlMountpoint
271287
return
272288
}
273289

274-
// NB: ideally, we could just do statfs and RDTGROUP_SUPER_MAGIC check, but
275-
// we have to parse mountinfo since we're also interested in mount options.
276-
root, err := findIntelRdtMountpointDir()
290+
// The resctrl fs could have been mounted somewhere nonstandard.
291+
mi, err := findResctrlMountinfo()
277292
if err != nil {
278293
intelRdtRootErr = err
279294
return
280295
}
281-
282-
intelRdtRoot = root
296+
intelRdtRoot = mi.Mountpoint
283297
})
284298

285299
return intelRdtRoot, intelRdtRootErr
@@ -430,8 +444,14 @@ func IsMBAEnabled() bool {
430444

431445
// Check if Intel RDT/MBA Software Controller is enabled
432446
func IsMBAScEnabled() bool {
433-
featuresInit()
434-
return mbaScEnabled
447+
// Neither libcontainer nor runc need to check, and parsing mountinfo is
448+
// the only way to check it. Parse it lazily.
449+
mi, err := findResctrlMountinfo()
450+
if err != nil {
451+
return false
452+
}
453+
// Check if MBA Software Controller is enabled through mount option "-o mba_MBps"
454+
return strings.Contains(","+mi.VFSOptions+",", ",mba_MBps,")
435455
}
436456

437457
// Get the path of the clos group in "resource control" filesystem that the container belongs to

0 commit comments

Comments
 (0)