Skip to content

cgroupfs: drop registry entries on filesystem release - #14946

Merged
copybara-service[bot] merged 2 commits into
masterfrom
test/cl986971688
Sep 25, 2026
Merged

copybara-service[bot] merged 2 commits into
masterfrom
test/cl986971688

Conversation

@copybara-service

Copy link
Copy Markdown

cgroupfs: drop registry entries on filesystem release

Summary

Follow-up to commit 26ef5174081be0b4b1f750a97e75ee6bad5d5a53 (cgroupfs: remove cgroup from CgroupRegistry on rmdir). Walk the cgroupfs dentry subtree in (*filesystem).Release and call CgroupRegistry.RemoveCgroup for every cgroupInode.id in the tree.

Why

The rmdir fix covers cgroups removed via rmdir(2). Two release paths still leak entries:

  1. FilesystemType.GetFilesystem error window at cgroupfs.go:376-398. newCgroupInode at base.go:190 calls r.AddCgroup for the root cgroup before prepareInitialCgroup and r.Register run. On either failure the code does rootD.DecRef(ctx); fs.VFSFilesystem().DecRef(ctx). Release then runs but currently skips ReleaseCgroupHierarchy and Unregister because fs.hierarchyID is still InvalidCgroupHierarchyID. The root cgroup id stays in CgroupRegistry.cgroups forever. Repeated mount failures accumulate unbounded entries that get serialized on every checkpoint, same class as the rmdir leak.

  2. prepareInitialCgroup at cgroupfs.go:440-447 creates intermediate cgroup directories via newDirWithOwner, each of which calls AddCgroup. If the path walk later fails, the partially created children leak.

Approach

Add a helper removeCgroupsFromRegistry that walks fs.root via the existing dir.forEachChildDir and calls RemoveCgroup for each cgroupInode.id. RemoveCgroup is documented as a no-op for ids that are not present (cgroup.go:667-669), so cgroups already removed by RmDir cost nothing. RmDir stays the primary owner of the live-cgroup case; this walk catches entries left at release.

Test

  • Earlier revision: bazel test //pkg/sentry/fsimpl/cgroupfs/... //test/syscalls:cgroup_test
  • Current revision: git diff --check upstream/master...HEAD. Bazel was unavailable in this environment, so the tests were not rerun after the rebase.

CLA

Signed via individual Google CLA on sactransport2000@gmail.com.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#13215 from ibondarenko1:cgroupfs-release-removecgroup b8d03ce

Commit 26ef517 added
CgroupRegistry.RemoveCgroup and called it from dir.RmDir so that
destroyed cgroup directories do not stay in
kernel.CgroupRegistry.cgroups across save/restore. That fix covers
rmdir but two release paths still leak:

1. GetFilesystem error window. newCgroupInode at base.go:190 calls
   r.AddCgroup for the root cgroup before prepareInitialCgroup and
   r.Register run. On either failure the code calls rootD.DecRef and
   fs.VFSFilesystem().DecRef. Release then runs but currently skips
   ReleaseCgroupHierarchy and Unregister because fs.hierarchyID is
   still InvalidCgroupHierarchyID. The root cgroup id stays in the
   registry forever. Repeated mount failures accumulate unbounded
   entries.

2. prepareInitialCgroup creates intermediate cgroup directories via
   newDirWithOwner, each of which calls AddCgroup. If
   prepareInitialCgroup fails partway, the same residue applies.

Walk fs.root in Release and call RemoveCgroup for each cgroupInode in
the subtree. RemoveCgroup is documented as a no-op for ids not in the
map (cgroup.go:667-669), so cgroups that RmDir already removed cost
nothing and the rmdir path stays the primary owner.

Signed-off-by: Ievgen Bondarenko <sactransport2000@gmail.com>
@copybara-service copybara-service Bot added the exported Issue was exported automatically label Sep 23, 2026
@copybara-service
copybara-service Bot force-pushed the test/cl986971688 branch 5 times, most recently from 5a6b778 to 2d0ced4 Compare September 25, 2026 21:39
@copybara-service
copybara-service Bot merged commit bc2e4d8 into master Sep 25, 2026
1 of 3 checks passed
@copybara-service
copybara-service Bot deleted the test/cl986971688 branch September 25, 2026 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

exported Issue was exported automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants