From nobody Fri Apr 3 01:24:28 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5D72378829 for ; Tue, 17 Feb 2026 20:06:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771358809; cv=none; b=Ex6lVjHp57G1rfo1t32B3Hw6f+OuJ6ASvGTJ9GKxM9vfuRe/sqi7Vezh8i+fA/xl87vRiqACYD+1WniCO+pYCJzWvNwX/IIpYhEDUyYbxWkllgXiS9X/IJlN1Jc6gZdDjoJxcqXvEBaN3+8254M4NMS0E/aXLnOd+bGbxdZz3ik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771358809; c=relaxed/simple; bh=bRCmnJbeggUZ91UjveLB02es9HZ0T/a5dTIwJjGY6v8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JztzxsmwXbKoRz4SbIBhjr4PnTAu/gMAfm6nm21pTfNRj+uTTfBw9p0rVAIvUKdkYc42V+tH534922OCZqTodwgVdlGk2Oq6+ioylp2xfnROpoS6WXwbZp8DQmP1HXU6kJGJua076mYLLgMg2mT/Uz8WiCbY+zNDoX2qr33LBZQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CS1J3qO0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CS1J3qO0" Received: by smtp.kernel.org (Postfix) with ESMTPS id 91414C19421; Tue, 17 Feb 2026 20:06:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771358809; bh=bRCmnJbeggUZ91UjveLB02es9HZ0T/a5dTIwJjGY6v8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=CS1J3qO0oP+QoHWETy+CXdOFfQK8ASdqXpu0z2U8t8QUw9iHL0BLf9j+BA621agEM oPP5GEvSoBn9ukXjr8Vm7LcSj+KcBEjhwru3KTzp0dqeKgpffiQu1fJpWWKkdz9FwE gCqyHZwUV/GlHNt1gJn1eOlB+beP65oCMeFHcfAo0oZVIbgfVsYoOpHvP/6b0NtXpj nZOXt7IVUyb2M0shJML/x9Un67380KvOHVgxHvvvEGQxq44R4b/pqTPD6YQgIwmaj4 /qxKMNodCprIWI1e1LhkWF9zIC6yTz3SHCAE3LNOujoq4w0vRfZ7lVksHCpqsTX67u S1MFY3NLmtiNA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A0EDE9A03F; Tue, 17 Feb 2026 20:06:49 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 18 Feb 2026 04:06:36 +0800 Subject: [PATCH v3 11/12] mm, swap: simplify checking if a folio is swapped 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: <20260218-swap-table-p3-v3-11-f4e34be021a7@tencent.com> References: <20260218-swap-table-p3-v3-0-f4e34be021a7@tencent.com> In-Reply-To: <20260218-swap-table-p3-v3-0-f4e34be021a7@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Johannes Weiner , David Hildenbrand , Lorenzo Stoakes , Youngjun Park , linux-kernel@vger.kernel.org, Chris Li , Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1771358806; l=7000; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=RmCYZA5fOPzN8Ok6mc+EihDY+qlYT4Nzp3aMmwNhDA8=; b=up3YTmSAV9fsExsL7mQJThV10LYZGkNQazOsqxJmgM05t1yZjP5R3KKgz/Mnn0sC0K+68bFMk EgllSQjSXwTCyY/g/F6d4V1i41f5KvPore41jSZwnrGbSgEKZti4JUl X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com From: Kairui Song Clean up and simplify how we check if a folio is swapped. The helper already requires the folio to be in swap cache and locked. That's enough to pin the swap cluster from being freed, so there is no need to lock anything else to avoid UAF. And besides, we have cleaned up and defined the swap operation to be mostly folio based, and now the only place a folio will have any of its swap slots' count increased from 0 to 1 is folio_dup_swap, which also requires the folio lock. So as we are holding the folio lock here, a folio can't change its swap status from not swapped (all swap slots have a count of 0) to swapped (any slot has a swap count larger than 0). So there won't be any false negatives of this helper if we simply depend on the folio lock to stabilize the cluster. We are only using this helper to determine if we can and should release the swap cache. So false positives are completely harmless, and also already exist before. Depending on the timing, previously, it's also possible that a racing thread releases the swap count right after releasing the ci lock and before this helper returns. In any case, the worst that could happen is we leave a clean swap cache. It will still be reclaimed when under pressure just fine. So, in conclusion, we can simplify and make the check much simpler and lockless. Also, rename it to folio_maybe_swapped to reflect the design. Signed-off-by: Kairui Song Acked-by: Chris Li --- mm/swap.h | 5 ++-- mm/swapfile.c | 82 ++++++++++++++++++++++++++++++++-----------------------= ---- 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index cc410b94e91a..9728e6a944b2 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -195,12 +195,13 @@ extern int swap_retry_table_alloc(swp_entry_t entry, = gfp_t gfp); * * folio_alloc_swap(): the entry point for a folio to be swapped * out. It allocates swap slots and pins the slots with swap cache. - * The slots start with a swap count of zero. + * The slots start with a swap count of zero. The slots are pinned + * by swap cache reference which doesn't contribute to swap count. * * folio_dup_swap(): increases the swap count of a folio, usually * during it gets unmapped and a swap entry is installed to replace * it (e.g., swap entry in page table). A swap slot with swap - * count =3D=3D 0 should only be increasd by this helper. + * count =3D=3D 0 can only be increased by this helper. * * folio_put_swap(): does the opposite thing of folio_dup_swap(). */ diff --git a/mm/swapfile.c b/mm/swapfile.c index df2b88c6c67b..dab5e726855b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1743,7 +1743,11 @@ int folio_alloc_swap(struct folio *folio) * @subpage: if not NULL, only increase the swap count of this subpage. * * Typically called when the folio is unmapped and have its swap entry to - * take its palce. + * take its place: Swap entries allocated to a folio has count =3D=3D 0 an= d pinned + * by swap cache. The swap cache pin doesn't increase the swap count. This + * helper sets the initial count =3D=3D 1 and increases the count as the f= olio is + * unmapped and swap entries referencing the slots are generated to replace + * the folio. * * Context: Caller must ensure the folio is locked and in the swap cache. * NOTE: The caller also has to ensure there is no raced call to @@ -1942,49 +1946,44 @@ int swp_swapcount(swp_entry_t entry) return count < 0 ? 0 : count; } =20 -static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, - swp_entry_t entry, int order) +/* + * folio_maybe_swapped - Test if a folio covers any swap slot with count >= 0. + * + * Check if a folio is swapped. Holding the folio lock ensures the folio w= on't + * go from not-swapped to swapped because the initial swap count increment= can + * only be done by folio_dup_swap, which also locks the folio. But a concu= rrent + * decrease of swap count is possible through swap_put_entries_direct, so = this + * may return a false positive. + * + * Context: Caller must ensure the folio is locked and in the swap cache. + */ +static bool folio_maybe_swapped(struct folio *folio) { + swp_entry_t entry =3D folio->swap; struct swap_cluster_info *ci; - unsigned int nr_pages =3D 1 << order; - unsigned long roffset =3D swp_offset(entry); - unsigned long offset =3D round_down(roffset, nr_pages); - unsigned int ci_off; - int i; + unsigned int ci_off, ci_end; bool ret =3D false; =20 - ci =3D swap_cluster_lock(si, offset); - if (nr_pages =3D=3D 1) { - ci_off =3D roffset % SWAPFILE_CLUSTER; - if (swp_tb_get_count(__swap_table_get(ci, ci_off))) - ret =3D true; - goto unlock_out; - } - for (i =3D 0; i < nr_pages; i++) { - ci_off =3D (offset + i) % SWAPFILE_CLUSTER; - if (swp_tb_get_count(__swap_table_get(ci, ci_off))) { - ret =3D true; - break; - } - } -unlock_out: - swap_cluster_unlock(ci); - return ret; -} - -static bool folio_swapped(struct folio *folio) -{ - swp_entry_t entry =3D folio->swap; - struct swap_info_struct *si; - VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_ONCE_FOLIO(!folio_test_swapcache(folio), folio); =20 - si =3D __swap_entry_to_info(entry); - if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!folio_test_large(folio))) - return swap_entry_swapped(si, entry); + ci =3D __swap_entry_to_cluster(entry); + ci_off =3D swp_cluster_offset(entry); + ci_end =3D ci_off + folio_nr_pages(folio); + /* + * Extra locking not needed, folio lock ensures its swap entries + * won't be released, the backing data won't be gone either. + */ + rcu_read_lock(); + do { + if (__swp_tb_get_count(__swap_table_get(ci, ci_off))) { + ret =3D true; + break; + } + } while (++ci_off < ci_end); + rcu_read_unlock(); =20 - return swap_page_trans_huge_swapped(si, entry, folio_order(folio)); + return ret; } =20 static bool folio_swapcache_freeable(struct folio *folio) @@ -2030,7 +2029,7 @@ bool folio_free_swap(struct folio *folio) { if (!folio_swapcache_freeable(folio)) return false; - if (folio_swapped(folio)) + if (folio_maybe_swapped(folio)) return false; =20 swap_cache_del_folio(folio); @@ -3719,6 +3718,8 @@ void si_swapinfo(struct sysinfo *val) * * Context: Caller must ensure there is no race condition on the reference * owner. e.g., locking the PTL of a PTE containing the entry being increa= sed. + * Also the swap entry must have a count >=3D 1. Otherwise folio_dup_swap = should + * be used. */ int swap_dup_entry_direct(swp_entry_t entry) { @@ -3730,6 +3731,13 @@ int swap_dup_entry_direct(swp_entry_t entry) return -EINVAL; } =20 + /* + * The caller must be increasing the swap count from a direct + * reference of the swap slot (e.g. a swap entry in page table). + * So the swap count must be >=3D 1. + */ + VM_WARN_ON_ONCE(!swap_entry_swapped(si, entry)); + return swap_dup_entries_cluster(si, swp_offset(entry), 1); } =20 --=20 2.52.0