From nobody Tue Feb 10 12:40:28 2026 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 95CB23451C6 for ; Fri, 19 Dec 2025 19:45:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766173536; cv=none; b=izqXEyoYcj95EHuyE2Nds05mkgIc92rWmXHGTj1eyWjldh5yQ9w9hnqhbvTJlz9bwbLffo3EYSK5xds//9VmSALGANroUVDQMdc7/hvUT9wTEUM+o7kKrSgSlfN/tioHLc962e948+f0Zaw6orJE9j0w1pWPnFyDoN8weX9uCz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766173536; c=relaxed/simple; bh=RJLL0Hah2Ooew4GfWCRyEdWrxQz+MRIFKyq2ADRnnOE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=buvywHpCJvBGarBewIahVReA8wnSRf6ia8sy/w8SREe2CA1mc2wp2OVLz48lxdO+C13fL0huhe7AzuOcUiOH8F8IzDAj1YNDawQN9Hr+Gdz4PGhUrwCJ2J4RtpMdnOYZB8Z3hEZPIwJHbGRXJJyrrUHXFwLO6o6pH/5E12fkaqg= 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=Mx+71TdS; arc=none smtp.client-ip=209.85.214.178 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="Mx+71TdS" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2a09d981507so15058555ad.1 for ; Fri, 19 Dec 2025 11:45:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766173534; x=1766778334; 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=C+RQ/TfHSPFWESgUxtvx2lU9JguTovbHRFDwOD/mV7w=; b=Mx+71TdSbDsBw5uWpfmYN9b+lvlJb7kGMVvCK2dVJ1uHjxbqukZ39s8EQF9lVuq7/2 +tQUQpQBHs691CyosoNbH0qsy5bz/OrHJfIkAUnZPFxBF3/7LQaAeO3g3gEe3Dpmy0br yEhyvSNcE7P5iIFT++x/6jp5PQBul8+f7YQLatp/on0zpeJ3z4KpcAGwGccW0oyHrK5a Jj0SeiCG7aIZSQjHmtwjB1t9GqS0c7hnnvRVm//pkw8CteyDoORKhkMWO0uizBLSyrTw bSFzJC1PiF2QFKJxnjOYYk6F52swEqM0QQ7w4btYlqJmvlx42QpcChvbs11wxAaP+hTW HtjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766173534; x=1766778334; 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=C+RQ/TfHSPFWESgUxtvx2lU9JguTovbHRFDwOD/mV7w=; b=Xg2BIw523Uk/WpirMRuBZYqjFID1fLa2JLzE00tON90HB6ESEDPRsruJfh0cnXOZXZ 18Waqce/H3AJufjeZeE4bTcCpDncJIhH1LJAM16EIUQjZcYIxvD0s+HZ6rLsilphaYv+ eCEPYCHSAncpiHoK9cHNp/hn5gNu64o+DWMUPxeapPr8y5VZajhIvrKhqy4NckiOheBm 4Pqw/Pp+iDEI1XY82c/E1zoii4VmbNz6c8LqClYa+S6CutzAfHsbmDG4GMoaANFB8zcX nEwhX2uPF0KLn3r8Mg29tnYzUYviJqrsP/jEwNGz672+Ol4YdywdOjBqR4UpzP+eBOIn rXQA== X-Forwarded-Encrypted: i=1; AJvYcCXOtVr6HUXHGsTVjUkaO0myu29lAdrWuDdyWn3OsrgH11eDUqzIXR5P7ZbFIQRpfnIQdhKiLJzcDc6Y2E8=@vger.kernel.org X-Gm-Message-State: AOJu0YzEG1hihG3ZvhlpUajuGq02NrBAsJ8zkbZgKVWoukA9NJ5SnvLO Kw18/x3elntqV/kk/5ykOeNn3rCdP8wbm9GEMwZsg5hfOvrBbOYBjEH6 X-Gm-Gg: AY/fxX5+NHiDwVmFzBG3REw4tfnsuJQXpdy5O3GnR/xt+lBrt4FjkfSY3NGlKeLnerT IYq0K3jL7+dOqpa3Dhol0eIpBHP/EMefXyIo0vHa6oXuhDQg/FSYdgnyIO12+fHcw0Il+Z3CNYV wvF0hFGFbK0nS6s417oLUld+t/bQ0palMjr+gn5n/iPMB5BHUEex6dId9wiviAnDRWxSkaU3KqF 8N2GxENzNM10Nt0aTollVtOnaybOsbgrtkXLXQ7HLpJ8v2OZsAHdHav/buEPokrs6h8l2rDqcaX uQy5oLKaD5kvy17wysQEjO0JLiNP/3x58axV51J3xr3F60w3aofg/zs0cs+UF4PfvsLbk3WkQ8r M9YyVMXMg133tGjx1tVQN4uQhLLLn4b5xA2MOmHKgWEavWl9LKAKXLDosUxe/pmiWfkOKpxPFsA 83H4brPhk1D2XY8IwKSs3tf/rpnqKizkF/w/GpYQaLseOj5REM3Wph X-Google-Smtp-Source: AGHT+IH/UTCTqpYSmz9cLrJwQg84NY1Mo97k7i39oFV9pTJDKLPSFp149pFzopAt++ZGKki6mXWCFQ== X-Received: by 2002:a17:902:e78c:b0:2a0:d0ae:454d with SMTP id d9443c01a7336-2a2cab4335fmr58302215ad.22.1766173533875; Fri, 19 Dec 2025 11:45:33 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a2f3d76ceesm30170985ad.91.2025.12.19.11.45.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Dec 2025 11:45:33 -0800 (PST) From: Kairui Song Date: Sat, 20 Dec 2025 03:43:45 +0800 Subject: [PATCH v5 16/19] mm, swap: check swap table directly for checking cache 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: <20251220-swap-table-p2-v5-16-8862a265a033@tencent.com> References: <20251220-swap-table-p2-v5-0-8862a265a033@tencent.com> In-Reply-To: <20251220-swap-table-p2-v5-0-8862a265a033@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=1766173451; l=7912; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=6WuwtHjxFkVBfgapei4Gm4oolBwvY/HEU9CfiVMF7mo=; b=eyypQBKFn6nJdcyrPA2kVE7MGM8ssqk71hkL0pP6t7Ns6U3cc+hHDDoOMIRLaoDkXUp26Te77 mpSbCIGxjtkDOMMG2Z2ZOw1FpaXoPdrAZAeOSfEyEWgBznKVO3FXQ/a X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song Instead of looking at the swap map, check swap table directly to tell if a swap slot is cached. Prepares for the removal of SWAP_HAS_CACHE. Signed-off-by: Kairui Song --- mm/swap.h | 11 ++++++++--- mm/swap_state.c | 16 ++++++++++++++++ mm/swapfile.c | 55 +++++++++++++++++++++++++++++-----------------------= --- mm/userfaultfd.c | 10 +++------- 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index ec1ef7d0c35b..3692e143eeba 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -275,6 +275,7 @@ void __swapcache_clear_cached(struct swap_info_struct *= si, * swap entries in the page table, similar to locking swap cache folio. * - See the comment of get_swap_device() for more complex usage. */ +bool swap_cache_has_folio(swp_entry_t entry); struct folio *swap_cache_get_folio(swp_entry_t entry); void *swap_cache_get_shadow(swp_entry_t entry); void swap_cache_del_folio(struct folio *folio); @@ -335,8 +336,6 @@ static inline int swap_zeromap_batch(swp_entry_t entry,= int max_nr, =20 static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) { - struct swap_info_struct *si =3D __swap_entry_to_info(entry); - pgoff_t offset =3D swp_offset(entry); int i; =20 /* @@ -345,8 +344,9 @@ static inline int non_swapcache_batch(swp_entry_t entry= , int max_nr) * be in conflict with the folio in swap cache. */ for (i =3D 0; i < max_nr; i++) { - if ((si->swap_map[offset + i] & SWAP_HAS_CACHE)) + if (swap_cache_has_folio(entry)) return i; + entry.val++; } =20 return i; @@ -449,6 +449,11 @@ static inline int swap_writeout(struct folio *folio, return 0; } =20 +static inline bool swap_cache_has_folio(swp_entry_t entry) +{ + return false; +} + static inline struct folio *swap_cache_get_folio(swp_entry_t entry) { return NULL; diff --git a/mm/swap_state.c b/mm/swap_state.c index 29fa8d313a79..0ff6c09ee702 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -103,6 +103,22 @@ struct folio *swap_cache_get_folio(swp_entry_t entry) return NULL; } =20 +/** + * swap_cache_has_folio - Check if a swap slot has cache. + * @entry: swap entry indicating the slot. + * + * Context: Caller must ensure @entry is valid and protect the swap + * device with reference count or locks. + */ +bool swap_cache_has_folio(swp_entry_t entry) +{ + unsigned long swp_tb; + + swp_tb =3D swap_table_get(__swap_entry_to_cluster(entry), + swp_cluster_offset(entry)); + return swp_tb_is_folio(swp_tb); +} + /** * swap_cache_get_shadow - Looks up a shadow in the swap cache. * @entry: swap entry used for the lookup. diff --git a/mm/swapfile.c b/mm/swapfile.c index de0f4c1352cb..91436ee41446 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -792,23 +792,18 @@ static bool cluster_reclaim_range(struct swap_info_st= ruct *si, unsigned int nr_pages =3D 1 << order; unsigned long offset =3D start, end =3D start + nr_pages; unsigned char *map =3D si->swap_map; - int nr_reclaim; + unsigned long swp_tb; =20 spin_unlock(&ci->lock); do { - switch (READ_ONCE(map[offset])) { - case 0: + if (swap_count(READ_ONCE(map[offset]))) break; - case SWAP_HAS_CACHE: - nr_reclaim =3D __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); - if (nr_reclaim < 0) - goto out; - break; - default: - goto out; + swp_tb =3D swap_table_get(ci, offset % SWAPFILE_CLUSTER); + if (swp_tb_is_folio(swp_tb)) { + if (__try_to_reclaim_swap(si, offset, TTRS_ANYWAY) < 0) + break; } } while (++offset < end); -out: spin_lock(&ci->lock); =20 /* @@ -829,37 +824,41 @@ static bool cluster_reclaim_range(struct swap_info_st= ruct *si, * Recheck the range no matter reclaim succeeded or not, the slot * could have been be freed while we are not holding the lock. */ - for (offset =3D start; offset < end; offset++) - if (READ_ONCE(map[offset])) + for (offset =3D start; offset < end; offset++) { + swp_tb =3D __swap_table_get(ci, offset % SWAPFILE_CLUSTER); + if (swap_count(map[offset]) || !swp_tb_is_null(swp_tb)) return false; + } =20 return true; } =20 static bool cluster_scan_range(struct swap_info_struct *si, struct swap_cluster_info *ci, - unsigned long start, unsigned int nr_pages, + unsigned long offset, unsigned int nr_pages, bool *need_reclaim) { - unsigned long offset, end =3D start + nr_pages; + unsigned long end =3D offset + nr_pages; unsigned char *map =3D si->swap_map; + unsigned long swp_tb; =20 if (cluster_is_empty(ci)) return true; =20 - for (offset =3D start; offset < end; offset++) { - switch (READ_ONCE(map[offset])) { - case 0: - continue; - case SWAP_HAS_CACHE: + do { + if (swap_count(map[offset])) + return false; + swp_tb =3D __swap_table_get(ci, offset % SWAPFILE_CLUSTER); + if (swp_tb_is_folio(swp_tb)) { + WARN_ON_ONCE(!(map[offset] & SWAP_HAS_CACHE)); if (!vm_swap_full()) return false; *need_reclaim =3D true; - continue; - default: - return false; + } else { + /* A entry with no count and no cache must be null */ + VM_WARN_ON_ONCE(!swp_tb_is_null(swp_tb)); } - } + } while (++offset < end); =20 return true; } @@ -1030,7 +1029,8 @@ static void swap_reclaim_full_clusters(struct swap_in= fo_struct *si, bool force) to_scan--; =20 while (offset < end) { - if (READ_ONCE(map[offset]) =3D=3D SWAP_HAS_CACHE) { + if (!swap_count(READ_ONCE(map[offset])) && + swp_tb_is_folio(__swap_table_get(ci, offset % SWAPFILE_CLUSTER))) { spin_unlock(&ci->lock); nr_reclaim =3D __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); @@ -1981,6 +1981,7 @@ void swap_put_entries_direct(swp_entry_t entry, int n= r) struct swap_info_struct *si; bool any_only_cache =3D false; unsigned long offset; + unsigned long swp_tb; =20 si =3D get_swap_device(entry); if (WARN_ON_ONCE(!si)) @@ -2005,7 +2006,9 @@ void swap_put_entries_direct(swp_entry_t entry, int n= r) */ for (offset =3D start_offset; offset < end_offset; offset +=3D nr) { nr =3D 1; - if (READ_ONCE(si->swap_map[offset]) =3D=3D SWAP_HAS_CACHE) { + swp_tb =3D swap_table_get(__swap_offset_to_cluster(si, offset), + offset % SWAPFILE_CLUSTER); + if (!swap_count(READ_ONCE(si->swap_map[offset])) && swp_tb_is_folio(swp_= tb)) { /* * Folios are always naturally aligned in swap so * advance forward to the next boundary. Zero means no diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index b11f81095fa5..46256e7a3d51 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1190,17 +1190,13 @@ static int move_swap_pte(struct mm_struct *mm, stru= ct vm_area_struct *dst_vma, * Check if the swap entry is cached after acquiring the src_pte * lock. Otherwise, we might miss a newly loaded swap cache folio. * - * Check swap_map directly to minimize overhead, READ_ONCE is sufficient. * We are trying to catch newly added swap cache, the only possible case= is * when a folio is swapped in and out again staying in swap cache, using= the * same entry before the PTE check above. The PTL is acquired and releas= ed - * twice, each time after updating the swap_map's flag. So holding - * the PTL here ensures we see the updated value. False positive is poss= ible, - * e.g. SWP_SYNCHRONOUS_IO swapin may set the flag without touching the - * cache, or during the tiny synchronization window between swap cache a= nd - * swap_map, but it will be gone very quickly, worst result is retry jit= ters. + * twice, each time after updating the swap table. So holding + * the PTL here ensures we see the updated value. */ - if (READ_ONCE(si->swap_map[swp_offset(entry)]) & SWAP_HAS_CACHE) { + if (swap_cache_has_folio(entry)) { double_pt_unlock(dst_ptl, src_ptl); return -EAGAIN; } --=20 2.52.0