From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.178 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796335220; b=PywuGtqA71QUNfwtrDhaSHD6K6KXbxMbCpDm80rmoFcB4Ud0ya+eaDn9IWlX5o6YtcQB TC2gpwiwzxpiEmkkvWZ0SCLApNJANMtBg8yg705ni8J/Jr8zKT8K/1nH6A3+ced3lIhs4 B4jbYp2PjFA5pPhHE1g5xFu2k7bSuVJhsHeMBGt1C+IUtmt6wtA9+Id5hfAAn+FGjSq1r +XNeQqghaIAt40qAnh8rnZzceg/6xBd39DbRfU8Fg2OE6t3Fmgerv5bA2yIzb4D7XQVnG LuBCOz4uVaOgW3MiaCmN6VnK+X5B1H86dPAwlPSzjh1F0/PI0r8/1jbqSlNtlUbucKA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335220; 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=Fsj4ssZKA791hjrXJ5AeKjVlIj/lK4V9bPoG18xPtZA=; b=CcJWkukoCKgPJjrCl13BAp/+a4LjZW79qaGohzN67Rw17kGOwbH/fyH4H3pSvZi6dxGl 4irtiPyP1MK3yjAjQgZ6MJQV0dVj6tK2A/k1efpOtbQIqh1v8UaKiXIvE06eEfky33xnS 4JyYhxKKEsAOHVk+BV2ai74CBTEBTEUrnKiyYdFBykKSmd2lZzbVCxwSCWrk603cyi9Bl 8KHE/CpI1GmTbb1eF8urUjARB/4jCphnWLW+ZV/EgKCIxrKc340xb3G+PeOBZbCTrZTIq 669NWlhNFQOxfaahS0HToZwvPAi2J0xg4XNFTaN6OlL770w809830k0OWUlMop5NKzA== 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=cfBSu4sD; arc=none smtp.remote-ip=209.85.128.178 Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 E6D54606A698 for ; Thu, 3 Dec 2026 22:00:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E6D54606A698 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=cfBSu4sD Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-8b4c2da74c0so1931827b3.1 for ; Thu, 03 Dec 2026 14:00:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796335219; x=1796940019; 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=Fsj4ssZKA791hjrXJ5AeKjVlIj/lK4V9bPoG18xPtZA=; b=cfBSu4sD4QgjhW4hoaY+TOYKNtisy3hpFYX76ApTYSF/4hw75afFf1SCyKnRO6ykP8 Kl5awuSLAektBRRGBWj6+WmbqJsYEsyRNBxsGXag6c8itvxt8IxEChcXFWdIbOOK6UTK MLvKcHdsm7rq8QQihSXmtd+9GgcSxgd4OC8gpht3l2VfRBZ5x4VoLOqLrpy+PWGV9nXm /hJfU3m7XewVnJu1IJanJYzAcqAALtDXByZhtEAYAsJuULyxYjjMujpAuxqwhDUSKcMN CHKfmPvJnSfhhe8bsu2GDjFgKfzJzX6UuKBfdPeTmLL3CN7D8xad1Hnr2/maPsZYZjDr EOjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796335219; x=1796940019; 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=Fsj4ssZKA791hjrXJ5AeKjVlIj/lK4V9bPoG18xPtZA=; b=h3ThSyr/P+e5SZ/XsdCHu5lluYPYDZ/G1qLt4BQnuVjht6FCvoBbKJVAvScZ2j4ANH Z8SIZOgctl3t1a0LT/OKfzX2DPKI7Lg66Ou2ejEpKuH18gvTBEUpXN4m0a449A7aRP1M fJXGgZuAeUe1VSRcOsDuc0DUC+zToaSB64wfHXqAQj9jWk5wj0kWjTL21bYHnhdtFz9t fcjed+uftzifTIwcnnmyUec8C8bTAWpH6ivmp+disUUBFlmLBsRIi+pYoW3BBstuMP5s szv7K/6G68uQFU2JRH9wkLtTprcKgF+pLWSaS9vBDvs+90PFxuh1pMWwq1Ysiv6DDvev odkg== X-Forwarded-Encrypted: i=1; AL+hErGzb7PfmNnTY+mvtyDhiBi+e5gQBZM9yz4rKgLXHEQbph+oFCnSGEikd0XXF+L8QWUoP0nIDCWlTBk=@vger.kernel.org X-Gm-Message-State: APuU4Z+gd1JInwK0DTrAgcmWJ5jnpGv4QCQ64rNXU11A6StOS8Z5mMMk efsuqfC3B+O9j+yYCDBk77A+NUuUEuEFwcXxd+ieV5tUj+qkCnbZ7OM3 X-Gm-Gg: AZFdTZevkG6bZXjaOy1K+gCHc26I1CGjhUP1BSpNJUcgJIauiEpIUpPI7mTNl3H+iG7 516I/mFDuYyUN9FmleSO6JTuWCMSDIeZwYIpANr8Jv8tTT2ibVV2GklKLTGb2Zou5zBCIAjIlRD oETNaH46xmz7s8xd/WCKbeC9ssYC4f3oNtj5g1/CcOC4DaTCJcSE8PAxbGLAyrptKGMqSF8bYBz o3LA99XXZw7PcmpprONquOOF9zWMQAnOFZSeOABgMK7LtCk8A9rCEgEk3sLJlAQr3/yuMVN1UpP 0TDEGIffJVd3ZsggpvNwCS4HHhvwXq2afmv2IqF4h4BNAN/CcXuVL3diNscm9qwCZX6cuy6c56H aUwNIERrwmlrWToHSTtSPt+jr+d6C0zPtdJpDrt1HlH/W7GsDbbCkKiw0d1yp8xA+SqaOHWM0f7 whp7HocILN8yX+9Wujv9omJRXkgtZXUbd0Mz9b9bXHhY7qpE1U1QewtM2qPJlKR9LUR0IhxrgBB apdAfFjf1fV+atcbjsHSDke26XWMTt1pCdDDCYRYKbUqbmYMFqtINW7nYPcvg3V5UXNSdIIl86N hAKWT+Oqd0Z2vQRDWUffycKVWlmbL9DAM6mClSAoa56aXsT6bF6edCWrbDeqyqL5b9xa5zukDNd uuS7QbD2UQShnT2Gg1/mZfdSyct+JfWInbOZ3rNw= X-Received: by 2002:a05:690c:443:b0:8b3:6d4e:3f60 with SMTP id 00721157ae682-8b660dbfa85mr969727b3.61.1796335218837; Thu, 03 Dec 2026 14:00:18 -0800 (PST) Received: from localhost ([2a03:2880:10ff:55::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660d9a917sm1013587b3.29.2026.12.03.14.00.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 14:00:18 -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 04/20] mm/swap: allocate virtual swap slots Date: Thu, 3 Dec 2026 13:59:54 -0800 Message-ID: <20261203220010.1302207-5-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 Virtual slots need per-CPU allocation hints for their own device. Add the virtual allocator and select it from folio_alloc_swap() when vswap and zswap are enabled and the cgroup permits zswap. Reuse the cluster allocator while keeping virtual slot usage out of the physical free-space counter. Avoid reclaiming virtual slots merely because physical swap is full. Retain direct physical allocation when virtual allocation fails. The selector remains gated by vswap_key. Device registration waits for the physical backend so writeout can fall back to disk when zswap rejects a folio. Suggested-by: Kairui Song Signed-off-by: Nhat Pham --- mm/swapfile.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 8 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 1387c8bb1e97..aa08111bea62 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -131,6 +131,16 @@ static DEFINE_PER_CPU(struct percpu_swap_cluster, percpu_swap_cluster) = { .lock = INIT_LOCAL_LOCK(), }; +struct percpu_vswap_cluster { + unsigned long offset[SWAP_NR_ORDERS]; + local_lock_t lock; +}; + +static DEFINE_PER_CPU(struct percpu_vswap_cluster, percpu_vswap_cluster) = { + .offset = { [0 ... SWAP_NR_ORDERS - 1] = SWAP_ENTRY_INVALID }, + .lock = INIT_LOCAL_LOCK(), +}; + /* May return NULL on invalid type, caller must check for NULL return */ static struct swap_info_struct *swap_type_to_info(int type) { @@ -236,7 +246,8 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, need_reclaim = ((flags & TTRS_ANYWAY) || ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) || - ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio))); + ((flags & TTRS_FULL) && mem_cgroup_swap_full(folio) && + !is_vswap_entry(folio->swap))); if (!need_reclaim || !folio_swapcache_freeable(folio)) goto out_unlock; @@ -585,7 +596,9 @@ swap_cluster_populate(struct swap_info_struct *si, /* * Only cluster isolation from the allocator does table allocation. * Swap allocator uses percpu clusters and holds the local lock. + * vswap clusters are destroyed rather than freed to si->free_clusters. */ + VM_WARN_ON_ONCE(swap_is_vswap(si)); lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); if (!(si->flags & SWP_SOLIDSTATE)) lockdep_assert_held(&si->global_cluster_lock); @@ -986,7 +999,8 @@ static bool cluster_scan_range(struct swap_info_struct *si, if (swp_tb_is_null(swp_tb)) continue; if (swp_tb_is_folio(swp_tb) && !__swp_tb_get_count(swp_tb)) { - if (!vm_swap_full()) + /* vswap slots are abundant; never reclaim to reuse one */ + if (swap_is_vswap(si) || !vm_swap_full()) return false; *need_reclaim = true; continue; @@ -1058,6 +1072,9 @@ static bool __swap_cluster_alloc_entries(struct swap_info_struct *si, */ static bool swap_prefer_free_cluster(struct swap_info_struct *si) { + if (swap_is_vswap(si)) + return false; + return (si->flags & SWP_PAGE_DISCARD) || !(si->flags & SWP_SYNCHRONOUS_IO); } @@ -1105,6 +1122,10 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, out: relocate_cluster(si, ci); swap_cluster_unlock(ci); + if (swap_is_vswap(si)) { + this_cpu_write(percpu_vswap_cluster.offset[order], next); + return found; + } if (si->flags & SWP_SOLIDSTATE) { this_cpu_write(percpu_swap_cluster.offset[order], next); this_cpu_write(percpu_swap_cluster.si[order], si); @@ -1255,7 +1276,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, * Swapfile is not block device so unable * to allocate large entries. */ - if (order && !(si->flags & SWP_BLKDEV)) + if (order && !(si->flags & SWP_BLKDEV) && !swap_is_vswap(si)) return 0; if (!(si->flags & SWP_SOLIDSTATE)) { @@ -1304,7 +1325,7 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, } /* Try reclaim full clusters if free and nonfull lists are drained */ - if (vm_swap_full()) + if (!swap_is_vswap(si) && vm_swap_full()) swap_reclaim_full_clusters(si, false); if (order < PMD_ORDER) { @@ -1465,7 +1486,8 @@ static void swap_range_alloc(struct swap_info_struct *si, if (vm_swap_full()) schedule_work(&si->reclaim_work); } - atomic_long_sub(nr_entries, &nr_swap_pages); + if (!swap_is_vswap(si)) + atomic_long_sub(nr_entries, &nr_swap_pages); } static void swap_range_free(struct swap_info_struct *si, unsigned long offset, @@ -1474,7 +1496,8 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); - zswap_invalidate(si->type, offset, nr_entries); + if (!swap_is_vswap(si)) + zswap_invalidate(si->type, offset, nr_entries); if (si->flags & SWP_BLKDEV) swap_slot_free_notify = @@ -1493,7 +1516,8 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, * only after the above cleanups are done. */ smp_wmb(); - atomic_long_add(nr_entries, &nr_swap_pages); + if (!swap_is_vswap(si)) + atomic_long_add(nr_entries, &nr_swap_pages); swap_usage_sub(si, nr_entries); } @@ -1881,6 +1905,60 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si, return err; } +static bool folio_may_vswap(struct folio *folio) +{ + struct obj_cgroup *objcg; + bool may_zswap; + + if (!vswap_is_enabled() || !zswap_is_enabled()) + return false; + + /* + * If zswap will not take the folio, writeout has to find a physical + * slot anyway. We are just incurring indirection overhead + * unnecessarily. + */ + objcg = get_obj_cgroup_from_folio(folio); + may_zswap = !objcg || obj_cgroup_may_zswap(objcg); + if (objcg) + obj_cgroup_put(objcg); + return may_zswap; +} + +static bool vswap_alloc(struct folio *folio) +{ + unsigned int order = folio_order(folio); + struct swap_cluster_info *ci; + unsigned long offset; + + local_lock(&percpu_vswap_cluster.lock); + offset = this_cpu_read(percpu_vswap_cluster.offset[order]); + + if (offset != SWAP_ENTRY_INVALID) { + ci = swap_cluster_lock(vswap_si, offset); + if (ci && cluster_is_usable(ci, order)) { + if (cluster_is_empty(ci)) + offset = cluster_offset(vswap_si, ci); + alloc_swap_scan_cluster(vswap_si, ci, folio, offset); + } else if (ci) { + swap_cluster_unlock(ci); + } + } + + if (!folio_test_swapcache(folio)) + cluster_alloc_swap_entry(vswap_si, folio); + + if (folio_test_swapcache(folio)) { + /* alloc_swap_scan_cluster updated percpu offset already */ + local_unlock(&percpu_vswap_cluster.lock); + return true; + } + + this_cpu_write(percpu_vswap_cluster.offset[order], SWAP_ENTRY_INVALID); + local_unlock(&percpu_vswap_cluster.lock); + return false; +} + /** * folio_alloc_swap - allocate swap space for a folio * @folio: folio we want to move to swap @@ -1897,10 +1975,13 @@ int folio_alloc_swap(struct folio *folio) { unsigned int order = folio_order(folio); unsigned int size = 1 << order; + bool may_vswap; VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); + may_vswap = folio_may_vswap(folio); + if (order) { /* * Reject large allocation when THP_SWAP is disabled. Check below @@ -1919,12 +2000,16 @@ int folio_alloc_swap(struct folio *folio) } } + if (may_vswap && vswap_alloc(folio)) + goto done; + again: local_lock(&percpu_swap_cluster.lock); if (!swap_alloc_fast(folio)) swap_alloc_slow(folio); local_unlock(&percpu_swap_cluster.lock); +done: if (!order && unlikely(!folio_test_swapcache(folio))) { if (swap_sync_discard()) goto again; @@ -1942,7 +2027,7 @@ int folio_alloc_swap(struct folio *folio) return 0; failed: - if (get_nr_swap_pages() <= 0) + if (get_nr_swap_pages() <= 0 && !may_vswap) return -ENOSPC; if (mem_cgroup_get_folio_swap_margin(folio) <= 0) return -ENOMEM; -- 2.53.1-Meta