Skip to content

mpk: reenable MPK support with vendor string check - #7513

Merged
abrown merged 2 commits into
bytecodealliance:mainfrom
abrown:pku-intel-check
Nov 9, 2023
Merged

abrown merged 2 commits into
bytecodealliance:mainfrom
abrown:pku-intel-check

Conversation

@abrown

@abrown abrown commented Nov 9, 2023

Copy link
Copy Markdown
Member

In #7446 I disabled MPK support temporarily due to failures in CI runs. Looking into this further in #7445, I discovered that it is due to how has_cpuid_bit_set works on different x86 machines: Intel's CPUID instruction reports support for MPK in a certain leaf bit, AMD does it some other (unknown?) way. The CI problem boiled down to occasional runs on AMD machines that would fail with SIGILL because the AMD machine reported that it had MPK support when it really did not. This change fixes the issue by first checking if the CPU vendor string is GenuineIntel before inspecting the MPK CPUID leaf bit.

Closes #7445.

In bytecodealliance#7446 I disabled MPK support temporarily due to failures in CI runs.
Looking into this further in bytecodealliance#7445, I discovered that it is due to how
`has_cpuid_bit_set` works on different x86 machines: Intel's `CPUID`
instruction reports support for MPK in a certain leaf bit, AMD does it
some other (unknown?) way. The CI problem boiled down to occasional runs
on AMD machines that would fail with `SIGILL` because the AMD machine
reported that it had MPK support when it really did not. This change
fixes the issue by first checking if the CPU vendor string is
`GenuineIntel` before inspecting the MPK `CPUID` leaf bit.

Closes bytecodealliance#7445.
@abrown
abrown requested a review from a team as a code owner November 9, 2023 17:16
@abrown
abrown requested review from fitzgen and removed request for a team November 9, 2023 17:16
Comment thread crates/runtime/src/mpk/pkru.rs Resolved
@abrown
abrown enabled auto-merge November 9, 2023 18:27
@abrown abrown mentioned this pull request Nov 9, 2023
@abrown
abrown added this pull request to the merge queue Nov 9, 2023
Merged via the queue into bytecodealliance:main with commit b3da3c3 Nov 9, 2023
@abrown
abrown deleted the pku-intel-check branch November 9, 2023 19:42
V0ldek added a commit to V0ldek/wasmtime that referenced this pull request Oct 23, 2026
The check for `mpk::is_supported` explicitly checked for GenuineIntel,
and then for the PKU bit on the cpuid message. This is misguided for two
reasons:

1. Zen3 AMD also supports MPKs but wasmtime misses out on using them;
2. the OSPKE bit also has to be checked to actually be correct.

Number 1. seems to have been done as an attempt to avoid CI crashes.
In bytecodealliance#7445, bytecodealliance#7446 MPK was disabled and then reenabled for Intel only in
PR bytecodealliance#7513. I believe that was actually a red herring and those crashes
were caused by the second point.

PKU only says if the CPU supports MPKs on the hardware level. However,
the OS still needs to enable PKE. If it is not enabled, RDPKRU/WRPKRU
raise #UD, which kills the process with SIGILL. An example is a VM that
runs on a modern CPU as the hardware, but whose hypervisor has PKU
disabled. This would actually fault on *any* vendor, Intel included.
You can test it today on the old version by running wasmtime in QEMU
with nopku passed (`-append "nopku"`), it will crash with the pooling
allocator when it tries to set up PKs.

In this commit we switch to checking both PKE and OSPKE, removing the
vendor check. This is confirmed to work on modern AMD and Intel, and
correctly recognises MPK when ran under QEMU:
- with `-cpu host` and default kernel settings (supported),
- with `-cpu host` but `nopku` in the kernel (unsupported),
- with `-cpu host,-pku` (unsupported).

We now have a test in tests/mpk.rs to ensure the result
of the check agrees with actual capabilities the process has.

Additionally, removed a stale test in `pkru::tests::check_read`,
it was ignored since forever and the new mpk test supersedes it.
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.

mpk: some GitHub CI runners have MPK enabled but fail to run MPK code

2 participants