Skip to contentSkip to conversation

Make deferred split shrinker memcg aware

PATCH Superseded Yang Shi 3 participants 14 messages Jun 12, 2019 – Jun 25, 2019

Subsystems (8)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Yang Shi

author
#0 lore raw

v3 cover Make deferred split shrinker memcg aware

Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:

$ cgcreate -g memory:thp
$ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes
$ cgexec -g memory:thp transhuge-stress 4000

transhuge-stress comes from kernel selftest.

It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.

Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.

Reuse the second tail page's deferred_list for per memcg list since the same THP can't be on multiple deferred split queues.

Make deferred split shrinker not depend on memcg kmem since it is not slab. It doesn’t make sense to not shrink THP even though memcg kmem is disabled.

With the above change the test demonstrated above doesn’t trigger OOM even though with cgroup.memory=nokmem.

Changelog:
v3: * Adopted the suggestion from Kirill Shutemov to move mem_cgroup_uncharge()
out of __page_cache_release() in order to handle THP free properly.
* Adjusted the sequence of the patches per Kirill Shutemov. Dropped the patch 3/4 in v2.
* Moved enqueuing THP onto "to" memcg deferred split queue after page->mem_cgroup is changed in memcg account move per Kirill Tkhai.

v2: * Adopted the suggestion from Krill Shutemov to extract deferred split
fields into a struct to reduce code duplication (patch 1/4). With this change, the lines of change is shrunk down to 198 from 278.
* Removed memcg_deferred_list. Use deferred_list for both global and memcg. With the code deduplication, it doesn't make too much sense to keep it. Kirill Tkhai also suggested so.
* Fixed typo for SHRINKER_NONSLAB.

Yang Shi (4):
mm: thp: extract split_queue_* into a struct
mm: move mem_cgroup_uncharge out of __page_cache_release()
mm: shrinker: make shrinker not depend on memcg kmem
mm: thp: make deferred split shrinker memcg aware

 include/linux/huge_mm.h    |  9 ++++++++
 include/linux/memcontrol.h |  4 ++++
 include/linux/mm_types.h   |  1 +
 include/linux/mmzone.h     | 12 ++++++++---
 include/linux/shrinker.h   |  3 +--
 mm/huge_memory.c           | 80 +++++++++++++++++++++++++++++++++++++++++++++-----------------------
 mm/memcontrol.c            | 24 +++++++++++++++++++++
 mm/page_alloc.c            |  9 +++++---
 mm/swap.c                  |  2 +-
 mm/vmscan.c                | 27 ++++++-----------------
 10 files changed, 114 insertions(+), 57 deletions(-)
author
#1 lore raw

PATCH v3 1/4 mm: thp: extract split_queue_* into a struct

Put split_queue, split_queue_lock and split_queue_len into a struct in order to reduce code duplication when we convert deferred_split to memcg aware in the later patches.

Cc Kirill Tkhai <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc David Rientjes <[email protected]>
---
 include/linux/mmzone.h | 12 +++++++++---
 mm/huge_memory.c       | 45 +++++++++++++++++++++++++--------------------
 mm/page_alloc.c        |  8 +++++---
 3 files changed, 39 insertions(+), 26 deletions(-)
include/linux/mmzone.h +9 -3
index 70394ca..7799166 100644
@@ -676,6 +676,14 @@ struct zonelist {
 extern struct page *mem_map;
 #endif
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+struct deferred_split {
+	spinlock_t split_queue_lock;
+	struct list_head split_queue;
+	unsigned long split_queue_len;
+};
+#endif
+
 /*
  * On NUMA machines, each NUMA node would have a pg_data_t to describe
  * it's memory layout. On UMA machines there is a single pglist_data which
@@ -755,9 +763,7 @@ struct zonelist {
 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
-	spinlock_t split_queue_lock;
-	struct list_head split_queue;
-	unsigned long split_queue_len;
+	struct deferred_split deferred_split_queue;
 #endif
 
 	/* Fields commonly accessed by the page reclaim scanner */
mm/huge_memory.c +25 -20

134 lines not on this page · raw message

mm/page_alloc.c +5 -3
index 3b13d39..a82104a 100644
@@ -6581,9 +6581,11 @@ static unsigned long __init calc_memmap_size(unsigned long spanned_pages,
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 static void pgdat_init_split_queue(struct pglist_data *pgdat)
 {
-	spin_lock_init(&pgdat->split_queue_lock);
-	INIT_LIST_HEAD(&pgdat->split_queue);
-	pgdat->split_queue_len = 0;
+	struct deferred_split *ds_queue = &pgdat->deferred_split_queue;
+
+	spin_lock_init(&ds_queue->split_queue_lock);
+	INIT_LIST_HEAD(&ds_queue->split_queue);
+	ds_queue->split_queue_len = 0;
 }
 #else
 static void pgdat_init_split_queue(struct pglist_data *pgdat) {}
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH v3 2/4 mm: move mem_cgroup_uncharge out of __page_cache_release()

In reply to Yang Shi

The later patch would make 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.

Cc Kirill Tkhai <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc David Rientjes <[email protected]>
---
 mm/page_alloc.c | 1 +
 mm/swap.c       | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
mm/page_alloc.c +1 -0
index a82104a..7f27f4e 100644
@@ -623,6 +623,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));
 }
 
mm/swap.c +1 -1
index 3a75722..982bd79 100644
@@ -70,12 +70,12 @@ static void __page_cache_release(struct page *page)
 		spin_unlock_irqrestore(&pgdat->lru_lock, 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);
 }
 
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [v3 PATCH 2/4] mm: move mem_cgroup_uncharge out of __page_cache_release()

Quoted 8 lines · Yang Shi

The later patch would make 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.

If I read the patch correctly, it will change behaviour for pages with NULL_COMPOUND_DTOR. Have you considered it? Are you sure it will not break anything?

Signature · 1 line
 Kirill A. Shutemov
author
#4 lore raw

Re: [v3 PATCH 2/4] mm: move mem_cgroup_uncharge out of __page_cache_release()

Quoted 5 lines · Kirill A. Shutemov
Quoted 4 lines · Yang Shi

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

Quoted 8 lines
Quoted 4 lines

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.

If I read the patch correctly, it will change behaviour for pages with NULL_COMPOUND_DTOR. Have you considered it? Are you sure it will not break anything?

So far a quick search shows NULL_COMPOUND_DTOR is not used by any type of compound page. The HugeTLB code sets destructor to NULL_COMPOUND_DTOR when freeing hugetlb pages via hugetlb specific destructor.

The prep_new_page() would call prep_compound_page() if __GFP_COMP is used, which sets dtor to COMPOUND_PAGE_DTOR by default.  Just hugetlb and THP set their specific dtors.

And, it looks __put_compound_page() doesn't check if dtor is NULL or not at all.

Quoted 1 line
author
#5 lore raw

Re: [v3 PATCH 2/4] mm: move mem_cgroup_uncharge out of __page_cache_release()

Quoted 8 lines · Yang Shi
Quoted 5 lines · Kirill A. Shutemov
Quoted 4 lines · Yang Shi

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

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

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.

If I read the patch correctly, it will change behaviour for pages with NULL_COMPOUND_DTOR. Have you considered it? Are you sure it will not break anything?

Hi Kirill,

Did this solve your concern? Any more comments on this series?

Thanks,
Yang

Quoted 8 lines

So far a quick search shows NULL_COMPOUND_DTOR is not used by any type of compound page. The HugeTLB code sets destructor to NULL_COMPOUND_DTOR when freeing hugetlb pages via hugetlb specific destructor.

The prep_new_page() would call prep_compound_page() if __GFP_COMP is used, which sets dtor to COMPOUND_PAGE_DTOR by default.  Just hugetlb and THP set their specific dtors.

And, it looks __put_compound_page() doesn't check if dtor is NULL or not at all.

Quoted 1 line
#6 lore raw

Re: [v3 PATCH 2/4] mm: move mem_cgroup_uncharge out of __page_cache_release()

Quoted 23 lines · Yang Shi
Quoted 14 lines · Yang Shi
Quoted 11 lines · Kirill A. Shutemov
Quoted 9 lines · Yang Shi

The later patch would make 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.

Quoted 5 lines
Quoted 4 lines

If I read the patch correctly, it will change behaviour for pages with NULL_COMPOUND_DTOR. Have you considered it? Are you sure it will not break
anything?

Quoted 3 lines

Hi Kirill,

Did this solve your concern? Any more comments on this series?

Everyting looks good now. You can use my

for the series.

Signature · 1 line
 Kirill A. Shutemov
author
#7 lore raw

Re: [v3 PATCH 2/4] mm: move mem_cgroup_uncharge out of __page_cache_release()

Quoted 19 lines · Kirill A. Shutemov
Quoted 18 lines · Yang Shi
Quoted 16 lines · Yang Shi
Quoted 14 lines · Kirill A. Shutemov
Quoted 9 lines · Yang Shi

The later patch would make 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.

If I read the patch correctly, it will change behaviour for pages with NULL_COMPOUND_DTOR. Have you considered it? Are you sure it will not break
anything?

Quoted 8 lines
Quoted 3 lines

Hi Kirill,

Did this solve your concern? Any more comments on this series?

Everyting looks good now. You can use my

for the series.

Thanks!

Quoted 1 line
author
#8 lore raw

PATCH v3 3/4 mm: shrinker: make shrinker not depend on memcg kmem

In reply to Yang Shi

Currently shrinker is just allocated and can work when memcg kmem is enabled. But, THP deferred split shrinker is not slab shrinker, it doesn't make too much sense to have such shrinker depend on memcg kmem. It should be able to reclaim THP even though memcg kmem is disabled.

Introduce a new shrinker flag, SHRINKER_NONSLAB, for non-slab shrinker. When memcg kmem is disabled, just such shrinkers can be called in shrinking memcg slab.

Cc Kirill Tkhai <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc "Kirill A . Shutemov" <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc David Rientjes <[email protected]>
---
 include/linux/shrinker.h |  3 +--
 mm/vmscan.c              | 27 ++++++---------------------
 2 files changed, 7 insertions(+), 23 deletions(-)
include/linux/shrinker.h +1 -2
index 9443caf..e14f68e 100644
@@ -69,10 +69,8 @@ struct shrinker {
 
 	/* These are for internal use */
 	struct list_head list;
-#ifdef CONFIG_MEMCG_KMEM
 	/* ID in shrinker_idr */
 	int id;
-#endif
 	/* objs pending delete, per node */
 	atomic_long_t *nr_deferred;
 };
@@ -81,6 +79,7 @@ struct shrinker {
 /* Flags */
 #define SHRINKER_NUMA_AWARE	(1 << 0)
 #define SHRINKER_MEMCG_AWARE	(1 << 1)
+#define SHRINKER_NONSLAB	(1 << 2)
 
 extern int prealloc_shrinker(struct shrinker *shrinker);
 extern void register_shrinker_prepared(struct shrinker *shrinker);
mm/vmscan.c +6 -21
index 7acd0af..62000ae 100644
@@ -174,8 +174,6 @@ struct scan_control {
 static LIST_HEAD(shrinker_list);
 static DECLARE_RWSEM(shrinker_rwsem);
 
-#ifdef CONFIG_MEMCG_KMEM
-
 /*
  * We allow subsystems to populate their shrinker-related
  * LRU lists before register_shrinker_prepared() is called
@@ -227,16 +225,6 @@ static void unregister_memcg_shrinker(struct shrinker *shrinker)
 	idr_remove(&shrinker_idr, id);
 	up_write(&shrinker_rwsem);
 }
-#else /* CONFIG_MEMCG_KMEM */
-static int prealloc_memcg_shrinker(struct shrinker *shrinker)
-{
-	return 0;
-}
-
-static void unregister_memcg_shrinker(struct shrinker *shrinker)
-{
-}
-#endif /* CONFIG_MEMCG_KMEM */
 
 #ifdef CONFIG_MEMCG
 static bool global_reclaim(struct scan_control *sc)
@@ -579,7 +567,6 @@ static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
 	return freed;
 }
 
-#ifdef CONFIG_MEMCG_KMEM
 static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
 			struct mem_cgroup *memcg, int priority)
 {
@@ -587,7 +574,7 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
 	unsigned long ret, freed = 0;
 	int i;
 
-	if (!memcg_kmem_enabled() || !mem_cgroup_online(memcg))
+	if (!mem_cgroup_online(memcg))
 		return 0;
 
 	if (!down_read_trylock(&shrinker_rwsem))
@@ -613,6 +600,11 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
 			continue;
 		}
 
+		/* Call non-slab shrinkers even though kmem is disabled */
+		if (!memcg_kmem_enabled() &&
+		    !(shrinker->flags & SHRINKER_NONSLAB))
+			continue;
+
 		ret = do_shrink_slab(&sc, shrinker, priority);
 		if (ret == SHRINK_EMPTY) {
 			clear_bit(i, map->map);
@@ -649,13 +641,6 @@ static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
 	up_read(&shrinker_rwsem);
 	return freed;
 }
-#else /* CONFIG_MEMCG_KMEM */
-static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
-			struct mem_cgroup *memcg, int priority)
-{
-	return 0;
-}
-#endif /* CONFIG_MEMCG_KMEM */
 
 /**
  * shrink_slab - shrink slab caches
Signature · 1 line
1.8.3.1
maintainer
#9 lore raw

Re: [v3 PATCH 3/4] mm: shrinker: make shrinker not depend on memcg kmem

Quoted 8 lines · On Thu, 13 Jun 2019 05:56:48 +0800 Yang Shi <[email protected]> wrote:

Currently shrinker is just allocated and can work when memcg kmem is enabled. But, THP deferred split shrinker is not slab shrinker, it doesn't make too much sense to have such shrinker depend on memcg kmem. It should be able to reclaim THP even though memcg kmem is disabled.

Introduce a new shrinker flag, SHRINKER_NONSLAB, for non-slab shrinker. When memcg kmem is disabled, just such shrinkers can be called in shrinking memcg slab.

This causes a couple of compile errors with an allnoconfig build. Please fix that and test any other Kconfig combinations which might trip things up.

author
#10 lore raw

Re: [v3 PATCH 3/4] mm: shrinker: make shrinker not depend on memcg kmem

Quoted 7 lines · Andrew Morton
Quoted 5 lines · On Thu, 13 Jun 2019 05:56:48 +0800 Yang Shi <[email protected]> wrote:

Currently shrinker is just allocated and can work when memcg kmem is enabled. But, THP deferred split shrinker is not slab shrinker, it doesn't make too much sense to have such shrinker depend on memcg kmem. It should be able to reclaim THP even though memcg kmem is disabled.

Quoted 6 lines
Quoted 3 lines

Introduce a new shrinker flag, SHRINKER_NONSLAB, for non-slab shrinker. When memcg kmem is disabled, just such shrinkers can be called in shrinking memcg slab.

This causes a couple of compile errors with an allnoconfig build. Please fix that and test any other Kconfig combinations which might trip things up.

I just tested !CONFIG_TRANSPARENT_HUGEPAGE, but I didn't test !CONFIG_MEMCG. It looks we need keep the code for !CONFIG_MEMCG, will post the corrected patches soon.

author
#11 lore raw

PATCH v3 4/4 mm: thp: make deferred split shrinker memcg aware

In reply to Yang Shi

Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:

$ cgcreate -g memory:thp
$ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes
$ cgexec -g memory:thp transhuge-stress 4000

transhuge-stress comes from kernel selftest.

It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.

Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.

Reuse the second tail page's deferred_list for per memcg list since the same THP can't be on multiple deferred split queues.

Cc Kirill Tkhai <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc "Kirill A . Shutemov" <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc David Rientjes <[email protected]>
---
 include/linux/huge_mm.h    |  9 +++++++++
 include/linux/memcontrol.h |  4 ++++
 include/linux/mm_types.h   |  1 +
 mm/huge_memory.c           | 45 +++++++++++++++++++++++++++++++++------------
 mm/memcontrol.c            | 24 ++++++++++++++++++++++++
 5 files changed, 71 insertions(+), 12 deletions(-)
include/linux/huge_mm.h +9 -0
index 7cd5c15..7738509 100644
@@ -250,6 +250,15 @@ static inline bool thp_migration_supported(void)
 	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
 }
 
+static inline struct list_head *page_deferred_list(struct page *page)
+{
+	/*
+	 * Global or memcg deferred list in the second tail pages is
+	 * occupied by compound_head.
+	 */
+	return &page[2].deferred_list;
+}
+
 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
 #define HPAGE_PMD_SHIFT ({ BUILD_BUG(); 0; })
 #define HPAGE_PMD_MASK ({ BUILD_BUG(); 0; })
include/linux/memcontrol.h +4 -0
index bc74d6a..5d3c10c 100644
@@ -316,6 +316,10 @@ struct mem_cgroup {
 	struct list_head event_list;
 	spinlock_t event_list_lock;
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	struct deferred_split deferred_split_queue;
+#endif
+
 	struct mem_cgroup_per_node *nodeinfo[0];
 	/* WARNING: nodeinfo must be the last member here */
 };
include/linux/mm_types.h +1 -0
index 8ec38b1..4eabf80 100644
@@ -139,6 +139,7 @@ struct page {
 		struct {	/* Second tail page of compound page */
 			unsigned long _compound_pad_1;	/* compound_head */
 			unsigned long _compound_pad_2;
+			/* For both global and memcg */
 			struct list_head deferred_list;
 		};
 		struct {	/* Page table pages */
mm/huge_memory.c +33 -12

99 lines not on this page · raw message

mm/memcontrol.c +24 -0
index e50a2db..16f9390 100644
@@ -4579,6 +4579,11 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 #ifdef CONFIG_CGROUP_WRITEBACK
 	INIT_LIST_HEAD(&memcg->cgwb_list);
 #endif
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
+	INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
+	memcg->deferred_split_queue.split_queue_len = 0;
+#endif
 	idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
 	return memcg;
 fail:
@@ -4949,6 +4954,14 @@ static int mem_cgroup_move_account(struct page *page,
 		__mod_memcg_state(to, NR_WRITEBACK, nr_pages);
 	}
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	if (compound && !list_empty(page_deferred_list(page))) {
+		spin_lock(&from->deferred_split_queue.split_queue_lock);
+		list_del(page_deferred_list(page));
+		from->deferred_split_queue.split_queue_len--;
+		spin_unlock(&from->deferred_split_queue.split_queue_lock);
+	}
+#endif
 	/*
 	 * It is safe to change page->mem_cgroup here because the page
 	 * is referenced, charged, and isolated - we can't race with
@@ -4957,6 +4970,17 @@ static int mem_cgroup_move_account(struct page *page,
 
 	/* caller should have done css_get */
 	page->mem_cgroup = to;
+
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	if (compound && list_empty(page_deferred_list(page))) {
+		spin_lock(&to->deferred_split_queue.split_queue_lock);
+		list_add_tail(page_deferred_list(page),
+			      &to->deferred_split_queue.split_queue);
+		to->deferred_split_queue.split_queue_len++;
+		spin_unlock(&to->deferred_split_queue.split_queue_lock);
+	}
+#endif
+
 	spin_unlock_irqrestore(&from->move_lock, flags);
 
 	ret = 0;
Signature · 1 line
1.8.3.1
maintainer
#12 lore raw

Re: [v3 PATCH 4/4] mm: thp: make deferred split shrinker memcg aware

Quoted 40 lines · On Thu, 13 Jun 2019 05:56:49 +0800 Yang Shi <[email protected]> wrote:

Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:

$ cgcreate -g memory:thp
$ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes
$ cgexec -g memory:thp transhuge-stress 4000

transhuge-stress comes from kernel selftest.

It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.

Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.

Reuse the second tail page's deferred_list for per memcg list since the same THP can't be on multiple deferred split queues.

...

mm/memcontrol.c +5 -0
@@ -4579,6 +4579,11 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
 #ifdef CONFIG_CGROUP_WRITEBACK
 	INIT_LIST_HEAD(&memcg->cgwb_list);
 #endif
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
+	INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
+	memcg->deferred_split_queue.split_queue_len = 0;
+#endif
 	idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
 	return memcg;
 fail:

mm/memcontrol.c static int mem_cgroup_move_account(struct page *page, · line 4960

Quoted 7 lines
mm/memcontrol.c +4 -0
@@ -4949,6 +4954,14 @@ static int mem_cgroup_move_account(struct page *page,
 		__mod_memcg_state(to, NR_WRITEBACK, nr_pages);
 	}
 
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	if (compound && !list_empty(page_deferred_list(page))) {
+		spin_lock(&from->deferred_split_queue.split_queue_lock);
+		list_del(page_deferred_list(page));

It's worrisome that this page still appears to be on the deferred_list and that the above if() would still succeed. Should this be list_del_init()?

Quoted 4 lines
mm/memcontrol.c +4 -0
+		from->deferred_split_queue.split_queue_len--;
+		spin_unlock(&from->deferred_split_queue.split_queue_lock);
+	}
+#endif
author
#13 lore raw

Re: [v3 PATCH 4/4] mm: thp: make deferred split shrinker memcg aware

Quoted 45 lines · Andrew Morton
Quoted 43 lines · On Thu, 13 Jun 2019 05:56:49 +0800 Yang Shi <[email protected]> wrote:

Currently THP deferred split shrinker is not memcg aware, this may cause premature OOM with some configuration. For example the below test would run into premature OOM easily:

$ cgcreate -g memory:thp
$ echo 4G > /sys/fs/cgroup/memory/thp/memory/limit_in_bytes
$ cgexec -g memory:thp transhuge-stress 4000

transhuge-stress comes from kernel selftest.

It is easy to hit OOM, but there are still a lot THP on the deferred split queue, memcg direct reclaim can't touch them since the deferred split shrinker is not memcg aware.

Convert deferred split shrinker memcg aware by introducing per memcg deferred split queue. The THP should be on either per node or per memcg deferred split queue if it belongs to a memcg. When the page is immigrated to the other memcg, it will be immigrated to the target memcg's deferred split queue too.

Reuse the second tail page's deferred_list for per memcg list since the same THP can't be on multiple deferred split queues.

...

mm/memcontrol.c +5 -0
@@ -4579,6 +4579,11 @@ static struct mem_cgroup *mem_cgroup_alloc(void)
  #ifdef CONFIG_CGROUP_WRITEBACK
  	INIT_LIST_HEAD(&memcg->cgwb_list);
  #endif
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	spin_lock_init(&memcg->deferred_split_queue.split_queue_lock);
+	INIT_LIST_HEAD(&memcg->deferred_split_queue.split_queue);
+	memcg->deferred_split_queue.split_queue_len = 0;
+#endif
  	idr_replace(&mem_cgroup_idr, memcg, memcg->id.id);
  	return memcg;
  fail:
@@ -4949,6 +4954,14 @@ static int mem_cgroup_move_account(struct page *page,
  		__mod_memcg_state(to, NR_WRITEBACK, nr_pages);
  	}
 
Quoted 7 lines

mm/memcontrol.c static int mem_cgroup_move_account(struct page *page, · line 4960

Quoted 4 lines
mm/memcontrol.c +4 -0
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+	if (compound && !list_empty(page_deferred_list(page))) {
+		spin_lock(&from->deferred_split_queue.split_queue_lock);
+		list_del(page_deferred_list(page));

It's worrisome that this page still appears to be on the deferred_list and that the above if() would still succeed. Should this be list_del_init()?

list_del_init() sounds safe although I'm not quite sure this is possible. Will update this with fixing build issue together.

Quoted 5 lines
Quoted 5 lines
mm/memcontrol.c +4 -0
+		from->deferred_split_queue.split_queue_len--;
+		spin_unlock(&from->deferred_split_queue.split_queue_lock);
+	}
+#endif