Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 8DC3048B38C for ; Tue, 25 Aug 2026 17:27:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787678847; cv=none; b=sUSfBJUh1aX+IdYFO3uGmFm9wYOvGKeY1d9LtoXUSXPt4iURPBvdHjr+PSUdpzhDU4hWETfLbe3YfnlZHVFcGyQpF+gRZ1ZdckzfgXlxOwJs1dIq8qvsp04YLjNL8xm2j3GhkRyLHwP1ImMqBPUw/v4owaTVZ9Eja0tN/npNWGo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787678847; c=relaxed/simple; bh=rlaWMxBw9Jb5y9mNHhiJxGF5ZHVAc0dM2exslR4pe8I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HtgF4/w2dhBQv60rA4zMOglJD7HXEI2S+AV43BZl6nqyfZnRZbjMzRZQFyf+l1W85h/5v9RSi0adg4o1q/hCWyVfc1BBidyUatfCWLCG8e0GAHIYqefI6EWbDKPuKC88h/i7e92h/i9EfIQOnr4SJLgJCdZgQ93pzcTPDrd3MHY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id EDEAF3EE41; Tue, 25 Aug 2026 17:27:13 +0000 (UTC) From: Alexandre Ghiti To: Johannes Weiner , Yosry Ahmed , Nhat Pham , Chengming Zhou , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Hugh Dickins , Baolin Wang , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Joonsoo Kim Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Alexandre Ghiti Subject: [PATCH v3 1/3] mm/swap: move LRU insertion out of the swap cache allocator Date: Tue, 25 Aug 2026 19:24:51 +0200 Message-ID: <20260825172604.3243589-2-alex@ghiti.fr> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260825172604.3243589-1-alex@ghiti.fr> References: <20260825172604.3243589-1-alex@ghiti.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-GND-Sasl: alex@ghiti.fr X-GND-Cause: dmFkZTGHWaeQor2XdghODYIizE0whc46SNMKvGLs5Ra4yOK+xpbqr4c3E/nFsazkd1XSSmwRHUwmmf/KPoOvyH5LiUDkGDLlWpaawqlR71TWKaTBuWmm9QefDgCYeZjOaHijyVfpjVjVK3lMlkqW5DbNiHpv9SXk8x0vCc5F7n6cybwIwvm55phmNpro5xdoAZx9zHyXtWN3FohIlrlG38PzZqwXMRXg/+FZUA4sNrgS2eQ6wgxdtKygivvS7GtkYwI4pogEPtjlQYDsX2EIZEahoTVLrM05Uo9YF1Oc2TCDCJXhvy4cga0VexTek6+4p0SodNxXDEsBCQY1VwNT3556twJLITgVUBGINoxNbMdNKJLxWvQltxzBw8zdTI6ljNVEhD3XLRpCxeTqRSkqAoX/2U8/Z56IYEJpor7cPskqxqzfHXEMzvPX8h/cFlV+CxaNPd9+cb6V789kz6mHliTTasP8mnpduYEtH4emdQ0eZxkbNu+hWxGkP7p2lHfWnsDxfSSL2mu3HeMRZlROIki1hL7C4uZti3trdLhGrY8eyQJP+oEQxFxgMwio/83j0VyCQfyqc/XKBw0cDWfCYuulsiPCXlW14udrFxcIUJiFYu4KAa5YaKYmSPUlk9o3poVGyoC47XnFxz7RfWnBrNPWwrjGScsRDpmqpKfbh96zt6sYlQ X-GND-State: clean X-GND-Score: -100 This is a preparatory patch. __swap_cache_alloc_folio() adds the new folio to the LRU itself, which leaves its callers no way to act on the folio before it becomes visible to reclaim. Two users need exactly that: - the next patch moves the refault evaluation out of the swap cache folio allocation, and it has to happen before folio_add_lru(): that consumes PG_active to file the folio on the inactive or the active list, and under MGLRU it also reads PG_workingset to pick the generation. Setting either flag afterwards does not move the folio; - the upcoming zswap writeback dropbehind implementation needs the buffer folio to stay off the LRU entirely, as the per-CPU LRU batch would hold a reference on it and keep remove_mapping() from freeing it once writeback completes. Defer the LRU insertion to the callers of __swap_cache_alloc_folio(): each of them adds the folio right after the allocation, so there is no functional change intended. Suggested-by: Kairui Song Signed-off-by: Alexandre Ghiti --- mm/swap.h | 6 +++--- mm/swap_state.c | 20 ++++++++++++-------- mm/zswap.c | 5 +++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index 90a551a88df6..8679cb61268e 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -312,9 +312,9 @@ bool swap_cache_has_folio(swp_entry_t entry); struct folio *swap_cache_get_folio(swp_entry_t entry); void *swap_cache_get_shadow(swp_entry_t entry); void swap_cache_del_folio(struct folio *folio); -struct folio *swap_cache_alloc_folio(swp_entry_t target_entry, gfp_t gfp_mask, - unsigned long orders, struct vm_fault *vmf, - struct mempolicy *mpol, pgoff_t ilx); +struct folio *__swap_cache_alloc_folio(swp_entry_t target_entry, gfp_t gfp_mask, + unsigned long orders, struct vm_fault *vmf, + struct mempolicy *mpol, pgoff_t ilx); /* Below helpers require the caller to lock and pass in the swap cluster. */ void __swap_cache_add_folio(struct swap_cluster_info *ci, struct folio *folio, swp_entry_t entry); diff --git a/mm/swap_state.c b/mm/swap_state.c index b76eb3d876fd..bf8ff2d2dbf1 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -489,13 +489,11 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages); lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages); - /* Caller will initiate read into locked new_folio */ - folio_add_lru(folio); return folio; } /** - * swap_cache_alloc_folio - Allocate folio for swapped out slot in swap cache. + * __swap_cache_alloc_folio - Allocate folio for swapped out slot in swap cache. * @targ_entry: swap entry indicating the target slot * @gfp: memory allocation flags * @orders: allocation orders, must be non zero @@ -507,13 +505,17 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, * doing IO (e.g. swap in or zswap writeback). The swap slot indicated by * @targ_entry must have a non-zero swap count (swapped out). * + * The returned folio is locked and is NOT on the LRU. The caller must either + * add it to the LRU with folio_add_lru() so page reclaim can find it, or free + * it directly once done; a folio left off the LRU is unreclaimable and leaks. + * * Context: Caller must protect the swap device with reference count or locks. * Return: Returns the folio if allocation succeeded and folio is in the swap * cache. Returns error code if failed due to race, OOM or invalid arguments. */ -struct folio *swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp, - unsigned long orders, struct vm_fault *vmf, - struct mempolicy *mpol, pgoff_t ilx) +struct folio *__swap_cache_alloc_folio(swp_entry_t targ_entry, gfp_t gfp, + unsigned long orders, struct vm_fault *vmf, + struct mempolicy *mpol, pgoff_t ilx) { int order, err; struct folio *ret; @@ -649,12 +651,13 @@ static struct folio *swap_cache_read_folio(struct swap_io_ctx *ctx, folio = swap_cache_get_folio(entry); if (folio) return folio; - folio = swap_cache_alloc_folio(entry, gfp, BIT(0), NULL, mpol, ilx); + folio = __swap_cache_alloc_folio(entry, gfp, BIT(0), NULL, mpol, ilx); } while (PTR_ERR(folio) == -EEXIST); if (IS_ERR_OR_NULL(folio)) return NULL; + folio_add_lru(folio); swap_read_folio(ctx, folio); if (readahead) { folio_set_readahead(folio); @@ -690,12 +693,13 @@ struct folio *swapin_sync(swp_entry_t entry, gfp_t gfp, unsigned long orders, folio = swap_cache_get_folio(entry); if (folio) return folio; - folio = swap_cache_alloc_folio(entry, gfp, orders, vmf, mpol, ilx); + folio = __swap_cache_alloc_folio(entry, gfp, orders, vmf, mpol, ilx); } while (PTR_ERR(folio) == -EEXIST); if (IS_ERR(folio)) return folio; + folio_add_lru(folio); swap_read_folio(&ctx, folio); swap_read_submit(&ctx); return folio; diff --git a/mm/zswap.c b/mm/zswap.c index 37f34e406c8e..0d2efe21f18a 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -1001,8 +1001,8 @@ static int zswap_writeback_entry(struct zswap_entry *entry, return -EEXIST; mpol = get_task_policy(current); - folio = swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol, - NO_INTERLEAVE_INDEX); + folio = __swap_cache_alloc_folio(swpentry, GFP_KERNEL, BIT(0), NULL, mpol, + NO_INTERLEAVE_INDEX); put_swap_device(si); /* @@ -1014,6 +1014,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, */ if (IS_ERR(folio)) return PTR_ERR(folio); + folio_add_lru(folio); /* * folio is locked, and the swapcache is now secured against -- 2.53.0-Meta