From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795351206; b=QHBFRgtv9StOeU57novO1UztLW5NLjNSXmMPemM+AelbRv4a/24BbdVeE4hF8rHvyKKe L3YJ0des0FpN1D3h9ATqYoeWRNhbKXpECgKZ5oBvo26xeWEm3o+LSi/Ya+bwKYBMG7UI4 oJ92qto6iFFGAzX/vgpKUguUjT7iUK6/PUOrgc9PjgTKePa7bi2+SYPC2DgriMOLz9iis 4Xy0FOvkjMjaXRSmNzuR/JrKtlywgrNJlsZ9yyaSGQdL3W+T7HpMWn30VfIgExHWPKI+t FfIlarqEW4jvVUFkvH614G8nbqeIAIE3jVeqsoDcN857SwJWzZBmbOj17C1cWeF99/Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795351206; 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=F7zcYqVKvyXt0oMuMwf5vjV4jkDKA7fpkzv0ga5XHJa8uFVsDY2t1itF6r3J+UhvKD/n HLccVSTirOrzf3fWaJvfhe2u3nqitOmF0ybNSHyeCx+Z6msurqj61VhnJeIQsWGwIPNTA tbm0rIsvGkgLlb794nJi8Gu0WPN1NX3pWTPTSr/QfBSozSBb9Z2v1s0a9kGwUqr7pwMCG DW/K1SICPx394oIgUCdw1QDhSb/RotW0N77SP2N8P/PkE8Dr3YYYQVWLCQi7CPKwy3YAu d7KuNnQeKBNeJ2+LjwbMsh3dAZWgkFc3gjFgCJ52ZyzOjerW3hRAOmz+cmJBURIZSXw== 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=Xe8mZKP4; arc=none smtp.remote-ip=91.218.175.19 Received: from mta0.migadu.com (out-19.mta0.migadu.com [91.218.175.19]) (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 939C4606A320 for ; Sun, 22 Nov 2026 12:40:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 939C4606A320 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=Xe8mZKP4 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=1795351203; v=1; x=1795956003; b=Xe8mZKP4qdDAwyabwpcNIA9oZ0DiVyAGhQTyTss9YCqZWc5QI8wFOCtBpCYrVXzoUOaaQQNp A0xF6YW83e/FcNKLkzfFRdovqtKj6ebtqGFnhIyG0Lvbiz2W9kSn6FH1fJjSN30UF8GSysfH+P+ Ez2g+U0IlqR1bLiY8BgZ0LWs= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta12.migadu.com with ESMTPS id f47dab14ca75afb5; Sun, 22 Nov 2026 12:40:02 +0000 X-Mizu-Trace-ID: f47dab14ca75afb5 X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, urezki@gmail.com, chrisl@kernel.org, kasong@tencent.com, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, shikemeng@huaweicloud.com, hannes@cmpxchg.org, yosry@kernel.org, chengming.zhou@linux.dev, corbet@lwn.net, kunwu.chan@gmail.com, klarasmodin@gmail.com, linux-kernel@vger.kernel.org, baoquan.he@linux.dev, Baoquan He , Kunwu Chan Subject: [PATCH v6 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Date: Sun, 22 Nov 2026 20:39:07 +0800 Message-ID: <20261122123918.3486916-4-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261122123918.3486916-1-baoquan.he@linux.dev> References: <20261122123918.3486916-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