linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] memcg: vmalloc: simplify MEMCG_VMALLOC updates
@ 2025-04-03  5:33 Shakeel Butt
  2025-04-03  7:45 ` Michal Hocko
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Shakeel Butt @ 2025-04-03  5:33 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Uladzislau Rezki, Johannes Weiner, Michal Hocko, Roman Gushchin,
	Muchun Song, linux-mm, cgroups, linux-kernel, Meta kernel team

The vmalloc region can either be charged to a single memcg or none. At
the moment kernel traverses all the pages backing the vmalloc region to
update the MEMCG_VMALLOC stat. However there is no need to look at all
the pages as all those pages will be charged to a single memcg or none.
Simplify the MEMCG_VMALLOC update by just looking at the first page of
the vmalloc region.

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 mm/vmalloc.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 3ed720a787ec..cdae76994488 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -3370,12 +3370,12 @@ void vfree(const void *addr)
 
 	if (unlikely(vm->flags & VM_FLUSH_RESET_PERMS))
 		vm_reset_perms(vm);
+	if (vm->nr_pages && !(vm->flags & VM_MAP_PUT_PAGES))
+		mod_memcg_page_state(vm->pages[0], MEMCG_VMALLOC, -vm->nr_pages);
 	for (i = 0; i < vm->nr_pages; i++) {
 		struct page *page = vm->pages[i];
 
 		BUG_ON(!page);
-		if (!(vm->flags & VM_MAP_PUT_PAGES))
-			mod_memcg_page_state(page, MEMCG_VMALLOC, -1);
 		/*
 		 * High-order allocs for huge vmallocs are split, so
 		 * can be freed as an array of order-0 allocations
@@ -3671,12 +3671,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 		node, page_order, nr_small_pages, area->pages);
 
 	atomic_long_add(area->nr_pages, &nr_vmalloc_pages);
-	if (gfp_mask & __GFP_ACCOUNT) {
-		int i;
-
-		for (i = 0; i < area->nr_pages; i++)
-			mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, 1);
-	}
+	if (gfp_mask & __GFP_ACCOUNT && area->nr_pages)
+		mod_memcg_page_state(area->pages[0], MEMCG_VMALLOC,
+				     area->nr_pages);
 
 	/*
 	 * If not enough pages were obtained to accomplish an
-- 
2.47.1



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

end of thread, other threads:[~2025-04-22 15:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-03  5:33 [PATCH] memcg: vmalloc: simplify MEMCG_VMALLOC updates Shakeel Butt
2025-04-03  7:45 ` Michal Hocko
2025-04-03 11:17 ` Uladzislau Rezki
2025-04-03 18:20   ` Shakeel Butt
2025-04-04 10:34     ` Uladzislau Rezki
2025-04-04 17:44       ` Shakeel Butt
2025-04-07  9:59         ` Uladzislau Rezki
2025-04-03 16:47 ` Johannes Weiner
2025-04-03 18:23   ` Shakeel Butt
2025-04-22 15:17 ` Yosry Ahmed

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