From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793181371; b=QFLcWseqNME6htGTNxeYBBEnr1GFD3ZMmO4ewTkYhiQehWinCHjyU5uMcJodyV+2KI3C FstKJIRYZe603WrsdlGAhOhIOHWJ+IfQTJk2GEgZSbeX+IFXPRHzz0CkX1g/KUuzf0H6B R0pNM+ZEFahOxcy0quH1bGGbwRBaSwou6An5FhxUVUOvCVTE5+j2P5jEPBtnJxZ0i+Vl3 2oXnDiBc4JANfONOTR6Xx8tFPa0w/9yEpdBbgK5M2f+lq6QGfu7gh/6wyd1MEJCOJzYvg WDmgq5AWY+yEwrXcEJO9+T8830i4/beSI3U4Vy5LcFwOKxh0PitPwCYpE65E+5E7Xng== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793181371; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=p9j3nRPXAYyZeyCseSaJF4UAcNQcHHRM87quWK0Zl5E=; b=WgL9F7HZYXotF+oo5srXs5PehgZ7x7nSzoAwqH7S0MVNoeFOYrSrujWwyQlZCxuKgGSa RZOQUbOAviajBGy3WiaxbWczJ6T/k7DNPUY/4maEO2y//DzqHQ4Vvv3pFps1p2q4w1giU Z1NFGi2ov6BTse2ynFBr5UF8zg0HfGTWmcsQ2aTp4FYwtAbIW4LdIwh8BehuZbHV/Rh7e oKAol/swnoCM79svt3lk/b2wrlraGyXNwptUYFj0LUe9PqRGOwgeNRptxig1r+YzB3Q5O 1Hnf0zoIn5yQ6Q4cUngy8ULrKlzPyiA6QH2+IGIce6EsH5EyX+6rGC4WDCJibtAM2ZQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=H+LOYwYD; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED0C7606A320; Wed, 28 Oct 2026 09:56:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=H+LOYwYD Received: by smtp.kernel.org (Postfix) with ESMTPSA id 626B31F01825; Wed, 28 Oct 2026 09:56:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793181370; bh=p9j3nRPXAYyZeyCseSaJF4UAcNQcHHRM87quWK0Zl5E=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=H+LOYwYD68mgaDcU1XuGGCbEvC2jziJLo6mDaI2/lqJvoI/K7k5xh2HaET6OQf8UJ zuxOtfH5V9+r+h9zC3gYL7+kTwjYLfQD9BGwQwx3TmfJsKi/bJnSW64SM90w8SZR8w 8GPG+BszKgBo+IflOgBS6ox0Phsd6d6nlffU4kPstFuSlORrvYwuO27TSvVEv3FZk/ 5yB7i4KEBaMKPoE67Xxu62sMmlm6JTmQT4wexklFTg3UlQMNacEQDIc3ZYM/NYTEFq 7LTOEobWWyDMtFWiDeh9+lihoT3/dSjTJNyc8iFKF64LedHq/xibuWHSBLSyCuwNHV trUixsDor0PeQ== From: "David Hildenbrand (Arm)" Date: Wed, 28 Oct 2026 10:55:04 +0100 Subject: [PATCH v3 06/15] mm/shmem: hold off eviction in shmem_writeout() X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261028-fixes_shmem-v3-6-4ce3baf2db4b@kernel.org> References: <20261028-fixes_shmem-v3-0-4ce3baf2db4b@kernel.org> In-Reply-To: <20261028-fixes_shmem-v3-0-4ce3baf2db4b@kernel.org> To: Andrew Morton , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Johannes Weiner , Vladimir Davydov , Michal Hocko , Hugh Dickins , Baolin Wang , Lukas Czerner , Carlos Maiolino , Christian Brauner , Jan Kara , Daniel Verkamp , Jeff Xu , Muchun Song , Oscar Salvador , David Stevens , "Matthew Wilcox (Oracle)" , William Kucharski , Chris Li , Kairui Song , Nhat Pham , Miaohe Lin , Naoya Horiguchi Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, "David Hildenbrand (Arm)" , Josef Bacik , stable@vger.kernel.org, Pedro Falcato , "Kiryl Shutsemau (Meta)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=5605; i=david@kernel.org; h=from:subject:message-id; bh=zzh9IGJps/yyCZLRquCkr7f7izRh7fedg/TKI2tiALY=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIeHms845jhvnFy8V6DTPGX1RO2hW8tWhko8cwpdub3Z AmxN/vaO0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZhIoTsjw3Qn9oXHThR7TVh4 Yc36/Edt7T0zEncs4Y6crvuwnu9LiRcjw9tDDofr3xXr2CtxbrVbMp9tVkZxXeXMa1eETpRu1Qy TYwAA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Josef Bacik Once shmem_writeout() has moved the folio from the page cache to the swap cache, the folio lock no longer keeps the inode alive: truncation and eviction find a swap entry, free it and do not wait for anybody. That did not matter as long as swap_writeout() was the last thing the function did. But since it puts the folio back when swap_writeout() returns AOP_WRITEPAGE_ACTIVATE, it uses the mapping and the inode afterwards: reclaim last close of an unlinked file shmem_writeout() shmem_delete_from_page_cache() swap_writeout() zswap_store() fails shmem_evict_inode() writeback is disabled shmem_truncate_range() return AOP_WRITEPAGE_ACTIVATE shmem_free_swap() destroy_inode() ... RCU grace period, freed shmem_add_to_page_cache(mapping) xas_lock_irq() <- use after free swap_writeout() behaves that way if zswap is enabled, cannot take the folio and the memcg or one of its ancestors has memory.zswap.writeback set to 0. shmem_unuse() already added infrastructure to deal with that. Reuse info->stop_eviction to protect the inode during writeout. Increment it before removing the folio from the page cache, and decrement it after swap_writeout() and any attempted reinsertion and inode accounting. In shmem_evict_inode(), wait for the counter to reach zero even if swapoff has already removed the inode from the swap list. Add some helpers to make the code easier to digest. Fix generated by an LLM and sent by Josef Bacik in private. Fixes: 6344a6d9ce13 ("mm/shmem: writeout free swap if swap_writeout() reactivates") Cc: stable@vger.kernel.org Signed-off-by: Josef Bacik Acked-by: Pedro Falcato Acked-by: Kiryl Shutsemau (Meta) Reviewed-by: Nhat Pham Reviewed-by: Baolin Wang Tested-by: Baolin Wang [ david: code cleanups; improve description ] Tested-by: Gregory Price (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/shmem.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index e3e4bbbe1d80..eddd5870be07 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1616,6 +1616,17 @@ static int shmem_setattr(const struct mnt_idmap *idmap, return error; } +static void shmem_disallow_eviction(struct shmem_inode_info *info) +{ + atomic_inc(&info->stop_eviction); +} + +static void shmem_allow_eviction(struct shmem_inode_info *info) +{ + if (atomic_dec_and_test(&info->stop_eviction)) + wake_up_var(&info->stop_eviction); +} + static void shmem_evict_inode(struct inode *inode) { struct shmem_inode_info *info = SHMEM_I(inode); @@ -1628,16 +1639,21 @@ static void shmem_evict_inode(struct inode *inode) mapping_set_exiting(inode->i_mapping); shmem_truncate_range(inode, 0, (loff_t)-1); shmem_unused_huge_del(inode); - while (!list_empty(&info->swaplist)) { - /* Wait while shmem_unuse() is scanning this inode... */ + /* + * Writeout may still use the inode after swapoff has removed it + * from the swaplist, so always wait at least once. + */ + do { wait_var_event(&info->stop_eviction, !atomic_read(&info->stop_eviction)); + if (list_empty(&info->swaplist)) + break; spin_lock(&shmem_swaplist_lock); /* ...but beware of the race if we peeked too early */ if (!atomic_read(&info->stop_eviction)) list_del_init(&info->swaplist); spin_unlock(&shmem_swaplist_lock); - } + } while (!list_empty(&info->swaplist)); } simple_xattrs_free(&sbinfo->xa_cache, &info->xattrs, sbinfo->max_inodes ? &freed : NULL); @@ -1776,15 +1792,14 @@ int shmem_unuse(unsigned int type) * remove placeholder inode from swaplist, nor let it be freed * (igrab() would protect from unlink, but not from unmount). */ - atomic_inc(&info->stop_eviction); + shmem_disallow_eviction(info); spin_unlock(&shmem_swaplist_lock); error = shmem_unuse_inode(&info->vfs_inode, type); cond_resched(); spin_lock(&shmem_swaplist_lock); - if (atomic_dec_and_test(&info->stop_eviction)) - wake_up_var(&info->stop_eviction); + shmem_allow_eviction(info); if (error) break; if (list_empty(&info->swaplist)) @@ -1915,6 +1930,13 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, spin_unlock(&shmem_swaplist_lock); } + /* + * Once the folio has left the page cache, its lock no longer + * keeps the inode from being evicted and freed: hold eviction + * off, in case the folio has to be put back below. + */ + shmem_disallow_eviction(info); + folio_dup_swap(folio, NULL); shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap)); @@ -1922,6 +1944,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, error = swap_writeout(ctx, folio); if (error != AOP_WRITEPAGE_ACTIVATE) { /* folio has been unlocked */ + shmem_allow_eviction(info); return error; } @@ -1938,6 +1961,7 @@ int shmem_writeout(struct swap_io_ctx *ctx, struct folio *folio, shmem_recalc_inode(inode, 0, -nr_pages); folio_put_swap(folio, NULL); } + shmem_allow_eviction(info); /* * The swap_cache_del_folio() below could be left for -- 2.43.0