Skip to content

Commit e470a84

Browse files
authored
Merge pull request #4915 from cyphar/1.4-memory-policy
[1.4] Add memory policy and schemata support
2 parents 82561cc + 910f134 commit e470a84

File tree

23 files changed

+494
-145
lines changed

23 files changed

+494
-145
lines changed

events.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ func convertLibcontainerStats(ls *libcontainer.Stats) *types.Stats {
173173
if intelrdt.IsCMTEnabled() {
174174
s.IntelRdt.CMTStats = is.CMTStats
175175
}
176+
177+
s.IntelRdt.Schemata = is.Schemata
176178
}
177179

178180
s.NetworkInterfaces = ls.Interfaces

features.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ var featuresCommand = cli.Command{
5656
Enabled: &t,
5757
},
5858
IntelRdt: &features.IntelRdt{
59-
Enabled: &t,
59+
Enabled: &t,
60+
Schemata: &t,
61+
},
62+
MemoryPolicy: &features.MemoryPolicy{
63+
Modes: specconv.KnownMemoryPolicyModes(),
64+
Flags: specconv.KnownMemoryPolicyFlags(),
6065
},
6166
MountExtensions: &features.MountExtensions{
6267
IDMap: &features.IDMap{

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/moby/sys/userns v0.1.0
1616
github.com/mrunalp/fileutils v0.5.1
1717
github.com/opencontainers/cgroups v0.0.4
18-
github.com/opencontainers/runtime-spec v1.2.2-0.20250401095657-e935f995dd67
18+
github.com/opencontainers/runtime-spec v1.2.2-0.20250818071321-383cadbf08c0
1919
github.com/opencontainers/selinux v1.12.0
2020
github.com/seccomp/libseccomp-golang v0.11.1
2121
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ github.com/mrunalp/fileutils v0.5.1 h1:F+S7ZlNKnrwHfSwdlgNSkKo67ReVf8o9fel6C3dkm
4646
github.com/mrunalp/fileutils v0.5.1/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
4747
github.com/opencontainers/cgroups v0.0.4 h1:XVj8P/IHVms/j+7eh8ggdkTLAxjz84ZzuFyGoE28DR4=
4848
github.com/opencontainers/cgroups v0.0.4/go.mod h1:s8lktyhlGUqM7OSRL5P7eAW6Wb+kWPNvt4qvVfzA5vs=
49-
github.com/opencontainers/runtime-spec v1.2.2-0.20250401095657-e935f995dd67 h1:Q+KewUGTMamIe6Q39xCD/T1NC1POmaTlWnhjikCrZHA=
50-
github.com/opencontainers/runtime-spec v1.2.2-0.20250401095657-e935f995dd67/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
49+
github.com/opencontainers/runtime-spec v1.2.2-0.20250818071321-383cadbf08c0 h1:RLn0YfUWkiqPGtgUANvJrcjIkCHGRl3jcz/c557M28M=
50+
github.com/opencontainers/runtime-spec v1.2.2-0.20250818071321-383cadbf08c0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
5151
github.com/opencontainers/selinux v1.12.0 h1:6n5JV4Cf+4y0KNXW48TLj5DwfXpvWlxXplUkdTrmPb8=
5252
github.com/opencontainers/selinux v1.12.0/go.mod h1:BTPX+bjVbWGXw7ZZWUbdENt8w0htPSrlgOOysQaU62U=
5353
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

internal/linux/linux.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package linux
22

33
import (
44
"os"
5+
"unsafe"
56

67
"golang.org/x/sys/unix"
78
)
@@ -72,3 +73,15 @@ func Sendmsg(fd int, p, oob []byte, to unix.Sockaddr, flags int) error {
7273
})
7374
return os.NewSyscallError("sendmsg", err)
7475
}
76+
77+
// SetMempolicy wraps set_mempolicy.
78+
func SetMempolicy(mode uint, mask *unix.CPUSet) error {
79+
err := retryOnEINTR(func() error {
80+
_, _, errno := unix.Syscall(unix.SYS_SET_MEMPOLICY, uintptr(mode), uintptr(unsafe.Pointer(mask)), unsafe.Sizeof(*mask)*8)
81+
if errno != 0 {
82+
return errno
83+
}
84+
return nil
85+
})
86+
return os.NewSyscallError("set_mempolicy", err)
87+
}

libcontainer/configs/config.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ type Config struct {
214214
// to limit the resources (e.g., L3 cache, memory bandwidth) the container has available
215215
IntelRdt *IntelRdt `json:"intel_rdt,omitempty"`
216216

217+
// MemoryPolicy specifies NUMA memory policy for the container.
218+
MemoryPolicy *LinuxMemoryPolicy `json:"memory_policy,omitempty"`
219+
217220
// RootlessEUID is set when the runc was launched with non-zero EUID.
218221
// Note that RootlessEUID is set to false when launched with EUID=0 in userns.
219222
// When RootlessEUID is set, runc creates a new userns for the container.
@@ -305,7 +308,8 @@ type CPUAffinity struct {
305308
Initial, Final *unix.CPUSet
306309
}
307310

308-
func toCPUSet(str string) (*unix.CPUSet, error) {
311+
// ToCPUSet parses a string in list format into a unix.CPUSet, e.g. "0-3,5,7-9".
312+
func ToCPUSet(str string) (*unix.CPUSet, error) {
309313
if str == "" {
310314
return nil, nil
311315
}
@@ -356,7 +360,7 @@ func toCPUSet(str string) (*unix.CPUSet, error) {
356360
}
357361
}
358362
if s.Count() == 0 {
359-
return nil, fmt.Errorf("no CPUs found in %q", str)
363+
return nil, fmt.Errorf("no members found in set %q", str)
360364
}
361365

362366
return s, nil
@@ -367,11 +371,11 @@ func ConvertCPUAffinity(sa *specs.CPUAffinity) (*CPUAffinity, error) {
367371
if sa == nil {
368372
return nil, nil
369373
}
370-
initial, err := toCPUSet(sa.Initial)
374+
initial, err := ToCPUSet(sa.Initial)
371375
if err != nil {
372376
return nil, fmt.Errorf("bad CPUAffinity.Initial: %w", err)
373377
}
374-
final, err := toCPUSet(sa.Final)
378+
final, err := ToCPUSet(sa.Final)
375379
if err != nil {
376380
return nil, fmt.Errorf("bad CPUAffinity.Final: %w", err)
377381
}

libcontainer/configs/intelrdt.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ type IntelRdt struct {
44
// The identity for RDT Class of Service
55
ClosID string `json:"closID,omitempty"`
66

7+
// Schemata is a generic field to specify schemata file in the resctrl
8+
// filesystem. Each element represents one line written to the schemata file.
9+
Schemata []string `json:"schemata,omitempty"`
10+
711
// The schema for L3 cache id and capacity bitmask (CBM)
812
// Format: "L3:<cache_id0>=<cbm0>;<cache_id1>=<cbm1>;..."
913
L3CacheSchema string `json:"l3_cache_schema,omitempty"`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package configs
2+
3+
import "golang.org/x/sys/unix"
4+
5+
// Memory policy modes and flags as defined in /usr/include/linux/mempolicy.h
6+
7+
//nolint:revive,staticcheck,nolintlint // ignore ALL_CAPS errors in consts from numaif.h, will match unix.* in the future
8+
const (
9+
MPOL_DEFAULT = 0
10+
MPOL_PREFERRED = 1
11+
MPOL_BIND = 2
12+
MPOL_INTERLEAVE = 3
13+
MPOL_LOCAL = 4
14+
MPOL_PREFERRED_MANY = 5
15+
MPOL_WEIGHTED_INTERLEAVE = 6
16+
17+
MPOL_F_STATIC_NODES = 1 << 15
18+
MPOL_F_RELATIVE_NODES = 1 << 14
19+
MPOL_F_NUMA_BALANCING = 1 << 13
20+
)
21+
22+
// LinuxMemoryPolicy contains memory policy configuration.
23+
type LinuxMemoryPolicy struct {
24+
// Mode specifies memory policy mode without mode flags. See
25+
// set_mempolicy() documentation for details.
26+
Mode uint `json:"mode,omitempty"`
27+
// Flags contains mode flags.
28+
Flags uint `json:"flags,omitempty"`
29+
// Nodes contains NUMA nodes to which the mode applies.
30+
Nodes *unix.CPUSet `json:"nodes,omitempty"`
31+
}

libcontainer/configs/tocpuset_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func TestToCPUSet(t *testing.T) {
5858

5959
for _, tc := range testCases {
6060
t.Run(tc.in, func(t *testing.T) {
61-
out, err := toCPUSet(tc.in)
62-
t.Logf("toCPUSet(%q) = %v (error: %v)", tc.in, out, err)
61+
out, err := ToCPUSet(tc.in)
62+
t.Logf("ToCPUSet(%q) = %v (error: %v)", tc.in, out, err)
6363
// Check the error.
6464
if tc.isErr {
6565
if err == nil {

libcontainer/configs/validate/validator.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func Validate(config *configs.Config) error {
3333
mountsStrict,
3434
scheduler,
3535
ioPriority,
36+
memoryPolicy,
3637
}
3738
for _, c := range checks {
3839
if err := c(config); err != nil {
@@ -482,3 +483,26 @@ func ioPriority(config *configs.Config) error {
482483

483484
return nil
484485
}
486+
487+
func memoryPolicy(config *configs.Config) error {
488+
mpol := config.MemoryPolicy
489+
if mpol == nil {
490+
return nil
491+
}
492+
switch mpol.Mode {
493+
case configs.MPOL_DEFAULT, configs.MPOL_LOCAL:
494+
if mpol.Nodes != nil && mpol.Nodes.Count() != 0 {
495+
return fmt.Errorf("memory policy mode requires 0 nodes but got %d", mpol.Nodes.Count())
496+
}
497+
case configs.MPOL_BIND, configs.MPOL_INTERLEAVE,
498+
configs.MPOL_PREFERRED_MANY, configs.MPOL_WEIGHTED_INTERLEAVE:
499+
if mpol.Nodes == nil || mpol.Nodes.Count() == 0 {
500+
return fmt.Errorf("memory policy mode requires at least one node but got 0")
501+
}
502+
case configs.MPOL_PREFERRED:
503+
// Zero or more nodes are allowed by the kernel.
504+
default:
505+
return fmt.Errorf("invalid memory policy mode: %d", mpol.Mode)
506+
}
507+
return nil
508+
}

0 commit comments

Comments
 (0)