From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.114 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795964944; b=gVq83EWNp+rKAa9auPHVsgwJv1gIwBe+dlBJUS9eRRtReMJAm8ubqG8OG+hrW3sNhmda XssX0kEmE5PWQZ+HFYBISVysKxt8e1Iss54xRF0rmD5Fu9lE5eu/xUKoTtptDRZRUR2N8 8HVOGZpnqT5vRBgrkel9qexukWCXAS4ZCSLXsGYwWMzXim7u3pp+kbKUrFXk1UXmg8B8u aoHRJ0/G5ET9FRXwksj0SppJ04imiPM8BhWLieFraAKr3jQaK7WB71bmFyjUvgGLam7/2 1ae49F4CiL2RCP3Mr7Yz2YlCxrka+EqJij/sB7zg85rhfvf+fdcew0g/WS6M1MUV4WQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795964944; 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=YlF1R+6AcP62NkgLhHlRUUaZz7QgKbeGcWuS0hppSFw=; b=1IrQygvMUAINpcR6V6xKKT4hCY28WFPhYPOOKV7NfSzTN5I2A/yKPP1gMR89+yEHTRPc vHR0IMtF5NQnfwHzo3/dWAVRQoYh7JlIjshTkAqjpt6laE3Zrygq4k7uUj1L3JywpeuS4 jr4GdupNTIUU3zdYfCLhQd5dps5d3CW1Dm14pJKRf8xnXS3X5uFQcOKHcaV819oK3PS6k 7xg0dORuAspM5YFfCowjdKyZAFtkJ+MD91SWUS6wBB+p3H2nKwDkyspwpLAgwN65n12Ol gUwTruLfUoE9oO4TaxPO0msJVj17Fa2cGpE7TJVfFtzf4qcwfXcTj1e81quK79jxl8Q== 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=g3eXw3d+; arc=none smtp.remote-ip=95.215.58.114 Received: from mta1.migadu.com (out-114.mta1.migadu.com [95.215.58.114]) (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 90025606A325 for ; Sun, 29 Nov 2026 15:09:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 90025606A325 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=g3eXw3d+ X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=GCzih+enEge11+3VjbiboCz37M6ja8C6GeqiBks3UHU=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795964941; v=1; x=1796569741; b=g3eXw3d+47uXWWmLZwtQR7fDoKsLaBo/F2TD0dp2qaWM1MZnmqsP/NLuebx+OH+t1uhXk4Xd HvaTXctusOnr5GiL3MeXxJkaSVWZ/eN32WjZ7+pMM/fFXtFKWhsdiLWFUBx/V7oe9RTXv/dx2ov pn1rAGMRyju13LABhWgqcYcU= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta10.migadu.com with ESMTPS id f894ccaba9c84365; Sun, 29 Nov 2026 15:09:01 +0000 X-Mizu-Trace-ID: f894ccaba9c84365 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 09/14] mm, swap: let the command line set the xswap device size Date: Sun, 29 Nov 2026 23:07:50 +0800 Message-ID: <20261129150755.191662-10-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 By default, a new xswap device is 200% of RAM, which is twice RAM. Add xswap.max= to set the size of devices created after it: xswap.max=300% three times RAM xswap.max=8G eight GiB xswap.max=max the largest size the device can address The default is fine for most users. But a machine with a lot of RAM and a small working set can get a SwapTotal much larger than it needs. The size is resolved at device creation, not at parameter parsing, because a percentage depends on RAM, which is not known yet when the parameters are parsed. An explicit size that is too large or overflows is refused; the default is clamped instead. A size below one cluster is also refused. Signed-off-by: Baoquan He --- .../admin-guide/kernel-parameters.txt | 7 + mm/swapfile.c | 135 ++++++++++++++++-- 2 files changed, 130 insertions(+), 12 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index dce9b8f3ae98..2b0fc585e0c7 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -8734,3 +8734,10 @@ Kernel parameters memory, and other data can't be written using xmon commands. off xmon is disabled. + + xswap.max= [KNL,SWAP] + Maximum size of a new xswap device. The value is a + size in bytes with an optional KMGTPE suffix, a + percentage of RAM, or "max" for the largest size + the device can address. + Format: [KMGTPE] | % | max diff --git a/mm/swapfile.c b/mm/swapfile.c index ba899e349e99..6ce5dbb3b103 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -74,6 +75,102 @@ static int xswap_destroy_by_name(const char *name); /* Serializes create and destroy: at most one xswap device exists. */ static DEFINE_MUTEX(xswap_ctl_lock); + +/* + * Size of a new device: xswap.max= if given, otherwise the built-in + * default. A percentage unless bytes is set; "max" asks for the largest + * size the device can address. + */ +static unsigned int xswap_size_pct = 200; +static unsigned long xswap_size_bytes; +static bool xswap_size_max; +static bool xswap_size_set; /* xswap.max= was given */ + +/* + * Parse "200%", "2T" or "max". @str is not modified. + */ +static int __init xswap_parse_size(const char *str, const char *what) +{ + const char *pct; + char buf[16]; + unsigned int pct_val; + unsigned long bytes; + char *end; + + pct = strchr(str, '%'); + if (!strcmp(str, "max")) { + xswap_size_max = true; + xswap_size_pct = 0; + xswap_size_bytes = 0; + } else if (pct) { + size_t len = pct - str; + + if (pct[1] || !len || len >= sizeof(buf)) + goto bad; + memcpy(buf, str, len); + buf[len] = '\0'; + if (kstrtouint(buf, 0, &pct_val) || !pct_val) + goto bad; + xswap_size_pct = pct_val; + xswap_size_bytes = 0; + xswap_size_max = false; + } else { + bytes = memparse(str, &end); + if (*end || bytes < PAGE_SIZE) + goto bad; + xswap_size_bytes = bytes; + xswap_size_pct = 0; + xswap_size_max = false; + } + + xswap_size_set = true; + return 0; +bad: + pr_err("xswap: bad %s=%s\n", what, str); + return -EINVAL; +} + +static int __init xswap_max_setup(char *str) +{ + xswap_parse_size(str, "xswap.max"); + return 1; +} +__setup("xswap.max=", xswap_max_setup); + +/* + * The most a device can address, whatever xswap.max= asked for. An xswap + * slot is still a swap entry, so the swap offset limit applies even though + * the data never goes through a physical address. + */ +static unsigned long xswap_size_limit(void) +{ + unsigned long limit = swapfile_maximum_size; + + /* A cluster index is an unsigned int. */ + if (limit / SWAPFILE_CLUSTER > UINT_MAX) + limit = (unsigned long)UINT_MAX * SWAPFILE_CLUSTER; + + return limit; +} + +static unsigned long xswap_size_pages(void) +{ + unsigned long long pages; + + if (xswap_size_max) + return xswap_size_limit(); + + if (xswap_size_bytes) + return xswap_size_bytes >> PAGE_SHIFT; + + /* Let xswap_create() refuse a size that does not fit in the type. */ + if (check_mul_overflow((unsigned long long)totalram_pages(), + (unsigned long long)xswap_size_pct, &pages)) + return ULONG_MAX; + + return div_u64(pages, 100); +} + #else /* !CONFIG_XSWAP */ static inline int xswap_create_by_name(const char *name, int swap_flags) { @@ -4079,13 +4176,15 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, #ifdef CONFIG_XSWAP /* - * Create the file-less xswap device, sized at twice RAM. Called from - * swapon(2) with SWAP_FLAG_XSWAP; there is at most one device per system. + * Create the file-less xswap device, sized by xswap.max= or the built-in + * default. Called from swapon(2) with SWAP_FLAG_XSWAP; there is at most + * one device per system. */ static int xswap_create(int prio) { struct swap_info_struct *si; - unsigned long ram, maxpages; + unsigned long maxpages; + unsigned long limit; unsigned int type; int error; @@ -4093,6 +4192,26 @@ static int xswap_create(int prio) if (!zswap_is_enabled()) return -EOPNOTSUPP; + /* + * Refuse an explicitly requested size the device cannot address; + * clamp the default size, which nobody asked for. + */ + limit = xswap_size_limit(); + maxpages = xswap_size_pages(); + if (xswap_size_set && maxpages > limit) { + pr_err("xswap: xswap.max is larger than the device can address\n"); + return -EINVAL; + } + maxpages = min(maxpages, limit); + /* + * Below one cluster, si->max is not cluster-aligned and a slot in + * the only cluster would be past the end of the device. + */ + if (maxpages < SWAPFILE_CLUSTER) { + pr_err("xswap: the configured size is smaller than one cluster\n"); + return -EINVAL; + } + mutex_lock(&xswap_ctl_lock); spin_lock(&swap_lock); @@ -4116,16 +4235,8 @@ static int xswap_create(int prio) INIT_WORK(&si->discard_work, swap_discard_work); INIT_WORK(&si->reclaim_work, swap_reclaim_work); - ram = totalram_pages(); - maxpages = min_t(unsigned long, ram * 2, swapfile_maximum_size); - /* A cluster index is an unsigned int, so cap the cluster count. */ - if (maxpages / SWAPFILE_CLUSTER > UINT_MAX) - maxpages = (unsigned long)UINT_MAX * SWAPFILE_CLUSTER; /* Cluster-aligned, so no cluster holds a slot past si->max. */ - if (maxpages > SWAPFILE_CLUSTER) - maxpages = rounddown(maxpages, SWAPFILE_CLUSTER); - if (maxpages < 2) - maxpages = 2; + maxpages = rounddown(maxpages, SWAPFILE_CLUSTER); si->bdev = NULL; si->swap_file = NULL; -- 2.54.0