From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335250; b=vXIQ2A7kPCxd8Y/IhtVjWBy5OgbqrD8gSYS3TEj1KqCDQl+qMvrGAJGLVT+GnLDjui3f Bwl0PbntrNzHMRh805TjUHZjjTq9XUZg4FMyCtZv25nToIPG9WPjt6OeFLoiE1K3xXGsa YC6XbwxjMHQ4BZC89hAsGqXq9P5ifRLinDD/rtybC0QHqr7Zpsa+CgJCxwmaOmsfUhZYh tzl7vCoMpfxDUHnzzD8wH+fWSRygADZErpNN1YkbL0rJsvHn3/kfH2iUXc4WHdXyoK/8b /tY4omx480bBCBpmeyE4kfNw6OgzoELU1WmqbdZGIQrdR8okMQlpNpkfo++1+YSO4yw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335250; 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=r4ETQ5pexZdtZlN6HAodNeoeGv23w2ZSNEfon9OOXUM=; b=j1sS/iJK3DWa9wuzA9vZTB7+0GYNH+q0CJV0gBTJLWIdZ53aP8PT2cFaJU4/YW4DcElv y2kte+c1V7LKui/zQmveekMDRhei8AIF5wojKo0Aw1vyLIWuirbuI3gMTlGj6MHUuQBbr DqPZgkgsut4YWsLXJeKfqZExa4x6s+Huh2H9GgAN15KxOaKB4x4SpSdTGqUKYbs1oWmRV JRl2h8oEg5BIFD9cMBNfbfB75LsOdphUtlvhS8eGWs0Gw/zrqHY6q0jJtskieJEeuLYJ/ V8bK3AhD4r4gL4Lqh6PUYZY8+lQ3qiunAQQzBghskD0pRg/iOciKvdXZwLiHdVFGVrg== 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=Xc5tRHj3; arc=none smtp.remote-ip=209.85.128.171 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 AD566606A6BC for ; Thu, 3 Dec 2026 22:00:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AD566606A6BC 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=Xc5tRHj3 Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-8b4e04eda5aso2068857b3.1 for ; Thu, 03 Dec 2026 14:00:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335249; x=1796940049; 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=r4ETQ5pexZdtZlN6HAodNeoeGv23w2ZSNEfon9OOXUM=; b=Xc5tRHj3oXZ0wbZgOtLkpCgXMItlhLXP6XAnll80BJycnqndjQY0aXxSjvd0OM+Zbs hr4z4KACFPeH89AVHfWMymKHhHSC91X1x6bOw82HGTGTtDSLGAy1M4693tTqI9d6qltz kwCS309SfZE7QcOktEXIuTKihBS9gT/DqZ0HF5sGvdnSZLT9vRB673dcD4N0jrNolvtv FeiKZU5r/MEqhxIkeOg8VZfUjtU1E4NE6/C9oSPSxfIPFdsJHoIVZCsYWtgf5Q1tHaDm jECgeA8nu+5nG+nKWPNz7Hf1bNNeZn5//uyb0PTcaMsgLqWYBAzKKWr8yqVlByuABGix NPbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335249; x=1796940049; 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=r4ETQ5pexZdtZlN6HAodNeoeGv23w2ZSNEfon9OOXUM=; b=VipooeMce/U3YTFvf+3VK25AELWPfkAL9KwY/F4YoQ+MAh2cNwaBkh96adiXwbk+tS /s34LbymfTB/5VQCVVpSuJ26GI1DH7JPsq7ri3nyecyp/RMI4CTCdvkt787YPuvyij2s vUj/nXQ5ozedpeI8UYC59A3YBVV7fdwIiLFKppTkz+m3HmJbwyQijJMfF47sSY3Yq8wd yI/5MWKBeRNm3Ona+ytCm7hX9zxtBcOIz0MBKbQ99uxPd0lOmd4dykhIbXffpF4DJzNO 9dxbYdb42RGeP+pghXjrpqUEc2LuIn0tl5o7C9UH3/j3NwDIZubyRJUsxJRd/t5eu7Xo YHIg== X-Forwarded-Encrypted: i=1; AL+hErGxi1NFjTwLSFcnLrpXjn9hKdrta+wHGlxFWJpWFoGCny2C9MOG1ojLQLFbbHBP0yAWzue7w63xqmE=@vger.kernel.org X-Gm-Message-State: APuU4Z8sZ0aHAevTbRs8926/XEZw586sXjvWKIBAmPbSevhsXJNKlk4M bG3JoK25H4zMNHNC6LRr2usX8LTArfXGTG0e1bFBZDMb6XT2lMSxQ+Kf X-Gm-Gg: AZFdTZfaONWEvhy9d8xkY48kNzusYElarGD1GKdFEtF9p5WB1CYmp3FUsDs5FIN+dsb HNsSimzU6nVkvjsdSbjGFWSwNQcvZ2COFFiP0I7LIefYMuFHO+vzT4MkUW3NzsHPzPiVC1xhvda IWKeMT5aYMafoSqpXt2h1/41VV+5eXaY8LzR34U0+JYEc75fsx5sE8oRXbTvR369A/siEOQ0njq 9aR4hf2URfNr/Cnd2Evg7XHahXlx5uYAf4nGfu6rugmAWUzH4Gs6vx3eEE39ENimR0eRm6YPUJw XZEq1FwHDE+ayKWTsAHSIpJSp1xthaw33Ak+VnBEElczzRVQrCkQzGD3AWvNfq00ZSRDphF+VDT qPCgHEZI07tef97bxUdLDFVV2LkM4RCa5G+4CzNy+WVCswbHKgzO4KMga28wEx+s9p8PDFyPJIc 0Bq4MC7ZEmYtITFBF0U0UYREY+ki9T6btsmBBd+HWrBBXEUIFLeoyhcA1hOMVRoNY3ciILXi9bG nWhsq1pxkjE26k7CQR7SFwePf/1sbMEyLiEcMR+fJTBCdG5z1ccYNr4av9GzIUeOcZ59pFUUbGb f265hwK3EHiBM2xdEjPScN9cegOhh4g76sYb4kaEoGgupDPFpeVkbHGV/yqWhivW8srDMhJsLNS xZjZY6qIeaDzjJKlFj2UUkIQT0G5eAcMiD1RHXw== X-Received: by 2002:a05:690c:e21:b0:8b1:60a1:cd3b with SMTP id 00721157ae682-8b660d4c8dbmr1753167b3.24.1796335248603; Thu, 03 Dec 2026 14:00:48 -0800 (PST) Received: from localhost ([2a03:2880:10ff:4e::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b6603fef77sm1162807b3.6.2026.12.03.14.00.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:47 -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 19/20] mm/swap: reclaim physical backing through reverse mappings Date: Thu, 3 Dec 2026 14:00:09 -0800 Message-ID: <20261203220010.1302207-20-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 The physical allocator can reuse slots backing a cache-only virtual entry. Recognize those slots from the reverse mapping in both the full-cluster and range scans. The last remaining caller of __try_to_reclaim_swap() drops a virtual swap count. It already asks for unmapped folios or physical backing held by a cgroup under swap pressure. Keep that test directly and remove the flags. Signed-off-by: Nhat Pham --- mm/swap_table.h | 5 ++++ mm/swapfile.c | 64 ++++++++++++------------------------------------- 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/mm/swap_table.h b/mm/swap_table.h index 4f4f3100e5f9..f723ce4f2fca 100644 --- a/mm/swap_table.h +++ b/mm/swap_table.h @@ -431,6 +431,11 @@ static inline bool swp_tb_is_vswap(unsigned long swp_tb) return (swp_tb & SWP_TB_VSWAP_MARK_MASK) == SWP_TB_VSWAP_MARK; } +static inline bool swp_tb_is_vswap_cache_only(unsigned long swp_tb) +{ + return swp_tb_is_vswap(swp_tb) && (swp_tb & SWP_RMAP_CACHE_ONLY); +} + #ifdef CONFIG_SWAP static inline unsigned long swp_entry_to_swp_tb_vswap(swp_entry_t entry) { diff --git a/mm/swapfile.c b/mm/swapfile.c index d71fccce5ae3..3f6c56c51c2c 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -188,16 +188,6 @@ static long swap_usage_in_pages(struct swap_info_struct *si) return atomic_long_read(&si->inuse_pages) & SWAP_USAGE_COUNTER_MASK; } -/* Reclaim the swap entry anyway if possible */ -#define TTRS_ANYWAY 0x1 -/* - * Reclaim the swap entry if there are no more mappings of the - * corresponding page - */ -#define TTRS_UNMAPPED 0x2 -/* Reclaim the swap entry if swap is getting full */ -#define TTRS_FULL 0x4 - static bool swap_only_has_cache(struct swap_cluster_info *ci, unsigned long offset, int nr_pages) { @@ -221,7 +211,7 @@ static bool swap_only_has_cache(struct swap_cluster_info *ci, * folio was associated with the swap entry. */ static int __try_to_reclaim_swap(struct swap_info_struct *si, - unsigned long offset, unsigned long flags) + unsigned long offset) { const swp_entry_t entry = swp_entry(si->type, offset); struct swap_cluster_info *ci; @@ -257,10 +247,8 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, } offset = swp_offset(folio->swap); - need_reclaim = ((flags & TTRS_ANYWAY) || - ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) || - ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio) && - folio_phys_swap_backed(folio))); + need_reclaim = !folio_mapped(folio) || + (mem_cgroup_swap_full(folio) && folio_phys_swap_backed(folio)); if (!need_reclaim || !folio_swapcache_freeable(folio)) goto out_unlock; @@ -1014,19 +1002,13 @@ static bool cluster_reclaim_range(struct swap_info_struct *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; + if (swp_tb_is_null(swp_tb)) continue; - } - if (swp_tb_get_count(swp_tb)) + if (!swp_tb_is_vswap_cache_only(swp_tb)) + break; + vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb); + if (try_to_reclaim_vswap_backing(si, offset, vswap_entry) < 0) break; - if (swp_tb_is_folio(swp_tb)) - if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY) < 0) - break; } while (++offset < end); spin_lock(&ci->lock); @@ -1057,8 +1039,7 @@ static bool cluster_reclaim_range(struct swap_info_struct *si, return true; } -static bool cluster_scan_range(struct swap_info_struct *si, - struct swap_cluster_info *ci, +static bool cluster_scan_range(struct swap_cluster_info *ci, unsigned long offset, unsigned int nr_pages, bool *need_reclaim) { @@ -1070,16 +1051,12 @@ 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)) || - (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; + if (swp_tb_is_vswap_cache_only(swp_tb) && vm_swap_full()) { *need_reclaim = true; continue; } /* Slot with zero count can only be NULL or folio */ - VM_WARN_ON(!swp_tb_get_count(swp_tb)); + VM_WARN_ON(swp_tb_is_shadow(swp_tb) && !__swp_tb_get_count(swp_tb)); return false; } while (++ci_off < ci_end); @@ -1194,7 +1171,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, for (end -= nr_pages; offset <= end; offset += nr_pages) { need_reclaim = false; - if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim)) + if (!cluster_scan_range(ci, offset, nr_pages, &need_reclaim)) continue; if (need_reclaim) { ret = cluster_reclaim_range(si, ci, offset, order, &usable); @@ -1318,17 +1295,7 @@ static void swap_reclaim_full_clusters(struct swap_info_struct *si, bool force) while (offset < end) { swp_tb = swap_table_get(ci, offset % SWAPFILE_CLUSTER); - if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) { - spin_unlock(&ci->lock); - nr_reclaim = __try_to_reclaim_swap(si, offset, - TTRS_ANYWAY); - spin_lock(&ci->lock); - if (nr_reclaim) { - offset += abs(nr_reclaim); - continue; - } - } else if (swp_tb_is_vswap(swp_tb) && - (swp_tb & SWP_RMAP_CACHE_ONLY)) { + if (swp_tb_is_vswap_cache_only(swp_tb)) { vswap_entry = swp_tb_vswap_to_swp_entry(swp_tb); spin_unlock(&ci->lock); nr_reclaim = try_to_reclaim_vswap_backing(si, offset, @@ -1923,8 +1890,7 @@ static void swap_put_entries_cluster(struct swap_info_struct *si, return; do { - nr_reclaimed = __try_to_reclaim_swap(si, offset, - TTRS_UNMAPPED | TTRS_FULL); + nr_reclaimed = __try_to_reclaim_swap(si, offset); offset++; if (nr_reclaimed) offset = round_up(offset, abs(nr_reclaimed)); @@ -2795,7 +2761,7 @@ static bool folio_swapcache_freeable(struct folio *folio) /* * Once hibernation has begun to create its image of memory, * there's a danger that one of the calls to folio_free_swap() - * - most probably a call from __try_to_reclaim_swap() while + * - most probably a call from try_to_reclaim_vswap_backing() while * hibernation is allocating its own swap pages for the image, * but conceivably even a call from memory reclaim - will free * the swap from a folio which has already been recorded in the -- 2.53.1-Meta