Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp3007875rdb; Tue, 13 Feb 2024 04:21:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUiYvjZnz1aW5DRLVsVDfUPzFus6yV6/A0OVOpFZoRgdWLZz8CSDkeBkoB6yws7F1ORFxK915+HamYB6UGf2xRAaBEiYb1vHzBzgHijXA== X-Google-Smtp-Source: AGHT+IErsUjEiy8zIozfxzworSp4m7uigGtxsGOY8IQuz46s6Qh8k+W/XNf9oG4Bwv+0tgxVmFdc X-Received: by 2002:a05:6512:2035:b0:511:5653:16ae with SMTP id s21-20020a056512203500b00511565316aemr5894073lfs.42.1707826896943; Tue, 13 Feb 2024 04:21:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707826896; cv=pass; d=google.com; s=arc-20160816; b=iZCnmTJhrUg5XIGqjqptFDUm/sjXIGk/XhESazZsWdQFV+4SOQFcePfM/E1IETLIoV 5RMh0s+AP3iL4B9zH83LhECj4k8XG3/FLdeOB9kkpwD6a2QdqDJfDMlpQGiU6S+zuE/D yMBPxut3ExxI852auYV4mY22WIH/92nfUuXYmjimC/EF2iaQ8ppycOXO+yuGMDb0tllZ 4zj54gT4e0J6NZKVDaKlHEP29JAh0HhQUggCjYz7vrChlSNTNtzZsOLuwWXferHS7Mad bHWM57zL8gXFn8xBqwapRIs14sXDcBsWGTuyNcmpVz/11N1BaR6OD8Hz6BMiWIz5A6sZ vdng== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=SfUDZuJiGoRwJPP7X2xXLHlifZ+QAvEysoWfBhzClVA=; fh=feDsAFRmyZ2sB0X2ulOi9xr244pf2IT5FTTCAn97ots=; b=fhwGM053c2nAQ86r13ABif/0vka4jxBfe9u2tsmPBVC5kWjt17m8T0Ajy2dsT4fx9r GyZxNtAYOyPw5MbniO/3ni2l5YQGh7WkC0RorL47QCaKx9flxy9S0CYvX6u3n2f+d+z8 mxN1NYOhtrTMWJUnOTwDSbVjYlR8LuKk0BGCOKqotJboerHZIUN9PSWKIylm2cwl00vv BdrnYuyK1c91iMwx6NfhLZTv2fisy7qIvO613qZzDT4E6oepCIfSrrV2PHV5eO4TXOx+ fq/1u8f9uuUn4wzyI7ferLdeIU88Rj9PbG9hRD2EnzkFsSeJoUbTjONnTbQXwhBjJbwH xXEw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bLdx5kyF; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-63488-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63488-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; AJvYcCWgd4RQtF44LRZQn6BgEJJGB35JwoUk8Qp/ctnbG7geGorgv8qRgXzq7RyHuaTpbRw/POqg8rOQWBQgJHp40rR1rPheSzf/xMbY05y5lg== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id d15-20020a170906370f00b00a3d13f4bc7esi276762ejc.414.2024.02.13.04.21.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Feb 2024 04:21:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-63488-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bLdx5kyF; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-63488-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-63488-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 944861F29411 for ; Tue, 13 Feb 2024 12:12:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 15C8C42075; Tue, 13 Feb 2024 12:12:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bLdx5kyF" 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 394F53FE2E; Tue, 13 Feb 2024 12:12:06 +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=1707826327; cv=none; b=MB5UAAOYJFMxht1vMT0BX/f1fKIWM44KgsJA8MRkUQu6XKJODsrP1Q92NUKHR6o/TLoHnRXfaNVHdJc8zyQJvhJNzNh+L2OREWv77qK6HcvbjwFin+7MscCylswe4jzDHLVVqHMzWhcFeGJSQ/fHu8LIsJwEmwciousdZucTAPE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707826327; c=relaxed/simple; bh=ib6PZ8xXRpfgU5jIgXHVcqSo6CO9mgmFcvdOj5YPt6k=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Q1F2cDUptuSAx7RNnRX857VXbkWYqGrUgq6+cbv7U1Wgfi56LrJVmaZ8BfWwRM200vF7dQy/bvnSwKC5P8bgXlHeO8km8UGqCEAWf9NK8uVvPziIbwf0G8bv5IC1Ve90/1zm+zKnOE4HX+/h3ON7XlIwQCWQIYoCJfIHbXkYJqo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bLdx5kyF; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5B0FDC43390; Tue, 13 Feb 2024 12:12:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707826326; bh=ib6PZ8xXRpfgU5jIgXHVcqSo6CO9mgmFcvdOj5YPt6k=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bLdx5kyFQjxOp5IYua7o7cVsJmsJy2pBEm3Yy7q2STJO4bSvzi7zwLSe5tzY8sIa7 uZmcgQrvfFs6LT+qVv7aKjSDO3j45v7i6IRtRta/LiSFQfkYGHZWpTBgUdOToX/GVp y0z5d9wIQAEsjkxVZKagE/t5lap6MwcngGWPt29IafqIQH3DAZfC+WumxJRLqM6fKr nFZDvcIJQEJbEB3idooxRE2/u1w0YV513qPeqwnnUKJJe8enga9EaLfd5EGKrYwFgc vTIQPSsNLJ53oNstXGzX8ZymOi0KYRNflmALGumljUjorSn0IXqw5YoKOzA9Iv/lYA ZKXiy/ZJG2C8Q== Date: Tue, 13 Feb 2024 12:12:01 +0000 From: Will Deacon To: Marc Zyngier Cc: Oliver Upton , 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() Message-ID: <20240213121201.GA28682@willie-the-truck> References: <20240212193052.27765-1-will@kernel.org> <86cyt062jh.wl-maz@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: <86cyt062jh.wl-maz@kernel.org> User-Agent: Mutt/1.10.1 (2018-07-13) 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: > > > 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. Yes, that's probably the best approach if you're adding a Fixes tag. > > > @@ -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. Fine by me! Even though I found it by inspection, I have taken the patch for a spin to check that I (somehow) didn't break something else. Will