From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 26C27386C20 for ; Mon, 4 May 2026 22:42:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777934552; cv=none; b=bNRWIuf7gbNXmvhyutQ6t/Xo+DIb3ae5+z5c4n1H2SkyXdzoSGtFupoodb8xYnl8fwXBjycmPS1vj3ZAnG1PqvbQoJ5AYdRjscMUmAHQ4hb2My0dDSWLoBISL/LDnSHDom01zkgjYXNYYx0y79Is6WxVABHQeURnYBdUhb/x9+Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777934552; c=relaxed/simple; bh=a6LAKe8ovUnasAZFDeUefPCWvqT60LXkBEdqdXAd5Mc=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=cPbOGAemU2Ns/T37EGl33qH8JMo9tGZm6zahNEmIjdNI6pBDQXfU2tCf7cXHB6NZzByVIsIBvDidyYbEjze1MIH6G6LqeAJ/J8yUr7tZwpFbyeEayKnVduL+/vZeJ/ukjD7zspRRgX00no4rOWpKDaVnBTIkxv4YX6usVO+mLLg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ksdeilwo; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jthoughton.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ksdeilwo" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-82fd55bf6cdso3230894b3a.3 for ; Mon, 04 May 2026 15:42:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777934549; x=1778539349; darn=lists.linux.dev; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=GreUyu/PwGDkwAAEFHxYx5rSO95cp1lrcmL4UWkbrcg=; b=Ksdeilwo7cv78EhIaBwBWS8c8eNJ/Jf6+621JiOxZVPZT3i4yoE+zw37UqCtwgChyi breODgqsmDdVM9wm7lkGkG5OQGM6h0pCBTl231ogKF166cZdkyRLAV4c+q4AG9YLvooQ XDCGD+N0hMSkiQ/SCaV9/icjonMw64Xa9jpQqHq8M4Fe1K940a0GBNmhCF+V3Dq0aV9v eogo5UpZ5oDiI1IF5l/QHMCUHmZx+jCguPz3hHyV4mmhGOpLS/X364sXDgcBmwEoJzJd aN+X1yALR7wYmg6qqBmXqDeRUwqxvwdUqUuDgy4H/3HGT2S9PG2i0nSI+qlE0nlrTSpg IIRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777934549; x=1778539349; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=GreUyu/PwGDkwAAEFHxYx5rSO95cp1lrcmL4UWkbrcg=; b=h/NaUl+Tudrt9bOOmOij2qfj+188RuxGLobebiy0V7r3c9tWa4Sp1vSSD2JyG3m++j +MyLCQ1GiQT156hru1WhkQjFDBzUGUjDozyOe8YFo4oRv4C2Ae7fT9SmHZm7IUIu+2Gx GuO8a9K6zVdSD9SgplVAwuQ6fwU5sbzDAOxRiY12CRDuE2AN+DmJUUVKpByfrXNJNzvZ juQfHS5bWqz24XQCBHE6f8VL77CzW0QQmpj1VHP+PBRgDkbz6AYHAWjwbG9REXNZ3Csa ghxLxvo1T6IqltAPEcDikWkBy/e7/y2JXYCIGTIV3qtFlZo8j3h2RxRSRJOUQLvxLgqZ lDWw== X-Forwarded-Encrypted: i=1; AFNElJ+LI9k6amW5Z25lSdbbAmLha5KfMXiV4sctWat5wDYEoJagY3Z1Q9l2lUMLlem9FlkPY6jrmyte1YY=@lists.linux.dev X-Gm-Message-State: AOJu0Yw21ESqpwU+wgRfTxtKtv6jPtg0ZYgDxlf94exw0zu7awcEkfri UQn04tnWS3mwhLkFs/Sz8k8Z8vs0D8BeAVeRO/nRAyjiDDrK106iO1z6z1worLXf/rVsbUjfSi+ GAkOG5nR6EHTdKgEucdS/zQ== X-Received: from pfbhm13.prod.google.com ([2002:a05:6a00:670d:b0:82f:7bc:70a9]) (user=jthoughton job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4c96:b0:82c:ded1:261f with SMTP id d2e1a72fcca58-8352d22c289mr10958941b3a.27.1777934549232; Mon, 04 May 2026 15:42:29 -0700 (PDT) Date: Mon, 4 May 2026 22:42:07 +0000 Precedence: bulk X-Mailing-List: loongarch@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260504224213.1049426-1-jthoughton@google.com> Subject: [PATCH 0/5] KVM: Fix race conditions in kvm_arch_flush_shadow_all() From: James Houghton To: Paolo Bonzini Cc: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Sean Christopherson , Gavin Shan , Shaoqin Huang , Ricardo Koller , Tianrui Zhao , Bibo Mao , Huacai Chen , James Hogan , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, James Houghton Content-Type: text/plain; charset="UTF-8" Hi Paolo, syzbot running on Google production kernels ran into a double-free on KVM/arm64 in kvm_mmu_free_memory_cache(). It turns out that loongarch and mips also have a similar problem. kvm_arch_flush_shadow_all() can be called on the same memslot concurrently, leading to double-freeing in arm64 and mips. loongarch is also affected: it can at least underflow some counters; I'm not sure what else can break. To get into this scenario, we need to have a process (P1) share an open VM with another process (P2). If P1 closes its VM to leave P2 holding the last reference, then there is a race between P1 exiting (exit_mm) and P2 dropping its last reference to the VM. exit_mm() and kvm_vm_release() both call kvm_mmu_notifier_release() on the same KVM, and the only locks held are the KVM srcu lock and the MMU notifier srcu lock. Please see the arm64 patch for another description of the same race with more context on the ensuing double-free in KVM/arm64. The first three patches fix each broken architecture; each of those patches have stable CCed with what I think are the appropriate Fixes. After patching the locking for the broken architectures, it seems better simply to have KVM take the MMU lock exclusively before calling kvm_arch_flush_shadow_all() so that architectures don't need to worry about it. Feel free to drop that patch, the fourth one, if you disagree with it. The fifth patch provides a repro (with a crude kernel patch to reliably demonstrate the double-free). Please do not merge this. The arm64 patch has been tested with the repro. The loongarch and mips patches have been compile-tested only. kvm_arch_guest_memory_reclaimed() is only implemented by one architecture: x86. Its implementation does not need the KVM MMU lock to be held. This series is based on 7.1-rc2. James Houghton (5): KVM: arm64: Grab KVM MMU write lock in kvm_arch_flush_shadow_all() KVM: loongarch: Grab MMU lock in kvm_arch_flush_shadow_all() KVM: mips: Grab MMU lock in kvm_arch_flush_shadow_all() KVM: Hold MMU lock exclusively when calling kvm_arch_flush_shadow_all() DO NOT MERGE: KVM: selftests: Reproducer for arm64 double-free arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_mmu.h | 1 + arch/arm64/kvm/mmu.c | 39 +++++- arch/arm64/kvm/nested.c | 4 +- arch/loongarch/kvm/mmu.c | 2 + arch/mips/kvm/mips.c | 2 + arch/mips/kvm/mmu.c | 2 + arch/riscv/kvm/mmu.c | 4 +- arch/riscv/kvm/vm.c | 2 + arch/x86/kvm/mmu/mmu.c | 4 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../testing/selftests/kvm/transfer_fd_test.c | 129 ++++++++++++++++++ virt/kvm/kvm_main.c | 3 + 13 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 tools/testing/selftests/kvm/transfer_fd_test.c base-commit: 6d35786de28116ecf78797a62b84e6bf3c45aa5a -- 2.54.0.545.g6539524ca2-goog