From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A5423D9680 for ; Wed, 5 Aug 2026 07:54:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785916477; cv=none; b=SI8pt6N2VyoqDw4HBGUHSufTMfS9v+FeHGj+8oq+ydJLdTgRHTlWGUJOftiqGCfEIBkzcaNiH1utLWIu/+aL4QHwneJvI+Se/VinUkqa0GM5blZS0QwKAUpiU2bBT3w6iXUX9NDbU3DzOteMVSbwLUYM5LKAKmsHNjDbvVjbDMQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785916477; c=relaxed/simple; bh=lnKOQpE6NFp+c7+J8jsraye3pE5Ig570bqpTZNlFz7g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type:Content-type; b=jMhrvs3oXSpHO98yznRYqffY5nswGXzDw4EjKpTUG8xU9KkgyiMDDw6JqyYzuldJEoWzldzndgQdvoFGew2i0K0wDHxwU6E13qpAAU4Vam44uEM4LZp/GSxURcFdsXYsv18xUnveMrjF5kLHXHqacmUrLdp3LcGO6FomMrFUNEY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=lwo+LBGq; arc=none smtp.client-ip=91.218.175.189 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) header.d=linux.dev header.i=@linux.dev header.b="lwo+LBGq" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1785916472; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A1v4kpNGwyglXBeEENPVH37QuycHMC781xF2cHBH8qU=; b=lwo+LBGqUGfl4wpOaO9E8CN/JxJ1ABjebdqeWt+2Jb5ttg/9qvrmXbXUodRe8Xw/8+/rLO wU1QcRvzLdgnMeilGnm/Ax9BmV2ojOea+t9HHZxWqTts1wDL8wHzA5CppS+bAcT7WRzqE7 cPXbnT7eE4cmO4RWb9v193K8nF5AYlI= From: Baoquan He To: linux-mm@kvack.org Cc: chrisl@kernel.org, nphamcs@gmail.com, kasong@tencent.com, baohua@kernel.org, youngjun.park@lge.com, hannes@cmpxchg.org, yosry@kernel.org, david@kernel.org, shikemeng@huaweicloud.com, chengming.zhou@linux.dev, linux-kernel@vger.kernel.org, Baoquan He Subject: [RFC PATCH v2 05/10] mm, swap: add xswap_try_shrink and shrink trigger on cluster free Date: Wed, 5 Aug 2026 15:53:28 +0800 Message-ID: <20260805075336.3579395-6-baoquan.he@linux.dev> In-Reply-To: <20260805075336.3579395-1-baoquan.he@linux.dev> References: <20260805075336.3579395-1-baoquan.he@linux.dev> 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-type: text/plain Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Add xswap_try_shrink() — the shrink logic that scans backwards from the tail to find contiguous free clusters, then unmaps full pages when >= XSWAP_GROW_CLUSTERS free clusters accumulate. Wire the trigger in __free_cluster(): after a cluster is released to the free list, call xswap_try_shrink() to attempt tail shrinking. Also update the XSWAP_GROW_CLUSTERS comment to reflect both grow and shrink semantics. Signed-off-by: Baoquan He --- mm/swapfile.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5d8e10be0159..0d1d0c5e24c6 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -51,11 +51,13 @@ #ifdef CONFIG_XSWAP /* - * xswap: dynamically grow the cluster_info array via a VM_SPARSE area. + * xswap: dynamically grow and shrink the cluster_info array via a + * VM_SPARSE area. * - * XSWAP_GROW_CLUSTERS is the number of clusters to map in one grow - * operation. It is set to the number of cluster_info structs that - * fit in a single page (at least 16), so that the vmalloc page table + * XSWAP_GROW_CLUSTERS is the number of clusters to map/unmap in one + * grow/shrink operation. It is set to the number of cluster_info + * structs that fit in a single page (at least 16), so that the vmalloc + * page table * overhead is proportional to the number of clusters mapped. */ #define XSWAP_GROW_CLUSTERS \ @@ -66,6 +68,7 @@ static int xswap_map_clusters(struct swap_info_struct *si, static void xswap_unmap_clusters(struct swap_info_struct *si, unsigned long start_idx, unsigned long nr); static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data); +static void xswap_try_shrink(struct swap_info_struct *si); #endif static void swap_range_alloc(struct swap_info_struct *si, @@ -629,6 +632,9 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info swap_cluster_free_table(ci); move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE); ci->order = 0; +#ifdef CONFIG_XSWAP + xswap_try_shrink(si); +#endif } /* @@ -3835,6 +3841,40 @@ static int xswap_check_mapped(pte_t *pte, unsigned long addr, void *data) { return 1; } + +/* + * Try to shrink the cluster_info tail: unmap contiguous free clusters + * at the end of the mapped range. + */ +static void xswap_try_shrink(struct swap_info_struct *si) +{ + struct swap_cluster_info *ci; + unsigned long last, idx; + + if (!(si->flags & SWP_XSWAP)) + return; + if (READ_ONCE(si->nr_clusters_mapped) <= 1) /* keep cluster 0 */ + return; + + /* Find the last non-free cluster from the tail */ + last = READ_ONCE(si->nr_clusters_mapped); + while (last > 1) { + idx = last - 1; + ci = &si->cluster_info[idx]; + if (ci->count || ci->flags != CLUSTER_FLAG_FREE) + break; + last = idx; + } + + if (last == si->nr_clusters_mapped) + return; /* nothing to shrink */ + + /* Only unmap if we can free at least one full page of clusters */ + if (si->nr_clusters_mapped - last < XSWAP_GROW_CLUSTERS) + return; + + xswap_unmap_clusters(si, last, si->nr_clusters_mapped - last); +} #endif /* CONFIG_XSWAP */ static int setup_swap_clusters_info(struct swap_info_struct *si, -- 2.54.0