From nobody Mon Feb 9 16:45:19 2026 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 BDD5134BA20 for ; Wed, 28 Jan 2026 09:31:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592674; cv=none; b=e2VdXlJyy0f2jTR492RU3hla/5nnlmAbon32njWfYVuisJRNuJtc5aTlIQftcJva6KVin3VawakJLih//XkCJs1inAIj1SLJS+N+LVdq2a2T2wOWACYINJsIWjv8ODqceW5eTfdVw2nh4feGpX0C8nx/uQTGprG9MvKUrVH0Ip0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592674; c=relaxed/simple; bh=V1casluMYNM9duIDGgLVQmk4P9Wz4Ub3EjlBggQQcHI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZfZCz8VE7eFyDCAyFG+A7gD9GPUf571nI1Hgik2QHT0V+osmbGTXy7Y1v8GahC501xeKsEvKUuljBOVWz+GK0CMixjWOQuTFb/Ny+4h1uTQmHgp4Pzr2Dk03/Y7Fuc79LZ49WfXtsy5irgMUiZ3bBCARjFqE40avmH3Em1cMOaw= 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=TQxRkn7Y; arc=none smtp.client-ip=209.85.216.46 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="TQxRkn7Y" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-34c3259da34so3920801a91.2 for ; Wed, 28 Jan 2026 01:31:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769592672; x=1770197472; 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=IO3x3MxqGRi8WhnUHEHqBelhkKQlEmUKW/aSZAplwzs=; b=TQxRkn7YsTpbvWLwp0VX7SFqaBQdYV692bWvCbODvI6TpnIQWp4BS0H9Fvc3VMt4VF xjTgvp5o4z/BvIf4/nvl6+eXSPQM29cRD/WyK5f3O586eu7PRKKPolR5JO1XMFZLWZ0s T+j0IjrbyDFWShTaYTo+iArAl3pF46jEKOpIMMQipMT1AH/Y2MJNoCbIUeUqmi2WYsA5 bAZIAtiM7AQZpbbN807iq1y0Ob8v9dx1LPQ19mgl0cSzg6oCjxyfcJi7CAGlzYsMJ03C 7TwDaDTUDVM1HW2XlwzuD77951d/ch+njYGXPx9/bYZFCCSf3G8bGhK87uglqF9LZw7g 1cqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769592672; x=1770197472; 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=IO3x3MxqGRi8WhnUHEHqBelhkKQlEmUKW/aSZAplwzs=; b=oIiz9khNJXEj873s4U0XXFXN4cCWBD6zLLMLTUPDlf2d7XNMjOiu1KE8GxGkNENYPH 2TnyBOZzGTHetwnSgAPe5pshd/bgbL2tfecARmA1lgDvfrGCfMPM5wX/72LbtIsK/HB+ tfBP6vgolYc/cAvcCzLG4wdYrckLMcyno2paq0X9zCX7mdZ2zpRoFMfCgVcmmujMLbcM 2XBCjy6HErvClukjT9agOb7RJWQy+CJxihhUdmu580mgw4XWztfY5vBsGwy5Tcd04x+x C2ybzTvtvr5AWqA+9fFvk4/I2zHy5bYmIyCQ9G+jR9rlH0XEs+nz6OuPk3AAVNBhWYox FMmg== X-Forwarded-Encrypted: i=1; AJvYcCXy7gPauPxA4aKs9XaHsoGy8CJ+gZsWdDBqkIpKG2ZE8rpMfJtiia0uWNBIC34hqCZSAfcWwnaxxABAi/w=@vger.kernel.org X-Gm-Message-State: AOJu0YxjMHfQfgr5hS50w0wywFX7Md6L8L/NFOrtbmfDRER2kKEHN+Kr 3xRERMyeus30W71IhRCg+aRpKVxSP5D5j6DC2dZd/u2xTbVme9D0DkRj X-Gm-Gg: AZuq6aIY3oB+C28ys/CrHEJGggXZ0VFWt27/Bi+5Ho/jrfFjK6RUh3mmNw2O4VI2r9l SCxaYbCmdEZ/8KrPxu02yupiKPpYll8rM1yMferJz40dkkUj1eu91K1af1O3SQcw9H11PtqmjvR +zGIdXthsffGPQ8KfWZLHW7/urH4tKHk5Gj4SeDSnVV8wmkSUBZrNnoFtNWNaLy/Oy1P1LW1nMR OY01fr5Sp/U+FhUb+reERc2RiqtmtX87aVOEDTojY013NMlNfQiVIUS+tf4p3WTe4Iu3y2w8ZGb s159bmVN3T3PNhQccBffNMO7po0QevQqd8qTej5KHIV3PRxt/olt+estF6Fmu20PELRc6hpoHR/ pRXBx8oXm5hgym9CNkZqEydDC/RlWupNGfMubOINgfV5Q0PzFiJBH0e4lxSR/A09NW0vNFUiGl7 QUH2XQCWBjryIyNt5VuK7omkiUPnkPbiIAPUwmMlWenZ85KMcjLceS/ipg4nIIYW0Uo6OP X-Received: by 2002:a17:90b:3ccf:b0:353:356c:6821 with SMTP id 98e67ed59e1d1-353fecd0906mr4046998a91.8.1769592672086; Wed, 28 Jan 2026 01:31:12 -0800 (PST) Received: from [127.0.0.1] ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3540f3eca6dsm1872235a91.15.2026.01.28.01.31.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 01:31:11 -0800 (PST) From: Kairui Song Date: Wed, 28 Jan 2026 17:28:35 +0800 Subject: [PATCH v2 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: <20260128-swap-table-p3-v2-11-fe0b67ef0215@tencent.com> References: <20260128-swap-table-p3-v2-0-fe0b67ef0215@tencent.com> In-Reply-To: <20260128-swap-table-p3-v2-0-fe0b67ef0215@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=1769592628; l=7000; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=JNIm9AZzdz6ohutIc0C0GEVsDBQSvtZ0ar1ajiaQqZM=; b=cXY2N0M4lgSWLTYK6dAshXYinNIZR/38U4M7MwXKjOqluHtDrbBrKH/NSmhj3jkwpLJCLsOkX rodLFRaLMbgDBVVA1L4Er5DEpZVqhzmWpkF+JV9PrCNB1OLiKTdJw7m X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= 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 --- 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 9fc5fecdcfdf..3ee761ee8348 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 a7fc8837eb74..f5474ddbba36 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 @@ -1944,49 +1948,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) @@ -2032,7 +2031,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); @@ -3710,6 +3709,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) { @@ -3721,6 +3722,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