Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2976159rdb; Tue, 13 Feb 2024 03:12:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IFIkDMMnSGMeX6CHEzbqGfmpqBk+1xUDXcWxJZtBZUogLuCBKCoPKXMWiCxO8C4wMhqEXtx X-Received: by 2002:a05:6402:1211:b0:560:b758:5e2d with SMTP id c17-20020a056402121100b00560b7585e2dmr6461277edw.27.1707822765481; Tue, 13 Feb 2024 03:12:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707822765; cv=pass; d=google.com; s=arc-20160816; b=a77NTItYzzVQkS9+pHGoQ7cMrDzPF05E3aFkIe/13kutz8PbBQabgk8Bs8vzAjeIOn YoIk1dH9PjVnwgFMILqoFHVsQeEw3O287ejB2vm2QGH6/EBH5+SQ1wGYbfTl3Bfn1QtP AfQfD+qL7ispe19qu6+EBkAswGdzBOmuLq9FXnOtVGnDApRbyAoD9xQ1dMmj3zf79tvq JzmmVBSgoLfFsGSMbeNzp+hUkvhb+r5WVSCxhgYsXo4ky2vNRgWahKbSVa/LaNJaRP0o ax8JDsoDFlLui4L0KTGXsYT7B1joMbZGNO8+URixZu5MIPmvJExprReOBugZxUOObxAA wICQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:references:in-reply-to:subject:cc:to:from:message-id :date:dkim-signature; bh=x2gqsNpJfEF/bVD+zJ1cdAozhZbxD6NQpqA5fJDb9mo=; fh=JtOigRe557v9Uhnswyy9VX+nlWRDByui1e0eY6XE24Y=; b=Dh3svgSwGAI+tc9J61UljTWBBBrbLfkRyqd5kXvPaz92gMMwNCnPJ0ptXQaw0Yw6/3 Dgx2cbmrEfTuRv1VOxVXpRgraKZJ798T2MFIw1CUcKFkyK6SWEAWxwdickonyChc8p5Q 0JHAl6vgTETbrE64579mrQJrlUjnIEXhTEKGVaicjhE3J3ulkg94LKnMkmjAk7vyV4AV 32x2zis4QfhYZVNcIVvUPj5rRtoIy22dJrXQLL9wYLQGdFK2gjT8T/+d2wlONKifcmJX 7lkImP/BsLK3YC0zgIA+4CM8ItuSRwECjfU7lxykZqEjOuPpBeXNnDSPT+IdVwPY3Vvc QonQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Og/M57C7"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-63402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63402-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCWPjB65dL2lNhAC4MdUlCixG7XCerZY8tN5fL0uFCMExbVpnnB8hngU8RyxpvNs1d4mmfJH7flWWiC6tfxc2GhyHOnGKZrfnJQXsCZSXQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id y20-20020a056402359400b005619eea8102si2567912edc.77.2024.02.13.03.12.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 03:12:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Og/M57C7"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-63402-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63402-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 3B77F1F26E82 for ; Tue, 13 Feb 2024 11:12:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CF323364B7; Tue, 13 Feb 2024 11:12:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Og/M57C7" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA25533CD2; Tue, 13 Feb 2024 11:12:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707822758; cv=none; b=BAsQieRm2PVAcy7oNZ8CZDwDfuin8v6qR1hjQInJq67gYIEx+V1A9DM7UlDPcdcgrUqqu2k9st/vX/hjZZ0c1YT/JgmPqQjlo9NjBKSj1zMNo92ata/f1f0gaDO6Vw8eErXxLNIPbTguHnWnTVvffuaV6t2TRcCpV+hAQVcerC4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707822758; c=relaxed/simple; bh=l/AxHuvyDAvfAeJ5JccBgRd6aZPBlEBLaye5AHZE0xA=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=UFLfb215OhsBUkoPIqE0r/KpNwNdvBiPvW2/7E1+TJHnl3j6zHA0G+QFSDdaDj12gu9MNFamK4KXC6W8TtaggIqQxRJeH46tLjrsxhssu121Kd+kIVTCY3lPsxK9G70KIEJYQFrZ+VxebI6Xsj+Fj+K8+U5uoJwNmViS8hBxCBE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Og/M57C7; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DEC5C433F1; Tue, 13 Feb 2024 11:12:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707822757; bh=l/AxHuvyDAvfAeJ5JccBgRd6aZPBlEBLaye5AHZE0xA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Og/M57C7LQu00prdAWJXqpV2oMEynPTBR9XSjclZ3/8LnEMoXQWAU02JwRQexq0Q2 y9DZxcgl8ZNzRf9AeMBrg1YBvZK08E5oCCCtyqpuEaL0jJS87bHx0QV2hKdEDKQ46Z J2ruq+3i5m2PJP7glCUxu4mZCg++RxHZuO3QYYYqMsESX20dFSgUlpNMcuG/E27ccZ QFLjrT7rIvBLWwLOmqMtBUo+Zbeob6GPKWW++KWeVk9zc0UHnfnwOpvqkc7bl4oZD5 a7IJ6dOg82EIrWhuW7P0G9rMGEgLzQm15EY+C6vn0PupohGo6Wr93UpsFDArPYz2+W mxIvvTWeWYnXw== 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 1rZqiI-002kE0-TP; Tue, 13 Feb 2024 11:12:34 +0000 Date: Tue, 13 Feb 2024 11:12:34 +0000 Message-ID: <86cyt062jh.wl-maz@kernel.org> From: Marc Zyngier To: Will Deacon , Oliver Upton Cc: 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> 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) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: will@kernel.org, oliver.upton@linux.dev, 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 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? > > > @@ -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. Thanks, M. -- Without deviation from the norm, progress is not possible.