From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 74439C48260 for ; Tue, 13 Feb 2024 16:52:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3w022TQw1Q9qqEobai+twKs0sHb1A0Vo7frN1abqW8Q=; b=joeQDE2AxT8J7f +/pKWhSRGMGoQRpny3fkOT/Plr0ljQurbG7KJZt/CT3KS/9BKgFK6GjPwrjxi2OUcpREg3L4likBo 3KPGVEQgEpvu7+lA3DbaYNJ0MM64w7rxXkHRiSh5Ti982YtSDvAO16SeX1Z0hV6Zn7eyx1QMozZTa n7Vs0DNQwFWssWOtmyMfe1czOMRxw4RMeOuaHhCrmNgF6sdR8EVZBRM+QnVpAGOYjpnw8DpE4C1Lq vDINPH9SP7X2mOjpfPfYjd7RjqFdYBKuyzoDvYowZcG5mCCtC9P4iRSdja1ldNsfS/wtaaNI0uuCN z0u3jxve5De2N+4lnLwA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZw1S-0000000A33R-2kSJ; Tue, 13 Feb 2024 16:52:42 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rZw1Q-0000000A334-2VZY for linux-arm-kernel@lists.infradead.org; Tue, 13 Feb 2024 16:52:41 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id D81A5615B8; Tue, 13 Feb 2024 16:52:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7AB14C433C7; Tue, 13 Feb 2024 16:52:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707843159; bh=PXdxy1hTes8PVKE+xBuDu7zQhe1++IbzR9FzZbvGKBM=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=gNz4eBY5MfB0YkD0cIH7GgA3W+ofJnbeHVoUVVcXGT/4pS7rlMgsmAdgB2d/Jhi5h S4XjzDARZYlbxdazPlM/+VwxBlsAClBDBb0rg4qlL6gTUfn6vzY4//TCOLCnvI/e1B w4IUtm++lBt3NU6Rne5u2YGHGVI8eig+ypPIqR6RJigi8BhW79sJIz/ioy23UrvnSY 5HDL5za63UCDg0uTV17WHlpamrJrhkrYK6qm39Bd8kQiZcDeGegVmGIqTbhN0QaVr6 tfDZkadMbA0KasM9Q/KGy1nOLs6+nf5kjnntG8R7JbwJ4s2N88C0wGcJzrBlJId5g8 oUxkk4RwpZb+A== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rZw1N-002qby-2U; Tue, 13 Feb 2024 16:52:37 +0000 Date: Tue, 13 Feb 2024 16:52:36 +0000 Message-ID: <868r3o5msr.wl-maz@kernel.org> From: Marc Zyngier To: Oliver Upton Cc: Will Deacon , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Ricardo Koller Subject: Re: [RFC PATCH] KVM: arm64: Fix double-free following kvm_pgtable_stage2_free_unlinked() In-Reply-To: References: <20240212193052.27765-1-will@kernel.org> <86cyt062jh.wl-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/29.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: oliver.upton@linux.dev, will@kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, ricarkol@google.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240213_085240_765832_80D05E93 X-CRM114-Status: GOOD ( 42.16 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, 13 Feb 2024 16:29:42 +0000, Oliver Upton wrote: > > On Tue, Feb 13, 2024 at 11:12:34AM +0000, Marc Zyngier wrote: > > On Mon, 12 Feb 2024 20:14:37 +0000, > > Oliver Upton wrote: > > > > > > On Mon, Feb 12, 2024 at 07:30:52PM +0000, Will Deacon wrote: > > > > kvm_pgtable_stage2_free_unlinked() does the final put_page() on the > > > > root page of the sub-tree before returning, so remove the additional > > > > put_page() invocations in the callers. > > > > > > > > Cc: Marc Zyngier > > > > Cc: Oliver Upton > > > > Cc: Ricardo Koller > > > > Signed-off-by: Will Deacon > > > > --- > > > > > > > > Hi folks, > > > > > > > > Sending this as an RFC as I only spotted it from code inspection and I'm > > > > surprised others aren't seeing fireworks if it's a genuine bug. I also > > > > couldn't come up with a sensible Fixes tag, as all of: > > > > > > > > e7c05540c694b ("KVM: arm64: Add helper for creating unlinked stage2 subtrees") > > > > 8f5a3eb7513fc ("KVM: arm64: Add kvm_pgtable_stage2_split()") > > > > f6a27d6dc51b2 ("KVM: arm64: Drop last page ref in kvm_pgtable_stage2_free_removed()") > > > > I'd blame it on the last commit, as we really ought to have it if we > > have the others. > > > > > > > > > > are actually ok in isolation. Hrm. Please tell me I'm wrong? > > > > > > > > arch/arm64/kvm/hyp/pgtable.c | 2 -- > > > > 1 file changed, 2 deletions(-) > > > > > > > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > > > > index c651df904fe3..ab9d05fcf98b 100644 > > > > --- a/arch/arm64/kvm/hyp/pgtable.c > > > > +++ b/arch/arm64/kvm/hyp/pgtable.c > > > > @@ -1419,7 +1419,6 @@ kvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt, > > > > level + 1); > > > > if (ret) { > > > > kvm_pgtable_stage2_free_unlinked(mm_ops, pgtable, level); > > > > - mm_ops->put_page(pgtable); > > > > return ERR_PTR(ret); > > > > } > > > > > > AFAICT, this entire branch is effectively dead code, unless there's a > > > KVM bug lurking behind the page table walk. The sub-tree isn't visible > > > to other software or hardware walkers yet, so none of the PTE races > > > could cause this to pop. > > > > > > So while this is very obviously a bug, it might be pure luck that folks > > > haven't seen smoke here. Perhaps while fixing the bug we should take the > > > opportunity to promote the condition to WARN_ON_ONCE(). > > > > Can't you construct a case where an allocation fails during the walk > > (memcache empty), and we end up on this exact path? > > Possibly, but AFAICT that can only happen if there was a bug in KVM. We > don't start the walk at all if userspace set the split chunk size to 0, > and otherwise we expect it to be at least PMD_SIZE, which will top up > the cache to 1 every pass. stage2_split_walker() will 'do the right > thing' if there aren't enough preallocated pages to get down to level 3. > > It really doesn't matter either way, I'm just trying to convince myself > of the reasons why we haven't seen this explode yet :) Yeah, that's probably very unlikely to hit given the current conditions. > > > > > > > > @@ -1502,7 +1501,6 @@ static int stage2_split_walker(const struct kvm_pgtable_visit_ctx *ctx, > > > > > > > > if (!stage2_try_break_pte(ctx, mmu)) { > > > > kvm_pgtable_stage2_free_unlinked(mm_ops, childp, level); > > > > - mm_ops->put_page(childp); > > > > return -EAGAIN; > > > > } > > > > > > This, on the other hand, seems possible. There exists a race where an > > > old block PTE could have the AF set on it and the underlying cmpxchg() > > > could fail. There shouldn't be a race with any software walkers, as we > > > hold the MMU lock for write here. > > > > AF update is indeed a likely candidate. > > > > In any case, this patch looks good to me as it is, and we can always > > have a separate tweak to adjust the severity of the first case as > > required. Unless anyone objects, I'd like to queue it shortly. > > Agreed, happy with the way this looks and should've added: > > Reviewed-by: Oliver Upton > > the first time around. Thanks for that. I'll queue that shortly and send (another) PR. M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel