Skip to contentSkip to conversation

mm, page_owner: make the owner in page owner clearer

RFC PATCH In review Jeff Xie 4 participants 18 messages Nov 9, 2023

Subsystems (7)
lore
Share & tools
lore

Jeff Xie

author
#0 lore raw

cover mm, page_owner: make the owner in page owner clearer

The current page_owner function can display the allocate and free process of a page, but there is no accurate information corresponding to the page.

This patchset can make the slab page to know which kmem cache the page is requested from.

For the file page, it can know the corresponding file inode,
if this file page is mapped to the process address space, we can know the virtual address corresponding to a certain process.

For the anon page, since it must be mapped to the process address space, at this time, we can also know the virtual address corresponding to a certain process.

To implement it, we only need to add a callback function in the struct page_owner, this let the slab layer or the anon/file handler layer or any other memory-allocated layers to implement what they would like to tell.

For example, for slab page, a line is added to the result of page_owner

added: "SLAB_PAGE slab_name:kmalloc-32"

Page allocated via order 0, mask 0x12cc0(GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY), pid 1, tgid 1 (swapper/0), ts 340615384 ns SLAB_PAGE slab_name:kmalloc-32
PFN 0x4be0 type Unmovable Block 37 type Unmovable Flags 0x1fffc0000000800(slab|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 allocate_slab+0x31f/0x410
 ___slab_alloc+0x3e8/0x850
 __kmem_cache_alloc_node+0x111/0x2b0
 kmalloc_trace+0x29/0x90
 iommu_setup_dma_ops+0x299/0x470
 bus_iommu_probe+0xe1/0x150
 iommu_device_register+0xad/0x120
 intel_iommu_init+0xe3a/0x1260
 pci_iommu_init+0x12/0x40
 do_one_initcall+0x45/0x210
 kernel_init_freeable+0x1a4/0x2e0
 kernel_init+0x1a/0x1c0

added: "SLAB_PAGE slab_name:mm_struct"

Page allocated via order 3, mask 0xd20c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), \
pid 86, tgid 86 (linuxrc), ts 1121118666 ns
SLAB_PAGE slab_name:mm_struct
PFN 0x6388 type Unmovable Block 49 type Unmovable Flags 0x1fffc0000000840(slab|head|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 allocate_slab+0x31f/0x410
 ___slab_alloc+0x3e8/0x850
 kmem_cache_alloc+0x2b8/0x2f0
 mm_alloc+0x1a/0x50
 alloc_bprm+0x8a/0x300
 do_execveat_common.isra.0+0x68/0x240
 __x64_sys_execve+0x37/0x50
 do_syscall_64+0x42/0xf0
 entry_SYSCALL_64_after_hwframe+0x6e/0x76

For the pid 98:
[root@JeffXie ]# cat /proc/98/maps
00400000-00401000 r--p 00000000 fd:00 1954 /test/mm/a.out

00401000-00499000 r-xp 00001000 fd:00 1954      /test/mm/a.out
00499000-004c2000 r--p 00099000 fd:00 1954      /test/mm/a.out
004c2000-004c6000 r--p 000c1000 fd:00 1954      /test/mm/a.out
004c6000-004c9000 rw-p 000c5000 fd:00 1954      /test/mm/a.out
004c9000-004ce000 rw-p 00000000 00:00 0 
01d97000-01db9000 rw-p 00000000 00:00 0                 [heap]
7f1588fc8000-7f1588fc9000 rw-p 00000000 fd:00 1945      /a.txt
7ffda207a000-7ffda209b000 rw-p 00000000 00:00 0         [stack]
7ffda2152000-7ffda2156000 r--p 00000000 00:00 0         [vvar]
7ffda2156000-7ffda2158000 r-xp 00000000 00:00 0         [vdso]

ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]

For file page:

added: "FILE_PAGE dev 253:0 ino:1954 index:0xc1 mapcount:1 refcount:2 0x4c2000 - 0x4c3000"

Page allocated via order 0, mask 0x152c4a(GFP_NOFS|__GFP_HIGHMEM|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_HARDWALL|__GFP_MOVABLE),\
pid 98, tgid 98 (a.out), ts 28441476044 ns
FILE_PAGE dev 253:0 ino:1954 index:0xc1 mapcount:1 refcount:2 0x4c2000 - 0x4c3000
PFN 0x5be8e type Movable Block 735 type Movable Flags 0x1fffc0000020028(uptodate|lru|mappedtodisk|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 folio_alloc+0x18/0x50
 page_cache_ra_unbounded+0x9b/0x1a0
 filemap_fault+0x5f7/0xc20
 __do_fault+0x31/0xc0
 __handle_mm_fault+0x1333/0x1760
 handle_mm_fault+0xbc/0x2f0
 do_user_addr_fault+0x1f8/0x5e0
 exc_page_fault+0x73/0x170
 asm_exc_page_fault+0x26/0x30

Charged to memcg /

For anon page:

added: "ANON_PAGE address 0x4c4000"

Page allocated via order 0, mask 0x140cca(GFP_HIGHUSER_MOVABLE|__GFP_COMP), pid 98, tgid 98 (a.out), ts 28442066180 ns ANON_PAGE address 0x4c4000
PFN 0x2c3db type Movable Block 353 type Movable Flags 0x1fffc00000a0028(uptodate|lru|mappedtodisk|swapbacked|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 vma_alloc_folio+0x5c/0xd0
 do_wp_page+0x288/0xe30
 __handle_mm_fault+0x8ca/0x1760
 handle_mm_fault+0xbc/0x2f0
 do_user_addr_fault+0x158/0x5e0
 exc_page_fault+0x73/0x170
 asm_exc_page_fault+0x26/0x30

Charged to memcg /

Jeff Xie (4):
mm, page_owner: add folio allocate post callback for struct page_owner
to make the owner clearer
mm, slub: implement slub allocate post callback for page_owner
filemap: implement filemap allocate post callback for page_owner mm/rmap: implement anonmap allocate post callback for page_owner

 include/linux/page_owner.h |  9 ++++++++
 include/linux/pagemap.h    |  7 ++++++
 include/linux/rmap.h       |  7 ++++++
 include/linux/slab.h       |  8 ++++++-
 mm/filemap.c               | 44 ++++++++++++++++++++++++++++++++++++++
 mm/page_owner.c            | 36 +++++++++++++++++++++++++++++++
 mm/rmap.c                  | 15 ++++++++++++-
 mm/slub.c                  | 15 +++++++++++++
 8 files changed, 139 insertions(+), 2 deletions(-)

based on mm-stable commit: be3ca57cfb77

Signature · 1 line
2.34.1
author
#1 lore raw

RFC PATCH 1/4 mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer

adding a callback function in the struct page_owner to let the slab layer or the anon/file handler layer or any other memory-allocated layers to implement what they would like to tell.

---
 include/linux/page_owner.h |  9 +++++++++
 mm/page_owner.c            | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
include/linux/page_owner.h +9 -0
index 119a0c9d2a8b..71698d82df7c 100644
@@ -4,6 +4,9 @@
 
 #include <linux/jump_label.h>
 
+typedef int (folio_alloc_post_page_owner_t)(struct folio *folio, struct task_struct *tsk,
+			void *data, char *kbuf, size_t count);
+
 #ifdef CONFIG_PAGE_OWNER
 extern struct static_key_false page_owner_inited;
 extern struct page_ext_operations page_owner_ops;
@@ -17,6 +20,8 @@ extern void __set_page_owner_migrate_reason(struct page *page, int reason);
 extern void __dump_page_owner(const struct page *page);
 extern void pagetypeinfo_showmixedcount_print(struct seq_file *m,
 					pg_data_t *pgdat, struct zone *zone);
+extern void set_folio_alloc_post_page_owner(struct folio *folio,
+		folio_alloc_post_page_owner_t *folio_alloc_post_page_owner, void *data);
 
 static inline void reset_page_owner(struct page *page, unsigned short order)
 {
@@ -72,5 +77,9 @@ static inline void set_page_owner_migrate_reason(struct page *page, int reason)
 static inline void dump_page_owner(const struct page *page)
 {
 }
+static inline void set_folio_alloc_post_page_owner(struct folio *folio,
+		folio_alloc_post_page_owner_t *folio_alloc_post_page_owner, void *data)
+{
+}
 #endif /* CONFIG_PAGE_OWNER */
 #endif /* __LINUX_PAGE_OWNER_H */
mm/page_owner.c +36 -0
index 4f13ce7d2452..4de03a7a10d4 100644
@@ -32,6 +32,9 @@ struct page_owner {
 	char comm[TASK_COMM_LEN];
 	pid_t pid;
 	pid_t tgid;
+	folio_alloc_post_page_owner_t *folio_alloc_post_page_owner;
+	/* for folio_alloc_post_page_owner function parameter */
+	void *data;
 };
 
 static bool page_owner_enabled __initdata;
@@ -152,6 +155,8 @@ void __reset_page_owner(struct page *page, unsigned short order)
 		page_owner = get_page_owner(page_ext);
 		page_owner->free_handle = handle;
 		page_owner->free_ts_nsec = free_ts_nsec;
+		page_owner->folio_alloc_post_page_owner = NULL;
+		page_owner->data = NULL;
 		page_ext = page_ext_next(page_ext);
 	}
 	page_ext_put(page_ext);
@@ -256,6 +261,8 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old)
 	new_page_owner->ts_nsec = old_page_owner->ts_nsec;
 	new_page_owner->free_ts_nsec = old_page_owner->ts_nsec;
 	strcpy(new_page_owner->comm, old_page_owner->comm);
+	new_page_owner->folio_alloc_post_page_owner = old_page_owner->folio_alloc_post_page_owner;
+	new_page_owner->data = old_page_owner->data;
 
 	/*
 	 * We don't clear the bit on the old folio as it's going to be freed
@@ -272,6 +279,25 @@ void __folio_copy_owner(struct folio *newfolio, struct folio *old)
 	page_ext_put(old_ext);
 }
 
+void set_folio_alloc_post_page_owner(struct folio *folio,
+		folio_alloc_post_page_owner_t *folio_alloc_post_page_owner, void *data)
+{
+	struct page *page;
+	struct page_ext *page_ext;
+	struct page_owner *page_owner;
+
+	page = &folio->page;
+	page_ext = page_ext_get(page);
+	if (unlikely(!page_ext))
+		return;
+
+	page_owner = get_page_owner(page_ext);
+	page_owner->folio_alloc_post_page_owner = folio_alloc_post_page_owner;
+	page_owner->data = data;
+
+	page_ext_put(page_ext);
+}
+
 void pagetypeinfo_showmixedcount_print(struct seq_file *m,
 				       pg_data_t *pgdat, struct zone *zone)
 {
@@ -400,6 +426,7 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn,
 		depot_stack_handle_t handle)
 {
 	int ret, pageblock_mt, page_mt;
+	struct task_struct *tsk;
 	char *kbuf;
 
 	count = min_t(size_t, count, PAGE_SIZE);
@@ -414,6 +441,15 @@ print_page_owner(char __user *buf, size_t count, unsigned long pfn,
 			page_owner->tgid, page_owner->comm,
 			page_owner->ts_nsec);
 
+	if (page_owner->folio_alloc_post_page_owner) {
+		rcu_read_lock();
+		tsk = find_task_by_pid_ns(page_owner->pid, &init_pid_ns);
+		rcu_read_unlock();
+		ret += page_owner->folio_alloc_post_page_owner(page_folio(page), tsk, page_owner->data,
+				kbuf + ret, count - ret);
+	} else
+		ret += scnprintf(kbuf + ret, count - ret, "OTHER_PAGE\n");
+
 	/* Print information relevant to grouping pages by mobility */
 	pageblock_mt = get_pageblock_migratetype(page);
 	page_mt  = gfp_migratetype(page_owner->gfp_mask);
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer

Quoted 3 lines · Jeff Xie

adding a callback function in the struct page_owner to let the slab layer or the anon/file handler layer or any other memory-allocated layers to implement what they would like to tell.

There's no need to add a callback. We can tell what a folio is.

mm/page_owner.c print_page_owner(char __user *buf, size_t count, unsigned long pfn, · line 451

Quoted 8 lines
mm/page_owner.c +8 -0
+	if (page_owner->folio_alloc_post_page_owner) {
+		rcu_read_lock();
+		tsk = find_task_by_pid_ns(page_owner->pid, &init_pid_ns);
+		rcu_read_unlock();
+		ret += page_owner->folio_alloc_post_page_owner(page_folio(page), tsk, page_owner->data,
+				kbuf + ret, count - ret);
+	} else
+		ret += scnprintf(kbuf + ret, count - ret, "OTHER_PAGE\n");
	if (folio_test_slab(folio))
		ret += slab_page_owner_info(folio, kbuf + ret, count - ret);
	else if (folio_test_anon(folio))
		ret += anon_page_owner_info(folio, kbuf + ret, count - ret);
	else if (folio_test_movable(folio))
		ret += null_page_owner_info(folio, kbuf + ret, count - ret);
	else if (folio->mapping)
		ret += file_page_owner_info(folio, kbuf + ret, count - ret);
	else
		ret += null_page_owner_info(folio, kbuf + ret, count - ret);

In this scenario, I have the anon handling ksm pages, but if that's not desirable, we can add

	else if (folio_test_ksm(folio))
		ret += ksm_page_owner_info(folio, kbuf + ret, count - ret);

right before the folio_test_anon() clause

#3 lore raw

Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer

Hi Matthew,

Quoted 28 lines · Matthew Wilcox
Quoted 3 lines · Jeff Xie

adding a callback function in the struct page_owner to let the slab layer or the anon/file handler layer or any other memory-allocated layers to implement what they would like to tell.

There's no need to add a callback. We can tell what a folio is.

mm/page_owner.c print_page_owner(char __user *buf, size_t count, unsigned long pfn, · line 451

Quoted 8 lines
mm/page_owner.c +8 -0
+     if (page_owner->folio_alloc_post_page_owner) {
+             rcu_read_lock();
+             tsk = find_task_by_pid_ns(page_owner->pid, &init_pid_ns);
+             rcu_read_unlock();
+             ret += page_owner->folio_alloc_post_page_owner(page_folio(page), tsk, page_owner->data,
+                             kbuf + ret, count - ret);
+     } else
+             ret += scnprintf(kbuf + ret, count - ret, "OTHER_PAGE\n");
        if (folio_test_slab(folio))
                ret += slab_page_owner_info(folio, kbuf + ret, count - ret);
        else if (folio_test_anon(folio))
                ret += anon_page_owner_info(folio, kbuf + ret, count - ret);
        else if (folio_test_movable(folio))
                ret += null_page_owner_info(folio, kbuf + ret, count - ret);
        else if (folio->mapping)
                ret += file_page_owner_info(folio, kbuf + ret, count - ret);
        else
                ret += null_page_owner_info(folio, kbuf + ret, count - ret);
Quoted 7 lines

In this scenario, I have the anon handling ksm pages, but if that's not desirable, we can add

else if (folio_test_ksm(folio))
ret += ksm_page_owner_info(folio, kbuf + ret, count - ret);

right before the folio_test_anon() clause

Thank you very much for your advice and guidance.
From the perspective of a folio, it cannot obtain information about all the situations in which folios are allocated.
If we want to determine whether a folio is related to vmalloc or kernel_stack or the other memory allocation process,
using just a folio parameter is not sufficient. To achieve this goal, we can add a callback function to provide more extensibility and information.

for example:

(the following "OTHER_PAGE" will be replaced with the specified information later)

Page allocated via order 0, mask
0x2102(__GFP_HIGHMEM|__GFP_NOWARN|__GFP_ZERO), pid 0, tgid 0 (swapper/0), ts 167618849 ns
OTHER_PAGE
PFN 0x4a92 type Unmovable Block 37 type Unmovable Flags 0x1fffc0000000000(node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 __alloc_pages_bulk+0x39f/0x620
 alloc_pages_bulk_array_mempolicy+0x1f4/0x210
 __vmalloc_node_range+0x756/0x870
 __vmalloc_node+0x48/0x60
 gen_pool_add_owner+0x3e/0xb0
 mce_gen_pool_init+0x5a/0x90
 mcheck_cpu_init+0x170/0x4c0
 identify_cpu+0x55f/0x7e0
 arch_cpu_finalize_init+0x10/0x100
 start_kernel+0x517/0x8e0
 x86_64_start_reservations+0x18/0x30
 x86_64_start_kernel+0xc6/0xe0
 secondary_startup_64_no_verify+0x178/0x17b
Signature · 2 lines
Thanks,
JeffXie
maintainer
#4 lore raw

Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer

Quoted 7 lines · Jeff Xie
Quoted 1 line

From the perspective of a folio, it cannot obtain information about

all the situations in which folios are allocated.
If we want to determine whether a folio is related to vmalloc or kernel_stack or the other memory allocation process,
using just a folio parameter is not sufficient. To achieve this goal, we can add a callback function to provide more extensibility and information.

But we want that anyway (or at least I do). You're right that vmalloc pages are not marked as being vmalloc pages and don't contain the information about which vmalloc area they belong to. I've talked about ways we can add that information to folios in the past, but I have a lot of other projects I'm working on. Are you interested in doing that?

#5 lore raw

Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer

Quoted 13 lines · Matthew Wilcox
Quoted 9 lines · Jeff Xie

From the perspective of a folio, it cannot obtain information about
all the situations in which folios are allocated.
If we want to determine whether a folio is related to vmalloc or kernel_stack or the other memory allocation process,
using just a folio parameter is not sufficient. To achieve this goal, we can add a callback function to provide more extensibility and information.

Quoted 6 lines

But we want that anyway (or at least I do). You're right that vmalloc pages are not marked as being vmalloc pages and don't contain the information about which vmalloc area they belong to. I've talked about ways we can add that information to folios in the past, but I have a lot of other projects I'm working on. Are you interested in doing that?

Certainly, I'm willing to give it a try. If a folio can include vmalloc information or more information, this is great. I may need to understand the background of why you proposed this method in the past.

--
Jeff Xie

maintainer
#6 lore raw

Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer

Quoted 11 lines · On Thu, Nov 09, 2023 at 04:04:39PM +0000, [email protected] wrote:
Quoted 6 lines · Matthew Wilcox

But we want that anyway (or at least I do). You're right that vmalloc pages are not marked as being vmalloc pages and don't contain the information about which vmalloc area they belong to. I've talked about ways we can add that information to folios in the past, but I have a lot of other projects I'm working on. Are you interested in doing that?

Certainly, I'm willing to give it a try. If a folio can include vmalloc information or more information, this is great. I may need to understand the background of why you proposed this method in the past.

I can't find the proposal now, but it's basically this:

Turn PG_slab into a PG_kernel. If PG_kernel is set, then other flags change their meaning. Flags that should be reusable: writeback, referenced, uptodate, lru, active, workingset, private, reclaim

One of those flags gets reused to be the new slab. So, eg folio_test_slab() becomes:

	return (folio->flags & (PG_kernel | PG_slab)) == (PG_kernel | PG_slab);

Now we have somewhere that we can use for PG_vmalloc (also PG_reserved can become a PG_kernel sub-flag, freeing up a page flag).

We'd need to change some helpers. eg folio_mapping() currently does:

        if (unlikely(folio_test_slab(folio)))
                return NULL;

and that should be:

	if (unlikely(folio_test_kernel(folio)))
		return NULL;

With that in place, we can reuse the folio->mapping space to point to the struct vm_struct that allocated it.

This isn't an easy project and will require a lot of testing. It has some upsides, like freeing up a page flag.

author
#7 lore raw

RFC PATCH 2/4 mm, slub: implement slub allocate post callback for page_owner

In reply to Jeff Xie

Implement the callback function slab_alloc_post_page_owner for the page_owner to make the owner of the slab page clearer

For example, for slab page, a line is added to the result of page_owner

added: "SLAB_PAGE slab_name:kmalloc-32"

Page allocated via order 0, mask 0x12cc0(GFP_KERNEL|__GFP_NOWARN|__GFP_NORETRY), pid 1, tgid 1 (swapper/0), ts 340615384 ns SLAB_PAGE slab_name:kmalloc-32
PFN 0x4be0 type Unmovable Block 37 type Unmovable Flags 0x1fffc0000000800(slab|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 allocate_slab+0x31f/0x410
 ___slab_alloc+0x3e8/0x850
 __kmem_cache_alloc_node+0x111/0x2b0
 kmalloc_trace+0x29/0x90
 iommu_setup_dma_ops+0x299/0x470
 bus_iommu_probe+0xe1/0x150
 iommu_device_register+0xad/0x120
 intel_iommu_init+0xe3a/0x1260
 pci_iommu_init+0x12/0x40
 do_one_initcall+0x45/0x210
 kernel_init_freeable+0x1a4/0x2e0
 kernel_init+0x1a/0x1c0

added: "SLAB_PAGE slab_name:mm_struct"

Page allocated via order 3, mask 0xd20c0(__GFP_IO|__GFP_FS|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), \
pid 86, tgid 86 (linuxrc), ts 1121118666 ns
SLAB_PAGE slab_name:mm_struct
PFN 0x6388 type Unmovable Block 49 type Unmovable Flags 0x1fffc0000000840(slab|head|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 allocate_slab+0x31f/0x410
 ___slab_alloc+0x3e8/0x850
 kmem_cache_alloc+0x2b8/0x2f0
 mm_alloc+0x1a/0x50
 alloc_bprm+0x8a/0x300
 do_execveat_common.isra.0+0x68/0x240
 __x64_sys_execve+0x37/0x50
 do_syscall_64+0x42/0xf0
 entry_SYSCALL_64_after_hwframe+0x6e/0x76
---
 include/linux/slab.h |  8 +++++++-
 mm/slub.c            | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
include/linux/slab.h +7 -1
index d6d6ffeeb9a2..c8969eb4f322 100644
@@ -795,5 +795,11 @@ int slab_dead_cpu(unsigned int cpu);
 #define slab_prepare_cpu	NULL
 #define slab_dead_cpu		NULL
 #endif
-
+#ifndef CONFIG_PAGE_OWNER
+static inline int slab_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+		void *data, char *kbuf, size_t count)
+{
+	return 0;
+}
+#endif
 #endif	/* _LINUX_SLAB_H */
mm/slub.c +15 -0
index 63d281dfacdb..7ab8c7aa78e5 100644
@@ -44,6 +44,7 @@
 
 #include <linux/debugfs.h>
 #include <trace/events/kmem.h>
+#include <linux/page_owner.h>
 
 #include "internal.h"
 
@@ -1993,6 +1994,19 @@ static inline bool shuffle_freelist(struct kmem_cache *s, struct slab *slab)
 }
 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
 
+#ifdef CONFIG_PAGE_OWNER
+static int slab_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+			void *data, char *kbuf, size_t count)
+{
+	int ret;
+	struct kmem_cache *kmem_cache = data;
+
+	ret = scnprintf(kbuf, count, "SLAB_PAGE slab_name:%s\n", kmem_cache->name);
+
+	return ret;
+}
+#endif
+
 static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 {
 	struct slab *slab;
@@ -2028,6 +2042,7 @@ static struct slab *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
 		stat(s, ORDER_FALLBACK);
 	}
 
+	set_folio_alloc_post_page_owner(slab_folio(slab), slab_alloc_post_page_owner, s);
 	slab->objects = oo_objects(oo);
 	slab->inuse = 0;
 	slab->frozen = 0;
Signature · 1 line
2.34.1
maintainer
#8 lore raw

Re: [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner

mm/slub.c static inline bool shuffle_freelist(struct kmem_cache *s, struct slab *slab) · line 2008

Quoted 12 lines · Jeff Xie
mm/slub.c +12 -0
+#ifdef CONFIG_PAGE_OWNER
+static int slab_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+			void *data, char *kbuf, size_t count)
+{
+	int ret;
+	struct kmem_cache *kmem_cache = data;
+
+	ret = scnprintf(kbuf, count, "SLAB_PAGE slab_name:%s\n", kmem_cache->name);
+
+	return ret;
+}
+#endif

Or we could do this typesafely ...

	struct slab *slab = folio_slab(folio);
	struct kmem_cache *kmem_cache = slab->slab_cache;

... and then there's no need to pass in a 'data' to the function.

#9 lore raw

Re: [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner

Quoted 18 lines · Matthew Wilcox
Quoted 14 lines · Jeff Xie

+#ifdef CONFIG_PAGE_OWNER
+static int slab_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+ void *data, char *kbuf, size_t count)
+{
+ int ret;
+ struct kmem_cache *kmem_cache = data;
+
+ ret = scnprintf(kbuf, count, "SLAB_PAGE slab_name:%s\n", kmem_cache->name); +
+ return ret;
+}
+#endif

Quoted 7 lines

Or we could do this typesafely ...

struct slab *slab = folio_slab(folio); struct kmem_cache *kmem_cache = slab->slab_cache;

... and then there's no need to pass in a 'data' to the function.

I accidentally replied using my other email ([email protected]) just now. Thank you for your advice. Indeed, the "data" didn't serve any purpose here.

--
Jeff Xie

author
#10 lore raw

RFC PATCH 3/4 filemap: implement filemap allocate post callback for page_owner

In reply to Jeff Xie

Implement the callback function filemap_alloc_post_page_owner for the page_owner to make the owner of the file page clearer

For the pid 98:
[root@JeffXie ]# cat /proc/98/maps
00400000-00401000 r--p 00000000 fd:00 1954 /test/mm/a.out

00401000-00499000 r-xp 00001000 fd:00 1954      /test/mm/a.out
00499000-004c2000 r--p 00099000 fd:00 1954      /test/mm/a.out
004c2000-004c6000 r--p 000c1000 fd:00 1954      /test/mm/a.out
004c6000-004c9000 rw-p 000c5000 fd:00 1954      /test/mm/a.out
004c9000-004ce000 rw-p 00000000 00:00 0 
01d97000-01db9000 rw-p 00000000 00:00 0                 [heap]
7f1588fc8000-7f1588fc9000 rw-p 00000000 fd:00 1945      /a.txt
7ffda207a000-7ffda209b000 rw-p 00000000 00:00 0         [stack]
7ffda2152000-7ffda2156000 r--p 00000000 00:00 0         [vvar]
7ffda2156000-7ffda2158000 r-xp 00000000 00:00 0         [vdso]

ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]

added: "FILE_PAGE dev 253:0 ino:1954 index:0xc1 mapcount:1 refcount:2 0x4c2000 - 0x4c3000"

Page allocated via order 0, mask 0x152c4a(GFP_NOFS|__GFP_HIGHMEM|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_HARDWALL|__GFP_MOVABLE),\
pid 98, tgid 98 (a.out), ts 28441476044 ns
FILE_PAGE dev 253:0 ino:1954 index:0xc1 mapcount:1 refcount:2 0x4c2000 - 0x4c3000
PFN 0x5be8e type Movable Block 735 type Movable Flags 0x1fffc0000020028(uptodate|lru|mappedtodisk|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 folio_alloc+0x18/0x50
 page_cache_ra_unbounded+0x9b/0x1a0
 filemap_fault+0x5f7/0xc20
 __do_fault+0x31/0xc0
 __handle_mm_fault+0x1333/0x1760
 handle_mm_fault+0xbc/0x2f0
 do_user_addr_fault+0x1f8/0x5e0
 exc_page_fault+0x73/0x170
 asm_exc_page_fault+0x26/0x30

Charged to memcg /

---
 include/linux/pagemap.h |  7 +++++++
 mm/filemap.c            | 44 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
include/linux/pagemap.h +7 -0
index bcc1ea44b4e8..900aa136c71f 100644
@@ -1514,4 +1514,11 @@ unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
 {
 	return i_blocks_per_folio(inode, page_folio(page));
 }
+#ifndef CONFIG_PAGE_OWNER
+static inline int filemap_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+		void *data, char *kbuf, size_t count)
+{
+	return 0;
+}
+#endif
 #endif /* _LINUX_PAGEMAP_H */
mm/filemap.c +44 -0

62 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#11 lore raw

Re: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner

mm/filemap.c void replace_page_cache_folio(struct folio *old, struct folio *new) · line 854

Quoted 12 lines · Jeff Xie
mm/filemap.c +12 -0
+static int filemap_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+			void *data, char *kbuf, size_t count)
+{
+	int ret;
+	int mapcount;
+	dev_t s_dev;
+	struct inode *inode;
+	struct vm_area_struct *vma;
+	struct mm_struct *mm;
+	unsigned long virtual_start = 0x0;
+	unsigned long virtual_end = 0x0;
+	struct address_space *mapping = data;

This is just folio->mapping.

Quoted 4 lines
mm/filemap.c +4 -0
+	mapcount = folio_mapcount(folio);
+	if (mapcount && tsk && tsk->mm) {
+		mm = tsk->mm;
+		VMA_ITERATOR(vmi, mm, 0);

mm/filemap.c void replace_page_cache_folio(struct folio *old, struct folio *new) · line 869

Quoted 10 lines
mm/filemap.c +10 -0
+		mmap_read_lock(mm);
+		for_each_vma(vmi, vma) {
+			if (page_mapped_in_vma(&folio->page, vma)) {
+				virtual_start = vma_address(&folio->page, vma);
+				virtual_end = virtual_start + folio_nr_pages(folio) * PAGE_SIZE;
+				break;
+			}
+		}
+		mmap_read_unlock(mm);
+	}

Why not just walk the rmap directly to find out where it's mapped in any process instead of the one which allocated it?

#12 lore raw

Re: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner

Quoted 18 lines · Matthew Wilcox
Quoted 14 lines · Jeff Xie

+static int filemap_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+ void *data, char *kbuf, size_t count)
+{
+ int ret;
+ int mapcount;
+ dev_t s_dev;
+ struct inode *inode;
+ struct vm_area_struct *vma;
+ struct mm_struct *mm;
+ unsigned long virtual_start = 0x0;
+ unsigned long virtual_end = 0x0;
+ struct address_space *mapping = data;

Quoted 1 line

This is just folio->mapping.

Thanks, I need to know more about struct folio ;-)

Quoted 18 lines
Quoted 17 lines

+ mapcount = folio_mapcount(folio);
+ if (mapcount && tsk && tsk->mm) {
+ mm = tsk->mm;
+ VMA_ITERATOR(vmi, mm, 0);
+ mmap_read_lock(mm);
+ for_each_vma(vmi, vma) {
+ if (page_mapped_in_vma(&folio->page, vma)) {
+ virtual_start = vma_address(&folio->page, vma);
+ virtual_end = virtual_start + folio_nr_pages(folio) * PAGE_SIZE;
+ break;
+ }
+ }
+ mmap_read_unlock(mm);
+ }

Quoted 2 lines

Why not just walk the rmap directly to find out where it's mapped in any process instead of the one which allocated it?

Since the page_owner's result only shows which PID allocated this page, we only need to obtain the address space of the corresponding process for that PID.

--
Jeff Xie

maintainer
#13 lore raw

Re: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner

Quoted 5 lines · On Thu, Nov 09, 2023 at 03:43:10PM +0000, [email protected] wrote:
Quoted 2 lines · Matthew Wilcox

Why not just walk the rmap directly to find out where it's mapped in any process instead of the one which allocated it?

Since the page_owner's result only shows which PID allocated this page, we only need to obtain the address space of the corresponding process for that PID.

But that's probably uninteresting. Consider, eg, a page from libc. That's going to be mapped by hundreds or even thousands of processes. And the one which originally allocated it may well have exited by this point; files often live long past the process that first reads them.

#14 lore raw

Re: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner

In reply to Jeff Xie
kernel test robot noticed the following build errors:

Hi Jeff,

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

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

url:    https://github.com/intel-lab-lkp/linux/commits/Jeff-Xie/mm-page_owner-add-folio-allocate-post-callback-for-struct-page_owner-to-make-the-owner-clearer/20231109-165052
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20231109032521.392217-4-jeff.xie%40linux.dev
patch subject: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner
config: sh-allyesconfig (https://download.01.org/0day-ci/archive/20231110/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231110/[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 errors (new ones prefixed by >>):

mm/filemap.c: In function 'filemap_alloc_post_page_owner':

>> mm/filemap.c:862:29: error: implicit declaration of function 'page_mapped_in_vma' [-Werror=implicit-function-declaration]
     862 |                         if (page_mapped_in_vma(&folio->page, vma)) {
         |                             ^~~~~~~~~~~~~~~~~~
>> mm/filemap.c:863:49: error: implicit declaration of function 'vma_address'; did you mean 'page_address'? [-Werror=implicit-function-declaration]
     863 |                                 virtual_start = vma_address(&folio->page, vma);
         |                                                 ^~~~~~~~~~~
         |                                                 page_address

cc1: some warnings being treated as errors

vim +/page_mapped_in_vma +862 mm/filemap.c

841
842 #ifdef CONFIG_PAGE_OWNER
843 static int filemap_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
844 void *data, char *kbuf, size_t count)
845 {
846 int ret;
847 int mapcount;
848 dev_t s_dev;
849 struct inode *inode;
850 struct vm_area_struct *vma;
851 struct mm_struct *mm;
852 unsigned long virtual_start = 0x0;
853 unsigned long virtual_end = 0x0;
854 struct address_space *mapping = data;
855
856 mapcount = folio_mapcount(folio);
857 if (mapcount && tsk && tsk->mm) {
858 mm = tsk->mm;
859 VMA_ITERATOR(vmi, mm, 0);
860 mmap_read_lock(mm);
861 for_each_vma(vmi, vma) {

 > 862				if (page_mapped_in_vma(&folio->page, vma)) {
 > 863					virtual_start = vma_address(&folio->page, vma);

864 virtual_end = virtual_start + folio_nr_pages(folio) * PAGE_SIZE;
865 break;
866 }
867 }
868 mmap_read_unlock(mm);
869 }
870
871 inode = mapping->host;
872 if (mapping->host->i_sb)
873 s_dev = mapping->host->i_sb->s_dev;
874 else
875 s_dev = mapping->host->i_rdev;
876 ret = scnprintf(kbuf, count, "FILE_PAGE dev %d:%d ino:%lu index:0x%lx mapcount:%d refcount:%d 0x%lx - 0x%lx\n",
877 MAJOR(s_dev), MINOR(s_dev), inode->i_ino, folio->index, mapcount, folio_ref_count(folio),
878 virtual_start, virtual_end);
879
880 return ret;
881 }
882 #endif
883

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

RFC PATCH 4/4 mm/rmap: implement anonmap allocate post callback for page_owner

In reply to Jeff Xie

Implement the callback function anon_alloc_post_page_owner for the page_owner to make the owner of the anon page clearer

For the pid 98:
[root@JeffXie ]# cat /proc/98/maps
00400000-00401000 r--p 00000000 fd:00 1954 /test/mm/a.out

00401000-00499000 r-xp 00001000 fd:00 1954      /test/mm/a.out
00499000-004c2000 r--p 00099000 fd:00 1954      /test/mm/a.out
004c2000-004c6000 r--p 000c1000 fd:00 1954      /test/mm/a.out
004c6000-004c9000 rw-p 000c5000 fd:00 1954      /test/mm/a.out
004c9000-004ce000 rw-p 00000000 00:00 0 
01d97000-01db9000 rw-p 00000000 00:00 0                 [heap]
7f1588fc8000-7f1588fc9000 rw-p 00000000 fd:00 1945      /a.txt
7ffda207a000-7ffda209b000 rw-p 00000000 00:00 0         [stack]
7ffda2152000-7ffda2156000 r--p 00000000 00:00 0         [vvar]
7ffda2156000-7ffda2158000 r-xp 00000000 00:00 0         [vdso]

ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]

added: "ANON_PAGE address 0x4c4000"

Page allocated via order 0, mask 0x140cca(GFP_HIGHUSER_MOVABLE|__GFP_COMP), pid 98, tgid 98 (a.out), ts 28442066180 ns ANON_PAGE address 0x4c4000
PFN 0x2c3db type Movable Block 353 type Movable Flags 0x1fffc00000a0028(uptodate|lru|mappedtodisk|swapbacked|node=0|zone=1|lastcpupid=0x3fff)

 post_alloc_hook+0x77/0xf0
 get_page_from_freelist+0x58d/0x14e0
 __alloc_pages+0x1b2/0x380
 alloc_pages_mpol+0x97/0x1f0
 vma_alloc_folio+0x5c/0xd0
 do_wp_page+0x288/0xe30
 __handle_mm_fault+0x8ca/0x1760
 handle_mm_fault+0xbc/0x2f0
 do_user_addr_fault+0x158/0x5e0
 exc_page_fault+0x73/0x170
 asm_exc_page_fault+0x26/0x30

Charged to memcg /

---
 include/linux/rmap.h |  7 +++++++
 mm/rmap.c            | 15 ++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
include/linux/rmap.h +7 -0
index b26fe858fd44..d85650c9c520 100644
@@ -502,4 +502,11 @@ static inline int page_mkclean(struct page *page)
 {
 	return folio_mkclean(page_folio(page));
 }
+#ifndef CONFIG_PAGE_OWNER
+static inline int anon_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+		void *data, char *kbuf, size_t count)
+{
+	return 0;
+}
+#endif
 #endif	/* _LINUX_RMAP_H */
mm/rmap.c +14 -1
index 7a27a2b41802..41c8a387cd37 100644
@@ -75,7 +75,7 @@
 #include <linux/memremap.h>
 #include <linux/userfaultfd_k.h>
 #include <linux/mm_inline.h>
-
+#include <linux/page_owner.h>
 #include <asm/tlbflush.h>
 
 #define CREATE_TRACE_POINTS
@@ -1151,6 +1151,18 @@ void folio_move_anon_rmap(struct folio *folio, struct vm_area_struct *vma)
 	 */
 	WRITE_ONCE(folio->mapping, anon_vma);
 }
+#ifdef CONFIG_PAGE_OWNER
+static int anon_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+			void *data, char *kbuf, size_t count)
+{
+	int ret;
+	unsigned long address = (unsigned long)data;
+
+	ret = scnprintf(kbuf, count, "ANON_PAGE address 0x%lx\n", address);
+
+	return ret;
+}
+#endif
 
 /**
  * __folio_set_anon - set up a new anonymous rmap for a folio
@@ -1182,6 +1194,7 @@ static void __folio_set_anon(struct folio *folio, struct vm_area_struct *vma,
 	anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
 	WRITE_ONCE(folio->mapping, (struct address_space *) anon_vma);
 	folio->index = linear_page_index(vma, address);
+	set_folio_alloc_post_page_owner(folio, anon_alloc_post_page_owner, (void *)address);
 }
 
 /**
Signature · 1 line
2.34.1
maintainer
#16 lore raw

Re: [RFC][PATCH 4/4] mm/rmap: implement anonmap allocate post callback for page_owner

mm/rmap.c void folio_move_anon_rmap(struct folio *folio, struct vm_area_struct *vma) · line 1161

Quoted 7 lines · Jeff Xie
mm/rmap.c +7 -0
+static int anon_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+			void *data, char *kbuf, size_t count)
+{
+	int ret;
+	unsigned long address = (unsigned long)data;
+
+	ret = scnprintf(kbuf, count, "ANON_PAGE address 0x%lx\n", address);

... completely ignoring that it might have been mremap() since ...

I'm not an expert on anon memory. I'm sure someone can tell you how to figure out the current address that a folio is mapped at.

#17 lore raw

Re: [RFC][PATCH 4/4] mm/rmap: implement anonmap allocate post callback for page_owner

Quoted 13 lines · Matthew Wilcox
Quoted 9 lines · Jeff Xie

+static int anon_alloc_post_page_owner(struct folio *folio, struct task_struct *tsk,
+ void *data, char *kbuf, size_t count)
+{
+ int ret;
+ unsigned long address = (unsigned long)data;
+
+ ret = scnprintf(kbuf, count, "ANON_PAGE address 0x%lx\n", address);

Quoted 5 lines

... completely ignoring that it might have been mremap() since ...

I'm not an expert on anon memory. I'm sure someone can tell you how to figure out the current address that a folio is mapped at.

I'm currently not sure if the address parameter is the mapped address, so this is an RFC patchset ;-)

__
Jeff Xie