Skip to content

Commit 62f8af1

Browse files
xiaochenshenstefanberger
authored andcommitted
libcontainer: intelrdt: use init() to avoid race condition
This is the follow-up PR of #1279 to fix remaining issues: Use init() to avoid race condition in IsIntelRdtEnabled(). Add also rename some variables and functions. Signed-off-by: Xiaochen Shen <[email protected]>
1 parent 45fbbc7 commit 62f8af1

File tree

6 files changed

+25
-26
lines changed

6 files changed

+25
-26
lines changed

libcontainer/configs/validate/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (v *ConfigValidator) sysctl(config *configs.Config) error {
159159

160160
func (v *ConfigValidator) intelrdt(config *configs.Config) error {
161161
if config.IntelRdt != nil {
162-
if !intelrdt.IsIntelRdtEnabled() {
162+
if !intelrdt.IsEnabled() {
163163
return fmt.Errorf("intelRdt is specified in config, but Intel RDT feature is not supported or enabled")
164164
}
165165
if config.IntelRdt.L3CacheSchema == "" {

libcontainer/container_linux_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func TestGetContainerStats(t *testing.T) {
160160
if stats.CgroupStats.MemoryStats.Usage.Usage != 1024 {
161161
t.Fatalf("expected memory usage 1024 but recevied %d", stats.CgroupStats.MemoryStats.Usage.Usage)
162162
}
163-
if intelrdt.IsIntelRdtEnabled() {
163+
if intelrdt.IsEnabled() {
164164
if stats.IntelRdtStats == nil {
165165
t.Fatal("intel rdt stats are nil")
166166
}
@@ -231,7 +231,7 @@ func TestGetContainerState(t *testing.T) {
231231
if memPath := paths["memory"]; memPath != expectedMemoryPath {
232232
t.Fatalf("expected memory path %q but received %q", expectedMemoryPath, memPath)
233233
}
234-
if intelrdt.IsIntelRdtEnabled() {
234+
if intelrdt.IsEnabled() {
235235
intelRdtPath := state.IntelRdtPath
236236
if intelRdtPath == "" {
237237
t.Fatal("intel rdt path should not be empty")

libcontainer/factory_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
204204
cgroupManager: l.NewCgroupsManager(config.Cgroups, nil),
205205
}
206206
c.intelRdtManager = nil
207-
if intelrdt.IsIntelRdtEnabled() && c.config.IntelRdt != nil {
207+
if intelrdt.IsEnabled() && c.config.IntelRdt != nil {
208208
c.intelRdtManager = l.NewIntelRdtManager(config, id, "")
209209
}
210210
c.state = &stoppedState{c: c}
@@ -245,7 +245,7 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
245245
return nil, err
246246
}
247247
c.intelRdtManager = nil
248-
if intelrdt.IsIntelRdtEnabled() && c.config.IntelRdt != nil {
248+
if intelrdt.IsEnabled() && c.config.IntelRdt != nil {
249249
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
250250
}
251251
return c, nil

libcontainer/intelrdt/intelrdt.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ var (
130130
intelRdtRootLock sync.Mutex
131131

132132
// The flag to indicate if Intel RDT is supported
133-
isIntelRdtEnabled bool
133+
isEnabled bool
134134
)
135135

136136
type intelRdtData struct {
@@ -139,6 +139,21 @@ type intelRdtData struct {
139139
pid int
140140
}
141141

142+
// Check if Intel RDT is enabled in init()
143+
func init() {
144+
// 1. Check if hardware and kernel support Intel RDT/CAT feature
145+
// "cat_l3" flag is set if supported
146+
isFlagSet, err := parseCpuInfoFile("/proc/cpuinfo")
147+
if !isFlagSet || err != nil {
148+
isEnabled = false
149+
return
150+
}
151+
152+
// 2. Check if Intel RDT "resource control" filesystem is mounted
153+
// The user guarantees to mount the filesystem
154+
isEnabled = isIntelRdtMounted()
155+
}
156+
142157
// Return the mount point path of Intel RDT "resource control" filesysem
143158
func findIntelRdtMountpointDir() (string, error) {
144159
f, err := os.Open("/proc/self/mountinfo")
@@ -369,24 +384,8 @@ func WriteIntelRdtTasks(dir string, pid int) error {
369384
}
370385

371386
// Check if Intel RDT is enabled
372-
func IsIntelRdtEnabled() bool {
373-
// We have checked the flag before
374-
if isIntelRdtEnabled {
375-
return true
376-
}
377-
378-
// 1. Check if hardware and kernel support Intel RDT/CAT feature
379-
// "cat_l3" flag is set if supported
380-
isFlagSet, err := parseCpuInfoFile("/proc/cpuinfo")
381-
if !isFlagSet || err != nil {
382-
isIntelRdtEnabled = false
383-
return false
384-
}
385-
386-
// 2. Check if Intel RDT "resource control" filesystem is mounted
387-
// The user guarantees to mount the filesystem
388-
isIntelRdtEnabled = isIntelRdtMounted()
389-
return isIntelRdtEnabled
387+
func IsEnabled() bool {
388+
return isEnabled
390389
}
391390

392391
// Get the 'container_id' path in Intel RDT "resource control" filesystem

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestIntelRdtSetL3CacheSchema(t *testing.T) {
11-
if !IsIntelRdtEnabled() {
11+
if !IsEnabled() {
1212
return
1313
}
1414

utils_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
4343
return nil, fmt.Errorf("systemd cgroup flag passed, but systemd support for managing cgroups is not available")
4444
}
4545
}
46-
if intelrdt.IsIntelRdtEnabled() {
46+
if intelrdt.IsEnabled() {
4747
intelRdtManager := libcontainer.IntelRdtFs
4848
return libcontainer.New(abs, cgroupManager, intelRdtManager, libcontainer.CriuPath(context.GlobalString("criu")))
4949
}

0 commit comments

Comments
 (0)