[PATCH v6 12/14] mm, swap: add debugfs counters for xswap

From: Baoquan He

Date: Sun Nov 22 2026 - 07:48:44 EST


From: Baoquan He <hebaoquan@xxxxxxxxxx>

The mapped range and the grow count are not visible from userspace.
This makes it hard to check if the grow thresholds work: we cannot see
how large the mapped range is, how much it grows, or how much free tail
it keeps.

Put this information in debugfs. It is a debugging aid, not a stable
interface, so it does not belong in sysfs or the ABI:

# cat /sys/kernel/debug/xswap/xswap.stats
type 0
clusters_max 2038783
clusters_mapped 255
usage_pages 91456
tail_free 16
grows 3

Signed-off-by: Baoquan He <hebaoquan@xxxxxxxxxx>
---
include/linux/swap.h | 1 +
mm/swapfile.c | 69 ++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index 5d9e5b6f2bd6..9daf0ac40c39 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 */
+ struct dentry *xswap_debugfs; /* mapped range and counters */
atomic_long_t xswap_grows; /* grow operations so far */
#endif
struct list_head free_clusters; /* free clusters list */
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 213b66bb1d6d..462efeef887a 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -43,12 +43,14 @@
#include <linux/suspend.h>
#include <linux/zswap.h>
#include <linux/plist.h>
+#include <linux/debugfs.h>

#include <asm/tlbflush.h>
#include <linux/leafops.h>
#include "swap_table.h"
#include "internal.h"
#include "swap.h"
+
#define DEF_SWAP_PRIO -1

/*
@@ -79,6 +81,7 @@

static bool xswap_should_grow(struct swap_info_struct *si);
static unsigned long xswap_grow(struct swap_info_struct *si);
+static void xswap_debugfs_del(struct swap_info_struct *si);
static int xswap_map_clusters(struct swap_info_struct *si,
unsigned long start_idx, unsigned long nr,
bool link_free);
@@ -3455,6 +3458,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si)
}
spin_unlock(&ci->lock);
}
+ xswap_debugfs_del(si);
vfree(si->cluster_info);
si->cluster_vm = NULL;
si->cluster_info = NULL;
@@ -4087,6 +4091,7 @@ static int xswap_map_clusters(struct swap_info_struct *si,
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);
@@ -4138,6 +4143,68 @@ static unsigned long xswap_grow(struct swap_info_struct *si)
return nr_new;
}

+static ssize_t xswap_debugfs_read(struct file *file, char __user *buf,
+ size_t count, loff_t *ppos)
+{
+ struct swap_info_struct *si = file->private_data;
+ struct swap_cluster_info *ci;
+ unsigned long mapped, tail = 0;
+ char tmp[192];
+ int len;
+
+ /*
+ * Count the free clusters at the end of the mapped range. The
+ * xswap_lock keeps the range from moving under the scan.
+ */
+ mutex_lock(&si->xswap_lock);
+ mapped = READ_ONCE(si->nr_clusters_mapped);
+ if (si->cluster_info) {
+ while (mapped - tail > 1) {
+ ci = &si->cluster_info[mapped - tail - 1];
+ if (READ_ONCE(ci->count) ||
+ READ_ONCE(ci->flags) != CLUSTER_FLAG_FREE)
+ break;
+ tail++;
+ }
+ }
+ len = scnprintf(tmp, sizeof(tmp),
+ "type %d\nclusters_max %lu\nclusters_mapped %lu\nusage_pages %lu\ntail_free %lu\ngrows %lu\n",
+ si->type, READ_ONCE(si->nr_clusters_max), mapped,
+ swap_usage_in_pages(si), tail,
+ atomic_long_read(&si->xswap_grows));
+ mutex_unlock(&si->xswap_lock);
+
+ return simple_read_from_buffer(buf, count, ppos, tmp, len);
+}
+
+static const struct file_operations xswap_debugfs_fops = {
+ .read = xswap_debugfs_read,
+ .open = simple_open,
+ .llseek = default_llseek,
+};
+
+static struct dentry *xswap_debugfs_root;
+
+static void xswap_debugfs_add(struct swap_info_struct *si)
+{
+ if (!xswap_debugfs_root)
+ xswap_debugfs_root = debugfs_create_dir("xswap", NULL);
+ if (IS_ERR_OR_NULL(xswap_debugfs_root))
+ return;
+
+ /* One device, so one file: /sys/kernel/debug/xswap/xswap.stats. */
+ si->xswap_debugfs = debugfs_create_file("xswap.stats", 0444,
+ xswap_debugfs_root, si,
+ &xswap_debugfs_fops);
+ if (IS_ERR(si->xswap_debugfs))
+ si->xswap_debugfs = NULL;
+}
+
+static void xswap_debugfs_del(struct swap_info_struct *si)
+{
+ debugfs_remove(si->xswap_debugfs);
+ si->xswap_debugfs = NULL;
+}
#endif /* CONFIG_XSWAP */

static int setup_swap_clusters_info(struct swap_info_struct *si,
@@ -4360,6 +4427,7 @@ static int xswap_create(int prio)
*/
si->ops = &swap_bdev_ops;

+ si->xswap_debugfs = NULL;
atomic_long_set(&si->xswap_grows, 0);

error = setup_swap_clusters_info(si, NULL, maxpages);
@@ -4371,6 +4439,7 @@ static int xswap_create(int prio)
goto bad_swap;

mutex_lock(&swapon_mutex);
+ xswap_debugfs_add(si);
si->prio = prio;
si->list.prio = -si->prio;
si->avail_list.prio = -si->prio;
--
2.54.0