From nobody Mon Feb 9 11:30:07 2026 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 07DEB2EF660 for ; Sun, 25 Jan 2026 17:58:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769363887; cv=none; b=jIx30F1K0jCNHbVZRETx4aZA6JBcubT6U3YtJ29ziN2pyMxCgwDeX2ppkBLwgepidN5Fp7XA6y9QSJCVoY05IbzVzJILMRvZtNO9f4Qie8PX8lrPamyq2JpJ96RTAGUivu4roRGMifzKIoX1HsNyToXb8C6NqRlg+A8ew8RTts4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769363887; c=relaxed/simple; bh=VG8IU6huOx+mOLrinUPxSTM5nUnH0djp/f3W9DrUDAM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GJ5QEYun8g4ZuK86UYn30OTOHmgQ6VVTLzj1JDQ3q3S7xMWL8Ed5y/xVvoRsBdhVcwOxHXLI88YpI1PrhnMtn062U/8zL5XyOKinWpiEvlQEWVfWsvPSafnunXIJVSh3fA+jc3ovpzmAM6ZHcE/ZENuuYx/W7LZpXMZ5gobs2Uw= 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=e2JJ6S9B; arc=none smtp.client-ip=209.85.210.173 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="e2JJ6S9B" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-8217f2ad01eso3335603b3a.2 for ; Sun, 25 Jan 2026 09:58:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769363885; x=1769968685; 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=e2JJ6S9B253YhpYR8KMt3yAUAxZFHkMrqUCHl7rstYPRuGhuj1r3JwWw2+EryGXaqD TbVvdl5qZPimWH1wA3J1LImuBkrRGFJN085TC5KIzNFz9xgnqt0ic7a7aOA+V4d6UzIH xaUGP5dWPjcoQztt7c3GSjigvwcgyi0/+SNVN4GIY+r7ixyUTqXKq8uje8UhKKk57xcc BZWk3l6pK+/nG0GH5OTdKKuzhXUYqwafcnqI6APky/2yZB6IYg4megKP5FmTSBhUKvaK WADCx2NKt5OU22WK7h/JkdA9d6B4QFf8gKmizEBsKCpBGZHyLvwhY3mXSR4zYz1JcVmy a99A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769363885; x=1769968685; 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=vjk/V92O1yLsuA9MKOkvGjYrKiEgtlYmy1ZNjo8Wmx15gob4+6EM4K5wWnrLIKBTMH RqiY76GbOTvZ9O/cSDDXm6BD8FnINXBp7iXn2kRCo0gvmhGeBu/1l65YJ2eBy0mGr9ZK 8NChqGJaJCm8vjUaPowl4DOhXjcwVTo6laoEDnOHDsFbv4NHdDFLgRssnlqShlzXznqA 1QT4tjYZtqoqi8JtDl0TDobX+7RvSgAOYXAZ9r5OjUhaVP5/Cip4sf/C0O5z1d0rjgqf 37qVuoKFQ45L6lymXyhTAc5er5NSjGdEu19qSpVYZcwv1YZbTVO7M3iuBeqRQ2VUzfU3 Ba2g== X-Forwarded-Encrypted: i=1; AJvYcCVpONg2ACFVjxqt0zUdGnPL+IM+74VckshbPrnFNnArqpL0/4WY83UXu11X67q+8r8JJJfTT5Cqq5Hb250=@vger.kernel.org X-Gm-Message-State: AOJu0Yxidwr6ISOIjVkT85atrrM6KJh8tW5ChepYJrvD8lKl9oqHPqFZ VY7gNHDKiQL+Zz6TsjOp0/A3HL9nYhIGnbZVoxQJu+tMe78/ajrpDCe5 X-Gm-Gg: AZuq6aIP//02IKL+WqkdNeanqcoMohU/jXeym/KULg7mpv0YNm2k8uVIIxQhnb5OogQ QlPhYGcJfLVrGVNWcD1mnOaHtQwsOJFZNWRGvkLFk3kJwxzQEEfzscbaKSPuiU26lVLdBO/TQD0 qedT80a+2f9LV03oa7Y7/FUf/Tqv90VHIWXZPvvRFIyUZSk1ZZfZoL+4SD9SwMv9oevl/84QXRF k6s3AC2PMCXCiXJC6JF9uTBhLSWzWLKeWVKtR4fD3GcmV7uRJdEQsiUUoLHkg5FxpTjI1UQ4/wq LzRutcmdXBwFrn8AxaINYi3h9qYqwIEduBfVXNfbfouarTlru8ls6c+JIRCD9JY1pHlYN+eR+85 E43vcCbCiHK5mrLcJUn0M6uNzfQjaYHqacaCm76MD33eEvXoPTU4gEql/7htItOyrMZV4hRMoTW 4HZAAAJgn2Da/jsv6cI0ast5d7HwiZx54nSiBdmidY2N53N1P5 X-Received: by 2002:a05:6a00:761b:b0:81f:4f74:2246 with SMTP id d2e1a72fcca58-823411fa543mr1939435b3a.16.1769363885303; Sun, 25 Jan 2026 09:58:05 -0800 (PST) Received: from [127.0.0.1] ([101.32.222.185]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8231876e718sm7405963b3a.62.2026.01.25.09.58.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jan 2026 09:58:04 -0800 (PST) From: Kairui Song Date: Mon, 26 Jan 2026 01:57:24 +0800 Subject: [PATCH 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: <20260126-swap-table-p3-v1-1-a74155fab9b0@tencent.com> References: <20260126-swap-table-p3-v1-0-a74155fab9b0@tencent.com> In-Reply-To: <20260126-swap-table-p3-v1-0-a74155fab9b0@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Johannes Weiner , David Hildenbrand , Lorenzo Stoakes , linux-kernel@vger.kernel.org, Chris Li , Kairui Song X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1769363877; l=5423; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=KCJ3euqA2qAaRKKZ+5BOjIB6OnqWy66OxHgNUHCngaI=; b=+gz5DUR9sRm7/DlBjNBzpGMqXfRha1zGai1p/b3tbjYg8aHCenc1uarNGtadbcM3aoVmafff+ 2wiceVFdUL4Dxgv2A1SJdkzRw26+MZR2C8HV9/QumIzrQcZay8aRMDm 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