Skip to content

Commit f36ed4b

Browse files
committed
libcontainer: cgroups: don't Set in Apply
Apply and Set are two separate operations, and it doesn't make sense to group the two together (especially considering that the bootstrap process is added to the cgroup as well). The only exception to this is the memory cgroup, which requires the configuration to be set before processes can join. One of the weird cases to deal with is systemd. Systemd sets some of the cgroup configuration options, but not all of them. Because memory is a special case, we need to explicitly set memory in the systemd Apply(). Otherwise, the rest can be safely re-applied in .Set() as usual. Signed-off-by: Aleksa Sarai <[email protected]>
1 parent db3159c commit f36ed4b

File tree

13 files changed

+66
-183
lines changed

13 files changed

+66
-183
lines changed

libcontainer/cgroups/fs/apply_raw.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ func (m *Manager) Apply(pid int) (err error) {
106106
return nil
107107
}
108108

109-
var c = m.Cgroups
110-
111109
d, err := getCgroupData(m.Cgroups, pid)
112110
if err != nil {
113111
return err
@@ -136,13 +134,6 @@ func (m *Manager) Apply(pid int) (err error) {
136134
paths[sys.Name()] = p
137135
}
138136
m.Paths = paths
139-
140-
if paths["cpu"] != "" {
141-
if err := CheckCpushares(paths["cpu"], c.Resources.CpuShares); err != nil {
142-
return err
143-
}
144-
}
145-
146137
return nil
147138
}
148139

@@ -181,6 +172,11 @@ func (m *Manager) GetStats() (*cgroups.Stats, error) {
181172

182173
func (m *Manager) Set(container *configs.Config) error {
183174
for name, path := range m.Paths {
175+
// We can't set this here, because after being applied, memcg doesn't
176+
// allow a non-empty cgroup from having its limits changed.
177+
if name == "memory" {
178+
continue
179+
}
184180
sys, err := subsystems.Get(name)
185181
if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) {
186182
continue
@@ -189,6 +185,12 @@ func (m *Manager) Set(container *configs.Config) error {
189185
return err
190186
}
191187
}
188+
189+
if m.Paths["cpu"] != "" {
190+
if err := CheckCpushares(m.Paths["cpu"], container.Cgroups.Resources.CpuShares); err != nil {
191+
return err
192+
}
193+
}
192194
return nil
193195
}
194196

libcontainer/cgroups/fs/blkio.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ func (s *BlkioGroup) Name() string {
2222
}
2323

2424
func (s *BlkioGroup) Apply(d *cgroupData) error {
25-
dir, err := d.join("blkio")
25+
_, err := d.join("blkio")
2626
if err != nil && !cgroups.IsNotFound(err) {
2727
return err
2828
}
29-
30-
if err := s.Set(dir, d.config); err != nil {
31-
return err
32-
}
33-
3429
return nil
3530
}
3631

libcontainer/cgroups/fs/cpu.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ func (s *CpuGroup) Name() string {
2222
func (s *CpuGroup) Apply(d *cgroupData) error {
2323
// We always want to join the cpu group, to allow fair cpu scheduling
2424
// on a container basis
25-
dir, err := d.join("cpu")
25+
_, err := d.join("cpu")
2626
if err != nil && !cgroups.IsNotFound(err) {
2727
return err
2828
}
29-
30-
if err := s.Set(dir, d.config); err != nil {
31-
return err
32-
}
33-
3429
return nil
3530
}
3631

libcontainer/cgroups/fs/cpuset.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ func (s *CpusetGroup) ApplyDir(dir string, cgroup *configs.Cgroup, pid int) erro
6363
if err := s.ensureParent(dir, root); err != nil {
6464
return err
6565
}
66-
// the default values inherit from parent cgroup are already set in
67-
// s.ensureParent, cover these if we have our own
68-
if err := s.Set(dir, cgroup); err != nil {
69-
return err
70-
}
7166
// because we are not using d.join we need to place the pid into the procs file
7267
// unlike the other subsystems
7368
if err := writeFile(dir, "cgroup.procs", strconv.Itoa(pid)); err != nil {

libcontainer/cgroups/fs/devices.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ func (s *DevicesGroup) Name() string {
1515
}
1616

1717
func (s *DevicesGroup) Apply(d *cgroupData) error {
18-
dir, err := d.join("devices")
18+
_, err := d.join("devices")
1919
if err != nil {
2020
// We will return error even it's `not found` error, devices
2121
// cgroup is hard requirement for container's security.
2222
return err
2323
}
24-
25-
if err := s.Set(dir, d.config); err != nil {
26-
return err
27-
}
28-
2924
return nil
3025
}
3126

libcontainer/cgroups/fs/freezer.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@ func (s *FreezerGroup) Name() string {
1919
}
2020

2121
func (s *FreezerGroup) Apply(d *cgroupData) error {
22-
dir, err := d.join("freezer")
22+
_, err := d.join("freezer")
2323
if err != nil && !cgroups.IsNotFound(err) {
2424
return err
2525
}
26-
27-
if err := s.Set(dir, d.config); err != nil {
28-
return err
29-
}
30-
3126
return nil
3227
}
3328

libcontainer/cgroups/fs/hugetlb.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@ func (s *HugetlbGroup) Name() string {
1919
}
2020

2121
func (s *HugetlbGroup) Apply(d *cgroupData) error {
22-
dir, err := d.join("hugetlb")
22+
_, err := d.join("hugetlb")
2323
if err != nil && !cgroups.IsNotFound(err) {
2424
return err
2525
}
26-
27-
if err := s.Set(dir, d.config); err != nil {
28-
return err
29-
}
30-
3126
return nil
3227
}
3328

libcontainer/cgroups/fs/memory.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
3232
return err
3333
}
3434
}
35-
3635
if err := s.Set(path, d.config); err != nil {
3736
return err
3837
}

libcontainer/cgroups/fs/net_cls.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ func (s *NetClsGroup) Name() string {
1515
}
1616

1717
func (s *NetClsGroup) Apply(d *cgroupData) error {
18-
dir, err := d.join("net_cls")
18+
_, err := d.join("net_cls")
1919
if err != nil && !cgroups.IsNotFound(err) {
2020
return err
2121
}
22-
23-
if err := s.Set(dir, d.config); err != nil {
24-
return err
25-
}
26-
2722
return nil
2823
}
2924

libcontainer/cgroups/fs/net_prio.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ func (s *NetPrioGroup) Name() string {
1515
}
1616

1717
func (s *NetPrioGroup) Apply(d *cgroupData) error {
18-
dir, err := d.join("net_prio")
18+
_, err := d.join("net_prio")
1919
if err != nil && !cgroups.IsNotFound(err) {
2020
return err
2121
}
22-
23-
if err := s.Set(dir, d.config); err != nil {
24-
return err
25-
}
26-
2722
return nil
2823
}
2924

0 commit comments

Comments
 (0)