From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97220446BEA for ; Fri, 16 Oct 2026 08:57:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792141070; cv=none; b=la1zuv3yom0+oGFdvDvfutM7maI6GpFdbPu+gNFnGb5RcRfDyMV0vBKi7MKkIaiM0fdMGsJw9VGWPYSHxKq39nlWbajmExc7Uuk01lc8Licj6m+6x0KWr53psVrcXWbHJ1SIDPnnJ7vFqVRxWQeVSXbB/Jh/QmZDQ9Ek9Vu68Mw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792141070; c=relaxed/simple; bh=eyiaikzsxAMuyI72I6YqnzybNEsCm9SqwDP8645im7w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=L3XZTX+njSLhf4YaIoglnO4UtegcRvCznT+tPbu27V7vaSGMllYE4bdGz3pbKJ5oLS1Fy0Y5yJCbJWsBudJo6HLadkM9ehAd4n3bcGsvuGFhY/+YI/YD00+XJDOKyWCL4y0jIOfj1QME7Ar7OAD5WPGD+tKUfdQjLWR+UBm9upg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=h/Wumc78; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=27Sy7gqD; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=h/Wumc78; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=27Sy7gqD; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="h/Wumc78"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="27Sy7gqD"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="h/Wumc78"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="27Sy7gqD" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 393B71F444; Fri, 16 Oct 2026 08:57:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1792141066; h=from:from:reply-to: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=hTHE59qFKdXlGzm4a4lQOx7/fPFo6UGzecCpWrMgCi8=; b=h/Wumc78Ru80QESHNjTTcEJO785CmYuTkuT125HJa/pU+5kD1btkF6PK76g2g1hxz3AHui chRxam58glQPmxc7dcKU7CNkTrDis3Fn3XNaRWggJHkVNhE+hTBEuwVVvaoaxbGabPPCpm L0ZdRe/BEMWiNoJTEVEwRCo3Vuhzp9A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1792141066; h=from:from:reply-to: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=hTHE59qFKdXlGzm4a4lQOx7/fPFo6UGzecCpWrMgCi8=; b=27Sy7gqDeqUeA4H8viYb/lq0RsS7KbG2FHNZqTMB10CXpvDPS1qToRmpwFzFq/9bG0v99C kAaIhf4Ie7u5TDCQ== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1792141066; h=from:from:reply-to: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=hTHE59qFKdXlGzm4a4lQOx7/fPFo6UGzecCpWrMgCi8=; b=h/Wumc78Ru80QESHNjTTcEJO785CmYuTkuT125HJa/pU+5kD1btkF6PK76g2g1hxz3AHui chRxam58glQPmxc7dcKU7CNkTrDis3Fn3XNaRWggJHkVNhE+hTBEuwVVvaoaxbGabPPCpm L0ZdRe/BEMWiNoJTEVEwRCo3Vuhzp9A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1792141066; h=from:from:reply-to: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=hTHE59qFKdXlGzm4a4lQOx7/fPFo6UGzecCpWrMgCi8=; b=27Sy7gqDeqUeA4H8viYb/lq0RsS7KbG2FHNZqTMB10CXpvDPS1qToRmpwFzFq/9bG0v99C kAaIhf4Ie7u5TDCQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 2CD841339F; Fri, 16 Oct 2026 08:57:45 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id HXRvOwjn0Wo3eAAAD6G6ig (envelope-from ); Fri, 16 Oct 2026 08:57:45 +0000 Date: Fri, 16 Oct 2026 09:57:43 +0100 From: Pedro Falcato To: Baolin Wang Cc: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, qi.zheng@linux.dev, jack@suse.cz, ayushr@modal.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] mm: memory: fix truncation removing mapped folios Message-ID: References: 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: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; MISSING_XM_UA(0.00)[]; RCPT_COUNT_TWELVE(0.00)[14]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; URIBL_BLOCKED(0.00)[suse.de:email,alibaba.com:email,imap1.dmz-prg2.suse.org:helo,modal.com:email]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:email] X-Spam-Score: -4.30 X-Spam-Level: X-Spam-Flag: NO On Fri, Oct 16, 2026 at 09:48:08AM +0800, Baolin Wang wrote: > Ayush reported an issue that shmem/tmpfs folios are still mapped when > truncating them on production: > " > BUG: Bad page cache in process ... pfn:... > page dumped because: still mapped when deleted > " > > I reproduced the issue with Ayush's reproducer [1] with the latest mm-unstable > branch, and I got the following crash info. From the dump message, we can > see that truncate_inode_folio() is really trying to remove mapped folios, > which is incorrect. > > " > [ 189.171977] page: refcount:3 mapcount:1 mapping:00000000fe168ee1 > index:0x3880 pfn:0x191647 > [ 189.171995] memcg:ffff0000cc073d40 > [ 189.171997] aops:shmem_aops ino:3c01 dentry name(?):"memfd:runsc-memory" > [ 189.172006] flags: 0x17fffef0002022d(locked|referenced|uptodate|lru| > workingset|swapbacked|node=0|zone=2|lastcpupid=0x3ffff) > ....... > [ 189.172017] page dumped because: VM_BUG_ON_FOLIO(folio_mapped(folio)) > [ 189.172026] ------------[ cut here ]------------ > [ 189.172027] kernel BUG at mm/filemap.c:155! > [ 189.172057] Internal error: Oops - BUG: 00000000f2000800 [#1] SMP > ....... > [ 189.177721] CPU: 3 UID: 0 PID: 5521 Comm: repro Kdump: loaded Tainted: > 7.3.0-rc4+ #277 PREEMPT(lazy) > ....... > [ 189.183986] Call trace: > [ 189.184124] filemap_unaccount_folio+0xf0/0x1e8 (P) > [ 189.184391] __filemap_remove_folio+0x34/0x160 > [ 189.184633] filemap_remove_folio+0x4c/0xb0 > [ 189.184859] truncate_inode_folio+0x34/0x58 > [ 189.185087] shmem_undo_range+0x220/0x658 > [ 189.185308] shmem_fallocate+0x2f0/0x470 > [ 189.185527] vfs_fallocate+0x128/0x328 > ...... > [ 189.187078] el0t_64_sync+0x184/0x188 > " > > After analysis, I believe the race exists between truncation and MADV_DONTNEED, > and shmem's ->map_pages() merely makes the issue easier to reproduce. Since > MADV_DONTNEED synchronously releases the pagetable page before calling > tlb_flush_rmaps(), this could cause another thread's truncation to skip > zap_pte_range() (due to pmd is none) but still observe the folio's mapcount > as non-zero. A possible race scenario is as follows: > > CPU 0 CPU 1 > shmem_fallocate madvise_dontneed_single_vma > unmap_mapping_range ....... > ...... > (filemap_map_pages() remap folios) > zap_pte_range > truncate_inode_folio zap_empty_pte_table(pmd clear) > unmap_mapping_folio > ...... > zap_pmd_range (saw pmd none, skip) > > filemap_remove_folio > BUG_ON(folio_mapped) > tlb_flush_rmaps(remove rmaps) > > To fix this issue, we should move the pmd clear operation (via zap_empty_pte_table()) > to after tlb_flush_rmaps() and use sync_with_folio_pmd_zap() to sync with > concurrent zapping, so that when we find a pmd_none() while unmapping a folio > without holding the PTL, the folio's mappings are guaranteed to have been > removed via tlb_flush_rmaps(). > > [1] https://lore.kernel.org/all/20260925065013.3682431-1-ayushr@modal.com/ > Reported-by: Ayush Ranjan > Closes: https://lore.kernel.org/all/20260924061708.1645968-1-ayushr@modal.com/ > Fixes: 6375e95f381e ("mm: pgtable: reclaim empty PTE page in madvise(MADV_DONTNEED)") > Signed-off-by: Baolin Wang Reviewed-by: Pedro Falcato -- Pedro