From nobody Tue Dec 2 01:06:32 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8DA622D94A9 for ; Mon, 24 Nov 2025 19:16:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764011815; cv=none; b=VRKF1XYen6otQdyHJznhjsl+/ZCvcDArdnRsNX1BmlbPehIEwEc1ugra2g5E2ePGsxjauCMajfzvmn8lZ6cuRPGvI8NVSOBA0esJ2aFLZs5hWul8ez7CFJpm2Nr/MJ47IFVDmsXhWFDNFYWUbBanWb254Ahi+LHFC5xMO0PikN8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764011815; c=relaxed/simple; bh=zNuICNwfmeIL82ZwwGgUJSCGnrZFTpE2skv72HAUrrA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RYImPiZx2BYXibtPwp7v57YHJZ2ffbnJY4m4/xVdeEEyP8wT8mcK0VmYXtvV2/K/Aokq4ERWbM0Q4t1A0s9MFvxWHM3qrYvOUjqmIWWycNmwvF92aQf5rtRIUCJezuHXwB+3YpWLZym3wYFO7OZxnBeP64X7rNt+Fs8zH+HxFSI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=QK/wdWmn; arc=none smtp.client-ip=209.85.210.179 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.b="QK/wdWmn" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7ba49f92362so2731969b3a.1 for ; Mon, 24 Nov 2025 11:16:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764011813; x=1764616613; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=dW2hSKXTwR8T2CwBP/MRRLrpmfjOQ9zqQiuqar3vsV4=; b=QK/wdWmn4GXr1vRmqIoU1OSvqL0C+q97RI71H+aK7ILsMu+WBvDsaE5x4gUD+vSaxp AzUabTjYktM+2RLmA0uRSQyvBwEtf+ThA7u/nU9eXobvLDuJ8XusgJJmgQJIRFNg5kFB 0xFmzfRm0zpt3AiM8Ndxrsc+nNKiNeXTu8wXQUm5SEAYJrrd6Xz5rU9ewmRLrUyFzi2l EaIDmvZ+zTXObufnsDwYpGAs0TybLhwz5VD52YR2CtKKB3+JhD65OFhq6KEwRJSUjVO9 sNxcGYkTK8AWuUy4+jmJT5778PRJe/OAUz+XHYx7zWBmfKhGiBtRNkKKDFElQ+88mSz2 skFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764011813; x=1764616613; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=dW2hSKXTwR8T2CwBP/MRRLrpmfjOQ9zqQiuqar3vsV4=; b=fif/w8aEKGaJ/0A1scQLSxWNFV8g06HGpWorrGPtJ2peeVhXeNRtPQAvFuWhMghE0r gqkNTwj0JVT0jSJzPxVxt0HSFXlKL0VLgjFyo7fWFs99mPsz5RwI0j0205BkcQZ/VN34 Xk2xCVA5uFx16apSvAk9dzQV9wrJXmtyoxZNLujuI0cJxggTJQg/mg9x03YSpZWbYanO jRDfNVCasQjGWJlpz9gkBgB6owrE7obUj+4YC36+eHUDRTyteW9AkEFDF5H5NG+3tjUh eBR+lBuX0c0iBbKytXPiQXN7LndbmdOvlo87fUKpy8ZZDaVNOLFm46sbA7OBADK2d87+ FE+w== X-Forwarded-Encrypted: i=1; AJvYcCV9zE0bQurcUipO5lbKWvfvDrxUOXtG6G+Bh3o06TkzCppOexSi8q+DUwUZnT0rv1ofX9taktUrV/jyl7w=@vger.kernel.org X-Gm-Message-State: AOJu0YwtSHWlpe0nC49YzHRkl/dGQDJogDfLqdddYtFYv7ESBoYVvSyt QiLl7WkSaDu+LyaIMJqtAP+FcgC3N5qCydNZ/2/MjE3OO3T8s8Y2aCGU X-Gm-Gg: ASbGnct9iw3olB2ErVfQK3bRvjNff63nm9Ptk7vK+Lk2stNBeYS/CB7wWxR3PyfH+OL /8UimrtzT60q/wBjTh2KrMx8AP39H2NSqpG5TmwtRCYiUL3UcBry9fTOPhTyOUN1agUWiGRs4a8 Ho1pALKTIsEKrBz8LKMbLLuC3+YUCQHY9u0Q2SwYws/jHmcRqACe5NHyaam8FKTQj5vmFHJUvzO hzqsLwQg+MaeED0tAMvpO5JsGeY4WJNi1ayhyeHttP8k8LxbsOjo2q+xQ0f39aGVNACaDauU9JH XMZQUvD8VcDih/Dc+D72AvhvIgMdfl2tfQtdmHR4LiwgkhpuxLm+A0xy7QC6X0ybHYscaUmFEEf SDC16j1ZlIe43CAPfdN/xdyTIZqqR8gfbLR02gu8jAGXMQbKDf09wJBo4yCi7s7IBZ4DddtHdIi g0qJt24pFUjs7nkBjbYkXmIuGWAsC3S/6N1BS34uPZvTIit/5tStkQ3ndXZDc= X-Google-Smtp-Source: AGHT+IEJWjl/z5j8RKVC9K8o8FTnjnCf1r/hFIhxiR9Mkw5UkeNOePRPXbwYfdwjKuCnb/LdcCXnPA== X-Received: by 2002:a05:6a20:a125:b0:35e:835:7ec9 with SMTP id adf61e73a8af0-3613e506c63mr20155913637.17.1764011812685; Mon, 24 Nov 2025 11:16:52 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-bd75def75ffsm14327479a12.3.2025.11.24.11.16.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Nov 2025 11:16:52 -0800 (PST) From: Kairui Song Date: Tue, 25 Nov 2025 03:13:58 +0800 Subject: [PATCH v3 15/19] mm, swap: add folio to swap cache directly on allocation Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251125-swap-table-p2-v3-15-33f54f707a5c@tencent.com> References: <20251125-swap-table-p2-v3-0-33f54f707a5c@tencent.com> In-Reply-To: <20251125-swap-table-p2-v3-0-33f54f707a5c@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Baoquan He , Barry Song , Chris Li , Nhat Pham , Yosry Ahmed , David Hildenbrand , Johannes Weiner , Youngjun Park , Hugh Dickins , Baolin Wang , Ying Huang , Kemeng Shi , Lorenzo Stoakes , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org, Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1764011730; l=18567; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=isfv5Ia1RYLaAnBGIOaD0Ewhzh9QTFk5l53JXG2l+S8=; b=2Dbui++dBDCvWX+pGy0ME3ZWHBHPq8rezk3u4V4eX4x/XiUlCH/eDWkpliHmO2KltawfWtXLR 58O6D2CwyvXBY3DqG+LD1w1k00URJ+8u/R1vvPe0XK/yGwIhtHDm1yk X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song The allocator uses SWAP_HAS_CACHE to pin a swap slot upon allocation. SWAP_HAS_CACHE is being deprecated as it caused a lot of confusion. This pinning usage here can be dropped by adding the folio to swap cache directly on allocation. All swap allocations are folio-based now (except for hibernation), so the swap allocator can always take the folio as the parameter. And now both swap cache (swap table) and swap map are protected by the cluster lock, scanning the map and inserting the folio can be done in the same critical section. This eliminates the time window that a slot is pinned by SWAP_HAS_CACHE, but it has no cache, and avoids touching the lock multiple times. This is both a cleanup and an optimization. Signed-off-by: Kairui Song --- include/linux/swap.h | 5 -- mm/swap.h | 10 +--- mm/swap_state.c | 58 +++++++++++-------- mm/swapfile.c | 157 +++++++++++++++++++++--------------------------= ---- 4 files changed, 101 insertions(+), 129 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index ac3caa4c6999..4b4b81fbc6a3 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -452,7 +452,6 @@ static inline long get_nr_swap_pages(void) } =20 extern void si_swapinfo(struct sysinfo *); -void put_swap_folio(struct folio *folio, swp_entry_t entry); extern int add_swap_count_continuation(swp_entry_t, gfp_t); int swap_type_of(dev_t device, sector_t offset); int find_first_swap(dev_t *device); @@ -534,10 +533,6 @@ static inline void swap_put_entries_direct(swp_entry_t= ent, int nr) { } =20 -static inline void put_swap_folio(struct folio *folio, swp_entry_t swp) -{ -} - static inline int __swap_count(swp_entry_t entry) { return 0; diff --git a/mm/swap.h b/mm/swap.h index 9ed12936b889..ec1ef7d0c35b 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -277,13 +277,13 @@ void __swapcache_clear_cached(struct swap_info_struct= *si, */ struct folio *swap_cache_get_folio(swp_entry_t entry); void *swap_cache_get_shadow(swp_entry_t entry); -int swap_cache_add_folio(struct folio *folio, swp_entry_t entry, - void **shadow, bool alloc); void swap_cache_del_folio(struct folio *folio); struct folio *swap_cache_alloc_folio(swp_entry_t entry, gfp_t gfp_flags, struct mempolicy *mpol, pgoff_t ilx, bool *alloced); /* Below helpers require the caller to lock and pass in the swap cluster. = */ +void __swap_cache_add_folio(struct swap_cluster_info *ci, + struct folio *folio, swp_entry_t entry); void __swap_cache_del_folio(struct swap_cluster_info *ci, struct folio *folio, swp_entry_t entry, void *shadow); void __swap_cache_replace_folio(struct swap_cluster_info *ci, @@ -459,12 +459,6 @@ static inline void *swap_cache_get_shadow(swp_entry_t = entry) return NULL; } =20 -static inline int swap_cache_add_folio(struct folio *folio, swp_entry_t en= try, - void **shadow, bool alloc) -{ - return -ENOENT; -} - static inline void swap_cache_del_folio(struct folio *folio) { } diff --git a/mm/swap_state.c b/mm/swap_state.c index c29b7e386a7c..eb7710120d5f 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -122,35 +122,56 @@ void *swap_cache_get_shadow(swp_entry_t entry) return NULL; } =20 +void __swap_cache_add_folio(struct swap_cluster_info *ci, + struct folio *folio, swp_entry_t entry) +{ + unsigned long new_tb; + unsigned int ci_start, ci_off, ci_end; + unsigned long nr_pages =3D folio_nr_pages(folio); + + VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); + VM_WARN_ON_ONCE_FOLIO(folio_test_swapcache(folio), folio); + VM_WARN_ON_ONCE_FOLIO(!folio_test_swapbacked(folio), folio); + + new_tb =3D folio_to_swp_tb(folio); + ci_start =3D swp_cluster_offset(entry); + ci_off =3D ci_start; + ci_end =3D ci_start + nr_pages; + do { + VM_WARN_ON_ONCE(swp_tb_is_folio(__swap_table_get(ci, ci_off))); + __swap_table_set(ci, ci_off, new_tb); + } while (++ci_off < ci_end); + + folio_ref_add(folio, nr_pages); + folio_set_swapcache(folio); + folio->swap =3D entry; + + node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages); + lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages); +} + /** * swap_cache_add_folio - Add a folio into the swap cache. * @folio: The folio to be added. * @entry: The swap entry corresponding to the folio. * @gfp: gfp_mask for XArray node allocation. * @shadowp: If a shadow is found, return the shadow. - * @alloc: If it's the allocator that is trying to insert a folio. Allocat= or - * sets SWAP_HAS_CACHE to pin slots before insert so skip map upda= te. * * Context: Caller must ensure @entry is valid and protect the swap device * with reference count or locks. */ -int swap_cache_add_folio(struct folio *folio, swp_entry_t entry, - void **shadowp, bool alloc) +static int swap_cache_add_folio(struct folio *folio, swp_entry_t entry, + void **shadowp) { int err; void *shadow =3D NULL; + unsigned long old_tb; struct swap_info_struct *si; - unsigned long old_tb, new_tb; struct swap_cluster_info *ci; unsigned int ci_start, ci_off, ci_end, offset; unsigned long nr_pages =3D folio_nr_pages(folio); =20 - VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); - VM_WARN_ON_ONCE_FOLIO(folio_test_swapcache(folio), folio); - VM_WARN_ON_ONCE_FOLIO(!folio_test_swapbacked(folio), folio); - si =3D __swap_entry_to_info(entry); - new_tb =3D folio_to_swp_tb(folio); ci_start =3D swp_cluster_offset(entry); ci_end =3D ci_start + nr_pages; ci_off =3D ci_start; @@ -166,7 +187,7 @@ int swap_cache_add_folio(struct folio *folio, swp_entry= _t entry, err =3D -EEXIST; goto failed; } - if (!alloc && unlikely(!__swap_count(swp_entry(swp_type(entry), offset))= )) { + if (unlikely(!__swap_count(swp_entry(swp_type(entry), offset)))) { err =3D -ENOENT; goto failed; } @@ -182,20 +203,11 @@ int swap_cache_add_folio(struct folio *folio, swp_ent= ry_t entry, * Still need to pin the slots with SWAP_HAS_CACHE since * swap allocator depends on that. */ - if (!alloc) - __swapcache_set_cached(si, ci, swp_entry(swp_type(entry), offset)); - __swap_table_set(ci, ci_off, new_tb); + __swapcache_set_cached(si, ci, swp_entry(swp_type(entry), offset)); offset++; } while (++ci_off < ci_end); - - folio_ref_add(folio, nr_pages); - folio_set_swapcache(folio); - folio->swap =3D entry; + __swap_cache_add_folio(ci, folio, entry); swap_cluster_unlock(ci); - - node_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages); - lruvec_stat_mod_folio(folio, NR_SWAPCACHE, nr_pages); - if (shadowp) *shadowp =3D shadow; return 0; @@ -464,7 +476,7 @@ static struct folio *__swap_cache_prepare_and_add(swp_e= ntry_t entry, __folio_set_locked(folio); __folio_set_swapbacked(folio); for (;;) { - ret =3D swap_cache_add_folio(folio, entry, &shadow, false); + ret =3D swap_cache_add_folio(folio, entry, &shadow); if (!ret) break; =20 diff --git a/mm/swapfile.c b/mm/swapfile.c index 7890039d2f65..91368294170f 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -884,28 +884,53 @@ static void swap_cluster_assert_table_empty(struct sw= ap_cluster_info *ci, } } =20 -static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_c= luster_info *ci, - unsigned int start, unsigned char usage, - unsigned int order) +static bool cluster_alloc_range(struct swap_info_struct *si, + struct swap_cluster_info *ci, + struct folio *folio, + unsigned int offset) { - unsigned int nr_pages =3D 1 << order; + unsigned long nr_pages; + unsigned int order; =20 lockdep_assert_held(&ci->lock); =20 if (!(si->flags & SWP_WRITEOK)) return false; =20 + /* + * All mm swap allocation starts with a folio (folio_alloc_swap), + * it's also the only allocation path for large orders allocation. + * Such swap slots starts with count =3D=3D 0 and will be increased + * upon folio unmap. + * + * Else, it's a exclusive order 0 allocation for hibernation. + * The slot starts with count =3D=3D 1 and never increases. + */ + if (likely(folio)) { + order =3D folio_order(folio); + nr_pages =3D 1 << order; + /* + * Pin the slot with SWAP_HAS_CACHE to satisfy swap_dup_entries. + * This is the legacy allocation behavior, will drop it very soon. + */ + memset(si->swap_map + offset, SWAP_HAS_CACHE, nr_pages); + __swap_cache_add_folio(ci, folio, swp_entry(si->type, offset)); + } else { + order =3D 0; + nr_pages =3D 1; + WARN_ON_ONCE(si->swap_map[offset]); + si->swap_map[offset] =3D 1; + swap_cluster_assert_table_empty(ci, offset, 1); + } + /* * The first allocation in a cluster makes the * cluster exclusive to this order */ if (cluster_is_empty(ci)) ci->order =3D order; - - memset(si->swap_map + start, usage, nr_pages); - swap_cluster_assert_table_empty(ci, start, nr_pages); - swap_range_alloc(si, nr_pages); ci->count +=3D nr_pages; + swap_range_alloc(si, nr_pages); =20 return true; } @@ -913,13 +938,12 @@ static bool cluster_alloc_range(struct swap_info_stru= ct *si, struct swap_cluster /* Try use a new cluster for current CPU and allocate from it. */ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci, - unsigned long offset, - unsigned int order, - unsigned char usage) + struct folio *folio, unsigned long offset) { unsigned int next =3D SWAP_ENTRY_INVALID, found =3D SWAP_ENTRY_INVALID; unsigned long start =3D ALIGN_DOWN(offset, SWAPFILE_CLUSTER); unsigned long end =3D min(start + SWAPFILE_CLUSTER, si->max); + unsigned int order =3D likely(folio) ? folio_order(folio) : 0; unsigned int nr_pages =3D 1 << order; bool need_reclaim, ret, usable; =20 @@ -943,7 +967,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap= _info_struct *si, if (!ret) continue; } - if (!cluster_alloc_range(si, ci, offset, usage, order)) + if (!cluster_alloc_range(si, ci, folio, offset)) break; found =3D offset; offset +=3D nr_pages; @@ -965,8 +989,7 @@ static unsigned int alloc_swap_scan_cluster(struct swap= _info_struct *si, =20 static unsigned int alloc_swap_scan_list(struct swap_info_struct *si, struct list_head *list, - unsigned int order, - unsigned char usage, + struct folio *folio, bool scan_all) { unsigned int found =3D SWAP_ENTRY_INVALID; @@ -978,7 +1001,7 @@ static unsigned int alloc_swap_scan_list(struct swap_i= nfo_struct *si, if (!ci) break; offset =3D cluster_offset(si, ci); - found =3D alloc_swap_scan_cluster(si, ci, offset, order, usage); + found =3D alloc_swap_scan_cluster(si, ci, folio, offset); if (found) break; } while (scan_all); @@ -1039,10 +1062,11 @@ static void swap_reclaim_work(struct work_struct *w= ork) * Try to allocate swap entries with specified order and try set a new * cluster for current CPU too. */ -static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si,= int order, - unsigned char usage) +static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, + struct folio *folio) { struct swap_cluster_info *ci; + unsigned int order =3D likely(folio) ? folio_order(folio) : 0; unsigned int offset =3D SWAP_ENTRY_INVALID, found =3D SWAP_ENTRY_INVALID; =20 /* @@ -1064,8 +1088,7 @@ static unsigned long cluster_alloc_swap_entry(struct = swap_info_struct *si, int o if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset =3D cluster_offset(si, ci); - found =3D alloc_swap_scan_cluster(si, ci, offset, - order, usage); + found =3D alloc_swap_scan_cluster(si, ci, folio, offset); } else { swap_cluster_unlock(ci); } @@ -1079,22 +1102,19 @@ static unsigned long cluster_alloc_swap_entry(struc= t swap_info_struct *si, int o * to spread out the writes. */ if (si->flags & SWP_PAGE_DISCARD) { - found =3D alloc_swap_scan_list(si, &si->free_clusters, order, usage, - false); + found =3D alloc_swap_scan_list(si, &si->free_clusters, folio, false); if (found) goto done; } =20 if (order < PMD_ORDER) { - found =3D alloc_swap_scan_list(si, &si->nonfull_clusters[order], - order, usage, true); + found =3D alloc_swap_scan_list(si, &si->nonfull_clusters[order], folio, = true); if (found) goto done; } =20 if (!(si->flags & SWP_PAGE_DISCARD)) { - found =3D alloc_swap_scan_list(si, &si->free_clusters, order, usage, - false); + found =3D alloc_swap_scan_list(si, &si->free_clusters, folio, false); if (found) goto done; } @@ -1110,8 +1130,7 @@ static unsigned long cluster_alloc_swap_entry(struct = swap_info_struct *si, int o * failure is not critical. Scanning one cluster still * keeps the list rotated and reclaimed (for HAS_CACHE). */ - found =3D alloc_swap_scan_list(si, &si->frag_clusters[order], order, - usage, false); + found =3D alloc_swap_scan_list(si, &si->frag_clusters[order], folio, fal= se); if (found) goto done; } @@ -1125,13 +1144,11 @@ static unsigned long cluster_alloc_swap_entry(struc= t swap_info_struct *si, int o * Clusters here have at least one usable slots and can't fail order 0 * allocation, but reclaim may drop si->lock and race with another user. */ - found =3D alloc_swap_scan_list(si, &si->frag_clusters[o], - 0, usage, true); + found =3D alloc_swap_scan_list(si, &si->frag_clusters[o], folio, true); if (found) goto done; =20 - found =3D alloc_swap_scan_list(si, &si->nonfull_clusters[o], - 0, usage, true); + found =3D alloc_swap_scan_list(si, &si->nonfull_clusters[o], folio, true= ); if (found) goto done; } @@ -1322,12 +1339,12 @@ static bool get_swap_device_info(struct swap_info_s= truct *si) * Fast path try to get swap entries with specified order from current * CPU's swap entry pool (a cluster). */ -static bool swap_alloc_fast(swp_entry_t *entry, - int order) +static bool swap_alloc_fast(struct folio *folio) { + unsigned int order =3D folio_order(folio); struct swap_cluster_info *ci; struct swap_info_struct *si; - unsigned int offset, found =3D SWAP_ENTRY_INVALID; + unsigned int offset; =20 /* * Once allocated, swap_info_struct will never be completely freed, @@ -1342,22 +1359,18 @@ static bool swap_alloc_fast(swp_entry_t *entry, if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset =3D cluster_offset(si, ci); - found =3D alloc_swap_scan_cluster(si, ci, offset, order, SWAP_HAS_CACHE); - if (found) - *entry =3D swp_entry(si->type, found); + alloc_swap_scan_cluster(si, ci, folio, offset); } else { swap_cluster_unlock(ci); } =20 put_swap_device(si); - return !!found; + return folio_test_swapcache(folio); } =20 /* Rotate the device and switch to a new cluster */ -static void swap_alloc_slow(swp_entry_t *entry, - int order) +static void swap_alloc_slow(struct folio *folio) { - unsigned long offset; struct swap_info_struct *si, *next; =20 spin_lock(&swap_avail_lock); @@ -1367,13 +1380,11 @@ static void swap_alloc_slow(swp_entry_t *entry, plist_requeue(&si->avail_list, &swap_avail_head); spin_unlock(&swap_avail_lock); if (get_swap_device_info(si)) { - offset =3D cluster_alloc_swap_entry(si, order, SWAP_HAS_CACHE); + cluster_alloc_swap_entry(si, folio); put_swap_device(si); - if (offset) { - *entry =3D swp_entry(si->type, offset); + if (folio_test_swapcache(folio)) return; - } - if (order) + if (folio_test_large(folio)) return; } =20 @@ -1434,7 +1445,6 @@ int folio_alloc_swap(struct folio *folio) { unsigned int order =3D folio_order(folio); unsigned int size =3D 1 << order; - swp_entry_t entry =3D {}; =20 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); @@ -1459,39 +1469,23 @@ int folio_alloc_swap(struct folio *folio) =20 again: local_lock(&percpu_swap_cluster.lock); - if (!swap_alloc_fast(&entry, order)) - swap_alloc_slow(&entry, order); + if (!swap_alloc_fast(folio)) + swap_alloc_slow(folio); local_unlock(&percpu_swap_cluster.lock); =20 - if (unlikely(!order && !entry.val)) { + if (!order && unlikely(!folio_test_swapcache(folio))) { if (swap_sync_discard()) goto again; } =20 /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */ - if (mem_cgroup_try_charge_swap(folio, entry)) - goto out_free; + if (unlikely(mem_cgroup_try_charge_swap(folio, folio->swap))) + swap_cache_del_folio(folio); =20 - if (!entry.val) + if (unlikely(!folio_test_swapcache(folio))) return -ENOMEM; =20 - /* - * Allocator has pinned the slots with SWAP_HAS_CACHE - * so it should never fail - */ - WARN_ON_ONCE(swap_cache_add_folio(folio, entry, NULL, true)); - - /* - * Allocator should always allocate aligned entries so folio based - * operations never crossed more than one cluster. - */ - VM_WARN_ON_ONCE_FOLIO(!IS_ALIGNED(folio->swap.val, size), folio); - return 0; - -out_free: - put_swap_folio(folio, entry); - return -ENOMEM; } =20 /** @@ -1790,29 +1784,6 @@ static void swap_entries_free(struct swap_info_struc= t *si, partial_free_cluster(si, ci); } =20 -/* - * Called after dropping swapcache to decrease refcnt to swap entries. - */ -void put_swap_folio(struct folio *folio, swp_entry_t entry) -{ - struct swap_info_struct *si; - struct swap_cluster_info *ci; - unsigned long offset =3D swp_offset(entry); - int size =3D 1 << swap_entry_order(folio_order(folio)); - - si =3D _swap_info_get(entry); - if (!si) - return; - - ci =3D swap_cluster_lock(si, offset); - if (swap_only_has_cache(si, offset, size)) - swap_entries_free(si, ci, entry, size); - else - for (int i =3D 0; i < size; i++, entry.val++) - swap_entry_put_locked(si, ci, entry, SWAP_HAS_CACHE); - swap_cluster_unlock(ci); -} - int __swap_count(swp_entry_t entry) { struct swap_info_struct *si =3D __swap_entry_to_info(entry); @@ -2063,7 +2034,7 @@ swp_entry_t swap_alloc_hibernation_slot(int type) * with swap table allocation. */ local_lock(&percpu_swap_cluster.lock); - offset =3D cluster_alloc_swap_entry(si, 0, 1); + offset =3D cluster_alloc_swap_entry(si, NULL); local_unlock(&percpu_swap_cluster.lock); if (offset) entry =3D swp_entry(si->type, offset); --=20 2.52.0