From nobody Mon Feb 9 15:30:04 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 523D534BA42 for ; Wed, 28 Jan 2026 09:31:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592662; cv=none; b=XN2swYDEXACA5Fh2IJmiy0Bg+6Mes7TZFKrW0szSUxWLa0nCeKiq3NDeAfwgQmbb7bKTwe27hZM8sItMbFAVWtLW2YYT1VFaSn2UY3yKSavCb5pztd6oDYToWjgvowUzTeRUgWyIVoyTBwa2qucBjqX577Y3JO4Pyc2FQNx0T0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592662; c=relaxed/simple; bh=duthAvrprtmW88C0Xisiob8i1iw8kK3zMTNLkXt6lqI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tC+qRooBhRYn3AA6lmFToWH053Hfx9nNDJS8Gaj2kd98yvSFpS7uKSSaTylhKhVYYLitk7mg8lMVvce1XUSfyoLVxE6l6iLMk2vpN1EqdvWrInktBu2KreoxkZ6tw6Wn8sH4mOx76xBAL02PItA9KDmslKPEXDuV7XN0+iqfOAE= 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=Xjz9Kmcw; arc=none smtp.client-ip=209.85.214.175 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="Xjz9Kmcw" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2a09d981507so4471125ad.1 for ; Wed, 28 Jan 2026 01:31:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769592661; x=1770197461; 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=bBjV8XyXDLoEReDqKUKA22e4GI0fZeu23FHRrREbJdM=; b=Xjz9KmcwYFeUgqjYtVt5TYjnR4SdnZVELYnoWEl5A0BqLDagrumoptA1rj9FN41U28 aGWhKS788YVM6cZdrPF7MQ9QAfLTSTM6/GktXnZP6iYFAlKGF2K2nhImgGSOUQt7kW8+ 6sgDo2eyNuxxMJ8kHpmoOBH3oak3tmoV7cTNh89eArC/oIB4r4wq+vMor6/oxwyN9zsA sxjyXMFybbLLMx8jT99Ryv49AJ5V3DhDC4tenEamcYRbCchtPISQYskTcHkiuDtQiMcG 9Xy+e9QmfjeerKS9R8Pohgirco4PwZyLaPiNZ6hB1jqMWiTk282bcwwuTiw8M6Y3BiVG rL+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769592661; x=1770197461; 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=bBjV8XyXDLoEReDqKUKA22e4GI0fZeu23FHRrREbJdM=; b=bSzEs4d4zdsAf46HwoBbR15VM6uVTs5fs2eGVzhWDzYybrE+qI8bVYIDnFCat664X5 eL3WuEIeUV70GIhCQjp4J5VcpIITK8GSh/RvRwfJrRlHrQDkIqc1cFD3YBOXUOUf+fad ss9iayoH/J/PYJ/l7tN7rNmOe4jx/4FwGywq4oc5p8TAOGtGVtnphGA5vqKZsZF9DN5V kPblMeErEDDvEGX+ohWBdyvtDMwSLqCwBWKOuRJ+xKUQtf8uu/06D/b1MqGXC6Xk1xni BamP5V5slHOtAr6xYowcFMaY+EUzvZQlPl1a/HOgpc9oedH+8zhHUx8pwEp392Ks0kGl w5mQ== X-Forwarded-Encrypted: i=1; AJvYcCVfDo0qKkS7O01WesktOBlRJVziESapz4/te5HuQoK2gtxYBta+WyCTtaFqdJHlH6+Cb0ZbKFL1eXntL+Y=@vger.kernel.org X-Gm-Message-State: AOJu0YztYFstYTbimbDK4NJR3qu/Li90/XXe7f2CrSAdIuvJ5lZI9CW5 AOwjUvwbxGgUnGHhnhou/Qn0JhyRtceePRubapk2Z/QeI/DdmDowiXnxrGjDwuH11BI= X-Gm-Gg: AZuq6aJZe+ikaWNKgXXLFEVHIdGOBSUkKNs6sRY7zAcWMtbguRr294zfWb+IlJgC9zD DzoJV6UkpCHZDp9a9KlFw1IIH5MetIaK5yADMyvQV88wE8Y0S+X1wh+MH0aYEKXRG6T3T9q5/Q5 GNzqA5ktrypZOZOKhqulhbIU7zPj+u/9G2hnaIwY7BW7SrUKO2K1WTx/IWvjR1vhMeZ8iV758me 5afLC4nPAiH4X3x6AJgPzWgKLUBL42zTc1pZZbvQOU4i7kaCmluM9t2Vhe+1w9dmaru7hv4IU0X z2MAEhc36qFf2u+bzzneLtrC98zq1We0eHEkw9BejtWu4A6f3RkzxhM2jTYzLbL0YtHrqp06zOd HJUF1QrAiBsk0V9QfA0CRaDUY1udTj10kevWyuvt1e12icP4v6QEeNphwm5JHPmWet/d1XFf9EX 7KGAWIIvh5ie08D/WPGUCGOVo7ObsKWk8Pj4HldVzNlmCzcIBuZ4SxeHTVsQ== X-Received: by 2002:a17:902:d512:b0:2a0:9238:881d with SMTP id d9443c01a7336-2a87130a499mr48464525ad.15.1769592660621; Wed, 28 Jan 2026 01:31:00 -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.30.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 01:31:00 -0800 (PST) From: Kairui Song Date: Wed, 28 Jan 2026 17:28:32 +0800 Subject: [PATCH v2 08/12] mm, swap: simplify swap table sanity range check 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-8-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=3805; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=xEm9vmKYs9I8cGuUNSJYWeygrYtNmSS9LIdq7v6ecsc=; b=CUVi91qTEx9seoLd4nVc/dyAm11X+quXmPpT9xgVXUnFcUQM1MsVopybAyeIimWtDj/4ioUTd 65YlGEfRcVhDQPepEaODn3lzB2foec2pXntqtIJ5Cqsb8HJ9ymQKWUK X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song The newly introduced helper, which checks bad slots and emptiness of a cluster, can cover the older sanity check just fine, with a more rigorous condition check. So merge them. Signed-off-by: Kairui Song --- mm/swapfile.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index bdce2abd9135..968153691fc4 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -459,9 +459,11 @@ static void swap_table_free(struct swap_table *table) * One special case is that bad slots can't be freed, so check the number = of * bad slots for swapoff, and non-swapoff path must never free bad slots. */ -static void swap_cluster_assert_empty(struct swap_cluster_info *ci, bool s= wapoff) +static void swap_cluster_assert_empty(struct swap_cluster_info *ci, + unsigned int ci_off, unsigned int nr, + bool swapoff) { - unsigned int ci_off =3D 0, ci_end =3D SWAPFILE_CLUSTER; + unsigned int ci_end =3D ci_off + nr; unsigned long swp_tb; int bad_slots =3D 0; =20 @@ -588,7 +590,7 @@ static void swap_cluster_schedule_discard(struct swap_i= nfo_struct *si, =20 static void __free_cluster(struct swap_info_struct *si, struct swap_cluste= r_info *ci) { - swap_cluster_assert_empty(ci, false); + swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, false); swap_cluster_free_table(ci); move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order =3D 0; @@ -898,26 +900,6 @@ static bool cluster_scan_range(struct swap_info_struct= *si, return true; } =20 -/* - * Currently, the swap table is not used for count tracking, just - * do a sanity check here to ensure nothing leaked, so the swap - * table should be empty upon freeing. - */ -static void swap_cluster_assert_table_empty(struct swap_cluster_info *ci, - unsigned int start, unsigned int nr) -{ - unsigned int ci_off =3D start % SWAPFILE_CLUSTER; - unsigned int ci_end =3D ci_off + nr; - unsigned long swp_tb; - - if (IS_ENABLED(CONFIG_DEBUG_VM)) { - do { - swp_tb =3D __swap_table_get(ci, ci_off); - VM_WARN_ON_ONCE(!swp_tb_is_null(swp_tb)); - } while (++ci_off < ci_end); - } -} - static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster_info *ci, struct folio *folio, @@ -943,13 +925,14 @@ static bool cluster_alloc_range(struct swap_info_stru= ct *si, if (likely(folio)) { order =3D folio_order(folio); nr_pages =3D 1 << order; + swap_cluster_assert_empty(ci, offset % SWAPFILE_CLUSTER, nr_pages, false= ); __swap_cache_add_folio(ci, folio, swp_entry(si->type, offset)); } else if (IS_ENABLED(CONFIG_HIBERNATION)) { 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); + swap_cluster_assert_empty(ci, offset % SWAPFILE_CLUSTER, 1, false); } else { /* Allocation without folio is only possible with hibernation */ WARN_ON_ONCE(1); @@ -1768,7 +1751,7 @@ void swap_entries_free(struct swap_info_struct *si, =20 mem_cgroup_uncharge_swap(entry, nr_pages); swap_range_free(si, offset, nr_pages); - swap_cluster_assert_table_empty(ci, offset, nr_pages); + swap_cluster_assert_empty(ci, offset % SWAPFILE_CLUSTER, nr_pages, false); =20 if (!ci->count) free_cluster(si, ci); @@ -2769,7 +2752,7 @@ static void free_swap_cluster_info(struct swap_cluste= r_info *cluster_info, /* Cluster with bad marks count will have a remaining table */ spin_lock(&ci->lock); if (rcu_dereference_protected(ci->table, true)) { - swap_cluster_assert_empty(ci, true); + swap_cluster_assert_empty(ci, 0, SWAPFILE_CLUSTER, true); swap_cluster_free_table(ci); } spin_unlock(&ci->lock); --=20 2.52.0