From nobody Sat Jul 25 20:47:29 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 0C118346FA1 for ; Mon, 13 Jul 2026 17:26:54 +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=1783963615; cv=none; b=uXbA6pJXtSdJUi53QvAoznFdWx/N9D5agD4uYYI2UW+oT9I0P3gUgZuOxuqskrwNUOirLyFoU6uqF1P2wMe+noXRrwQZvcWvYqR2K+Wa6w4/9XUyPnr0wOnAS1uCs26Xq5F2G3k2T8oX8lvgTi75xOkiBBHVZkqT49oUqUrgK6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=YexIlLH/xRiAaDy9nQacZ186o1VJKSRYwoHVeIPJ4Ns=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YdXI4cmZMGMKKyPEx1x+3TDE1gE7XUik++rT1zS7teHJOZf4Xwq0+4+q4VebcaC7PVkgxBxKlbAwXdOJBn+Fop9nobQfSO0AbXyfY/pL2BHcKgNxCJYccSN/yZcGPOLVpa+65gqA6178GMfIViouGbKU3JxGhftC5MYXC8WsP1U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=u3e1SH0h; 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="u3e1SH0h" Received: by smtp.kernel.org (Postfix) with ESMTPS id B6C77C2BCC7; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963614; bh=YexIlLH/xRiAaDy9nQacZ186o1VJKSRYwoHVeIPJ4Ns=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=u3e1SH0hqe/5kl0XulTnJH9WZ3RvKGZNn0WpqzWpAhJrM8RDHshEdhvV7R1JKhnIx BpxN4H51r7QjcuutergmLfB/mLmz3Kvp1dDfNridAs2kv8WZ5EOKGzIXymhgqlOhAR rB2SN3u3D3zjv8mJ/WK9aG7UnKa3jMvoEvEShstJupBCXV3dwxdC7aRY2ZLKArIh59 oMApZ5mYkaYwpNGwj8AVwDkC66CDIqO5o3iPbPhFAuC1V9Wbv9U/HaPepKAz4UMzkq DyAXBBUga1ijHz1arEFZfDPE+MTFupfEsg46R1aJLcjU8pBWFTuE43iLVXhNjyXTB6 GGEmNhI7TEhdw== 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 96743C44507; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:39 +0800 Subject: [PATCH RFC 01/13] mm/swap: remove unused parameter for reading swap header 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: <20260714-swap-pcp-priq-v1-1-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=1063; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=XGt445t9iTFErCSJfFQu9ZkczDPJlNnc2UbKTqyWoyw=; b=I6cvsoytYxfN/+g7A51gAi/oEuel7bBFBZKk7mBWokMWtK3MDyq3dZwzoZvdNMdR2CszDgzzx O7p41Lhx6OWDhpDASgLP489tK1HfPfBsf46BX1pG56SPdIa2UOxHwl/ 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 No feature change, just a minor cleanup. Signed-off-by: Kairui Song --- mm/swapfile.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 775c385490a5..27b4d11125d6 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3453,9 +3453,8 @@ __weak unsigned long arch_max_swapfile_size(void) return generic_max_swapfile_size(); } =20 -static unsigned long read_swap_header(struct swap_info_struct *si, - union swap_header *swap_header, - struct inode *inode) +static unsigned long read_swap_header(union swap_header *swap_header, + struct inode *inode) { int i; unsigned long maxpages; @@ -3679,7 +3678,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) } swap_header =3D kmap_local_folio(folio, 0); =20 - maxpages =3D read_swap_header(si, swap_header, inode); + maxpages =3D read_swap_header(swap_header, inode); if (unlikely(!maxpages)) { error =3D -EINVAL; goto bad_swap_unlock_inode; --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 0C09E346E60 for ; Mon, 13 Jul 2026 17:26:54 +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=1783963615; cv=none; b=Bp1Yp6aGd3WQCVRIxEzS4U3G0dvwKaGySJk9fROtFqn0+ojUy9bWluUhL5HyapB27j5bfX65kjS6CIyafRsUWQTa9cyoLx6pwQse6QGcwgsWCZVzwKn6jV7sEpJM8vo/dVcbVCikSHnh6X3nEfDty21NGNfj0LennBEjONMFM4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=B4zCWGJ9+aLc/O6vq6PcXzy8sOSwPeK/kwVGyL76lCs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=U+CNxPTSiei561NRXZyn846mJI8Y8LVEWO7sxCcjG5pDnEmGD7049cJKYQ54N+ka/1ZzMK+yUONHH3iQg1fy/bTrs6RzF7zCjY3YsQSFtwWDumgRjq2IEkxRFXsEH0s08eB/vUdaaoB73KNFnwYmMZcOOLWnDuRMA6xEg2giETQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kakovTCw; 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="kakovTCw" Received: by smtp.kernel.org (Postfix) with ESMTPS id C563DC2BCF7; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963614; bh=B4zCWGJ9+aLc/O6vq6PcXzy8sOSwPeK/kwVGyL76lCs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=kakovTCwIWNPxgoAGw5/gERoibOiZW5BPtUcYKiCPaouisMVusIeSc2T3/IGjsqG/ GkeAX7KyS2Ble8Qfrz86ms6HF/N6q0hzerZIieCViv4LnaldtEuXxGY/PNGGVa0Nrd AHe97alC0dE29efvOBm6fOR4Czgu6BmEBsEsl6JAJicQOE5WWOmwlH3oU0A37EGNOR z1/tFf4IYLijHlTCEGcCZ4be1G797l8DynI+RZI4rTme0qPPeB/js677jgytnRhPyh GsPVkLkK2HN+Qyxe4QyMApYp2CbzcrTp1gP/Dr/08sExvG0MTc6DhKWkuEFiR9k3DY T9SdBUqRzjbEQ== 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 A530AC44509; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:40 +0800 Subject: [PATCH RFC 02/13] mm/swap: slightly cleanup the code for hibernation error handling 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: <20260714-swap-pcp-priq-v1-2-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=2506; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=MoWWPNOWkGVpdbQrV1EThfD/0KHjuJm/eolt7XVFWQk=; b=MQ54BpDpDAfKb1RowmKnHfEt3Y6KtS8Jx/Yq7lkNkJOU5oneODQ0VBeWa7pVznUJtE6mEvHxR w4Z8sd6jzKuBSQXl9v7gxd4u93v77a38GAmvggh4/D08rxbMsllzVgp 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 Restructure the error paths in pin_hibernation_swap_type() using a goto out pattern to avoid repeated spin_unlock() calls and simplify the return flow. Also simplify unpin_hibernation_swap_type() by making the si NULL check inline, and clean up find_first_swap() to avoid an early return inside the loop. Signed-off-by: Kairui Song --- mm/swapfile.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 27b4d11125d6..30ee75bdec40 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2247,21 +2247,18 @@ static int __find_hibernation_swap_type(dev_t devic= e, sector_t offset) */ int pin_hibernation_swap_type(dev_t device, sector_t offset) { - int type; + int ret; struct swap_info_struct *si; =20 spin_lock(&swap_lock); + ret =3D __find_hibernation_swap_type(device, offset); + if (ret < 0) + goto out; =20 - type =3D __find_hibernation_swap_type(device, offset); - if (type < 0) { - spin_unlock(&swap_lock); - return type; - } - - si =3D swap_type_to_info(type); + si =3D swap_type_to_info(ret); if (WARN_ON_ONCE(!si)) { - spin_unlock(&swap_lock); - return -ENODEV; + ret =3D -ENODEV; + goto out; } =20 /* @@ -2270,14 +2267,15 @@ int pin_hibernation_swap_type(dev_t device, sector_= t offset) * the same session. */ if (WARN_ON_ONCE(si->flags & SWP_HIBERNATION)) { - spin_unlock(&swap_lock); - return -EBUSY; + ret =3D -EBUSY; + goto out; } =20 si->flags |=3D SWP_HIBERNATION; =20 +out: spin_unlock(&swap_lock); - return type; + return ret; } =20 /** @@ -2296,11 +2294,8 @@ void unpin_hibernation_swap_type(int type) =20 spin_lock(&swap_lock); si =3D swap_type_to_info(type); - if (!si) { - spin_unlock(&swap_lock); - return; - } - si->flags &=3D ~SWP_HIBERNATION; + if (si) + si->flags &=3D ~SWP_HIBERNATION; spin_unlock(&swap_lock); } =20 @@ -2335,7 +2330,7 @@ int find_hibernation_swap_type(dev_t device, sector_t= offset) =20 int find_first_swap(dev_t *device) { - int type; + int type, ret =3D -ENODEV; =20 spin_lock(&swap_lock); for (type =3D 0; type < nr_swapfiles; type++) { @@ -2344,11 +2339,11 @@ int find_first_swap(dev_t *device) if (!(sis->flags & SWP_WRITEOK)) continue; *device =3D sis->bdev->bd_dev; - spin_unlock(&swap_lock); - return type; + ret =3D type; + break; } spin_unlock(&swap_lock); - return -ENODEV; + return ret; } =20 /* --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 23401346FA7 for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=uBfBmMPmxQm1JxiaLNLxfJyHJKSkJqKYj3IIZ09MgwQ79QMZfqRZn8Ce/TzOXwTXQ3dgeATK451hplkdMYI1nRs4PeJR+pTjtezwLNZ5rWQegV7tW0gYLzgfSWukTUM8XiN9Fe2LcWi0Vrt9iEeJmgp4PTyMkZNa43Ud7nT7Qlc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=PTUIwiXZC2UgMIw6+Z902E3vukWmaqBtCnBDKNTP2uM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iSKr8V2PNnBBDzW8pixvJLmprjQOuI9QAXJwbxgmAinbFVjT0rVUMN0mJJEYbFiG4KsVOoBSaaJRQzsaMYuhKWkcY9PeMa5iYD0pTbXpzsnaEaIiGd6Zj3Qc+WWpsAhXf1P0Nw7wde1seLCbwxeyRZbxHIZ6121z1IUbllr726A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eF/xggNo; 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="eF/xggNo" Received: by smtp.kernel.org (Postfix) with ESMTPS id CFEDDC2BCFA; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963614; bh=PTUIwiXZC2UgMIw6+Z902E3vukWmaqBtCnBDKNTP2uM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=eF/xggNoKGV851h6xMXRWFTZ2RxaMeVjvra2QZDhlR4sqB1HIDN86IJeryCTX5CSK XUSsavUU8qN8QZ+ggkdmHpf71WQTxOpjiiTnGdHz44W5mBW/3+WU+Elz1/TFZwmRRw /hykEsk9lFOyVAvxNubg/YWOAJ8GndIjr3JrprxcER19eeFzsV+Ci8VdOzUKoIq9tQ 1aLfzk4rOlVqBr/46xGe8BQrgnxBZx6H37os8/5U1MXNCa6lKZIgR28b/oulHQFxar emZZAhGebLO8+12vSh2y+NGyw+EGpUmvHxTqy3F4MXlJmncqHhK5UfraNQEJxuQjFf W++NDtXmOG+OA== 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 B5CFCC44508; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:41 +0800 Subject: [PATCH RFC 03/13] mm/swap: cleanup and document swap device availability flag usage 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: <20260714-swap-pcp-priq-v1-3-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=12401; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ZELEcS43EUtrQZh/ZUjmqEURIG5xrZ5nw0j/vyLi2T0=; b=zV6MMRuZ0Hv3afqz6vL3J1WG1Y1E+dTwNo0mf/LJDx3BlpNvFTaECmW7AEXMUhkJ3GlVP5Y1c epTJ6zz3e+fAsUyjphZZzeeEu1Kj0sbIy3X1js03PcYKZczLpP857LR 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 Rework swap device flag and metedata handling and swapon/swapoff to be cleaner and better documented, in preparation for locking cleanup. Consolidate existing routines and introduce swap_device_enable and swap_device_disable as the clean boundary of exposing or isolating a swap device. swap_device_enable sets the proper flags and exposes the device as an allocation candidate, while swap_device_disable clears related flags and ensures no more allocations will happen. And add comment blocks documenting the lifetime and locking rules for swap device flags and their locking conventions. No feature change, only code rearrangement and documentation. Signed-off-by: Kairui Song --- include/linux/swap.h | 27 +++++--- mm/swapfile.c | 189 +++++++++++++++++++++++------------------------= ---- 2 files changed, 104 insertions(+), 112 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 696ed01709c2..a8ce05024cfe 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -194,6 +194,22 @@ struct swap_extent { ((offsetof(union swap_header, magic.magic) - \ offsetof(union swap_header, info.badpages)) / sizeof(int)) =20 +/* + * Swap device flags, except the ones documented below, all are immutable + * after exposed by swap_device_enable, and until the device is freed again + * (SWP_USED unset). The exceptions: + * - SWP_USED: Protected by swap_lock. Indicates the device is inuse. Once + * set, won't be cleared unless all reference to this device is freed and + * swapoff finished. + * - SWP_WRITEOK: Protected by both swap_lock and swap_avail_lock, clearing + * this flag also waits for all current cluster lock users to exit so + * checking this flag while holding any of these locks ensures the device + * is safe to use at the moment. Note: clearing this flag doesn't affect + * pending IO or async requests, it only prevents further entry allocati= on + * or new async request (e.g. discard) from initiating. + * - SWP_HIBERNATION: Protected by swap_lock. Indicates if the device + * is pinned for hibernation. + */ enum { SWP_USED =3D (1 << 0), /* is slot in swap_info[] used? */ SWP_WRITEOK =3D (1 << 1), /* ok to write to this swap? */ @@ -263,14 +279,9 @@ struct swap_info_struct { struct file *swap_file; /* seldom referenced */ struct completion comp; /* seldom referenced */ spinlock_t lock; /* - * protect map scan related fields like - * inuse_pages and all cluster lists. - * Other fields are only changed - * at swapon/swapoff, so are protected - * by swap_lock. changing flags need - * hold this lock and swap_lock. If - * both locks need hold, hold swap_lock - * first. + * Protect cluster lists. Other fields + * are only changed at swapon/swapoff, + * so are protected by swap_lock. */ struct work_struct discard_work; /* discard worker */ struct work_struct reclaim_work; /* reclaim worker */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 30ee75bdec40..a4701692d330 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1193,28 +1193,20 @@ static void del_from_avail_list(struct swap_info_st= ruct *si, bool swapoff) =20 spin_lock(&swap_avail_lock); =20 - if (swapoff) { - /* - * Forcefully remove it. Clear the SWP_WRITEOK flags for - * swapoff here so it's synchronized by both si->lock and - * swap_avail_lock, to ensure the result can be seen by - * add_to_avail_list. - */ - lockdep_assert_held(&si->lock); - si->flags &=3D ~SWP_WRITEOK; - atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages); - } else { - /* - * If not called by swapoff, take it off-list only if it's - * full and SWAP_USAGE_OFFLIST_BIT is not set (strictly - * si->inuse_pages =3D=3D pages), any concurrent slot freeing, - * or device already removed from plist by someone else - * will make this return false. - */ + /* + * Force remove it only for swapoff. Else, take it off-list only if + * it's full and SWAP_USAGE_OFFLIST_BIT is not set (strictly + * si->inuse_pages =3D=3D pages), so concurrent slot freeing, or + * concurrent list removal will make the cmpxchg fail and skip + * the removal. + */ + if (!swapoff) { pages =3D si->pages; if (!atomic_long_try_cmpxchg(&si->inuse_pages, &pages, pages | SWAP_USAGE_OFFLIST_BIT)) goto skip; + } else { + atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages); } =20 plist_del(&si->avail_list, &swap_avail_head); @@ -1224,21 +1216,21 @@ static void del_from_avail_list(struct swap_info_st= ruct *si, bool swapoff) } =20 /* SWAP_USAGE_OFFLIST_BIT can only be cleared by this helper. */ -static void add_to_avail_list(struct swap_info_struct *si, bool swapon) +static void add_to_avail_list(struct swap_info_struct *si) { long val; unsigned long pages; =20 spin_lock(&swap_avail_lock); =20 - /* Corresponding to SWP_WRITEOK clearing in del_from_avail_list */ - if (swapon) { - lockdep_assert_held(&si->lock); - si->flags |=3D SWP_WRITEOK; - } else { - if (!(READ_ONCE(si->flags) & SWP_WRITEOK)) - goto skip; - } + /* + * Add the device to the avail list if SWP_WRITEOK is set and + * SWAP_USAGE_OFFLIST_BIT is still set. Swapoff clears + * SWP_WRITEOK first, so the device won't be re-added after + * swapoff starts unless swap_device_enable resurrects it. + */ + if (!(si->flags & SWP_WRITEOK)) + goto skip; =20 if (!(atomic_long_read(&si->inuse_pages) & SWAP_USAGE_OFFLIST_BIT)) goto skip; @@ -1294,7 +1286,7 @@ static void swap_usage_sub(struct swap_info_struct *s= i, unsigned int nr_entries) * add it to the plist. */ if (unlikely(val & SWAP_USAGE_OFFLIST_BIT)) - add_to_avail_list(si, false); + add_to_avail_list(si); } =20 static void swap_range_alloc(struct swap_info_struct *si, @@ -1348,7 +1340,7 @@ static bool get_swap_device_info(struct swap_info_str= uct *si) * up to dated. * * Paired with the spin_unlock() after setup_swap_info() in - * enable_swap_info(), and smp_wmb() in swapoff. + * swap_device_enable(), and smp_wmb() in swapoff. */ smp_rmb(); return true; @@ -2963,58 +2955,71 @@ static int setup_swap_extents(struct swap_info_stru= ct *sis, return generic_swapfile_activate(sis, swap_file, span); } =20 -static void _enable_swap_info(struct swap_info_struct *si) -{ - atomic_long_add(si->pages, &nr_swap_pages); - total_swap_pages +=3D si->pages; - - assert_spin_locked(&swap_lock); - - plist_add(&si->list, &swap_active_head); - - /* Add back to available list */ - add_to_avail_list(si, true); -} - /* - * Called after the swap device is ready, resurrect its percpu ref, it's n= ow - * safe to reference it. Add it to the list to expose it to the allocator. + * Called after the swap device is ready to be used. Marking it writable a= nd + * exposing it to the allocator. Resurrect its percpu ref if it was dead b= efore. */ -static void enable_swap_info(struct swap_info_struct *si) +static void swap_device_enable(struct swap_info_struct *si) { - percpu_ref_resurrect(&si->users); spin_lock(&swap_lock); - spin_lock(&si->lock); - _enable_swap_info(si); - spin_unlock(&si->lock); - spin_unlock(&swap_lock); -} =20 -static void reinsert_swap_info(struct swap_info_struct *si) -{ - spin_lock(&swap_lock); - spin_lock(&si->lock); - _enable_swap_info(si); - spin_unlock(&si->lock); + spin_lock(&swap_avail_lock); + si->flags |=3D SWP_WRITEOK; + spin_unlock(&swap_avail_lock); + + atomic_long_add(si->pages, &nr_swap_pages); + total_swap_pages +=3D si->pages; + plist_add(&si->list, &swap_active_head); spin_unlock(&swap_lock); + + add_to_avail_list(si); } =20 -/* - * Called after clearing SWP_WRITEOK, ensures cluster_alloc_range - * see the updated flags, so there will be no more allocations. - */ -static void wait_for_allocation(struct swap_info_struct *si) +static int swap_device_disable(struct swap_info_struct *si) { unsigned long offset; unsigned long end =3D ALIGN(si->max, SWAPFILE_CLUSTER); struct swap_cluster_info *ci; =20 - BUG_ON(si->flags & SWP_WRITEOK); + /* + * If SWP_WRITEOK is not set: another process already disabling it. + * If SWP_HIBERNATION is set: the device is pinned for hibernation. + */ + spin_lock(&swap_lock); + if (!(si->flags & SWP_WRITEOK) || + si->flags & SWP_HIBERNATION) { + spin_unlock(&swap_lock); + return -EBUSY; + } =20 + if (security_vm_enough_memory_mm(current->mm, si->pages)) { + spin_unlock(&swap_lock); + return -ENOMEM; + } + vm_unacct_memory(si->pages); + + spin_lock(&swap_avail_lock); + si->flags &=3D ~SWP_WRITEOK; + spin_unlock(&swap_avail_lock); + + plist_del(&si->list, &swap_active_head); + total_swap_pages -=3D si->pages; + atomic_long_sub(si->pages, &nr_swap_pages); + spin_unlock(&swap_lock); + + del_from_avail_list(si, true); + + /* + * Swap allocator doesn't touch si lock, so looping through all + * ci locks ensures __swap_cluster_alloc_entries sees the + * updated flags, and no more allocations will occur. + */ for (offset =3D 0; offset < end; offset +=3D SWAPFILE_CLUSTER) { ci =3D swap_cluster_lock(si, offset); swap_cluster_unlock(ci); } + + return 0; } =20 static void free_swap_cluster_info(struct swap_cluster_info *cluster_info, @@ -3059,7 +3064,6 @@ static void flush_percpu_swap_cluster(struct swap_inf= o_struct *si) } } =20 - SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) { struct swap_info_struct *p =3D NULL; @@ -3083,42 +3087,21 @@ SYSCALL_DEFINE1(swapoff, const char __user *, speci= alfile) mapping =3D victim->f_mapping; spin_lock(&swap_lock); plist_for_each_entry(p, &swap_active_head, list) { - if (p->flags & SWP_WRITEOK) { - if (p->swap_file->f_mapping =3D=3D mapping) { - found =3D 1; - break; - } + if (p->flags & SWP_WRITEOK && + p->swap_file->f_mapping =3D=3D mapping) { + found =3D 1; + break; } } - if (!found) { - err =3D -EINVAL; - spin_unlock(&swap_lock); - goto out_dput; - } - - /* Refuse swapoff while the device is pinned for hibernation */ - if (p->flags & SWP_HIBERNATION) { - err =3D -EBUSY; - spin_unlock(&swap_lock); - goto out_dput; - } - - if (!security_vm_enough_memory_mm(current->mm, p->pages)) - vm_unacct_memory(p->pages); - else { - err =3D -ENOMEM; - spin_unlock(&swap_lock); - goto out_dput; - } - spin_lock(&p->lock); - del_from_avail_list(p, true); - plist_del(&p->list, &swap_active_head); - atomic_long_sub(p->pages, &nr_swap_pages); - total_swap_pages -=3D p->pages; - spin_unlock(&p->lock); spin_unlock(&swap_lock); + filp_close(victim, NULL); + + if (!found) + return -EINVAL; =20 - wait_for_allocation(p); + err =3D swap_device_disable(p); + if (err) + return err; =20 set_current_oom_origin(); err =3D try_to_unuse(p->type); @@ -3126,8 +3109,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) =20 if (err) { /* re-insert swap space back into swap_list */ - reinsert_swap_info(p); - goto out_dput; + swap_device_enable(p); + return err; } =20 /* @@ -3187,13 +3170,10 @@ SYSCALL_DEFINE1(swapoff, const char __user *, speci= alfile) p->flags =3D 0; spin_unlock(&swap_lock); =20 - err =3D 0; atomic_inc(&proc_poll_event); wake_up_interruptible(&proc_poll_wait); =20 -out_dput: - filp_close(victim, NULL); - return err; + return 0; } =20 #ifdef CONFIG_PROC_FS @@ -3615,7 +3595,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) /* * Allocate or reuse existing !SWP_USED swap_info. The returned * si will stay in a dying status, so nothing will access its content - * until enable_swap_info resurrects its percpu ref and expose it. + * until swap_device_enable resurrects its percpu ref and expose it. */ si =3D alloc_swap_info(); if (IS_ERR(si)) @@ -3773,7 +3753,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) si->swap_file =3D swap_file; =20 /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */ - enable_swap_info(si); + percpu_ref_resurrect(&si->users); + swap_device_enable(si); =20 pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s= %s%s\n", K(si->pages), name->name, si->prio, nr_extents, --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 2623834750D for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=uREFilkh1C0192gZQjec2aVl3msmFoYW5VvyRtJq/cEtOMLVR7strujw2Q4v44CYGnqbWgNYN7FIu2GJIPz69vrH4f2OPUMlqtLGl3S/fE711vo75NBVb+5M34xpRvdmkD4VPrnKeKf3LWmyyLQQU4ytGV/YsOoEXFL0RWebGHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=Alfa5R7cuPoITFX+rbarbwLG1+I6hT50bFmqlnb/xXI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aZdIlphs64aEux1hLwzBjj4//u+ZNXX8HuB90RhQZ5PggDWIrKSUtJxkFPpQiXuCyRww0CehEk7PjGEJ7SxCb1XYseZZHEwwwzYn48jAZ5Q4a7V9QcymO0RaWi+kE6YhOcAW5YB77ADACkfodGTCuRxSBP1CAfuXwCPcnKBkXr8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=owDRiqNh; 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="owDRiqNh" Received: by smtp.kernel.org (Postfix) with ESMTPS id D63BFC2BCFC; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963614; bh=Alfa5R7cuPoITFX+rbarbwLG1+I6hT50bFmqlnb/xXI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=owDRiqNhkPISIr/+9c3MPxYnCxFfajBExfW0QFiJLtSuVw/7lCfhn5bLJ5Q4nfiXM 16ev8ogax2mMrmepZQQm5LmAEsyM4lt0wxr58v5dmsA50BRiLF2mM4C7eGkPf5uMGQ hgs8gYzGGaYRwHDfb5c+6dVpEcr6tG5DosMhwy2ko8LkE207qJiaZhbYNVodyQ7Rvr 5MRDMJNs1UNhtla9pTwBshAwsITTdujxoB1VLZ4JsFyrNe59IwntF196FXqiXlKLHd YG4ke/NC/ekRz7nHvQKBCEnQq0bs9ykfQB9wkUow46v16huSeuBNFjBmbEOg09Amrl ZRXufPtERmqGA== 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 C33FEC4450A; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:42 +0800 Subject: [PATCH RFC 04/13] mm/swap: introduce swap device iteration helper 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: <20260714-swap-pcp-priq-v1-4-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=5272; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ypbKQldXEjwRE9cbv1ZROZRw4f0vEb/XoGPlY7YD2wA=; b=Uvd2+nXpHDCFssiQ4RQhSvh6jVc3zRB9V+1OBBAkp00UiXNZVUy6wcAZNAxW1K8zUWd2HjZH/ O2m747SWr55BHSHZYq6lR93oeK05RAcG4eOeEF5viKvmRqRce6bESNv 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 There are many users that access the swap info array just to iterate through the swap devices to find usable ones. Introduce a generic helper for this to prepare for dropping the lock convention. Also slightly adjust __swap_type_to_info to allow access of swapped off devices by moving the sanity check into its only current caller, and this way makes more sense too: the only caller is __swap_entry_to_info, where we should never see an entry referring to a dead device. Signed-off-by: Kairui Song --- mm/swap.h | 12 +++++----- mm/swapfile.c | 77 ++++++++++++++++++++++++++++++++++++-------------------= ---- 2 files changed, 53 insertions(+), 36 deletions(-) diff --git a/mm/swap.h b/mm/swap.h index b51ad3071a73..08a782f3d5ac 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -91,16 +91,16 @@ static inline unsigned int swp_cluster_offset(swp_entry= _t entry) */ static inline struct swap_info_struct *__swap_type_to_info(int type) { - struct swap_info_struct *si; - - si =3D READ_ONCE(swap_info[type]); /* rcu_dereference() */ - VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ - return si; + return READ_ONCE(swap_info[type]); /* rcu_dereference() */ } =20 static inline struct swap_info_struct *__swap_entry_to_info(swp_entry_t en= try) { - return __swap_type_to_info(swp_type(entry)); + struct swap_info_struct *si; + + si =3D __swap_type_to_info(swp_type(entry)); /* rcu_dereference() */ + VM_WARN_ON_ONCE(percpu_ref_is_zero(&si->users)); /* race with swapoff */ + return si; } =20 static inline struct swap_cluster_info *__swap_offset_to_cluster( diff --git a/mm/swapfile.c b/mm/swapfile.c index a4701692d330..8faa4e7c32bf 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -106,6 +106,34 @@ static DEFINE_SPINLOCK(swap_avail_lock); =20 struct swap_info_struct *swap_info[MAX_SWAPFILES]; =20 +static inline struct swap_info_struct *__swap_iter(int *i, unsigned long f= lag) +{ + lockdep_assert_held(&swap_lock); + while (*i < nr_swapfiles) { + struct swap_info_struct *si =3D __swap_type_to_info(*i); + + VM_WARN_ON(!si); + (*i)++; + if (flag && !((si->flags & flag) =3D=3D flag)) + continue; + return si; + } + return NULL; +} + +#define __for_each_swap(si, flag) \ + for (int __i =3D 0; ((si) =3D __swap_iter(&__i, flag));) + +/* + * for_each_swap - iterate through all allocated and inuse swap devices + * @si: the iterator + * + * Context: The caller must hold swap_lock. The lock may be dropped during + * the loop body but must be re-acquired before the next iteration. + */ +#define for_each_swap(si) __for_each_swap(si, SWP_USED) +#define for_each_avail_swap(si) __for_each_swap(si, SWP_USED | SWP_WRITEOK) + static struct kmem_cache *swap_table_cachep; =20 /* Protects si->swap_file for /proc/swaps usage */ @@ -2197,26 +2225,18 @@ void swap_free_hibernation_slot(swp_entry_t entry) =20 static int __find_hibernation_swap_type(dev_t device, sector_t offset) { - int type; - - lockdep_assert_held(&swap_lock); + struct swap_info_struct *si; =20 if (!device) return -EINVAL; =20 - for (type =3D 0; type < nr_swapfiles; type++) { - struct swap_info_struct *sis =3D swap_info[type]; - - if (!(sis->flags & SWP_WRITEOK)) - continue; - - if (device =3D=3D sis->bdev->bd_dev) { - struct swap_extent *se =3D first_se(sis); - - if (se->start_block =3D=3D offset) - return type; + for_each_avail_swap(si) { + if (device =3D=3D si->bdev->bd_dev) { + if (first_se(si)->start_block =3D=3D offset) + return si->type; } } + return -ENODEV; } =20 @@ -2322,16 +2342,13 @@ int find_hibernation_swap_type(dev_t device, sector= _t offset) =20 int find_first_swap(dev_t *device) { - int type, ret =3D -ENODEV; + int ret =3D -ENODEV; + struct swap_info_struct *si; =20 spin_lock(&swap_lock); - for (type =3D 0; type < nr_swapfiles; type++) { - struct swap_info_struct *sis =3D swap_info[type]; - - if (!(sis->flags & SWP_WRITEOK)) - continue; - *device =3D sis->bdev->bd_dev; - ret =3D type; + for_each_avail_swap(si) { + *device =3D si->bdev->bd_dev; + ret =3D si->type; break; } spin_unlock(&swap_lock); @@ -2817,12 +2834,14 @@ static int try_to_unuse(unsigned int type) */ static void drain_mmlist(void) { + struct swap_info_struct *si; struct list_head *p, *next; - unsigned int type; =20 - for (type =3D 0; type < nr_swapfiles; type++) - if (swap_usage_in_pages(swap_info[type])) + for_each_swap(si) { + if (swap_usage_in_pages(si)) return; + } + spin_lock(&mmlist_lock); list_for_each_safe(p, next, &init_mm.mmlist) list_del_init(p); @@ -3802,14 +3821,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specia= lfile, int, swap_flags) =20 void si_swapinfo(struct sysinfo *val) { - unsigned int type; + struct swap_info_struct *si; unsigned long nr_to_be_unused =3D 0; =20 spin_lock(&swap_lock); - for (type =3D 0; type < nr_swapfiles; type++) { - struct swap_info_struct *si =3D swap_info[type]; - - if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) + for_each_swap(si) { + if (!(si->flags & SWP_WRITEOK)) nr_to_be_unused +=3D swap_usage_in_pages(si); } val->freeswap =3D atomic_long_read(&nr_swap_pages) + nr_to_be_unused; --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 337CD34751E for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=RAZePAb1ByhRu3B9rZbQ8my6FobahlU2yEWqved8GjK0ke5V0ZY+EDWpsRguw4UGJAqg/qbdetf+hNe8IkZoKRVUtEuLJ++UBeW2DOaJzguhAgTqUfOJ52C8pBvTcIhS6bjx2aqQsaaz/SJ0YDjUEbLfR80Rc0ciLKLT0e7G750= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=krFo8t/q3g+P3S7coY6W3tqdeS0OL4CwQGAAMQGMemI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=c8sWYS4CtR795XQudlic5MvXzM0MQehHGYTpLHokrwTfEfPUz8HTwC8vFVfVQ74UdStse47C2b/475E0enUBxpLXjt2L+BR1pObdmRgvkuvV530dlswhPiHcVnLjrH5aZ0uDV1w0VWTJTnx+hFS5crO5MamxwC1LZgUtsMA23k0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FXllBRGo; 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="FXllBRGo" Received: by smtp.kernel.org (Postfix) with ESMTPS id E7963C4AF09; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=krFo8t/q3g+P3S7coY6W3tqdeS0OL4CwQGAAMQGMemI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=FXllBRGo1GMeQj0k2AwoHzEi+JDS6EMmGgbVqdyaVEMkc13sCFCAMzMm3ipWIFWKC cU1HPPJhLbmHYwD77y+ARSv3Xgj7OheJ+GSGnso1DlwAA6FqubI2jRm0NsNcOzXJut fJxemVLM4xRAXBjiSjiIy90Zb9rA+vyjOCYj0kLaSo4IcIjbY9ta+hgyBhtcJGPvpz v9DYdy6YB+AVJFjMh6oqNFylVnhvmNq+poV8gXbdWFFlpoOuZRe9QlRP0iNGXOI3NA wT2VWv0oRkbuQ7MM8xVDw4TdwHPOmFeXTW8l3/7rYcHmJ1zVPODgI9LZQgEBAhJAas DNSnbw+z49gvw== 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 D43CEC44501; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:43 +0800 Subject: [PATCH RFC 05/13] mm/swap: change the swapon lock into a percpu rwsem 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: <20260714-swap-pcp-priq-v1-5-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=15672; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=jUpqZi3PB5w2EdScPkNb0dLNkS8Pe3V4vt1vzNmkxZU=; b=Cm2pvMDQdoy4Pjql3yJbCYUyfEoznI0IpuepcQpZIh7jc33AL5WOKWYZW3WxQJWHsMgTDiq8p oebV/YKKgAyDMi0WhU+y/EoDIznaZLgVT9DPqmICxpeWwgUjnaAxejX 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 Reading swap metadata is a common hot path, while swapon and swapoff are costly and very rare. Convert the existing swap_lock spinlock into a percpu rwsem so that readers can run concurrently without contention. This is a first step toward cleaning up the swap lock model and we might already see a performance gain for existing readers. Also update the comments in swap_info_struct to reflect the lock name change. Signed-off-by: Kairui Song --- include/linux/swap.h | 10 ++--- mm/swapfile.c | 121 +++++++++++++++++++++++++++--------------------= ---- 2 files changed, 69 insertions(+), 62 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index a8ce05024cfe..d0c4e0ab5806 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -197,17 +197,17 @@ struct swap_extent { /* * Swap device flags, except the ones documented below, all are immutable * after exposed by swap_device_enable, and until the device is freed again - * (SWP_USED unset). The exceptions: - * - SWP_USED: Protected by swap_lock. Indicates the device is inuse. Once + * (SWP_USED unset). The exceptions are all protected by swapon_rwsem: + * - SWP_USED: Protected by swapon_rwsem. Indicates the device is inuse. O= nce * set, won't be cleared unless all reference to this device is freed and * swapoff finished. - * - SWP_WRITEOK: Protected by both swap_lock and swap_avail_lock, clearing + * - SWP_WRITEOK: Protected by both swapon_rwsem and swap_avail_lock, clea= ring * this flag also waits for all current cluster lock users to exit so * checking this flag while holding any of these locks ensures the device * is safe to use at the moment. Note: clearing this flag doesn't affect * pending IO or async requests, it only prevents further entry allocati= on * or new async request (e.g. discard) from initiating. - * - SWP_HIBERNATION: Protected by swap_lock. Indicates if the device + * - SWP_HIBERNATION: Protected by swapon_rwsem. Indicates if the device * is pinned for hibernation. */ enum { @@ -281,7 +281,7 @@ struct swap_info_struct { spinlock_t lock; /* * Protect cluster lists. Other fields * are only changed at swapon/swapoff, - * so are protected by swap_lock. + * so are protected by swapon_rwsem. */ struct work_struct discard_work; /* discard worker */ struct work_struct reclaim_work; /* reclaim worker */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 8faa4e7c32bf..d75fad161ba5 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -57,23 +57,29 @@ static void move_cluster(struct swap_info_struct *si, enum swap_cluster_flags new_flags); =20 /* - * Protects the swap_info array, and the SWP_USED flag. swap_info contains - * lazily allocated & freed swap device info struts, and SWP_USED indicates - * which device is used, ~SWP_USED devices and can be reused. - * - * Also protects swap_active_head total_swap_pages, and the SWP_WRITEOK fl= ag. + * Serializes swapon/swapoff (writers) and protects the swap_info + * array, nr_swapfiles, total_swap_pages, and part of swap device + * info content (see comment of swap_info_struct). Readers + * (allocation, /proc/swaps, etc.) take percpu_down_read() which + * is cheap as the hot path. + */ +DEFINE_STATIC_PERCPU_RWSEM(swapon_rwsem); +/* + * swap_info contains lazily allocated swap device info structs, and + * SWP_USED indicates which device is used, ~SWP_USED devices can be + * reused. Protected by swapon_rwsem, but reading could be lockless. */ -static DEFINE_SPINLOCK(swap_lock); +struct swap_info_struct *swap_info[MAX_SWAPFILES]; static unsigned int nr_swapfiles; -atomic_long_t nr_swap_pages; +long total_swap_pages; + /* * Some modules use swappable objects and may try to swap them out under * memory pressure (via the shrinker). Before doing so, they may wish to * check to see if any swap space is available. */ +atomic_long_t nr_swap_pages; EXPORT_SYMBOL_GPL(nr_swap_pages); -/* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ -long total_swap_pages; #define DEF_SWAP_PRIO -1 unsigned long swapfile_maximum_size; #ifdef CONFIG_MIGRATION @@ -85,7 +91,7 @@ static const char Bad_offset[] =3D "Bad swap offset entry= "; =20 /* * all active swap_info_structs - * protected with swap_lock, and ordered by priority. + * protected with swapon_rwsem, and ordered by priority. */ static PLIST_HEAD(swap_active_head); =20 @@ -95,20 +101,18 @@ static PLIST_HEAD(swap_active_head); * This is used by folio_alloc_swap() instead of swap_active_head * because swap_active_head includes all swap_info_structs, * but folio_alloc_swap() doesn't need to look at full ones. - * This uses its own lock instead of swap_lock because when a + * This uses its own lock instead of swapon_rwsem because when a * swap_info_struct changes between not-full/full, it needs to * add/remove itself to/from this list, but the swap_info_struct->lock - * is held and the locking order requires swap_lock to be taken + * is held and the locking order requires swapon_rwsem to be taken * before any swap_info_struct->lock. */ static PLIST_HEAD(swap_avail_head); static DEFINE_SPINLOCK(swap_avail_lock); =20 -struct swap_info_struct *swap_info[MAX_SWAPFILES]; - static inline struct swap_info_struct *__swap_iter(int *i, unsigned long f= lag) { - lockdep_assert_held(&swap_lock); + lockdep_assert_held(&swapon_rwsem); while (*i < nr_swapfiles) { struct swap_info_struct *si =3D __swap_type_to_info(*i); =20 @@ -128,7 +132,7 @@ static inline struct swap_info_struct *__swap_iter(int = *i, unsigned long flag) * for_each_swap - iterate through all allocated and inuse swap devices * @si: the iterator * - * Context: The caller must hold swap_lock. The lock may be dropped during + * Context: The caller must hold swapon_rwsem. The lock may be dropped dur= ing * the loop body but must be re-acquired before the next iteration. */ #define for_each_swap(si) __for_each_swap(si, SWP_USED) @@ -1365,10 +1369,10 @@ static bool get_swap_device_info(struct swap_info_s= truct *si) /* * Guarantee the si->users are checked before accessing other * fields of swap_info_struct, and si->flags (SWP_WRITEOK) is - * up to dated. + * up to date. * - * Paired with the spin_unlock() after setup_swap_info() in - * swap_device_enable(), and smp_wmb() in swapoff. + * Paired with percpu_up_write() in swap_device_enable(), and + * smp_wmb() after clearing SWP_WRITEOK in swapoff. */ smp_rmb(); return true; @@ -1453,10 +1457,10 @@ static bool swap_sync_discard(void) bool ret =3D false; struct swap_info_struct *si, *next; =20 - spin_lock(&swap_lock); + percpu_down_read(&swapon_rwsem); start_over: plist_for_each_entry_safe(si, next, &swap_active_head, list) { - spin_unlock(&swap_lock); + percpu_up_read(&swapon_rwsem); if (get_swap_device_info(si)) { if (si->flags & SWP_PAGE_DISCARD) ret =3D swap_do_scheduled_discard(si); @@ -1465,11 +1469,11 @@ static bool swap_sync_discard(void) if (ret) return true; =20 - spin_lock(&swap_lock); + percpu_down_read(&swapon_rwsem); if (plist_node_empty(&next->list)) goto start_over; } - spin_unlock(&swap_lock); + percpu_up_read(&swapon_rwsem); =20 return false; } @@ -2262,7 +2266,7 @@ int pin_hibernation_swap_type(dev_t device, sector_t = offset) int ret; struct swap_info_struct *si; =20 - spin_lock(&swap_lock); + percpu_down_write(&swapon_rwsem); ret =3D __find_hibernation_swap_type(device, offset); if (ret < 0) goto out; @@ -2286,7 +2290,7 @@ int pin_hibernation_swap_type(dev_t device, sector_t = offset) si->flags |=3D SWP_HIBERNATION; =20 out: - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); return ret; } =20 @@ -2304,11 +2308,11 @@ void unpin_hibernation_swap_type(int type) { struct swap_info_struct *si; =20 - spin_lock(&swap_lock); + percpu_down_write(&swapon_rwsem); si =3D swap_type_to_info(type); if (si) si->flags &=3D ~SWP_HIBERNATION; - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); } =20 /** @@ -2333,9 +2337,9 @@ int find_hibernation_swap_type(dev_t device, sector_t= offset) { int type; =20 - spin_lock(&swap_lock); + percpu_down_read(&swapon_rwsem); type =3D __find_hibernation_swap_type(device, offset); - spin_unlock(&swap_lock); + percpu_up_read(&swapon_rwsem); =20 return type; } @@ -2345,13 +2349,13 @@ int find_first_swap(dev_t *device) int ret =3D -ENODEV; struct swap_info_struct *si; =20 - spin_lock(&swap_lock); + percpu_down_read(&swapon_rwsem); for_each_avail_swap(si) { *device =3D si->bdev->bd_dev; ret =3D si->type; break; } - spin_unlock(&swap_lock); + percpu_up_read(&swapon_rwsem); return ret; } =20 @@ -2380,7 +2384,7 @@ unsigned int count_swap_pages(int type, int free) { unsigned int n =3D 0; =20 - spin_lock(&swap_lock); + percpu_down_read(&swapon_rwsem); if ((unsigned int)type < nr_swapfiles) { struct swap_info_struct *sis =3D swap_info[type]; =20 @@ -2392,7 +2396,7 @@ unsigned int count_swap_pages(int type, int free) } spin_unlock(&sis->lock); } - spin_unlock(&swap_lock); + percpu_up_read(&swapon_rwsem); return n; } #endif /* CONFIG_HIBERNATION */ @@ -2704,10 +2708,10 @@ static unsigned int find_next_to_unuse(struct swap_= info_struct *si, unsigned long swp_tb; =20 /* - * No need for swap_lock here: we're just looking + * No need for swapon_rwsem here: we're just looking * for whether an entry is in use, not modifying it; false * hits are okay, and sys_swapoff() has already prevented new - * allocations from this area (while holding swap_lock). + * allocations from this area (while holding swapon_rwsem). */ for (i =3D prev + 1; i < si->max; i++) { swp_tb =3D swap_table_get(__swap_offset_to_cluster(si, i), @@ -2828,8 +2832,8 @@ static int try_to_unuse(unsigned int type) =20 /* * After a successful try_to_unuse, if no swap is now in use, we know - * we can empty the mmlist. swap_lock must be held on entry and exit. - * Note that mmlist_lock nests inside swap_lock, and an mm must be + * we can empty the mmlist. swapon_rwsem must be held on entry and exit. + * Note that mmlist_lock nests inside swapon_rwsem, and an mm must be * added to the mmlist just after page_duplicate - before would be racy. */ static void drain_mmlist(void) @@ -2980,8 +2984,7 @@ static int setup_swap_extents(struct swap_info_struct= *sis, */ static void swap_device_enable(struct swap_info_struct *si) { - spin_lock(&swap_lock); - + percpu_down_write(&swapon_rwsem); spin_lock(&swap_avail_lock); si->flags |=3D SWP_WRITEOK; spin_unlock(&swap_avail_lock); @@ -2989,7 +2992,7 @@ static void swap_device_enable(struct swap_info_struc= t *si) atomic_long_add(si->pages, &nr_swap_pages); total_swap_pages +=3D si->pages; plist_add(&si->list, &swap_active_head); - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); =20 add_to_avail_list(si); } @@ -3004,15 +3007,15 @@ static int swap_device_disable(struct swap_info_str= uct *si) * If SWP_WRITEOK is not set: another process already disabling it. * If SWP_HIBERNATION is set: the device is pinned for hibernation. */ - spin_lock(&swap_lock); + percpu_down_write(&swapon_rwsem); if (!(si->flags & SWP_WRITEOK) || si->flags & SWP_HIBERNATION) { - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); return -EBUSY; } =20 if (security_vm_enough_memory_mm(current->mm, si->pages)) { - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); return -ENOMEM; } vm_unacct_memory(si->pages); @@ -3024,7 +3027,7 @@ static int swap_device_disable(struct swap_info_struc= t *si) plist_del(&si->list, &swap_active_head); total_swap_pages -=3D si->pages; atomic_long_sub(si->pages, &nr_swap_pages); - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); =20 del_from_avail_list(si, true); =20 @@ -3104,7 +3107,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) return PTR_ERR(victim); =20 mapping =3D victim->f_mapping; - spin_lock(&swap_lock); + percpu_down_read(&swapon_rwsem); plist_for_each_entry(p, &swap_active_head, list) { if (p->flags & SWP_WRITEOK && p->swap_file->f_mapping =3D=3D mapping) { @@ -3112,7 +3115,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) break; } } - spin_unlock(&swap_lock); + percpu_up_read(&swapon_rwsem); filp_close(victim, NULL); =20 if (!found) @@ -3154,7 +3157,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) atomic_dec(&nr_rotate_swap); =20 mutex_lock(&swapon_mutex); - spin_lock(&swap_lock); + percpu_down_write(&swapon_rwsem); spin_lock(&p->lock); drain_mmlist(); =20 @@ -3165,7 +3168,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) p->max =3D 0; p->cluster_info =3D NULL; spin_unlock(&p->lock); - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); arch_swap_invalidate_area(p->type); zswap_swapoff(p->type); mutex_unlock(&swapon_mutex); @@ -3184,10 +3187,14 @@ SYSCALL_DEFINE1(swapoff, const char __user *, speci= alfile) * Clear the SWP_USED flag after all resources are freed so that swapon * can reuse this swap_info in alloc_swap_info() safely. It is ok to * not hold p->lock after we cleared its SWP_WRITEOK. + * + * The write lock ensures the flag clear is visible to lockless + * readers of swap_type_to_info() before alloc_swap_info() reuses + * this slot. */ - spin_lock(&swap_lock); + percpu_down_write(&swapon_rwsem); p->flags =3D 0; - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); =20 atomic_inc(&proc_poll_event); wake_up_interruptible(&proc_poll_wait); @@ -3347,13 +3354,13 @@ static struct swap_info_struct *alloc_swap_info(voi= d) return ERR_PTR(-ENOMEM); } =20 - spin_lock(&swap_lock); + percpu_down_write(&swapon_rwsem); for (type =3D 0; type < nr_swapfiles; type++) { if (!(swap_info[type]->flags & SWP_USED)) break; } if (type >=3D MAX_SWAPFILES) { - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); percpu_ref_exit(&p->users); kvfree(p); return ERR_PTR(-EPERM); @@ -3378,7 +3385,7 @@ static struct swap_info_struct *alloc_swap_info(void) plist_node_init(&p->list, 0); plist_node_init(&p->avail_list, 0); p->flags =3D SWP_USED; - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); if (defer) { percpu_ref_exit(&defer->users); kvfree(defer); @@ -3804,9 +3811,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) * Clear the SWP_USED flag after all resources are freed so * alloc_swap_info can reuse this si safely. */ - spin_lock(&swap_lock); + percpu_down_write(&swapon_rwsem); si->flags =3D 0; - spin_unlock(&swap_lock); + percpu_up_write(&swapon_rwsem); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); if (swap_file) @@ -3824,14 +3831,14 @@ void si_swapinfo(struct sysinfo *val) struct swap_info_struct *si; unsigned long nr_to_be_unused =3D 0; =20 - spin_lock(&swap_lock); + percpu_down_read(&swapon_rwsem); for_each_swap(si) { if (!(si->flags & SWP_WRITEOK)) nr_to_be_unused +=3D swap_usage_in_pages(si); } val->freeswap =3D atomic_long_read(&nr_swap_pages) + nr_to_be_unused; val->totalswap =3D total_swap_pages + nr_to_be_unused; - spin_unlock(&swap_lock); + percpu_up_read(&swapon_rwsem); } =20 /* --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 34092347532 for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=VgzXsWO1F/wZWqt7fqu+N5kJANuCsIDy/hhcgqo/x5kBThNnX02mrL6HXegFetuB8dydxJo8OVtR+Inv0R5aW30pEe8cEr6z7zuwqIAFu1HEshBLnBWjLBByxwYZM6kQtWFi/PD8IQjCeJJXIwKPyfysdUBNblonZHzE8Hn/0lc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=aRYM02wEbr/o6bgrCnlzBlc0T+QJvdAK2GhDQkdgStM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=A9SdjSqz0mUHq8A28OtdwaS+H0US4Lv11/Tx0Hsjd4PgRsggLkE8/OtnsiwGnm9xOVlEQlCJIeDz4BOURgeXc6p8T+pTFvQmVs+oxNIwd0TIUlwTgE440OtURscGymr4Xlb6ChcgKo2I2FBCyaXJK3Q1zWUzAEvuw7VjeLC3qDg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RO0uB8Ak; 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="RO0uB8Ak" Received: by smtp.kernel.org (Postfix) with ESMTPS id 00E63C2BD05; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=aRYM02wEbr/o6bgrCnlzBlc0T+QJvdAK2GhDQkdgStM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=RO0uB8AkoELV2kiI6BadjvnOQXoW2C+tf/R+j6wi3OSQR4O1QZGbqlYSlooPGFZRY ZDVDAGQ5/M0v20oHj1B8eoBwj3+FS2g95ep05fMlwi9bFT5+VWa/2/R3FMJus5Y3Jy +pEV63YPcxiD8ViklpWckWAM0bH/TYBDqrNXO9xpv9T+yQ22XrJlI9G4MqEDyVg1iu tdfXzMwSd8zKkhF4MjIqJmjNv7IbWqdwNjQb70PG8ZQDnyMHcqjeYv54sP/3+HsEdq sJXUgPFFy/m4sIbRtBqvNwHhrrEHDhA0oOmGFcUduYfeqdJiZ7VAh/Fd3AkmBZE20d vJ1l1rnyrST8w== 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 E24F0C44509; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:44 +0800 Subject: [PATCH RFC 06/13] mm/swap: remove swapon mutex and update proc reader 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: <20260714-swap-pcp-priq-v1-6-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=3465; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=j1za3Qxt32KGithzma37GogOBsyq+U5KkuqTTp+siv0=; b=wBkRluP9NMxSP0zuqKH8cLiWSuMS6fkcqfGHdt2jZ/pxZPZ6RGda/q03nFgRLUVaj+2Z9sKKn QtVt3wfBTDqDBlDPBmqPdBWzZy0yFqPYxzgd2KLccMc/PEZpPkWAGkI 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 Now that swapon_rwsem protects all swapon/swapoff-sensitive data, the old swapon_mutex is redundant. Remove it and convert the /proc/swaps seq_file reader to use the rwsem instead. Also convert the swap_start iterator to use for_each_swap() for consistency. Also adapt swap_next, it needs an explicit SWP_USED check. swap_file is not reliably protected by swapon_rwsem, so checking SWP_USED is more accurate and stable for filtering in-use devices. Signed-off-by: Kairui Song --- mm/swapfile.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index d75fad161ba5..8b073569d1d9 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -140,9 +140,6 @@ static inline struct swap_info_struct *__swap_iter(int = *i, unsigned long flag) =20 static struct kmem_cache *swap_table_cachep; =20 -/* Protects si->swap_file for /proc/swaps usage */ -static DEFINE_MUTEX(swapon_mutex); - static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); /* Activity counter to indicate that a swapon or swapoff has occurred */ static atomic_t proc_poll_event =3D ATOMIC_INIT(0); @@ -3156,7 +3153,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) if (!(p->flags & SWP_SOLIDSTATE)) atomic_dec(&nr_rotate_swap); =20 - mutex_lock(&swapon_mutex); percpu_down_write(&swapon_rwsem); spin_lock(&p->lock); drain_mmlist(); @@ -3171,7 +3167,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) percpu_up_write(&swapon_rwsem); arch_swap_invalidate_area(p->type); zswap_swapoff(p->type); - mutex_unlock(&swapon_mutex); kfree(p->global_cluster); p->global_cluster =3D NULL; free_swap_cluster_info(cluster_info, maxpages); @@ -3217,20 +3212,18 @@ static __poll_t swaps_poll(struct file *file, poll_= table *wait) return EPOLLIN | EPOLLRDNORM; } =20 -/* iterator */ static void *swap_start(struct seq_file *swap, loff_t *pos) { struct swap_info_struct *si; - int type; loff_t l =3D *pos; =20 - mutex_lock(&swapon_mutex); + percpu_down_read(&swapon_rwsem); =20 if (!l) return SEQ_START_TOKEN; =20 - for (type =3D 0; (si =3D swap_type_to_info(type)); type++) { - if (!(si->swap_file)) + for_each_swap(si) { + if (!si->swap_file) continue; if (!--l) return si; @@ -3251,7 +3244,7 @@ static void *swap_next(struct seq_file *swap, void *v= , loff_t *pos) =20 ++(*pos); for (; (si =3D swap_type_to_info(type)); type++) { - if (!(si->swap_file)) + if (!(si->flags & SWP_USED) || !(si->swap_file)) continue; return si; } @@ -3261,7 +3254,7 @@ static void *swap_next(struct seq_file *swap, void *v= , loff_t *pos) =20 static void swap_stop(struct seq_file *swap, void *v) { - mutex_unlock(&swapon_mutex); + percpu_up_read(&swapon_rwsem); } =20 static int swap_show(struct seq_file *swap, void *v) @@ -3764,7 +3757,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) goto free_swap_zswap; } =20 - mutex_lock(&swapon_mutex); prio =3D DEF_SWAP_PRIO; if (swap_flags & SWAP_FLAG_PREFER) prio =3D swap_flags & SWAP_FLAG_PRIO_MASK; @@ -3790,7 +3782,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) (si->flags & SWP_AREA_DISCARD) ? "s" : "", (si->flags & SWP_PAGE_DISCARD) ? "c" : ""); =20 - mutex_unlock(&swapon_mutex); atomic_inc(&proc_poll_event); wake_up_interruptible(&proc_poll_wait); =20 --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 3386234752A for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=jhIxbNxUkKt/Qb1/Zeex+Mj2Js0JZwxWUJ70ARAEYqQpAAwMa7rmOP1z3tf5YoVrWZXfIKPsGDxbidy4Hw6RmbRhqphYgE6yyNbaCZGh6kIs0WtkIEGC6UXI8eFz2zcMcVbvb1YW63khTkSvwebOnmvTaJjhndHuiJTrLvqqfQ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=WzxC+eElxIID/vTACljQfff57E8zSB0pagmJWpqAdKY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UBBQw0fgfWiD8JT1Rq8e9D6tmm3uhBLrIFTKtYsiNfMhY2CM7ImhXeHLx27luOglKjtfQzsNsWvg0x+oB5fd26M49mFXqvZRnXSW6Qp25eZZMrTIeo/unqCo17/jTmKXsZezQ8Q+BILCzrzp6hGFtWaORxol4LWaCdybzboe/Es= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IrqC8bJZ; 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="IrqC8bJZ" Received: by smtp.kernel.org (Postfix) with ESMTPS id 102C0C2BCFD; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=WzxC+eElxIID/vTACljQfff57E8zSB0pagmJWpqAdKY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=IrqC8bJZyhiBZZHXB43YKzLB9RVOMlim/hvAlCr4JSSvDvHi0YOozXzLLKGIgJmhD kbRMlUqN9AYCOnQtL1/xjKZqzIXdHoJEUqHZ7U5oD5RPV3FdVO8qZ9HSHLtCr6uPDT ue2wCF6NclGRKM4NOYT6NELyUbUfgxpr4aj9+RmWFc6okav99HCjPO2QoGoS6pRiCB o9f0ex6y0evYhapvHoDl3NmJrz7H/9o2ZZpaY8wiMv6laa5N1LTCwqludVOFXyrK5+ vYxMonSu67pFawa9A5KbWb5Ao8liQMK7uCFTSmdtintDKKhKLRcDfaf9RCyfSgljI2 0LyOr4RqmkvDg== 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 F2A28C44507; Mon, 13 Jul 2026 17:26:54 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:45 +0800 Subject: [PATCH RFC 07/13] mm/swap: consolidate swap inuse accounting helpers 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: <20260714-swap-pcp-priq-v1-7-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=4815; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=d4HZUEwBV+hR00Jn7TMM7WH5lEZpnnAHEskQhntWz50=; b=Xgg1N8VIGPOVqFWmXlu8FGYB244KXlcsSsKDCRT4hPv9QwaQC5+4gvFSSHymiO2do27GrNivF B8+6XJcy+jBAbzYC9aYU4aQAJpIFSv5rz3CzSCwUJCnSpUd3z+iyGw2 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 Inline the swap_usage_add/sub helpers into their only callers and rename the callers to better reflect what they actually do: track per-device inuse page counts. No functional change. Signed-off-by: Kairui Song --- mm/swapfile.c | 64 +++++++++++++++++++++++++------------------------------= ---- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 8b073569d1d9..df9855401a2e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -49,8 +49,8 @@ #include "internal.h" #include "swap.h" =20 -static void swap_range_alloc(struct swap_info_struct *si, - unsigned int nr_entries); +static void swap_device_inuse_add(struct swap_info_struct *si, + unsigned int nr_entries); static bool folio_swapcache_freeable(struct folio *folio); static void move_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci, struct list_head *list, @@ -984,7 +984,7 @@ static bool __swap_cluster_alloc_entries(struct swap_in= fo_struct *si, if (cluster_is_empty(ci)) ci->order =3D order; ci->count +=3D nr_pages; - swap_range_alloc(si, nr_pages); + swap_device_inuse_add(si, nr_pages); =20 return true; } @@ -1286,53 +1286,36 @@ static void add_to_avail_list(struct swap_info_stru= ct *si) } =20 /* - * swap_usage_add / swap_usage_sub of each slot are serialized by ci->lock - * within each cluster, so the total contribution to the global counter sh= ould - * always be positive and cannot exceed the total number of usable slots. + * swap_device_inuse_add / swap_device_inuse_sub are called within cluster + * update critical sections and serialized by ci->lock within each cluster, + * so the total contribution to the global counter should always be positi= ve + * and cannot exceed the total number of usable slots. */ -static bool swap_usage_add(struct swap_info_struct *si, unsigned int nr_en= tries) +static void swap_device_inuse_add(struct swap_info_struct *si, + unsigned int nr_entries) { - long val =3D atomic_long_add_return_relaxed(nr_entries, &si->inuse_pages); + long inuse_pages; =20 /* * If device is full, and SWAP_USAGE_OFFLIST_BIT is not set, * remove it from the plist. */ - if (unlikely(val =3D=3D si->pages)) { - del_from_avail_list(si, false); - return true; - } - - return false; -} - -static void swap_usage_sub(struct swap_info_struct *si, unsigned int nr_en= tries) -{ - long val =3D atomic_long_sub_return_relaxed(nr_entries, &si->inuse_pages); - - /* - * If device is not full, and SWAP_USAGE_OFFLIST_BIT is set, - * add it to the plist. - */ - if (unlikely(val & SWAP_USAGE_OFFLIST_BIT)) - add_to_avail_list(si); -} - -static void swap_range_alloc(struct swap_info_struct *si, - unsigned int nr_entries) -{ - if (swap_usage_add(si, nr_entries)) { + inuse_pages =3D atomic_long_add_return_relaxed(nr_entries, &si->inuse_pag= es); + if (unlikely(inuse_pages =3D=3D si->pages)) { if (vm_swap_full()) schedule_work(&si->reclaim_work); + del_from_avail_list(si, false); } + atomic_long_sub(nr_entries, &nr_swap_pages); } =20 -static void swap_range_free(struct swap_info_struct *si, unsigned long off= set, - unsigned int nr_entries) +static void swap_device_inuse_sub(struct swap_info_struct *si, unsigned lo= ng offset, + unsigned int nr_entries) { unsigned long end =3D offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); + long inuse_pages; unsigned int i; =20 for (i =3D 0; i < nr_entries; i++) @@ -1356,7 +1339,14 @@ static void swap_range_free(struct swap_info_struct = *si, unsigned long offset, */ smp_wmb(); atomic_long_add(nr_entries, &nr_swap_pages); - swap_usage_sub(si, nr_entries); + + /* + * If device is not full, and SWAP_USAGE_OFFLIST_BIT is set, + * add it back to the plist. + */ + inuse_pages =3D atomic_long_sub_return_relaxed(nr_entries, &si->inuse_pag= es); + if (unlikely(inuse_pages & SWAP_USAGE_OFFLIST_BIT)) + add_to_avail_list(si); } =20 static bool get_swap_device_info(struct swap_info_struct *si) @@ -1969,7 +1959,7 @@ void __swap_cluster_free_entries(struct swap_info_str= uct *si, if (batch_id) mem_cgroup_uncharge_swap(batch_id, ci_off - batch_off); =20 - swap_range_free(si, ci_head + ci_start, nr_pages); + swap_device_inuse_sub(si, ci_head + ci_start, nr_pages); swap_cluster_assert_empty(ci, ci_start, nr_pages, false); =20 if (!ci->count) @@ -2821,7 +2811,7 @@ static int try_to_unuse(unsigned int type) success: /* * Make sure that further cleanups after try_to_unuse() returns happen - * after swap_range_free() reduces si->inuse_pages to 0. + * after swap_device_inuse_sub() reduces si->inuse_pages to 0. */ smp_mb(); return 0; --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 41275347534 for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=IRjI2Sltj3VQ7qoJfQsUp8w2YIK0ij2HNxz/qMRtw2hN2UVguP2NYUyTmz2MSK43jN3pyFxWvR+5k4EOPwuHFAO3yhJYFF0Lnw6jfxFtI1tMxgWba9ZpGV3bklnrUy3X/sTnMFsDGVf/sRFz2e8IjLOeutuaRn6LO36F09m58wY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=X9++b0tdU0WcK1lvav/2MrzCsxCu0DZ8UXbLrxHkZpQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ileGjiYkyZXXm8Tf+LbZxfkEl8AS9EZ3eevjHsKhxnWno8VSI3DRXPO9hwgQanDyoL92gJ7qt4V3wzEb+ww34OwcVxxTz07PKiWoSFBGL8tSvO+a930A0JC5dDC+VufN8KR9rzpL2x3dZCCd0F39sHVUVDcgVuqKLmvon4OHUNU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YibZUN3Q; 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="YibZUN3Q" Received: by smtp.kernel.org (Postfix) with ESMTPS id 261B0C2BCB9; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=X9++b0tdU0WcK1lvav/2MrzCsxCu0DZ8UXbLrxHkZpQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=YibZUN3QFbz/dcnGnqo/M7CioFKlkMB42/wyptKa/4u1VuZyIO2iamg+NnI+ODfve X8sfJIwmlHqGkPB+at8D5gcH7782WL3ABHSywBclxYUl8JwRAKjGvH/YtCJxBniqoL d4TBzOoqS3itFCDHXePFsCfqnnQ6GkOPtCv4SbX22i4eQfUakGiJxeLF8Fw51XU8as XBEeTzukgLsxVwAt8lM16btmZORlqp1UE6t02it+qRl8cAnbmnSATr7HhxwNX6Fl3G O+eMD+8+r97kALU+bdLCOK00sUodQlKIfy7IXVd+cO5OnWSRsI1Rtcd6FCQYJXGFV5 sRzSur5JraDXg== 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 137EAC44508; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:46 +0800 Subject: [PATCH RFC 08/13] mm/swap: change back to use each swap device's percpu cluster 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: <20260714-swap-pcp-priq-v1-8-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song , Baoquan He X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=14705; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=BUmWfAPWY09foj/vlnOFDopoDyBAWhFozE81v29suzM=; b=rJ1L0xbAejlWMyJoHalFLIP/Kv16TAwkfpvaKsbOlF3oSfK1tekLjknjzLUgw4RUee53+L/ZQ 8jhSptV7X7jB3ZaP7/W+Vz6wfqFKi4cNFZkfZrjIICQMxGH4GhMRm2c 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: Youngjun Park This reverts commit 1b7e90020eb7 ("mm, swap: use percpu cluster as allocation fast path"). The global percpu cluster causes several issues: 1) It prevents efficient swap allocation for users that do not want the default swap device policy. Because the cluster cache sits above device selection, all allocation users must stick with the default allocation policy. This fundamentally conflicts with incoming ideas like swap tiering. 2) It can cause priority inversion. Consider two memcgs: memcg1 can access devices A and B, where A has higher priority; memcg2 can only access B. Memcg2 can write the global percpu cluster with device B, then memcg1 takes B in the fast path even though higher-priority device A is not exhausted. 3) The fast-path / slow-path design bundled with the global per-CPU cache is problematic: only the fast path uses the global cache, and the slow path is forced to rotate the allocation list. This complicates consumers like discard. Revert to per-device percpu clusters first. A later patch will introduce new infrastructure for a high-performance global allocation path. Suggested-by: Kairui Song Co-developed-by: Baoquan He Signed-off-by: Baoquan He Signed-off-by: Youngjun Park Signed-off-by: Kairui Song --- include/linux/swap.h | 13 +++- mm/swapfile.c | 184 +++++++++++++++++------------------------------= ---- 2 files changed, 72 insertions(+), 125 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index d0c4e0ab5806..6733a8979aa2 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -246,10 +246,17 @@ enum { #endif =20 /* - * We keep using same cluster for rotational device so IO will be sequenti= al. - * The purpose is to optimize SWAP throughput on these device. + * We assign a cluster to each CPU, so each CPU can allocate swap entry fr= om + * its own cluster and swapout sequentially. The purpose is to optimize sw= apout + * throughput. */ +struct percpu_cluster { + local_lock_t lock; /* Protect the percpu_cluster above */ + unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */ +}; + struct swap_sequential_cluster { + spinlock_t lock; /* Serialize usage of global cluster */ unsigned int next[SWAP_NR_ORDERS]; /* Likely next allocation offset */ }; =20 @@ -272,8 +279,8 @@ struct swap_info_struct { /* list of cluster that are fragmented or contented */ unsigned int pages; /* total of usable pages of swap */ atomic_long_t inuse_pages; /* number of those currently in use */ + struct percpu_cluster __percpu *percpu_cluster; /* per cpu's swap locatio= n */ struct swap_sequential_cluster *global_cluster; /* Use one global cluster= for rotating device */ - spinlock_t global_cluster_lock; /* Serialize usage of global cluster */ struct rb_root swap_extent_root;/* root of the swap extent rbtree */ struct block_device *bdev; /* swap device or bdev of swap file */ struct file *swap_file; /* seldom referenced */ diff --git a/mm/swapfile.c b/mm/swapfile.c index df9855401a2e..b2adf36a0884 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -146,18 +146,6 @@ static atomic_t proc_poll_event =3D ATOMIC_INIT(0); =20 atomic_t nr_rotate_swap =3D ATOMIC_INIT(0); =20 -struct percpu_swap_cluster { - struct swap_info_struct *si[SWAP_NR_ORDERS]; - unsigned long offset[SWAP_NR_ORDERS]; - local_lock_t lock; -}; - -static DEFINE_PER_CPU(struct percpu_swap_cluster, percpu_swap_cluster) =3D= { - .si =3D { NULL }, - .offset =3D { SWAP_ENTRY_INVALID }, - .lock =3D INIT_LOCAL_LOCK(), -}; - /* May return NULL on invalid type, caller must check for NULL return */ static struct swap_info_struct *swap_type_to_info(int type) { @@ -562,9 +550,10 @@ swap_cluster_populate(struct swap_info_struct *si, * Only cluster isolation from the allocator does table allocation. * Swap allocator uses percpu clusters and holds the local lock. */ - lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); - if (!(si->flags & SWP_SOLIDSTATE)) - lockdep_assert_held(&si->global_cluster_lock); + if (si->flags & SWP_SOLIDSTATE) + lockdep_assert_held(this_cpu_ptr(&si->percpu_cluster->lock)); + else + lockdep_assert_held(&si->global_cluster->lock); lockdep_assert_held(&ci->lock); =20 if (!swap_cluster_alloc_table(ci, __GFP_HIGH | __GFP_NOMEMALLOC | @@ -577,9 +566,10 @@ swap_cluster_populate(struct swap_info_struct *si, * the potential recursive allocation is limited. */ spin_unlock(&ci->lock); - if (!(si->flags & SWP_SOLIDSTATE)) - spin_unlock(&si->global_cluster_lock); - local_unlock(&percpu_swap_cluster.lock); + if (si->flags & SWP_SOLIDSTATE) + local_unlock(&si->percpu_cluster->lock); + else + spin_unlock(&si->global_cluster->lock); =20 ret =3D swap_cluster_alloc_table(ci, __GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL); @@ -592,9 +582,10 @@ swap_cluster_populate(struct swap_info_struct *si, * could happen with ignoring the percpu cluster is fragmentation, * which is acceptable since this fallback and race is rare. */ - local_lock(&percpu_swap_cluster.lock); - if (!(si->flags & SWP_SOLIDSTATE)) - spin_lock(&si->global_cluster_lock); + if (si->flags & SWP_SOLIDSTATE) + local_lock(&si->percpu_cluster->lock); + else + spin_lock(&si->global_cluster->lock); spin_lock(&ci->lock); =20 if (ret) { @@ -700,7 +691,7 @@ static bool swap_do_scheduled_discard(struct swap_info_= struct *si) ci =3D list_first_entry(&si->discard_clusters, struct swap_cluster_info,= list); /* * Delete the cluster from list to prepare for discard, but keep - * the CLUSTER_FLAG_DISCARD flag, percpu_swap_cluster could be + * the CLUSTER_FLAG_DISCARD flag, there could be percpu_cluster * pointing to it, or ran into by relocate_cluster. */ list_del(&ci->list); @@ -1032,12 +1023,10 @@ static unsigned int alloc_swap_scan_cluster(struct = swap_info_struct *si, out: relocate_cluster(si, ci); swap_cluster_unlock(ci); - if (si->flags & SWP_SOLIDSTATE) { - this_cpu_write(percpu_swap_cluster.offset[order], next); - this_cpu_write(percpu_swap_cluster.si[order], si); - } else { + if (si->flags & SWP_SOLIDSTATE) + this_cpu_write(si->percpu_cluster->next[order], next); + else si->global_cluster->next[order] =3D next; - } return found; } =20 @@ -1132,13 +1121,17 @@ static unsigned long cluster_alloc_swap_entry(struc= t swap_info_struct *si, if (order && !(si->flags & SWP_BLKDEV)) return 0; =20 - if (!(si->flags & SWP_SOLIDSTATE)) { + if (si->flags & SWP_SOLIDSTATE) { + /* Fast path using per CPU cluster */ + local_lock(&si->percpu_cluster->lock); + offset =3D __this_cpu_read(si->percpu_cluster->next[order]); + } else { /* Serialize HDD SWAP allocation for each device. */ - spin_lock(&si->global_cluster_lock); + spin_lock(&si->global_cluster->lock); offset =3D si->global_cluster->next[order]; - if (offset =3D=3D SWAP_ENTRY_INVALID) - goto new_cluster; + } =20 + if (offset !=3D SWAP_ENTRY_INVALID) { ci =3D swap_cluster_lock(si, offset); /* Cluster could have been used by another order */ if (cluster_is_usable(ci, order)) { @@ -1152,7 +1145,6 @@ static unsigned long cluster_alloc_swap_entry(struct = swap_info_struct *si, goto done; } =20 -new_cluster: /* * If the device need discard, prefer new cluster over nonfull * to spread out the writes. @@ -1209,8 +1201,10 @@ static unsigned long cluster_alloc_swap_entry(struct= swap_info_struct *si, goto done; } done: - if (!(si->flags & SWP_SOLIDSTATE)) - spin_unlock(&si->global_cluster_lock); + if (si->flags & SWP_SOLIDSTATE) + local_unlock(&si->percpu_cluster->lock); + else + spin_unlock(&si->global_cluster->lock); =20 return found; } @@ -1365,41 +1359,8 @@ static bool get_swap_device_info(struct swap_info_st= ruct *si) return true; } =20 -/* - * Fast path try to get swap entries with specified order from current - * CPU's swap entry pool (a cluster). - */ -static bool swap_alloc_fast(struct folio *folio) -{ - unsigned int order =3D folio_order(folio); - struct swap_cluster_info *ci; - struct swap_info_struct *si; - unsigned int offset; - - /* - * Once allocated, swap_info_struct will never be completely freed, - * so checking it's liveness by get_swap_device_info is enough. - */ - si =3D this_cpu_read(percpu_swap_cluster.si[order]); - offset =3D this_cpu_read(percpu_swap_cluster.offset[order]); - if (!si || !offset || !get_swap_device_info(si)) - return false; - - ci =3D swap_cluster_lock(si, offset); - if (cluster_is_usable(ci, order)) { - if (cluster_is_empty(ci)) - offset =3D cluster_offset(si, ci); - alloc_swap_scan_cluster(si, ci, folio, offset); - } else { - swap_cluster_unlock(ci); - } - - put_swap_device(si); - return folio_test_swapcache(folio); -} - /* Rotate the device and switch to a new cluster */ -static void swap_alloc_slow(struct folio *folio) +static void swap_alloc_entry(struct folio *folio) { struct swap_info_struct *si, *next; =20 @@ -1769,10 +1730,7 @@ int folio_alloc_swap(struct folio *folio) } =20 again: - local_lock(&percpu_swap_cluster.lock); - if (!swap_alloc_fast(folio)) - swap_alloc_slow(folio); - local_unlock(&percpu_swap_cluster.lock); + swap_alloc_entry(folio); =20 if (!order && unlikely(!folio_test_swapcache(folio))) { if (swap_sync_discard()) @@ -2161,31 +2119,14 @@ void swap_put_entries_direct(swp_entry_t entry, int= nr) */ swp_entry_t swap_alloc_hibernation_slot(int type) { - struct swap_info_struct *pcp_si, *si =3D swap_type_to_info(type); - unsigned long pcp_offset, offset =3D SWAP_ENTRY_INVALID; - struct swap_cluster_info *ci; + struct swap_info_struct *si =3D swap_type_to_info(type); + unsigned long offset =3D SWAP_ENTRY_INVALID; swp_entry_t entry =3D {0}; =20 if (!si) goto fail; =20 - /* - * Try the local cluster first if it matches the device. If - * not, try grab a new cluster and override local cluster. - */ - local_lock(&percpu_swap_cluster.lock); - pcp_si =3D this_cpu_read(percpu_swap_cluster.si[0]); - pcp_offset =3D this_cpu_read(percpu_swap_cluster.offset[0]); - if (pcp_si =3D=3D si && pcp_offset) { - ci =3D swap_cluster_lock(si, pcp_offset); - if (cluster_is_usable(ci, 0)) - offset =3D alloc_swap_scan_cluster(si, ci, NULL, pcp_offset); - else - swap_cluster_unlock(ci); - } - if (!offset) - offset =3D cluster_alloc_swap_entry(si, NULL); - local_unlock(&percpu_swap_cluster.lock); + offset =3D cluster_alloc_swap_entry(si, NULL); if (offset) entry =3D swp_entry(si->type, offset); =20 @@ -3052,27 +2993,6 @@ static void free_swap_cluster_info(struct swap_clust= er_info *cluster_info, kvfree(cluster_info); } =20 -/* - * Called after swap device's reference count is dead, so - * neither scan nor allocation will use it. - */ -static void flush_percpu_swap_cluster(struct swap_info_struct *si) -{ - int cpu, i; - struct swap_info_struct **pcp_si; - - for_each_possible_cpu(cpu) { - pcp_si =3D per_cpu_ptr(percpu_swap_cluster.si, cpu); - /* - * Invalidate the percpu swap cluster cache, si->users - * is dead, so no new user will point to it, just flush - * any existing user. - */ - for (i =3D 0; i < SWAP_NR_ORDERS; i++) - cmpxchg(&pcp_si[i], si, NULL); - } -} - SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) { struct swap_info_struct *p =3D NULL; @@ -3136,7 +3056,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) =20 flush_work(&p->discard_work); flush_work(&p->reclaim_work); - flush_percpu_swap_cluster(p); =20 destroy_swap_extents(p, p->swap_file); =20 @@ -3157,6 +3076,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) percpu_up_write(&swapon_rwsem); arch_swap_invalidate_area(p->type); zswap_swapoff(p->type); + free_percpu(p->percpu_cluster); + p->percpu_cluster =3D NULL; kfree(p->global_cluster); p->global_cluster =3D NULL; free_swap_cluster_info(cluster_info, maxpages); @@ -3505,7 +3426,7 @@ static int setup_swap_clusters_info(struct swap_info_= struct *si, { unsigned long nr_clusters =3D DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); struct swap_cluster_info *cluster_info; - int err =3D -ENOMEM; + int cpu, err =3D -ENOMEM; unsigned long i; =20 cluster_info =3D kvzalloc_objs(*cluster_info, nr_clusters); @@ -3515,13 +3436,26 @@ static int setup_swap_clusters_info(struct swap_inf= o_struct *si, for (i =3D 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); =20 - if (!(si->flags & SWP_SOLIDSTATE)) { + if (si->flags & SWP_SOLIDSTATE) { + si->percpu_cluster =3D alloc_percpu(struct percpu_cluster); + if (!si->percpu_cluster) + goto err; + + for_each_possible_cpu(cpu) { + struct percpu_cluster *cluster; + + cluster =3D per_cpu_ptr(si->percpu_cluster, cpu); + for (i =3D 0; i < SWAP_NR_ORDERS; i++) + cluster->next[i] =3D SWAP_ENTRY_INVALID; + local_lock_init(&cluster->lock); + } + } else { si->global_cluster =3D kmalloc_obj(*si->global_cluster); if (!si->global_cluster) goto err; for (i =3D 0; i < SWAP_NR_ORDERS; i++) si->global_cluster->next[i] =3D SWAP_ENTRY_INVALID; - spin_lock_init(&si->global_cluster_lock); + spin_lock_init(&si->global_cluster->lock); } =20 /* @@ -3683,11 +3617,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) =20 maxpages =3D si->max; =20 - /* Set up the swap cluster info */ - error =3D setup_swap_clusters_info(si, swap_header, maxpages); - if (error) - goto bad_swap_unlock_inode; - if (si->bdev && bdev_stable_writes(si->bdev)) si->flags |=3D SWP_STABLE_WRITES; =20 @@ -3701,6 +3630,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) inced_nr_rotate_swap =3D true; } =20 + /* + * Set up the swap cluster info. This must run after SWP_SOLIDSTATE + * is determined above, as it decides whether to allocate the per-CPU + * cluster (solid state) or the global cluster (rotational). + */ + error =3D setup_swap_clusters_info(si, swap_header, maxpages); + if (error) + goto bad_swap_unlock_inode; + if ((swap_flags & SWAP_FLAG_DISCARD) && si->bdev && bdev_max_discard_sectors(si->bdev)) { /* @@ -3782,6 +3720,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) bad_swap_unlock_inode: inode_unlock(inode); bad_swap: + free_percpu(si->percpu_cluster); + si->percpu_cluster =3D NULL; kfree(si->global_cluster); si->global_cluster =3D NULL; inode =3D NULL; --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 5558A347BAF for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=BiyDdHVy/kBOFtp+t86iyfOOvor2S4WRH5y8iyJDhlh/nr28HLX2jzW86Dkg/tw4QHY4RIFT6jVMVt2PFnIGakXjm+QFDCR6zoH21RyZs00TjAiviRjNRcywwfeGC3MURJZr49K1Ve/n0QIl+FeE/+1pmRKBT6qKFV17eYbL01k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=JQKJIgJ9eudom/HTZ6AW9z1JjGv/8wYywvRoKVizjs4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DqAZDjS0Gk/NNG+UiIRz0JLLJBqqa3LUnnB6QyRBT7K4zuIFFtZEaq8WKfHogLNtwiZHU73Ii6Vc6KM91GaCbsQYJPWX6WheYPAOhLkA+4XJB0TxFQefQdybsvDQr+H7mzthEnSfaZ0korGUfcXkJ1x/h9hhKstapVTRfjGnTRU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NQXWFo2X; 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="NQXWFo2X" Received: by smtp.kernel.org (Postfix) with ESMTPS id 3A278C2BCB8; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=JQKJIgJ9eudom/HTZ6AW9z1JjGv/8wYywvRoKVizjs4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=NQXWFo2XNWGl88EopSPbISG+j4H3/JVmFQbuCjUXuXJBJ3vF3Tujef3uPKQ28pilR Q3ZmGmUmEENYqNfjicdWvgR9ZFJAAMcMTaC85mgZQKV4AhMN3qQutN9+baLBmHMnsh l81RXjNUpDr/lIxxbrYPR/cbR/hpWgQs4bxYdh80tjCwebqyttEXKjQaGoG2ciuYGV eI9nkpMv2ZJoLcfJNBVZr7LIJXv+wgwOENSWIYm1Ff6tcz1w0kGqx2y5VxRmrz/Qiw 9WpstUluvSJPiRu9nmSRtQEkpICxQCGcPU0ENz6my9MsJCg1r3FhpF2fuoYiZvwaqC LrMzceYV3jp2A== 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 29AC5C44501; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:47 +0800 Subject: [PATCH RFC 09/13] mm/swap: add priority queue for swap device allocation 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: <20260714-swap-pcp-priq-v1-9-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=22359; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=8tS3UITEBOW10HWmX2xTNUIk0tFMGKzXLCIVKJeQmlw=; b=asjL7hIUg10OcSXI2eBjFUqN4Hj13zEqZjrAQamKtY/UIj8mbASUi+TojxI+I7DnE5Kt7b89b HefxciF5gYgAf3ZCBXFWPtE4LLFEkSq2bJ2GmwJixZFlPxjequ4EdbR 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 The swap allocator uses a plist (swap_avail_head) ordered by priority to pick the next device. To spread IO across devices at the same priority it rotates the current device to the tail via plist_requeue(), which requires holding swap_avail_lock. It then drops the lock for the allocation attempt and reacquires to check whether the next device is still on the list. This per-allocation lock cycling and the single global rotation point mean all CPUs contend for the same list head. Add a custom priority queue that groups devices into rings, ordered by priority to form a static array as the queue. Devices in the same priority ring also form a static array. To eliminate CPU contention on a global lock, swapon_rwsem is reused to protect the queue. The queue stays read-only, only swapon and swapoff can adjust the length of the queue or rings in it. The writer lock is costly but swapon and swapoff are rare so this is acceptable. Since the queue is read-only, rotation is done from the reader side. Each CPU maintains its own iterator using a counter that counts down to advance to the next device in the same ring, so allocation is round-robin while retaining locality for clustering. After a failed attempt the iterator also advances so the caller does not retry the same full or fragmented device. If a whole ring is attempted and allocation could not be made, the iterator falls through to the next ring. This almost never happens because we always want to allocate from the device with the highest priority, unless that device is full. To speed up iteration when there are multiple full devices, a device pointer stored in the ring can be marked as unavailable so the iterator skips past it. Fully used devices can mark their pointer as unavailable without taking the rwsem lock. The marking is done using a spin lock to sync with potential writer because percpu rwsem is too heavy for writers, and full device transitions are more frequent and performance-sensitive compared to swapon and swapoff. With this patch, swap allocation should be faster and cleaner. The percpu rwsem has very low overhead for the allocator, and it can now hold swapon_rwsem for the entire allocation loop, no longer needing to keep dropping and reacquiring a lock to rotate list entries. The old swap_avail_head plist is still maintained in parallel for now, updated alongside the queue in del_from_avail_list() and add_to_avail_list(). It will be removed once the remaining consumer is converted in a follow-up patch. Signed-off-by: Kairui Song --- mm/swapfile.c | 455 ++++++++++++++++++++++++++++++++++++++++++++++++++----= ---- 1 file changed, 398 insertions(+), 57 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index b2adf36a0884..f6dd85968928 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -55,6 +55,7 @@ static bool folio_swapcache_freeable(struct folio *folio); static void move_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci, struct list_head *list, enum swap_cluster_flags new_flags); +static bool get_swap_device_info(struct swap_info_struct *si); =20 /* * Serializes swapon/swapoff (writers) and protects the swap_info @@ -160,18 +161,350 @@ static struct swap_info_struct *swap_entry_to_info(s= wp_entry_t entry) return swap_type_to_info(swp_type(entry)); } =20 +/* + * All available swap_info_structs are grouped by priority rings, the rings + * are ordered in a queue by priority (lower prio value =3D higher priorit= y). + * The allocator iterates and rotates devices within each priority ring. + * When all devices in a ring are iterated, it goes to the next lower + * priority ring. + */ +struct swap_prio_ring { + int prio; + unsigned int size; + struct swap_info_struct *dev[] __counted_by(size); +}; + +/* + * The ring is protected by swapon_rwsem so updating it is costly. To make + * the allocator and other users skip full devices faster, the lowest bit = of + * a device pointer is used to mark it disabled (temporarily unavailable). + * This relies on struct swap_info_struct being sufficiently + * aligned (guaranteed by kmalloc). + */ +#define SWAP_DEVICE_MASKED_SHIFT 0 +#define SWAP_DEVICE_MASKED_BIT BIT(SWAP_DEVICE_MASKED_SHIFT) + +/* + * Serializes queue content mutations and keeps SWAP_USAGE_OFFLIST_BIT + * consistent with the masked state of each device pointer. + */ +static DEFINE_SPINLOCK(swap_queue_update_lock); + +/* + * Swap queue is protected by both swap_queue_update_lock and swapon_rwsem. + * Only swapon/swapoff will take the write lock, and modify the queue leng= th + * or any ring's length. swap_queue_update_lock protects the content so + * devices can be masked easily without taking the writelock, which is hea= vy. + */ +static struct swap_prio_ring **swap_queue; +static unsigned int swap_queue_len; + +/* + * Each CPU has its read iterator, so the queue itself will remain read + * only and the CPU side reader rotates by iterating the devices + * periodically using the counter. + */ +#define SWAP_ROUND_ROBIN_QUOTA SWAPFILE_CLUSTER +struct swap_ring_iterator { + int offset; + long rr_counter; +}; + +struct swap_queue_reader { + local_lock_t lock; + struct swap_ring_iterator ri[]; +}; + +static __percpu struct swap_queue_reader *swap_queue_readers; + +static inline bool swap_device_masked(struct swap_info_struct *si) +{ + return (unsigned long)si & SWAP_DEVICE_MASKED_BIT; +} + +static inline struct swap_info_struct *swap_device_unmask_ptr(struct swap_= info_struct *si) +{ + return (struct swap_info_struct *)((unsigned long)si & ~SWAP_DEVICE_MASKE= D_BIT); +} + +static struct swap_queue_reader __percpu *swap_queue_prealloc_readers(int = nr_rings, gfp_t gfp) +{ + struct swap_queue_reader __percpu *readers; + + if (!nr_rings) + return NULL; + + readers =3D __alloc_percpu_gfp(struct_size(readers, ri, nr_rings), + __alignof__(*readers), gfp); + return readers; +} + +static void swap_queue_install_readers(struct swap_queue_reader __percpu *= readers) +{ + int ring_idx, cpu; + struct swap_prio_ring *ring; + + free_percpu(swap_queue_readers); + swap_queue_readers =3D readers; + if (!readers) + return; + + /* Distribute each CPU's swap IO fairly across devices. */ + for_each_possible_cpu(cpu) { + local_lock_init(&per_cpu_ptr(readers, cpu)->lock); + + for (ring_idx =3D 0; ring_idx < swap_queue_len; ring_idx++) { + ring =3D swap_queue[ring_idx]; + per_cpu_ptr(readers, cpu)->ri[ring_idx].offset =3D + cpu % ring->size; + } + } +} + +static struct swap_info_struct *swap_queue_get_device(long nr_alloc, int n= r_iter) +{ + bool rotate =3D false; + struct swap_info_struct *si; + struct swap_ring_iterator *ri; + struct swap_prio_ring *ring; + unsigned int queue_idx; + + if (!swap_queue_len) + return ERR_PTR(-ENOENT); + + queue_idx =3D 0; + while (nr_iter >=3D swap_queue[queue_idx]->size) { + nr_iter -=3D swap_queue[queue_idx]->size; + if (++queue_idx >=3D swap_queue_len) + return ERR_PTR(-ENOENT); + } + + ring =3D swap_queue[queue_idx]; + local_lock(&swap_queue_readers->lock); + ri =3D this_cpu_ptr(&swap_queue_readers->ri[queue_idx]); + /* Rotate while iterating the ring, just not on the first try */ + if (nr_iter) + rotate =3D true; + else if (ri->rr_counter < nr_alloc) + rotate =3D true; + else if (ri->offset >=3D ring->size) + rotate =3D true; + if (rotate) { + ri->offset++; + ri->offset %=3D ring->size; + ri->rr_counter =3D SWAP_ROUND_ROBIN_QUOTA; + } + ri->rr_counter -=3D nr_alloc; + si =3D READ_ONCE(ring->dev[ri->offset]); + local_unlock(&swap_queue_readers->lock); + + if (swap_device_masked(si)) + return ERR_PTR(-EBUSY); + + si =3D swap_device_unmask_ptr(si); + return si; +} + +static bool swap_queue_find(struct swap_info_struct *si, + unsigned int *ring_idx, unsigned int *dev_idx) +{ + unsigned int i, j; + struct swap_prio_ring *ring; + + lockdep_assert(lockdep_is_held(&swapon_rwsem) || + lockdep_is_held(&swap_queue_update_lock)); + + for (i =3D 0; i < swap_queue_len; i++) { + ring =3D swap_queue[i]; + if (ring->prio !=3D si->prio) + continue; + for (j =3D 0; j < ring->size; j++) { + if (swap_device_unmask_ptr(READ_ONCE(ring->dev[j])) !=3D si) + continue; + *ring_idx =3D i; + *dev_idx =3D j; + return true; + } + } + return false; +} + +static void swap_queue_mask(struct swap_info_struct *si) +{ + unsigned int ring_idx, dev_idx; + + lockdep_assert_held(&swap_queue_update_lock); + if (swap_queue_find(si, &ring_idx, &dev_idx)) + __set_bit(SWAP_DEVICE_MASKED_SHIFT, + (unsigned long *)&swap_queue[ring_idx]->dev[dev_idx]); +} + +static void swap_queue_unmask(struct swap_info_struct *si) +{ + unsigned int ring_idx, dev_idx; + + lockdep_assert_held(&swap_queue_update_lock); + if (swap_queue_find(si, &ring_idx, &dev_idx)) + __clear_bit(SWAP_DEVICE_MASKED_SHIFT, + (unsigned long *)&swap_queue[ring_idx]->dev[dev_idx]); +} + +static int swap_queue_add(struct swap_info_struct *si) +{ + struct swap_prio_ring **new_queue =3D NULL, **old_queue =3D NULL; + struct swap_queue_reader __percpu *new_readers =3D NULL; + struct swap_prio_ring *ring, *new_ring =3D NULL, *old_ring =3D NULL; + int prio =3D si->prio; + int i, pos, err =3D -ENOMEM; + gfp_t gfp; + + /* Swap not usable here because this is swap, just reclaim cache. */ + gfp =3D GFP_NOIO | __GFP_HIGH; + lockdep_assert_held_write(&swapon_rwsem); + + for (pos =3D 0; pos < swap_queue_len; pos++) { + if (swap_queue[pos]->prio =3D=3D prio) + goto add_to_ring; + if (swap_queue[pos]->prio < prio) + break; + } + + /* No ring at this priority: insert a new one at pos. */ + new_readers =3D swap_queue_prealloc_readers(swap_queue_len + 1, gfp); + if (!new_readers) + goto failed; + new_queue =3D kmalloc_array(swap_queue_len + 1, sizeof(*swap_queue), gfp); + if (!new_queue) + goto failed; + new_ring =3D kmalloc(struct_size(new_ring, dev, 1), gfp); + if (!new_ring) + goto failed; + if (!get_swap_device_info(si)) + goto failed; + + new_ring->prio =3D prio; + new_ring->size =3D 1; + new_ring->dev[0] =3D si; + for (i =3D 0; i < pos; i++) + new_queue[i] =3D swap_queue[i]; + new_queue[pos] =3D new_ring; + for (i =3D pos; i < swap_queue_len; i++) + new_queue[i + 1] =3D swap_queue[i]; + + spin_lock(&swap_queue_update_lock); + old_queue =3D swap_queue; + swap_queue =3D new_queue; + swap_queue_len++; + spin_unlock(&swap_queue_update_lock); + kfree(old_queue); + + swap_queue_install_readers(new_readers); + return 0; + +add_to_ring: + ring =3D swap_queue[pos]; + new_ring =3D kmalloc(struct_size(ring, dev, ring->size + 1), gfp); + if (!new_ring) + goto failed; + if (!get_swap_device_info(si)) + goto failed; + spin_lock(&swap_queue_update_lock); + memcpy(new_ring, ring, struct_size(ring, dev, ring->size)); + new_ring->size++; + new_ring->dev[new_ring->size - 1] =3D si; + old_ring =3D swap_queue[pos]; + swap_queue[pos] =3D new_ring; + spin_unlock(&swap_queue_update_lock); + kfree(old_ring); + return 0; + +failed: + free_percpu(new_readers); + kfree(new_queue); + kfree(new_ring); + return err; +} + +static void swap_queue_del(struct swap_info_struct *si) +{ + gfp_t gfp; + unsigned int ring_idx, dev_idx; + struct swap_queue_reader __percpu *new_readers =3D NULL; + struct swap_prio_ring *ring, *new_ring =3D NULL, *old_ring =3D NULL; + struct swap_prio_ring **new_queue =3D NULL, **old_queue =3D NULL; + + lockdep_assert_held_write(&swapon_rwsem); + if (!swap_queue_find(si, &ring_idx, &dev_idx)) { + WARN_ON(1); + return; + } + + /* + * To shrink memory usage, pre-allocate new smaller data before + * locking. Failure is fine, swapoff will release them anyway. + */ + gfp =3D GFP_NOIO | __GFP_HIGH; + ring =3D swap_queue[ring_idx]; + if (ring->size > 1) + new_ring =3D kmalloc(struct_size(ring, dev, ring->size - 1), gfp); + if (ring->size =3D=3D 1 && swap_queue_len > 1) { + new_readers =3D swap_queue_prealloc_readers( + swap_queue_len - 1, gfp); + new_queue =3D kmalloc(sizeof(*swap_queue) * + (swap_queue_len - 1), gfp); + } + + spin_lock(&swap_queue_update_lock); + if (ring->size > 1) { + /* Shift trailing devices left to fill the gap. */ + while (++dev_idx < ring->size) + ring->dev[dev_idx - 1] =3D + ring->dev[dev_idx]; + ring->size--; + if (new_ring) { + memcpy(new_ring, ring, + struct_size(ring, dev, ring->size)); + old_ring =3D ring; + swap_queue[ring_idx] =3D new_ring; + } + } else { + /* Last device in this ring: remove the ring. */ + old_ring =3D ring; + swap_queue_len--; + while (++ring_idx <=3D swap_queue_len) + swap_queue[ring_idx - 1] =3D + swap_queue[ring_idx]; + if (new_queue) { + memcpy(new_queue, swap_queue, + sizeof(*swap_queue) * swap_queue_len); + old_queue =3D swap_queue; + swap_queue =3D new_queue; + } else if (!swap_queue_len) { + old_queue =3D swap_queue; + swap_queue =3D NULL; + } + if (new_readers || !swap_queue_len) + swap_queue_install_readers(new_readers); + } + spin_unlock(&swap_queue_update_lock); + + kfree(old_ring); + kfree(old_queue); + put_swap_device(si); +} + /* * Use the second highest bit of inuse_pages counter as the indicator - * if one swap device is on the available plist, so the atomic can + * if one swap device is unavailable for allocation, so the atomic can * still be updated arithmetically while having special data embedded. * * inuse_pages counter is the only thing indicating if a device should - * be on avail_lists or not (except swapon / swapoff). By embedding the - * off-list bit in the atomic counter, updates no longer need any lock - * to check the list status. + * be in the available queue or not (except swapon / swapoff). By + * embedding the off-list bit in the atomic counter, updates no longer + * need any lock to check the list status. * - * This bit will be set if the device is not on the plist and not - * usable, will be cleared if the device is on the plist. + * This bit will be set if the device is not in the available queue + * and not usable, will be cleared if the device is in the queue. */ #define SWAP_USAGE_OFFLIST_BIT (1UL << (BITS_PER_TYPE(atomic_t) - 2)) #define SWAP_USAGE_COUNTER_MASK (~SWAP_USAGE_OFFLIST_BIT) @@ -1215,6 +1548,7 @@ static void del_from_avail_list(struct swap_info_stru= ct *si, bool swapoff) unsigned long pages; =20 spin_lock(&swap_avail_lock); + spin_lock(&swap_queue_update_lock); =20 /* * Force remove it only for swapoff. Else, take it off-list only if @@ -1232,9 +1566,10 @@ static void del_from_avail_list(struct swap_info_str= uct *si, bool swapoff) atomic_long_or(SWAP_USAGE_OFFLIST_BIT, &si->inuse_pages); } =20 + swap_queue_mask(si); plist_del(&si->avail_list, &swap_avail_head); - skip: + spin_unlock(&swap_queue_update_lock); spin_unlock(&swap_avail_lock); } =20 @@ -1245,12 +1580,12 @@ static void add_to_avail_list(struct swap_info_stru= ct *si) unsigned long pages; =20 spin_lock(&swap_avail_lock); + spin_lock(&swap_queue_update_lock); =20 /* - * Add the device to the avail list if SWP_WRITEOK is set and - * SWAP_USAGE_OFFLIST_BIT is still set. Swapoff clears - * SWP_WRITEOK first, so the device won't be re-added after - * swapoff starts unless swap_device_enable resurrects it. + * Mark the device as avail if SWP_WRITEOK is set. Swapoff clears + * SWP_WRITEOK first, so check that first so the device won't be + * re-added after swapoff started. */ if (!(si->flags & SWP_WRITEOK)) goto skip; @@ -1261,9 +1596,10 @@ static void add_to_avail_list(struct swap_info_struc= t *si) val =3D atomic_long_fetch_and_relaxed(~SWAP_USAGE_OFFLIST_BIT, &si->inuse= _pages); =20 /* - * When device is full and device is on the plist, only one updater will - * see (inuse_pages =3D=3D si->pages) and will call del_from_avail_list. = If - * that updater happen to be here, just skip adding. + * When device is full and marked as available, one reader will see + * (inuse_pages =3D=3D si->pages) and should mark it as unavailable and + * set SWAP_USAGE_OFFLIST_BIT. If that updater happens to be here, just + * skip the rest. */ pages =3D si->pages; if (val =3D=3D pages) { @@ -1273,10 +1609,10 @@ static void add_to_avail_list(struct swap_info_stru= ct *si) goto skip; } =20 + swap_queue_unmask(si); plist_add(&si->avail_list, &swap_avail_head); - skip: - spin_unlock(&swap_avail_lock); + spin_unlock(&swap_queue_update_lock); } =20 /* @@ -1292,7 +1628,7 @@ static void swap_device_inuse_add(struct swap_info_st= ruct *si, =20 /* * If device is full, and SWAP_USAGE_OFFLIST_BIT is not set, - * remove it from the plist. + * mark it unavailable. */ inuse_pages =3D atomic_long_add_return_relaxed(nr_entries, &si->inuse_pag= es); if (unlikely(inuse_pages =3D=3D si->pages)) { @@ -1336,7 +1672,7 @@ static void swap_device_inuse_sub(struct swap_info_st= ruct *si, unsigned long off =20 /* * If device is not full, and SWAP_USAGE_OFFLIST_BIT is set, - * add it back to the plist. + * add it back to the available queue. */ inuse_pages =3D atomic_long_sub_return_relaxed(nr_entries, &si->inuse_pag= es); if (unlikely(inuse_pages & SWAP_USAGE_OFFLIST_BIT)) @@ -1359,41 +1695,40 @@ static bool get_swap_device_info(struct swap_info_s= truct *si) return true; } =20 -/* Rotate the device and switch to a new cluster */ -static void swap_alloc_entry(struct folio *folio) +static int swap_alloc_entry(struct folio *folio) { - struct swap_info_struct *si, *next; + long nr_pages =3D folio_nr_pages(folio); + struct swap_info_struct *si; + int nr_iter, ret; =20 - spin_lock(&swap_avail_lock); -start_over: - plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { - /* Rotate the device and switch to a new cluster */ - plist_requeue(&si->avail_list, &swap_avail_head); - spin_unlock(&swap_avail_lock); - if (get_swap_device_info(si)) { - cluster_alloc_swap_entry(si, folio); - put_swap_device(si); - if (folio_test_swapcache(folio)) - return; - if (folio_test_large(folio)) - return; + percpu_down_read(&swapon_rwsem); + for (nr_iter =3D 0;; nr_iter++) { + si =3D swap_queue_get_device(nr_pages, nr_iter); + if (IS_ERR(si)) { + ret =3D PTR_ERR(si); + if (ret =3D=3D -EBUSY) + continue; + break; + } + cluster_alloc_swap_entry(si, folio); + + if (folio_test_swapcache(folio)) { + ret =3D 0; + break; } =20 - spin_lock(&swap_avail_lock); /* - * if we got here, it's likely that si was almost full before, - * multiple callers probably all tried to get a page from the - * same si and it filled up before we could get one; or, the si - * filled up between us dropping swap_avail_lock. - * Since we dropped the swap_avail_lock, the swap_avail_list - * may have been modified; so if next is still in the - * swap_avail_head list then try it, otherwise start over if we - * have not gotten any slots. + * For large allocation, return error directly to inform the + * caller to split it instead of fallback to other devices. */ - if (plist_node_empty(&next->avail_list)) - goto start_over; + if (folio_test_large(folio)) { + ret =3D -E2BIG; + break; + } } - spin_unlock(&swap_avail_lock); + + percpu_up_read(&swapon_rwsem); + return ret; } =20 /* @@ -1707,6 +2042,7 @@ int folio_alloc_swap(struct folio *folio) { unsigned int order =3D folio_order(folio); unsigned int size =3D 1 << order; + int ret; =20 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_uptodate(folio), folio); @@ -1730,7 +2066,7 @@ int folio_alloc_swap(struct folio *folio) } =20 again: - swap_alloc_entry(folio); + ret =3D swap_alloc_entry(folio); =20 if (!order && unlikely(!folio_test_swapcache(folio))) { if (swap_sync_discard()) @@ -1742,7 +2078,7 @@ int folio_alloc_swap(struct folio *folio) swap_cache_del_folio(folio); =20 if (unlikely(!folio_test_swapcache(folio))) - return -ENOMEM; + return ret ? ret : -ENOMEM; =20 return 0; } @@ -2913,10 +3249,9 @@ static int setup_swap_extents(struct swap_info_struc= t *sis, static void swap_device_enable(struct swap_info_struct *si) { percpu_down_write(&swapon_rwsem); - spin_lock(&swap_avail_lock); + spin_lock(&swap_queue_update_lock); si->flags |=3D SWP_WRITEOK; - spin_unlock(&swap_avail_lock); - + spin_unlock(&swap_queue_update_lock); atomic_long_add(si->pages, &nr_swap_pages); total_swap_pages +=3D si->pages; plist_add(&si->list, &swap_active_head); @@ -2948,16 +3283,14 @@ static int swap_device_disable(struct swap_info_str= uct *si) } vm_unacct_memory(si->pages); =20 - spin_lock(&swap_avail_lock); + spin_lock(&swap_queue_update_lock); si->flags &=3D ~SWP_WRITEOK; - spin_unlock(&swap_avail_lock); - + spin_unlock(&swap_queue_update_lock); plist_del(&si->list, &swap_active_head); total_swap_pages -=3D si->pages; atomic_long_sub(si->pages, &nr_swap_pages); - percpu_up_write(&swapon_rwsem); - del_from_avail_list(si, true); + percpu_up_write(&swapon_rwsem); =20 /* * Swap allocator doesn't touch si lock, so looping through all @@ -3042,6 +3375,9 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) return err; } =20 + percpu_down_write(&swapon_rwsem); + swap_queue_del(p); + /* * Wait for swap operations protected by get/put_swap_device() * to complete. Because of synchronize_rcu() here, all swap @@ -3062,7 +3398,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) if (!(p->flags & SWP_SOLIDSTATE)) atomic_dec(&nr_rotate_swap); =20 - percpu_down_write(&swapon_rwsem); spin_lock(&p->lock); drain_mmlist(); =20 @@ -3700,6 +4035,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) =20 /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */ percpu_ref_resurrect(&si->users); + percpu_down_write(&swapon_rwsem); + error =3D swap_queue_add(si); + percpu_up_write(&swapon_rwsem); + if (error) + goto free_swap_zswap; + swap_device_enable(si); =20 pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s= %s%s\n", --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 641C829B228 for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=E3DwihMWVV+OCW1Kqnr78MwRlx9IdonrBJQ5enQZyhYmiEN3snWStzir6K5C6NhSgeTS0Yp+kDpsoPXLGQcRuvh+x3cJdw0D3liXpTXTc/r7SGR9HQWR2oY8TzGDGLFcOIHUbf94XfZ3FvFBpL465KQYN1F+59FDTuHPS7dUI20= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=jSttoTQ+5tq94iBCfbqmVA02B8ZLP5x+PrcpR0wM01s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ocnlnIzcligJDqcJhFj3JzDjWhoTRGS/ORpTfednpdyX6MRJM7QuIDQ+erxHiNfoY9uaBf0z083yjckeMbgy9/UcdHT+8O9j5Yw7KJeKPuV7PvdRrepEViABl9w1glRd6cZvK86hEMI0sdLZEi8sCKsXszpgZfqzoxbB2SbRdM8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XLoET2gF; 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="XLoET2gF" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4AAA7C2BCC7; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=jSttoTQ+5tq94iBCfbqmVA02B8ZLP5x+PrcpR0wM01s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=XLoET2gF85tjKX7YS8zMgutusg5Tg3yf2pYufWrKXtRxX5gCFqfQWxeDdwhJaDT4M YoZg9u14HM2cSNYWONUgrJbdmVnGfXl+PbsZOgeR0yGKFk02aexs8MEsCSzWYZaCtc 6TNZbsSCXarA6WwCo9NUMDCysAhAacy8rmKxgPo5yKQej3e0Ygn4hOWy9OClm+4mcE n2LrYvv1h4z+Z0W5ZiwDKlHI0GJMaabCAWjmjMDZRBXRIoizG6TcL0T6HXcu3dXvIa 7HLCWpQsvVUEy9r8h6ps9rJXPDyA2NkEmzUVAcYIAQLq3zNe2xfXw3+qgWrpW77ACq 3bto8zGygTw0A== 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 396D9C44509; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:48 +0800 Subject: [PATCH RFC 10/13] mm/swap: remove available list 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: <20260714-swap-pcp-priq-v1-10-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=4228; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=56bxVOA8Kh8NG90pe0lrBG+qdvyKutM/P/TW2AoLLAg=; b=AB+E/zHQ9XaayHuwu1wLH65kfRaAKI0gjPf/1wBZpP4AycdYJe/1cG5AuMpFV+B3fK04NNpMc RIcqDcMfR3+CO/ZovoZXzpo7n87PKqoKoD6nEjhKAqBJhXBHvr5Dwvp 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 Pure cleanup, no functional change. After the priority queue replaced the allocator's device selection, swap_avail_head and swap_avail_lock are useless now. Remove them. __folio_throttle_swaprate() now iterates swap_active_head under swapon_rwsem instead, the SWP_WRITEOK filtering makes it equivalent to before. Signed-off-by: Kairui Song --- include/linux/swap.h | 1 - mm/swapfile.c | 30 ++++-------------------------- 2 files changed, 4 insertions(+), 27 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 6733a8979aa2..189c9898641b 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -293,7 +293,6 @@ struct swap_info_struct { struct work_struct discard_work; /* discard worker */ struct work_struct reclaim_work; /* reclaim worker */ struct list_head discard_clusters; /* discard clusters list */ - struct plist_node avail_list; /* entry in swap_avail_head */ }; =20 static inline swp_entry_t page_swap_entry(struct page *page) diff --git a/mm/swapfile.c b/mm/swapfile.c index f6dd85968928..01240c4c9db3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -96,21 +96,6 @@ static const char Bad_offset[] =3D "Bad swap offset entr= y "; */ static PLIST_HEAD(swap_active_head); =20 -/* - * all available (active, not full) swap_info_structs - * protected with swap_avail_lock, ordered by priority. - * This is used by folio_alloc_swap() instead of swap_active_head - * because swap_active_head includes all swap_info_structs, - * but folio_alloc_swap() doesn't need to look at full ones. - * This uses its own lock instead of swapon_rwsem because when a - * swap_info_struct changes between not-full/full, it needs to - * add/remove itself to/from this list, but the swap_info_struct->lock - * is held and the locking order requires swapon_rwsem to be taken - * before any swap_info_struct->lock. - */ -static PLIST_HEAD(swap_avail_head); -static DEFINE_SPINLOCK(swap_avail_lock); - static inline struct swap_info_struct *__swap_iter(int *i, unsigned long f= lag) { lockdep_assert_held(&swapon_rwsem); @@ -1547,7 +1532,6 @@ static void del_from_avail_list(struct swap_info_stru= ct *si, bool swapoff) { unsigned long pages; =20 - spin_lock(&swap_avail_lock); spin_lock(&swap_queue_update_lock); =20 /* @@ -1567,10 +1551,8 @@ static void del_from_avail_list(struct swap_info_str= uct *si, bool swapoff) } =20 swap_queue_mask(si); - plist_del(&si->avail_list, &swap_avail_head); skip: spin_unlock(&swap_queue_update_lock); - spin_unlock(&swap_avail_lock); } =20 /* SWAP_USAGE_OFFLIST_BIT can only be cleared by this helper. */ @@ -1579,7 +1561,6 @@ static void add_to_avail_list(struct swap_info_struct= *si) long val; unsigned long pages; =20 - spin_lock(&swap_avail_lock); spin_lock(&swap_queue_update_lock); =20 /* @@ -1610,7 +1591,6 @@ static void add_to_avail_list(struct swap_info_struct= *si) } =20 swap_queue_unmask(si); - plist_add(&si->avail_list, &swap_avail_head); skip: spin_unlock(&swap_queue_update_lock); } @@ -3622,7 +3602,6 @@ static struct swap_info_struct *alloc_swap_info(void) } p->swap_extent_root =3D RB_ROOT; plist_node_init(&p->list, 0); - plist_node_init(&p->avail_list, 0); p->flags =3D SWP_USED; percpu_up_write(&swapon_rwsem); if (defer) { @@ -4030,7 +4009,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) */ si->prio =3D prio; si->list.prio =3D -si->prio; - si->avail_list.prio =3D -si->prio; si->swap_file =3D swap_file; =20 /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */ @@ -4163,14 +4141,14 @@ void __folio_throttle_swaprate(struct folio *folio,= gfp_t gfp) if (current->throttle_disk) return; =20 - spin_lock(&swap_avail_lock); - plist_for_each_entry(si, &swap_avail_head, avail_list) { - if (si->bdev) { + percpu_down_read(&swapon_rwsem); + plist_for_each_entry(si, &swap_active_head, list) { + if ((si->flags & SWP_WRITEOK) && si->bdev) { blkcg_schedule_throttle(si->bdev->bd_disk, true); break; } } - spin_unlock(&swap_avail_lock); + percpu_up_read(&swapon_rwsem); } #endif =20 --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 7590A348452 for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=JvgOkiMviz+s0bcPy9tKk9mU74ks+rvqPUyCpVky9z+xHcx4eZGS2Fb9uhPdwq8xluYnmLt0eOiytWTZRpIXD0lnXhrrsaviAXXc0FLXf915tCHH3KmJo7emfk98S7gp1GhM+/IxeQjK/CTV/QY1PbYDk90wG1LsZflhMbX0jzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=WqTYjdOGaZHRxMbAxfndXOIkYayJUYR6EirK7bK+HPE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qBdU/rI6eySoyXdVfmu2Jc6NcubV9xHOO5yQ4BIh4TejGolKr2m48JLaT3fizdIpgX+oiQJCUprJiL3KrGt7gAMSf1nkCzqXPhbqiJoSkZSaxfqtT/GWSYGt1HO60aMzxdWA9po32CnWS3xzufSXXhpnjBIekyai9LmMn8dPyhQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ql3he0TL; 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="Ql3he0TL" Received: by smtp.kernel.org (Postfix) with ESMTPS id 5DEE3C2BCFB; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=WqTYjdOGaZHRxMbAxfndXOIkYayJUYR6EirK7bK+HPE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=Ql3he0TLF71rVGT72d5Bc6gLh5p5r7SmU8v51tPJFE5ZwUs+aegZAuCS0zr8sGqec Y6viC8qAqlFQpEm8rDouYlwtL/wncOpkyZkN3jIcfI28XjXiwDzDYlYG4nkzVMcHKT P88GfvuBTWlA16bqCNozTETWkR90qguTtO6NXpEZzEzRc4WqD62t4HHadR42RA4Ezl TTJDr9gEd+pHyF+FzI2HZw7nZgxYzKh9uXBVlu6rMZbDcVkPRsDEuHV2jM9IOKkbYl hIBaizNSKC3ysfMxtzmOVg0ngzzDCj66kYa8cSJOTZxPVunMN8LjjtmZDdqEg6DrNi 7fVzf/fackgMw== 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 4F2F7C44508; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:49 +0800 Subject: [PATCH RFC 11/13] mm/swap: perform sync discard on single device more proactively 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: <20260714-swap-pcp-priq-v1-11-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=5605; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=nhsTnwGNr5mf8R77bamTTsWF0IFsUzZ/c3ikG+tnQoI=; b=O1S81EtMQVty3RnxX+OFve6HrrAWeJdN1IMW1ByIaQf6Zz+TtBVwOtMwyT0JlzbaXElvd8Y2I VSIabMtf9i8Dxzgy7IhHWGiaIlO1q7fT91QAevkyoEcZf0doxKAvfNC 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 Previous commit 9fb749cd15078 ("mm, swap: do not perform synchronous discard during allocation") added a workaround-style global sync discard when all devices are drained to prevent OOM. It wasn't an optimal solution and in the discussion [1], it's preferred to discard more proactively, and in the scope of a single device instead of globally. That wasn't achievable due to the limitations of the previous swap allocation design, or would have ended up too complex. Now with the new workflow and swap cluster cache moved inside the device scope, we can easily achieve that. So drop the old workaround and implement a more proactive and simpler solution. Doing discard when the free list is drained will prevent fragmentation better and avoid a global discard scan. Link: https://lore.kernel.org/all/CAMgjq7CsYhEjvtN85XGkrONYAJxve7gG593TFeOG= V-oax++kWA@mail.gmail.com/ [1] Signed-off-by: Kairui Song --- mm/swapfile.c | 88 ++++++++++++++++++++++---------------------------------= ---- 1 file changed, 33 insertions(+), 55 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 01240c4c9db3..74a50c39da60 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -498,6 +498,26 @@ static long swap_usage_in_pages(struct swap_info_struc= t *si) return atomic_long_read(&si->inuse_pages) & SWAP_USAGE_COUNTER_MASK; } =20 +/* + * Serialize the allocation on single CPU or globally to avoid + * fragmentation and make the workflow easier to follow. + */ +static void swap_alloc_lock_device(struct swap_info_struct *si) +{ + if (si->flags & SWP_SOLIDSTATE) + local_lock(&si->percpu_cluster->lock); + else + spin_lock(&si->global_cluster->lock); +} + +static void swap_alloc_unlock_device(struct swap_info_struct *si) +{ + if (si->flags & SWP_SOLIDSTATE) + local_unlock(&si->percpu_cluster->lock); + else + spin_unlock(&si->global_cluster->lock); +} + /* Reclaim the swap entry anyway if possible */ #define TTRS_ANYWAY 0x1 /* @@ -884,10 +904,7 @@ swap_cluster_populate(struct swap_info_struct *si, * the potential recursive allocation is limited. */ spin_unlock(&ci->lock); - if (si->flags & SWP_SOLIDSTATE) - local_unlock(&si->percpu_cluster->lock); - else - spin_unlock(&si->global_cluster->lock); + swap_alloc_unlock_device(si); =20 ret =3D swap_cluster_alloc_table(ci, __GFP_HIGH | __GFP_NOMEMALLOC | GFP_KERNEL); @@ -900,10 +917,7 @@ swap_cluster_populate(struct swap_info_struct *si, * could happen with ignoring the percpu cluster is fragmentation, * which is acceptable since this fallback and race is rare. */ - if (si->flags & SWP_SOLIDSTATE) - local_lock(&si->percpu_cluster->lock); - else - spin_lock(&si->global_cluster->lock); + swap_alloc_lock_device(si); spin_lock(&ci->lock); =20 if (ret) { @@ -1439,15 +1453,12 @@ static unsigned long cluster_alloc_swap_entry(struc= t swap_info_struct *si, if (order && !(si->flags & SWP_BLKDEV)) return 0; =20 - if (si->flags & SWP_SOLIDSTATE) { - /* Fast path using per CPU cluster */ - local_lock(&si->percpu_cluster->lock); +restart: + swap_alloc_lock_device(si); + if (si->flags & SWP_SOLIDSTATE) offset =3D __this_cpu_read(si->percpu_cluster->next[order]); - } else { - /* Serialize HDD SWAP allocation for each device. */ - spin_lock(&si->global_cluster->lock); + else offset =3D si->global_cluster->next[order]; - } =20 if (offset !=3D SWAP_ENTRY_INVALID) { ci =3D swap_cluster_lock(si, offset); @@ -1471,6 +1482,12 @@ static unsigned long cluster_alloc_swap_entry(struct= swap_info_struct *si, found =3D alloc_swap_scan_list(si, &si->free_clusters, folio, false); if (found) goto done; + + if (!list_empty(&si->discard_clusters)) { + swap_alloc_unlock_device(si); + swap_do_scheduled_discard(si); + goto restart; + } } =20 if (order < PMD_ORDER) { @@ -1519,10 +1536,7 @@ static unsigned long cluster_alloc_swap_entry(struct= swap_info_struct *si, goto done; } done: - if (si->flags & SWP_SOLIDSTATE) - local_unlock(&si->percpu_cluster->lock); - else - spin_unlock(&si->global_cluster->lock); + swap_alloc_unlock_device(si); =20 return found; } @@ -1711,36 +1725,6 @@ static int swap_alloc_entry(struct folio *folio) return ret; } =20 -/* - * Discard pending clusters in a synchronized way when under high pressure. - * Return: true if any cluster is discarded. - */ -static bool swap_sync_discard(void) -{ - bool ret =3D false; - struct swap_info_struct *si, *next; - - percpu_down_read(&swapon_rwsem); -start_over: - plist_for_each_entry_safe(si, next, &swap_active_head, list) { - percpu_up_read(&swapon_rwsem); - if (get_swap_device_info(si)) { - if (si->flags & SWP_PAGE_DISCARD) - ret =3D swap_do_scheduled_discard(si); - put_swap_device(si); - } - if (ret) - return true; - - percpu_down_read(&swapon_rwsem); - if (plist_node_empty(&next->list)) - goto start_over; - } - percpu_up_read(&swapon_rwsem); - - return false; -} - static int swap_extend_table_alloc(struct swap_info_struct *si, struct swap_cluster_info *ci, unsigned int ci_off, gfp_t gfp) @@ -2045,14 +2029,8 @@ int folio_alloc_swap(struct folio *folio) } } =20 -again: ret =3D swap_alloc_entry(folio); =20 - if (!order && unlikely(!folio_test_swapcache(folio))) { - if (swap_sync_discard()) - goto again; - } - /* Need to call this even if allocation failed, for MEMCG_SWAP_FAIL. */ if (unlikely(mem_cgroup_try_charge_swap(folio))) swap_cache_del_folio(folio); --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 86D1D348C42 for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=TSxLvhbezwH3sePg0AL1CNJEeQ7QZc2iMiyc9PKVkla8GOhYc2lvrwJ+YMdvhBydTb5Ms3eRuCq0i2MX9prpMrP/HnigR7+HUTb8cuU7UB1Eyv/A2eQPKFfKUgFcRs4T1FOhyQTIER/zQnKzXGpnAfCvyEfAvX1DOZivotQV4Bc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=OS4JIaTeeAb4KSOvL78pQgFPmyNPwbHN2qLt75AcNeU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qzFi+eDwn9/QLuZf2b8TW92x7JMyxQrFMt+znve0aC05EwskGbhr7VpSPpZPDNlVkCbCDv1ySCXukBLaW5GUlq6d2ec5ZlFg0eppAZoLW/blhs3UBzGym9Pq+YyBaNv0+gbGy9Gxml4aqbIfxZiLNmq2c+nBwXR9LDP9AZcSKBk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SXUp7Eh2; 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="SXUp7Eh2" Received: by smtp.kernel.org (Postfix) with ESMTPS id 6D312C2BCFC; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=OS4JIaTeeAb4KSOvL78pQgFPmyNPwbHN2qLt75AcNeU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=SXUp7Eh2WqbTV2o/LHlwFpu+XmADivXhXSYAV+6jhTwpoJWyLZ6vp+r1aE2nAMaOL SmKDhBa6aTNAqZCKr+YKy60np4vzHJ5+ToNypJvdP2rkjsiSXsUwcQ1+2anD1jeeFJ LMJhwH38cX3SYorUdHJNHSy4QRuCQQbceylHTu2Na/C5kbJLr6HOEh2A4lfMMusCMW rmBnxtKpRM0rtf5ENUGrBF1SJ4qSs+LGsXxTw4H/oWbkxX/9gmsr+S3JXaXqhDwBXh Hc5RjSk/gINjb1lmozsHdu/M6TDOG3eUnACFvuTkhIdrBsX1MKYEWw6mlYNw+tZZXI 5uwVwN5vNyU1Q== 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 5E54FC44507; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:50 +0800 Subject: [PATCH RFC 12/13] mm/swap: drop swap active plist 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: <20260714-swap-pcp-priq-v1-12-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=4414; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=G45gA89umSL+kNs6bHyiaF1kIcN9pJ1ls62NQeOiYC4=; b=u/9trm152FgryD98mxUIQo6uu+x7EW9bzV/9nIVIRIh+hgrKj7kmLlR7Hv9I6jJ7gNWDavUCJ duRagp71EcEAIb40QpjiSyJQ5sj8Hf12sDBBp+XT3wQO0N93DXKzAqv 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 The swap_active_head plist tracked all writable swap devices, ordered by priority. Now that every consumer that needed priority ordering has moved to the priority queue, the remaining users only need to find any writable device. Remove swap_active_head, the associated plist_node from swap_info_struct, and migrate the remaining plist operations. Signed-off-by: Kairui Song --- include/linux/swap.h | 1 - mm/swapfile.c | 26 +++++--------------------- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 189c9898641b..04767540972b 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -267,7 +267,6 @@ struct swap_info_struct { struct percpu_ref users; /* indicate and keep swap device valid. */ unsigned long flags; /* SWP_USED etc: see above */ signed short prio; /* swap priority of this type */ - struct plist_node list; /* entry in swap_active_head */ signed char type; /* strange name for an index */ unsigned int max; /* size of this swap device */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 74a50c39da60..f93863309a5a 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -41,7 +41,6 @@ #include #include #include -#include =20 #include #include @@ -90,12 +89,6 @@ bool swap_migration_ad_supported; static const char Bad_file[] =3D "Bad swap file entry "; static const char Bad_offset[] =3D "Bad swap offset entry "; =20 -/* - * all active swap_info_structs - * protected with swapon_rwsem, and ordered by priority. - */ -static PLIST_HEAD(swap_active_head); - static inline struct swap_info_struct *__swap_iter(int *i, unsigned long f= lag) { lockdep_assert_held(&swapon_rwsem); @@ -3212,7 +3205,6 @@ static void swap_device_enable(struct swap_info_struc= t *si) spin_unlock(&swap_queue_update_lock); atomic_long_add(si->pages, &nr_swap_pages); total_swap_pages +=3D si->pages; - plist_add(&si->list, &swap_active_head); percpu_up_write(&swapon_rwsem); =20 add_to_avail_list(si); @@ -3244,7 +3236,6 @@ static int swap_device_disable(struct swap_info_struc= t *si) spin_lock(&swap_queue_update_lock); si->flags &=3D ~SWP_WRITEOK; spin_unlock(&swap_queue_update_lock); - plist_del(&si->list, &swap_active_head); total_swap_pages -=3D si->pages; atomic_long_sub(si->pages, &nr_swap_pages); del_from_avail_list(si, true); @@ -3306,9 +3297,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) =20 mapping =3D victim->f_mapping; percpu_down_read(&swapon_rwsem); - plist_for_each_entry(p, &swap_active_head, list) { - if (p->flags & SWP_WRITEOK && - p->swap_file->f_mapping =3D=3D mapping) { + for_each_avail_swap(p) { + if (p->swap_file->f_mapping =3D=3D mapping) { found =3D 1; break; } @@ -3579,7 +3569,6 @@ static struct swap_info_struct *alloc_swap_info(void) */ } p->swap_extent_root =3D RB_ROOT; - plist_node_init(&p->list, 0); p->flags =3D SWP_USED; percpu_up_write(&swapon_rwsem); if (defer) { @@ -3981,12 +3970,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) if (swap_flags & SWAP_FLAG_PREFER) prio =3D swap_flags & SWAP_FLAG_PRIO_MASK; =20 - /* - * The plist prio is negated because plist ordering is - * low-to-high, while swap ordering is high-to-low - */ si->prio =3D prio; - si->list.prio =3D -si->prio; si->swap_file =3D swap_file; =20 /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */ @@ -4096,7 +4080,7 @@ int swap_dup_entry_direct(swp_entry_t entry) #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP) static bool __has_usable_swap(void) { - return !plist_head_empty(&swap_active_head); + return READ_ONCE(total_swap_pages) > 0; } =20 void __folio_throttle_swaprate(struct folio *folio, gfp_t gfp) @@ -4120,8 +4104,8 @@ void __folio_throttle_swaprate(struct folio *folio, g= fp_t gfp) return; =20 percpu_down_read(&swapon_rwsem); - plist_for_each_entry(si, &swap_active_head, list) { - if ((si->flags & SWP_WRITEOK) && si->bdev) { + for_each_avail_swap(si) { + if (si->bdev) { blkcg_schedule_throttle(si->bdev->bd_disk, true); break; } --=20 2.55.0 From nobody Sat Jul 25 20:47:29 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 9A39E348C4B for ; Mon, 13 Jul 2026 17:26:55 +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=1783963615; cv=none; b=nl1tVbNyrp2LupWD5XdGOq16UYnGH6I4AxDKXkZ1NcAvw5vIeHFCyIBtA/qzOAB9+B4Kn+s0TwLj7yJ0Ap6eNNIc1wsostke1b6Gt3jOHPSHrBxrADi/DVm9Uh8D+TtLmfdT7GSkIuzGe0SrROJqDcpjjF80exhLzBOU7XcjPII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783963615; c=relaxed/simple; bh=imwRiWt5m5R7pwZ7Gp6Llq5cU3KdGzhsaV9q2p7GNmY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=aeMqHs2hofhaXZKj9JodOfXQxx84mIarq+neFY88Pz9LvEDJe6U0Mkh0YE5Q1rAe4SDozQWf2JtJZBT5tXb6IA6KEcrrz33Dan+r6+loydW+oyPYONtDcWySAWaFMW2q0tIpCrhLW55J2+66dGHJpR65zseJg1yNcu4qZX7ZkDo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tEkHnUhI; 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="tEkHnUhI" Received: by smtp.kernel.org (Postfix) with ESMTPS id 81C59C4AF09; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783963615; bh=imwRiWt5m5R7pwZ7Gp6Llq5cU3KdGzhsaV9q2p7GNmY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=tEkHnUhIbvAqRe8IC3N3VKzN3NZYa2UnvhLXYC3QLJFr2oweNy2GRbtYjh7sEgmGN iNIgVdfj7QqPxUe3A5Ey1/dMGG15pfyY3ILduLKLok0TeNQMGC/GA/Eux45JgJGTs/ rXVPjSgWX3LV5uTGob4tjnHpm3oZeZ7HfumB5yqoGXLpZ2TBAIw0QL9CmvTjhIUcvW JDeL64k1R4I5xRuT+Ao48y1d4See2fzP3fGE04d4H6XU5A9LrpUofpeSIDQTr+Hwv4 YsfhESe1gYKr61/+OnITZ8fMyMvP7BETaIPOUsmlE6XfDgFdl5r+F8uYfwmNwM3RI6 FS24jW/dDTgsw== 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 6F438C44509; Mon, 13 Jul 2026 17:26:55 +0000 (UTC) From: Kairui Song via B4 Relay Date: Tue, 14 Jul 2026 01:25:51 +0800 Subject: [PATCH RFC 13/13] lib/plist.c: remove requeue function 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: <20260714-swap-pcp-priq-v1-13-de9b164ed419@tencent.com> References: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> In-Reply-To: <20260714-swap-pcp-priq-v1-0-de9b164ed419@tencent.com> To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Chris Li , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Kemeng Shi , Kairui Song X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1783963612; l=3268; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=NQdvrWdzsSXKqo+q0HqE4Pla3VWVnV+ggTUFkX8wpiM=; b=Q6n5nBBbacEZZaL44/dX2mVEo+/4JgkdSEvicdYtCsgzhi3cAz+ZlaWpQtluSBlt4/xYyJppn kYO1kKTDiQ1DVKThhAovUCKTD+EMCA5mtDvuvk8OKV05/Bre0E5ub07 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 Now the last user of plist requeue is gone, this function can be removed. Signed-off-by: Kairui Song --- include/linux/plist.h | 2 -- lib/plist.c | 64 -----------------------------------------------= ---- 2 files changed, 66 deletions(-) diff --git a/include/linux/plist.h b/include/linux/plist.h index 16cf4355b5c1..73d359c1a053 100644 --- a/include/linux/plist.h +++ b/include/linux/plist.h @@ -132,8 +132,6 @@ static inline void plist_node_init(struct plist_node *n= ode, int prio) extern void plist_add(struct plist_node *node, struct plist_head *head); extern void plist_del(struct plist_node *node, struct plist_head *head); =20 -extern void plist_requeue(struct plist_node *node, struct plist_head *head= ); - /** * plist_for_each - iterate over the plist * @pos: the type * to use as a loop counter diff --git a/lib/plist.c b/lib/plist.c index a5bef38add43..b05ae7ffea87 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -142,58 +142,6 @@ void plist_del(struct plist_node *node, struct plist_h= ead *head) plist_check_head(head); } =20 -/** - * plist_requeue - Requeue @node at end of same-prio entries. - * - * This is essentially an optimized plist_del() followed by - * plist_add(). It moves an entry already in the plist to - * after any other same-priority entries. - * - * @node: &struct plist_node pointer - entry to be moved - * @head: &struct plist_head pointer - list head - */ -void plist_requeue(struct plist_node *node, struct plist_head *head) -{ - struct plist_node *iter; - struct list_head *node_next =3D &head->node_list; - - plist_check_head(head); - BUG_ON(plist_head_empty(head)); - BUG_ON(plist_node_empty(node)); - - if (node =3D=3D plist_last(head)) - return; - - iter =3D plist_next(node); - - if (node->prio !=3D iter->prio) - return; - - plist_del(node, head); - - /* - * After plist_del(), iter is the replacement of the node. If the node - * was on prio_list, take shortcut to find node_next instead of looping. - */ - if (!list_empty(&iter->prio_list)) { - iter =3D list_entry(iter->prio_list.next, struct plist_node, - prio_list); - node_next =3D &iter->node_list; - goto queue; - } - - plist_for_each_continue(iter, head) { - if (node->prio !=3D iter->prio) { - node_next =3D &iter->node_list; - break; - } - } -queue: - list_add_tail(&node->node_list, node_next); - - plist_check_head(head); -} - #ifdef CONFIG_DEBUG_PLIST #include #include @@ -231,14 +179,6 @@ static void __init plist_test_check(int nr_expect) BUG_ON(prio_pos->prio_list.next !=3D &first->prio_list); } =20 -static void __init plist_test_requeue(struct plist_node *node) -{ - plist_requeue(node, &test_head); - - if (node !=3D plist_last(&test_head)) - BUG_ON(node->prio =3D=3D plist_next(node)->prio); -} - static int __init plist_test(void) { int nr_expect =3D 0, i, loop; @@ -262,10 +202,6 @@ static int __init plist_test(void) nr_expect--; } plist_test_check(nr_expect); - if (!plist_node_empty(test_node + i)) { - plist_test_requeue(test_node + i); - plist_test_check(nr_expect); - } } =20 for (i =3D 0; i < ARRAY_SIZE(test_node); i++) { --=20 2.55.0