linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mauricio Faria de Oliveira <mfo@igalia.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
	Michal Hocko <mhocko@suse.com>, Vlastimil Babka <vbabka@suse.cz>,
	Oscar Salvador <osalvador@suse.de>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	kernel-dev@igalia.com
Subject: [PATCH RFC 7/9] mm/page_owner: add debugfs file 'swap_page_owner'
Date: Fri,  5 Dec 2025 20:17:19 -0300	[thread overview]
Message-ID: <20251205231721.104505-8-mfo@igalia.com> (raw)
In-Reply-To: <20251205231721.104505-1-mfo@igalia.com>

Add debugfs file /sys/kernel/debug/swap_page_owner to report the allocation
stack traces for pages in swap space (i.e., dump the XArray).

Now, it is possible to list the allocation stack traces both for pages in
memory and swap space, via /sys/kernel/debug/{page_owner,swap_page_owner}.

Signed-off-by: Mauricio Faria de Oliveira <mfo@igalia.com>
---
 mm/page_owner.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/mm/page_owner.c b/mm/page_owner.c
index 589aa1d7b9b6..e014eb7caf56 100644
--- a/mm/page_owner.c
+++ b/mm/page_owner.c
@@ -1103,6 +1103,77 @@ static const struct file_operations page_owner_fops = {
 	.llseek		= lseek_page_owner,
 };
 
+#ifdef CONFIG_SWAP_PAGE_OWNER
+static ssize_t print_swap_page_owner(char __user *buf, size_t count,
+				     swp_entry_t entry,
+				     struct swap_page_owner *spo)
+{
+	int ret;
+	char *kbuf;
+
+	count = min_t(size_t, count, PAGE_SIZE);
+	kbuf = kmalloc(count, GFP_KERNEL);
+	if (!kbuf)
+		return -ENOMEM;
+
+	ret = scnprintf(kbuf, count,
+			"Page allocated via pid %d, tgid %d (%s), ts %llu ns\n",
+			spo->pid, spo->tgid, spo->comm, spo->ts_nsec);
+
+	ret += scnprintf(kbuf + ret, count - ret,
+			 "SWP entry 0x%lx\n", entry.val);
+
+	ret += stack_depot_snprint(spo->handle, kbuf + ret, count - ret, 0);
+	if (ret >= count)
+		goto err;
+
+	ret += snprintf(kbuf + ret, count - ret, "\n");
+	if (ret >= count)
+		goto err;
+
+	if (copy_to_user(buf, kbuf, ret))
+		ret = -EFAULT;
+
+	kfree(kbuf);
+	return ret;
+
+err:
+	kfree(kbuf);
+	return -ENOMEM;
+}
+
+static ssize_t read_swap_page_owner(struct file *file, char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	swp_entry_t entry = { .val = *ppos };
+	void *spo;
+	XA_STATE(xa_state, &swap_page_owners, entry.val);
+
+	if (!static_branch_unlikely(&swap_page_owner_inited))
+		return -EINVAL;
+
+	xa_lock(&swap_page_owners);
+	xas_for_each(&xa_state, spo, ULONG_MAX) {
+
+		/* Resume after current index. */
+		entry.val = xa_state.xa_index;
+		*ppos = entry.val + 1;
+
+		xa_unlock(&swap_page_owners);
+		return print_swap_page_owner(buf, count, entry,
+					     (struct swap_page_owner *) spo);
+	}
+	xa_unlock(&swap_page_owners);
+
+	return 0;
+}
+
+static const struct file_operations swap_page_owner_fops = {
+	.read		= read_swap_page_owner,
+	.llseek		= lseek_page_owner,
+};
+#endif
+
 static void *stack_start(struct seq_file *m, loff_t *ppos)
 {
 	struct stack *stack;
@@ -1247,6 +1318,10 @@ static int __init pageowner_init(void)
 			    &page_owner_stack_fops);
 	debugfs_create_file("count_threshold", 0600, dir, NULL,
 			    &page_owner_threshold_fops);
+#ifdef CONFIG_SWAP_PAGE_OWNER
+	debugfs_create_file("swap_page_owner", 0400, NULL, NULL,
+			    &swap_page_owner_fops);
+#endif
 	return 0;
 }
 late_initcall(pageowner_init)
-- 
2.51.0



  parent reply	other threads:[~2025-12-05 23:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-05 23:17 [PATCH RFC 0/9] mm/page_owner: add support for pages in swap space Mauricio Faria de Oliveira
2025-12-05 23:17 ` [PATCH RFC 1/9] mm: add config option SWAP_PAGE_OWNER Mauricio Faria de Oliveira
2025-12-05 23:17 ` [PATCH RFC 2/9] mm/page_owner: add parameter option 'page_owner=on,swap' Mauricio Faria de Oliveira
2025-12-05 23:17 ` [PATCH RFC 3/9] mm/page_owner: add 'struct swap_page_owner' and helpers Mauricio Faria de Oliveira
2025-12-05 23:17 ` [PATCH RFC 4/9] mm/page_owner: add 'xarray swap_page_owners' " Mauricio Faria de Oliveira
2025-12-05 23:17 ` [PATCH RFC 5/9] mm/page_owner: add swap hooks Mauricio Faria de Oliveira
2025-12-05 23:17 ` [PATCH RFC 6/9] mm/page_owner: report '(swapped)' pages in debugfs file 'page_owner' Mauricio Faria de Oliveira
2025-12-05 23:17 ` Mauricio Faria de Oliveira [this message]
2025-12-05 23:17 ` [PATCH RFC 8/9] mm: call arch-specific swap hooks from generic swap hooks Mauricio Faria de Oliveira
2025-12-05 23:17 ` [PATCH RFC 9/9] mm: call page_owner " Mauricio Faria de Oliveira

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=20251205231721.104505-8-mfo@igalia.com \
    --to=mfo@igalia.com \
    --cc=akpm@linux-foundation.org \
    --cc=david@kernel.org \
    --cc=kernel-dev@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=mhocko@suse.com \
    --cc=osalvador@suse.de \
    --cc=vbabka@suse.cz \
    /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