@@ -6,12 +6,15 @@ import (
6
6
"errors"
7
7
"fmt"
8
8
"io"
9
+ "maps"
9
10
"net"
10
11
"os"
11
12
"os/exec"
13
+ "path"
12
14
"path/filepath"
13
15
"runtime"
14
16
"strconv"
17
+ "strings"
15
18
"sync"
16
19
"time"
17
20
@@ -153,7 +156,6 @@ func (p *containerProcess) wait() (*os.ProcessState, error) { //nolint:unparam
153
156
154
157
type setnsProcess struct {
155
158
containerProcess
156
- cgroupPaths map [string ]string
157
159
rootlessCgroups bool
158
160
intelRdtPath string
159
161
initProcessPid int
@@ -199,7 +201,20 @@ func (p *setnsProcess) setFinalCPUAffinity() error {
199
201
}
200
202
201
203
func (p * setnsProcess ) addIntoCgroupV1 () error {
202
- for _ , path := range p .cgroupPaths {
204
+ paths := maps .Clone (p .manager .GetPaths ())
205
+ for ctrl , sub := range p .process .SubCgroupPaths {
206
+ base , ok := paths [ctrl ]
207
+ if ! ok {
208
+ return fmt .Errorf ("unknown controller %s in SubCgroupPaths" , ctrl )
209
+ }
210
+ cgPath := path .Join (base , sub )
211
+ if ! strings .HasPrefix (cgPath , base ) {
212
+ return fmt .Errorf ("%s is not a sub cgroup path" , sub )
213
+ }
214
+ paths [ctrl ] = cgPath
215
+ }
216
+
217
+ for _ , path := range paths {
203
218
if err := cgroups .WriteCgroupProc (path , p .pid ()); err != nil && ! p .rootlessCgroups {
204
219
return fmt .Errorf ("error adding pid %d to cgroups: %w" , p .pid (), err )
205
220
}
@@ -209,19 +224,28 @@ func (p *setnsProcess) addIntoCgroupV1() error {
209
224
}
210
225
211
226
func (p * setnsProcess ) addIntoCgroupV2 () error {
212
- path := p .cgroupPaths ["" ]
213
- if err := cgroups .WriteCgroupProc (path , p .pid ()); err != nil && ! p .rootlessCgroups {
227
+ base := p .manager .Path ("" )
228
+ sub := ""
229
+ if p .process .SubCgroupPaths != nil {
230
+ sub = p .process .SubCgroupPaths ["" ]
231
+ }
232
+ cgPath := path .Join (base , sub )
233
+ if ! strings .HasPrefix (cgPath , base ) {
234
+ return fmt .Errorf ("%s is not a sub cgroup path" , sub )
235
+ }
236
+
237
+ if err := cgroups .WriteCgroupProc (cgPath , p .pid ()); err != nil && ! p .rootlessCgroups {
214
238
// On cgroup v2 + nesting + domain controllers, WriteCgroupProc may fail with EBUSY.
215
239
// https://github.com/opencontainers/runc/issues/2356#issuecomment-621277643
216
- // Try to join the cgroup of InitProcessPid.
217
- if p .initProcessPid != 0 {
240
+ // Try to join the cgroup of InitProcessPid, unless sub-cgroup is explicitly set .
241
+ if p .initProcessPid != 0 && sub == "" {
218
242
initProcCgroupFile := fmt .Sprintf ("/proc/%d/cgroup" , p .initProcessPid )
219
243
initCg , initCgErr := cgroups .ParseCgroupFile (initProcCgroupFile )
220
244
if initCgErr == nil {
221
245
if initCgPath , ok := initCg ["" ]; ok {
222
246
initCgDirpath := filepath .Join (fs2 .UnifiedMountpoint , initCgPath )
223
- logrus .Debugf ("adding pid %d to cgroups %v failed (%v), attempting to join %q (obtained from %s) " ,
224
- p .pid (), p . cgroupPaths , err , initCg , initCgDirpath )
247
+ logrus .Debugf ("adding pid %d to cgroup %s failed (%v), attempting to join %s " ,
248
+ p .pid (), cgPath , err , initCgDirpath )
225
249
// NOTE: initCgDirPath is not guaranteed to exist because we didn't pause the container.
226
250
err = cgroups .WriteCgroupProc (initCgDirpath , p .pid ())
227
251
}
0 commit comments