🐛 Refuse a Linux host below 7.2 at reims-graphics-pci's realize

The device now needs Linux 7.2 or later on a Linux host: its realize fails
below it, naming the running release and "Linux 7.2 or later". Before 7.2 a
udmabuf over large pages is malformed (an entry per base page naming a large
folio's head page and an offset past the page), and amdgpu's import binds the
wrong pages (reims-vgpu issue 185); 7.2 builds the table from the pages
themselves (upstream 5bf888673e0d).

The floor takes the place of the below-7.2 form: ram_block_add advises every
block MADV_HUGEPAGE again, the device maps its region and pool pieces with no
advice of its own, and the backend collapses preallocated shared RAM with no
4 KiB line. qemu_guest_ram_huge_pages and qemu_linux_release_at_least become
qemu_linux_release_meets_floor, the floor's one check, whose test holds its
table: 7.1 refused, 7.2 passes, a release that does not parse refused.

What hosts see: below 7.2 the device's realize fails, where 51382c20 and
1a35b9fb kept guest RAM and the device's library RAM on 4 KiB pages; on 7.2
and later nothing changes.
This commit is contained in:
Anees Iqbal committed 2026-11-25 20:09:18 -08:00
1 parent 695ad62cce
commit 458f4b7b31
6 files changed
+53 -119

No files matched your search

-11
View File
@@ -347,9 +347,6 @@ size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
* pages, which would keep them where they are. A range it cannot collapse
* stays on its pages; one line says how much is on huge pages, and the first
* failure. An async prealloc (prealloc-context) has not run yet here: none.
* On Linux 7.2 or later only (qemu_guest_ram_huge_pages): below it the RAM
* was advised MADV_NOHUGEPAGE (ram_block_add), and one line says it stays on
* 4 KiB pages, naming the kernel's release.
*/
static void host_memory_backend_collapse(HostMemoryBackend *backend,
void *ptr, uint64_t sz,
@@ -363,7 +360,6 @@ static void host_memory_backend_collapse(HostMemoryBackend *backend,
uintptr_t start = QEMU_ALIGN_UP((uintptr_t)ptr, huge);
uintptr_t end = QEMU_ALIGN_DOWN((uintptr_t)ptr + sz, huge);
g_autofree char *name = NULL;
const char *release;
uint64_t done = 0;
int64_t t0;
int err = 0;
@@ -374,13 +370,6 @@ static void host_memory_backend_collapse(HostMemoryBackend *backend,
memory_region_get_fd(&backend->mr) < 0 || pagesize != 4 * KiB) {
return;
}
if (!qemu_guest_ram_huge_pages(&release)) {
name = host_memory_backend_get_name(backend);
info_report("memory backend '%s': %" PRIu64 " bytes on 4 KiB pages, "
"MADV_NOHUGEPAGE and no MADV_COLLAPSE: Linux %s is before "
"7.2", name, sz, release);
return;
}
t0 = g_get_monotonic_time();
for (uintptr_t addr = start; addr < end; addr += huge) {
if (madvise((void *)addr, huge, MADV_COLLAPSE) == 0) {
+33 -24
View File
@@ -104,7 +104,6 @@
#include "qemu/event_notifier.h"
#include "qemu/lockable.h"
#include "qemu/log.h"
#include "qemu/madvise.h"
#include "qemu/main-loop.h"
#include "qemu/queue.h"
#include "qemu/target-info.h"
@@ -136,6 +135,7 @@
#include "qemu/rcu.h"
#include "trace.h"
#ifdef CONFIG_LINUX
#include <sys/utsname.h>
#include "reims-graphics-pci-kvm.h"
#endif
#include "reims-graphics-pci-prefault.h"
@@ -1362,27 +1362,6 @@ static bool reims_graphics_pool_free(ReimsGraphicsPCIState *s, hwaddr gpa,
return true;
}
/*
* @mr as RAM of @size bytes of the library's memory at @host (the region, a
* pool piece), advised off transparent huge pages below Linux 7.2.
* ram_block_add advises a block from a pointer MADV_HUGEPAGE, which takes
* back the library's advice: the library's memory is shared mappings of
* memfds it hands GPUs as udmabuf dma-bufs, and Linux 6.11 to 7.1 build a
* udmabuf's scatterlist for a large folio's pages with entries amdgpu's
* import binds to the wrong pages (qemu_guest_ram_huge_pages; reims-vgpu
* issue 185). With shmem_enabled at advise the region held 70 MiB of 2 MiB
* folios after a boot. On 7.2 and later the huge page advice stands.
*/
static void reims_graphics_library_ram(MemoryRegion *mr, Object *owner,
const char *name, uint64_t size,
void *host)
{
memory_region_init_ram_ptr(mr, owner, name, size, host);
if (!qemu_guest_ram_huge_pages(NULL)) {
qemu_madvise(host, size, QEMU_MADV_NOHUGEPAGE);
}
}
/*
* Adds @ask's piece to the guest's address space, inside the run's memory
* transaction (reims_graphics_pool_bh); false, with nothing added, where it
@@ -1406,7 +1385,7 @@ static bool reims_graphics_pool_add(ReimsGraphicsPCIState *s,
piece = REIMS_GRAPHICS_POOL_PIECE(object_new(TYPE_REIMS_GRAPHICS_POOL_PIECE));
piece->s = s;
piece->gpa = ask->gpa;
reims_graphics_library_ram(&piece->mr, OBJECT(piece),
memory_region_init_ram_ptr(&piece->mr, OBJECT(piece),
"reims-graphics-pci.pool-piece", ask->size,
ask->host);
/*
@@ -2449,6 +2428,33 @@ static void reims_graphics_pci_at_exit(void)
}
}
/*
* Whether the host meets the device's floor, Linux 7.2 or later; else false,
* with @errp naming the running release. The guest's pages reach the host's
* GPU as udmabuf dma-bufs of its RAM's memfd, and before 7.2 a udmabuf over
* large pages is malformed, so an AMD GPU's import binds the wrong pages
* (qemu_linux_release_meets_floor; reims-vgpu issue 185). True off Linux.
*/
static bool reims_graphics_linux_floor(Error **errp)
{
#ifdef CONFIG_LINUX
struct utsname uts;
if (uname(&uts) != 0) {
pstrcpy(uts.release, sizeof(uts.release), "unknown");
}
if (!qemu_linux_release_meets_floor(uts.release)) {
error_setg(errp, "%s: this host runs Linux %s, and the device needs "
"Linux 7.2 or later: before 7.2 a udmabuf over large pages "
"is malformed, and an AMD GPU is bound to the wrong pages "
"of the guest's memory", TYPE_REIMS_GRAPHICS_PCI,
uts.release);
return false;
}
#endif
return true;
}
static void reims_graphics_pci_realize(PCIDevice *dev, Error **errp)
{
ReimsGraphicsPCIState *s = REIMS_GRAPHICS_PCI(dev);
@@ -2505,6 +2511,9 @@ static void reims_graphics_pci_realize(PCIDevice *dev, Error **errp)
uint64_t pool_room = 0;
uint32_t i;
if (!reims_graphics_linux_floor(errp)) {
return;
}
if (s->displays < 1 || s->displays > RVG_GRAPHICS_DISPLAYS_MAX) {
error_setg(errp, "%s: displays must be 1 to %d, not %" PRIu32,
TYPE_REIMS_GRAPHICS_PCI, RVG_GRAPHICS_DISPLAYS_MAX,
@@ -2674,7 +2683,7 @@ static void reims_graphics_pci_realize(PCIDevice *dev, Error **errp)
PCI_BASE_ADDRESS_SPACE_MEMORY, &s->doorbells);
base = rvg_graphics_device_region(s->device, &size);
reims_graphics_library_ram(&s->region, OBJECT(dev),
memory_region_init_ram_ptr(&s->region, OBJECT(dev),
"reims-graphics-pci.region", size, base);
pci_register_bar(dev, RVG_GRAPHICS_PCI_BAR_REGION,
PCI_BASE_ADDRESS_SPACE_MEMORY |
+7 -21
View File
@@ -175,30 +175,16 @@ int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
char *size_to_str(uint64_t val);
/**
* qemu_linux_release_at_least:
* qemu_linux_release_meets_floor:
* @release: a Linux kernel release, as uname(2) gives it ("7.3.3-1-cachyos")
* @major: the major version to compare with
* @minor: the minor version to compare with
*
* Returns: whether @release's leading "MAJOR.MINOR" is @major.@minor or
* later; false where @release does not start with two numbers and a dot
* between them.
* Returns: whether @release's leading "MAJOR.MINOR" is 7.2 or later, the
* oldest Linux the reims-graphics device runs on; false where @release does
* not start with two numbers and a dot between them. Before 7.2 a udmabuf
* over large pages is malformed, and an AMD GPU's import of it is bound to
* the wrong pages (reims-vgpu issue 185).
*/
bool qemu_linux_release_at_least(const char *release, unsigned int major,
unsigned int minor);
/**
* qemu_guest_ram_huge_pages:
* @release: if not NULL, set to the running kernel's release
*
* Returns: whether a shared file's guest RAM may go on transparent huge
* pages: the running kernel is Linux 7.2 or later, read once. Linux 6.11 to
* 7.1 build a udmabuf's scatterlist with an entry per base page that names a
* large folio's head page and an offset past the page, which importers walking
* it by DMA page (amdgpu's dma-buf import) bind to the wrong pages; 7.2 builds
* it from the pages themselves (upstream 5bf888673e0d). False off Linux.
*/
bool qemu_guest_ram_huge_pages(const char **release);
bool qemu_linux_release_meets_floor(const char *release);
/**
* freq_to_str:
+1 -21
View File
@@ -2427,27 +2427,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
if (new_block->host) {
qemu_ram_setup_dump(new_block->host, new_block->max_length);
/*
* Below Linux 7.2, a shared file of base pages, memory-backend-memfd's
* shmem among it, stays on base pages: its pages are handed to devices
* as udmabuf dma-bufs, and Linux 6.11 to 7.1 build a udmabuf's
* scatterlist with an entry per base page that names a large folio's
* head page and an offset past PAGE_SIZE, which importers walking it
* by DMA page (amdgpu's dma-buf import) bind to the wrong pages
* (qemu_guest_ram_huge_pages). Advised before any prealloc touches the
* file, so no fault through this mapping and no khugepaged pass over
* it makes a large folio, at any shmem_enabled but force. On 7.2 and
* later, and for other RAM, the huge page advice.
*/
if (new_block->fd >= 0 && qemu_ram_is_shared(new_block) &&
new_block->page_size == qemu_real_host_page_size() &&
!qemu_guest_ram_huge_pages(NULL)) {
qemu_madvise(new_block->host, new_block->max_length,
QEMU_MADV_NOHUGEPAGE);
} else {
qemu_madvise(new_block->host, new_block->max_length,
QEMU_MADV_HUGEPAGE);
}
qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUGEPAGE);
/*
* MADV_DONTFORK is also needed by KVM in absence of synchronous MMU
* Configure it unless the machine is a qtest server, in which case
+10 -9
View File
@@ -3688,17 +3688,18 @@ static void test_qemu_hexdump_alignment(void)
}
/*
* The table qemu_guest_ram_huge_pages decides by, which the library's
* linux_release_at_least test holds too: 7.2 and later take huge pages.
* The reims-graphics device's Linux floor: 7.2 and later pass, an older
* release or one that does not parse is refused.
*/
static void test_qemu_linux_release_at_least(void)
static void test_qemu_linux_release_meets_floor(void)
{
static const struct {
const char *release;
bool huge;
bool met;
} cases[] = {
{ "6.13", false },
{ "6.18.59", false },
{ "7.1", false },
{ "7.1.9", false },
{ "7.1.0-arch1-1", false },
{ "7.2", true },
@@ -3714,10 +3715,10 @@ static void test_qemu_linux_release_at_least(void)
};
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
g_assert_cmpint(qemu_linux_release_at_least(cases[i].release, 7, 2),
==, cases[i].huge);
g_assert_cmpint(qemu_linux_release_meets_floor(cases[i].release),
==, cases[i].met);
}
g_assert_false(qemu_linux_release_at_least(NULL, 7, 2));
g_assert_false(qemu_linux_release_meets_floor(NULL));
}
int main(int argc, char **argv)
@@ -4094,8 +4095,8 @@ int main(int argc, char **argv)
g_test_add_func("/cutils/qemu_hexdump/alignment",
test_qemu_hexdump_alignment);
g_test_add_func("/cutils/linux_release_at_least",
test_qemu_linux_release_at_least);
g_test_add_func("/cutils/linux_release_meets_floor",
test_qemu_linux_release_meets_floor);
return g_test_run();
}
+2 -33
View File
@@ -48,16 +48,11 @@
#include <wchar.h>
#endif
#ifdef CONFIG_LINUX
#include <sys/utsname.h>
#endif
#include "qemu/ctype.h"
#include "qemu/cutils.h"
#include "qemu/error-report.h"
bool qemu_linux_release_at_least(const char *release, unsigned int major,
unsigned int minor)
bool qemu_linux_release_meets_floor(const char *release)
{
unsigned long maj, min;
char *end;
@@ -70,33 +65,7 @@ bool qemu_linux_release_at_least(const char *release, unsigned int major,
return false;
}
min = strtoul(end + 1, &end, 10);
return maj > major || (maj == major && min >= minor);
}
bool qemu_guest_ram_huge_pages(const char **release)
{
#ifdef CONFIG_LINUX
static gsize once;
static struct utsname uts;
static bool huge;
if (g_once_init_enter(&once)) {
if (uname(&uts) != 0) {
pstrcpy(uts.release, sizeof(uts.release), "unknown");
}
huge = qemu_linux_release_at_least(uts.release, 7, 2);
g_once_init_leave(&once, 1);
}
if (release) {
*release = uts.release;
}
return huge;
#else
if (release) {
*release = "not Linux";
}
return false;
#endif
return maj > 7 || (maj == 7 && min >= 2);
}
void strpadcpy(char *buf, int buf_size, const char *str, char pad)