Skip to content

Commit 8698335

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 f3bfc57 commit 8698335

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
@@ -205,7 +205,6 @@ var (
205205
)
206206

207207
type intelRdtData struct {
208-
root string
209208
config *configs.Config
210209
}
211210

@@ -548,6 +547,14 @@ func (m *intelRdtManager) Apply(pid int) (err error) {
548547
m.mu.Lock()
549548
defer m.mu.Unlock()
550549

550+
if m.config.IntelRdt.ClosID != "" && m.config.IntelRdt.L3CacheSchema == "" && m.config.IntelRdt.MemBwSchema == "" {
551+
// Check that the CLOS exists, i.e. it has been pre-configured to
552+
// conform with the runtime spec
553+
if _, err := os.Stat(path); err != nil {
554+
return fmt.Errorf("clos dir not accessible (must be pre-created when l3CacheSchema and memBwSchema are empty): %w", err)
555+
}
556+
}
557+
551558
if err := os.MkdirAll(path, 0o755); err != nil {
552559
return newLastCmdError(err)
553560
}
@@ -723,6 +730,12 @@ func (m *intelRdtManager) Set(container *configs.Config) error {
723730
l3CacheSchema := container.IntelRdt.L3CacheSchema
724731
memBwSchema := container.IntelRdt.MemBwSchema
725732

733+
// TODO: verify that l3CacheSchema and/or memBwSchema match the
734+
// existing schemata if ClosID has been specified. This is a more
735+
// involved than reading the file and doing plain string comparison as
736+
// the value written in does not necessarily match what gets read out
737+
// (leading zeros, cache id ordering etc).
738+
726739
// Write a single joint schema string to schemata file
727740
if l3CacheSchema != "" && memBwSchema != "" {
728741
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
@@ -5,6 +5,8 @@ package intelrdt
55
import (
66
"errors"
77
"io"
8+
"os"
9+
"path/filepath"
810
"strings"
911
"testing"
1012
)
@@ -114,6 +116,36 @@ func TestIntelRdtSetMemBwScSchema(t *testing.T) {
114116
}
115117
}
116118

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