Skip to content

Fix/cgroup2 bind mount shared cgroupns - #5267

Open
xujihui1985 wants to merge 1 commit into
opencontainers:mainfrom
xujihui1985:fix/cgroup2-bind-mount-shared-cgroupns
Open

xujihui1985 wants to merge 1 commit into
opencontainers:mainfrom
xujihui1985:fix/cgroup2-bind-mount-shared-cgroupns

Conversation

@xujihui1985

Copy link
Copy Markdown
Contributor

When cgroup namespaces is host, mounting a new cgroup2 fs instance for /sys/fs/cgroup can affect the host-visible cgroupfs mount state, including options such as nsdelegate.

Avoid that by preferring a bind mount of the existing cgroup v2 hierarchy when cgroupns is host. Keep the existing cgroup2 mount-first logic for private cgroup namespaces, including the EPERM/EBUSY fallback to a bind mount and the rootless ENOENT masking behavior.

this PR fix issue #5258

@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch 3 times, most recently from 9a08cc5 to 922bd42 Compare May 3, 2026 07:21
@xujihui1985

Copy link
Copy Markdown
Contributor Author

@lifubang Hi fubang, This PR fixes #5258. I’ve checked the failing CI and it doesn’t seem related to these changes. Please review it when you have time.

@lifubang
lifubang force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from 5c685ee to 0b7a122 Compare May 18, 2026 14:36

@lifubang lifubang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

Comment thread tests/integration/mounts.bats Resolved
@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch 2 times, most recently from 0b7a122 to 9653c5c Compare May 30, 2026 12:41
@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch 2 times, most recently from 1360929 to 491bf97 Compare May 31, 2026 00:41
@xujihui1985

Copy link
Copy Markdown
Contributor Author

Hi @kolyshkin would you like to move forward with this PR?

@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from 491bf97 to c8ed922 Compare June 9, 2026 03:23
Comment thread tests/integration/mounts.bats Resolved
Comment thread tests/integration/mounts.bats Resolved
Comment thread tests/integration/checkpoint.bats Resolved

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to submit pending review comments, my bad.

Overall it's almost ready.

Comment thread tests/integration/mounts.bats Resolved
@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch 4 times, most recently from 3786615 to 3d9d5a7 Compare June 12, 2026 05:52
@xujihui1985

Copy link
Copy Markdown
Contributor Author

@lifubang @kolyshkin I have moved the checkpoint-related commit into a new PR: #5318, and fixed the other review suggestions.

Comment thread libcontainer/rootfs_linux.go Resolved
Comment thread tests/integration/mounts.bats Resolved

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a few comments

@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch 3 times, most recently from 6c3174f to 4e14d47 Compare July 3, 2026 03:07
Comment thread tests/integration/mounts.bats Resolved
@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from 4e14d47 to e8edba3 Compare July 3, 2026 06:08
@xujihui1985

Copy link
Copy Markdown
Contributor Author

@lifubang @kolyshkin I think I have fixed all the issue listed above, anything I can improve here?

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit out of scope for this PR, but I think we should test that the host mounts are not changed after tests (or after each test) as, in general, runc should not change host side mounts (with some exceptions).

@xujihui1985

xujihui1985 commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor Author

This is a bit out of scope for this PR, but I think we should test that the host mounts are not changed after tests (or after each test) as, in general, runc should not change host side mounts (with some exceptions).

@kolyshkin yes, the second commit is suppose to do this check e8edba3 so do you think we should run this check for each test?

@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from e8edba3 to f5442f7 Compare August 9, 2026 02:19
@xujihui1985

Copy link
Copy Markdown
Contributor Author

@lifubang @kolyshkin Hi everyone, just checking in on this PR since it's been pending for a while. Let me know if there's anything else I need to improve, or if we're good to move forward

@xujihui1985
xujihui1985 requested a review from lifubang August 21, 2026 10:39
@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from f5442f7 to 387008e Compare August 21, 2026 10:39
@xujihui1985
xujihui1985 requested a review from kolyshkin August 21, 2026 10:41
@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from 387008e to f29c93a Compare October 2, 2026 03:05
@xujihui1985

Copy link
Copy Markdown
Contributor Author

@lifubang @kolyshkin Hi, since it has been a long time since last review, I would like to know if there is anything I need to improve on this PR.

@kolyshkin kolyshkin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this fell off my radar somehow.

Part of second commit (that changes the rootfs_linux.go) should be in the first one. Yet better, just squash the commits.

Otherwise LGTM

@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from f29c93a to 13816cc Compare October 2, 2026 05:17
@xujihui1985

Copy link
Copy Markdown
Contributor Author

Sorry, this fell off my radar somehow.

Part of second commit (that changes the rootfs_linux.go) should be in the first one. Yet better, just squash the commits.

Otherwise LGTM

@kolyshkin no worry, thanks for your reply, I have update the PR squash the commits into one

@kolyshkin

Copy link
Copy Markdown
Contributor

@lifubang PTAL

@kolyshkin
kolyshkin force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch 2 times, most recently from 8373deb to 671312e Compare October 5, 2026 04:22
@xujihui1985

Copy link
Copy Markdown
Contributor Author

ping @lifubang

@kolyshkin
kolyshkin requested review from AkihiroSuda and rata and a balanced review from Copilot November 10, 2026 08:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Explicit host cgroup-namespace joins can still alter the host’s global cgroup2 options, and the regression test may miss these changes because it reads an unreliable mountinfo field.

2 open issues
What changed in this PR

This PR addresses #5258 by using the existing cgroup v2 hierarchy when no cgroup namespace is configured, avoiding fresh mounts that can change host-wide options such as nsdelegate.

Changes:

  • Uses a private bind mount for the no-cgroup-namespace path.
  • Preserves mount-first behavior and existing fallback handling for configured cgroup namespaces.
  • Adds a regression assertion that host cgroup mount options remain unchanged.
File Description
tests/​integration/​mounts.bats Compares host cgroup mount options before and after container execution.
libcontainer/​rootfs_linux.go Selects bind mounting when no cgroup namespace is configured and disables host-side propagation.

Review effort: Balanced

})
if err == nil || (!errors.Is(err, unix.EPERM) && !errors.Is(err, unix.EBUSY)) {
return err
if c.cgroupns {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is paranoid, if you insist this change, I will change

Comment thread tests/integration/mounts.bats Outdated
}

function get_cgroup_mount_flags() {
awk '$5 == "/sys/fs/cgroup" {print $10; exit}' /proc/self/mountinfo

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xujihui1985 you marked this as resolved but probably forgot to git push?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, my bad

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noting the issue is still not fixed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kolyshkin fixed in latest commit

Comment thread tests/integration/mounts.bats Outdated
}

function get_cgroup_mount_flags() {
awk '$5 == "/sys/fs/cgroup" {print $10; exit}' /proc/self/mountinfo

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noting the issue is still not fixed

@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from 671312e to dc9d957 Compare November 11, 2026 14:28
@kolyshkin
kolyshkin force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from dc9d957 to 55bfccf Compare November 11, 2026 22:04
@kolyshkin

Copy link
Copy Markdown
Contributor

rebased to fix binary-size job

When cgroup namespaces is host, mounting a new cgroup2 fs instance for
/sys/fs/cgroup can affect the host-visible cgroupfs mount state, including
options such as nsdelegate.

Avoid that by preferring a bind mount of the existing cgroup v2 hierarchy
when cgroupns is host. Keep the existing cgroup2 mount-first logic
for private cgroup namespaces, including the EPERM/EBUSY fallback to a
bind mount and the rootless ENOENT masking behavior.

Signed-off-by: sean <xujihui1985@gmail.com>
@xujihui1985
xujihui1985 force-pushed the fix/cgroup2-bind-mount-shared-cgroupns branch from 55bfccf to 10ff8ad Compare November 12, 2026 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants