Skip to contentSkip to conversation

mm/page_owner: add support for pages in swap space

RFC PATCH In review Mauricio Faria de Oliveira 2 participants 12 messages Dec 5, 2025 – Dec 8, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover mm/page_owner: add support for pages in swap space

This series extends page_owner to track the allocation stack trace of pages in swap space as well (not just pages in memory), and maintain it over swap-out and swap-in (no longer lost/overriden at swap-out/swap-in).

It stores a copy of the allocation stack trace (plus some other fields) at swap-out and loads it back at swap-in, with an xarray by swp_entry_t and the swap hooks used for CONFIG_ARM64_MTE (memory tagging extension).

The refcounts of the allocation stack traces (i.e., number of base pages) for the initial allocation and the allocation at swap-in are fixed-up at swap-out and swap-in, to maintain the refcount on the initial allocation (for 'nr_base_pages' in '/sys/kernel/debug/page_owner_stacks/show_*').

The series is split into more, small patches on purpose, just for review (some patches fail to build with '-Werror=unused-function').
- Patches 1-4 add infrastructure.
- Patch 5 adds the key functionality.
- Patches 6-7 add reporting.
- Patches 8-9 add hooks for this.

It is basically tested (below); hopefully sufficient for an RFC in order to get comments on the idea, approach, design, problems and suggestions.

For future revisions: add pr_debug() and more error messages, and write Documentation; maybe rename functions with 'spo' prefix (swap page_owner). (I'll check a FIXME on the refcount fix-up due to an occasional WARN+OOPS mostly on shutdown; but that is just for 'nr_base_pages', without impact to the key functionality, and can be just commented out.)

Thanks,
Mauricio

# Testing

## Configuration

- next-20251205
- x86_64_defconfig
- CONFIG_PAGE_OWNER=n / CONFIG_SWAP_PAGE_OWNER=n (build-only)
- CONFIG_PAGE_OWNER=y / CONFIG_SWAP_PAGE_OWNER=n (build-only)
- CONFIG_PAGE_OWNER=y / CONFIG_SWAP_PAGE_OWNER=y (build/tests)

## Format

- Existing file (page_owner)

	# cat /sys/kernel/debug/page_owner
	...
	Page allocated via order 0, mask 0x140cca(GFP_HIGHUSER_MOVABLE|__GFP_COMP), pid 231, tgid 231 (dd), ts 13255570102 ns
	PFN 0xa35b type Movable Block 81 type Movable Flags 0x10000000002003c(referenced|uptodate|dirty|lru|swapbacked|node=0|zone=1)
	 get_page_from_freelist+0x13b9/0x1530
	 ...
	 entry_SYSCALL_64_after_hwframe+0x77/0x7f
	Charged to memcg session-1.scope
	...

- New file (swap_page_owner)

	# cat /sys/kernel/debug/swap_page_owner # new
	...
	Page allocated via pid 231, tgid 231 (dd), ts 13255313898 ns
	SWP entry 0x2e
	 get_page_from_freelist+0x13b9/0x1530
	 ...
	 entry_SYSCALL_64_after_hwframe+0x77/0x7f
	...

- Existing file (page_owner), when modified with '(swapped)$'

	# cat /sys/kernel/debug/page_owner
	...
	Page allocated via order 0, mask 0x140cca(GFP_HIGHUSER_MOVABLE|__GFP_COMP), pid 231, tgid 231 (dd), ts 13255427451 ns (swapped)

## Environment:

	# uname -r
	6.18.0-next-20251205-00009-g8042ea636379
	# mount | grep /tmpfs
	none on /tmpfs type tmpfs (rw,relatime,size=1048576k)
	none on /tmpfs-noswap type tmpfs (rw,relatime,size=1048576k,noswap)

## Before (page_owner without swap support)

	# grep -o 'page_owner=.*' /proc/cmdline
	page_owner=on

- Mem/Swap usage per page_owner (swap is not reported)

	# free -m
		       total        used        free      shared  buff/cache   available
	Mem:             458          68         367           0          33         389
	Swap:           1023           0        1023
	# echo $((458-367)) # total - free
	91
	# cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	91.2578 MiB
	# cat /sys/kernel/debug/swap_page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=1 } END { print PAGES*4096/1024^2 " MiB" }'
	cat: /sys/kernel/debug/swap_page_owner: Invalid argument
	0 MiB

- Use 256 MiB of pages allocated by 'dd' (compare with pages allocated by 'cat')

	# dd if=/dev/zero of=/tmpfs/file status=none bs=1M count=256
	# COMM=dd; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	256.559 MiB

	# COMM=cat; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	0.464844 MiB

- Move that 256 MiB of pages from memory to swap (use 'DD' to differentiate from 'dd's memory)

	# ln -sf $(which dd) ./DD
	# ./DD if=/dev/zero of=/tmpfs-noswap/file status=none bs=1M count=384
	# free -m
		       total        used        free      shared  buff/cache   available
	Mem:             458         445           6         388         405          12
	Swap:           1023         264         759

- Mem/Swap usage per page_owner (swap is not reported)

	# echo $((458-6)) # total - free
	452
	# cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	450.445 MiB
	# cat /sys/kernel/debug/swap_page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=1 } END { print PAGES*4096/1024^2 " MiB" }'
	cat: /sys/kernel/debug/swap_page_owner: Invalid argument
	0 MiB

- Move that 256 MiB of pages back from swap to memory

	# rm /tmpfs-noswap/file
	# cat /tmpfs/file >/dev/null

- The 256 MiB of pages is _now_ allocated by 'cat' (instead of 'dd')

	# COMM=dd; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	1.66016 MiB

	# COMM=cat; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	258.309 MiB

## After (page_owner with swap support)

	# grep -o 'page_owner=.*' /proc/cmdline
	page_owner=on,swap

- Mem/Swap usage per page_owner (swap is reported now)

	# free -m
		       total        used        free      shared  buff/cache   available
	Mem:             458          68         360           0          40         389
	Swap:           1023           0        1023
	# echo $((458-360)) # total - free
	98
	# cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	97 MiB
	# cat /sys/kernel/debug/swap_page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=1 } END { print PAGES*4096/1024^2 " MiB" }'
	0 MiB

- Use 256 MiB of pages allocated by 'dd' (compare with pages allocated by 'cat')

	# dd if=/dev/zero of=/tmpfs/file status=none bs=1M count=256
	# COMM=dd; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	256.504 MiB

	# COMM=cat; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	0.175781 MiB

- Move that 256 MiB of pages from memory to swap (use 'DD' to differentiate from 'dd's memory)

	# ln -sf $(which dd) ./DD
	# ./DD if=/dev/zero of=/tmpfs-noswap/file status=none bs=1M count=384

- Mem/Swap usage per page_owner (swap is reported now)

	# free -m
		       total        used        free      shared  buff/cache   available
	Mem:             458         435           6         384         412          22
	Swap:           1023         278         745
	# echo $((458-6)) # total - free
	452
	# cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	449.258 MiB
	# cat /sys/kernel/debug/swap_page_owner | awk -F '[ ,]' '/^Page allocated/ { PAGES+=1 } END { print PAGES*4096/1024^2 " MiB" }'
	278.793 MiB

- Move that 256 MiB of pages back from swap to memory

	# rm /tmpfs-noswap/file
	# cat /tmpfs/file >/dev/null

- The 256 MiB of pages is _still_ allocated by 'dd' (instead of 'cat' as above)

	# COMM=dd; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	256.445 MiB

	# COMM=cat; cat /sys/kernel/debug/page_owner | awk -F '[ ,]' '/^Page allocated .* \('${COMM}'\)/ { PAGES+=2^$5 } END { print PAGES*4096/1024^2 " MiB" }'
	7.80859 MiB

Mauricio Faria de Oliveira (9):
mm: add config option SWAP_PAGE_OWNER
mm/page_owner: add parameter option 'page_owner=on,swap' mm/page_owner: add 'struct swap_page_owner' and helpers mm/page_owner: add 'xarray swap_page_owners' and helpers mm/page_owner: add swap hooks
mm/page_owner: report '(swapped)' pages in debugfs file 'page_owner' mm/page_owner: add debugfs file 'swap_page_owner'
mm: call arch-specific swap hooks from generic swap hooks
mm: call page_owner swap hooks from generic swap hooks

 include/linux/page_owner.h |  50 ++++++
 include/linux/pgtable.h    |  30 ++++
 mm/Kconfig.debug           |   9 +
 mm/memory.c                |   2 +-
 mm/page_io.c               |   2 +-
 mm/page_owner.c            | 338 ++++++++++++++++++++++++++++++++++++-
 mm/shmem.c                 |   2 +-
 mm/swapfile.c              |   6 +-
 8 files changed, 427 insertions(+), 12 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

RFC PATCH 1/9 mm: add config option SWAP_PAGE_OWNER

Add config option for page_owner support for pages in swap space.

---
 mm/Kconfig.debug | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/Kconfig.debug +9 -0
index 7638d75b27db..60bc0b6c3d69 100644
@@ -119,6 +119,15 @@ config PAGE_OWNER
 
 	  If unsure, say N.
 
+config SWAP_PAGE_OWNER
+	bool "Track page owner in swap space (EXPERIMENTAL)"
+	depends on PAGE_OWNER
+	help
+	  This also keeps track of what call chain is the owner of a page
+	  in swap space, maintaining the initial call chain over swap-out
+	  and swap-in (otherwise lost at swap-out and reset at swap-in).
+	  You should pass the "page_owner=on,swap" parameter to enable it.
+
 config PAGE_TABLE_CHECK
 	bool "Check for invalid mappings in user page tables"
 	depends on ARCH_SUPPORTS_PAGE_TABLE_CHECK
Signature · 1 line
2.51.0
#2 lore raw

RFC PATCH 2/9 mm/page_owner: add parameter option 'page_owner=on,swap'

In reply to Mauricio Faria de Oliveira

CONFIG_SWAP_PAGE_OWNER will introduce a behavior change in page_owner on systems with swap space. Thus, add the 'swap' option to the 'page_owner' parameter to enable it ('page_owner=on,swap').

Currently, the allocation stack trace of a page is lost at swap-out (the page is freed) and reset at swap-in (a new page is allocated).

With the new feature, if this option is enabled, the (initial) allocation stack trace of a page is maintained over swap-out and swap-in, and the refcounts for the initial and swap-in allocation stack traces are adjusted.

(Note: 'swap_page_owner_enabled' is parsed before 'page_owner_enabled', so 'page_owner=off,swap' could set the former but not the latter, but this is a nop as then init_page_owner() returns early and doesn't enable anything.)

---
 mm/page_owner.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
mm/page_owner.c +21 -1
index a70245684206..a27958872b07 100644
@@ -57,6 +57,11 @@ struct stack_print_ctx {
 static bool page_owner_enabled __initdata;
 DEFINE_STATIC_KEY_FALSE(page_owner_inited);
 
+#ifdef CONFIG_SWAP_PAGE_OWNER
+static bool swap_page_owner_enabled __initdata;
+DEFINE_STATIC_KEY_FALSE(swap_page_owner_inited);
+#endif
+
 static depot_stack_handle_t dummy_handle;
 static depot_stack_handle_t failure_handle;
 static depot_stack_handle_t early_handle;
@@ -81,7 +86,18 @@ static inline void unset_current_in_page_owner(void)
 
 static int __init early_page_owner_param(char *buf)
 {
-	int ret = kstrtobool(buf, &page_owner_enabled);
+	int ret;
+
+#ifdef CONFIG_SWAP_PAGE_OWNER
+	char *swap = strchr(buf, ',');
+
+	if (swap) {
+		*swap++ = '\0';
+		if (!strcmp(swap, "swap"))
+			swap_page_owner_enabled = true;
+	}
+#endif
+	ret = kstrtobool(buf, &page_owner_enabled);
 
 	if (page_owner_enabled)
 		stack_depot_request_early_init();
@@ -138,6 +154,10 @@ static __init void init_page_owner(void)
 	dummy_stack.next = &failure_stack;
 	stack_list = &dummy_stack;
 	static_branch_enable(&page_owner_inited);
+#ifdef CONFIG_SWAP_PAGE_OWNER
+	if (swap_page_owner_enabled)
+		static_branch_enable(&swap_page_owner_inited);
+#endif
 }
 
 struct page_ext_operations page_owner_ops = {
Signature · 1 line
2.51.0
#3 lore raw

RFC PATCH 3/9 mm/page_owner: add 'struct swap_page_owner' and helpers

In reply to Mauricio Faria de Oliveira

Add 'struct swap_page_owner' to store the (initial) allocation stack trace and some attributes, plus helpers to allocate, free, and copy it to/from 'struct page_owner'.

---
 mm/page_owner.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
mm/page_owner.c +40 -0
index a27958872b07..19ccbda1c2a4 100644
@@ -60,6 +60,14 @@ DEFINE_STATIC_KEY_FALSE(page_owner_inited);
 #ifdef CONFIG_SWAP_PAGE_OWNER
 static bool swap_page_owner_enabled __initdata;
 DEFINE_STATIC_KEY_FALSE(swap_page_owner_inited);
+
+struct swap_page_owner {
+	depot_stack_handle_t handle;
+	u64 ts_nsec;
+	char comm[TASK_COMM_LEN];
+	pid_t pid;
+	pid_t tgid;
+};
 #endif
 
 static depot_stack_handle_t dummy_handle;
@@ -442,6 +450,38 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old)
 	rcu_read_unlock();
 }
 
+#ifdef CONFIG_SWAP_PAGE_OWNER
+static void *alloc_swap_page_owner(void)
+{
+	return kmalloc(sizeof(struct swap_page_owner), GFP_KERNEL);
+}
+
+static void free_swap_page_owner(void *spo)
+{
+	kfree(spo);
+}
+
+static void copy_to_swap_page_owner(struct swap_page_owner *spo,
+				    struct page_owner *page_owner)
+{
+	spo->handle = page_owner->handle;
+	spo->ts_nsec = page_owner->ts_nsec;
+	spo->pid = page_owner->pid;
+	spo->tgid = page_owner->tgid;
+	strscpy(spo->comm, page_owner->comm, sizeof(page_owner->comm));
+}
+
+static void copy_from_swap_page_owner(struct page_owner *page_owner,
+				      struct swap_page_owner *spo)
+{
+	page_owner->handle = spo->handle;
+	page_owner->ts_nsec = spo->ts_nsec;
+	page_owner->pid = spo->pid;
+	page_owner->tgid = spo->tgid;
+	strscpy(page_owner->comm, spo->comm, sizeof(page_owner->comm));
+}
+#endif
+
 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
 				       pg_data_t *pgdat, struct zone *zone)
 {
Signature · 1 line
2.51.0
#4 lore raw

RFC PATCH 4/9 mm/page_owner: add 'xarray swap_page_owners' and helpers

In reply to Mauricio Faria de Oliveira

Add 'xarray swap_page_owners' to index 'struct swap_page_owner' objects by key 'swp_entry_t', and helpers to store, load, and erase.

This is used to track the allocation stack trace of pages in swap space.

---
 mm/page_owner.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
mm/page_owner.c +45 -0
index 19ccbda1c2a4..5cd7de1f8023 100644
@@ -68,6 +68,7 @@ struct swap_page_owner {
 	pid_t pid;
 	pid_t tgid;
 };
+static DEFINE_XARRAY(swap_page_owners);
 #endif
 
 static depot_stack_handle_t dummy_handle;
@@ -461,6 +462,50 @@ static void free_swap_page_owner(void *spo)
 	kfree(spo);
 }
 
+static int store_swap_page_owner(struct swap_page_owner *spo, swp_entry_t entry)
+{
+	/* lookup the swap entry.val from the page */
+	void *ret = xa_store(&swap_page_owners, entry.val, spo, GFP_KERNEL);
+
+	if (WARN(xa_is_err(ret),
+	    "page_owner: swap: swp_entry not stored (%lu)\n", entry.val)) {
+		free_swap_page_owner(spo);
+		return xa_err(ret);
+	} else if (ret) {
+		/* Entry is being replaced, free the old entry */
+		free_swap_page_owner(ret);
+	}
+
+	return 0;
+}
+
+static void *load_swap_page_owner(swp_entry_t entry)
+{
+	void *spo = xa_load(&swap_page_owners, entry.val);
+
+	if (WARN(!spo,
+	    "page_owner: swap: swp_entry not loaded (%lu)\n", entry.val))
+		return NULL;
+
+	return spo;
+}
+
+static void erase_swap_page_owner(swp_entry_t entry, bool lock)
+{
+	void *spo;
+
+	if (lock)
+		spo = xa_erase(&swap_page_owners, entry.val);
+	else
+		spo = __xa_erase(&swap_page_owners, entry.val);
+
+	if (WARN(!spo,
+	    "page_owner: swap: swp_entry not erased (%lu)\n", entry.val))
+		return;
+
+	free_swap_page_owner(spo);
+}
+
 static void copy_to_swap_page_owner(struct swap_page_owner *spo,
 				    struct page_owner *page_owner)
 {
Signature · 1 line
2.51.0
#5 lore raw

RFC PATCH 5/9 mm/page_owner: add swap hooks

In reply to Mauricio Faria de Oliveira

Add the swap hooks 'prepare_to_swap()' (before swap-out and page-free), 'swap_restore()' (after page-alloc and swap-in), and 'swap_invalidate()' for page and area.

The first two hooks are the core of the new functionality. They store the (initial) allocation stack trace at swap-out and load it at swap-in, in order to 'maintain' the allocation stack trace over swap-out/swap-in.

The refcounts for the initial allocation and the allocation at swap-in are adjusted/fixed-up (incremented and decremented, respectively), as the initial allocation is decremented at swap-out (page free) later, and the swap-in allocation is incremented at swap-in (page alloc) earlier.

This is based on the swap hooks implementation for memory tags on arm64 ('arch/arm64/mm/mteswap.c'; thanks!)

---
 include/linux/page_owner.h |  49 +++++++++++++++
 mm/page_owner.c            | 120 +++++++++++++++++++++++++++++++++++++
 2 files changed, 169 insertions(+)
include/linux/page_owner.h +49 -0
index 3328357f6dba..cd95aacceba7 100644
@@ -75,4 +75,53 @@ static inline void dump_page_owner(const struct page *page)
 {
 }
 #endif /* CONFIG_PAGE_OWNER */
+
+#ifdef CONFIG_SWAP_PAGE_OWNER
+extern struct static_key_false swap_page_owner_inited;
+
+extern int __page_owner_prepare_to_swap(struct folio *folio);
+extern void __page_owner_swap_restore(swp_entry_t entry, struct folio *folio);
+extern void __page_owner_swap_invalidate_page(int type, pgoff_t offset);
+extern void __page_owner_swap_invalidate_area(int type);
+
+static inline int page_owner_prepare_to_swap(struct folio *folio)
+{
+	if (static_branch_unlikely(&swap_page_owner_inited))
+		return __page_owner_prepare_to_swap(folio);
+
+	return 0;
+}
+
+static inline void page_owner_swap_restore(swp_entry_t entry, struct folio *folio)
+{
+	if (static_branch_unlikely(&swap_page_owner_inited))
+		return __page_owner_swap_restore(entry, folio);
+}
+
+static inline void page_owner_swap_invalidate_page(int type, pgoff_t offset)
+{
+	if (static_branch_unlikely(&swap_page_owner_inited))
+		return __page_owner_swap_invalidate_page(type, offset);
+}
+
+static inline void page_owner_swap_invalidate_area(int type)
+{
+	if (static_branch_unlikely(&swap_page_owner_inited))
+		return __page_owner_swap_invalidate_area(type);
+}
+#else
+static inline int page_owner_prepare_to_swap(struct folio *folio)
+{
+	return 0;
+}
+static inline void page_owner_swap_restore(swp_entry_t entry, struct folio *folio)
+{
+}
+static inline void page_owner_swap_invalidate_page(int type, pgoff_t offset)
+{
+}
+static inline void page_owner_swap_invalidate_area(int type)
+{
+}
+#endif /* CONFIG_SWAP_PAGE_OWNER */
 #endif /* __LINUX_PAGE_OWNER_H */
mm/page_owner.c +120 -0

126 lines not on this page · raw message

Signature · 1 line
2.51.0
#6 lore raw

Re: [PATCH RFC 5/9] mm/page_owner: add swap hooks

kernel test robot noticed the following build warnings:

Hi Mauricio,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Mauricio-Faria-de-Oliveira/mm-add-config-option-SWAP_PAGE_OWNER/20251206-072021
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20251205231721.104505-6-mfo%40igalia.com
patch subject: [PATCH RFC 5/9] mm/page_owner: add swap hooks
config: x86_64-allmodconfig (https://download.01.org/0day-ci/archive/20251208/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251208/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> mm/page_owner.c:593:7: warning: variable 'i' set but not used [-Wunused-but-set-variable]
     593 |         long i = 0, nr_pages = folio_nr_pages(folio);
         |              ^

1 warning generated.

vim +/i +593 mm/page_owner.c

584
585 /* Load the initial stack information from xarray to page_owner. */
586 void __page_owner_swap_restore(swp_entry_t entry, struct folio *folio)
587 {
588 struct page_ext_iter iter;
589 struct page_ext *page_ext;
590 struct page_owner *page_owner;
591 struct swap_page_owner *spo;
592 depot_stack_handle_t handle = 0;

 > 593		long i = 0, nr_pages = folio_nr_pages(folio);

594
595 rcu_read_lock();
596 for_each_page_ext(&folio->page, nr_pages, page_ext, iter) {
597 spo = (struct swap_page_owner *) load_swap_page_owner(entry);
598 if (!spo) {
599 rcu_read_unlock();
600 return;
601 }
602
603 page_owner = get_page_owner(page_ext);
604 copy_from_swap_page_owner(page_owner, spo);
605
606 if (!handle)
607 handle = page_owner->handle;
608 i++;
609 entry.val++;
610 }
611 rcu_read_unlock();
612
613 /*
614 * Fix-up: decrement refcount of the swap-in allocation.
615 * It was incremented by the page-alloc at swap-in.
616 * (early_handle: see __reset_page_owner().)
617 *
618 * FIXME(mfo): 'dec_stack_record_count: refcount went to 0 ...'
619 * with stack_depot oops is hit occasionaly on tests or shutdown.
620 */
621 if (handle != early_handle)
622 dec_stack_record_count(handle, nr_pages);
623 }
624

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

Re: [PATCH RFC 5/9] mm/page_owner: add swap hooks

Quoted 4 lines · kernel test robot
Quoted 1 line
Quoted 1 line

mm/page_owner.c:593:7: warning: variable 'i' set but not used [-Wunused-but-set-variable]

     593 |         long i = 0, nr_pages = folio_nr_pages(folio);
         |              ^

1 warning generated.

Queued for v2.

Signature · 1 line
Mauricio
#8 lore raw

RFC PATCH 6/9 mm/page_owner: report '(swapped)' pages in debugfs file 'page_owner'

In reply to Mauricio Faria de Oliveira

Report whether a page has been swapped in /sys/kernel/debug/page_owner. This is helpful for stats, verifying the functionality, and debugging.

Add the 'swapped' field to 'struct page_owner' to track whether the page has been swapped-in (and had allocation stack and some fields overridden).

Clear it via __set_page_owner() (page allocation), __reset_page_owner() (page free), and init_pages_in_zone() (init). Set it in the swap_restore() swap hook (swap-in).

---
 mm/page_owner.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
mm/page_owner.c +31 -5
index d256f58deca4..589aa1d7b9b6 100644
@@ -34,6 +34,9 @@ struct page_owner {
 	pid_t tgid;
 	pid_t free_pid;
 	pid_t free_tgid;
+#ifdef CONFIG_SWAP_PAGE_OWNER
+	bool swapped;
+#endif
 };
 
 struct stack {
@@ -275,7 +278,8 @@ static inline void __update_page_owner_handle(struct page *page,
 					      unsigned short order,
 					      gfp_t gfp_mask,
 					      short last_migrate_reason, u64 ts_nsec,
-					      pid_t pid, pid_t tgid, char *comm)
+					      pid_t pid, pid_t tgid, char *comm,
+					      bool __maybe_unused swapped)
 {
 	struct page_ext_iter iter;
 	struct page_ext *page_ext;
@@ -293,6 +297,9 @@ static inline void __update_page_owner_handle(struct page *page,
 		page_owner->ts_nsec = ts_nsec;
 		strscpy(page_owner->comm, comm,
 			sizeof(page_owner->comm));
+#ifdef CONFIG_SWAP_PAGE_OWNER
+		page_owner->swapped = swapped;
+#endif
 		__set_bit(PAGE_EXT_OWNER, &page_ext->flags);
 		__set_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
 	}
@@ -316,6 +323,9 @@ static inline void __update_page_owner_free_handle(struct page *page,
 		if (handle) {
 			__clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags);
 			page_owner->free_handle = handle;
+#ifdef CONFIG_SWAP_PAGE_OWNER
+			page_owner->swapped = false;
+#endif
 		}
 		page_owner->free_ts_nsec = free_ts_nsec;
 		page_owner->free_pid = current->pid;
@@ -370,7 +380,7 @@ noinline void __set_page_owner(struct page *page, unsigned short order,
 	handle = save_stack(gfp_mask);
 	__update_page_owner_handle(page, handle, order, gfp_mask, -1,
 				   ts_nsec, current->pid, current->tgid,
-				   current->comm);
+				   current->comm, false);
 	inc_stack_record_count(handle, gfp_mask, 1 << order);
 }
 
@@ -428,7 +438,13 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old)
 				   old_page_owner->order, old_page_owner->gfp_mask,
 				   old_page_owner->last_migrate_reason,
 				   old_page_owner->ts_nsec, old_page_owner->pid,
-				   old_page_owner->tgid, old_page_owner->comm);
+				   old_page_owner->tgid, old_page_owner->comm,
+#ifdef CONFIG_SWAP_PAGE_OWNER
+				   old_page_owner->swapped
+#else
+				   false
+#endif
+				  );
 	/*
 	 * Do not proactively clear PAGE_EXT_OWNER{_ALLOCATED} bits as the folio
 	 * will be freed after migration. Keep them until then as they may be
@@ -602,6 +618,7 @@ void __page_owner_swap_restore(swp_entry_t entry, struct folio *folio)
 
 		page_owner = get_page_owner(page_ext);
 		copy_from_swap_page_owner(page_owner, spo);
+		page_owner->swapped = true;
 
 		if (!handle)
 			handle = page_owner->handle;
@@ -783,12 +800,20 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn,
 		return -ENOMEM;
 
 	ret = scnprintf(kbuf, count,
-			"Page allocated via order %u, mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu ns\n",
+			"Page allocated via order %u, mask %#x(%pGg), pid %d, tgid %d (%s), ts %llu ns",
 			page_owner->order, page_owner->gfp_mask,
 			&page_owner->gfp_mask, page_owner->pid,
 			page_owner->tgid, page_owner->comm,
 			page_owner->ts_nsec);
 
+#ifdef CONFIG_SWAP_PAGE_OWNER
+	if (static_branch_unlikely(&swap_page_owner_inited) &&
+	    page_owner->swapped) {
+		ret += scnprintf(kbuf + ret, count - ret, " (swapped)");
+	}
+#endif
+	ret += scnprintf(kbuf + ret, count - ret, "\n");
+
 	/* Print information relevant to grouping pages by mobility */
 	pageblock_mt = get_pageblock_migratetype(page);
 	page_mt  = gfp_migratetype(page_owner->gfp_mask);
@@ -1052,7 +1077,8 @@ static void init_pages_in_zone(struct zone *zone)
 			/* Found early allocated page */
 			__update_page_owner_handle(page, early_handle, 0, 0,
 						   -1, local_clock(), current->pid,
-						   current->tgid, current->comm);
+						   current->tgid, current->comm,
+						   false);
 			count++;
 ext_put_continue:
 			page_ext_put(page_ext);
Signature · 1 line
2.51.0
#9 lore raw

RFC PATCH 7/9 mm/page_owner: add debugfs file 'swap_page_owner'

In reply to Mauricio Faria de Oliveira

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}.

---
 mm/page_owner.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
mm/page_owner.c +75 -0
index 589aa1d7b9b6..e014eb7caf56 100644
@@ -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)
Signature · 1 line
2.51.0
#10 lore raw

RFC PATCH 8/9 mm: call arch-specific swap hooks from generic swap hooks

In reply to Mauricio Faria de Oliveira

Add generic swap hooks that just call the arch-specific ones (e.g., arm64).

---
 include/linux/pgtable.h | 20 ++++++++++++++++++++
 mm/memory.c             |  2 +-
 mm/page_io.c            |  2 +-
 mm/shmem.c              |  2 +-
 mm/swapfile.c           |  6 +++---
 5 files changed, 26 insertions(+), 6 deletions(-)
include/linux/pgtable.h +20 -0
index 652f287c1ef6..23d30afe439e 100644
@@ -1220,6 +1220,26 @@ static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio)
 }
 #endif
 
+static inline int hook_prepare_to_swap(struct folio *folio)
+{
+	return arch_prepare_to_swap(folio);
+}
+
+static inline void hook_swap_invalidate_page(int type, pgoff_t offset)
+{
+	arch_swap_invalidate_page(type, offset);
+}
+
+static inline void hook_swap_invalidate_area(int type)
+{
+	arch_swap_invalidate_area(type);
+}
+
+static inline void hook_swap_restore(swp_entry_t entry, struct folio *folio)
+{
+	arch_swap_restore(entry, folio);
+}
+
 #ifndef __HAVE_ARCH_MOVE_PTE
 #define move_pte(pte, old_addr, new_addr)	(pte)
 #endif
mm/memory.c +1 -1
index 2a55edc48a65..6a5a5bd3eeed 100644
@@ -4945,7 +4945,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
 	 * when reading from swap. This metadata may be indexed by swap entry
 	 * so this must be called before swap_free().
 	 */
-	arch_swap_restore(folio_swap(entry, folio), folio);
+	hook_swap_restore(folio_swap(entry, folio), folio);
 
 	/*
 	 * Remove the swap entry and conditionally try to free up the swapcache.
mm/page_io.c +1 -1
index 3c342db77ce3..b62c53a86a59 100644
@@ -248,7 +248,7 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug)
 	 * Arch code may have to preserve more data than just the page
 	 * contents, e.g. memory tags.
 	 */
-	ret = arch_prepare_to_swap(folio);
+	ret = hook_prepare_to_swap(folio);
 	if (ret) {
 		folio_mark_dirty(folio);
 		goto out_unlock;
mm/shmem.c +1 -1
index 3f194c9842a8..ef53c14156fb 100644
@@ -2395,7 +2395,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,
 	 * Some architectures may have to restore extra metadata to the
 	 * folio after reading from swap.
 	 */
-	arch_swap_restore(folio_swap(swap, folio), folio);
+	hook_swap_restore(folio_swap(swap, folio), folio);
 
 	if (shmem_should_replace_folio(folio, gfp)) {
 		error = shmem_replace_folio(&folio, gfp, info, index, vma);
mm/swapfile.c +3 -3
index 46d2008e4b99..e156f5cb6e9f 100644
@@ -1270,7 +1270,7 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
 	else
 		swap_slot_free_notify = NULL;
 	while (offset <= end) {
-		arch_swap_invalidate_page(si->type, offset);
+		hook_swap_invalidate_page(si->type, offset);
 		if (swap_slot_free_notify)
 			swap_slot_free_notify(si->bdev, offset);
 		offset++;
@@ -2196,7 +2196,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
 	 * when reading from swap. This metadata may be indexed by swap entry
 	 * so this must be called before swap_free().
 	 */
-	arch_swap_restore(folio_swap(entry, folio), folio);
+	hook_swap_restore(folio_swap(entry, folio), folio);
 
 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
@@ -2931,7 +2931,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
 	p->cluster_info = NULL;
 	spin_unlock(&p->lock);
 	spin_unlock(&swap_lock);
-	arch_swap_invalidate_area(p->type);
+	hook_swap_invalidate_area(p->type);
 	zswap_swapoff(p->type);
 	mutex_unlock(&swapon_mutex);
 	kfree(p->global_cluster);
Signature · 1 line
2.51.0
#11 lore raw

RFC PATCH 9/9 mm: call page_owner swap hooks from generic swap hooks

In reply to Mauricio Faria de Oliveira

Finally, call the page_owner swap hooks from the generic swap hooks. This plugs the new functionality into the kernel.

Add a missing include in 'page_owner.h' to fix build errors in files that include 'pgtable.h' (thus now 'page_owner.h') without an earlier include that provides 'pg_data_t':

In file included from .../include/linux/pgtable.h:19,
from .../arch/x86/boot/startup/map_kernel.c:7:
.../include/linux/page_owner.h:20:41: error: unknown type name ‘pg_data_t’

---
 include/linux/page_owner.h |  1 +
 include/linux/pgtable.h    | 12 +++++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
include/linux/page_owner.h +1 -0
index cd95aacceba7..d7029d1e41de 100644
@@ -3,6 +3,7 @@
 #define __LINUX_PAGE_OWNER_H
 
 #include <linux/jump_label.h>
+#include <linux/mmzone.h>
 
 #ifdef CONFIG_PAGE_OWNER
 extern struct static_key_false page_owner_inited;
include/linux/pgtable.h +11 -1
index 23d30afe439e..ae397f5884d5 100644
@@ -16,6 +16,7 @@
 #include <linux/errno.h>
 #include <asm-generic/pgtable_uffd.h>
 #include <linux/page_table_check.h>
+#include <linux/page_owner.h>
 
 #if 5 - defined(__PAGETABLE_P4D_FOLDED) - defined(__PAGETABLE_PUD_FOLDED) - \
 	defined(__PAGETABLE_PMD_FOLDED) != CONFIG_PGTABLE_LEVELS
@@ -1222,22 +1223,31 @@ static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio)
 
 static inline int hook_prepare_to_swap(struct folio *folio)
 {
-	return arch_prepare_to_swap(folio);
+	int ret;
+
+	ret = arch_prepare_to_swap(folio);
+	if (ret)
+		return ret;
+
+	return page_owner_prepare_to_swap(folio);
 }
 
 static inline void hook_swap_invalidate_page(int type, pgoff_t offset)
 {
 	arch_swap_invalidate_page(type, offset);
+	page_owner_swap_invalidate_page(type, offset);
 }
 
 static inline void hook_swap_invalidate_area(int type)
 {
 	arch_swap_invalidate_area(type);
+	page_owner_swap_invalidate_area(type);
 }
 
 static inline void hook_swap_restore(swp_entry_t entry, struct folio *folio)
 {
 	arch_swap_restore(entry, folio);
+	page_owner_swap_restore(entry, folio);
 }
 
 #ifndef __HAVE_ARCH_MOVE_PTE
Signature · 1 line
2.51.0