From nobody Mon Feb 9 16:50:59 2026 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 A0366346FA0 for ; Wed, 28 Jan 2026 09:30:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592641; cv=none; b=KZtH9RN8/zMfa7J+VEUWVWp12F4D/U0R0aEZ3yIAWVzfJIzW68CnHA9raWFJe6zr4NeRJFbisgbwzoVHIeBql3vVv03/vXOlU13HKwkopXmU+OBiovRUPInFoZGQR8BBvUOXf5+D2VZxxXHqtf997afXDvUnjWAUKqhRHc9vwvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769592641; c=relaxed/simple; bh=kO89w4TSJX4bByAX1RSmDOrEA4P5dAaQCy7BjrZ/M2M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bBXebOiP6WVt+faHYEfN4dRZBsEakF8XZ9GsC67HSyR7QhM9HnBCNxq26xQoyymCNZt5vzIWzDPha8BKCF/OhpNNlbTWF/C118flOd8yUbPgn3X+SkF24uKG8/Pgy9EUwGBkLO7Y7MQMkTn6r8NamhsIYuWa69+sP2YuEAhKIzQ= 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=EwsgJRql; arc=none smtp.client-ip=209.85.216.51 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="EwsgJRql" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-34c3259da34so3920362a91.2 for ; Wed, 28 Jan 2026 01:30:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769592639; x=1770197439; 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=jHlSAs5DuvtfLFcO72y/SZVl/BNUOfJD+/3/mENU6eI=; b=EwsgJRql4hvh2RrKg/+VnNO2ljL5h5lzWJdhRDCdZEmGFnhwP4WtYurTdW+DCRF9og oNPCDAfofzlWyoGnMR+0Y2BXtuMj1YBBGGh4ZnW9+30SAR7l7iK1bhAhHKgZztpT616q 0vWGricgysGEf/x9ro0k9MQjee4/QsIPTNoX7RVOmoIvor6oTHxEkUz47opK2x08gj+t QgowpLg2Jrmnb8SWvqvzbvITrXDOAHvjms1cb0eHZzgC0yz6l/w3F9THrAsdoa+qTA61 ecwd8LGYsPxznCM/aTuSRggJRIWL8QQLBalHwb9SbtnhHyMXIzNcDfwwtELQAPNG6jTN CZ6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769592639; x=1770197439; 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=jHlSAs5DuvtfLFcO72y/SZVl/BNUOfJD+/3/mENU6eI=; b=VxPRmQlYHsv+B6Z1fAW/5WLIs4Wux2jrIH0LIZE8nmnXXtbgwfxNshTvNskCUgp+aj vfhNlzjoUCKPGsMZ4eajP6KlOX6S3wO9e/+RikLfui+30JZ0DtTU1N+U/3v57oYH3oef CIjGkh4Ep+Jqq85fqq9JqByWPs8Q5XD82bxTo4glnjqZR+Dhk5ri0TccBGBwEqrZFqKz APJipLpOT13b0ob6UZpa1iNVnGDcKRwmSxS5CxH5kzIdUGpz4C0LBU1dUU/qPZ+okaVc weKJvKSvprV92oObrUMoml3R7Mza/TuQS2ez9ptyf5dctssz9bV5T6dNQrHVx4rCGoCQ rkJA== X-Forwarded-Encrypted: i=1; AJvYcCV9pF2powaSpNEmVn2uEN5jQtjveo/0UAInJqQ4oSMixNl7zLvHLYyj1qnuKGjSU9BFa6rD5ofHuRmc958=@vger.kernel.org X-Gm-Message-State: AOJu0YwFU/FMbTW+ctd+hCo4NhtiXulI+LjWVyWCdtS+z0WiA/04XV6f HdH/X8a5fFG/0lvZseIWxqUoYjJtpxqarzv2/NRgxjtMp9jYzRGi8kPP X-Gm-Gg: AZuq6aLHicleFk9hnEnmeOtIKIzvUFOt/3OzSC78Emi9u1AYYU0d2eMzpQAe9GbqA5n Lj9Sh8eNJyuJsgXeWWK7a36h23APu6C5jsg+H52nVmtwibdvUrSQfxKcgUAVIWj7PNuJzf4FryY eBgfm6E4wytJLOKkaNV4f1yka95cKxWfb98py2kKCyao8jw1J19TgQOwsJlxYet/lYv0N4+VLs9 KewVGZblL/0tUNnJ7/cIaCtC7balskyqJJccZjizCa0l2A93FI99eJivfm2HylMtDYa0IDfbkTc S0gex1ylGlsgraKSpFDCwHn1jHAXBtMJoBiIP52QRyTGk7iaS1jaddV3PIDD6wzF04RLDSKaw/y 5wz8TWekGjM1YQJyC9NYY9M7vkP+21Mlr45capTtQljBSJmdAZEzq0hn/n8UZKX1UFoyWO/wP1H mg0OsNnknxjlFFViNYJNh/AzIKwHXbr6JGMi7AOawXYnGJH8labl2TFUsCDL+4mw+HPCGE X-Received: by 2002:a17:90b:3901:b0:32d:f352:f764 with SMTP id 98e67ed59e1d1-353fecd08edmr3956834a91.2.1769592638870; Wed, 28 Jan 2026 01:30:38 -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.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 01:30:38 -0800 (PST) From: Kairui Song Date: Wed, 28 Jan 2026 17:28:26 +0800 Subject: [PATCH v2 02/12] mm, swap: clean up swapon process and locking 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-2-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=8467; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=GJOWST4EwFrMORO0TdNo6OHVZqB7RqYtXfEdQQZxN/s=; b=eaBaKJzF46kjkypZP4nFMY/8qkGRIc2eKS5aNa/7z8CfPJ2+q5TnR7L2qpYC5Tg4uFLC0vwP6 y1nyymxOE7tDzhppZ04ZedNIJwRp+gsO1r1jLBx2C6FJ81WxLUiledA X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= From: Kairui Song Slightly clean up the swapon process. Add comments about what swap_lock protects, introduce and rename helpers that wrap swap_map and cluster_info setup, and do it outside of the swap_lock lock. This lock protection is not needed for swap_map and cluster_info setup because all swap users must either hold the percpu ref or hold a stable allocated swap entry (e.g., locking a folio in the swap cache) before accessing. So before the swap device is exposed by enable_swap_info, nothing would use the swap device's map or cluster. So we are safe to allocate and set up swap data freely first, then expose the swap device and set the SWP_WRITEOK flag. Signed-off-by: Kairui Song --- mm/swapfile.c | 87 ++++++++++++++++++++++++++++++++-----------------------= ---- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 521f7713a7c3..53ce222c3aba 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -65,6 +65,13 @@ static void move_cluster(struct swap_info_struct *si, struct swap_cluster_info *ci, struct list_head *list, 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. + */ static DEFINE_SPINLOCK(swap_lock); static unsigned int nr_swapfiles; atomic_long_t nr_swap_pages; @@ -2646,8 +2653,6 @@ static int setup_swap_extents(struct swap_info_struct= *sis, } =20 static void setup_swap_info(struct swap_info_struct *si, int prio, - unsigned char *swap_map, - struct swap_cluster_info *cluster_info, unsigned long *zeromap) { si->prio =3D prio; @@ -2657,8 +2662,6 @@ static void setup_swap_info(struct swap_info_struct *= si, int prio, */ si->list.prio =3D -si->prio; si->avail_list.prio =3D -si->prio; - si->swap_map =3D swap_map; - si->cluster_info =3D cluster_info; si->zeromap =3D zeromap; } =20 @@ -2676,13 +2679,11 @@ static void _enable_swap_info(struct swap_info_stru= ct *si) } =20 static void enable_swap_info(struct swap_info_struct *si, int prio, - unsigned char *swap_map, - struct swap_cluster_info *cluster_info, - unsigned long *zeromap) + unsigned long *zeromap) { spin_lock(&swap_lock); spin_lock(&si->lock); - setup_swap_info(si, prio, swap_map, cluster_info, zeromap); + setup_swap_info(si, prio, zeromap); spin_unlock(&si->lock); spin_unlock(&swap_lock); /* @@ -2700,7 +2701,7 @@ 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->swap_map, si->cluster_info, si->zeromap= ); + setup_swap_info(si, si->prio, si->zeromap); _enable_swap_info(si); spin_unlock(&si->lock); spin_unlock(&swap_lock); @@ -2724,8 +2725,8 @@ static void wait_for_allocation(struct swap_info_stru= ct *si) } } =20 -static void free_cluster_info(struct swap_cluster_info *cluster_info, - unsigned long maxpages) +static void free_swap_cluster_info(struct swap_cluster_info *cluster_info, + unsigned long maxpages) { struct swap_cluster_info *ci; int i, nr_clusters =3D DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); @@ -2883,7 +2884,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) p->global_cluster =3D NULL; vfree(swap_map); kvfree(zeromap); - free_cluster_info(cluster_info, maxpages); + free_swap_cluster_info(cluster_info, maxpages); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); =20 @@ -3232,10 +3233,15 @@ static unsigned long read_swap_header(struct swap_i= nfo_struct *si, =20 static int setup_swap_map(struct swap_info_struct *si, union swap_header *swap_header, - unsigned char *swap_map, unsigned long maxpages) { unsigned long i; + unsigned char *swap_map; + + swap_map =3D vzalloc(maxpages); + si->swap_map =3D swap_map; + if (!swap_map) + return -ENOMEM; =20 swap_map[0] =3D SWAP_MAP_BAD; /* omit header page */ for (i =3D 0; i < swap_header->info.nr_badpages; i++) { @@ -3256,9 +3262,9 @@ static int setup_swap_map(struct swap_info_struct *si, return 0; } =20 -static struct swap_cluster_info *setup_clusters(struct swap_info_struct *s= i, - union swap_header *swap_header, - unsigned long maxpages) +static int setup_swap_clusters_info(struct swap_info_struct *si, + union swap_header *swap_header, + unsigned long maxpages) { unsigned long nr_clusters =3D DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); struct swap_cluster_info *cluster_info; @@ -3328,10 +3334,11 @@ static struct swap_cluster_info *setup_clusters(str= uct swap_info_struct *si, } } =20 - return cluster_info; + si->cluster_info =3D cluster_info; + return 0; err: - free_cluster_info(cluster_info, maxpages); - return ERR_PTR(err); + free_swap_cluster_info(cluster_info, maxpages); + return err; } =20 SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) @@ -3347,9 +3354,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) int nr_extents; sector_t span; unsigned long maxpages; - unsigned char *swap_map =3D NULL; unsigned long *zeromap =3D NULL; - struct swap_cluster_info *cluster_info =3D NULL; struct folio *folio =3D NULL; struct inode *inode =3D NULL; bool inced_nr_rotate_swap =3D false; @@ -3360,6 +3365,11 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) if (!capable(CAP_SYS_ADMIN)) return -EPERM; =20 + /* + * 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. + */ si =3D alloc_swap_info(); if (IS_ERR(si)) return PTR_ERR(si); @@ -3442,18 +3452,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specia= lfile, int, swap_flags) =20 maxpages =3D si->max; =20 - /* OK, set up the swap map and apply the bad block list */ - swap_map =3D vzalloc(maxpages); - if (!swap_map) { - error =3D -ENOMEM; + /* Setup the swap map and apply bad block */ + error =3D setup_swap_map(si, swap_header, maxpages); + if (error) goto bad_swap_unlock_inode; - } =20 - error =3D swap_cgroup_swapon(si->type, maxpages); + /* Set up the swap cluster info */ + error =3D setup_swap_clusters_info(si, swap_header, maxpages); if (error) goto bad_swap_unlock_inode; =20 - error =3D setup_swap_map(si, swap_header, swap_map, maxpages); + error =3D swap_cgroup_swapon(si->type, maxpages); if (error) goto bad_swap_unlock_inode; =20 @@ -3481,13 +3490,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) inced_nr_rotate_swap =3D true; } =20 - cluster_info =3D setup_clusters(si, swap_header, maxpages); - if (IS_ERR(cluster_info)) { - error =3D PTR_ERR(cluster_info); - cluster_info =3D NULL; - goto bad_swap_unlock_inode; - } - if ((swap_flags & SWAP_FLAG_DISCARD) && si->bdev && bdev_max_discard_sectors(si->bdev)) { /* @@ -3540,7 +3542,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) prio =3D swap_flags & SWAP_FLAG_PRIO_MASK; =20 si->swap_file =3D swap_file; - enable_swap_info(si, prio, swap_map, cluster_info, zeromap); + + /* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */ + enable_swap_info(si, prio, zeromap); =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, @@ -3566,13 +3570,18 @@ SYSCALL_DEFINE2(swapon, const char __user *, specia= lfile, int, swap_flags) inode =3D NULL; destroy_swap_extents(si, swap_file); swap_cgroup_swapoff(si->type); + vfree(si->swap_map); + si->swap_map =3D NULL; + free_swap_cluster_info(si->cluster_info, si->max); + si->cluster_info =3D NULL; + /* + * Clear the SWP_USED flag after all resources are freed so + * alloc_swap_info can reuse this si safely. + */ spin_lock(&swap_lock); si->flags =3D 0; spin_unlock(&swap_lock); - vfree(swap_map); kvfree(zeromap); - if (cluster_info) - free_cluster_info(cluster_info, maxpages); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); if (swap_file) --=20 2.52.0