Subject: [PATCH 0330/2944] mm: move mem_cgroup_uncharge out of __page_cache_release()

From: Yang Shi <[email-masked]>
Date: Tue, 22 Oct 2019 01:33:53 +0800

commit 7ae88534cdd96235cd775c03b32a75009355740b upstream

A later patch makes THP deferred split shrinker memcg aware, but it
needs page->mem_cgroup information in THP destructor, which is called after
mem_cgroup_uncharge() now.

So move mem_cgroup_uncharge() from __page_cache_release() to compound
page destructor, which is called by both THP and other compound pages except
HugeTLB.  And call it in __put_single_page() for single order page.

Link: http://lkml.kernel.org/r/[email-masked]
Signed-off-by: Yang Shi <[email-masked]>
Suggested-by: "Kirill A . Shutemov" <[email-masked]>
Acked-by: Kirill A. Shutemov <[email-masked]>
Reviewed-by: Kirill Tkhai <[email-masked]>
Cc: Johannes Weiner <[email-masked]>
Cc: Michal Hocko <[email-masked]>
Cc: Hugh Dickins <[email-masked]>
Cc: Shakeel Butt <[email-masked]>
Cc: David Rientjes <[email-masked]>
Cc: Qian Cai <[email-masked]>
Cc: Vladimir Davydov <[email-masked]>
Signed-off-by: Andrew Morton <[email-masked]>
Signed-off-by: Linus Torvalds <[email-masked]>
Reviewed-by: Xunlei Pang <[email-masked]>
---
 mm/page_alloc.c | 1 +
 mm/swap.c       | 2 +-
 mm/vmscan.c     | 7 ++-----
 3 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index f3b5015..faeed52 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -592,6 +592,7 @@ static void bad_page(struct page *page, const char *reason,
 
 void free_compound_page(struct page *page)
 {
+	mem_cgroup_uncharge(page);
 	__free_pages_ok(page, compound_order(page));
 }
 
diff --git a/mm/swap.c b/mm/swap.c
index 45fdbfb..02f6ac5 100644
--- a/mm/swap.c
+++ b/mm/swap.c
@@ -71,12 +71,12 @@ static void __page_cache_release(struct page *page)
 		spin_unlock_irqrestore(zone_lru_lock(zone), flags);
 	}
 	__ClearPageWaiters(page);
-	mem_cgroup_uncharge(page);
 }
 
 static void __put_single_page(struct page *page)
 {
 	__page_cache_release(page);
+	mem_cgroup_uncharge(page);
 	free_unref_page(page);
 }
 
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 350d040..5cb60c8 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1543,10 +1543,9 @@ static unsigned long shrink_page_list(struct list_head *page_list,
 		 * Is there need to periodically free_page_list? It would
 		 * appear not as the counts should be low
 		 */
-		if (unlikely(PageTransHuge(page))) {
-			mem_cgroup_uncharge(page);
+		if (unlikely(PageTransHuge(page)))
 			(*get_compound_page_dtor(page))(page);
-		} else
+		else
 			list_add(&page->lru, &free_pages);
 		continue;
 
@@ -1944,7 +1943,6 @@ static int too_many_isolated(struct pglist_data *pgdat, int file,
 
 			if (unlikely(PageCompound(page))) {
 				spin_unlock_irq(&pgdat->lru_lock);
-				mem_cgroup_uncharge(page);
 				(*get_compound_page_dtor(page))(page);
 				spin_lock_irq(&pgdat->lru_lock);
 			} else
@@ -2134,7 +2132,6 @@ static unsigned move_active_pages_to_lru(struct lruvec *lruvec,
 
 			if (unlikely(PageCompound(page))) {
 				spin_unlock_irq(&pgdat->lru_lock);
-				mem_cgroup_uncharge(page);
 				(*get_compound_page_dtor(page))(page);
 				spin_lock_irq(&pgdat->lru_lock);
 			} else
-- 
1.8.3.1