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 A3363606A327 for ; Sun, 29 Nov 2026 15:09:25 +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=eS8Qz0Tl Received: by kanga.kvack.org (Postfix) id F31076B05C2; Sun, 29 Nov 2026 10:09:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EFC326B05C6; Sun, 29 Nov 2026 10:09:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E12136B05C2; Sun, 29 Nov 2026 10:09:21 -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 B0B8A6B05C3 for ; Sun, 29 Nov 2026 10:09:21 -0500 (EST) Received: from smtpin13.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 42D031A05C2 for ; Sun, 29 Nov 2026 15:09:21 +0000 (UTC) X-FDA: 85488490122.13.DF66940 Received: from mta1.migadu.com (out-131.mta1.migadu.com [95.215.58.131]) by imf26.hostedemail.com (Postfix) with ESMTP id 4276214000C for ; Sun, 29 Nov 2026 15:09:19 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=eS8Qz0Tl; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.131 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=1795964959; b=T7EscCeSF4+9lebHlcnGBcNBw9apJYrLfgS66LSMd9v+RYG7YH4jbiCuJLBLLbkBIIYLJ/ teCJYnNL3aZGhOT9OdNleH3g+0BXuIpOjcbKrkoavYguqGQrMTvNuNLPZ5s/RlK0RspbxY L7G4Hf3Zo0U2/hnHm7vf0xnpW2nhl4U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795964959; 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=jt2g1IRCYbN9WF0yB6bO/95kQAmc8spUQdmXJMkz9Po=; b=KA3tPCY4YBVU9O+zxsJTsGKNwIQ08m8AN4sEDqIl20fvAAyK0kSTNDhpD+g9uU8agsWEn6 /xNnsnb2KdMArTvpKeb/8otzhOx7wnYt8ufnHkDdgslkrVxJ32UzJbv4ZIRqaw1WCcU34y EU7Gf1FlKZi3CjlOkqIsU/X7xPMWfbM= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=eS8Qz0Tl; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf26.hostedemail.com: domain of baoquan.he@linux.dev designates 95.215.58.131 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=uPHEv/bvQoH8xTttFsPBozXF5cOb1XkD0bdBsL4Bbws=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795964957; v=1; x=1796569757; b=eS8Qz0TlEQyD1lvmjj6g9w3oqFIdEbv16oRHoB7LMQXbxxw4yOXvZs+WAJJ6eIegx5Bog2c5 4OkSL4i/zzhSrHQMnmRuDgzYCIYSCLagTKULK7Ca9ls5UH28A8z+q7yg5kNYzizxKE7FPwN4IuQ ea28Z3UA2jNza5hYli1nFH6s= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id c40cc21ec17dad95; Sun, 29 Nov 2026 15:09:17 +0000 X-Mizu-Trace-ID: c40cc21ec17dad95 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 12/14] mm, swap: add debugfs counters for xswap Date: Sun, 29 Nov 2026 23:07:53 +0800 Message-ID: <20261129150755.191662-13-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-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4276214000C X-Stat-Signature: jnuugaf8qm1d7cxz57fo4ijobaebnhj5 X-Rspam-User: 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-Rspamd-Server: rspam04 X-HE-Tag: 1795964959-840382 X-HE-Meta: U2FsdGVkX1/k3HpYHFOtorsPNUyVhenNdHAPiqo1jhX/tzmCv2zPO3VxGiJOZ/AqhVHWVlEeAcDvmavN81S0/cKPAANtVz/cxV2ZVuJaVPOGvTF8lZ6MUM/seHQzMqBXxxWlz7ZQgsK3hgrC60BHHNqwlwrDvXOwriWRwgBrKQas2qKn8rOFs6N8UzTY+soPkUu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cX4kVKVfvvPgwRvQYeWghBFhRGzfkXS/TLPRPz7xpmsFbMJVMyvVhSV/PEIL6AWj9sm3s65S5Rbm4o7yiQTvAXCdQt0B6oPx2r+n7u2cOgqpAGhRnQvAsLlVKijz/DleZfXrC/qS2RT8rWtOt3EIUBS5PRlWcTWPma/+WxlSO3g8eOfpnPHUn45rYV/spjsRdoOO5J528m8n3sciOwFWYHru6gMta8h7z33GqVk1rZTQc5Opfx+heA= 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 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 --- 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 55649ad5b145..7979b56e5224 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 9dc6bdc1a6de..8b2082bdac08 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -43,12 +43,14 @@ #include #include #include +#include #include #include #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, bool force); +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); @@ -3463,6 +3466,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; @@ -4095,6 +4099,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); @@ -4149,6 +4154,68 @@ static unsigned long xswap_grow(struct swap_info_struct *si, bool force) 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, @@ -4371,6 +4438,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); @@ -4382,6 +4450,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