Skip to contentSkip to conversation

mm/page_alloc: Dedupe some memcg uncharging logic

PATCH Applied Brendan Jackman 3 participants 4 messages Nov 8, 2023 – Nov 9, 2023

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

PATCH v2 1/1 mm/page_alloc: Dedupe some memcg uncharging logic

The duplication makes it seem like some work is required before uncharging in the !PageHWPoison case. But it isn't, so we can simplify the code a little.

Note the PageMemcgKmem check is redundant, but I've left it in as it avoids an unnecessary function call.

---
 mm/page_alloc.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
mm/page_alloc.c +4 -8
index 733732e7e0ba..dd5e8a759d27 100644
@@ -1086,13 +1086,11 @@ static __always_inline bool free_pages_prepare(struct page *page,
 	trace_mm_page_free(page, order);
 	kmsan_free_page(page, order);
 
+	if (memcg_kmem_online() && PageMemcgKmem(page))
+		__memcg_kmem_uncharge_page(page, order);
+
 	if (unlikely(PageHWPoison(page)) && !order) {
-		/*
-		 * Do not let hwpoison pages hit pcplists/buddy
-		 * Untie memcg state and reset page's owner
-		 */
-		if (memcg_kmem_online() && PageMemcgKmem(page))
-			__memcg_kmem_uncharge_page(page, order);
+		/* Do not let hwpoison pages hit pcplists/buddy */
 		reset_page_owner(page, order);
 		page_table_check_free(page, order);
 		return false;
@@ -1123,8 +1121,6 @@ static __always_inline bool free_pages_prepare(struct page *page,
 	}
 	if (PageMappingFlags(page))
 		page->mapping = NULL;
-	if (memcg_kmem_online() && PageMemcgKmem(page))
-		__memcg_kmem_uncharge_page(page, order);
 	if (is_check_pages_enabled()) {
 		if (free_page_is_bad(page))
 			bad++;
Signature · 1 line
2.42.0.869.gea05f2083d-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 36 lines · Brendan Jackman

The duplication makes it seem like some work is required before uncharging in the !PageHWPoison case. But it isn't, so we can simplify the code a little.

Note the PageMemcgKmem check is redundant, but I've left it in as it avoids an unnecessary function call.

---
 mm/page_alloc.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
mm/page_alloc.c +4 -6
index 733732e7e0ba..dd5e8a759d27 100644
@@ -1086,13 +1086,11 @@ static __always_inline bool free_pages_prepare(struct page *page,
        trace_mm_page_free(page, order);
        kmsan_free_page(page, order);
 
+       if (memcg_kmem_online() && PageMemcgKmem(page))
+               __memcg_kmem_uncharge_page(page, order);
+
        if (unlikely(PageHWPoison(page)) && !order) {
-               /*
-                * Do not let hwpoison pages hit pcplists/buddy
-                * Untie memcg state and reset page's owner
-                */
-               if (memcg_kmem_online() && PageMemcgKmem(page))
-                       __memcg_kmem_uncharge_page(page, order);
+               /* Do not let hwpoison pages hit pcplists/buddy */
                reset_page_owner(page, order);
                page_table_check_free(page, order);
                return false;

mm/page_alloc.c static __always_inline bool free_pages_prepare(struct page *page, · line 1127

Quoted 5 lines
mm/page_alloc.c +0 -2
@@ -1123,8 +1121,6 @@ static __always_inline bool free_pages_prepare(struct page *page,
        }
        if (PageMappingFlags(page))
                page->mapping = NULL;
-       if (memcg_kmem_online() && PageMemcgKmem(page))
-               __memcg_kmem_uncharge_page(page, order);

Nothing happening in the function before this point seems to affect the memcg uncharging. It only acts on the head page, and most of the code up until here is acting on tail pages.

LGTM, but I'd be more comfortable if Roman took a look as well.

Quoted 7 lines
mm/page_alloc.c +0 -1
        if (is_check_pages_enabled()) {
                if (free_page_is_bad(page))
                        bad++;
--

2.42.0.869.gea05f2083d-goog

Acked-by on v2 1/1
#2 lore raw
In reply to Brendan Jackman
Quoted 9 lines · Brendan Jackman

The duplication makes it seem like some work is required before uncharging in the !PageHWPoison case. But it isn't, so we can simplify the code a little.

Note the PageMemcgKmem check is redundant, but I've left it in as it avoids an unnecessary function call.

Thanks for the patch. Actually the PageMemcgKmem/folio_memcg_kmem check should be in memcg_kmem_uncharge_page() and not in __memcg_kmem_uncharge_page(). Anyways, that is orthogonal to this patch.

#3 lore raw
Quoted 10 lines · Shakeel Butt
Quoted 8 lines · Brendan Jackman

The duplication makes it seem like some work is required before uncharging in the !PageHWPoison case. But it isn't, so we can simplify the code a little.

Note the PageMemcgKmem check is redundant, but I've left it in as it avoids an unnecessary function call.

Quoted 6 lines
Quoted 1 line

Thanks for the patch. Actually the PageMemcgKmem/folio_memcg_kmem check should be in memcg_kmem_uncharge_page() and not in __memcg_kmem_uncharge_page(). Anyways, that is orthogonal to this patch.

Agreed. If we move the check into memcg_kmem_uncharge_page(), perhaps we should call it directly here instead of doing the checks, since there won't be an extra function call as it is inline, right? We can also make __memcg_kmem_uncharge_page static to mm/memcontrol.c

I suspect the same can be done for __memcg_kmem_charge_page() as well.