🐛 Refuse a Linux host below 7.2 for a VM on our GPU
A VM on reims-vgpu's GPU now needs Linux 7.2 or later on a Linux host, under any accelerator, where KVM's VMs needed 6.13: reims-ctl's start check refuses an older kernel, naming the running one and "Linux 7.2 or later", and so does the reims-graphics device's realize in QEMU (vendor/qemu 458f4b7b). Before 7.2 a udmabuf over large pages is malformed, and an AMD GPU's import binds the wrong pages of guest RAM (issue 185); 7.2 builds the table from the pages themselves (upstream 5bf888673e0d, read from v7.2's drivers/dma-buf/udmabuf.c). The floor holds KVM's 6.13 reason too. MIN_KVM_KERNEL is MIN_LINUX, and doctor's kernel line shows on every Linux host. The floor takes the place of the below-7.2 form: QEMU no longer advises guest RAM, the device's region or its pool pieces MADV_NOHUGEPAGE, nor logs the 4 KiB line, and the library's views of guest RAM take no advice (off_huge_pages, huge_pages_take and linux_release_at_least go, with their tests). On 7.2 and later nothing changes: QEMU's advice and its collapse with its share line stay. What hosts see: below 7.2 a start is refused, where it ran with guest RAM on 4 KiB pages; on 7.2 and later the VM starts as before. README, docs/qemu.md, docs/graphics-kext/design.md and docs/lessons.md say so.
This commit is contained in:
1 parent
57495f2c77
commit
699d299e3c
9 files changed
+100
-272
No files matched your search
@@ -31,9 +31,9 @@ x86-64 macOS needs an x86-64 Linux host or an x86-64 Windows PC. Our drivers loa
|
||||
and later for now; we've tested macOS 26 on an RTX 5080 laptop GPU and a Radeon 780M on Linux, and
|
||||
on an Intel UHD 630 on Windows 11.
|
||||
|
||||
On Linux, a VM on our virtual GPU needs Linux 6.13 or later. An older kernel's KVM can't map the
|
||||
GPU's memory into the guest, so the VM hangs at boot. `reims-ctl` won't start one on an older
|
||||
kernel, and `reims-ctl doctor` shows the kernel it found.
|
||||
On Linux, a VM on our virtual GPU needs Linux 7.2 or later. Before 7.2, the kernel hands an AMD
|
||||
GPU the wrong pages of the guest's memory, which can overwrite it. `reims-ctl` won't start one on
|
||||
an older kernel, and `reims-ctl doctor` shows the kernel it found.
|
||||
|
||||
arm64 macOS 14 to 27 runs on QEMU too, with Ambrosia, our own arm64 firmware, and the same virtual
|
||||
GPU. On an Apple silicon Mac it runs under HVF, macOS's hypervisor, and on arm64 Linux under KVM.
|
||||
@@ -247,7 +247,7 @@ it needs itself; working on the code needs the following.
|
||||
### Requirements
|
||||
|
||||
- A Linux host for the library and for VMs (on x86-64, x86-64 VMs under KVM and arm64 VMs under
|
||||
TCG; on arm64, arm64 VMs under KVM; KVM's VMs need Linux 6.13 or later), and an Apple silicon Mac for the metallibs and the guest's
|
||||
TCG; on arm64, arm64 VMs under KVM; VMs on our GPU need Linux 7.2 or later), and an Apple silicon Mac for the metallibs and the guest's
|
||||
kexts, where arm64 VMs run under HVF. Every Rust binary built on the Mac is arm64
|
||||
(`aarch64-apple-darwin`): rustc's arm64e target has no pointer authentication, and its binaries
|
||||
crash before `main` (`rust-toolchain.toml`).
|
||||
|
||||
@@ -1874,11 +1874,11 @@ fn doctor(host: Host) -> Result<i32> {
|
||||
),
|
||||
),
|
||||
}
|
||||
// KVM and the library's needs, for the x86-64 VMs Linux runs; KVM alone for the arm64 VMs
|
||||
// arm64 Linux runs.
|
||||
// The kernel a VM on reims-vgpu's GPU needs, under any accelerator; then KVM and the
|
||||
// library's needs, for the x86-64 VMs Linux runs; KVM alone for the arm64 VMs arm64 Linux
|
||||
// runs.
|
||||
let vms = registered_vms();
|
||||
if host.os == crate::host::Os::Linux
|
||||
&& profile.accel(host) == "kvm"
|
||||
&& let Some(v) = runtime::host_kernel()
|
||||
{
|
||||
let problem = runtime::kernel_problem(v);
|
||||
|
||||
@@ -132,43 +132,46 @@ pub fn preflight(vm: &Vm, host: Host) -> Result<()> {
|
||||
yourself to the kvm group (sudo usermod -aG kvm $USER, then log in again)"
|
||||
);
|
||||
}
|
||||
if c.graphics() == Graphics::ReimsVgpu
|
||||
&& let Some(problem) = host_kernel().and_then(kernel_problem)
|
||||
{
|
||||
bail!("{problem}");
|
||||
}
|
||||
}
|
||||
if host.os == Os::Linux
|
||||
&& c.graphics() == Graphics::ReimsVgpu
|
||||
&& let Some(problem) = host_kernel().and_then(kernel_problem)
|
||||
{
|
||||
bail!("{problem}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The oldest Linux whose KVM runs a VM on reims-vgpu's GPU. The guest's Shared memory is GPU
|
||||
/// memory the host's driver allocated, which QEMU maps into the guest. Before 6.13, KVM maps a
|
||||
/// page of a driver's mapping only where it can take a reference on the page, and the pages the
|
||||
/// drivers on TTM (amdgpu among them) allocate in higher orders have none: the guest's first
|
||||
/// write to them stops its vCPU (QEMU logs "kvm run failed Bad address"), and the VM hangs at
|
||||
/// boot. Read from Linux 6.12's `hva_to_pfn_remapped` and `ttm_pool_alloc_page`.
|
||||
pub const MIN_KVM_KERNEL: (u32, u32) = (6, 13);
|
||||
/// The oldest Linux that runs a VM on reims-vgpu's GPU, under any accelerator; QEMU's device
|
||||
/// refuses an older one at its realize too. The guest's pages reach the host's GPU as udmabuf
|
||||
/// dma-bufs of its RAM's memfd, which QEMU puts on huge pages, and before 7.2 a udmabuf over large
|
||||
/// folios has a malformed table, which an AMD GPU's import binds to the wrong pages (issue 185;
|
||||
/// read from Linux 7.1's and 7.2's `drivers/dma-buf/udmabuf.c`, upstream 5bf888673e0d). It holds
|
||||
/// KVM's own floor too: before 6.13 KVM can't map the pages TTM's drivers (amdgpu among them)
|
||||
/// allocate in higher orders, which back the guest's Shared memory (Linux 6.12's
|
||||
/// `hva_to_pfn_remapped`).
|
||||
pub const MIN_LINUX: (u32, u32) = (7, 2);
|
||||
|
||||
/// The running kernel's version, major and minor (Linux).
|
||||
pub fn host_kernel() -> Option<(u32, u32)> {
|
||||
kernel_version(&std::fs::read_to_string("/proc/sys/kernel/osrelease").ok()?)
|
||||
}
|
||||
|
||||
/// The major and minor version a kernel's release names: 6.12 for `6.12.117+deb13-amd64`.
|
||||
/// The major and minor version a kernel's release names: 7.1 for `7.1.9-arch1-1`.
|
||||
fn kernel_version(release: &str) -> Option<(u32, u32)> {
|
||||
let mut v = release.trim().split(|c: char| !c.is_ascii_digit());
|
||||
Some((v.next()?.parse().ok()?, v.next()?.parse().ok()?))
|
||||
}
|
||||
|
||||
/// Why a kernel of version `v` runs no VM on reims-vgpu's GPU under KVM, if it does not
|
||||
/// ([`MIN_KVM_KERNEL`]).
|
||||
/// Why a kernel of version `v` runs no VM on reims-vgpu's GPU, if it does not ([`MIN_LINUX`]).
|
||||
pub fn kernel_problem(v: (u32, u32)) -> Option<String> {
|
||||
(v < MIN_KVM_KERNEL).then(|| {
|
||||
(v < MIN_LINUX).then(|| {
|
||||
let min = format!("{}.{}", MIN_LINUX.0, MIN_LINUX.1);
|
||||
format!(
|
||||
"this host runs Linux {}.{}, and a VM on reims-vgpu's GPU needs Linux {}.{} or later: \
|
||||
an older kernel's KVM can't map the GPU's memory into the guest, so the VM hangs at \
|
||||
boot. Install a newer kernel, then start the VM again",
|
||||
v.0, v.1, MIN_KVM_KERNEL.0, MIN_KVM_KERNEL.1
|
||||
"this host runs Linux {}.{}, and a VM on reims-vgpu's GPU needs Linux {min} or later: \
|
||||
before {min} a udmabuf over large pages is malformed, and an AMD GPU is bound to the \
|
||||
wrong pages of the guest's memory. Install a newer kernel, then start the VM again",
|
||||
v.0, v.1
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -1326,23 +1329,26 @@ impl Drop for TestQemu {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A start under KVM on reims-vgpu's GPU asks for Linux 6.13 or later: Debian 13's 6.12 is
|
||||
/// refused with the reason and what to do, and 6.13 and every later release start.
|
||||
/// A start on reims-vgpu's GPU asks for Linux 7.2 or later: 7.1 is refused with the reason
|
||||
/// and what to do, as are Debian 13's 6.12 and the 6.18 line, and 7.2 and every later release
|
||||
/// start.
|
||||
#[test]
|
||||
fn a_kvm_start_on_our_gpu_asks_for_linux_6_13() {
|
||||
fn a_start_on_our_gpu_asks_for_linux_7_2() {
|
||||
assert_eq!(kernel_version("6.12.117+deb13-amd64\n"), Some((6, 12)));
|
||||
assert_eq!(kernel_version("7.1.9-arch1-1"), Some((7, 1)));
|
||||
assert_eq!(kernel_version("7.3.3-1-cachyos"), Some((7, 3)));
|
||||
assert_eq!(kernel_version("6.13"), Some((6, 13)));
|
||||
assert_eq!(kernel_version("7.2"), Some((7, 2)));
|
||||
assert_eq!(kernel_version("garbage"), None);
|
||||
let m = kernel_problem((6, 12)).unwrap();
|
||||
let m = kernel_problem((7, 1)).unwrap();
|
||||
assert!(
|
||||
m.contains("Linux 6.12") && m.contains("Linux 6.13 or later") && m.contains("hangs"),
|
||||
m.contains("Linux 7.1,") && m.contains("Linux 7.2 or later") && m.contains("udmabuf"),
|
||||
"{m}"
|
||||
);
|
||||
assert!(kernel_problem((5, 19)).is_some());
|
||||
assert!(kernel_problem((6, 13)).is_none());
|
||||
assert!(kernel_problem((6, 18)).is_none());
|
||||
assert!(kernel_problem((7, 0)).is_none());
|
||||
assert!(kernel_problem((6, 12)).is_some());
|
||||
assert!(kernel_problem((6, 18)).is_some());
|
||||
assert!(kernel_problem((7, 2)).is_none());
|
||||
assert!(kernel_problem((7, 3)).is_none());
|
||||
assert!(kernel_problem((8, 0)).is_none());
|
||||
}
|
||||
|
||||
/// A stop waits for QEMU to end after `quit` about twice the longest it was measured to take
|
||||
|
||||
@@ -50,18 +50,17 @@
|
||||
//! pages by pointer did (3.1), against 5.3 over 4 KiB pages.
|
||||
//!
|
||||
//! So a dma-buf of huge pages helps the RX 7900 XTX as the pages themselves would, and the Radeon
|
||||
//! 780M only within a window of a few MiB. On Linux 7.2 and later QEMU puts guest RAM's memfd on
|
||||
//! huge pages (its advice and a collapse after the prealloc, `vendor/qemu/backends/hostmem.c`);
|
||||
//! below 7.2 its pages are base pages: QEMU advises the RAM's mapping `MADV_NOHUGEPAGE`
|
||||
//! (`vendor/qemu/system/physmem.c`, `ram_block_add`), and the library each mapping of a file that
|
||||
//! could hold a huge page (`super::vm`'s `off_huge_pages`). Linux 6.11 to 7.1 build a udmabuf's
|
||||
//! table with an entry a base page that names a large folio's head page and the page's offset in
|
||||
//! it, past `PAGE_SIZE` (`sg_set_folio`, `get_sg_table`; fixed in 7.2 by 5bf888673e0d, read from
|
||||
//! code), and an importer that walks the table by DMA page counts such an entry as its offset and
|
||||
//! length: amdgpu's import (`drm_prime_sg_to_dma_addr_array`, under dma-direct, as an AMD
|
||||
//! integrated GPU runs by default) binds an entry at a folio's page k to k + 1 pages, so the
|
||||
//! buffer's pages after the first lie on other pages, past the folio's end for k of 256 or more. A
|
||||
//! macOS guest's kernel memory was overwritten so on a Radeon 890M with Linux 6.18.59 (issue 185).
|
||||
//! 780M only within a window of a few MiB. QEMU puts guest RAM's memfd on huge pages (its advice
|
||||
//! and a collapse after the prealloc, `vendor/qemu/backends/hostmem.c`), and a udmabuf of them
|
||||
//! binds right on the Linux a VM on the device needs, 7.2 or later (reims-ctl's start check and
|
||||
//! the device's realize refuse an older one): 7.2 builds a udmabuf's table from its pages
|
||||
//! themselves (`sg_alloc_table_from_pages` in `get_sg_table`, upstream 5bf888673e0d, read from
|
||||
//! code). Linux 6.11 to 7.1 built it with an entry a base page that names a large folio's head
|
||||
//! page and the page's offset in it, past `PAGE_SIZE` (`sg_set_folio`), and amdgpu's import
|
||||
//! (`drm_prime_sg_to_dma_addr_array`, under dma-direct, as an AMD integrated GPU runs by default)
|
||||
//! binds an entry at a folio's page k to k + 1 pages, so the buffer's pages after the first lie
|
||||
//! on other pages: a macOS guest's kernel memory was overwritten so on a Radeon 890M with Linux
|
||||
//! 6.18.59 (issue 185).
|
||||
//!
|
||||
//! What udmabuf takes besides ([`Refusal`]): memfds sealed against shrinking (`F_SEAL_SHRINK`,
|
||||
//! which QEMU's `seal=on`, its default, sets, and [`super::vm::memfd`] sets on the library's own),
|
||||
|
||||
@@ -118,7 +118,7 @@
|
||||
//! memory is, counted alike, naming the open-file limit.
|
||||
|
||||
use std::os::fd::{AsRawFd as _, FromRawFd as _, OwnedFd};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::core::num::{ToU64, ToUsize};
|
||||
use crate::core::platform::os::vm::{HostAddr, HostVm, KernReturn, VmProt};
|
||||
@@ -493,87 +493,7 @@ pub(super) fn file(
|
||||
if mapped == libc::MAP_FAILED {
|
||||
return Err(refused("a view of guest pages", false));
|
||||
}
|
||||
let first = mapped.addr().to_u64();
|
||||
off_huge_pages(first, length, backing.offset);
|
||||
Ok(first)
|
||||
}
|
||||
|
||||
/// The size a huge page of a file's pages is aligned to in a mapping on a host of 4 KiB pages,
|
||||
/// whose multiples the larger pages of other hosts are: 2 MiB.
|
||||
const HUGE_ALIGN: u64 = 2 << 20;
|
||||
|
||||
/// Advises `length` bytes from `at`, a shared mapping just made of a file from byte `offset`,
|
||||
/// `MADV_NOHUGEPAGE` where its address and the offset agree modulo [`HUGE_ALIGN`]: only there
|
||||
/// can a fault through the mapping back the file's pages with a huge folio, or khugepaged
|
||||
/// collapse them into one through it (`thp_vma_suitable_order`, Linux 6.18 and 7.3, read from
|
||||
/// code), as `shmem_enabled` at `always` or `within_size` lets both with no advice. Mappings
|
||||
/// that follow on in one file agree alike, so they get the same advice, and the kernel still
|
||||
/// joins them into one ([`mappings_added`]).
|
||||
///
|
||||
/// The guest's RAM, the files views are made of, stays on base pages so ([`super::dma_buf`],
|
||||
/// "The pages' size"): Linux 6.11 to 7.1 build a udmabuf's table with an entry a base page that
|
||||
/// names a large folio's head page and the page's offset in it, and amdgpu's import of such a
|
||||
/// dma-buf binds the buffer's pages after the first to other pages (upstream 5bf888673e0d fixed
|
||||
/// it in 7.2): the guest's kernel memory was overwritten so on a Radeon 890M with Linux 6.18.59
|
||||
/// (issue 185). QEMU advises the RAM's own mapping so (`vendor/qemu/system/physmem.c`,
|
||||
/// `ram_block_add`), before its prealloc touches a page. A refusal changes nothing: a kernel
|
||||
/// without transparent huge pages refuses the advice and makes none. On Linux 7.2 and later
|
||||
/// ([`huge_pages_take`]) nothing is advised, as QEMU then puts the guest's RAM on huge pages.
|
||||
fn off_huge_pages(at: u64, length: u64, offset: u64) {
|
||||
if huge_pages_take() || !at.wrapping_sub(offset).is_multiple_of(HUGE_ALIGN) {
|
||||
return;
|
||||
}
|
||||
// SAFETY: advice on a mapping just made, which changes none of its bytes.
|
||||
unsafe { libc::madvise(ptr(at), length.to_usize(), libc::MADV_NOHUGEPAGE) };
|
||||
}
|
||||
|
||||
/// Whether the guest's RAM may lie on huge pages: the running kernel is Linux 7.2 or later
|
||||
/// ([`linux_release_at_least`] of `uname`'s release), read once. 7.2 builds a udmabuf's table from
|
||||
/// its pages themselves (upstream 5bf888673e0d), so its imports bind the right pages over large
|
||||
/// folios. QEMU decides the same of the same fact (`qemu_guest_ram_huge_pages`,
|
||||
/// `vendor/qemu/util/cutils.c`), whose test holds the same table.
|
||||
fn huge_pages_take() -> bool {
|
||||
#[cfg(test)]
|
||||
if let Some(take) = TEST_HUGE_PAGES.with(std::cell::Cell::get) {
|
||||
return take;
|
||||
}
|
||||
static TAKE: OnceLock<bool> = OnceLock::new();
|
||||
*TAKE.get_or_init(|| {
|
||||
// SAFETY: an empty utsname for the kernel to fill.
|
||||
let mut uts: libc::utsname = unsafe { std::mem::zeroed() };
|
||||
// SAFETY: a utsname to fill.
|
||||
if unsafe { libc::uname(&mut uts) } != 0 {
|
||||
return false;
|
||||
}
|
||||
// SAFETY: the kernel's NUL-terminated release, in the buffer it filled.
|
||||
let release = unsafe { std::ffi::CStr::from_ptr(uts.release.as_ptr()) };
|
||||
linux_release_at_least(&release.to_string_lossy(), 7, 2)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// A test's answer of [`huge_pages_take`] in place of the kernel's, on the thread that sets
|
||||
/// it.
|
||||
static TEST_HUGE_PAGES: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
|
||||
}
|
||||
|
||||
/// Whether `release`, a Linux kernel release as `uname` gives it (`7.3.3-1-cachyos`), is
|
||||
/// `major.minor` or later by its leading `MAJOR.MINOR`; `false` where it does not start with two
|
||||
/// numbers and a dot between them.
|
||||
fn linux_release_at_least(release: &str, major: u64, minor: u64) -> bool {
|
||||
fn number(s: &str) -> Option<(u64, &str)> {
|
||||
let digits = s.bytes().take_while(u8::is_ascii_digit).count();
|
||||
let value = s.get(..digits)?.parse().ok()?;
|
||||
Some((value, &s[digits..]))
|
||||
}
|
||||
let Some((maj, rest)) = number(release) else {
|
||||
return false;
|
||||
};
|
||||
let Some((min, _)) = rest.strip_prefix('.').and_then(number) else {
|
||||
return false;
|
||||
};
|
||||
maj > major || (maj == major && min >= minor)
|
||||
Ok(mapped.addr().to_u64())
|
||||
}
|
||||
|
||||
/// A fresh range of `len` bytes, rounded up to whole pages, of zeros, that a view can be made of:
|
||||
@@ -2052,102 +1972,6 @@ pub(crate) mod tests {
|
||||
})
|
||||
}
|
||||
|
||||
/// The `VmFlags` of the mapping `at` lies in, as `/proc/self/smaps` lists them, one word a
|
||||
/// flag (`nh` for `MADV_NOHUGEPAGE`); `None` where no mapping holds it.
|
||||
fn vm_flags_at(at: u64) -> Option<Vec<String>> {
|
||||
let smaps = std::fs::read_to_string("/proc/self/smaps").unwrap();
|
||||
let mut inside = false;
|
||||
for line in smaps.lines() {
|
||||
let first = line.split_whitespace().next().unwrap_or("");
|
||||
if let Some((low, high)) = first.split_once('-')
|
||||
&& let (Ok(low), Ok(high)) =
|
||||
(u64::from_str_radix(low, 16), u64::from_str_radix(high, 16))
|
||||
{
|
||||
inside = (low..high).contains(&at);
|
||||
} else if inside && let Some(flags) = line.strip_prefix("VmFlags:") {
|
||||
return Some(flags.split_whitespace().map(str::to_owned).collect());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Below Linux 7.2, a shared mapping of a file whose address agrees with its offset modulo
|
||||
/// 2 MiB, where a fault or khugepaged could put the file's pages on a huge folio, is advised
|
||||
/// `MADV_NOHUGEPAGE` (`off_huge_pages`), so the guest's RAM stays on base pages through the
|
||||
/// library's views of it; two such that follow on in the file are still one mapping to the
|
||||
/// kernel, as the views' budget counts them (`mappings_added`); one that does not agree is left
|
||||
/// without the advice; and on 7.2 and later none is advised
|
||||
#[test]
|
||||
fn a_files_mapping_that_could_hold_a_huge_page_is_advised_off_them_before_7_2() {
|
||||
if !std::path::Path::new("/sys/kernel/mm/transparent_hugepage").exists() {
|
||||
eprintln!("skipped: this kernel has no transparent huge pages to advise off");
|
||||
return;
|
||||
}
|
||||
TEST_HUGE_PAGES.with(|h| h.set(Some(false)));
|
||||
let p = page();
|
||||
let v = vm();
|
||||
let rw = VmProt::READ | VmProt::WRITE;
|
||||
let fd = memfd(2 * HUGE_ALIGN).unwrap();
|
||||
let backing = |offset| Backing {
|
||||
file: Arc::clone(&fd),
|
||||
offset,
|
||||
};
|
||||
let reserved = v.allocate_anywhere(4 * HUGE_ALIGN).unwrap();
|
||||
let aligned = reserved.addr().to_u64().next_multiple_of(HUGE_ALIGN);
|
||||
let half = HUGE_ALIGN / 2;
|
||||
assert_eq!(file(Some(aligned), half, &backing(0), rw), Ok(aligned));
|
||||
assert_eq!(
|
||||
file(Some(aligned + half), half, &backing(half), rw),
|
||||
Ok(aligned + half)
|
||||
);
|
||||
let flags = vm_flags_at(aligned).expect("the mapping");
|
||||
assert!(flags.iter().any(|f| f == "nh"), "{flags:?}");
|
||||
assert_eq!(
|
||||
vm_flags_at(aligned + HUGE_ALIGN - 1),
|
||||
Some(flags),
|
||||
"the second piece's"
|
||||
);
|
||||
assert_eq!(kernel_mappings(aligned, aligned + HUGE_ALIGN), 1);
|
||||
|
||||
let apart = aligned + HUGE_ALIGN + p;
|
||||
assert_eq!(file(Some(apart), p, &backing(HUGE_ALIGN), rw), Ok(apart));
|
||||
let flags = vm_flags_at(apart).expect("the mapping");
|
||||
assert!(!flags.iter().any(|f| f == "nh"), "{flags:?}");
|
||||
|
||||
TEST_HUGE_PAGES.with(|h| h.set(Some(true)));
|
||||
let later = aligned + 2 * HUGE_ALIGN;
|
||||
assert_eq!(file(Some(later), HUGE_ALIGN, &backing(0), rw), Ok(later));
|
||||
let flags = vm_flags_at(later).expect("the mapping");
|
||||
assert!(!flags.iter().any(|f| f == "nh"), "7.2 and later: {flags:?}");
|
||||
TEST_HUGE_PAGES.with(|h| h.set(None));
|
||||
assert!(v.deallocate(reserved, 4 * HUGE_ALIGN).is_success());
|
||||
}
|
||||
|
||||
/// The kernel's release decides as QEMU's `qemu_linux_release_at_least` does, by the same
|
||||
/// table (`vendor/qemu/tests/unit/test-cutils.c`): 7.2 and later take huge pages, an older
|
||||
/// release or one that does not parse does not
|
||||
#[test]
|
||||
fn the_kernels_release_decides_huge_pages_as_qemu_does() {
|
||||
for (release, huge) in [
|
||||
("6.13", false),
|
||||
("6.18.59", false),
|
||||
("7.1.9", false),
|
||||
("7.1.0-arch1-1", false),
|
||||
("7.2", true),
|
||||
("7.2.0", true),
|
||||
("7.3.3-1-cachyos", true),
|
||||
("10.0.1", true),
|
||||
("", false),
|
||||
("garbage", false),
|
||||
("7", false),
|
||||
("7.", false),
|
||||
("v7.3.3", false),
|
||||
("7.x", false),
|
||||
] {
|
||||
assert_eq!(linux_release_at_least(release, 7, 2), huge, "{release:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A graphics device's region's memory is one shared mapping, read and write, of a memfd of
|
||||
/// its own sealed against growing and shrinking, from the file's first byte: zeros, which a
|
||||
/// write through the mapping changes in the file, registered in the table while it lives, so
|
||||
|
||||
@@ -119,22 +119,26 @@ Stages:
|
||||
libraries' staging chunks lie in the device's region (BAR 2) instead.
|
||||
Where the device offers no pool, Shared buffers and heaps lie in the
|
||||
region or in the process's own pages ("The Shared pool").
|
||||
- Under KVM that needs Linux 6.13 or later. Before 6.13, KVM maps a page
|
||||
- A Linux host needs Linux 7.2 or later, under any accelerator: before
|
||||
7.2 a udmabuf over large pages is malformed, and an AMD GPU's import of
|
||||
the guest's RAM is bound to the wrong pages ([qemu.md](../qemu.md),
|
||||
"Linux hosts: guest RAM's pages and the kernel's release"). reims-ctl
|
||||
refuses a start on an older kernel (`MIN_LINUX` in
|
||||
`crates/reims-ctl/src/runtime.rs`), and QEMU's device its realize. The
|
||||
floor holds KVM's own for this memory too: before 6.13, KVM maps a page
|
||||
of a driver's mapping only where it can take a reference on the page,
|
||||
and the drivers on TTM (amdgpu among them) allocate higher-order pages
|
||||
whose tail pages have none, so the guest's first write to them stops
|
||||
its vCPU ("kvm run failed Bad address"; read from Linux 6.12's
|
||||
`hva_to_pfn_remapped` and `ttm_pool_alloc_page`). reims-ctl refuses a
|
||||
start on an older kernel (`MIN_KVM_KERNEL` in
|
||||
`crates/reims-ctl/src/runtime.rs`).
|
||||
which the tail pages of the higher-order pages that the drivers on TTM
|
||||
(amdgpu among them) allocate lack, so the guest's first write to them
|
||||
stops its vCPU ("kvm run failed Bad address"; read from Linux 6.12's
|
||||
`hva_to_pfn_remapped` and `ttm_pool_alloc_page`).
|
||||
- Real discrete Macs keep Shared in system memory, so this is Metal's
|
||||
discrete model.
|
||||
- IOSurfaces are the one place the host reaches guest RAM: where the
|
||||
device has `RGFX_FEATURE_SURFACE_PAGES`, the host maps a surface's own
|
||||
guest pages for its GPU in place (Decided 14, "Today"), from the guest
|
||||
RAM's map QEMU gives it.
|
||||
- VRAM mapped into the guest was considered and dropped. It needed kernel
|
||||
6.13 or later and resizable BAR, and was good for guest writes only:
|
||||
- VRAM mapped into the guest was considered and dropped. It needed
|
||||
resizable BAR, and was good for guest writes only:
|
||||
measured on amdgpu, CPU reads from VRAM run at 15 MiB/s with memcpy,
|
||||
against about 10 GiB/s for writes. It returns only if a measured frame
|
||||
shows the GPU's reads of Shared memory across PCIe costing.
|
||||
|
||||
+1
-1
@@ -137,7 +137,7 @@ one. A new trap found the same way goes here.
|
||||
- A kernel that copies CPU state at fixed offsets is right only on the CPUs whose layout it was written for. XNU copies a thread's AVX-512 state (k0-7, zmm0-15's upper halves, zmm16-31) between its save area and the `x86_AVX512_STATE64` flavor at Intel's XSAVE offsets, 1088, 1152 and 1664 (`fpu_get_fxstate` and `fpu_set_fxstate`, `osfmk/i386/fpu.c`, read from code), while the save area holds what the CPU's XSAVE wrote, at the offsets CPUID leaf 0xD gives: on a Zen 4 core 832, 896 and 1408. A signal's frame (sendsig) and its return (sigreturn) go through those copies, so with AVX-512 state on under KVM on a Zen 4 host, a thread that ran AVX-512 without checking the feature read other bytes than its registers held in its signal frame and through `thread_get_state`, on every release from 11.7.11 to 26.7.2, and lost k3 across a signal handler on 12.7.6 and later; nothing logged it, and the guest booted and ran as before (**measured**, 2026-11-20 and 21, with the promotion and without the copy, m3-max-63651 among them). RgfxGate's xstate route copies the current thread's state with XSAVE and XRSTOR of components 5 to 7 at leaf 0xD's offsets (`rgfx_xstate.h`, `xstate_test`; `tests/xstate_sigproof.c` asserts it in a guest). How to spot the next one: a kernel or framework that names an architectural layout by constants, run on a CPU of another vendor; read the layout the CPU reports (CPUID) beside the constants, and round-trip known register values through the path (a signal handler, `thread_get_state`) before trusting it.
|
||||
- QEMU in a rootless container sees only the USB devices plugged in as it starts, so a passed-through device that re-enumerates never comes back to the guest, and nothing logs why. On the M1 Pro under Fedora Asahi Remix, where QEMU runs in a rootless distrobox, an iPhone given to an arm64 VM came back from each reset as a new device number (as QEMU took it from `ipheth`, and as `usb_host_close` reset it), and QEMU's `info usbhost` never listed the new number, though the device's file was there and the user could open it (**measured** 2026-11-24, docs/qemu.md "arm64 guests: USB passthrough"). Its auto-check kept trying the old number (`usb_host_open_failure dev 1:3`). How to spot the next one: a `usb-host` device that shows in the host's `lsusb` but not in QEMU's `info usbhost`; compare the two before suspecting the guest or the controller.
|
||||
- A margin sized as a measured spread plus one of its parts counts that part twice, and a run's own start hides its worst start. dsound's playback reserve was sized as the 61 ms that the fill at each period's arrival ranged, plus a timer run 30 ms late; the 61 ms already held the timer's wait, and every run started where it happened to on the guest's 41.67 ms post grid, so no run showed how near the reserve the posts could come. Split a reserve into its measured parts (here the guest's post spread, the wait for the write, and DirectSound's 10 ms cursor steps) and replay the trace from every start the stream could take: the smallest reserve with no gap at any start is the margin's largest fall from a post's write to any later read ([qemu.md](qemu.md), "Windows hosts: sound"). A run at 56 ms had no gap at its own start and would have had one at 1 of its 2,235 possible starts.
|
||||
- A change to how the host backs guest RAM can overwrite the guest's memory minutes into its use, and a boot that ends at the desktop does not see it. QEMU's `53e76630` moved a memfd's preallocated guest RAM onto 2 MiB pages with an `madvise` once the prealloc had run, before any device was realized. It was measured with one boot to the desktop at each of `shmem_enabled`'s `advise` and `never`, on a discrete RX 7900 XTX (2026-11-23), each with no fatal line. A user's x86-64 macOS 26.7.2 guest on the reims-graphics device, on an integrated Radeon 890M (RADV, Mesa 26.1.8, Linux 6.18.59), then panicked 80 to 154 s into each of 5 boots, its kernel's memory overwritten (issue 185). The reporter's bisect: the device with that commit panicked; the prealloc with the `madvise` skipped ran clean for 20 minutes; the `madvise` with plain VGA in place of the device ran clean. The writes were the GPU's, read from code: Linux 6.11 to 7.1 build a udmabuf's scatterlist with an entry per 4 KiB page naming a large folio's head page and the page's offset in it, and amdgpu's dma-buf import walks that table by DMA page, counting an entry at folio page k as k + 1 pages, so under dma-direct (an AMD integrated GPU's default identity domain) the buffer's pages after the first were bound to other guest pages (upstream fixed it in 7.2, `5bf888673e0d`; our lab ran 7.3, where the same workout on a Radeon 780M ran clean, m3-max-74522). QEMU's `de738a41` took the `madvise` out; since `695ad62c` QEMU collapses the RAM onto huge pages only on Linux 7.2 and later, and below 7.2 QEMU advises the RAM, the device its region and pool, and the library its views of guest RAM `MADV_NOHUGEPAGE`, so they stay on 4 KiB pages ([qemu.md](qemu.md), "Linux hosts: guest RAM's pages and the kernel's release"). How to spot the next one: a change to what backs or maps guest RAM (its pages, their size, where they sit) is proven by the guest in use for longer than such a fault takes to show, with apps drawing, on an integrated GPU and a discrete one, not by a boot that ends at the desktop; and a lab on one kernel line proves nothing of a kernel path that changed between lines, so read that path (here `drivers/dma-buf/udmabuf.c`) in the oldest kernel users run.
|
||||
- A change to how the host backs guest RAM can overwrite the guest's memory minutes into its use, and a boot that ends at the desktop does not see it. QEMU's `53e76630` moved a memfd's preallocated guest RAM onto 2 MiB pages with an `madvise` once the prealloc had run, before any device was realized. It was measured with one boot to the desktop at each of `shmem_enabled`'s `advise` and `never`, on a discrete RX 7900 XTX (2026-11-23), each with no fatal line. A user's x86-64 macOS 26.7.2 guest on the reims-graphics device, on an integrated Radeon 890M (RADV, Mesa 26.1.8, Linux 6.18.59), then panicked 80 to 154 s into each of 5 boots, its kernel's memory overwritten (issue 185). The reporter's bisect: the device with that commit panicked; the prealloc with the `madvise` skipped ran clean for 20 minutes; the `madvise` with plain VGA in place of the device ran clean. The writes were the GPU's, read from code: Linux 6.11 to 7.1 build a udmabuf's scatterlist with an entry per 4 KiB page naming a large folio's head page and the page's offset in it, and amdgpu's dma-buf import walks that table by DMA page, counting an entry at folio page k as k + 1 pages, so under dma-direct (an AMD integrated GPU's default identity domain) the buffer's pages after the first were bound to other guest pages (upstream fixed it in 7.2, `5bf888673e0d`; our lab ran 7.3, where the same workout on a Radeon 780M ran clean, m3-max-74522). QEMU's `de738a41` took the `madvise` out; `695ad62c` put the collapse back, and since `458f4b7b` `reims-ctl` and the device refuse a Linux host below 7.2 ([qemu.md](qemu.md), "Linux hosts: guest RAM's pages and the kernel's release"). How to spot the next one: a change to what backs or maps guest RAM (its pages, their size, where they sit) is proven by the guest in use for longer than such a fault takes to show, with apps drawing, on an integrated GPU and a discrete one, not by a boot that ends at the desktop; and a lab on one kernel line proves nothing of a kernel path that changed between lines, so read that path (here `drivers/dma-buf/udmabuf.c`) in the oldest kernel users run.
|
||||
|
||||
## Tools and worktrees
|
||||
|
||||
|
||||
+29
-34
@@ -144,7 +144,7 @@ virtio device (`d151bcd9`, upstream's `b501f21312`).
|
||||
| the window opens at the `resolution` property's size, 1920x1080 without it | `9bf62d84` |
|
||||
| the `displays` property, 1 to 4 (1 by default, others refused at realize): display i is head i, with a console, a window under `-display none` and a cursor of its own; console i reads display i (`rvg_graphics_device_read_display_at`), so QMP's `screendump` of head i saves it ("The reims-graphics device", below) | `010bf827` |
|
||||
| file-backed guest RAM (the memfd of `reims-ctl`'s VMs) mapped 2 MiB-aligned on Linux, as anonymous RAM is | `5e83e90dc2` |
|
||||
| on Linux 7.2 and later, shared file-backed guest RAM of base pages (the memfd of `reims-ctl`'s VMs) advised `MADV_HUGEPAGE` and, once its prealloc has run, collapsed into huge pages (`MADV_COLLAPSE`), with a line saying how much of it the collapse put on them; below 7.2, it and the device's RAM from the library's memory (the region, BAR 2, and each piece of the Shared pool) advised `MADV_NOHUGEPAGE`, not collapsed, with a line naming the kernel's release and "before 7.2" ("Linux hosts: guest RAM's pages and the kernel's release", below) | `51382c20`, `1a35b9fb`, `695ad62c` |
|
||||
| shared file-backed guest RAM of base pages (the memfd of `reims-ctl`'s VMs) collapsed into huge pages (`MADV_COLLAPSE`) once its prealloc has run, with a line saying how much of it the collapse put on them; the device's realize fails on a Linux host below 7.2, naming its release ("Linux hosts: guest RAM's pages and the kernel's release", below) | `695ad62c`, `458f4b7b` |
|
||||
| `isa-applesmc` answers GET_KEY_BY_INDEX, listing its keys by name and 0xb8 past the last, so the key walk macOS 15.4 and later make at boot ends ("x86-64 macOS guests: the SMC", below) | `83de566436` |
|
||||
| QMP's `screendump` in PPM, its default format, writes rows of width × 3 bytes. Upstream's wrote each row with its line buffer's stride, which pixman rounds up to 4 bytes, so a display whose width is not a multiple of 4 (a window of 3662 × 2039 pixels) read back sheared, its colours rotating from row to row; PNG's rows were right. `tests/qtest/display-screendump-test.c` dumps `bochs-display` at widths of 64, 66 and 67 | `e1360002ad` |
|
||||
| QMP's and HMP's `screendump` copy the console's surface under the BQL, then encode and write the copy on a thread of the pool with the BQL let go (`thread_pool_submit_co`), so a vCPU that touches a device the BQL guards no longer waits out the encode: a PNG of 2880x1620 takes about 120 ms on a Mac mini 2018 ("The reims-graphics device", below) | `500d30fb` |
|
||||
@@ -687,33 +687,29 @@ QEMU rely on.
|
||||
|
||||
### Linux hosts: guest RAM's pages and the kernel's release
|
||||
|
||||
On Linux 7.2 and later QEMU puts the guest's RAM, the memfd of `reims-ctl`'s
|
||||
VMs, on 2 MiB pages: it advises it `MADV_HUGEPAGE` and collapses it once its
|
||||
prealloc has run (`host_memory_backend_collapse`, `backends/hostmem.c`).
|
||||
Below 7.2 the RAM stays on 4 KiB pages at every `shmem_enabled`: QEMU advises
|
||||
it `MADV_NOHUGEPAGE` before its prealloc and does not collapse it
|
||||
(`ram_block_add`, `system/physmem.c`), the device advises the library's
|
||||
memory it maps into the guest, the region and the Shared pool's pieces, the
|
||||
same (`reims_graphics_library_ram`), and the library each of its views of
|
||||
guest RAM that could hold a huge page (`off_huge_pages`,
|
||||
`crates/reims-vgpu-vulkan/src/os/linux/vm.rs`). The release is `uname`'s,
|
||||
read once by its leading `MAJOR.MINOR` (`qemu_guest_ram_huge_pages`,
|
||||
`util/cutils.c`, and the library's `huge_pages_take`, both tested with one
|
||||
table); one that does not parse counts as older.
|
||||
A VM on the reims-graphics device needs Linux 7.2 or later, under any
|
||||
accelerator. `reims-ctl` refuses to start one on an older kernel
|
||||
(`MIN_LINUX`, `crates/reims-ctl/src/runtime.rs`), and the device's realize
|
||||
fails there, naming the running release (`reims_graphics_linux_floor`, by
|
||||
`qemu_linux_release_meets_floor` in `util/cutils.c`, whose test holds its
|
||||
table: `uname`'s release by its leading `MAJOR.MINOR`, one that does not
|
||||
parse counting as older). Why: Linux 6.11 to 7.1 build a udmabuf's
|
||||
scatterlist over a large folio with entries that name its head page and an
|
||||
offset past the page, which amdgpu's import binds to the wrong host pages
|
||||
(issue 185: a macOS 26.7.2 guest's kernel memory overwritten on a Radeon 890M
|
||||
with Linux 6.18.59; the arithmetic is in
|
||||
`crates/reims-vgpu-vulkan/src/os/linux/dma_buf.rs`, "The pages' size"), and
|
||||
7.2 builds it from the pages themselves (`sg_alloc_table_from_pages`,
|
||||
upstream `5bf888673e0d`, read from v7.2's `drivers/dma-buf/udmabuf.c`).
|
||||
|
||||
Why: Linux 6.11 to 7.1 build a udmabuf's scatterlist with an entry per 4 KiB
|
||||
page that names a large folio's head page and an offset past the page, and
|
||||
amdgpu's import of such a dma-buf binds the buffer's pages to the wrong host
|
||||
pages, which overwrote a macOS 26.7.2 guest's kernel memory on a Radeon 890M
|
||||
with Linux 6.18.59 (issue 185; the arithmetic is in
|
||||
`crates/reims-vgpu-vulkan/src/os/linux/dma_buf.rs`, "The pages' size"). 7.2
|
||||
builds the scatterlist from the pages themselves (upstream `5bf888673e0d`).
|
||||
|
||||
The backend logs one line saying which it took: on 7.2 and later, how many of
|
||||
the RAM's bytes the collapse put on huge pages, in how long, and its first
|
||||
failure; below 7.2, that the RAM is on 4 KiB pages, naming the kernel's
|
||||
release and "before 7.2". The share a host gets on 7.2 and later depends on
|
||||
its free memory as the VM starts, and the line says what it got.
|
||||
QEMU puts the guest's RAM, the memfd of `reims-ctl`'s VMs, on 2 MiB pages: it
|
||||
advises it `MADV_HUGEPAGE` (`ram_block_add`, `system/physmem.c`), as it does
|
||||
the device's RAM from the library's memory (the region, BAR 2, and each piece
|
||||
of the Shared pool), and collapses it once its prealloc has run
|
||||
(`host_memory_backend_collapse`, `backends/hostmem.c`). The backend logs one
|
||||
line saying how many of the RAM's bytes the collapse put on huge pages, in
|
||||
how long, and its first failure. The share a host gets depends on its free
|
||||
memory as the VM starts, and the line says what it got.
|
||||
|
||||
Measured on msi-5080 (macos-26, 16 GiB, a drag and a UFO test, 2026-10-27),
|
||||
with the RAM on huge pages from the THP settings at a fault: KVM mapped about
|
||||
@@ -727,10 +723,9 @@ on 4 KiB pages at the same frame rate.
|
||||
vCPUs (`prealloc-threads`). Without it, the guest's first touch of a page
|
||||
faults on a vCPU, and KVM resolves the fault through `get_user_pages`, which
|
||||
holds the process's mmap lock for read. Where the host backs the memfd with
|
||||
transparent huge pages (on Linux 7.2 and later, above), that fault allocates
|
||||
a 2 MiB page, and with `defrag` at `madvise` (the kernel's default) and
|
||||
QEMU's `MADV_HUGEPAGE` it reclaims and compacts memory for it inside the
|
||||
fault. On a host whose free
|
||||
transparent huge pages (above), that fault allocates a 2 MiB page, and with
|
||||
`defrag` at `madvise` (the kernel's default) and QEMU's `MADV_HUGEPAGE` it
|
||||
reclaims and compacts memory for it inside the fault. On a host whose free
|
||||
memory is fragmented, compaction mostly fails and can take over a second. A
|
||||
thread that asks for the lock for write meanwhile (glibc growing a heap by
|
||||
`mprotect`, a view's `mmap`) waits behind the fault, and every later reader
|
||||
@@ -1478,9 +1473,9 @@ once, in 5 rounds that rotate the kinds. The table gives each rate's median in M
|
||||
- On Linux the same probe put the pool within 5% of the region
|
||||
(homelab-um790-xtx-1 under KVM, `m3-max-51954`). One thread stored 28,531
|
||||
MB/s into the pool and 29,991 into the region, and 8 threads loaded 77,731
|
||||
and 78,000. There, QEMU madvised each piece HUGEPAGE, as it does on Linux
|
||||
7.2 and later, and none to all of a piece was in huge pages, against 202 of
|
||||
the region's 268 MiB resident.
|
||||
and 78,000. There, QEMU madvised each piece HUGEPAGE, as it does, and none
|
||||
to all of a piece was in huge pages, against 202 of the region's 268 MiB
|
||||
resident.
|
||||
|
||||
Making a piece: the pool starts with no piece, so the guest's first take
|
||||
waited for one to be made and mapped. That take, 128 KiB, waited:
|
||||
|
||||
Vendored
+1
-1
Submodule vendor/qemu updated: 695ad62cce...458f4b7b31.
Reference in new issue
Block a user