Skip to content

Support for cgroups#21

Merged
cpuguy83 merged 23 commits intocontainerd:mainfrom
cpuguy83:cgroups
Nov 18, 2022
Merged

Support for cgroups#21
cpuguy83 merged 23 commits intocontainerd:mainfrom
cpuguy83:cgroups

Conversation

@cpuguy83
Copy link
Copy Markdown
Member

This is a series of patches to add support for running wasm code in a cgroup.
While some cgroup controllers can support running different threads in different cgroups, the main one we are interested in, the cgroup memory controller, this doesn't make sense since threads all share memory.
Because of this we'll fork off a new process (which should have CoW version of the main shim's memory) to run the wasm code.

Of course all the issues of fork apply here since this is fairly unsafe from a multithreaded program (which the shim is always multi-threaded), so care must be taken to not try do things like take a lock in the new process because this can cause a deadlock.
There may be some interesting things to test out with the wasmtime engine just to make sure we aren't going to deadlock if there's multiple things happening when the fork occurs.

@cpuguy83 cpuguy83 marked this pull request as ready for review October 26, 2022 21:58
@cpuguy83 cpuguy83 force-pushed the cgroups branch 10 times, most recently from 863822c to 968c783 Compare October 27, 2022 23:26
@cpuguy83
Copy link
Copy Markdown
Member Author

cpuguy83 commented Oct 27, 2022

Recommend reviewing this with whitespace changes disabled.
image

Copy link
Copy Markdown
Contributor

@danbugs danbugs left a comment

Choose a reason for hiding this comment

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

~haven't finished reviewing the src/instance.rs, crates/containerd-shim-wasm/src/sandbox/exec.rs, and crates/containerd-shim-wasm/src/sandbox/oci.rs files just, but it's getting late, so figured I'd just post what I have so far. That is:

  • two suggestions (regarding dependencies versioning), and
  • a possible mistake in a CI action!

I'll probably review the rest during the weekend!

Copy link
Copy Markdown
Contributor

@danbugs danbugs left a comment

Choose a reason for hiding this comment

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

some improvements here and there~

P.S. After applying these, I suggest running cargo fmt --all

cgroups::new(p.as_ref().unwrap().to_str().unwrap().to_string())
}

pub fn setup_cgroup(cg: &Box<dyn cgroups::Cgroup>, spec: &Spec) -> Result<()> {
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.

Suggested change
pub fn setup_cgroup(cg: &Box<dyn cgroups::Cgroup>, spec: &Spec) -> Result<()> {
pub fn setup_cgroup(cg: &dyn cgroups::Cgroup, spec: &Spec) -> Result<()> {

~don't need Box if we are passing a reference, will suggest fixes on the function calls of this too.

src/instance.rs Outdated
*ec = Some((137, Utc::now()));
}
};
oci::setup_cgroup(&cg, &spec)?;
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.

Suggested change
oci::setup_cgroup(&cg, &spec)?;
oci::setup_cgroup(&*cg, &spec)?;

@@ -52,9 +55,6 @@ impl From<Error> for ttrpc::Error {
ttrpc::Error::RpcStatus(ttrpc::get_status(ttrpc::Code::FAILED_PRECONDITION, s))
}
Error::Oci(ref s) => match s {
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.

Suggested change
Error::Oci(ref s) => match s {
Error::Oci(ref s) => {

@danbugs
Copy link
Copy Markdown
Contributor

danbugs commented Nov 5, 2022

I'll come back to do a review of the functionality tomorrow~ lemme know if there's anything specific you'd like me to do locally to test the cgroups!

Copy link
Copy Markdown
Member

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

Great work, @cpuguy83 ! Left some comments on cgroups.rs file~

@Mossaka
Copy link
Copy Markdown
Member

Mossaka commented Nov 7, 2022

For some reason, when I retried to run a testwasm container, I got the following error

➜  runwasi git:(pr-21) ✗ sudo ctr run --rm --runtime=io.containerd.wasmtime.v1 docker.io/library/wasmtest:v1 testwasm wasm echo hello world 
ctr: Others("could not open cgroup file /default/testwasm/cpu.shares: No such file or directory (os error 2)"): unknown

Copy link
Copy Markdown
Member

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

exec.rs lgtm!

@cpuguy83 cpuguy83 force-pushed the cgroups branch 4 times, most recently from ee18818 to 929be45 Compare November 8, 2022 00:09
danbugs added a commit that referenced this pull request Nov 8, 2022
some more clippy-ing I noticed while reviewing #21
cpuguy83 and others added 17 commits November 15, 2022 23:12
Some of the tests really require the CAP_SYS_ADMIN capability and
behavior differently without it.
This is really what it is and the function makes more sense named this
way.
Also updated the doc for this since it was outdated already.
This allows us to return an object that will be properly closed when out
of scope as well as do more interaction with the process, such as
sending signals.
Since we're using fork now there is no need to spin up a new thread for
most of the module setup. We only need a new thread to wait on the wasi
function call to return to report the status.

We also store the pidfd and use that for sending signals to.
Now the shim just calls `kill -9` on the process running the module.
No more need for the deprecated feature from wasmtime.
This is better left to the caller, or perhaps some better integration
with the oci spec.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Co-authored-by: Dan Chiarlone <danilochiarlone@gmail.com>
Co-authored-by: Jiaxiao Zhou <duibao55328@gmail.com>
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Co-authored-by: Dan Chiarlone <danilochiarlone@gmail.com>
This just makes the intention of this function more obvious.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This makes the implementations a little easier to read and other changes
to make it much easier to test certain things.
This re-execs the test binary to run just the single test that requires
sudo.
This should just work in CI since there's no need to enter a password
there.
For local machines you'll just need to enter your sudo password to run
these tests.
This adds some missing controllers, adds constants for cgroup files, and
some minor cleanups.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
@cpuguy83
Copy link
Copy Markdown
Member Author

Updated:

  • Fixed issue with absolute group paths (e.g. /foo/bar) causing the base path of the group to be overwritten (along with tests)
  • Split cgroup impls into separate modules
  • Re-wrote cgroup detection logic to be more robust
  • Support for v1 cgroup controllers which do not share a common root
  • Support for more cgroupv1 controllers
  • Added more tests around group setup

This should have a follow-up to add CI for cgroupv2 to make sure those code paths are exercised.
This will require setting up nested virt so we have control over the kernel.

@cpuguy83 cpuguy83 requested review from Mossaka and danbugs November 16, 2022 02:20
Copy link
Copy Markdown
Member

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

lgtm!

@Mossaka
Copy link
Copy Markdown
Member

Mossaka commented Nov 18, 2022

Hey @cpuguy83, in our last meeting, I asked you feature parity between runwasi and runc. I remember you said there are features like namespaces and OCI hooks which are not implemented in runwasi. Do we plan to add them in the future? If yes, we could create issues for them.

@cpuguy83 cpuguy83 mentioned this pull request Nov 18, 2022
18 tasks
Copy link
Copy Markdown
Contributor

@danbugs danbugs left a comment

Choose a reason for hiding this comment

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

LGTM!

@cpuguy83
Copy link
Copy Markdown
Member Author

2 LGTM's, I'm gonna go ahead and merge.

@cpuguy83 cpuguy83 merged commit aa7fe9f into containerd:main Nov 18, 2022
rumpl pushed a commit to rumpl/runwasi that referenced this pull request Nov 23, 2022
…etch_cargo.io

fix 'Unable to update registry crates-io' issue
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.

3 participants