From: sashiko-bot@kernel.org
To: "Baoquan He" <baoquan.he@linux.dev>, linux-mm@kvack.org
Cc: akpm@linux-foundation.org, david@kernel.org, corbet@lwn.net,
chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com,
hannes@cmpxchg.org, yosry@kernel.org, nphamcs@gmail.com,
baohua@kernel.org, youngjun.park@lge.com, kunwu.chan@gmail.com,
klarasmodin@gmail.com, "Baoquan He" <hebaoquan@kylinos.cn>
Subject: Re: [PATCH v8 12/14] mm, swap: add debugfs counters for xswap
Date: Fri, 04 Dec 2026 16:48:27 +0000 [thread overview]
Message-ID: <sashiko-outbox-250230@kernel.org> (raw)
In-Reply-To: <20261203164336.864691-13-baoquan.he@linux.dev>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] mm: xswap_debugfs_read() corrupts output on multi-read with
live stats
- [Medium] mm: xswap_debugfs_read() scans under xswap_lock without
cond_resched()
- [Low] mm: xswap_debugfs_add() permanently caches ERR_PTR in
xswap_debugfs_root
--
commit 661117d24147f38fb85952116650fbeb71a65516
Author: Baoquan He <hebaoquan@kylinos.cn>
mm, swap: add debugfs counters for xswap
This commit exposes xswap mapped cluster counts, free tail length, usage,
and grow statistics through /sys/kernel/debug/xswap/xswap.stats.
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 4d6a46346b935..b2d24e1a24f08 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
[ ... ]
> @@ -4156,6 +4161,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++;
> + }
> + }
[Severity: Medium]
Could this loop cause scheduling stalls on large xswap devices without a
cond_resched()?
Once nr_clusters_mapped has grown large and trailing clusters are freed,
reading xswap.stats walks millions of vmalloc-backed swap_cluster_info
entries in a tight loop. On CONFIG_PREEMPT_NONE or
CONFIG_PREEMPT_VOLUNTARY kernels this causes non-preemptible stalls.
Holding si->xswap_lock across the scan also blocks swap-out growth in
cluster_alloc_swap_entry() -> xswap_grow() -> xswap_map_clusters().
Calling cond_resched() periodically inside the loop would avoid the
scheduling stall.
> + 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);
> +}
[Severity: Medium]
Can reformatting live counters on every read() corrupt output on the
follow-up EOF read?
Standard readers such as cat issue a second read() at *ppos == len to
check for EOF. If any counter grows in decimal width between the two
calls, len increases, so simple_read_from_buffer() copies trailing bytes
from tmp instead of returning 0 and appends garbled output.
This also re-acquires si->xswap_lock and rescans the tail on every EOF
check. Using DEFINE_SHOW_ATTRIBUTE() would snapshot the stats once per
open and avoid both issues.
[ ... ]
> +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;
[Severity: Low]
Does this permanently cache an ERR_PTR in xswap_debugfs_root if
debugfs_create_dir() fails?
debugfs_create_dir() returns a non-NULL ERR_PTR on failure such as
-ENOMEM. On later swapoff/swapon cycles, !xswap_debugfs_root is false
and IS_ERR_OR_NULL() returns early, leaving xswap.stats unavailable
until reboot.
Resetting xswap_debugfs_root to NULL on error, as done for
si->xswap_debugfs below, would allow subsequent xswap_create() calls to
retry.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203164336.864691-1-baoquan.he@linux.dev?part=12
next prev parent reply other threads:[~2026-12-04 16:49 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 16:43 [PATCH v8 00/14] mm, swap: extendable swap devices (xswap phase I) Baoquan He
2026-12-03 16:43 ` [PATCH v8 01/14] mm/vmalloc: support vmalloc-backed VM_SPARSE areas Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 02/14] mm: xswap support for zswap Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 03/14] mm, swap: refactor free_swap_cluster_info to take swap_info_struct Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 04/14] mm, swap: use a VM_SPARSE cluster array for xswap devices Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 05/14] mm, swap: widen swap_info_struct max/pages to unsigned long Baoquan He
2026-12-03 16:43 ` [PATCH v8 06/14] mm, swap: create the xswap device from swapon(2) Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 07/14] mm, swap: refactor swapoff and add xswap_destroy Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 08/14] mm, swap: require zswap for xswap devices Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 09/14] mm, swap: let the command line set the xswap device size Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 10/14] mm, swap: add CONFIG_XSWAP_DEFAULT_MAX Baoquan He
2026-12-03 16:43 ` [PATCH v8 11/14] mm, swap: add xswap grow trigger on cluster allocation Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 12/14] mm, swap: add debugfs counters for xswap Baoquan He
2026-12-04 16:48 ` sashiko-bot [this message]
2026-12-03 16:43 ` [PATCH v8 13/14] mm, swap: do not give an xswap slot to a cgroup that cannot zswap Baoquan He
2026-12-04 16:48 ` sashiko-bot
2026-12-03 16:43 ` [PATCH v8 14/14] mm, swap: skip the overcommit probe when removing xswap Baoquan He
2026-12-04 16:48 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-250230@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baoquan.he@linux.dev \
--cc=chrisl@kernel.org \
--cc=corbet@lwn.net \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hebaoquan@kylinos.cn \
--cc=kasong@tencent.com \
--cc=klarasmodin@gmail.com \
--cc=kunwu.chan@gmail.com \
--cc=linux-mm@kvack.org \
--cc=nphamcs@gmail.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=urezki@gmail.com \
--cc=yosry@kernel.org \
--cc=youngjun.park@lge.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox