From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CAE37606A32C for ; Sun, 29 Nov 2026 15:09:22 +0000 (UTC) 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=v7LIANrM Received: by kanga.kvack.org (Postfix) id E13E96B05C3; Sun, 29 Nov 2026 10:09:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D9F5C6B05C4; Sun, 29 Nov 2026 10:09:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C8CFA6B05C5; Sun, 29 Nov 2026 10:09:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9EEAA6B05C2 for ; Sun, 29 Nov 2026 10:09:21 -0500 (EST) Received: from smtpin12.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 23DA7A085E for ; Sun, 29 Nov 2026 15:09:16 +0000 (UTC) X-FDA: 85488489912.12.2765185 Received: from mta1.migadu.com (out-126.mta1.migadu.com [95.215.58.126]) by imf31.hostedemail.com (Postfix) with ESMTP id 261F520009 for ; Sun, 29 Nov 2026 15:09:13 +0000 (UTC) Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=v7LIANrM; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf31.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.126 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795964954; b=bi562CkTDlU3tzi0vQJEPx3YE+3Pvhh3Xr/PZVTXfTrS/ubI6nKK2bgczeHqfL0gFS6RGg x6u7/wWI1wj+Q0QCSNuuIyN+Gvljkw9b0XN8gOPPp41QIGJVjZYsfCJB+bQ78EJTnfBBEq 7S3IgzUZmpYvCRKkUIrAv7cDOBE33x8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795964954; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nnDt2se0Jl2qnqhzQxiWSsSV0HaaiaOaLNBM77iWrN0=; b=YS+xYyqjM6koJaxR4rgB/CD6svYgqlSyOyaOUMIjbjyn21I8iLCuIAveoqgiUem0egDNHu hyMHmpATBb5Op7FZCy/mnaXcconYP8cTqg6hppgzSw1iAho+U0ezz9hdYJHKNxxB1/wNXY atvmse7TD0m1enKR1AI4QwqvXJa2xIk= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=v7LIANrM; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf31.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.126 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=NISRTUlu2kKzai+tNdG/3O6XZyQNoJZbXKPm7WZXqO8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795964952; v=1; x=1796569752; b=v7LIANrMEaW+JQpNqQTZpnSdmusD2Ej2vFYXxVX4bvoxMbU+0uc/50yYFswf7R5MjvfjSEus 7uo6286158iludH5ijYUEGUTwM021OMG8hl09FPPgaBqQv9yjf6AtUzysvYNGdomb8fad5ZbGqR 2MuSJhiHcBBMJ2qusXfrUJOg= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id 7133fb63a2177a89; Sun, 29 Nov 2026 15:09:12 +0000 X-Mizu-Trace-ID: 7133fb63a2177a89 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 Subject: [PATCH v7 11/14] mm, swap: add xswap grow trigger on cluster allocation Date: Sun, 29 Nov 2026 23:07:52 +0800 Message-ID: <20261129150755.191662-12-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-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Stat-Signature: 3t3e5horzp9q4oqf5ycac3icmfgiocm7 X-Rspamd-Queue-Id: 261F520009 X-Rspamd-Server: rspam06 X-Rspam-User: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1795964953-982191 X-HE-Meta: U2FsdGVkX18NPZPkVR1Wr8dXFztrh8VSjrgdMP0AlbZoej5nyvhoiXY46BfLVMRpV8Q4i8xo9u6/LaE6LVJpU3Td0WFpzw8Ig+8kw5xIsOKDYppcDKA80ZUsb1t+VrPWsEtmMBSpMATVwc889znQT6kJxMLGzBTvcLuHUB7LLQo01PwnXbKhriKW7ZvOsGbnrhGX3Au6nBl2SduHJZ8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ttWw44WFHKu1xg8TSKAhnLbv9TjyP5WWZQyeFgabD1qg05rX2aiWUkKidgT7iISFwIZK+6t3eA1IWFBlGgB3kyoZZd2h3t7K8Z32ITp5u8Bqqv6mNFaW+IZ20fVmLTZRZ3w+AapvYLlZboBcyxTlpv2tlE+kmYcPuKsfpbxgGUxN7SQZF5KeqG4nf4vlDZFFxvz+3D+1DxGKeCHEVZFx3gbRu99z0inFM= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Baoquan He Grow the mapped xswap range before it runs out. When usage reaches 85% of the mapped range, map more clusters from cluster_alloc_swap_entry(). Grow to about 65% usage. This leaves free space in the mapped range before the next grow at 85%, so one grow is not immediately followed by another. A burst of concurrent allocators can take every mapped cluster off the free list while usage is still below the threshold. When the allocator cannot find a cluster, it forces one grow chunk and retries, instead of failing while unmapped clusters remain. Mapping pages can sleep. Grow before the allocation that needs them, not after it fails. Signed-off-by: Baoquan He --- include/linux/swap.h | 1 + mm/swapfile.c | 135 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 129 insertions(+), 7 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index ba1bf7e09893..55649ad5b145 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -246,6 +246,7 @@ struct swap_info_struct { unsigned long nr_clusters_max;/* total clusters in the xswap address space */ unsigned long nr_clusters_mapped; /* currently mapped cluster count */ struct mutex xswap_lock; /* serialize map/unmap operations */ + atomic_long_t xswap_grows; /* grow operations so far */ #endif struct list_head free_clusters; /* free clusters list */ struct list_head full_clusters; /* full clusters list */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 074c5114d129..9dc6bdc1a6de 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -68,6 +68,21 @@ #define XSWAP_GROW_CLUSTERS \ max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16) +/* + * Grow thresholds, as a percentage of the mapped range in use. The target + * sits below the next grow trigger, so a grow that follows a rise in usage + * does not immediately trigger another one. + */ +#define XSWAP_GROW_UNTIL 65 +#define XSWAP_GROW_WHEN 85 +#define XSWAP_GROW_CHUNKS 4 + +static bool xswap_should_grow(struct swap_info_struct *si); +static unsigned long xswap_grow(struct swap_info_struct *si, bool force); +static int xswap_map_clusters(struct swap_info_struct *si, + unsigned long start_idx, unsigned long nr, + bool link_free); + static int xswap_create(int prio); static int xswap_create_by_name(const char *name, int swap_flags); static int xswap_destroy(void); @@ -1325,6 +1340,13 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, if (order && !(si->flags & SWP_BLKDEV)) return 0; +#ifdef CONFIG_XSWAP + /* Grow before allocating: mapping pages can sleep. */ + if ((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK) && + xswap_should_grow(si)) + xswap_grow(si, false); +#endif + if (!(si->flags & SWP_SOLIDSTATE)) { /* Serialize HDD SWAP allocation for each device. */ spin_lock(&si->global_cluster_lock); @@ -1397,6 +1419,20 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, if (found) goto done; } + +#ifdef CONFIG_XSWAP + /* + * A burst of allocators can hold every mapped cluster while usage is + * still below the growth threshold, so grow before retrying. + */ + if (!found && (si->flags & SWP_XSWAP)) { + if (READ_ONCE(si->nr_clusters_mapped) < + READ_ONCE(si->nr_clusters_max)) + xswap_grow(si, true); + found = alloc_swap_scan_list(si, &si->free_clusters, + folio, false); + } +#endif done: if (!(si->flags & SWP_SOLIDSTATE)) spin_unlock(&si->global_cluster_lock); @@ -3986,8 +4022,39 @@ static unsigned long read_swap_header(struct swap_info_struct *si, } #ifdef CONFIG_XSWAP +/* + * Link the new clusters and publish the larger range in one si->lock + * section: linking first makes a cluster visible to an allocator only + * after it is on the free list. @link_free is false for the first + * chunk, whose free list is not initialized yet. + */ +static void xswap_publish_clusters(struct swap_info_struct *si, + unsigned long start_idx, unsigned long nr, + bool link_free) +{ + unsigned long i; + + spin_lock(&si->lock); + if (link_free) { + for (i = start_idx; i < start_idx + nr; i++) { + struct swap_cluster_info *ci = &si->cluster_info[i]; + + /* A concurrent grower cannot take these: xswap_lock. */ + if (ci->flags != CLUSTER_FLAG_NONE) + continue; + list_add_tail(&ci->list, &si->free_clusters); + WRITE_ONCE(ci->flags, CLUSTER_FLAG_FREE); + ci->order = 0; + } + } + /* Pairs with the smp_load_acquire() in the readers of the range. */ + smp_store_release(&si->nr_clusters_mapped, start_idx + nr); + spin_unlock(&si->lock); +} + static int xswap_map_clusters(struct swap_info_struct *si, - unsigned long start_idx, unsigned long nr) + unsigned long start_idx, unsigned long nr, + bool link_free) { /* vm_area_grow_pages() takes an offset into the area, not an address. */ unsigned long vm_end = PAGE_ALIGN((size_t)(start_idx + nr) * @@ -4023,14 +4090,65 @@ static int xswap_map_clusters(struct swap_info_struct *si, for (i = start_idx; i < start_idx + nr; i++) spin_lock_init(&si->cluster_info[i].lock); - /* - * Publish the new mappings and cluster lock initialization before - * the count; walkers without xswap_lock use smp_load_acquire(). - */ - smp_store_release(&si->nr_clusters_mapped, start_idx + nr); + /* The mappings and the locks are in place; make them visible. */ + xswap_publish_clusters(si, start_idx, nr, link_free); mutex_unlock(&si->xswap_lock); return 0; } +static bool xswap_should_grow(struct swap_info_struct *si) +{ + unsigned long mapped = READ_ONCE(si->nr_clusters_mapped); + + if (mapped >= READ_ONCE(si->nr_clusters_max)) + return false; + + return swap_usage_in_pages(si) * 100 >= + mapped * SWAPFILE_CLUSTER * XSWAP_GROW_WHEN; +} + +/* + * Map more of the cluster_info array, up to GROW_UNTIL in use. The caller + * holds percpu_swap_cluster.lock, which this drops while it sleeps. + */ +static unsigned long xswap_grow(struct swap_info_struct *si, bool force) +{ + unsigned long mapped = READ_ONCE(si->nr_clusters_mapped); + unsigned long nr_new, want; + int ret; + + lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); + + want = DIV_ROUND_UP(swap_usage_in_pages(si) * 100, + XSWAP_GROW_UNTIL * SWAPFILE_CLUSTER); + if (want <= mapped) { + if (!force) + return 0; + nr_new = XSWAP_GROW_CLUSTERS; + } else { + nr_new = rounddown(want - mapped, XSWAP_GROW_CLUSTERS); + } + if (!nr_new) + nr_new = XSWAP_GROW_CLUSTERS; + if (nr_new > XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS) + nr_new = XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS; + nr_new = min(nr_new, READ_ONCE(si->nr_clusters_max) - mapped); + if (!nr_new) + return 0; + + /* + * Mapping pages can sleep. The lock only guards the per-cpu + * cluster cache, which this path does not touch. + */ + local_unlock(&percpu_swap_cluster.lock); + ret = xswap_map_clusters(si, mapped, nr_new, true); + local_lock(&percpu_swap_cluster.lock); + if (ret) + return 0; + + atomic_long_inc(&si->xswap_grows); + return nr_new; +} + #endif /* CONFIG_XSWAP */ static int setup_swap_clusters_info(struct swap_info_struct *si, @@ -4060,7 +4178,8 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, mutex_init(&si->xswap_lock); err = xswap_map_clusters(si, 0, min_t(unsigned long, - XSWAP_GROW_CLUSTERS, nr_clusters)); + XSWAP_GROW_CLUSTERS, nr_clusters), + false); if (err) goto err_free_vm; @@ -4252,6 +4371,8 @@ static int xswap_create(int prio) */ si->ops = &swap_bdev_ops; + atomic_long_set(&si->xswap_grows, 0); + error = setup_swap_clusters_info(si, NULL, maxpages); if (error) goto bad_swap; -- 2.54.0