From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58F8C15B971 for ; Sun, 21 Jun 2026 13:37:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782049034; cv=none; b=nyaXG2tVIdIyGKsJHfifep6VbOfpDQTI8t3Qlt9MTe5ItOlTq/0HeDQeDZ8vK2XkoRutzqu8MUI6LR0BfJbtdjaDblZ3NNrZx/vunkr8xh9qRDuPLdiLvxa/MrVaEaBnuvDCE6Z+Jadzkvg9Sanmz7V9mjrLW50PJJ+IYmdLbVQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782049034; c=relaxed/simple; bh=6ihalu85en/+gN7tgPEPg2Vj92sCtuBXcx5ejgJvQ2g=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=oJcpjQqUkw3t5kckArt5Waq4iZTiHh2EEnFBB0GzVAfV9yoOikVPvw+U8pfSCJlRrr6pDer7YOC2tE5jN/I851Hf5zEwlMu8w+X/LvAJ4HqWeyS8bkUjh43FI+uJXAxtXPg+ztBfubtA7+gMj0RcW6B9+3pF4Ki6u/3jAB6SjFo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JfKROgm4; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JfKROgm4" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-490b3637b90so26526945e9.3 for ; Sun, 21 Jun 2026 06:37:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782049031; x=1782653831; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=G/+hi9dABkCsEUrlA98qCvRSl1EMUnAqOVs23V/vAGE=; b=JfKROgm4JBgDFUzpRcYMw/GogccEVb38UET3ucjgK9FvTTKKBqe8/qydrsgEvG2Aqq xEGzySq15bo6gFrmtTV4PQcOrg+1zWzEL+Lize1YOxb682CZPHC9W4v4fmTF5lRBImgR /1dUdHjc7RngZZ/L2tQJ76RsEArHSjSg2+7SXlWyao/lR/t6X+CtRtOPAPzy5WKVq7FE BHf1d+POqdZ4mrRs3URHcaB4d3weRzLgI8wNzAlCUZKbLeG9q2YG1Y66yEtRlg6tewYo gUOp78X39AYfGyNVznU4mPeahXb/ukhEDbJOHPZ+1H+xQLy7cneZKkLBbmG/ebGNcJ7g gKFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782049031; x=1782653831; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=G/+hi9dABkCsEUrlA98qCvRSl1EMUnAqOVs23V/vAGE=; b=A+CfLRmFBvr5dqsCHbdQmfGzCzE6cykkQldD1GzbdntrJyGX+X/5yxBbB2UsiawN/3 mqCZrKUnqIn/1283Fhi2UhR/zekande5cTkv8LohI68n97S+0WO1XF3Z5gLdYzRpjFtv nBEQmsBSsKNdiUEY4pgYR2EszowXOBuKM93muhYaPv+Q/nhuxq8aDsqpvRiDNukW5+i7 VMWv5q6vJ7+wkVuKuFtTBPgPWLMKpgG6oNR2qsH3Q2hkraxrwx/qI1mFrjOBhWmBy+K9 BwlKPHLwvBnJ9T/kSDj4LtYe+5tC4nogVmqEC/1naMf/8PZN1HYyN948ln1grSkahFvE wjYw== X-Gm-Message-State: AOJu0Yw2Y1jjTpGdH+4mLEM+X84To+lhgmRw95cOrLwG0MF25m6VbkyE ihGwvH1/L39DXKx/CC4ch6eeSPog8Gx3OAjyE/o+JLdIXin8HRAVD0d9 X-Gm-Gg: AfdE7cmh+78TCoogAFmDfYGYB6FLKgrRI3jZJ+PBK3STAYVrRDxVCV8M4IA7vmXKDzJ EkQQTt0vxoY8ZjMH2AysN3C8cggfuQEQ/JxWvWqQNtpVEJjLZsfoPe41SH6UkqPkaHrOYEjoMXO sPr7WXGGR7Hs5S/HAqoFj6TPfIwjIf/g2fh88Qkq3XYxOvHSdwZREkcKRwMOj9QYnn6jVa3ZlSa zQSr2StOEo3igtobgfmdNRahi9urM7mLwdD6E2R4eb7vXc6jSc6CIzIWhvafkiLA6M8RiwchR5m P3kHxyewu5ytvMNf5WZ4OKdkYackGtJkMCj4EpB+8SburaRJWF9kPIHJdN49RK8r8z/byZKl37h W6pU5qL6VJX17GCeWwID5VxMtmfVqQD7ASTknuzITXJOUtP21g0FxvIcES5b2HvMDDAOdgjnk5/ rP27NR8+a8kyRKvbjzwA== X-Received: by 2002:a05:600c:19c8:b0:492:4cc7:de73 with SMTP id 5b1f17b1804b1-4924cc7df3dmr76007435e9.31.1782049030547; Sun, 21 Jun 2026 06:37:10 -0700 (PDT) Received: from build-server.. ([62.96.37.222]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-466648c5397sm18864155f8f.13.2026.06.21.06.37.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jun 2026 06:37:09 -0700 (PDT) From: mike.malyshev@gmail.com To: Sean Christopherson , Paolo Bonzini , kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, "H. Peter Anvin" , Mikhail Malyshev Subject: [PATCH 0/1] KVM: x86/mmu: don't kill the VM on access to a disabled passthrough BAR Date: Sun, 21 Jun 2026 13:37:07 +0000 Message-ID: <20260621133708.3454718-1-mike.malyshev@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Mikhail Malyshev A guest with an assigned PCI device can crash its own VM by toggling PCI_COMMAND.MEM on that device while another vCPU accesses the device's BAR. KVM_RUN returns -EFAULT, which userspace (QEMU) treats as fatal. This is a guest-triggerable, host-side VM kill, so I think it is worth addressing in KVM rather than papering over it in userspace. The window ========== A passed-through BAR is mapped into the guest via a VM_IO/VM_PFNMAP VMA whose fault handler (vfio_pci_mmap_fault()) refuses to install a PTE while the device's memory space is disabled. When the guest clears PCI_COMMAND.MEM: - the kernel vfio config-write path zaps the BAR's userspace mapping; - userspace's memory listener later removes the corresponding KVM memslot. A vCPU that faults on the BAR in the window after the mapping is zapped but before the memslot is removed lands in the page fault path with a *valid* memslot but a backing whose fault handler declines. hva_to_pfn_remapped() returns an error, the gfn resolves to KVM_PFN_ERR_FAULT, and kvm_handle_error_pfn() returns -EFAULT. On bare metal the same access is an Unsupported Request (reads all ones, writes dropped), not a fatal error. This series makes KVM emulate the access as MMIO in that case, matching hardware, while leaving genuine faults (e.g. a vanished anonymous backing, vma == NULL) returning -EFAULT as before -- consistent with what tools/testing/selftests/kvm/ mmu_stress_test.c already asserts. How it was found / confirmed ============================ The crash was originally hit in production on edge devices that pass an Intel iGPU (Raptor Lake-P) through to a guest; the guest's display driver clears PCI_COMMAND.MEM on one vCPU while another vCPU is mid-MMIO to BAR0. To study it deterministically I built a reduced, hardware-light reproducer (no specific guest OS required, the race is host-side): - a Linux guest with any assigned PCI device whose BAR0 is mmap'd from userspace (/sys/.../resource0) and hammered with a tight MMIO write loop on one vCPU; - a second thread that toggles PCI_COMMAND.MEM 1->0->1 on that device via the VFIO config region. Without this patch the VM dies within ~1 s. eBPF on the fault path showed the -EFAULT originating in the faultin path (kvm_mmu_faultin_pfn -> kvm_handle_error_pfn) with the memslot valid (flags=0, not KVM_MEMSLOT_INVALID), no mmu_notifier invalidation in progress, and the pfn equal to the GUP-error value -- i.e. the VM_PFNMAP fault handler declining, exactly the case this patch targets. (An earlier attempt to treat it as a stale-mapping race and retry on mmu_invalidate_retry_gfn() did not help, because by the time of the fault the invalidation has already completed and the seq is stable; that confirmed the failure is a steady-state "device decoding disabled" condition, not a transient invalidation, and led to the MMIO approach here.) With the patch the same reproducer survived 200k toggle cycles, and a fleet of 17 devices ran 48h with no recurrence. Open questions for reviewers ============================ - hva_to_pfn_remapped() can in principle return an error for reasons other than "fault handler declined" (e.g. an OOM from fixup_user_fault()). Treating all of them as MMIO is what this patch does for simplicity; I can instead plumb the specific condition through if you'd prefer to narrow it. - I could not find a clean way to add a selftest: a faithful regression test needs a VM_PFNMAP backing whose fault handler can be toggled, which from pure userspace means /dev/mem or a real assigned device (neither CI-portable), or a dedicated test module (outside selftests). Guidance on the preferred shape would be welcome. Mikhail Malyshev (1): KVM: x86/mmu: Emulate, don't kill the VM, on access to a disabled passthrough BAR arch/x86/kvm/mmu/mmu.c | 16 +++++++++++++++- include/linux/kvm_host.h | 8 ++++++++ virt/kvm/kvm_main.c | 9 ++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) -- 2.43.0