Skip to content

Commit

Permalink
libcontainer: intelrdt: use init() to avoid race condition
Browse files Browse the repository at this point in the history
This is the follow-up PR of opencontainers#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 <xiaochen.shen@intel.com>
  • Loading branch information
xiaochenshen authored and stefanberger committed Sep 8, 2017
1 parent 8f57d28 commit c22ce79
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
2 changes: 1 addition & 1 deletion libcontainer/configs/validate/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (v *ConfigValidator) sysctl(config *configs.Config) error {

func (v *ConfigValidator) intelrdt(config *configs.Config) error {
if config.IntelRdt != nil {
if !intelrdt.IsIntelRdtEnabled() {
if !intelrdt.IsEnabled() {
return fmt.Errorf("intelRdt is specified in config, but Intel RDT feature is not supported or enabled")
}
if config.IntelRdt.L3CacheSchema == "" {
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestGetContainerStats(t *testing.T) {
if stats.CgroupStats.MemoryStats.Usage.Usage != 1024 {
t.Fatalf("expected memory usage 1024 but recevied %d", stats.CgroupStats.MemoryStats.Usage.Usage)
}
if intelrdt.IsIntelRdtEnabled() {
if intelrdt.IsEnabled() {
if stats.IntelRdtStats == nil {
t.Fatal("intel rdt stats are nil")
}
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestGetContainerState(t *testing.T) {
if memPath := paths["memory"]; memPath != expectedMemoryPath {
t.Fatalf("expected memory path %q but received %q", expectedMemoryPath, memPath)
}
if intelrdt.IsIntelRdtEnabled() {
if intelrdt.IsEnabled() {
intelRdtPath := state.IntelRdtPath
if intelRdtPath == "" {
t.Fatal("intel rdt path should not be empty")
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/factory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
cgroupManager: l.NewCgroupsManager(config.Cgroups, nil),
}
c.intelRdtManager = nil
if intelrdt.IsIntelRdtEnabled() && c.config.IntelRdt != nil {
if intelrdt.IsEnabled() && c.config.IntelRdt != nil {
c.intelRdtManager = l.NewIntelRdtManager(config, id, "")
}
c.state = &stoppedState{c: c}
Expand Down Expand Up @@ -245,7 +245,7 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
return nil, err
}
c.intelRdtManager = nil
if intelrdt.IsIntelRdtEnabled() && c.config.IntelRdt != nil {
if intelrdt.IsEnabled() && c.config.IntelRdt != nil {
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
}
return c, nil
Expand Down
37 changes: 18 additions & 19 deletions libcontainer/intelrdt/intelrdt.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ var (
intelRdtRootLock sync.Mutex

// The flag to indicate if Intel RDT is supported
isIntelRdtEnabled bool
isEnabled bool
)

type intelRdtData struct {
Expand All @@ -139,6 +139,21 @@ type intelRdtData struct {
pid int
}

// Check if Intel RDT is enabled in init()
func init() {
// 1. Check if hardware and kernel support Intel RDT/CAT feature
// "cat_l3" flag is set if supported
isFlagSet, err := parseCpuInfoFile("/proc/cpuinfo")
if !isFlagSet || err != nil {
isEnabled = false
return
}

// 2. Check if Intel RDT "resource control" filesystem is mounted
// The user guarantees to mount the filesystem
isEnabled = isIntelRdtMounted()
}

// Return the mount point path of Intel RDT "resource control" filesysem
func findIntelRdtMountpointDir() (string, error) {
f, err := os.Open("/proc/self/mountinfo")
Expand Down Expand Up @@ -369,24 +384,8 @@ func WriteIntelRdtTasks(dir string, pid int) error {
}

// Check if Intel RDT is enabled
func IsIntelRdtEnabled() bool {
// We have checked the flag before
if isIntelRdtEnabled {
return true
}

// 1. Check if hardware and kernel support Intel RDT/CAT feature
// "cat_l3" flag is set if supported
isFlagSet, err := parseCpuInfoFile("/proc/cpuinfo")
if !isFlagSet || err != nil {
isIntelRdtEnabled = false
return false
}

// 2. Check if Intel RDT "resource control" filesystem is mounted
// The user guarantees to mount the filesystem
isIntelRdtEnabled = isIntelRdtMounted()
return isIntelRdtEnabled
func IsEnabled() bool {
return isEnabled
}

// Get the 'container_id' path in Intel RDT "resource control" filesystem
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/intelrdt/intelrdt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestIntelRdtSetL3CacheSchema(t *testing.T) {
if !IsIntelRdtEnabled() {
if !IsEnabled() {
return
}

Expand Down
2 changes: 1 addition & 1 deletion utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
return nil, fmt.Errorf("systemd cgroup flag passed, but systemd support for managing cgroups is not available")
}
}
if intelrdt.IsIntelRdtEnabled() {
if intelrdt.IsEnabled() {
intelRdtManager := libcontainer.IntelRdtFs
return libcontainer.New(abs, cgroupManager, intelRdtManager, libcontainer.CriuPath(context.GlobalString("criu")))
}
Expand Down

0 comments on commit c22ce79

Please sign in to comment.