bytecodealliance / lucet

Lucet, the Sandboxing WebAssembly Compiler.
Apache License 2.0
4.06k stars 165 forks source link

Add userfaultfd-based Region backend #492

Closed tyler closed 4 years ago

tyler commented 4 years ago

This pull request integrates Fastly's userfaultfd based memory management backend as an alternate Region implementation.

Userfaultfd is a Linux-specific mechanism for handling page faults within userspace. (See https://www.kernel.org/doc/html/latest/admin-guide/mm/userfaultfd.html for a good technical explanation of it.) For Lucet, the use case is that when an Instance is started, none of the linear memory has to be copied initially.

We register the entire region with userfaultfd. When an instance is started, we set up the stacks, metadata, etc as normal, but we leave the pages of the linear memory "missing". When the instance starts and tries to access one of the linear memory pages, this triggers a page fault, as there is no physical memory backing the virtual memory. Since the region is registered with userfaultfd, this triggers a message to be sent to the userfaultfd handler thread. The handler thread determines which instance and module has faulted, and copies the necessary memory into place, before reawakening the instance thread.

At its core what this does is provide a way of reducing startup time and increasing flexibility in how memory is handled at the cost of increased runtime overhead (by was of context switches especially).

(All credit for this actually goes to @acfoltzer.)

acfoltzer commented 4 years ago

@tyler there are a few things I'd like to add to the public API surface and documentation to go with this. It would probably be easiest if I did that on a branch and then PRed into this, is that alright with you?

fst-crenshaw commented 4 years ago

Now that CircleCI is running mmap and userfaultfd tests, will we be able to merge this?

acfoltzer commented 4 years ago

Now that CircleCI is running mmap and userfaultfd tests, will we be able to merge this?

CircleCI experiments are currently only happening on branches, but I'm hoping to get a proper PR together today.

fst-crenshaw commented 4 years ago

So excited!

tyler commented 4 years ago

Ready for review :)

fst-crenshaw commented 4 years ago

The GitHub Actions tests are failing since they can't run the userfaultfd tests. I thought I'd try a little PR to have the offensive tests get skipped by GitHub Actions, since they are being run by CircleCI. See: https://github.com/bytecodealliance/lucet/pull/528