From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335234; b=5jYsmRFsrEr69XeXjW3dmdrp5VvKt5lpKWYwbpBjbWbMtNKnogqqyOvF6yoqreqYq0oh 7T0nz3ULQrAMFAEvYU8Urq8hcF1j3BHc4AbrU4SNT4gie6q5E9DSIkCxEYlGqIGbDAb7Z EhPWXJrAJRAlD2WsAyT/0GYFjKpAQYrCiEz0PNfEXROw24H2qkiNTcEv5/s41A+qUH0gk t0mqcBRbkAa+Surpd0SXjHd6p3F6M+UYsvEbN826lvK0B2wa7FdSjYaXIKqeL/inSYnX6 oCd+p8XCRasZ4Pdvh5nAzkjx84E8IHhIKPUvt530ZzNuFGffT9UKymeXTDse5qy0REg== 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=wB7UZMVOMNdhBW55jv/bCk1yKdutE7uoXXBvhihT7dWJ/RtODmh2+pVt03OOTNkbVgKw jTeezqJ0T8lwkX/G4qe18zV/mLhmFVqsyFmKbUBDMk8t6KdexyVYRdcs6VegggH7tA2eP U/1QM9EOtV/8kFFWT4VxCKYoKbvPL+LE6oaSmMwIbVb4tQtfJwlP9cY9RBFwlBZCCiVct 0rsg6ixQ6hlI03BX4v47Ihi2WaazmP3Z8Cd3D6atAAi20wwDgOsRuCPlquAnTSfyjm6Nv WqFDNSUJZG2oh8lN0lvgKd9fIV8LjFEgIHXIMshsNIPp/2MAa6ci5V/5RUydUnPyh7Q== 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.177 Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) (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 8231B606A696 for ; Thu, 3 Dec 2026 22:00:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8231B606A696 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-f177.google.com with SMTP id 00721157ae682-8b502b9b702so193417b3.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=BxrnOWtEPVKS8VRV1NF7hdubraKWav/WRy+r9ueGXkkf3fkWv+n9v1CNIaR1PYmRT2 iahD1nRChLLsVgR9ugG8ODeGo3xP1WA+C+xXgaIuW5FI1tElMh7M17eUhWVc+b1Tquu+ mZ2tM8/gz0V9dYqdSHiG/tY89oCA4Nk3ELJPcf3pCJg4TkckvSGeftnv3l4qLtbGqFyW RJdFKpy4TGkLJcSYgxEBLrTW48pAXEC/RrIjomIHOUZ4bJuj+pXhQkq7kWAr36bqTuwt LCniwa1ScyOkYb/ypseDgHjQi3u51mO7A3YP0AzhVRbn7v3uJ6AdzqGtYGC7pS3YOnj5 pJLw== X-Forwarded-Encrypted: i=1; AL+hErHYirJjO5t0c+QgEZ9IZl8f90aQ3MOqbvQvmLxkRk9e4tEETxNOrB5mQ0kN4dg/8sCzJqApMUbYih1Dic8=@vger.kernel.org X-Gm-Message-State: APuU4Z/blFjw1uxrc6M4ttdXduOJWWy7LwdD32UGRVm+rGEXA7C12xEY nRUICxtuh1cGOMwU9EeiJfbPvEoN9OT4V6c8/QUgqt9UaPjGcDc9jThv X-Gm-Gg: AZFdTZeAaaqTgB8EbnUq2ECBd3GbXLOyrc4uaImdzf5Dr+mEp2gdvhRec3qCEPL4kNc g5wcCQMSKo2YONUNfZkZtmIi/jZzygPC/zjwbeEXPhDSZLqs6vOYyL47IBhnVkbQAksOtrVgfdS eaEg0K136lwOIQP8JOnBfjOcm2nBH+8GQoFSeE8Apm6TuQBDPefZGQe5ZVvOwynO3ufrBeREJfx NLDpQ14aPG3xEdCDpg4LGhol+5OE0RDfTKOBalDnZUdRBdJ2E4Xoge3LDcsDhki+YFJyrjHbAPs j0nsQjOWXWgr1HLXkyCeMCjbiqtTQFgbjnun84vXWwuuaaRNYF06zF4CFJdW9hqRX1H5tbkiJAJ M1QLjglZB7UETDs+21yf4FIvhA33vMUtxU3RiPbnMO+Ak2MCzOvhvTCvrHPtpFF6gI+J/q4eRBh 1CgrEp/3wWnYsNa0mYaoSxt/ZzT4gSmSnej7nX+xbi7EBZFFOjoIqKLJtPbGb2+rG48bJIZTreH iIfTEw8Pbz2PnsTWej1toOSyoCcUxqNmRIXTvmD6d0YHPytQyeO+5OJOggpBn7IMcU2KlZwkR3z TSOsZVk4Fid8dN0fPNDuQ25QYJRkMI7iMQEbDZeaK47V8Ui8DALEFm73jdWjzwJikZAZ5jJG7fl pEGrNoc0Uyve0GrlbZP8+RVFhTrYST8i3opew 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-kernel@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