linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC][PATCH 0/4] mm, page_owner: make the owner in page owner clearer
@ 2023-11-09  3:25 Jeff Xie
  2023-11-09  3:25 ` [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the " Jeff Xie
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Jeff Xie @ 2023-11-09  3:25 UTC (permalink / raw)
  To: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, willy
  Cc: linux-mm, linux-fsdevel, linux-kernel, chensong_2000, xiehuan09,
	Jeff Xie

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
-- 
2.34.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer
  2023-11-09  3:25 [RFC][PATCH 0/4] mm, page_owner: make the owner in page owner clearer Jeff Xie
@ 2023-11-09  3:25 ` Jeff Xie
  2023-11-09 13:59   ` Matthew Wilcox
  2023-11-09  3:25 ` [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner Jeff Xie
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 17+ messages in thread
From: Jeff Xie @ 2023-11-09  3:25 UTC (permalink / raw)
  To: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, willy
  Cc: linux-mm, linux-fsdevel, linux-kernel, chensong_2000, xiehuan09,
	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.

Signed-off-by: Jeff Xie <jeff.xie@linux.dev>
---
 include/linux/page_owner.h |  9 +++++++++
 mm/page_owner.c            | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/include/linux/page_owner.h b/include/linux/page_owner.h
index 119a0c9d2a8b..71698d82df7c 100644
--- a/include/linux/page_owner.h
+++ b/include/linux/page_owner.h
@@ -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 */
diff --git a/mm/page_owner.c b/mm/page_owner.c
index 4f13ce7d2452..4de03a7a10d4 100644
--- a/mm/page_owner.c
+++ b/mm/page_owner.c
@@ -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);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner
  2023-11-09  3:25 [RFC][PATCH 0/4] mm, page_owner: make the owner in page owner clearer Jeff Xie
  2023-11-09  3:25 ` [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the " Jeff Xie
@ 2023-11-09  3:25 ` Jeff Xie
  2023-11-09 14:05   ` Matthew Wilcox
  2023-11-09 15:34   ` jeff.xie
  2023-11-09  3:25 ` [RFC][PATCH 3/4] filemap: implement filemap " Jeff Xie
  2023-11-09  3:25 ` [RFC][PATCH 4/4] mm/rmap: implement anonmap " Jeff Xie
  3 siblings, 2 replies; 17+ messages in thread
From: Jeff Xie @ 2023-11-09  3:25 UTC (permalink / raw)
  To: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, willy
  Cc: linux-mm, linux-fsdevel, linux-kernel, chensong_2000, xiehuan09,
	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

Signed-off-by: Jeff Xie <jeff.xie@linux.dev>
---
 include/linux/slab.h |  8 +++++++-
 mm/slub.c            | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/include/linux/slab.h b/include/linux/slab.h
index d6d6ffeeb9a2..c8969eb4f322 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -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 */
diff --git a/mm/slub.c b/mm/slub.c
index 63d281dfacdb..7ab8c7aa78e5 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -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;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner
  2023-11-09  3:25 [RFC][PATCH 0/4] mm, page_owner: make the owner in page owner clearer Jeff Xie
  2023-11-09  3:25 ` [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the " Jeff Xie
  2023-11-09  3:25 ` [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner Jeff Xie
@ 2023-11-09  3:25 ` Jeff Xie
  2023-11-09 14:09   ` Matthew Wilcox
  2023-11-09 15:43   ` jeff.xie
  2023-11-09  3:25 ` [RFC][PATCH 4/4] mm/rmap: implement anonmap " Jeff Xie
  3 siblings, 2 replies; 17+ messages in thread
From: Jeff Xie @ 2023-11-09  3:25 UTC (permalink / raw)
  To: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, willy
  Cc: linux-mm, linux-fsdevel, linux-kernel, chensong_2000, xiehuan09,
	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 / 

Signed-off-by: Jeff Xie <jeff.xie@linux.dev>
---
 include/linux/pagemap.h |  7 +++++++
 mm/filemap.c            | 44 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index bcc1ea44b4e8..900aa136c71f 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -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 */
diff --git a/mm/filemap.c b/mm/filemap.c
index 9710f43a89ac..0a346443309a 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -45,6 +45,7 @@
 #include <linux/migrate.h>
 #include <linux/pipe_fs_i.h>
 #include <linux/splice.h>
+#include <linux/page_owner.h>
 #include <asm/pgalloc.h>
 #include <asm/tlbflush.h>
 #include "internal.h"
@@ -838,6 +839,48 @@ void replace_page_cache_folio(struct folio *old, struct folio *new)
 }
 EXPORT_SYMBOL_GPL(replace_page_cache_folio);
 
+#ifdef CONFIG_PAGE_OWNER
+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;
+
+	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);
+	}
+
+	inode = mapping->host;
+	if (mapping->host->i_sb)
+		s_dev = mapping->host->i_sb->s_dev;
+	else
+		s_dev = mapping->host->i_rdev;
+	ret = scnprintf(kbuf, count, "FILE_PAGE dev %d:%d ino:%lu index:0x%lx mapcount:%d refcount:%d 0x%lx - 0x%lx\n",
+		MAJOR(s_dev), MINOR(s_dev), inode->i_ino, folio->index, mapcount, folio_ref_count(folio),
+		virtual_start, virtual_end);
+
+	return ret;
+}
+#endif
+
 noinline int __filemap_add_folio(struct address_space *mapping,
 		struct folio *folio, pgoff_t index, gfp_t gfp, void **shadowp)
 {
@@ -915,6 +958,7 @@ noinline int __filemap_add_folio(struct address_space *mapping,
 	if (xas_error(&xas))
 		goto error;
 
+	set_folio_alloc_post_page_owner(folio, filemap_alloc_post_page_owner, mapping);
 	trace_mm_filemap_add_to_page_cache(folio);
 	return 0;
 error:
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [RFC][PATCH 4/4] mm/rmap: implement anonmap allocate post callback for page_owner
  2023-11-09  3:25 [RFC][PATCH 0/4] mm, page_owner: make the owner in page owner clearer Jeff Xie
                   ` (2 preceding siblings ...)
  2023-11-09  3:25 ` [RFC][PATCH 3/4] filemap: implement filemap " Jeff Xie
@ 2023-11-09  3:25 ` Jeff Xie
  2023-11-09 14:10   ` Matthew Wilcox
  2023-11-09 15:47   ` jeff.xie
  3 siblings, 2 replies; 17+ messages in thread
From: Jeff Xie @ 2023-11-09  3:25 UTC (permalink / raw)
  To: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, willy
  Cc: linux-mm, linux-fsdevel, linux-kernel, chensong_2000, xiehuan09,
	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 / 

Signed-off-by: Jeff Xie <jeff.xie@linux.dev>
---
 include/linux/rmap.h |  7 +++++++
 mm/rmap.c            | 15 ++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/include/linux/rmap.h b/include/linux/rmap.h
index b26fe858fd44..d85650c9c520 100644
--- a/include/linux/rmap.h
+++ b/include/linux/rmap.h
@@ -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 */
diff --git a/mm/rmap.c b/mm/rmap.c
index 7a27a2b41802..41c8a387cd37 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -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);
 }
 
 /**
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer
  2023-11-09  3:25 ` [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the " Jeff Xie
@ 2023-11-09 13:59   ` Matthew Wilcox
  2023-11-09 15:25     ` Jeff Xie
  0 siblings, 1 reply; 17+ messages in thread
From: Matthew Wilcox @ 2023-11-09 13:59 UTC (permalink / raw)
  To: Jeff Xie
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

On Thu, Nov 09, 2023 at 11:25:18AM +0800, Jeff Xie wrote:
> 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.

> +	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

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner
  2023-11-09  3:25 ` [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner Jeff Xie
@ 2023-11-09 14:05   ` Matthew Wilcox
  2023-11-09 15:34   ` jeff.xie
  1 sibling, 0 replies; 17+ messages in thread
From: Matthew Wilcox @ 2023-11-09 14:05 UTC (permalink / raw)
  To: Jeff Xie
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

On Thu, Nov 09, 2023 at 11:25:19AM +0800, Jeff Xie wrote:
> +#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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner
  2023-11-09  3:25 ` [RFC][PATCH 3/4] filemap: implement filemap " Jeff Xie
@ 2023-11-09 14:09   ` Matthew Wilcox
  2023-11-09 15:43   ` jeff.xie
  1 sibling, 0 replies; 17+ messages in thread
From: Matthew Wilcox @ 2023-11-09 14:09 UTC (permalink / raw)
  To: Jeff Xie
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

On Thu, Nov 09, 2023 at 11:25:20AM +0800, Jeff Xie wrote:
> +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.

> +	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);
> +	}

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


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 4/4] mm/rmap: implement anonmap allocate post callback for page_owner
  2023-11-09  3:25 ` [RFC][PATCH 4/4] mm/rmap: implement anonmap " Jeff Xie
@ 2023-11-09 14:10   ` Matthew Wilcox
  2023-11-09 15:47   ` jeff.xie
  1 sibling, 0 replies; 17+ messages in thread
From: Matthew Wilcox @ 2023-11-09 14:10 UTC (permalink / raw)
  To: Jeff Xie
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

On Thu, Nov 09, 2023 at 11:25:21AM +0800, Jeff Xie wrote:
> +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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer
  2023-11-09 13:59   ` Matthew Wilcox
@ 2023-11-09 15:25     ` Jeff Xie
  2023-11-09 15:36       ` Matthew Wilcox
  2023-11-09 16:04       ` jeff.xie
  0 siblings, 2 replies; 17+ messages in thread
From: Jeff Xie @ 2023-11-09 15:25 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: Jeff Xie, akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000

Hi Matthew,

On Thu, Nov 9, 2023 at 10:00 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> On Thu, Nov 09, 2023 at 11:25:18AM +0800, Jeff Xie wrote:
> > 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.
>
> > +     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

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

-- 
Thanks,
JeffXie

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner
  2023-11-09  3:25 ` [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner Jeff Xie
  2023-11-09 14:05   ` Matthew Wilcox
@ 2023-11-09 15:34   ` jeff.xie
  1 sibling, 0 replies; 17+ messages in thread
From: jeff.xie @ 2023-11-09 15:34 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

November 9, 2023 at 10:05 PM, "Matthew Wilcox" <willy@infradead.org> wrote:


> 
> On Thu, Nov 09, 2023 at 11:25:19AM +0800, Jeff Xie wrote:
> 
> > 
> > +#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.
>

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

--
Jeff Xie

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer
  2023-11-09 15:25     ` Jeff Xie
@ 2023-11-09 15:36       ` Matthew Wilcox
  2023-11-09 16:04       ` jeff.xie
  1 sibling, 0 replies; 17+ messages in thread
From: Matthew Wilcox @ 2023-11-09 15:36 UTC (permalink / raw)
  To: Jeff Xie
  Cc: Jeff Xie, akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000

On Thu, Nov 09, 2023 at 11:25:18PM +0800, Jeff Xie wrote:
> >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?

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner
  2023-11-09  3:25 ` [RFC][PATCH 3/4] filemap: implement filemap " Jeff Xie
  2023-11-09 14:09   ` Matthew Wilcox
@ 2023-11-09 15:43   ` jeff.xie
  2023-11-09 15:46     ` Matthew Wilcox
  1 sibling, 1 reply; 17+ messages in thread
From: jeff.xie @ 2023-11-09 15:43 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

November 9, 2023 at 10:09 PM, "Matthew Wilcox" <willy@infradead.org> wrote:


> 
> On Thu, Nov 09, 2023 at 11:25:20AM +0800, Jeff Xie wrote:
> 
> > 
> > +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.

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

> 
> > 
> > + 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);
> >  + }
> > 
> 
> 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

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 3/4] filemap: implement filemap allocate post callback for page_owner
  2023-11-09 15:43   ` jeff.xie
@ 2023-11-09 15:46     ` Matthew Wilcox
  0 siblings, 0 replies; 17+ messages in thread
From: Matthew Wilcox @ 2023-11-09 15:46 UTC (permalink / raw)
  To: jeff.xie
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

On Thu, Nov 09, 2023 at 03:43:10PM +0000, jeff.xie@linux.dev wrote:
> November 9, 2023 at 10:09 PM, "Matthew Wilcox" <willy@infradead.org> wrote:
> > 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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 4/4] mm/rmap: implement anonmap allocate post callback for page_owner
  2023-11-09  3:25 ` [RFC][PATCH 4/4] mm/rmap: implement anonmap " Jeff Xie
  2023-11-09 14:10   ` Matthew Wilcox
@ 2023-11-09 15:47   ` jeff.xie
  1 sibling, 0 replies; 17+ messages in thread
From: jeff.xie @ 2023-11-09 15:47 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000, xiehuan09

November 9, 2023 at 10:10 PM, "Matthew Wilcox" <willy@infradead.org> wrote:


> 
> On Thu, Nov 09, 2023 at 11:25:21AM +0800, Jeff Xie wrote:
> 
> > 
> > +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.
>

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

__
Jeff Xie

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer
  2023-11-09 15:25     ` Jeff Xie
  2023-11-09 15:36       ` Matthew Wilcox
@ 2023-11-09 16:04       ` jeff.xie
  2023-11-09 16:38         ` Matthew Wilcox
  1 sibling, 1 reply; 17+ messages in thread
From: jeff.xie @ 2023-11-09 16:04 UTC (permalink / raw)
  To: Matthew Wilcox, Jeff Xie
  Cc: akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000

November 9, 2023 at 11:36 PM, "Matthew Wilcox" <willy@infradead.org> wrote:


> 
> On Thu, Nov 09, 2023 at 11:25:18PM +0800, Jeff Xie wrote:
> 
> > 
> > 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?
>

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

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the owner clearer
  2023-11-09 16:04       ` jeff.xie
@ 2023-11-09 16:38         ` Matthew Wilcox
  0 siblings, 0 replies; 17+ messages in thread
From: Matthew Wilcox @ 2023-11-09 16:38 UTC (permalink / raw)
  To: jeff.xie
  Cc: Jeff Xie, akpm, iamjoonsoo.kim, vbabka, cl, penberg, rientjes,
	roman.gushchin, 42.hyeyoo, linux-mm, linux-fsdevel, linux-kernel,
	chensong_2000

On Thu, Nov 09, 2023 at 04:04:39PM +0000, jeff.xie@linux.dev wrote:
> November 9, 2023 at 11:36 PM, "Matthew Wilcox" <willy@infradead.org> wrote:
> > 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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2023-11-09 16:39 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-09  3:25 [RFC][PATCH 0/4] mm, page_owner: make the owner in page owner clearer Jeff Xie
2023-11-09  3:25 ` [RFC][PATCH 1/4] mm, page_owner: add folio allocate post callback for struct page_owner to make the " Jeff Xie
2023-11-09 13:59   ` Matthew Wilcox
2023-11-09 15:25     ` Jeff Xie
2023-11-09 15:36       ` Matthew Wilcox
2023-11-09 16:04       ` jeff.xie
2023-11-09 16:38         ` Matthew Wilcox
2023-11-09  3:25 ` [RFC][PATCH 2/4] mm, slub: implement slub allocate post callback for page_owner Jeff Xie
2023-11-09 14:05   ` Matthew Wilcox
2023-11-09 15:34   ` jeff.xie
2023-11-09  3:25 ` [RFC][PATCH 3/4] filemap: implement filemap " Jeff Xie
2023-11-09 14:09   ` Matthew Wilcox
2023-11-09 15:43   ` jeff.xie
2023-11-09 15:46     ` Matthew Wilcox
2023-11-09  3:25 ` [RFC][PATCH 4/4] mm/rmap: implement anonmap " Jeff Xie
2023-11-09 14:10   ` Matthew Wilcox
2023-11-09 15:47   ` jeff.xie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).