From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.113 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795964909; b=YokjitQ207j0KBDK8XvtRT5rh0Xbni4O+pTdDajLyp6JV9dm5MRoyukRb3z7D2yXfmjc wdDGHJeKAKled/3zIiC9STJIWCeUpamQIEjX+payopoU5XEEPJpmYnfu/VZnUhhQcTg7H 2c4hw+7DWG94rujqyUUNn6HjDu14NjmsfWGMlxAISpyn+Ja0J2Vdx8npWsy2vZDGag0KN 1Rs54TTyugsPI1LY8l77I1tnD8fxd8csu/XvreLRxCsvP5SbyKL+OUN2ZThLk6cw5rIi6 YoA0us8yyguj8zPCyKzoDJJpsYImHb5fKXAGDV80+CWHFh8c2PrbJTrl87zU8YssKeA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795964909; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version:Content-type: Content-Transfer-Encoding; bh=wqeYfKLkqkO2fo0Ok6Kpook35o5H/Hggacweb9IUcO8=; b=oU0CRBzzctkMrzH18KtBrrNURCE3D5vr4KxeSGcRg6pU/EuYO6yc3y4NvTx2pdDSg/3M 13Vu3TS+4Ybh6dI2ASY+NrD6PVlPzWFRq1ypN2NWUbrlkQwZYC3oBrLD563wz/Syy1QSG u/YilyJ7ahogWERXfBZqdDVfKmOjK1tjniYiufqEA+jnuyD/BYB3TgG4+QtJzZWoAN29P SqRCNBuVQ7LYv1GG+Z+4Bm0xR2yTYFa7LbnLlQMvJWqqZtVuIYQesugzE5XDppSplmGqd Y3ewerqi0agoF/AzmzfP0qewO8FqXzXgcaM//b1Ufi4KAzt1PKgzDBqrJXFjqQinI/g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="Ol/2K9/K"; arc=none smtp.remote-ip=91.218.175.113 Received: from mta0.migadu.com (out-113.mta0.migadu.com [91.218.175.113]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0B242606A31C for ; Sun, 29 Nov 2026 15:08:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0B242606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Ol/2K9/K X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=2bXaGVW2NID6qzPP0iSrWS/UKvSBqUCkOqdFi1F6OSc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795964905; v=1; x=1796569705; b=Ol/2K9/K6TZ2amtMLll1aPlqayedTKSog5NNB9VzhJbssrwgQvZGfztYdyfuYMmUKbXUNIHs mFD2rFlkMx2lL5jNPFZALMB/H8j6gD9fnm3tWnxA5mHEcw3lSg4rDd7KGJxQPo5+igfHH1URBhO Cg1UV8GyJIcD7iXawHnUpHlU= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id e76d2066e95306e2; Sun, 29 Nov 2026 15:08:24 +0000 X-Mizu-Trace-ID: e76d2066e95306e2 X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org, corbet@lwn.net, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, shikemeng@huaweicloud.com, yosry@kernel.org, chengming.zhou@linux.dev, kunwu.chan@gmail.com, klarasmodin@gmail.com, linux-kernel@vger.kernel.org, baoquan.he@linux.dev, Baoquan He , Kunwu Chan Subject: [PATCH v7 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Date: Sun, 29 Nov 2026 23:07:44 +0800 Message-ID: <20261129150755.191662-4-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261129150755.191662-1-baoquan.he@linux.dev> References: <20261129150755.191662-1-baoquan.he@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit From: Baoquan He Change free_swap_cluster_info() to take struct swap_info_struct* instead of (cluster_info, maxpages). It now extracts the fields from si and clears si->cluster_info after freeing to avoid a double free on the swapon() error path. The new parameter also lets the xswap path access si->flags in the function. Reviewed-by: Chris Li Reviewed-by: Kunwu Chan Signed-off-by: Baoquan He --- mm/swapfile.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 558c4b75bf92..35a079f2bb76 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3208,14 +3208,17 @@ static void wait_for_allocation(struct swap_info_struct *si) } } -static void free_swap_cluster_info(struct swap_cluster_info *cluster_info, - unsigned long maxpages) +static void free_swap_cluster_info(struct swap_info_struct *si) { + struct swap_cluster_info *cluster_info = si->cluster_info; + unsigned long maxpages = si->max; struct swap_cluster_info *ci; - int i, nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); + int i, nr_clusters; if (!cluster_info) return; + + nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); for (i = 0; i < nr_clusters; i++) { ci = cluster_info + i; /* Cluster with bad marks count will have a remaining table */ @@ -3227,6 +3230,7 @@ static void free_swap_cluster_info(struct swap_cluster_info *cluster_info, spin_unlock(&ci->lock); } kvfree(cluster_info); + si->cluster_info = NULL; } /* @@ -3254,11 +3258,9 @@ static void flush_percpu_swap_cluster(struct swap_info_struct *si) SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) { struct swap_info_struct *p = NULL; - struct swap_cluster_info *cluster_info; struct file *swap_file, *victim; struct address_space *mapping; struct inode *inode; - unsigned int maxpages; int err, found = 0; if (!capable(CAP_SYS_ADMIN)) @@ -3350,10 +3352,6 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) swap_file = p->swap_file; p->swap_file = NULL; - maxpages = p->max; - cluster_info = p->cluster_info; - p->max = 0; - p->cluster_info = NULL; spin_unlock(&p->lock); spin_unlock(&swap_lock); arch_swap_invalidate_area(p->type); @@ -3361,7 +3359,9 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) mutex_unlock(&swapon_mutex); kfree(p->global_cluster); p->global_cluster = NULL; - free_swap_cluster_info(cluster_info, maxpages); + free_swap_cluster_info(p); + p->max = 0; + p->cluster_info = NULL; inode = mapping->host; @@ -3728,6 +3728,8 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, if (!cluster_info) goto err; + si->cluster_info = cluster_info; + for (i = 0; i < nr_clusters; i++) spin_lock_init(&cluster_info[i].lock); @@ -3791,7 +3793,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, si->cluster_info = cluster_info; return 0; err: - free_swap_cluster_info(cluster_info, maxpages); + free_swap_cluster_info(si); return err; } @@ -4013,7 +4015,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) si->global_cluster = NULL; inode = NULL; destroy_swap_extents(si, swap_file); - free_swap_cluster_info(si->cluster_info, si->max); + free_swap_cluster_info(si); si->cluster_info = NULL; /* * Clear the SWP_USED flag after all resources are freed so -- 2.54.0