Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2670090rdb; Mon, 12 Feb 2024 12:17:03 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWWkX9E5fc+1B5mTLbEx7xc8UMuPKQ3SfRxte9F5aa8tcp0Juk53g26fgq5WepkrZUcYMSvU+KVQLQx8QLyh5MgIzMHKPJx6cwAyDnnFw== X-Google-Smtp-Source: AGHT+IFjjmKXOMnzgFzJ6/ks4EIDxFnOTiNlE+y5McUP4Q1bCFJg6l5Ta6ijqBM4Jw7V3oyzQK7a X-Received: by 2002:a17:90b:3b51:b0:296:33bc:5ed6 with SMTP id ot17-20020a17090b3b5100b0029633bc5ed6mr5635396pjb.32.1707769023414; Mon, 12 Feb 2024 12:17:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707769023; cv=pass; d=google.com; s=arc-20160816; b=vNNE8oLb6A0TWiqhYmR5aHrE3pDT2Dkuws575+glfmwlzS2bvDJ6//xgAxP/ADPN8y 5WoIkY1U3UcAx8bbK5vRSoTJIgd5cAZSRF48JRthXF5sTOwqg/3pjkV2v0vo8uVBIctw dceP+k+n/SXIZmoSP+3xPbY2dwNl/HThgT3F1yEok+JKToOcHiKMT8OkxtoZrR0ioUgd gfzk/ItfM2dnJhXSmtQfCbprghZVXID1VnlGZp+6ulK5I0eKbtYWe4FIN9RY01NUFMD5 m+9icXc8oGw0vrufGHgvPF1ocUt4UaKEnCDaLsqCQRjs0Mnh1TaXJajKkmSmfq/GN0MC pkKw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:dkim-signature:date; bh=jB6lm62DkG62rqMTYd6eJaWPvnP+AAaRdrvKn0LsuG4=; fh=e4bc1KPDm0sKSu8inSoWlNtPdKb8cQL16lItcP/tKJA=; b=HKHfTx4wpE3tUZec/YeJXboP61b1v4NOo4lzgfz2oOmhjWbV5wqNTD7YzWAutVl2AW 9hce3U6NffOC7OxFw5J18cKDqJuVVVe3epC+D+fiibYbSDNRLLDyZPVLTl0sLubEpeni AaUpAGykSXt1GiW5c1bXoo4PBClU6IPG2OVoqjCu53Jq0s3LveShr2niv0If1LJsyoim xl8vGYHgCOgVQVKtEGD9/AOhKwfKqMM7JHJ10V1ly/tl9iceLDPKFyWuATVVOvE/Yd3y Gn3ZN7XKEcBPKtzk6j2n5pxiPMyHJzCQrJxZ62Sdyt1mxuBy3ba/AbXYNI9J1Ko2BRAe dc8w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=E7WP7Tud; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-62321-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62321-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev X-Forwarded-Encrypted: i=2; AJvYcCW7qn8OotvRvKB7obb2W9CD+68H19qcrfhvk3V4Yj2oAaM+P/tCGo4onLgL1sx+XssxhkavnVOBQD7EEmgx+fFvyuQa4fnwmvJI28/ZXg== Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id st4-20020a17090b1fc400b0029701ed7d80si762917pjb.115.2024.02.12.12.17.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 12:17:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62321-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=E7WP7Tud; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-62321-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62321-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D23DB28592E for ; Mon, 12 Feb 2024 20:14:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A2C454C61C; Mon, 12 Feb 2024 20:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="E7WP7Tud" Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (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 511E34C604 for ; Mon, 12 Feb 2024 20:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707768886; cv=none; b=nJfHHCsZt5SOG+U0uRQVRbLaNZiVTVsTk5XE9rpN0BBdwIXIetshe/byPfCdUC69pJuzGhnuCGNFXltiG6VmHqYQn8WEW58IMvoyiO8KMPKoyzo4GjPNeZlvW7s4DEhuGDgX0FX8pDY6K6ZXS1enGZ5IP4QVyx06sR2yFn/ShIs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707768886; c=relaxed/simple; bh=pM+JwIpEhS4K66fk0hub7cdKHekvQSrc3X6VYgClJgk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cDoO306hST8dbm/aTJFfJsXjUfqUKh+NzrWld6wXDA+txjaxYXkzSp2OTDz7hLvhWiTN9myi/jSzKQvpxQkTZmyF3y0dRdNNSXwbI0PRnj2GuK31UL3iTQQe8bF1b8oG5O9Qu7A/nTjDj7nQZlRIuJ7+i6ySIi3aGuHR9bZ9hG8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=E7WP7Tud; arc=none smtp.client-ip=91.218.175.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Mon, 12 Feb 2024 20:14:37 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1707768881; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jB6lm62DkG62rqMTYd6eJaWPvnP+AAaRdrvKn0LsuG4=; b=E7WP7TudAWDuX/Ylql0OZ0KhRkLPhK6IXL+TZpzCPqtTJWDHcDM7uArNyMKfZdKsKzBdo3 CoTUrrTt78lNuCY1M/7DWdd50K+WUnam2PwOiMsb5+OxsAGX+FWB6NdVIV0bGwCLbbhnWe WKpu0G1iGNQGkGx2h4Cu+Faewh8N00c= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Will Deacon Cc: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Marc Zyngier , Ricardo Koller Subject: Re: [RFC PATCH] KVM: arm64: Fix double-free following kvm_pgtable_stage2_free_unlinked() Message-ID: References: <20240212193052.27765-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240212193052.27765-1-will@kernel.org> X-Migadu-Flow: FLOW_OUT 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()") > > 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(). > @@ -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. -- Thanks, Oliver