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 868AE606A333 for ; Thu, 3 Dec 2026 16:45:03 +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=HIp6i/pQ Received: by kanga.kvack.org (Postfix) id 3C4816B05C4; Thu, 3 Dec 2026 11:44:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 39D266B05C5; Thu, 3 Dec 2026 11:44:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D8DA6B05C6; Thu, 3 Dec 2026 11:44:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B4B136B05C4 for ; Thu, 3 Dec 2026 11:44:43 -0500 (EST) Received: from smtpin06.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A11F01A07A8 for ; Thu, 3 Dec 2026 16:44:42 +0000 (UTC) X-FDA: 85503245604.06.6347A74 Received: from mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) by imf27.hostedemail.com (Postfix) with ESMTP id AE2174000E for ; Thu, 3 Dec 2026 16:44:40 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="HIp6i/pQ"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.174 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=1796316281; b=uTsDCxUdRB0ZV47tPW3KvHmsCbjGfQ+DrfSkloeTPWn9w2S+B+KybuMNsKgSYuzR0cN/7T m1EhEcxm+AKs+iTvnvKSo7tFbaV3aDRKUFGj+3Gf00u6Bel696WjX2VBSmy0MrZmuvkQL2 5fBvgU/a9tH0rdAM1dQLCqsAmt6+qNk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796316281; 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=5Aas1i23yDF7vvfFYk0OwQBsHUI3qvBsp3skN4YV0P0=; b=Hr/B1huw21Q939SsArV2xUJw27sjV1nzQ0v1bfGtwGN0QzCV70jPY9wRmNU/MEO6z2AvRv 0h7V6jKVI+ZiIybI/VEzX5X6nOKCawL5j++4yqtZab8ljXQTSj8QWmk8CzrZJTIQhCLqom p+AUy2GvXbpxCoaiKOuqj1tqTnFbb80= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="HIp6i/pQ"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=+5vNsaLOxcOyZo0kp7ZwJLO1coQpa8mlIngIYwjyvl8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796316279; v=1; x=1796921079; b=HIp6i/pQtey8PEPb13cPhirrGyUIbdRgc/V8wHXI8U+u+K4hXD+JnBUNuTUZTCfh7iCO18/3 3UGTTei4z66DMcPjhEoaYpsVxq5DHArbEk+imoIQnM9HNupJn5LHU6reqXh4csjp7rxjLtgfWdp 9o4AIIrsRZodFUiHQZ1T1EUM= X-Envelope-To: linux-mm@kvack.org Received: by mta11.migadu.com with ESMTPS id bdd0235bafea37aa; Thu, 03 Dec 2026 16:44:39 +0000 X-Mizu-Trace-ID: bdd0235bafea37aa X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, corbet@lwn.net, chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, kunwu.chan@gmail.com, klarasmodin@gmail.com, baoquan.he@linux.dev, Baoquan He Subject: [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation Date: Fri, 4 Dec 2026 00:43:33 +0800 Message-ID: <20261203164336.864691-12-baoquan.he@linux.dev> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20261203164336.864691-1-baoquan.he@linux.dev> References: <20261203164336.864691-1-baoquan.he@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Stat-Signature: ri79di67jn8ni49dr9rzymmgcbtadmmh X-Rspamd-Queue-Id: AE2174000E X-Rspam-User: X-Rspamd-Server: rspam10 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: 1796316280-228816 X-HE-Meta: U2FsdGVkX18eyc8U2yZdgFzVLZ8Xjppg0KcZLmJMlJNuyq1NVZny+UZ/UcOElQs3KHk1PHPxcd5Fgx3awSZ09zcz4EON9EYtovvQwuvcyHb+6wEr0Gte4vRwVAmohSvWuxutnskCi7ORZlXs5cN844HNWXjv8ndCvm4R6+QSXmo6vdXdvaNeqhkhJZFjIjahXlXmyBwi1b68ohlsiryW0vcG244C1pc7cE4c3sLR+4c5GNRnH65/WLu4vWM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gcuLoeSpvrh1a0IcLynrAGbwZv6DTmlSsKRTL5V1IRir8S/bBHrr9ir/umjCgNSC9DKHmBoKP/ay5wbwwee10wFo7UuasTjfERTAKhPTPn2jcCbqeqG1JAJpOi4Kwnl26evScLn667Jd9EBrIZ60NV0i6nJPKhFybGjnxwDsULoqUqN3mq9Z/Q358VU= 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 toward 65% usage, but by a bounded step. The target leaves free space in the mapped range before the next grow at 85%, so one grow is not immediately followed by another. The bounded step keeps one grow from mapping an unbounded number of pages while it sleeps, so on a large device the in-use fraction stays closer to the 85% trigger than to the 65% target. 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 | 142 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 136 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..4d6a46346b93 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -68,6 +68,23 @@ #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. It is a goal, not a bound: a + * grow maps at most XSWAP_GROW_CHUNKS chunks, so on a large device the + * in-use fraction stays closer to the trigger than to the target. + */ +#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); @@ -1317,6 +1334,9 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, struct swap_cluster_info *ci; unsigned int order = likely(folio) ? folio_order(folio) : 0; unsigned long offset = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID; +#ifdef CONFIG_XSWAP + bool retried = false; +#endif /* * Swapfile is not block device so unable @@ -1325,6 +1345,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 +1424,22 @@ 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. Grow and rescan all lists: a + * cluster published by another grower may already be on a frag list. + */ + if (!found && (si->flags & SWP_XSWAP) && + (READ_ONCE(si->flags) & SWP_WRITEOK) && !retried) { + retried = true; + if (READ_ONCE(si->nr_clusters_mapped) < + READ_ONCE(si->nr_clusters_max)) + xswap_grow(si, true); + goto new_cluster; + } +#endif done: if (!(si->flags & SWP_SOLIDSTATE)) spin_unlock(&si->global_cluster_lock); @@ -3986,8 +4029,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 +4097,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 +4185,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 +4378,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