From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335234; b=vOVfDKmSsdCSDZKhh9TOHG9y0Wkq7K5UFzeOVgxGa7zUWDIe/Nlz2BBJbMweGPTJRMu7 tG7XY9t09/axNr3m3mK8aE7CIbX/wUYIJwuIKwIsnz/qtQ+ebuCvX69IzLzsP7m6TmXC9 ivi00VdCB3RJgs0K6q02bMFEG7eDlPKW/n8iHJblBH1Um2BXI2x55ToMaVGyhSjacZ4du XFlzFtjTVSNRJdVM6jigYJ1RBYwY0hP1b3TbXG/Y67mqCcLR7nwkOyl95wwYs9+3SfPiZ pqNSV6bJulsa73ylnLf+eWkWhz5VcTpWo5IOTZmKIGUkxdZiQeg+LhrFXTrl7bTNSoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335234; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=mP5FG6Li9kuUQwaa9Kz69I2SMNmiIlspwXx0FM9mheg=; b=kwQAeYXtO52nSo58ZiU7Jgnl2UqBYp32knopdL15+y5KkRsA+ZvRJk50DSASkicM7Rww wUn1Ckl5SxsKw/VSS/KAQfkKu90MZJ7hYId/JRy9/D0NK28azUZe4xxToiOvBOrazIkLr MNnjvK0u1g1gM8UEUQteIengdPeZvlliG+LZdrXj3STIvBvMO9jaTMCkg2Vft9t05d8a0 lwc1bjVY7RgzJzhhc7opwg9t8Utqwc1LJwJgA0XrSmVqW8lY6TTB5wEyzqk+m6v0YQ/jw BAd6ctKP5N0CphZ8q7JteKUQE+Zey5EMqVRmwJBpKuXXdvkbV4bQmp5TbtO6N8QCChA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lSHj65t6; arc=none smtp.remote-ip=209.85.128.175 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6881C606A689 for ; Thu, 3 Dec 2026 22:00:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6881C606A689 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lSHj65t6 Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-8b502b9b702so193397b3.3 for ; Thu, 03 Dec 2026 14:00:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335233; x=1796940033; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=mP5FG6Li9kuUQwaa9Kz69I2SMNmiIlspwXx0FM9mheg=; b=lSHj65t6X6JoBdCnRV57duvWpFkmDdlkqaRe7BQhhZQqeU5BD1eU98piLeyPc8i9PY j20JNET3ra8GI7FZoJe5GP6u6DmEXTRkSTUOzlKvjHY62UioWei4PehWfmHAYkwZups5 Q4Y+VXF4faSdfgPmXzMnBmd8GBPjavnM/knR8al9vJ+12zlka6H4euV3qpgxr70tpJAD RUVn2PvQMOCfExpZy74JGq79FSIhv6d9wMscyPPkjjv3pI354RUwMWvGSGs5xKq8k06Q d0msiGPCQ5xNyQHe+LV7Worlhx5BzThU+dLafcqUwXof1t9jHebGN+26uXqo0MTZHnHd OW8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335233; x=1796940033; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=mP5FG6Li9kuUQwaa9Kz69I2SMNmiIlspwXx0FM9mheg=; b=suuFHXwhbjQirUI2aKDre0Sa2Ob82FzfUYWXCLLw6X8LOE55Ox8cwUdFWJ1K4e6v2/ AKzcSg1QDmmdMt4xApFc00szGdFvgDEg9lgjXNwE+vHV5cKoWo+BW037Bsq6jWs3njVn 99+ZXwity2Rp0fhJNOWcXtFoAa0kfWiltS5sTRGNrb+r9cDpejFMchLmg5KCDUk+i90H sjeNiILtlR81y6n2lVvGEusic1v4/Acx/wOSpNnfw0/l7LMpZYODqZeLDEC7abqXFq83 goWd/3ZM/YhZKFzwPc2SkTPkNqZ/J90pkbCjPlZZlYtW4v+f3k0hqsosvJhxSfmdNfIE MK4A== X-Forwarded-Encrypted: i=1; AL+hErEGt6b4pgpOqpcoZOGcc7K8iPDOxcLhX6A9i3W1iJytsGe4s1R1+NUGVqT4/67BCzLMFvc+7cHqDkA=@vger.kernel.org X-Gm-Message-State: APuU4Z+rzGYm5Bgfy4HNOiCJ/XLYSAloqy7qmrDjpkTI9n90c+65hQDm zTFyBA/DhEO6M+995e2j0N2WmeQ1fU5aMPT9xbyU/JQ5S+swvTRx/EME X-Gm-Gg: AZFdTZcmDwO6JRDvicyH3mKbmWA1RtOEomS3yVMIMnzr2UyvFyZAHFHANV4Rghe9Jog Odlwzp0kRYIsTHODzJQqmq+oML5khUMCY1q1Nr0VxVnkdjCpCOa85kNRzd5gJ8myxfZWLSX/oWq kAtMbIC1GKgeLS5aiJ4ivcPXiBfBuS3zs+OAyS1/cfiCNz2DwwAGtaoI2HAln8D95Vz3kZu+re4 WOkoY+QnxQj/5lF061BvnPymoFl+91olq5QH3DRwV1yA778v9yA/GIKzAF/CECwJFoy4D3UyLoY zukegCJGZ537ri0blhlnxDcN+xgGHIlrArwtMbpMeHHFH+VASnq3+KOR08JLmED33D2NRqpHlhL /Zo5hSwSuLDwVrQM0aPtyW1THCk37aJR9esxH+yIKWyXLrEP1XwIsXnwQppU6oj2wB46NEOFVl8 sx1ys7MdSurhFeG9NCsCl02G+3nJg3GiMg7oonOhmhfaYPPZvMf1DpKtWlT+pFufBrIqhrpNHLw AP/mfWUZWiXkaoFsA9Wqqh9nCaT6GMumdf/sjjr85Yhwd4/I6yh1fxnWynOr3RqCYW3sH77Z22d ewtAD9RWzR4MjijhkvpUc0WO6fTd9rCv3eu6UEFz7xf45YDgu/ERrtcM3YlBP6C44D8Joz6kGtn /dZJ7oz+e25tSomNsSmDxRlRv434n+8BRxQVB X-Received: by 2002:a05:690c:9a0a:b0:873:5bd1:98cf with SMTP id 00721157ae682-8b660de5d38mr1175337b3.65.1796335233300; Thu, 03 Dec 2026 14:00:33 -0800 (PST) Received: from localhost ([2a03:2880:10ff:6::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b66042fbbasm1141077b3.3.2026.12.03.14.00.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:32 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Baoquan He , Barry Song , Youngjun Park , Johannes Weiner , Yosry Ahmed , Chengming Zhou , Qi Zheng , Shakeel Butt , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baolin Wang , Michal Hocko , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Hugh Dickins , Jonathan Corbet , Shuah Khan , Randy Dunlap , Roman Gushchin , Muchun Song , Arnd Bergmann , Karl Mehltretter , Dapeng Mi , Marco Elver , Ethan Nelson-Moore , "Borislav Petkov (AMD)" , Jeff Layton , linux-doc@vger.kernel.org, cgroups@vger.kernel.org, Tejun Heo , =?UTF-8?q?Michal=20Koutn=C3=BD?= Subject: [PATCH v7 11/20] mm, swap: reclaim physical slots backing cache-only vswap entries Date: Thu, 3 Dec 2026 14:00:01 -0800 Message-ID: <20261203220010.1302207-12-nphamcs@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261203220010.1302207-1-nphamcs@gmail.com> References: <20261203220010.1302207-1-nphamcs@gmail.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A vswap folio can keep physical swapfile space after its swap count reaches zero, even though its data remains in the swap cache. That space is scarce and valuable: retaining backing leaves less capacity for pages that need it. Put-time reclaim is best-effort and does not reliably recover these slots. Reclaim such slots from the physical reclaim scanner, once swap is more than half used (vm_swap_full()), to free physical capacity for new allocations. Extend the allocation-range scans to reclaim them too. Drop the physical cluster lock before finding and locking the virtual folio. Recheck the entry, backing and swap count before freeing the physical slot. Advance one inspected physical slot at a time so a large folio cannot skip entries in the requested range. Link: https://lore.kernel.org/all/sashiko-outbox-181081@kernel.org/ Signed-off-by: Nhat Pham --- mm/swap_table.h | 12 +++-- mm/swapfile.c | 127 +++++++++++++++++++++++++++++++++++++++++++++++- mm/vswap.h | 31 ++++++++++++ 3 files changed, 165 insertions(+), 5 deletions(-) diff --git a/mm/swap_table.h b/mm/swap_table.h index a54cacbc2ade..4f4f3100e5f9 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -31,7 +31,7 @@ struct swap_memcg_table { * NULL: |---------------- 0 ---------------| - Free slot * Shadow: |SWAP_COUNT|Z|---- SHADOW_VAL ---|1| - Swapped out slot * PFN: |SWAP_COUNT|Z|------ PFN -------|10| - Cached slot - * Vswap: |-------- vswap offset --------|100| - vswap rmap + * Vswap: |C|------- vswap offset -------|100| - vswap rmap * Bad: |------------- 1 -------------|1000| - Bad slot * HIB: | 0 |------- 1 -------|10000| - Hibernation slot * @@ -413,14 +413,18 @@ static inline unsigned short __swap_cgroup_clear(struct swap_cluster_info *ci, * On physical clusters, a Vswap-tagged entry stores the offset of the * vswap entry that owns this physical slot (the reverse map). Only the * offset is stored; the swap type is implicit (always vswap_si->type, - * since there is exactly one vswap device). + * since there is exactly one vswap device). The top bit is reserved as + * a cache-only flag, set when vswap swap_count drops to 0 but the folio + * is still in swap cache. * - * Vswap: |---- vswap offset ----|100| + * Vswap: |C|---- vswap offset ----|100| + * C = SWP_RMAP_CACHE_ONLY (the top bit) */ #define SWP_TB_VSWAP_MARK_BITS 3 #define SWP_TB_VSWAP_MARK 0b100UL #define SWP_TB_VSWAP_MARK_MASK ((1UL << SWP_TB_VSWAP_MARK_BITS) - 1) -#define SWP_RMAP_ENTRY_MASK (~SWP_TB_VSWAP_MARK_MASK) +#define SWP_RMAP_CACHE_ONLY (1UL << (BITS_PER_LONG - 1)) +#define SWP_RMAP_ENTRY_MASK (~(SWP_RMAP_CACHE_ONLY | SWP_TB_VSWAP_MARK_MASK)) static inline bool swp_tb_is_vswap(unsigned long swp_tb) { diff --git a/mm/swapfile.c b/mm/swapfile.c index 3dca9bd55736..ab65b84a51c7 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -145,6 +145,11 @@ static DEFINE_PER_CPU(struct percpu_vswap_cluster, percpu_vswap_cluster) = { .lock = INIT_LOCAL_LOCK(), }; +static void vswap_mark_cache_only(struct swap_cluster_info *ci, + unsigned int ci_off); +static void vswap_clear_cache_only(struct swap_cluster_info *ci, + unsigned int ci_start, int nr); + /* May return NULL on invalid type, caller must check for NULL return */ static struct swap_info_struct *swap_type_to_info(int type) { @@ -936,6 +941,55 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si, return ret; } +/* + * Try to reclaim a Vswap-tagged physical slot backing a vswap entry. + * The physical cluster lock must NOT be held. Returns the backing folio's + * page count, negated if the slots could not be reclaimed, or 0 if the + * folio could not be shown to own @offset (i.e. there is a race). + */ +static int try_to_reclaim_vswap_backing(struct swap_info_struct *si, + unsigned long offset, + swp_entry_t vswap_entry) +{ + swp_entry_t phys_base; + struct folio *folio; + unsigned int i; + int ret; + + folio = swap_cache_get_folio(vswap_entry); + if (!folio) + return 0; + + if (!folio_trylock(folio)) { + /* Unvalidated: the folio may not even own @offset. */ + folio_put(folio); + return 0; + } + + if (!folio_matches_swap_entry(folio, vswap_entry)) { + folio_unlock(folio); + folio_put(folio); + return 0; + } + + i = vswap_entry.val - folio->swap.val; + phys_base = vswap_to_phys(folio->swap); + if (!phys_base.val || swp_type(phys_base) != si->type || + swp_offset(phys_base) + i != offset) { + folio_unlock(folio); + folio_put(folio); + return 0; + } + + /* The run is ours: skip it all, whether or not the free succeeds. */ + ret = folio_nr_pages(folio); + if (!folio_free_swap(folio)) + ret = -ret; + folio_unlock(folio); + folio_put(folio); + return ret; +} + /* * Reclaim drops the ci lock, so the cluster may become unusable (freed or * stolen by a lower order). @usable will be set to false if that happens. @@ -948,12 +1002,21 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, unsigned int nr_pages = 1 << order; unsigned long offset = start, end = start + nr_pages; unsigned long swp_tb; + swp_entry_t vswap_entry; VM_WARN_ON_ONCE(swap_is_vswap(si)); spin_unlock(&ci->lock); do { swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER); + if (swp_tb_is_vswap(swp_tb) && + (swp_tb & SWP_RMAP_CACHE_ONLY)) { + vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb); + if (try_to_reclaim_vswap_backing(si, offset, + vswap_entry) < 0) + break; + continue; + } if (swp_tb_get_count(swp_tb)) break; if (swp_tb_is_folio(swp_tb)) @@ -1002,7 +1065,8 @@ static bool cluster_scan_range(struct swap_info_struct *si, swp_tb = __swap_table_get(ci, ci_off); if (swp_tb_is_null(swp_tb)) continue; - if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) { + if ((swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) || + (swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY))) { /* vswap slots are abundant; never reclaim to reuse one */ if (swap_is_vswap(si) || !vm_swap_full()) return false; @@ -1233,6 +1297,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) long to_scan = 1; unsigned long offset, end; struct swap_cluster_info *ci; + swp_entry_t vswap_entry; unsigned long swp_tb; int nr_reclaim; @@ -1255,6 +1320,17 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) offset += abs(nr_reclaim); continue; } + } else if (swp_tb_is_vswap(swp_tb) && + (swp_tb & SWP_RMAP_CACHE_ONLY)) { + vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb); + spin_unlock(&ci->lock); + nr_reclaim = try_to_reclaim_vswap_backing(si, offset, + vswap_entry); + spin_lock(&ci->lock); + if (nr_reclaim) { + offset += abs(nr_reclaim); + continue; + } } offset++; } @@ -1820,6 +1896,8 @@ static void swap_put_entries_cluster(struct swap_info_struct *si, } /* count will be 0 after put, slot can be reclaimed */ need_reclaim = true; + if (swap_is_vswap(si)) + vswap_mark_cache_only(ci, ci_off); } /* * A count != 1 or cached slot can't be freed. Put its swap @@ -1926,6 +2004,8 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, goto failed; } } while (++ci_off < ci_end); + if (swap_is_vswap(si)) + vswap_clear_cache_only(ci, ci_start, nr); swap_cluster_unlock(ci); return 0; failed: @@ -2076,6 +2156,51 @@ int folio_alloc_swap(struct folio *folio) return order ? -E2BIG : -ENOMEM; } +static void vswap_mark_cache_only(struct swap_cluster_info *ci, + unsigned int ci_off) +{ + struct swap_cluster_info_dynamic *ci_dyn; + struct swap_cluster_info *pci; + swp_entry_t phys; + unsigned long vt; + + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + vt = __vtable_get(ci_dyn, ci_off); + + if (vtable_type(vt) == VSWAP_SWAPFILE) { + phys = vtable_to_phys(vt); + pci = __swap_entry_to_cluster(phys); + swap_rmap_mark_cache_only(pci, swp_cluster_offset(phys)); + } +} + +/* + * Clear the cache-only rmap hint for entries re-referenced from count 0 to 1 + * (no longer reclaimable), so the physical reclaim scanner skips them. + */ +static void vswap_clear_cache_only(struct swap_cluster_info *ci, + unsigned int ci_start, int nr) +{ + struct swap_cluster_info_dynamic *ci_dyn; + struct swap_cluster_info *pci; + unsigned long swp_tb, vt; + swp_entry_t phys; + unsigned int off; + + ci_dyn = container_of(ci, struct swap_cluster_info_dynamic, ci); + for (off = ci_start; off < ci_start + nr; off++) { + swp_tb = __swap_table_get(ci, off); + if (!swp_tb_is_folio(swp_tb) || swp_tb_get_count(swp_tb) != 1) + continue; + vt = __vtable_get(ci_dyn, off); + if (vtable_type(vt) != VSWAP_SWAPFILE) + continue; + phys = vtable_to_phys(vt); + pci = __swap_entry_to_cluster(phys); + swap_rmap_clear_cache_only(pci, swp_cluster_offset(phys)); + } +} + /** * __vswap_release_backing - release the backing of a range of vtable slots * @ci: the locked vswap cluster diff --git a/mm/vswap.h b/mm/vswap.h index 080e4bec8280..72b43897faae 100644 --- a/mm/vswap.h +++ b/mm/vswap.h @@ -43,6 +43,37 @@ static inline bool is_vswap_entry(swp_entry_t entry) return swap_is_vswap(__swap_entry_to_info(entry)); } +/* + * Rmap cache-only helpers for physical cluster Vswap-tagged entries. + * SWP_RMAP_CACHE_ONLY records, inline on the physical swap_table entry, + * that the backing vswap entry has swap_count == 0 (swap-cache-only, so + * reclaimable). The physical reclaim scanner reads it directly instead of + * chasing the rmap into the vswap layer and paying the cluster-lookup + * indirection. + * + * Callers hold the vswap cluster lock, not the physical one. The rmap is + * only touched while the vtable holds the slot as SWAPFILE, and that + * window is opened and closed under the vswap cluster lock, so the + * allocator has finished writing the entry by then. + */ +static inline void swap_rmap_mark_cache_only(struct swap_cluster_info *ci, + unsigned int off) +{ + atomic_long_t *table; + + table = rcu_dereference_protected(ci->table, true); + atomic_long_or(SWP_RMAP_CACHE_ONLY, &table[off]); +} + +static inline void swap_rmap_clear_cache_only(struct swap_cluster_info *ci, + unsigned int off) +{ + atomic_long_t *table; + + table = rcu_dereference_protected(ci->table, true); + atomic_long_and(~SWP_RMAP_CACHE_ONLY, &table[off]); +} + /* * Virtual table entry encoding for vswap clusters. * -- 2.53.1-Meta