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 89E8C3D332C for ; Wed, 18 Mar 2026 11:55:45 +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=1773834945; cv=none; b=GuG4Fd+kPV4NWVxVCgYh4pfVrNamcVgZBXkNVyw+50ITktr/JtMMjAMXvKqzdqoNO/ncOJUc46vWpuaKCiXuF1Dr6artKF9viT65EmsUwgsokIyD4stq2vHipDTjubleTCr8wbfthzWHvELGQZ7dqrNCk6fj+Zijacb22zsfvwM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773834945; c=relaxed/simple; bh=bpccDuS6OLTp8o1NwaEwGiI7WGiKxLq+rged8ze6+KM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=DMp0zpz7RiYDoKeqmezi7on+zlrWe40Obdvv8DVnKTt4iptIbzu/5qXGxbTi5iKFgfJfUekoYX8qynGWGXn4eR5IAazb5gDQr+TjuomdLeKs3+qzOK3s0gUt7WBwND3dUlXZwtsv6NYxjRxRXL0CnNEilIEUCufRiccTYVusw1A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bd8FpXyB; 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="bd8FpXyB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9016C19421; Wed, 18 Mar 2026 11:55:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773834945; bh=bpccDuS6OLTp8o1NwaEwGiI7WGiKxLq+rged8ze6+KM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bd8FpXyBrIGAVDD+7UEAwyGSFXaMeO9Ubxc5dc2TijU5DljuY69dYoF0K0oHB35ee FBeI8fpbd355KkrEUNjB7XnTAi+Zh121TBfwp1S1cBRPN+qo678evIrOIm/s2cXvtF RXJbKG7pCCTdHfjNfZzt4wz5fBid1TpOteNZlrJAgVSO639qw5gW/uYAvCIddbo6za V99s08wo0UtWM8nvFXzoYKiigirfBEQNsZagCv6IlOPhSps0NZz2T8bV7exWZc1eQM WScNEWMcHvLswh3NQvqglZ3qqi/U5fpkK/AsODfW+xj3WSeEEXcgvrwXwkc6Kh9f5o wedCPqF21sgeg== Date: Wed, 18 Mar 2026 11:55:42 +0000 From: "Lorenzo Stoakes (Oracle)" To: Jiakai Xu Cc: LKML , linux-mm@kvack.org, Liam.Howlett@oracle.com, akpm@linux-foundation.org, david@kernel.org, harry.yoo@oracle.com, jannh@google.com, riel@surriel.com, vbabka@kernel.org, Sasha Levin Subject: Re: [BUG] WARNING in unlink_anon_vmas() Message-ID: References: <12501051-98d4-479a-8f10-547b2c08ad59@lucifer.local> 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: <12501051-98d4-479a-8f10-547b2c08ad59@lucifer.local> +cc Sasha On Wed, Mar 18, 2026 at 10:59:33AM +0000, Lorenzo Stoakes (Oracle) wrote: > (-cc old email) > > On Wed, Mar 18, 2026 at 06:42:49PM +0800, Jiakai Xu wrote: > > Hi all, > > > > While fuzzing the KVM subsystem on RISC-V, I stumbled upon a kernel WARNING > > that triggers in unlink_anon_vmas(). > > Thanks! > > Will have a look at this. > > > > > WARNING: mm/rmap.c:528 at unlink_anon_vmas+0x562/0x768 mm/rmap.c:528 > > unlink_anon_vmas+0x562/0x768 mm/rmap.c:528 > > Assuming there's not some big mismatch with kernel versions this is: > > VM_WARN_ON(anon_vma->num_active_vmas); OK so this _was_ reported by Sasha (via an AI assessment), but it completely dropped off my radar sorry about that! https://lore.kernel.org/linux-mm/20260302151547.2389070-1-sashal@kernel.org/ I want to fix this a slightly different way though. SO what's happening is in dup_anon_vma() we do: static int dup_anon_vma(struct vm_area_struct *dst, struct vm_area_struct *src, struct vm_area_struct **dup) { ... dst->anon_vma = src->anon_vma; ret = anon_vma_clone(dst, src, VMA_OP_MERGE_UNFAULTED); if (ret) return ret; <-- fault injection error here *dup = dst; <-- NOT set ... } Then: int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src, enum vma_operation operation) { ... list_for_each_entry(pavc, &src->anon_vma_chain, same_vma) { avc = anon_vma_chain_alloc(GFP_KERNEL); <-- FAULT INJECTED HERE if (!avc) goto enomem_failure; <-- so we do this anon_vma_chain_assign(dst, avc, pavc->anon_vma); } ... if (operation != VMA_OP_FORK) dst->anon_vma->num_active_vmas++; <-- does NOT get run ... enomem_failure: cleanup_partial_anon_vmas(dst); return -ENOMEM; } We only clear down the partially allocated anon_vma_chain objects: static void cleanup_partial_anon_vmas(struct vm_area_struct *vma) { struct anon_vma_chain *avc, *next; list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { list_del(&avc->same_vma); anon_vma_chain_free(avc); } } But, crucially, dst->anon_vma is LEFT IN PLACE. So on process exit, we call into unlink_anon_vmas() for that VMA: void unlink_anon_vmas(struct vm_area_struct *vma) { ... struct anon_vma *active_anon_vma = vma->anon_vma; <-- is SET ... /* Unfaulted is a no-op. */ if (!active_anon_vma) { <-- is not called VM_WARN_ON_ONCE(!list_empty(&vma->anon_vma_chain)); return; } ... active_anon_vma->num_active_vmas--; <-- Incorrect (*) ... list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) { ... VM_WARN_ON(anon_vma->num_active_vmas); <-- triggers put_anon_vma(anon_vma); ... } } * If anon_vma->num_active_vmas was 0, it underflows, but otherwise it'll get decrement one time too many, and so will _eventually underflow_ guaranteed and trigger the bug for for VMAs associated with this anon_vma. The fix is to set vma->anon_vma = NULL in this situation, which I think is best done in the cleanup code as I said at https://lore.kernel.org/linux-mm/a709c736-fd76-4bc9-a1d2-e1351742b321@lucifer.local/ but then... didn't do as it dropped off my radar (oops!) Will send a fix + cc, attribute Reported-by etc., thanks very much for reporting this Jiakai, was very useful! Cheers, Lorenzo