-
Notifications
You must be signed in to change notification settings - Fork 2.2k
cgroupv2: fix fs2 driver initialization #2299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
353e917
cgroups/fs2: do not use securejoin
kolyshkin 480bca9
cgroups/fs2: move type decl to beginning
kolyshkin 9c80cd6
cgroupv2: rm legacy Paths from systemd driver
kolyshkin 88c13c0
cgroupv2: use SecureJoin in systemd driver
kolyshkin dbeff89
cgroupv2/systemd: privatize UnifiedManager
kolyshkin 60eaed2
cgroupv2: move sanity path check to common code
kolyshkin 813cb3e
cgroupv2: fix fs2 cgroup init
kolyshkin b5c1949
cgroups/fs2/CreateCgroupPath: reinstate check
kolyshkin de11341
cgroups/fs2/CreateCgroupPath: nit
kolyshkin bb47e35
cgroup/systemd: reorganize
kolyshkin 4b4bc99
CreateCgroupPath: only enable needed controllers
kolyshkin 992d5ca
travis: enable fs2 driver test on fedora
kolyshkin ab276b1
cgroups/fs2/Destroy: use Remove, ignore ENOENT
kolyshkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package fs2 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/opencontainers/runc/libcontainer/configs" | ||
| ) | ||
|
|
||
| // neededControllers returns the string to write to cgroup.subtree_control, | ||
| // containing the list of controllers to enable (for example, "+cpu +pids"), | ||
| // based on (1) controllers available and (2) resources that are being set. | ||
| // | ||
| // The resulting string does not include "pseudo" controllers such as | ||
| // "freezer" and "devices". | ||
| func neededControllers(cgroup *configs.Cgroup) ([]string, error) { | ||
| var list []string | ||
|
|
||
| if cgroup == nil { | ||
| return list, nil | ||
| } | ||
|
|
||
| // list of all available controllers | ||
| const file = UnifiedMountpoint + "/cgroup.controllers" | ||
AkihiroSuda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| content, err := ioutil.ReadFile(file) | ||
| if err != nil { | ||
| return list, err | ||
| } | ||
| avail := make(map[string]struct{}) | ||
| for _, ctr := range strings.Fields(string(content)) { | ||
| avail[ctr] = struct{}{} | ||
| } | ||
|
|
||
| // add the controller if available | ||
| add := func(controller string) { | ||
| if _, ok := avail[controller]; ok { | ||
| list = append(list, "+"+controller) | ||
| } | ||
| } | ||
|
|
||
| if isPidsSet(cgroup) { | ||
| add("pids") | ||
| } | ||
| if isMemorySet(cgroup) { | ||
| add("memory") | ||
| } | ||
| if isIoSet(cgroup) { | ||
| add("io") | ||
| } | ||
| if isCpuSet(cgroup) { | ||
| add("cpu") | ||
| } | ||
| if isCpusetSet(cgroup) { | ||
| add("cpuset") | ||
| } | ||
| if isHugeTlbSet(cgroup) { | ||
| add("hugetlb") | ||
| } | ||
|
|
||
| return list, nil | ||
| } | ||
|
|
||
| // CreateCgroupPath creates cgroupv2 path, enabling all the | ||
| // needed controllers in the process. | ||
| func CreateCgroupPath(path string, c *configs.Cgroup) (Err error) { | ||
| if !strings.HasPrefix(path, UnifiedMountpoint) { | ||
| return fmt.Errorf("invalid cgroup path %s", path) | ||
| } | ||
|
|
||
| ctrs, err := neededControllers(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| allCtrs := strings.Join(ctrs, " ") | ||
|
|
||
| elements := strings.Split(path, "/") | ||
| elements = elements[3:] | ||
| current := "/sys/fs" | ||
| for i, e := range elements { | ||
| current = filepath.Join(current, e) | ||
| if i > 0 { | ||
| if err := os.Mkdir(current, 0755); err != nil { | ||
| if !os.IsExist(err) { | ||
| return err | ||
| } | ||
| } else { | ||
| // If the directory was created, be sure it is not left around on errors. | ||
| current := current | ||
| defer func() { | ||
| if Err != nil { | ||
| os.Remove(current) | ||
| } | ||
| }() | ||
| } | ||
| } | ||
| // enable needed controllers | ||
| if i < len(elements)-1 { | ||
| file := filepath.Join(current, "cgroup.subtree_control") | ||
| if err := ioutil.WriteFile(file, []byte(allCtrs), 0755); err != nil { | ||
| // XXX: we can enable _some_ controllers doing it one-by one | ||
| // instead of erroring out -- does it makes sense to do so? | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure whether this validation is useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I added it is peculiar -- if someone will be modifying
setCpu(), adding more fields to set, they will be reminded to also modifyisCpuSet(). Having this check here might also help to uncover bugs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E.g. someone adds
cpuFoobarsetting to setCpu(), but forgets to modifyisCpuSet(). They add a unit test to check ifcpuFoobaris applied, and the test fails! Hope it makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be just code comment lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? Add a comment like "if you're modifying this, don't forget to modify isSetCpu"? In my experience, this never works. The added runtime check might actually help to uncover such a bug.
Or do you mean adding a comment stating the reason why this check is added?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have amended the commit telling why
is*Setis added toset*:Amend all setFoo() functions to call isFooSet(). While this might
seem unnecessary, it might actually help to uncover a bug.
Imagine someone:
In this case, a test case modifying CpuFoo will help
to uncover the BUG. This is the reason why it's added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the former, but not a blocker