From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795963072; b=b8MuI6wkU28JgLvulGR1Nr3iJzX0VIxhAWK6nYrXH4BvmbpmpWZTb8ThgQ3iJyVRDrbJ O2VtlK6l7IsXS5duOJmUNGQKzc3P/dmCc6cqWzKpWz270EXOUnB0NDP8v9Bjh4M60fcNU aD0ncYUe1QO91f8dQNzXZg6IaUJvXsFVk2gseoCIUBDOUdxv3CU6tW8RdANzAxvqDSUCh aHWV92qCGFjqVtAFrf1088bVyZm2eQQVYno0GhiDbQdzgS6MRd3riEkxZFWG6/VRSqaje UvwF9H05e+HwYH05+9jfWs48ZaxTA8LAVGPPFOSnv+vopfvjoXOhw5pdCU8BsWDD2+A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795963072; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=ib+pg6bO+xmxHDNggZ6JUYEqgUu4XFwG9BmXLT16Lg8=; b=UTMFyfj9iErTLVoDb/4xwiFYONUqtOrSQp15gC7No4sGjD6rHxbzwh11v5YqGDPPO3QT xKwEWY/7uwkJcmtoZ1rDnDoIG0G4oXg+87pqkfNoLaB/t5UXARQjxFYEpzz2nAH5LhHmW oup82zgSejAUzhTY2dUp9qCQFBNtUZlLWrMavp9XEwkpDjVbw47uqyGoFbBRwPjtYZqOd dExfsvMKJfmqjyGR0oZRnd6koVsjHMNWot+wX+f40OsOqjn52gTIqfh7N8TX9ti+/plhX r3GJtLLMGUS+xrmGupdHdg/IoVyL8XySUgxnFhR7T2S7x0uxhut1D0etIEmaZdycYDA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S1ehwTJp; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D46E2606A31C for ; Sun, 29 Nov 2026 14:37:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S1ehwTJp Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F1CD1F00896; Sun, 29 Nov 2026 14:37:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795963072; bh=ib+pg6bO+xmxHDNggZ6JUYEqgUu4XFwG9BmXLT16Lg8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=S1ehwTJpIVldyNmFtXWHobwGP24t7UtQhMdKthk68wGMbLT91K2oSlgMV1ViLUXSR enoetVm1Ko5nm5ZmxT3pfEbRVDxCH4nxyzHr/Do+KVg+jq0u+lYhXQLulQgblPXJW8 ECGHq+ju583bnFd3ltFW9qM2eWG5EGZCKZoKa8Yu6WJeLR1NupaGWfO9j9D24+q7Xw tsFAC7CBHZgcXZZ4bGRqh10cyuA9YSjZfBNt++9OPctWZ5/jY1iQXu7hvwoeTspgnG BPXmhmXPG64ocecboJL7m6FC6TMWB3y5cDt2dd6DL8hajHw2iU367ljKwovjC42+8i ezuqztd08aOtQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH 1/3] KVM: riscv: Introduce a recursive G-stage unmap walker Reply-To: sashiko-reviews@lists.linux.dev To: "Tan Chi" Cc: kvm@vger.kernel.org In-Reply-To: <20261129142252.171879-2-tanchi25@mails.ucas.ac.cn> References: <20261129142252.171879-1-tanchi25@mails.ucas.ac.cn> <20261129142252.171879-2-tanchi25@mails.ucas.ac.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sun, 29 Nov 2026 14:37:52 +0000 X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: riscv: use-after-free of gstage->pgd in gstage_unmap_range_recursive() -- commit 5d921baa27d30ba8a457e5d017135853833a0eb5 Author: Tan Chi KVM: riscv: Introduce a recursive G-stage unmap walker This commit introduces a recursive G-stage page table walker to unmap address ranges by walking adjacent PTEs within each page table level, while keeping a fast path for single-page unmaps. > diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c > index f4ed659b11e94..dca3cd1c35e41 100644 > --- a/arch/riscv/kvm/gstage.c > +++ b/arch/riscv/kvm/gstage.c [ ... ] > +static noinline bool gstage_unmap_range_recursive(struct kvm_gstage *gst= age, > + gpa_t start, gpa_t size, bool may_block) > +{ > + int ret; > + gpa_t addr =3D start, end =3D start + size; > + bool flush =3D false; > + > + if (gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) > + may_block =3D false; > + > + while (addr < end) { > + ret =3D gstage_unmap_walk(gstage, (pte_t *)gstage->pgd, > + gstage->pgd_levels - 1, > + &addr, end, may_block, &flush); > + > + if (ret !=3D -EAGAIN || addr >=3D end) > + break; > + > /* > * If the range is too large, release the kvm->mmu_lock > * to prevent starvation and lockup detector warnings. > */ > - if (!(gstage->flags & KVM_GSTAGE_FLAGS_LOCAL) && may_block && addr < e= nd) > - cond_resched_rwlock_write(&gstage->kvm->mmu_lock); > + cond_resched_rwlock_write(&gstage->kvm->mmu_lock); > } > =20 > return flush; > } [Severity: High] Can gstage_unmap_range_recursive() access a freed root PGD after cond_resched_rwlock_write() drops and reacquires kvm->mmu_lock? kvm_unmap_gfn_range() snapshots kvm->arch.pgd into gstage.pgd once via kvm_riscv_gstage_init() before calling kvm_riscv_gstage_unmap_range(). When cond_resched_rwlock_write(&gstage->kvm->mmu_lock) releases mmu_lock and schedules out, a concurrent kvm_mmu_notifier_release() can tear down and free the root PGD: CPU0 CPU1 kvm_unmap_gfn_range() kvm_riscv_gstage_init(&gstage, kvm) gstage->pgd =3D kvm->arch.pgd ... gstage_unmap_range_recursive() gstage_unmap_walk() -> -EAGAIN cond_resched_rwlock_write() write_unlock(&kvm->mmu_lock) kvm_mmu_notifier_release() ... kvm_riscv_mmu_free_pgd() write_lock(&kvm->mmu_lock) kvm_riscv_gstage_unmap_all() WRITE_ONCE(kvm->arch.pgd, NULL) write_unlock(&kvm->mmu_lock) call_rcu(..., kvm_riscv_mmu_free_pgd_rcu) schedule() /* RCU grace period completes, PGD pages freed */ write_lock(&kvm->mmu_lock) gstage_unmap_walk(..., gstage->pgd, ...) ptep_get(ptep) <-- use-after-free Before this change, kvm_riscv_gstage_get_leaf() read gstage->pgd too, Wait: actually both old and new read gstage->pgd, or rather after reacquiring mmu_lock, checking !gstage->kvm->arch.pgd (like mmu_split_huge_pages() does after cond_resched_rwlock_write()) would avoid walking the stale gstage->pgd pointer. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261129142252.1718= 79-1-tanchi25@mails.ucas.ac.cn?part=3D1