From nobody Tue Dec 16 13:09:48 2025 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 85F9D34D91E for ; Thu, 4 Dec 2025 19:30:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764876627; cv=none; b=lwhXWT/jesb8iSbIYelCFiVy3YClUwjZYgk3K5ZQSeFXsSL+haK9tf3i8t9L592tG9L2qEV/OQDWlUDUNNhsIirnSLrz7N2ZY2fEgmPQFv5GuMB828y1hx6R5Rb7fJpIxAh174rXoCgigWaDxi1w9tbHi4+jf4zrtCmYsd6Qfus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764876627; c=relaxed/simple; bh=FxduN62BIxwiKsv0ce0msdiZXipiWdd3pAEz9c8aVYQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ah1waVauY1hmHIQxLz2X9ykIsYdEeEVymrPmqNVfa5WvKcCM6FegskDIKX45CFJAidToPTwSIPtZ3+WxfDF5Gexj0TA0BJHpX8AmQj4tlJNHV8EMJKayzF/3YKZemAr7ykyu75H9zT0wRZm2dhpQ/6oQuS0ONvgZr2LFichhVsY= 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=jUSgI7ru; arc=none smtp.client-ip=209.85.214.176 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="jUSgI7ru" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-297dc3e299bso12692775ad.1 for ; Thu, 04 Dec 2025 11:30:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764876625; x=1765481425; 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=k6CQ8ZhfqyyFU9/2UA1NETtU6jN0ODo0V1rMBQeu5q0=; b=jUSgI7ru/5AcQddalYbZcwoBNPkzu+SRJLInyWZeChaC/6f7NUmEdm9fZLR5l0PH/G 0StxsCeebT6aH5YXUthiOSh5iipUeqrn3ydTGnPKE0NEtylrNFMdspk1iWTLv2pTtslo 1tgr4AMQd7C/IXrTX5al7qWt6fEPHYfMd4c7w3Qm0t4J+clrp9LNX4v4dysL//vPgSzA cD51ug3HpG95bn3ma5zZtBMksEHHOAvS1lvGgVlCkGeWGEwOD4+vKASjByZFhTVrqUud mwvmCv/LSufSuP/XrIUz/Cwy3f7dhau4TxRW4BpDeHmk7/fnCjX2eO9m+rsWZY8RxrYI fuUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764876625; x=1765481425; 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=k6CQ8ZhfqyyFU9/2UA1NETtU6jN0ODo0V1rMBQeu5q0=; b=cIeorcyRvxSY2BDj2AACtmZfCw/qI5pN4Iui/NTkfMafkEqEwwGw+wjIIDDE1dt+aZ h/1ivPSnW5HdRRS184wMVW86Za9HY2lNAtB1Th9di+n90NCK0fEJK5AVSyAb/AyU07lC MEBLrI0xSV3uI6UetBgN5TB13pF91iA76y0/dwsMWadHFa5+9Tpsncfh5iNv0oiawBem 2oIF3IRcnV/aK0Rrww1Cee0tkqkQuIzS0Q2e5KSLP5t4udmxAu61yfWPJwgO1e1/pXr7 odSu5mjhlCVtcckVvBo0WryZe9f1W6h6RgoW0/zD1hHqIJyc0E3CYxZ7hrjKKWSTr05x Q5ig== X-Forwarded-Encrypted: i=1; AJvYcCWInNYS2E1CDVuoiuKMyK9VzlwCen2fFuceE127rDrnssk/4W67lvuqy3NcyijJw2Y7nLPjqdh00kVZn1Q=@vger.kernel.org X-Gm-Message-State: AOJu0YwfbxUA70nO7K/tH2jwUaXqNH/7gxwIsFjlDA5TaJZxi0bT7XT9 SugLjQBGJdhWJkEN9O1LB62HX/aaPL6r/9WB28QbINk52nyNvWMpApRN X-Gm-Gg: ASbGnctwegop1wJLROmyg29dIMDUlKvTLhg2PaGiSTGpwAJs4s4kkR1n2CArHSFl/lL MJnRis0PdbbuBcE50IMAf3Co1/Z5o+WpjxP+e5di7Naw0cgARMfWi/U5YWigk5KBIEmTAJH1JrT koLxwj0h5SPI9LnM4ONdufW8ezgDzU0XbLHK5/RqHC2Lz88wD2RBu7pXpZ/dLx78Qt5XuVVykSi b7ZreqRl/fGFzXeMR+53l30KkDFAYvdhdmybOKbs8+p0MT82hDyCgkl+WhPXgCMB5SqS5Fu7Czg 0FuUBcDH2hlBgrhDmpthiaZzgeP7c4pQGkEeOdDOVS/xH6YmplkqBZ78eyIk5I2aDX0o4uzMLja q+oM4hEDzg+26XQGbAvEfUpqE3vfxVmB1TXIeY8XmeWWnqjrDBR1Etg05g7WgUT1RY4hc9JYHaP lzhXwjmIgaa4//5/UNUlCuAYiLdDQOsh3XtFUWF0FRf3H+9p5+FXHAF4Nej28= X-Google-Smtp-Source: AGHT+IFHyywcPX1/2rRywR3A3G3qtQXd16j/VV0NMDNxSNumz/8b1jacGP1q4rkQk6BYnCOs2mhl6Q== X-Received: by 2002:a17:903:298b:b0:298:58ae:f91a with SMTP id d9443c01a7336-29da1eea914mr49921075ad.57.1764876624820; Thu, 04 Dec 2025 11:30:24 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-bf686b3b5a9sm2552926a12.9.2025.12.04.11.30.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Dec 2025 11:30:24 -0800 (PST) From: Kairui Song Date: Fri, 05 Dec 2025 03:29:18 +0800 Subject: [PATCH v4 10/19] mm, swap: consolidate cluster reclaim and usability 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: <20251205-swap-table-p2-v4-10-cb7e28a26a40@tencent.com> References: <20251205-swap-table-p2-v4-0-cb7e28a26a40@tencent.com> In-Reply-To: <20251205-swap-table-p2-v4-0-cb7e28a26a40@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=1764876574; l=4270; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=EXBJk1CmynZyFl/MBOO52+UVDAeGzVYnf+qyN2ZRgnU=; b=Zd7rpSLnPPYVIUN/r4Lc3fHgl3rBvcSEdXFO75CbxeJaKwwNOpHKjGCvFPVxKi7nFw4/XqKRd 61GAcg1cDkjBmr3CJMoif3qk9vXF4k4zAkoLoB0Jn0B18JpyUIPmrXc X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song Swap cluster cache reclaim requires releasing the lock, so the cluster may become unusable after the reclaim. To prepare for checking swap cache using the swap table directly, consolidate the swap cluster reclaim and the check logic. We will want to avoid touching the cluster's data completely with the swap table, to avoid RCU overhead here. And by moving the cluster usable check into the reclaim helper, it will also help avoid a redundant scan of the slots if the cluster is no longer usable, and we will want to avoid touching the cluster. Also, adjust it very slightly while at it: always scan the whole region during reclaim, don't skip slots covered by a reclaimed folio. Because the reclaim is lockless, it's possible that new cache lands at any time. And for allocation, we want all caches to be reclaimed to avoid fragmentation. Besides, if the scan offset is not aligned with the size of the reclaimed folio, we might skip some existing cache and fail the reclaim unexpectedly. There should be no observable behavior change. It might slightly improve the fragmentation issue or performance. Signed-off-by: Kairui Song --- mm/swapfile.c | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5a766d4fcaa5..2703dfafc632 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -777,33 +777,51 @@ static int swap_cluster_setup_bad_slot(struct swap_cl= uster_info *cluster_info, return 0; } =20 +/* + * Reclaim drops the ci lock, so the cluster may become unusable (freed or + * stolen by a lower order). @usable will be set to false if that happens. + */ static bool cluster_reclaim_range(struct swap_info_struct *si, struct swap_cluster_info *ci, - unsigned long start, unsigned long end) + unsigned long start, unsigned int order, + bool *usable) { + unsigned int nr_pages =3D 1 << order; + unsigned long offset =3D start, end =3D start + nr_pages; unsigned char *map =3D si->swap_map; - unsigned long offset =3D start; int nr_reclaim; =20 spin_unlock(&ci->lock); do { switch (READ_ONCE(map[offset])) { case 0: - offset++; break; case SWAP_HAS_CACHE: nr_reclaim =3D __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); - if (nr_reclaim > 0) - offset +=3D nr_reclaim; - else + if (nr_reclaim < 0) goto out; break; default: goto out; } - } while (offset < end); + } while (++offset < end); out: spin_lock(&ci->lock); + + /* + * We just dropped ci->lock so cluster could be used by another + * order or got freed, check if it's still usable or empty. + */ + if (!cluster_is_usable(ci, order)) { + *usable =3D false; + return false; + } + *usable =3D true; + + /* Fast path, no need to scan if the whole cluster is empty */ + if (cluster_is_empty(ci)) + return true; + /* * Recheck the range no matter reclaim succeeded or not, the slot * could have been be freed while we are not holding the lock. @@ -900,9 +918,10 @@ static unsigned int alloc_swap_scan_cluster(struct swa= p_info_struct *si, unsigned long start =3D ALIGN_DOWN(offset, SWAPFILE_CLUSTER); unsigned long end =3D min(start + SWAPFILE_CLUSTER, si->max); unsigned int nr_pages =3D 1 << order; - bool need_reclaim, ret; + bool need_reclaim, ret, usable; =20 lockdep_assert_held(&ci->lock); + VM_WARN_ON(!cluster_is_usable(ci, order)); =20 if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) goto out; @@ -912,14 +931,8 @@ static unsigned int alloc_swap_scan_cluster(struct swa= p_info_struct *si, if (!cluster_scan_range(si, ci, offset, nr_pages, &need_reclaim)) continue; if (need_reclaim) { - ret =3D cluster_reclaim_range(si, ci, offset, offset + nr_pages); - /* - * Reclaim drops ci->lock and cluster could be used - * by another order. Not checking flag as off-list - * cluster has no flag set, and change of list - * won't cause fragmentation. - */ - if (!cluster_is_usable(ci, order)) + ret =3D cluster_reclaim_range(si, ci, offset, order, &usable); + if (!usable) goto out; if (cluster_is_empty(ci)) offset =3D start; --=20 2.52.0