From nobody Fri Apr 3 01:24:31 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 341D736BCEB for ; Tue, 17 Feb 2026 20:06:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771358809; cv=none; b=kuuNThGx9PKGyl/dmUjxLxxvsfFoX1EgDdq9GclXJTOlaER4D2b659VdQ69HCS1r+4PP7pUqQwBXiD78WdpMOAkWsfoeuBLijQYBPuiNEIySNDAm9v3g2Ppi+mZaeyovwOYZZhLfQFWY4x5XzO3RyRtqGLbHmRVaSNdtZNFWAbw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771358809; c=relaxed/simple; bh=KjN4Ur2B7QTWJCpQhJuDPDSjA7dfJDSlU7kJLSeey80=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=DUgoOFhmz+8KvJ3Q/2YcraDTURXON/AKMrbAcuaPlWrETWyBGRP1rLX3GZfSHhv1IJhk9yzjFCtfsg2+Zw6vDHkt6ZgCJa/woc3yWLEZzreBGD7mpwUfH6qlWWuccCayoAn8VOOMxBK8JLmcfXzlOdCouS4bWxxdKlj6QaTLCvE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mnL4P1w7; 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="mnL4P1w7" Received: by smtp.kernel.org (Postfix) with ESMTPS id 027EAC2BC87; Tue, 17 Feb 2026 20:06:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1771358809; bh=KjN4Ur2B7QTWJCpQhJuDPDSjA7dfJDSlU7kJLSeey80=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=mnL4P1w7MZpA8O74DWDaAAKa0gX8kAoHB+S7sz2flKiBEXrq4Vv1iIGCaxttPdgGd 6SSfrcvgMQnPhfp06g9kvxxnbYybhB4uu7Gg2paaF5JZSAeq/dUi1mp4zMh8JLsdJi dMPfPpTeF7+WTtkvM0zhKrWSi6ecZxguYQphzhqxiWjWiMBKF77yxkazzZxQw0ZzXF EgiHAo3rt+YKpuiuRaWsTr7DzT6rinaEK0zuuCbWqckgqaObwOA6rbVw5rCzNsWZ71 6MaktsNGj83Iy1/pCAt1hx5tOmspKJDWfZNspzVqifTDolzBjdJy5wgKUA9mabcwFI RUa2rxtscXnVg== 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 EB4C4E9A044; Tue, 17 Feb 2026 20:06:48 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 18 Feb 2026 04:06:28 +0800 Subject: [PATCH v3 03/12] mm, swap: remove redundant arguments and locking for enabling a device Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260218-swap-table-p3-v3-3-f4e34be021a7@tencent.com> References: <20260218-swap-table-p3-v3-0-f4e34be021a7@tencent.com> In-Reply-To: <20260218-swap-table-p3-v3-0-f4e34be021a7@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Johannes Weiner , David Hildenbrand , Lorenzo Stoakes , Youngjun Park , linux-kernel@vger.kernel.org, Chris Li , Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1771358806; l=4474; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=7v/yOoUm+JKrDy/cUABunBQ64SLCuHCZ71QcM/9ivnk=; b=pe7JYHGn/ycRyxgjvXfgwIlVdmMo/7yYX6nZc5R0wASNkiT41lF4CipymFe7E/R78JWzygbk2 rfrQGO4XYH1CvtT5mQhICq1K9z8VGrFEWknTZcO7tflKC4tAPK7m94m 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 is no need to repeatedly pass zero map and priority values. zeromap is similar to cluster info and swap_map, which are only used once the swap device is exposed. And the prio values are currently read only once set, and only used for the list insertion upon expose or swap info display. Signed-off-by: Kairui Song Acked-by: Chris Li --- mm/swapfile.c | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 8fc35b316ade..fb0d48681c48 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2663,19 +2663,6 @@ static int setup_swap_extents(struct swap_info_struc= t *sis, return generic_swapfile_activate(sis, swap_file, span); } =20 -static void setup_swap_info(struct swap_info_struct *si, int prio, - unsigned long *zeromap) -{ - si->prio =3D prio; - /* - * the plist prio is negated because plist ordering is - * low-to-high, while swap ordering is high-to-low - */ - si->list.prio =3D -si->prio; - si->avail_list.prio =3D -si->prio; - si->zeromap =3D zeromap; -} - static void _enable_swap_info(struct swap_info_struct *si) { atomic_long_add(si->pages, &nr_swap_pages); @@ -2689,17 +2676,12 @@ static void _enable_swap_info(struct swap_info_stru= ct *si) add_to_avail_list(si, true); } =20 -static void enable_swap_info(struct swap_info_struct *si, int prio, - unsigned long *zeromap) +/* + * 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. + */ +static void enable_swap_info(struct swap_info_struct *si) { - spin_lock(&swap_lock); - spin_lock(&si->lock); - setup_swap_info(si, prio, zeromap); - spin_unlock(&si->lock); - spin_unlock(&swap_lock); - /* - * Finished initializing swap device, now it's safe to reference it. - */ percpu_ref_resurrect(&si->users); spin_lock(&swap_lock); spin_lock(&si->lock); @@ -2712,7 +2694,6 @@ static void reinsert_swap_info(struct swap_info_struc= t *si) { spin_lock(&swap_lock); spin_lock(&si->lock); - setup_swap_info(si, si->prio, si->zeromap); _enable_swap_info(si); spin_unlock(&si->lock); spin_unlock(&swap_lock); @@ -3365,7 +3346,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) int nr_extents; sector_t span; unsigned long maxpages; - unsigned long *zeromap =3D NULL; struct folio *folio =3D NULL; struct inode *inode =3D NULL; bool inced_nr_rotate_swap =3D false; @@ -3481,9 +3461,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) * Use kvmalloc_array instead of bitmap_zalloc as the allocation order mi= ght * be above MAX_PAGE_ORDER incase of a large swap file. */ - zeromap =3D kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), - GFP_KERNEL | __GFP_ZERO); - if (!zeromap) { + si->zeromap =3D kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + GFP_KERNEL | __GFP_ZERO); + if (!si->zeromap) { error =3D -ENOMEM; goto bad_swap_unlock_inode; } @@ -3552,10 +3532,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specia= lfile, 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->avail_list.prio =3D -si->prio; si->swap_file =3D swap_file; =20 /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */ - enable_swap_info(si, prio, zeromap); + enable_swap_info(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, @@ -3585,6 +3572,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) si->swap_map =3D NULL; free_swap_cluster_info(si->cluster_info, si->max); si->cluster_info =3D NULL; + kvfree(si->zeromap); + si->zeromap =3D NULL; /* * Clear the SWP_USED flag after all resources are freed so * alloc_swap_info can reuse this si safely. @@ -3592,7 +3581,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) spin_lock(&swap_lock); si->flags =3D 0; spin_unlock(&swap_lock); - kvfree(zeromap); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); if (swap_file) --=20 2.52.0