From nobody Mon Feb 9 23:42:38 2026 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DD14346E78 for ; Wed, 28 Jan 2026 09:30:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592637; cv=none; b=n19PG5xx2DukP3dLXh62kippqkz12cKy8XRmigdU8iTMweGk0V1ptO0g8NXiXSlWIRCi00XmEPNjiCcUnuXleKboxj2HFf+YLdCw3gzOo7Qo9VdhBSa1UhYn0ghAMNoHLVrrAmPOko65pdeL0HqJP6hmSYVvZvhecuYfK67pODk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592637; c=relaxed/simple; bh=VG8IU6huOx+mOLrinUPxSTM5nUnH0djp/f3W9DrUDAM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=u/B84nCAAkrS8eR2nDBDfhOjxmhC/o/rHBVnmpa4FGJfmsr4ZbjKp5TchjI4e4O7zV0bi/p+gHvFff6/F6kPpRBuF3sub6F2b/GyYTC3+qicXxgJMISc9MTi3uGkNOlBGhqLQ2LkBEN28EM+2r+Um8WUd8bl3rDfHGff0+MpHzI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VGQbS9Bt; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VGQbS9Bt" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-352e3d18fa7so4595971a91.1 for ; Wed, 28 Jan 2026 01:30:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769592635; x=1770197435; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=JXkKpfW2fKRP2DHBX2nOhkvpUQvYM7wlOqYMZBE7moY=; b=VGQbS9BtDj05s7YEhp3ZYTaqFCe+scsFjQcrzujHw0eQs7crgm7NNTh3jGBuOZhgrl iien81oscYoBeOOJKBgeD5KT2ef5GiwgFeBGJjXe5zh96em4cuagEvA0dujTBl5YuWC4 haHi8aF8fps45Kg/2RT4dIhK3SY0Yg0LUFggaGglUPipTs9RZyq+2qumxPGmjT1FLtDb 3HSKUNqg5MeGQYvLTe/mn4wghrzHcJQvMFTjKlpohlOx+3Uv6kmEQtTyt+QfV6pUOcBj tFmxiHWdFW/2H5r2O/qyfXfh8gAMiXN7BwyxoiXF8jGxEQqRJahA3eJjFcw/d3piRKL1 iSMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769592635; x=1770197435; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=JXkKpfW2fKRP2DHBX2nOhkvpUQvYM7wlOqYMZBE7moY=; b=OG9X46el6/hQs5EMQMKIvEjGDTuwWbKKW8/1clC5DXRJGTJi3OR3/OvGTAGL8VxxVo JLlwuz6xf+s5LBqVXbsd6RKhtTXy2zh4dAJDaFI6c20YoEvc7NrqTNPhnP4Yq2IcVqJ5 hnAmPE6m29vw6fAGhhHiFTNx/VaFyaDouARB0yBK4bjGoIL3Mj4YRIOfxN4UTvXeHBvP +LKMmjwCR1Z+dlf/dSKds7lD6sJ7wNVoKVr0nWDbGz++4SwcQ4B33O3JCuzlDDhVegDG aCnnJE0U+SZFpumdj4xfT0g8rgHHyvdhKfAkfGtZ3sbvQ/yOmQ8L0kHmrisigYxES/wK +TSA== X-Forwarded-Encrypted: i=1; AJvYcCVJmj7SUR756iESMNIa3i+0bOd/0yTSFgGbY12I1grwdkV48CKs421EZD1FheggtF6rEb7HHRvwK0LSnek=@vger.kernel.org X-Gm-Message-State: AOJu0Yw4RCstmSZAY2b2bEEWVxZX5qhbVQDx6gzVVCXfNh3GT+J+s6fv Hcp1M6oOGSCzljFDc485VEeKTjV591T2pv+7SjE5mtlSg3jEQU5yiRD5 X-Gm-Gg: AZuq6aIFRYsgOyYBzOOGgpdHKZGSA2FeaZ6EIus9w/zW6sn32Mw/elEvzDDbII/FdMR ZspSpAUPtukllAya0qLc6T8qjN9U8zeUawg6sjkgz+3KpQE/YtafFP6lDrYDZE/kCFsO1VmPwP4 ER8eoNQKmLO3bZK/tLHOOcA3JgbRrGek9f0DE7SKVxncLuhJpngkUT44jMqUGGR9SklpxStWyoN ChJ1GL2b695m26LfnfeJFeH0wBS2PaR1kVEtHK6G4B1ToS5yDtx40y+GScSFw+2KUHgLhYN6swP 9DLuYnY7/56EKotRMuU2AGs5TPNB0TUtv2b4V9ICNoEekyr51EEsiwm/BLzILVwMz8uZCK7+l7H x2l3hnX8vdR2IrVkvh8hm32JBfijyVra0YUwif4JvvzeKfoU361p8H9QW8UXQdU2JO3aiONQePz dcdr9SCTuzuQIUrcjXTXalViv6v4Dv/UF6aTD6SKfx+YTtJT+TjCt4xIrDh7Cq40g4VxZj X-Received: by 2002:a17:90a:c2cb:b0:34c:4c6d:ad0f with SMTP id 98e67ed59e1d1-353fedb04e4mr4505185a91.37.1769592635182; Wed, 28 Jan 2026 01:30:35 -0800 (PST) Received: from [127.0.0.1] ([43.132.141.21]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3540f3eca6dsm1872235a91.15.2026.01.28.01.30.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 01:30:34 -0800 (PST) From: Kairui Song Date: Wed, 28 Jan 2026 17:28:25 +0800 Subject: [PATCH v2 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: <20260128-swap-table-p3-v2-1-fe0b67ef0215@tencent.com> References: <20260128-swap-table-p3-v2-0-fe0b67ef0215@tencent.com> In-Reply-To: <20260128-swap-table-p3-v2-0-fe0b67ef0215@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Johannes Weiner , David Hildenbrand , Lorenzo Stoakes , Youngjun Park , linux-kernel@vger.kernel.org, Chris Li , Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1769592628; l=5423; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=KCJ3euqA2qAaRKKZ+5BOjIB6OnqWy66OxHgNUHCngaI=; b=yUDffmpYnq0E0QTlzeFqQIlUHmu6snQJuTNpipCSscvVCs/a0w84zEQXy7hojSBMeXeMrbeiT uuD5ZksxBlFCx3xtlmgpeUcAp/e/Q7vCV0UdfEg0XIrukAPUSaAbqkI X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= 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 --- mm/swapfile.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 7b055f15d705..521f7713a7c3 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); @@ -2521,7 +2522,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; @@ -2532,7 +2534,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; @@ -2615,9 +2616,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; @@ -2635,7 +2636,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; @@ -2851,7 +2852,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 @@ -2941,7 +2942,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; @@ -2962,7 +2963,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; } @@ -3379,7 +3380,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; @@ -3429,7 +3429,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; @@ -3538,6 +3538,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", @@ -3562,10 +3564,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