From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 96427606A322 for ; Thu, 3 Dec 2026 22:06:31 +0000 (UTC) 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=TxWEKctD Received: by kanga.kvack.org (Postfix) id 70D556B05D0; Thu, 3 Dec 2026 17:06:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E5006B05D1; Thu, 3 Dec 2026 17:06:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FA5E6B05D2; Thu, 3 Dec 2026 17:06:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1E52E6B05D0 for ; Thu, 3 Dec 2026 17:06:30 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1491E401C0 for ; Thu, 3 Dec 2026 22:06:29 +0000 (UTC) X-FDA: 85504056498.09.C54CC69 Received: from mail-oo1-f41.google.com (mail-oo1-f41.google.com [209.85.161.41]) by imf06.hostedemail.com (Postfix) with ESMTP id 33357180011 for ; Thu, 3 Dec 2026 22:06:27 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=TxWEKctD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.161.41 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796335587; b=dZy6s2AZEQ9N2jUywqMPHgGDDaQTnsfcj73rgfoqSf2BCKI6zOUL8W8L2ZpbKUdTxh6c0X eLUeo3zxDz+UQDnNaKIDuhRK73E1fyZVrY5qTIWb/CM8ElkBp8wUBaz12PSpKzjIvgRwRf r3uooJy6jqojXplr5HaqIS5iuuv0f98= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796335587; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mP5FG6Li9kuUQwaa9Kz69I2SMNmiIlspwXx0FM9mheg=; b=5+jxDzeIY12jHh3CbbDGP0hMovNJQ4Hvc5pEVLZ+IElMNrtmG68ZintlLB4NbM5VSG4Crk Hu6gGLGejn+HJbikWl2he0fXTHS5wZhc+rsnMlT0RCRcTAE6qJRqii8YvChiuvtChN3u2O ZKS1/UphJqhnCYuxpLbckZRvYl5Ddj4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=TxWEKctD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.161.41 as permitted sender) smtp.mailfrom=nphamcs@gmail.com Received: by mail-oo1-f41.google.com with SMTP id 006d021491bc7-6b8e2d8df4fso15990eaf.1 for ; Thu, 03 Dec 2026 14:06:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335586; x=1796940386; darn=kvack.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=TxWEKctDK/TaKYaS6H1KHvqG1KAAF4chtvvSnkB9n/oBWlTnX8ua+YDHjCkMzztz/o AIGc7sZT/Y0DA//WkJXOdoeX9oqneqtgx9T8iYdvec7HY0W4JFWcEL+wEDLo1YB2Ea2y qF1mn2chxSncPyvX0UisqN2eacv4KhCUjXskdakdNm3CCXwj8xwMJINP++Hm8iv4uIpc wZ0lNcdPd8nRTgyMqQJekM0qslupVPutropJJBOs0o4B4MAqFOngZLnrl1CbGxxA01ye kkutQJf7TqbpP65AjnKxjFcRLnTjpl8NJpnd8Mk5+oUi56W4rDCiBVH6q32Cf1AasQ9W lX4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335586; x=1796940386; 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=M46fz0KWP+0qVMo3L81tUa6UWCOHhm+v/S7Mry53i4z/qM4TDw8YFdDJiMuHZclbbJ DpBXh6fNYN1tNrYSMZi2m3ybAwWsCwcZH9zMIGkVAOcW24WxeX5hdHXNFJPzT92Bm3W6 zIcD4JbMzvtwHLwZlaNLIY/qM3Le6Itqiy4Xo2m23lHAtPElJ47i/vgZfVRvLaw31qrc USoEleCEjISYd1cBO3yK8rfPK4Mu+1xUSMj1/QTcfqwkF5fiUz3itF8R/9lXfzmzMy3b lVk32bBSxeI+Hin9CoE1NDaAr009GTry4JBlP69pGSQ+pa8LMDRxwEiwa1YO2SsOueGm MGkg== X-Gm-Message-State: APuU4Z9p84rxo9b/jS72JUUlvu8uvn4sj1QrMtTkF1bf9j/DMnIZJSzw XdZcdmUNAToIl+lJ1tawndPg1+SYekGnOYYGt8gM9OwCTHbp3wOU6r5HecSb6GdQ X-Gm-Gg: AZFdTZcTvfYq7WlaDtDqezbMczaSJZaQBBYwLTbccH3mMl8IqbG4RhRRyU2HLG+92GA aAGSEuXu/xzYO8T5EF74Giesxl+tQdBaovJ9nVt/DYRvKt8KMLfeHI4n4H08sHtNy7AofyU3jkL ncFK62n9e1bYUstQ2nUfAdSpQ+8epa/Sv+cfaYZuYDA9vuwV2c8hNKS/gWQu70TBScbN/L2dH6l h4cILYSgfdvHjLIGpuY3BcfHrywgJSZi/zPM+UtBqNIcdIrFGFQ60oe7DF2wgGwv6axICFSRCa2 QxNwy2CBerI8IizuXU7mfnmdQqRNs/MFCPB/BgIdeEdX+F7y0/Kf0JGSckPJHk/JvSkXvk4Klka XqA5mxTBuq9Z1V4BiSmMPkr7U2Isz9ZVgDiVEWj5e6ijnCrVMHIPfc6G7xlAhmmu2ENxz5z84nE G2PaceM4ty68bGrVMl1c8HdTw7byC6qEpoiSeETj5OxisUH00AQI3UDHdMP9qFEjD6dNAR/QXt7 NzbkiH8QMTwfJNKHq72S6oVnv5dkPxsNHE0OSTZ4vAHHj4/2ihBkOjVNMIC3JX0Z4Yqgtuazonp Y0iR93w0L5x40oJ6uWVAoAl9TFGG3muv95ht5qlSnlE3wc5oTjD3UgkZthed7NMvbxWa7ioOCT3 nrZ16H+RnHXSLdH9mBSAGsXv51jPGeYDsjDDHqQ== 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam02 X-Rspam-User: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: i4qsujyqno8ho13qzkzf8t7i8m1fnj41 X-Rspamd-Queue-Id: 33357180011 X-HE-Tag: 1796335587-970641 X-HE-Meta: U2FsdGVkX1+liTPOM5uJkn1ucGbXnXnoLMaDPnjF1z3K6/WlqGne3Cg74IeHVXFq5ZbQ/ZkW83lTFSk9nGS+1GJI8x1i/PDF1dForbUVqAyLL8gKkVQ+OvhYrE51sHYk78Xm5N9QcZsY7hixCs42555vqwTqwW/6Cqxm4z/rmgpWN/mwmMVuKXGUYeBnOcrOaYxU+RFmO//5uCarNfRASDjeFq0WeFz020ZIclr0vG3e+l/+nwWZK9aFrVe9XWnspkaiH1d0uhmxZpSYF7/blMJaqUvh4D7AOlNJBRlNCqjG4tMSZE2Tpz1KfCu8n5iLokSDp68gSnceHaR8oSZQ0oO6qR2Z1Ajwp/2lQoJ99h4P55l8LQ0N2vYAmtSIN6v9Mt8pIxqWpeke8eTgSjaR8MHx5kRfymP6qmtivGyfDrhAlgfv17a5Gcz2Dq8JyVD3d1s+mfM3ls5DZQDn0kvo26+h1B8U8IeyeqiELXsXk96N3KBrSaZGXk/uHf9jTYpBCs/Io2HOTyDEBVNCNk5kX1Tp/FJus90M6I7ATOFs6tCPu93urJOx/KjtLBuJc+Xo0b2kEfhRDLvRnncEe9Cgqb3Yh0hDY4yh7yaM/GWotV8CqNw3vJjE8zRZ1HDfWsvFno4LeuqzAXJ4yNs69qLCJTDfUuZQsCKVZaN5DE6ck73w5aK6zUUcaAnjefz+d/QEZ6WLGUUWC2fBEHZvRCVTHQq5wDhAuk3SV0lZkW3Ra0oeSvrQq9qoRw0gdNediNAWiKkrRdZymGrs657vvfYvRDfOLVzAhPURALT/jOiCDzL5Td+th9jKugplErl2xSarQDLfWE1VB6BWhHkqVmE5b3k7ovSTi0C87W8YDjf6gmTiH3kBURnE0MqHEZY6Zo5dyBbV+zXZiahbI1uIfqyqaRpwdI2i1ttL5+1lXsEumHcqMlPy4dbIjIBHDVZtoxrp1vKdbZeU4DzEm9B3HWJ ochtcaK5 f2ZO6jDWItusAWQjghNJFLg6mIc2I6on4SRqoYocjzXIDQooDSfezqu+sXDkHimmPAL50pMVOixA1PaFF4NVA8NthUpCImU0Q4NOKJvNAOiYONolGrPHa2eJAlAerZ+jMQP+yOK1o8XeSD4JVcnQH7o+bQaX/YMVIKmHvX23NnltwnBnK2d+naiGu0ODLFNvbkohgZAXJbycAu8RN94Gru4Yn0r67rfSiMxEoHKcwadw0VvGPqzFVw32777MkS79e8YjDXYBO5X0tI61z1JGe+FAv4ssGktAcgDFmIUR8hpigMQgM0LB8lkYfGo3OAABjJ0/AQQkfgT2jaFjJADzE+5cnaGSqu7gqxsBGeqsOFm4CiTi3nNFUughAoCRZR/6rij4VNW/MyOsonhxwaIrz9DV6fFM+815np0pmaT3eyZZGDXf1H7+VgfYBjr/4I4I0biJS5HTAuPKKPeBG+AFnrVJN2imyPlmkxm5fYjLcpMEHJkqvlTcWw50UKnHiplJdyi5yPtWaestSn1s0pL3tczeTqEap91m4yz98f+zhTvDV7b0OC/uoSQ1Qc66elPzLihMpM1Ga2GAX4WKLuA0KIPgTLqgWFiheoDSE+cQURk6gciwXzPxo8+mlksp8gRvIWPCTNbW8d1aGfADKfBbUJybby4hB6rYU1RcN8T2R2VwrE+fLuoI+O1FLYMEwMyMLydS9FjwXeZuXjCd/t2sfQmRneg== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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