From mboxrd@z Thu Jan 1 00:00:00 1970 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 C03DF3D7D87 for ; Wed, 18 Mar 2026 13:29:09 +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=1773840549; cv=none; b=JGQZeMbTvMeYyF+8BjK/MbmfT0RDV9e9ya96w2Cd2XPxEuaA6f75aKwWfDBjtYOKBiUWHyex7WaNNEfs+CzxnmVNRtv0DzF92YWMdcYfUJ+eHpSHT9gwX0akHaBACaHSUNO01Psypc6gWyFrv49fkFOGP8ZCetWcB5chZSK5eeU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773840549; c=relaxed/simple; bh=laWeUr6lgGK+Rk6Mc1+rANAvR/Y/XEHDielWETb3IvY=; h=Message-ID:Date:MIME-Version:From:Subject:To:Cc:References: In-Reply-To:Content-Type; b=RpM0YJ1uP6H3fi5qkfFUDhm+sJiIjiaEMLZtMfJhG0GYaUWawwO2TjTTIlk3sNZkwNNiwVu0Ohx+35DZBKsypnaAzdUI5xYzfnmqKNz2In//QtVgznq/M/J7QZn8JR22if13PdifGJoIwRcdCDdPHFpe1xwF7xvINHISqMHdEAQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QM8LHZaY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QM8LHZaY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44B4AC19421; Wed, 18 Mar 2026 13:29:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773840549; bh=laWeUr6lgGK+Rk6Mc1+rANAvR/Y/XEHDielWETb3IvY=; h=Date:From:Subject:To:Cc:References:In-Reply-To:From; b=QM8LHZaY1JS5ior7T24ylVC+SfrKbaRRr9RndQJ/GaeqA86du7OIy4nn5rz/XdKVg oK7ELaw5x/ekqMbVtLDN70088oycqLIlgoAeLI0JsvQrcMiIcpWiGR7baN/UHNw5fF IQMcloccssqd1jFxs/QA0pV2dnvknoHWQ4f/dIzPf42MATTRIEuU3o3P9NsNpeh7VF YAG6wfZSl7TeDCqsmsgi7L++qVMWtadq6saRkflfAaZNASWay3nz0WHl1fOaDWx3mW w9ntK3Ds7QJMGt+4QU94UkS5KC6u1U52bwCHQg1P3cLSxxssLpPsQy7myDZic7GliK I5dBnLxbAt/7g== Message-ID: <63f4a898-5f12-4496-89a5-f7e8df1227ef@kernel.org> Date: Wed, 18 Mar 2026 14:29:05 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Vlastimil Babka Subject: Re: [PATCH mm-hotfixes] mm/rmap: clear vma->anon_vma on error Content-Language: en-US To: "Lorenzo Stoakes (Oracle)" , Andrew Morton Cc: David Hildenbrand , Rik van Riel , "Liam R . Howlett" , Harry Yoo , Jann Horn , Sasha Levin , Jiakai Xu , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20260318122632.63404-1-ljs@kernel.org> In-Reply-To: <20260318122632.63404-1-ljs@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 3/18/26 13:26, Lorenzo Stoakes (Oracle) wrote: > Commit 542eda1a8329 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() > comments, add asserts") alters the way errors are handled, but overlooked > one important aspect of clean up. > > When a VMA encounters an error state in anon_vma_clone() (that is, on > attempted allocation of anon_vma_chain objects), it cleans up partially > established state in cleanup_partial_anon_vmas(), before returning an > error. > > However, this occurs prior to anon_vma->num_active_vmas being incremented, > and it also fails to clear the VMA's vma->anon_vma field, which remains in > place. > > This is immediately an inconsistent state, because > anon_vma->num_active_vmas is supposed to track the number of VMAs whose > vma->anon_vma field references that anon_vma, and now that count is > off-by-negative-1 for each VMA for which this error state has occurred. > > When VMAs are unlinked from this anon_vma, unlink_anon_vmas() will > eventually underflow anon_vma->num_active_vmas, which will trigger a > warning. > > This will always eventually happen, as we unlink anon_vma's at process > teardown. > > It could also cause maybe_reuse_anon_vma() to incorrectly permit the reuse > of an anon_vma which has active VMAs attached, which will lead to a > persistently invalid state. > > The solution is to clear the VMA's anon_vma field when we clean up partial > state, as the fact we are doing so indicates clearly that the VMA is not > correctly integrated into the anon_vma tree and thus this field is invalid. > > Reported-by: Sasha Levin > Closes: https://lore.kernel.org/linux-mm/20260302151547.2389070-1-sashal@kernel.org/ > Reported-by: Jiakai Xu > Closes: https://lore.kernel.org/linux-mm/CAFb8wJvRhatRD-9DVmr5v5pixTMPEr3UKjYBJjCd09OfH55CKg@mail.gmail.com/ > Fixes: 542eda1a8329 ("mm/rmap: improve anon_vma_clone(), unlink_anon_vmas() comments, add asserts") > Signed-off-by: Lorenzo Stoakes (Oracle) Acked-by: Vlastimil Babka (SUSE) Thanks! > --- > mm/rmap.c | 7 +++++++ > 1 file changed, 7 insertions(+) > > diff --git a/mm/rmap.c b/mm/rmap.c > index 6398d7eef393..abe4712a220c 100644 > --- a/mm/rmap.c > +++ b/mm/rmap.c > @@ -457,6 +457,13 @@ static void cleanup_partial_anon_vmas(struct vm_area_struct *vma) > list_del(&avc->same_vma); > anon_vma_chain_free(avc); > } > + > + /* > + * The anon_vma assigned to this VMA is no longer valid, as we were not > + * able to correctly clone AVC state. Avoid inconsistent anon_vma tree > + * state by resetting. > + */ > + vma->anon_vma = NULL; > } > > /** > -- > 2.53.0