Skip to content

Commit

Permalink
libctr: Reset the inherited cpu affinity
Browse files Browse the repository at this point in the history
Old kernels do that automatically, but new kernels remember
the affinity that was set before the cgroup move due to

https://lore.kernel.org/lkml/20220922180041.1768141-1-longman@redhat.com

This is undesirable for containers, because they inherit
the systemd affinity when the should really move to the container
space cpus.

see https://issues.redhat.com/browse/OCPBUGS-15102 for more information

Co-authored-by:  Martin Sivak <msivak@redhat.com>
Signed-off-by: Peter Hunt <pehunt@redhat.com>
  • Loading branch information
haircommander and MarSik committed Sep 28, 2023
1 parent 1614cab commit d946178
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libcontainer/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (p *setnsProcess) start() (retErr error) {
initProcCgroupFile := fmt.Sprintf("/proc/%d/cgroup", p.initProcessPid)
initCg, initCgErr := cgroups.ParseCgroupFile(initProcCgroupFile)
if initCgErr == nil {
s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / compile-buildtags

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / lint

undefined: s) (typecheck)

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / lint

undefined: s (typecheck)

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / lint

undefined: s) (typecheck)

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / lint

undefined: s) (typecheck)

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, rootless)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x, rootless, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, rootless, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / cross-i386 (runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.20.x, rootless)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / cross-i386

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, rootless, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, -race, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, rootless, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, rootless, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, rootless, -race, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, rootless)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, rootless, -race, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.20.x, -race, runc_nodmz)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, rootless)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04, 1.21.x, criu-dev)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, rootless, -race)

undefined: s

Check failure on line 133 in libcontainer/process_linux.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.21.x, criu-dev)

undefined: s
if initCgPath, ok := initCg[""]; ok {
initCgDirpath := filepath.Join(fs2.UnifiedMountpoint, initCgPath)
logrus.Debugf("adding pid %d to cgroups %v failed (%v), attempting to join %q (obtained from %s)",
Expand All @@ -144,6 +145,14 @@ func (p *setnsProcess) start() (retErr error) {
}
}
}
// Reset the inherited cpu affinity. Old kernels do that automatically, but
// new kernels remember the affinity that was set before the cgroup move.
// This is undesirable, because it inherits the systemd affinity when the container
// should really move to the container space cpus.
if err := unix.SchedSetaffinity(p.pid(), &unix.CPUSet{}); err != nil && err != unix.EINVAL && err != unix.ENODEV {
return fmt.Errorf("error resetting pid %d affinity: %w", p.pid(), err)
}

if p.intelRdtPath != "" {
// if Intel RDT "resource control" filesystem path exists
_, err := os.Stat(p.intelRdtPath)
Expand Down Expand Up @@ -419,6 +428,14 @@ func (p *initProcess) start() (retErr error) {
if err := p.manager.Apply(p.pid()); err != nil {
return fmt.Errorf("unable to apply cgroup configuration: %w", err)
}

// Reset the inherited cpu affinity. Old kernels do that automatically, but
// new kernels remember the affinity that was set before the cgroup move.
// This is undesirable, because it inherits the systemd affinity when the container
// should really move to the container space cpus.
if err := unix.SchedSetaffinity(p.pid(), &unix.CPUSet{}); err != nil && err != unix.EINVAL && err != unix.ENODEV {
return fmt.Errorf("error resetting pid %d affinity: %w", p.pid(), err)
}
if p.intelRdtManager != nil {
if err := p.intelRdtManager.Apply(p.pid()); err != nil {
return fmt.Errorf("unable to apply Intel RDT configuration: %w", err)
Expand Down

0 comments on commit d946178

Please sign in to comment.