From nobody Fri Apr 3 01:24: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 1417C378D7B for ; Tue, 17 Feb 2026 20:06:50 +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=1771358810; cv=none; b=uePl7768gIpUuVUZtdThbn1GQcaL40b0B1K+eoY3xiqiNTwzZ4vKeng7awwzbfy0cnTQefXqvimgkwfNsS8A1d9HQHLKsLH+Wx77hluHeOwOex7eyrspmDkRC6PCmyPtWD5XRLdVxo2eoZ36wMth1ucI5ZW0uBk+Wsy/VuTknfA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771358810; c=relaxed/simple; bh=lkvo+r1Vv+yQtg/HXKc6w9CnaZkujAHTcZpC1b096Ck=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T4SDH34vwjI7ssGr91UjAzbNYB31ypQH40m45a2Nyfimo4JWDfZ5xoKuER4gdlxNQ1Zx4Wo8C0ngCwP3OfoP97qeM6s8XL2j6+14xCWX+IxvAvPPkriXDC/taFG1sJGlxhhKzn5633PPcqtcWrAtWqEtzT4XVKx2lNwch/Y4Jbw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uf+6IuFp; 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="uf+6IuFp" Received: by smtp.kernel.org (Postfix) with ESMTPS id D58BDC19421; 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=lkvo+r1Vv+yQtg/HXKc6w9CnaZkujAHTcZpC1b096Ck=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=uf+6IuFpHtJne94zrrs2xlJWs3MSS3E/gZ4EdCiQEqtvleG8W0EmB+L31ASBC1YIQ A7E/dpoQprqvm10lCSpXYMaaW30OEFfWk6eg1VUTyjvMdLVQ/JOudC4Rd3rWasIFrb IuWUm+BtkuH2MhCQocLtqb1iKzbg1bcZK4DiG0lKQMpfgGn3SlUiMa7bm+j0iJCY+Q J64VFOc2RBjMUnEgekJUCoyLZZBqxM/Y8t4gGT5GehAAPrFCdDmUM9yeX9mvCZqC4V 3zceMK6Br7PqU45q7vfobL4afDDI36/oL6RN/OZI8qTuvLsGqmUhUmlkxICDa/hhux IMRK/yaEZp8hQ== 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 C7519E9A03E; Tue, 17 Feb 2026 20:06:48 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 18 Feb 2026 04:06:26 +0800 Subject: [PATCH v3 01/12] mm, swap: protect si->swap_file properly and use as a mount indicator 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-1-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=5423; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=OErUm6YzDXG8MUnIOgrfhhtTdULgZJzpg5xc2rcjJG4=; b=KefXHGVHQAdLFCFWu9nqJfG3nJH7rws2nW9hIB7xb9Nm1CovW7LHFC1ilWbZagYgRGOgXBJ9F i0WZsakiuEACJSPGsYv8bIL3j90aQTgfDsJQPU/voTO/wk/Fe3jfE8U 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 /proc/swaps uses si->swap_map as the indicator to check if the swap device is mounted. swap_map will be removed soon, so change it to use si->swap_file instead because: - si->swap_file is exactly the only dynamic content that /proc/swaps is interested in. Previously, it was checking si->swap_map just to ensure si->swap_file is available. si->swap_map is set under mutex protection, and after si->swap_file is set, so having si->swap_map set guarantees si->swap_file is set. - Checking si->flags doesn't work here. SWP_WRITEOK is cleared during swapoff, but /proc/swaps is supposed to show the device under swapoff too to report the swapoff progress. And SWP_USED is set even if the device hasn't been properly set up. We can have another flag, but the easier way is to just check si->swap_file directly. So protect si->swap_file setting with mutext, and set si->swap_file only when the swap device is truly enabled. /proc/swaps only interested in si->swap_file and a few static data reading. Only si->swap_file needs protection. Reading other static fields is always fine. Signed-off-by: Kairui Song Acked-by: Chris Li --- mm/swapfile.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 32e0e7545ab8..25dfe992538d 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -110,6 +110,7 @@ struct swap_info_struct *swap_info[MAX_SWAPFILES]; =20 static struct kmem_cache *swap_table_cachep; =20 +/* Protects si->swap_file for /proc/swaps usage */ static DEFINE_MUTEX(swapon_mutex); =20 static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); @@ -2532,7 +2533,8 @@ static void drain_mmlist(void) /* * Free all of a swapdev's extent information */ -static void destroy_swap_extents(struct swap_info_struct *sis) +static void destroy_swap_extents(struct swap_info_struct *sis, + struct file *swap_file) { while (!RB_EMPTY_ROOT(&sis->swap_extent_root)) { struct rb_node *rb =3D sis->swap_extent_root.rb_node; @@ -2543,7 +2545,6 @@ static void destroy_swap_extents(struct swap_info_str= uct *sis) } =20 if (sis->flags & SWP_ACTIVATED) { - struct file *swap_file =3D sis->swap_file; struct address_space *mapping =3D swap_file->f_mapping; =20 sis->flags &=3D ~SWP_ACTIVATED; @@ -2626,9 +2627,9 @@ EXPORT_SYMBOL_GPL(add_swap_extent); * Typically it is in the 1-4 megabyte range. So we can have hundreds of * extents in the rbtree. - akpm. */ -static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) +static int setup_swap_extents(struct swap_info_struct *sis, + struct file *swap_file, sector_t *span) { - struct file *swap_file =3D sis->swap_file; struct address_space *mapping =3D swap_file->f_mapping; struct inode *inode =3D mapping->host; int ret; @@ -2646,7 +2647,7 @@ static int setup_swap_extents(struct swap_info_struct= *sis, sector_t *span) sis->flags |=3D SWP_ACTIVATED; if ((sis->flags & SWP_FS_OPS) && sio_pool_init() !=3D 0) { - destroy_swap_extents(sis); + destroy_swap_extents(sis, swap_file); return -ENOMEM; } return ret; @@ -2862,7 +2863,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) flush_work(&p->reclaim_work); flush_percpu_swap_cluster(p); =20 - destroy_swap_extents(p); + destroy_swap_extents(p, p->swap_file); if (p->flags & SWP_CONTINUED) free_swap_count_continuations(p); =20 @@ -2952,7 +2953,7 @@ static void *swap_start(struct seq_file *swap, loff_t= *pos) return SEQ_START_TOKEN; =20 for (type =3D 0; (si =3D swap_type_to_info(type)); type++) { - if (!(si->flags & SWP_USED) || !si->swap_map) + if (!(si->swap_file)) continue; if (!--l) return si; @@ -2973,7 +2974,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->flags & SWP_USED) || !si->swap_map) + if (!(si->swap_file)) continue; return si; } @@ -3390,7 +3391,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) goto bad_swap; } =20 - si->swap_file =3D swap_file; mapping =3D swap_file->f_mapping; dentry =3D swap_file->f_path.dentry; inode =3D mapping->host; @@ -3440,7 +3440,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) =20 si->max =3D maxpages; si->pages =3D maxpages - 1; - nr_extents =3D setup_swap_extents(si, &span); + nr_extents =3D setup_swap_extents(si, swap_file, &span); if (nr_extents < 0) { error =3D nr_extents; goto bad_swap_unlock_inode; @@ -3549,6 +3549,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) prio =3D DEF_SWAP_PRIO; if (swap_flags & SWAP_FLAG_PREFER) prio =3D swap_flags & SWAP_FLAG_PRIO_MASK; + + si->swap_file =3D swap_file; enable_swap_info(si, prio, swap_map, cluster_info, zeromap); =20 pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s= %s%s\n", @@ -3573,10 +3575,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) kfree(si->global_cluster); si->global_cluster =3D NULL; inode =3D NULL; - destroy_swap_extents(si); + destroy_swap_extents(si, swap_file); swap_cgroup_swapoff(si->type); spin_lock(&swap_lock); - si->swap_file =3D NULL; si->flags =3D 0; spin_unlock(&swap_lock); vfree(swap_map); --=20 2.52.0