Skip to content

Commit 685dc58

Browse files
committed
libcontainer/intelrdt: verify ClosID existence
Check that the ClosID directory pre-exists if no L3 or MB schema has been specified. Conform with the following line from runtime-spec (config-linux): If closID is set, and neither of l3CacheSchema and memBwSchema are set, runtime MUST check if corresponding pre-configured directory closID is present in mounted resctrl. If such pre-configured directory closID exists, runtime MUST assign container to this closID and generate an error if directory does not exist. Add a TODO note for verifying existing schemata against L3/MB parameters. Signed-off-by: Markus Lehtonen <[email protected]>
1 parent e40d51c commit 685dc58

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

libcontainer/intelrdt/intelrdt.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ var (
201201
)
202202

203203
type intelRdtData struct {
204-
root string
205204
config *configs.Config
206205
pid int
207206
}
@@ -563,6 +562,14 @@ func (m *intelRdtManager) Apply(pid int) (err error) {
563562
m.mu.Lock()
564563
defer m.mu.Unlock()
565564

565+
if m.config.IntelRdt.ClosID != "" && m.config.IntelRdt.L3CacheSchema == "" && m.config.IntelRdt.MemBwSchema == "" {
566+
// Check that the CLOS exists, i.e. it has been pre-configured to
567+
// conform with the runtime spec
568+
if _, err := os.Stat(path); err != nil {
569+
return fmt.Errorf("clos dir not accessible (must be pre-created when l3CacheSchema and memBwSchema are empty): %w", err)
570+
}
571+
}
572+
566573
if err := os.MkdirAll(path, 0o755); err != nil {
567574
return NewLastCmdError(err)
568575
}
@@ -738,6 +745,12 @@ func (m *intelRdtManager) Set(container *configs.Config) error {
738745
l3CacheSchema := container.IntelRdt.L3CacheSchema
739746
memBwSchema := container.IntelRdt.MemBwSchema
740747

748+
// TODO: verify that l3CacheSchema and/or memBwSchema match the
749+
// existing schemata if ClosID has been specified. This is a more
750+
// involved than reading the file and doing plain string comparison as
751+
// the value written in does not necessarily match what gets read out
752+
// (leading zeros, cache id ordering etc).
753+
741754
// Write a single joint schema string to schemata file
742755
if l3CacheSchema != "" && memBwSchema != "" {
743756
if err := writeFile(path, "schemata", l3CacheSchema+"\n"+memBwSchema); err != nil {

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package intelrdt
44

55
import (
66
"io"
7+
"os"
8+
"path/filepath"
79
"strings"
810
"testing"
911
)
@@ -113,6 +115,36 @@ func TestIntelRdtSetMemBwScSchema(t *testing.T) {
113115
}
114116
}
115117

118+
func TestApply(t *testing.T) {
119+
helper := NewIntelRdtTestUtil(t)
120+
defer helper.cleanup()
121+
122+
const closID = "test-clos"
123+
124+
helper.IntelRdtData.config.IntelRdt.ClosID = closID
125+
intelrdt := NewManager(helper.IntelRdtData.config, "", helper.IntelRdtPath)
126+
if err := intelrdt.Apply(1234); err == nil {
127+
t.Fatal("unexpected success when applying pid")
128+
}
129+
if _, err := os.Stat(filepath.Join(helper.IntelRdtPath, closID)); err == nil {
130+
t.Fatal("closid dir should not exist")
131+
}
132+
133+
// Dir should be created if some schema has been specified
134+
intelrdt.(*intelRdtManager).config.IntelRdt.L3CacheSchema = "L3:0=f"
135+
if err := intelrdt.Apply(1235); err != nil {
136+
t.Fatalf("Apply() failed: %v", err)
137+
}
138+
139+
pids, err := getIntelRdtParamString(intelrdt.GetPath(), "tasks")
140+
if err != nil {
141+
t.Fatalf("failed to read tasks file: %v", err)
142+
}
143+
if pids != "1235" {
144+
t.Fatalf("unexpected tasks file, expected '1235', got %q", pids)
145+
}
146+
}
147+
116148
const (
117149
mountinfoValid = `18 40 0:18 / /sys rw,nosuid,nodev,noexec,relatime shared:6 - sysfs sysfs rw
118150
19 40 0:3 / /proc rw,nosuid,nodev,noexec,relatime shared:5 - proc proc rw

libcontainer/intelrdt/util_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func NewIntelRdtTestUtil(t *testing.T) *intelRdtTestUtil {
3838
if err != nil {
3939
t.Fatal(err)
4040
}
41-
d.root = tempDir
42-
testIntelRdtPath := filepath.Join(d.root, "resctrl")
41+
intelRdtRoot = tempDir
42+
testIntelRdtPath := filepath.Join(tempDir, "resctrl")
4343
if err != nil {
4444
t.Fatal(err)
4545
}
@@ -53,6 +53,7 @@ func NewIntelRdtTestUtil(t *testing.T) *intelRdtTestUtil {
5353
}
5454

5555
func (c *intelRdtTestUtil) cleanup() {
56+
intelRdtRoot = ""
5657
os.RemoveAll(c.tempDir)
5758
}
5859

0 commit comments

Comments
 (0)