From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.157 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795964955; b=chtPpnQFYV0bUksvAOH3VCsXidEUrliPjMyJNEoQZ5Rk2rQmbP49/KmT+//Yo1G8VADD 0LCMnTQ6Z/JKdWqEElzqw1+GSs09LiDoQX3bBUc6JrI/DfplVzGA6i38gYef7KeovwG9D viIBJhivd3Brea8s+MbFHDiu+PPPB7Od61oae403PZ445F3KjLpFmljbYuXUL++ycfNeU R5kAAdh89xLLufU4ri5Z2z+aRnOrcP5sw8ic2FB8znS7o1rIZxfHHLOmy6rVwTkpCul4o 2iQawFPIA2CPjfle81wj8zv1H85O+DHyd7Tc/8Aje3gZv9gfdTSdkkNRseqVRkVyhZg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795964955; 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=nnDt2se0Jl2qnqhzQxiWSsSV0HaaiaOaLNBM77iWrN0=; b=13TvQ0L9YHlTeC33kkQVo6M2xRWuyl29EZZbDrAlkIp9khwzH259pkQpkhmgKi5hy45S dd2lgIBz64ILBCthLvhGs45DCMgAGHhq0/ugLZ0tYaS9+/nN66uUltb7xk+PARcIR0fZg iad3QogqSveOY7VEX3hKV1UrTl+qG4eXPSPhnRrAAqZnqkA8W2uuqLkON8xWlrZCTHsH8 c5T0Y+9aRE7zhIcsuqYGGyR5nIvW6NFQrO6YGadggEqOjP42YywoCQTn5LKp6M9WgCIgU QifWanWKxhd+LnFLUizWlE8PRsmIv9FOjkmK41wsXLyPZXjB7cSs57XzWSFgo5kFAKQ== 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=v7LIANrM; arc=none smtp.remote-ip=91.218.175.157 Received: from mta0.migadu.com (out-157.mta0.migadu.com [91.218.175.157]) (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 07100606A327 for ; Sun, 29 Nov 2026 15:09:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 07100606A327 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=v7LIANrM X-Envelope-To: linux-kernel@vger.kernel.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-kernel@vger.kernel.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-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 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