From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) (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 81DD732F749 for ; Mon, 27 Jul 2026 14:06:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785161194; cv=none; b=VbqGFmUvv7/j949VNWGdXKa9tCdPTd3HsgkT1u6gERvSezTIJdlp+zfKraNcgVPj4nVwlrr9RxaP1gIf163pE78L3qLQQdhaRTuVzgh79pd4W1E+mHG2TGYGoQZ1ob15q7MIHd04/2ifH1OfpAZpA35XKMn/Wh5oWXmLRA+0zB8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785161194; c=relaxed/simple; bh=tGHO9DuE5MSe4UelGj5Hy6o+vWoXYBTM0lOG/gewIgc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-type; b=TFUuVdq/DU3nYcPTT8tF5vx9bhArL/OOhS5gsNAYWwm4vSczhB7A8OdlPHuDVZS2BKAs+ULqKH6SLC/H5NfgkW87iDsBf/+TgjtOLY/3DVfv9vr8tgW0bEiK1QWba3XLtZJt4Yeyf8X3FM9qyhyWGUQok7kACJGWsvnKk9FQyc0= 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=Mh1r5Igc; arc=none smtp.client-ip=95.215.58.178 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="Mh1r5Igc" 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=1785161190; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VFSGu4QBN+hpnyuZeo9XJJjO+bFyU4xQyTIcewNSbOg=; b=Mh1r5Igcec+GnxgN3CUlBXSw6HXecZKAD2ykAwO91ASaXAuG60sz39RLT6bT753dU25GVe YDLEF6Awo70yHSfl+hcrtvdR7suPFC7VFLLpsAe9KPJQJIxmHB/J2x/DKcUXSfqwbNcZ4A w6KXRoF0r0Lm/plSPbL3YN2oB7YZQGg= From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, 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 09/11] mm, swap: add debugfs knob for xswap per-device cluster limit Date: Mon, 27 Jul 2026 22:05:00 +0800 Message-ID: <20260727140503.1060918-8-baoquan.he@linux.dev> In-Reply-To: <20260727140503.1060918-1-baoquan.he@linux.dev> References: <20260727135029.1059441-1-baoquan.he@linux.dev> <20260727140503.1060918-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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Add a per-device debugfs file for runtime adjustment of xswap cluster limit: /sys/kernel/debug/xswap/type_cluster_limit Reading shows the current ceiling (in clusters); writing sets it (clamped to [0, nr_clusters_max]). Setting below nr_clusters_mapped triggers an immediate shrink check via xswap_try_shrink(). The debugfs entry is created at swapon and removed at swapoff. Signed-off-by: Baoquan He --- include/linux/swap.h | 1 + mm/swapfile.c | 97 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index e4512aeead36..ebcd5eaa7924 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -254,6 +254,7 @@ struct swap_info_struct { unsigned long nr_clusters; /* current growth ceiling (≤ nr_clusters_max) */ unsigned long nr_clusters_mapped; /* currently mapped cluster count */ unsigned long nr_free_tail; /* contiguous free clusters at tail */ + struct dentry *debugfs_entry; /* debugfs: type_max_clusters */ #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 f57823d6449f..af62981506d3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -48,6 +48,9 @@ #include "swap_table.h" #include "internal.h" #include "swap.h" +#include + +static DEFINE_SPINLOCK(swap_lock); #ifdef CONFIG_XSWAP /* @@ -63,6 +66,8 @@ #define XSWAP_GROW_CLUSTERS \ max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16) +static struct dentry *xswap_debugfs_root; + static int xswap_map_clusters(struct swap_info_struct *si, unsigned long start_idx, unsigned long nr); static void xswap_unmap_clusters(struct swap_info_struct *si, @@ -73,6 +78,90 @@ static void xswap_update_free_tail(struct swap_info_struct *si, unsigned long freed_idx); static void xswap_try_shrink(struct swap_info_struct *si); +/* + * debugfs read/write for per-device max cluster count. + * Shows/sets si->nr_clusters (current growth ceiling), clamped to + * [0, si->nr_clusters_max]. + */ +static ssize_t xswap_max_clusters_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct swap_info_struct *si = file->private_data; + char tmp[32]; + int len; + + len = snprintf(tmp, sizeof(tmp), "%lu\n", READ_ONCE(si->nr_clusters)); + return simple_read_from_buffer(buf, count, ppos, tmp, len); +} + +static ssize_t xswap_max_clusters_write(struct file *file, + const char __user *buf, + size_t count, loff_t *ppos) +{ + struct swap_info_struct *si = file->private_data; + unsigned long val, new_pages; + int err; + + err = kstrtoul_from_user(buf, count, 0, &val); + if (err) + return err; + + if (val > si->nr_clusters_max) + val = si->nr_clusters_max; + + spin_lock(&si->lock); + si->nr_clusters = val; + spin_unlock(&si->lock); + + /* Keep the visible swap size in sync with the new ceiling. */ + new_pages = min_t(unsigned long, val * SWAPFILE_CLUSTER, si->max); + if (new_pages) + new_pages--; + if (new_pages != si->pages) { + long delta = (long)new_pages - (long)si->pages; + + spin_lock(&swap_lock); + si->pages = new_pages; + atomic_long_add(delta, &nr_swap_pages); + total_swap_pages += delta; + spin_unlock(&swap_lock); + } + + /* + * Lowering the ceiling may make tail clusters eligible for + * shrinking. Trigger an immediate check. + */ + xswap_try_shrink(si); + + return count; +} + +static const struct file_operations xswap_debugfs_fops = { + .read = xswap_max_clusters_read, + .write = xswap_max_clusters_write, + .open = simple_open, + .llseek = default_llseek, +}; + +static void xswap_debugfs_add(struct swap_info_struct *si) +{ + char name[32]; + + if (!xswap_debugfs_root) + return; + + snprintf(name, sizeof(name), "type%d_cluster_limit", si->type); + si->debugfs_entry = debugfs_create_file(name, 0644, xswap_debugfs_root, + si, &xswap_debugfs_fops); +} + +static void xswap_debugfs_del(struct swap_info_struct *si) +{ + debugfs_remove(si->debugfs_entry); + si->debugfs_entry = NULL; +} + + #endif static void swap_range_alloc(struct swap_info_struct *si, @@ -89,7 +178,6 @@ static void move_cluster(struct swap_info_struct *si, * * Also protects swap_active_head total_swap_pages, and the SWP_WRITEOK flag. */ -static DEFINE_SPINLOCK(swap_lock); static unsigned int nr_swapfiles; atomic_long_t nr_swap_pages; atomic_t nr_real_swapfiles; @@ -3147,6 +3235,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si) #ifdef CONFIG_XSWAP if (si->flags & SWP_XSWAP) { + xswap_debugfs_del(si); /* Unmap all mapped clusters and free the VM_SPARSE area */ if (si->nr_clusters_mapped > 0) xswap_unmap_clusters(si, 0, si->nr_clusters_mapped); @@ -4048,6 +4137,7 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, /* All mapped clusters except cluster 0 are free at the tail */ si->nr_free_tail = si->nr_clusters_mapped - 1; + xswap_debugfs_add(si); return 0; err_unmap: @@ -4464,6 +4554,11 @@ static int __init swapfile_init(void) if (swapfile_maximum_size >= (1UL << SWP_MIG_TOTAL_BITS)) swap_migration_ad_supported = true; #endif /* CONFIG_MIGRATION */ + +#ifdef CONFIG_XSWAP + xswap_debugfs_root = debugfs_create_dir("xswap", NULL); +#endif + return 0; } subsys_initcall(swapfile_init); -- 2.54.0