Skip to contentSkip to conversation

mm: use vmalloc fallback path for certain memcg allocations

PATCH In review Anatoly Stepanov 5 participants 14 messages Dec 2, 2016 – Dec 8, 2016

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

PATCH 1/1 mm: use vmalloc fallback path for certain memcg allocations

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

The same story with memcg_lrus allocations.
So let's work this around by utilizing vmalloc fallback path.

---
 include/linux/memcontrol.h | 16 ++++++++++++++++
 mm/list_lru.c              | 14 +++++++-------
 mm/slab_common.c           | 21 ++++++++++++++-------
 3 files changed, 37 insertions(+), 14 deletions(-)
include/linux/memcontrol.h +16 -0
index 61d20c1..a281622 100644
@@ -29,6 +29,9 @@
 #include <linux/mmzone.h>
 #include <linux/writeback.h>
 #include <linux/page-flags.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
 
 struct mem_cgroup;
 struct page;
@@ -878,4 +881,17 @@ static inline void memcg_kmem_update_page_stat(struct page *page,
 }
 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
 
+static inline void memcg_free(const void *ptr)
+{
+	is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
+}
+
+static inline void *memcg_alloc(size_t size)
+{
+	if (likely(size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)))
+		return kzalloc(size, GFP_KERNEL|__GFP_NORETRY);
+
+	return vzalloc(size);
+}
+
 #endif /* _LINUX_MEMCONTROL_H */
mm/list_lru.c +7 -7
index 234676e..8f49339 100644
@@ -327,12 +327,12 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 {
 	int size = memcg_nr_cache_ids;
 
-	nlru->memcg_lrus = kmalloc(size * sizeof(void *), GFP_KERNEL);
-	if (!nlru->memcg_lrus)
+	nlru->memcg_lrus = memcg_alloc(size * sizeof(void *));
+	if (nlru->memcg_lrus == NULL)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(nlru->memcg_lrus, 0, size)) {
-		kfree(nlru->memcg_lrus);
+		memcg_free(nlru->memcg_lrus);
 		return -ENOMEM;
 	}
 
@@ -342,7 +342,7 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
 {
 	__memcg_destroy_list_lru_node(nlru->memcg_lrus, 0, memcg_nr_cache_ids);
-	kfree(nlru->memcg_lrus);
+	memcg_free(nlru->memcg_lrus);
 }
 
 static int memcg_update_list_lru_node(struct list_lru_node *nlru,
@@ -353,12 +353,12 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	BUG_ON(old_size > new_size);
 
 	old = nlru->memcg_lrus;
-	new = kmalloc(new_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(new_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(new, old_size, new_size)) {
-		kfree(new);
+		memcg_free(new);
 		return -ENOMEM;
 	}
 
@@ -375,7 +375,7 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	nlru->memcg_lrus = new;
 	spin_unlock_irq(&nlru->lock);
 
-	kfree(old);
+	memcg_free(old);
 	return 0;
 }
 
mm/slab_common.c +14 -7
index 329b038..19f8cb5 100644
@@ -157,9 +157,8 @@ static int init_memcg_params(struct kmem_cache *s,
 	if (!memcg_nr_cache_ids)
 		return 0;
 
-	arr = kzalloc(sizeof(struct memcg_cache_array) +
-		      memcg_nr_cache_ids * sizeof(void *),
-		      GFP_KERNEL);
+	arr = memcg_alloc(sizeof(struct memcg_cache_array) +
+			memcg_nr_cache_ids * sizeof(void *));
 	if (!arr)
 		return -ENOMEM;
 
@@ -170,7 +169,15 @@ static int init_memcg_params(struct kmem_cache *s,
 static void destroy_memcg_params(struct kmem_cache *s)
 {
 	if (is_root_cache(s))
-		kfree(rcu_access_pointer(s->memcg_params.memcg_caches));
+		memcg_free(rcu_access_pointer(s->memcg_params.memcg_caches));
+}
+
+static void memcg_rcu_free(struct rcu_head *rcu)
+{
+	struct memcg_cache_array *arr;
+
+	arr = container_of(rcu, struct memcg_cache_array, rcu);
+	memcg_free(arr);
 }
 
 static int update_memcg_params(struct kmem_cache *s, int new_array_size)
@@ -180,8 +187,8 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 	if (!is_root_cache(s))
 		return 0;
 
-	new = kzalloc(sizeof(struct memcg_cache_array) +
-		      new_array_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(sizeof(struct memcg_cache_array) +
+				new_array_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 
@@ -193,7 +200,7 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 
 	rcu_assign_pointer(s->memcg_params.memcg_caches, new);
 	if (old)
-		kfree_rcu(old, rcu);
+		call_rcu(&old->rcu, memcg_rcu_free);
 	return 0;
 }
 
Signature · 7 lines
1.8.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 36 lines

1 дек. 2016 г., в 4:16, Anatoly Stepanov <[email protected]> написал(а):

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

The same story with memcg_lrus allocations.
So let's work this around by utilizing vmalloc fallback path.

---

include/linux/memcontrol.h | 16 ++++++++++++++++
mm/list_lru.c              | 14 +++++++-------
mm/slab_common.c           | 21 ++++++++++++++-------
3 files changed, 37 insertions(+), 14 deletions(-)
include/linux/memcontrol.h +3 -0
index 61d20c1..a281622 100644
@@ -29,6 +29,9 @@
 #include <linux/mmzone.h>
 #include <linux/writeback.h>
 #include <linux/page-flags.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
 
 struct mem_cgroup;
 struct page;

include/linux/memcontrol.h static inline void memcg_kmem_update_page_stat(struct page *page, · line 887

Quoted 7 lines
include/linux/memcontrol.h +4 -0
@@ -878,4 +881,17 @@ static inline void memcg_kmem_update_page_stat(struct page *page,
 }
 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
 
+static inline void memcg_free(const void *ptr)
+{
+	is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
+}

please to use a kvfree() instead.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
In reply to Anatoly Stepanov
Quoted 37 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

The same story with memcg_lrus allocations.
So let's work this around by utilizing vmalloc fallback path.

---
 include/linux/memcontrol.h | 16 ++++++++++++++++
 mm/list_lru.c              | 14 +++++++-------
 mm/slab_common.c           | 21 ++++++++++++++-------
 3 files changed, 37 insertions(+), 14 deletions(-)
include/linux/memcontrol.h +4 -0
index 61d20c1..a281622 100644
@@ -29,6 +29,9 @@
 #include <linux/mmzone.h>
 #include <linux/writeback.h>
 #include <linux/page-flags.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
 
 struct mem_cgroup;
 struct page;
@@ -878,4 +881,17 @@ static inline void memcg_kmem_update_page_stat(struct page *page,
 }
 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
 
+static inline void memcg_free(const void *ptr)

include/linux/memcontrol.h static inline void memcg_kmem_update_page_stat(struct page *page, · line 894

Quoted 10 lines
include/linux/memcontrol.h +10 -0
+{
+	is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
+}
+
+static inline void *memcg_alloc(size_t size)
+{
+	if (likely(size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)))
+		return kzalloc(size, GFP_KERNEL|__GFP_NORETRY);
+
+	return vzalloc(size);

That's not how I imagine a "fallback" to work. You should be trying kzalloc() and if that fails, call vzalloc(), not distinguish it by costly order check. Also IIRC __GFP_NORETRY can easily fail even for non-costly orders.

Quoted 102 lines
include/linux/memcontrol.h +2 -0
+}
+
 #endif /* _LINUX_MEMCONTROL_H */
mm/list_lru.c +7 -7
index 234676e..8f49339 100644
@@ -327,12 +327,12 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 {
 	int size = memcg_nr_cache_ids;
 
-	nlru->memcg_lrus = kmalloc(size * sizeof(void *), GFP_KERNEL);
-	if (!nlru->memcg_lrus)
+	nlru->memcg_lrus = memcg_alloc(size * sizeof(void *));
+	if (nlru->memcg_lrus == NULL)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(nlru->memcg_lrus, 0, size)) {
-		kfree(nlru->memcg_lrus);
+		memcg_free(nlru->memcg_lrus);
 		return -ENOMEM;
 	}
 
@@ -342,7 +342,7 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
 {
 	__memcg_destroy_list_lru_node(nlru->memcg_lrus, 0, memcg_nr_cache_ids);
-	kfree(nlru->memcg_lrus);
+	memcg_free(nlru->memcg_lrus);
 }
 
 static int memcg_update_list_lru_node(struct list_lru_node *nlru,
@@ -353,12 +353,12 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	BUG_ON(old_size > new_size);
 
 	old = nlru->memcg_lrus;
-	new = kmalloc(new_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(new_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(new, old_size, new_size)) {
-		kfree(new);
+		memcg_free(new);
 		return -ENOMEM;
 	}
 
@@ -375,7 +375,7 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	nlru->memcg_lrus = new;
 	spin_unlock_irq(&nlru->lock);
 
-	kfree(old);
+	memcg_free(old);
 	return 0;
 }
 
mm/slab_common.c +13 -6
index 329b038..19f8cb5 100644
@@ -157,9 +157,8 @@ static int init_memcg_params(struct kmem_cache *s,
 	if (!memcg_nr_cache_ids)
 		return 0;
 
-	arr = kzalloc(sizeof(struct memcg_cache_array) +
-		      memcg_nr_cache_ids * sizeof(void *),
-		      GFP_KERNEL);
+	arr = memcg_alloc(sizeof(struct memcg_cache_array) +
+			memcg_nr_cache_ids * sizeof(void *));
 	if (!arr)
 		return -ENOMEM;
 
@@ -170,7 +169,15 @@ static int init_memcg_params(struct kmem_cache *s,
 static void destroy_memcg_params(struct kmem_cache *s)
 {
 	if (is_root_cache(s))
-		kfree(rcu_access_pointer(s->memcg_params.memcg_caches));
+		memcg_free(rcu_access_pointer(s->memcg_params.memcg_caches));
+}
+
+static void memcg_rcu_free(struct rcu_head *rcu)
+{
+	struct memcg_cache_array *arr;
+
+	arr = container_of(rcu, struct memcg_cache_array, rcu);
+	memcg_free(arr);
 }
 
 static int update_memcg_params(struct kmem_cache *s, int new_array_size)
@@ -180,8 +187,8 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 	if (!is_root_cache(s))
 		return 0;
 
-	new = kzalloc(sizeof(struct memcg_cache_array) +
-		      new_array_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(sizeof(struct memcg_cache_array) +
+				new_array_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 

mm/slab_common.c static int update_memcg_params(struct kmem_cache *s, int new_array_size) · line 206

Quoted 8 lines
mm/slab_common.c +1 -1
@@ -193,7 +200,7 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 
 	rcu_assign_pointer(s->memcg_params.memcg_caches, new);
 	if (old)
-		kfree_rcu(old, rcu);
+		call_rcu(&old->rcu, memcg_rcu_free);
 	return 0;
 }
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Thanks for CCing me Vlastimil

Quoted 8 lines · Vlastimil Babka
Quoted 7 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

This is definitely not nice and worth fixing! I am just wondering whether this is something you have encountered in the real life. Having thousands of memcgs sounds quite crazy^Wscary to me. I am not at all sure we are prepared for that and some controllers would have real issues with it AFAIR.

Quoted 28 lines
Quoted 28 lines

The same story with memcg_lrus allocations.
So let's work this around by utilizing vmalloc fallback path.

---
 include/linux/memcontrol.h | 16 ++++++++++++++++
 mm/list_lru.c              | 14 +++++++-------
 mm/slab_common.c           | 21 ++++++++++++++-------
 3 files changed, 37 insertions(+), 14 deletions(-)
include/linux/memcontrol.h +3 -0
index 61d20c1..a281622 100644
@@ -29,6 +29,9 @@
 #include <linux/mmzone.h>
 #include <linux/writeback.h>
 #include <linux/page-flags.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
 
 struct mem_cgroup;
 struct page;
@@ -878,4 +881,17 @@ static inline void memcg_kmem_update_page_stat(struct page *page,
 }
 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
 
Quoted 8 lines

include/linux/memcontrol.h static inline void memcg_kmem_update_page_stat(struct page *page, · line 891

Quoted 8 lines
include/linux/memcontrol.h +8 -0
+static inline void memcg_free(const void *ptr)
+{
+	is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
+}
+
+static inline void *memcg_alloc(size_t size)
+{
+	if (likely(size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)))

you are mixing two different units here...

Quoted 8 lines

include/linux/memcontrol.h static inline void memcg_kmem_update_page_stat(struct page *page, · line 894

Quoted 3 lines
include/linux/memcontrol.h +3 -0
+		return kzalloc(size, GFP_KERNEL|__GFP_NORETRY);
+
+	return vzalloc(size);

That's not how I imagine a "fallback" to work. You should be trying kzalloc() and if that fails, call vzalloc(), not distinguish it by costly order check. Also IIRC __GFP_NORETRY can easily fail even for non-costly orders.

Completely agreed! This should be done simply by

	gfp_t gfp_mask = GFP_KERNEL;
	void *ret;
	
	/*
	 * Do not invoke OOM killer for larger requests as we can fall
	 * back to the vmalloc
	 */
	if (size > PAGE_SIZE)
		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;

	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Quoted 111 lines
Quoted 111 lines
include/linux/memcontrol.h +2 -0
+}
+
 #endif /* _LINUX_MEMCONTROL_H */
mm/list_lru.c +7 -7
index 234676e..8f49339 100644
@@ -327,12 +327,12 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 {
 	int size = memcg_nr_cache_ids;
 
-	nlru->memcg_lrus = kmalloc(size * sizeof(void *), GFP_KERNEL);
-	if (!nlru->memcg_lrus)
+	nlru->memcg_lrus = memcg_alloc(size * sizeof(void *));
+	if (nlru->memcg_lrus == NULL)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(nlru->memcg_lrus, 0, size)) {
-		kfree(nlru->memcg_lrus);
+		memcg_free(nlru->memcg_lrus);
 		return -ENOMEM;
 	}
 
@@ -342,7 +342,7 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
 {
 	__memcg_destroy_list_lru_node(nlru->memcg_lrus, 0, memcg_nr_cache_ids);
-	kfree(nlru->memcg_lrus);
+	memcg_free(nlru->memcg_lrus);
 }
 
 static int memcg_update_list_lru_node(struct list_lru_node *nlru,
@@ -353,12 +353,12 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	BUG_ON(old_size > new_size);
 
 	old = nlru->memcg_lrus;
-	new = kmalloc(new_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(new_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(new, old_size, new_size)) {
-		kfree(new);
+		memcg_free(new);
 		return -ENOMEM;
 	}
 
@@ -375,7 +375,7 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	nlru->memcg_lrus = new;
 	spin_unlock_irq(&nlru->lock);
 
-	kfree(old);
+	memcg_free(old);
 	return 0;
 }
 
mm/slab_common.c +14 -7
index 329b038..19f8cb5 100644
@@ -157,9 +157,8 @@ static int init_memcg_params(struct kmem_cache *s,
 	if (!memcg_nr_cache_ids)
 		return 0;
 
-	arr = kzalloc(sizeof(struct memcg_cache_array) +
-		      memcg_nr_cache_ids * sizeof(void *),
-		      GFP_KERNEL);
+	arr = memcg_alloc(sizeof(struct memcg_cache_array) +
+			memcg_nr_cache_ids * sizeof(void *));
 	if (!arr)
 		return -ENOMEM;
 
@@ -170,7 +169,15 @@ static int init_memcg_params(struct kmem_cache *s,
 static void destroy_memcg_params(struct kmem_cache *s)
 {
 	if (is_root_cache(s))
-		kfree(rcu_access_pointer(s->memcg_params.memcg_caches));
+		memcg_free(rcu_access_pointer(s->memcg_params.memcg_caches));
+}
+
+static void memcg_rcu_free(struct rcu_head *rcu)
+{
+	struct memcg_cache_array *arr;
+
+	arr = container_of(rcu, struct memcg_cache_array, rcu);
+	memcg_free(arr);
 }
 
 static int update_memcg_params(struct kmem_cache *s, int new_array_size)
@@ -180,8 +187,8 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 	if (!is_root_cache(s))
 		return 0;
 
-	new = kzalloc(sizeof(struct memcg_cache_array) +
-		      new_array_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(sizeof(struct memcg_cache_array) +
+				new_array_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 
@@ -193,7 +200,7 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 
 	rcu_assign_pointer(s->memcg_params.memcg_caches, new);
 	if (old)
-		kfree_rcu(old, rcu);
+		call_rcu(&old->rcu, memcg_rcu_free);
 	return 0;
 }
 
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

Alex, Vlasimil, Michal, thanks for your responses!

Quoted 9 lines · Michal Hocko

Thanks for CCing me Vlastimil

Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

This is definitely not nice and worth fixing! I am just wondering whether this is something you have encountered in the real life. Having thousands of memcgs sounds quite crazy^Wscary to me. I am not at all sure we are prepared for that and some controllers would have real issues with it AFAIR.

In our company we use custom-made lightweight container technology, the thing is we can have up to several thousands of them on a server.
So those high-order allocations were observed on a real production workload.

Quoted 49 lines
Quoted 28 lines
Quoted 28 lines

The same story with memcg_lrus allocations.
So let's work this around by utilizing vmalloc fallback path.

---
 include/linux/memcontrol.h | 16 ++++++++++++++++
 mm/list_lru.c              | 14 +++++++-------
 mm/slab_common.c           | 21 ++++++++++++++-------
 3 files changed, 37 insertions(+), 14 deletions(-)
include/linux/memcontrol.h +3 -0
index 61d20c1..a281622 100644
@@ -29,6 +29,9 @@
 #include <linux/mmzone.h>
 #include <linux/writeback.h>
 #include <linux/page-flags.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
 
 struct mem_cgroup;
 struct page;
@@ -878,4 +881,17 @@ static inline void memcg_kmem_update_page_stat(struct page *page,
 }
 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
 
Quoted 8 lines

include/linux/memcontrol.h static inline void memcg_kmem_update_page_stat(struct page *page, · line 891

Quoted 8 lines
include/linux/memcontrol.h +8 -0
+static inline void memcg_free(const void *ptr)
+{
+	is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
+}
+
+static inline void *memcg_alloc(size_t size)
+{
+	if (likely(size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)))

you are mixing two different units here...

Quoted 8 lines

include/linux/memcontrol.h static inline void memcg_kmem_update_page_stat(struct page *page, · line 894

Quoted 3 lines
include/linux/memcontrol.h +3 -0
+		return kzalloc(size, GFP_KERNEL|__GFP_NORETRY);
+
+	return vzalloc(size);

That's not how I imagine a "fallback" to work. You should be trying kzalloc() and if that fails, call vzalloc(), not distinguish it by costly order check. Also IIRC __GFP_NORETRY can easily fail even for non-costly orders.

Completely agreed! This should be done simply by

Quoted 9 lines
	gfp_t gfp_mask = GFP_KERNEL;
	void *ret;
	
	/*
	 * Do not invoke OOM killer for larger requests as we can fall
	 * back to the vmalloc
	 */
	if (size > PAGE_SIZE)
		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;

I think we should check against PAGE_ALLOC_COSTLY_ORDER anyway,
as there's no big need to allocate large contiguous chunks here, at the same time someone in the kernel might really need them.

Quoted 11 lines
	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Agreed, it would be nice to have a generic call.
I would suggest an impl. like this:

void *kvmalloc(size_t size)

{
	gfp_t gfp_mask = GFP_KERNEL;
	void *ret;

if (size > PAGE_SIZE)
gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;

	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
		ret = kzalloc(size, gfp_mask);
		if (ret)
			return ret;
	}
	
	return vzalloc(size);
}
Quoted 103 lines
Quoted 103 lines
Quoted 103 lines
include/linux/memcontrol.h +2 -0
+}
+
 #endif /* _LINUX_MEMCONTROL_H */
mm/list_lru.c +7 -7
index 234676e..8f49339 100644
@@ -327,12 +327,12 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 {
 	int size = memcg_nr_cache_ids;
 
-	nlru->memcg_lrus = kmalloc(size * sizeof(void *), GFP_KERNEL);
-	if (!nlru->memcg_lrus)
+	nlru->memcg_lrus = memcg_alloc(size * sizeof(void *));
+	if (nlru->memcg_lrus == NULL)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(nlru->memcg_lrus, 0, size)) {
-		kfree(nlru->memcg_lrus);
+		memcg_free(nlru->memcg_lrus);
 		return -ENOMEM;
 	}
 
@@ -342,7 +342,7 @@ static int memcg_init_list_lru_node(struct list_lru_node *nlru)
 static void memcg_destroy_list_lru_node(struct list_lru_node *nlru)
 {
 	__memcg_destroy_list_lru_node(nlru->memcg_lrus, 0, memcg_nr_cache_ids);
-	kfree(nlru->memcg_lrus);
+	memcg_free(nlru->memcg_lrus);
 }
 
 static int memcg_update_list_lru_node(struct list_lru_node *nlru,
@@ -353,12 +353,12 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	BUG_ON(old_size > new_size);
 
 	old = nlru->memcg_lrus;
-	new = kmalloc(new_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(new_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 
 	if (__memcg_init_list_lru_node(new, old_size, new_size)) {
-		kfree(new);
+		memcg_free(new);
 		return -ENOMEM;
 	}
 
@@ -375,7 +375,7 @@ static int memcg_update_list_lru_node(struct list_lru_node *nlru,
 	nlru->memcg_lrus = new;
 	spin_unlock_irq(&nlru->lock);
 
-	kfree(old);
+	memcg_free(old);
 	return 0;
 }
 
mm/slab_common.c +13 -6
index 329b038..19f8cb5 100644
@@ -157,9 +157,8 @@ static int init_memcg_params(struct kmem_cache *s,
 	if (!memcg_nr_cache_ids)
 		return 0;
 
-	arr = kzalloc(sizeof(struct memcg_cache_array) +
-		      memcg_nr_cache_ids * sizeof(void *),
-		      GFP_KERNEL);
+	arr = memcg_alloc(sizeof(struct memcg_cache_array) +
+			memcg_nr_cache_ids * sizeof(void *));
 	if (!arr)
 		return -ENOMEM;
 
@@ -170,7 +169,15 @@ static int init_memcg_params(struct kmem_cache *s,
 static void destroy_memcg_params(struct kmem_cache *s)
 {
 	if (is_root_cache(s))
-		kfree(rcu_access_pointer(s->memcg_params.memcg_caches));
+		memcg_free(rcu_access_pointer(s->memcg_params.memcg_caches));
+}
+
+static void memcg_rcu_free(struct rcu_head *rcu)
+{
+	struct memcg_cache_array *arr;
+
+	arr = container_of(rcu, struct memcg_cache_array, rcu);
+	memcg_free(arr);
 }
 
 static int update_memcg_params(struct kmem_cache *s, int new_array_size)
@@ -180,8 +187,8 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 	if (!is_root_cache(s))
 		return 0;
 
-	new = kzalloc(sizeof(struct memcg_cache_array) +
-		      new_array_size * sizeof(void *), GFP_KERNEL);
+	new = memcg_alloc(sizeof(struct memcg_cache_array) +
+				new_array_size * sizeof(void *));
 	if (!new)
 		return -ENOMEM;
 
@@ -193,7 +200,7 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 
Quoted 12 lines
Quoted 8 lines
Quoted 8 lines
mm/slab_common.c +1 -1
 	rcu_assign_pointer(s->memcg_params.memcg_caches, new);
 	if (old)
-		kfree_rcu(old, rcu);
+		call_rcu(&old->rcu, memcg_rcu_free);
 	return 0;
 }
 
Signature · 2 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 21 lines · Anatoly Stepanov

Alex, Vlasimil, Michal, thanks for your responses!

Quoted 9 lines · Michal Hocko

Thanks for CCing me Vlastimil

Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

This is definitely not nice and worth fixing! I am just wondering whether this is something you have encountered in the real life. Having thousands of memcgs sounds quite crazy^Wscary to me. I am not at all sure we are prepared for that and some controllers would have real issues with it AFAIR.

Quoted 3 lines

In our company we use custom-made lightweight container technology, the thing is we can have up to several thousands of them on a server.
So those high-order allocations were observed on a real production workload.

OK, this is interesting. Definitely worth mentioning in the changelog!

[...]

Quoted 10 lines
Quoted 6 lines
	/*
	 * Do not invoke OOM killer for larger requests as we can fall
	 * back to the vmalloc
	 */
	if (size > PAGE_SIZE)
		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;

I think we should check against PAGE_ALLOC_COSTLY_ORDER anyway, as there's no big need to allocate large contiguous chunks here, at the same time someone in the kernel might really need them.

PAGE_ALLOC_COSTLY_ORDER is and should remain the page allocator internal implementation detail and shouldn't spread out much outside. GFP_NORETRY will already make sure we do not push hard here.

Quoted 13 lines
Quoted 8 lines

	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);
Quoted 4 lines

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Quoted 4 lines

Agreed, it would be nice to have a generic call.
I would suggest an impl. like this:

void *kvmalloc(size_t size)

gfp_t gfp_mask should be a parameter as this should be a generic helper.

Quoted 10 lines
{
	gfp_t gfp_mask = GFP_KERNEL;


	void *ret;
(quoted diff) +0 -0
 	if (size > PAGE_SIZE)
 		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 5 lines
	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
		ret = kzalloc(size, gfp_mask);
		if (ret)
			return ret;
	}

No, please just do as suggested above. Tweak the gfp_mask for higher order requests and do kmalloc first with vmalloc as a fallback.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw
Quoted 29 lines · Michal Hocko
Quoted 12 lines
Quoted 11 lines
	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Quoted 4 lines

Agreed, it would be nice to have a generic call.
I would suggest an impl. like this:

void *kvmalloc(size_t size)

gfp_t gfp_mask should be a parameter as this should be a generic helper.

Quoted 10 lines
{
	gfp_t gfp_mask = GFP_KERNEL;


	void *ret;
(quoted diff) +0 -0
 	if (size > PAGE_SIZE)
 		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 8 lines
Quoted 5 lines
	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
		ret = kzalloc(size, gfp_mask);
		if (ret)
			return ret;
	}

No, please just do as suggested above. Tweak the gfp_mask for higher order requests and do kmalloc first with vmalloc as a fallback.

You may simply use the slightly different and open-coded variant within fs/seq_file.c:seq_buf_alloc(). That one got a lot of testing in the meantime...

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
Quoted 36 lines · Heiko Carstens
Quoted 35 lines · Michal Hocko
Quoted 12 lines
Quoted 11 lines
	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Quoted 4 lines

Agreed, it would be nice to have a generic call.
I would suggest an impl. like this:

void *kvmalloc(size_t size)

gfp_t gfp_mask should be a parameter as this should be a generic helper.

Quoted 15 lines
{
	gfp_t gfp_mask = GFP_KERNEL;


	void *ret;
(quoted diff) +0 -0
 	if (size > PAGE_SIZE)
 		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;

	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
		ret = kzalloc(size, gfp_mask);
		if (ret)
			return ret;
	}
Quoted 6 lines
Quoted 2 lines

No, please just do as suggested above. Tweak the gfp_mask for higher order requests and do kmalloc first with vmalloc as a fallback.

You may simply use the slightly different and open-coded variant within fs/seq_file.c:seq_buf_alloc(). That one got a lot of testing in the meantime...

Yeah. I would just add WARN_ON((gfp_mask & GFP_KERNEL) != GFP_KERNEL) to catch users who might want to rely on GFP_NOFS, GFP_NOWAIT or other restricted requests because vmalloc cannot cope with those properly.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#8 lore raw
Quoted 40 lines · Michal Hocko
Quoted 39 lines · Heiko Carstens
Quoted 37 lines · Michal Hocko
Quoted 12 lines
Quoted 11 lines
	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Quoted 4 lines

Agreed, it would be nice to have a generic call.
I would suggest an impl. like this:

void *kvmalloc(size_t size)

gfp_t gfp_mask should be a parameter as this should be a generic helper.

Quoted 10 lines
{
	gfp_t gfp_mask = GFP_KERNEL;


	void *ret;
(quoted diff) +0 -0
 	if (size > PAGE_SIZE)
 		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 5 lines
	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
		ret = kzalloc(size, gfp_mask);
		if (ret)
			return ret;
	}

No, please just do as suggested above. Tweak the gfp_mask for higher order requests and do kmalloc first with vmalloc as a fallback.

Quoted 7 lines
Quoted 3 lines

You may simply use the slightly different and open-coded variant within fs/seq_file.c:seq_buf_alloc(). That one got a lot of testing in the meantime...

Yeah. I would just add WARN_ON((gfp_mask & GFP_KERNEL) != GFP_KERNEL) to catch users who might want to rely on GFP_NOFS, GFP_NOWAIT or other restricted requests because vmalloc cannot cope with those properly.

What about __vmalloc(size, gfp, prot)? I guess it's fine with theese

Quoted 4 lines
Signature · 3 lines
-- 
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw
Quoted 45 lines · Anatoly Stepanov
Quoted 44 lines · Michal Hocko
Quoted 42 lines · Heiko Carstens
Quoted 29 lines · Michal Hocko
Quoted 12 lines
Quoted 11 lines
	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Quoted 4 lines

Agreed, it would be nice to have a generic call.
I would suggest an impl. like this:

void *kvmalloc(size_t size)

gfp_t gfp_mask should be a parameter as this should be a generic helper.

Quoted 10 lines
{
	gfp_t gfp_mask = GFP_KERNEL;


	void *ret;
(quoted diff) +0 -0
 	if (size > PAGE_SIZE)
 		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 8 lines
Quoted 5 lines
	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
		ret = kzalloc(size, gfp_mask);
		if (ret)
			return ret;
	}

No, please just do as suggested above. Tweak the gfp_mask for higher order requests and do kmalloc first with vmalloc as a fallback.

You may simply use the slightly different and open-coded variant within fs/seq_file.c:seq_buf_alloc(). That one got a lot of testing in the meantime...

Quoted 5 lines
Quoted 3 lines

Yeah. I would just add WARN_ON((gfp_mask & GFP_KERNEL) != GFP_KERNEL) to catch users who might want to rely on GFP_NOFS, GFP_NOWAIT or other restricted requests because vmalloc cannot cope with those properly.

What about __vmalloc(size, gfp, prot)? I guess it's fine with theese

Not really. Parts of the vmalloc allocator still use hardcoded GFP_KERNEL AFAIR.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#10 lore raw
In reply to Michal Hocko
Quoted 36 lines · Michal Hocko
Quoted 21 lines · Anatoly Stepanov

Alex, Vlasimil, Michal, thanks for your responses!

Quoted 9 lines · Michal Hocko

Thanks for CCing me Vlastimil

Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

This is definitely not nice and worth fixing! I am just wondering whether this is something you have encountered in the real life. Having thousands of memcgs sounds quite crazy^Wscary to me. I am not at all sure we are prepared for that and some controllers would have real issues with it AFAIR.

Quoted 3 lines

In our company we use custom-made lightweight container technology, the thing is we can have up to several thousands of them on a server.
So those high-order allocations were observed on a real production workload.

OK, this is interesting. Definitely worth mentioning in the changelog!

[...]

Quoted 7 lines
Quoted 6 lines
	/*
	 * Do not invoke OOM killer for larger requests as we can fall
	 * back to the vmalloc
	 */
	if (size > PAGE_SIZE)
		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 7 lines
Quoted 3 lines

I think we should check against PAGE_ALLOC_COSTLY_ORDER anyway, as there's no big need to allocate large contiguous chunks here, at the same time someone in the kernel might really need them.

PAGE_ALLOC_COSTLY_ORDER is and should remain the page allocator internal implementation detail and shouldn't spread out much outside. GFP_NORETRY will already make sure we do not push hard here.

May be i didn't put my thoughts well, so let's discuss in more detail:

1. Yes, we don't try that hard to allocate high-order blocks with __GFP_NORETRY, but we still can do compaction and direct reclaim, which can be heavy for large chunk. In the worst case we can even fail to find the chunk, after all reclaim/compaction steps were made.

2. The second point is, even if we got the desired chunk quickly, we end up wasting large contiguous chunks, which might be needed for CMA or some h/w driver (DMA for inst.), when they can't use non-contiguous chunks.

BTW, in the kernel there are few examples like alloc_fdmem() for inst., which use that "costly order" idea of the fallback.

static void *alloc_fdmem(size_t size)

{
        /*
         * Very large allocations can stress page reclaim, so fall back to
         * vmalloc() if the allocation size will be considered "large" by the VM.
         */
        if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
                void *data = kmalloc(size, GFP_KERNEL_ACCOUNT |
                                     __GFP_NOWARN | __GFP_NORETRY);
                if (data != NULL)
                        return data;
        }
        return __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM, PAGE_KERNEL);
}

Also in netfilter,ceph and some other places.

Please, correct me if i'am somewhere mistaken with this thoughts.

And don't get me wrong, i'm just trying to get deeper into the problem, especially as we're going to introduce something more or less generic.

Quoted 31 lines
Quoted 14 lines
Quoted 9 lines


	ret = kzalloc(size, gfp_mask);
	if (ret)
		return ret;
	return vzalloc(size);
Quoted 4 lines

I also do not like memcg_alloc helper name. It suggests we are allocating a memcg while it is used for cache arrays and slab LRUS. Anyway this pattern is quite widespread in the kernel so I would simply suggest adding kvmalloc function instead.

Quoted 4 lines

Agreed, it would be nice to have a generic call.
I would suggest an impl. like this:

void *kvmalloc(size_t size)

gfp_t gfp_mask should be a parameter as this should be a generic helper.

Quoted 10 lines
{
	gfp_t gfp_mask = GFP_KERNEL;


	void *ret;
(quoted diff) +0 -0
 	if (size > PAGE_SIZE)
 		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 12 lines
Quoted 5 lines
	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
		ret = kzalloc(size, gfp_mask);
		if (ret)
			return ret;
	}

No, please just do as suggested above. Tweak the gfp_mask for higher order requests and do kmalloc first with vmalloc as a fallback.

Signature · 2 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw
Quoted 45 lines · Anatoly Stepanov
Quoted 36 lines · Michal Hocko
Quoted 21 lines · Anatoly Stepanov

Alex, Vlasimil, Michal, thanks for your responses!

Quoted 9 lines · Michal Hocko

Thanks for CCing me Vlastimil

Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

This is definitely not nice and worth fixing! I am just wondering whether this is something you have encountered in the real life. Having thousands of memcgs sounds quite crazy^Wscary to me. I am not at all sure we are prepared for that and some controllers would have real issues with it AFAIR.

Quoted 3 lines

In our company we use custom-made lightweight container technology, the thing is we can have up to several thousands of them on a server.
So those high-order allocations were observed on a real production workload.

OK, this is interesting. Definitely worth mentioning in the changelog!

[...]

Quoted 7 lines
Quoted 6 lines
	/*
	 * Do not invoke OOM killer for larger requests as we can fall
	 * back to the vmalloc
	 */
	if (size > PAGE_SIZE)
		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 7 lines
Quoted 3 lines

I think we should check against PAGE_ALLOC_COSTLY_ORDER anyway, as there's no big need to allocate large contiguous chunks here, at the same time someone in the kernel might really need them.

PAGE_ALLOC_COSTLY_ORDER is and should remain the page allocator internal implementation detail and shouldn't spread out much outside. GFP_NORETRY will already make sure we do not push hard here.

Quoted 6 lines

May be i didn't put my thoughts well, so let's discuss in more detail:

1. Yes, we don't try that hard to allocate high-order blocks with __GFP_NORETRY, but we still can do compaction and direct reclaim, which can be heavy for large chunk. In the worst case we can even fail to find the chunk, after all reclaim/compaction steps were made.

Yes this is correct. But I am not sure what you are trying to tell by that. Highorder requests are a bit of a problem. That's why __GFP_NORETRY is implicit here. It also guarantees that we won't hit the OOM killer because we do have a reasonable fallback. I do not see a point to play with COSTLY_ORDER though. The page allocator knows how to handle those and we are trying hard that those requests are not too disruptive. Or am I still missing your point?

Quoted 4 lines

2. The second point is, even if we got the desired chunk quickly, we end up wasting large contiguous chunks, which might be needed for CMA or some h/w driver (DMA for inst.), when they can't use non-contiguous chunks.

On the other hand vmalloc is not free either.

Quoted 2 lines

BTW, in the kernel there are few examples like alloc_fdmem() for inst., which use that "costly order" idea of the fallback.

I am not familiar with this code much so it is hard for me to comment. Anyway I am not entirely sure the code is still valid. We do not do excessive reclaim nor compaction for costly orders. THey are mostly an optimistic try without __GFP_REPEAT these days. So the assumption which it was based on back in 2011 might be no longer true.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#12 lore raw
Quoted 53 lines · Michal Hocko
Quoted 45 lines · Anatoly Stepanov
Quoted 36 lines · Michal Hocko
Quoted 21 lines · Anatoly Stepanov

Alex, Vlasimil, Michal, thanks for your responses!

Quoted 9 lines · Michal Hocko

Thanks for CCing me Vlastimil

Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

This is definitely not nice and worth fixing! I am just wondering whether this is something you have encountered in the real life. Having thousands of memcgs sounds quite crazy^Wscary to me. I am not at all sure we are prepared for that and some controllers would have real issues with it AFAIR.

Quoted 3 lines

In our company we use custom-made lightweight container technology, the thing is we can have up to several thousands of them on a server.
So those high-order allocations were observed on a real production workload.

OK, this is interesting. Definitely worth mentioning in the changelog!

[...]

Quoted 7 lines
Quoted 6 lines
	/*
	 * Do not invoke OOM killer for larger requests as we can fall
	 * back to the vmalloc
	 */
	if (size > PAGE_SIZE)
		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 7 lines
Quoted 3 lines

I think we should check against PAGE_ALLOC_COSTLY_ORDER anyway, as there's no big need to allocate large contiguous chunks here, at the same time someone in the kernel might really need them.

PAGE_ALLOC_COSTLY_ORDER is and should remain the page allocator internal implementation detail and shouldn't spread out much outside. GFP_NORETRY will already make sure we do not push hard here.

Quoted 6 lines

May be i didn't put my thoughts well, so let's discuss in more detail:

1. Yes, we don't try that hard to allocate high-order blocks with __GFP_NORETRY, but we still can do compaction and direct reclaim, which can be heavy for large chunk. In the worst case we can even fail to find the chunk, after all reclaim/compaction steps were made.

Quoted 7 lines

Yes this is correct. But I am not sure what you are trying to tell by that. Highorder requests are a bit of a problem. That's why __GFP_NORETRY is implicit here. It also guarantees that we won't hit the OOM killer because we do have a reasonable fallback. I do not see a point to play with COSTLY_ORDER though. The page allocator knows how to handle those and we are trying hard that those requests are not too disruptive. Or am I still missing your point?

My point is, while we're trying to get a pretty big contig. chunk (let's say of COSTLY_SIZE), the reclaim can induce a lot of disk I/O which can be crucial
for overall system performance, at the same time we don't need that contig. chunk.

So, for COSTLY_SIZE chunks, vmalloc should perform better, as it's obviosly more likely to find order-0 blocks w/o reclaim.

The bottom line is for COSTLY_SIZE chunks it's more likely to end up with the reclaim (=>disk I/O). Which means for those chunks it's better to allocate them via vmalloc():

(Pseudo-code)

if (size < COSTLY) {
	ret = kmalloc(size);
	if (ret)
		return ret;
}

return vmalloc(size);

Right now i cannot see any significant overhead inside vmalloc() that can be an issue in this case. Please tell if you know such a thing, it would be really interesting.

Quoted 7 lines
Quoted 5 lines

2. The second point is, even if we got the desired chunk quickly, we end up wasting large contiguous chunks, which might be needed for CMA or some h/w driver (DMA for inst.), when they can't use non-contiguous chunks.

On the other hand vmalloc is not free either.

Of course, but as i explained my thoughts above, i think kmalloc() seems to be worse for large contig. chunks.

Quoted 9 lines
Quoted 3 lines

BTW, in the kernel there are few examples like alloc_fdmem() for inst., which use that "costly order" idea of the fallback.

I am not familiar with this code much so it is hard for me to comment. Anyway I am not entirely sure the code is still valid. We do not do excessive reclaim nor compaction for costly orders. THey are mostly an optimistic try without __GFP_REPEAT these days. So the assumption which it was based on back in 2011 might be no longer true.

Just to tell you one more case i oberved in real life scenario.
Once we encountered a problem on production server when a tons of forking from different apps caused a lot of direct reclaim, and we fixed it by even more limiting usage of kmalloc(). PAGE_ALLOC_COSTLY_ORDER size was too big for us, after that production server started feeling better. So, this limitation of kmalloc really makes much sense sometimes, and we need to take this into account.

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw
Quoted 62 lines · Anatoly Stepanov
Quoted 53 lines · Michal Hocko
Quoted 45 lines · Anatoly Stepanov
Quoted 36 lines · Michal Hocko
Quoted 21 lines · Anatoly Stepanov

Alex, Vlasimil, Michal, thanks for your responses!

Quoted 9 lines · Michal Hocko

Thanks for CCing me Vlastimil

Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Anatoly Stepanov

As memcg array size can be up to:
sizeof(struct memcg_cache_array) + kmemcg_id * sizeof(void *);

where kmemcg_id can be up to MEMCG_CACHES_MAX_SIZE.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

When a memcg instance count is large enough it can lead to high order allocations up to order 7.

This is definitely not nice and worth fixing! I am just wondering whether this is something you have encountered in the real life. Having thousands of memcgs sounds quite crazy^Wscary to me. I am not at all sure we are prepared for that and some controllers would have real issues with it AFAIR.

Quoted 3 lines

In our company we use custom-made lightweight container technology, the thing is we can have up to several thousands of them on a server.
So those high-order allocations were observed on a real production workload.

OK, this is interesting. Definitely worth mentioning in the changelog!

[...]

Quoted 7 lines
Quoted 6 lines
	/*
	 * Do not invoke OOM killer for larger requests as we can fall
	 * back to the vmalloc
	 */
	if (size > PAGE_SIZE)
		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
Quoted 7 lines
Quoted 3 lines

I think we should check against PAGE_ALLOC_COSTLY_ORDER anyway, as there's no big need to allocate large contiguous chunks here, at the same time someone in the kernel might really need them.

PAGE_ALLOC_COSTLY_ORDER is and should remain the page allocator internal implementation detail and shouldn't spread out much outside. GFP_NORETRY will already make sure we do not push hard here.

Quoted 6 lines

May be i didn't put my thoughts well, so let's discuss in more detail:

1. Yes, we don't try that hard to allocate high-order blocks with __GFP_NORETRY, but we still can do compaction and direct reclaim, which can be heavy for large chunk. In the worst case we can even fail to find the chunk, after all reclaim/compaction steps were made.

Quoted 7 lines

Yes this is correct. But I am not sure what you are trying to tell by that. Highorder requests are a bit of a problem. That's why __GFP_NORETRY is implicit here. It also guarantees that we won't hit the OOM killer because we do have a reasonable fallback. I do not see a point to play with COSTLY_ORDER though. The page allocator knows how to handle those and we are trying hard that those requests are not too disruptive. Or am I still missing your point?

Quoted 2 lines

My point is, while we're trying to get a pretty big contig. chunk (let's say of COSTLY_SIZE), the reclaim can induce a lot of disk I/O

Not really, as I've tried to explain above. The page allocator really doesn't try hard for costly orders and bail out early after the first round of reclaim compaction.

Quoted 5 lines

which can be crucial for overall system performance, at the same time we don't need that contig. chunk.

So, for COSTLY_SIZE chunks, vmalloc should perform better, as it's obviosly more likely to find order-0 blocks w/o reclaim.

Again, vmalloc is not free either and a problem especially on 32b arches.

Anyway, I think we are going in circles here and repeating the same arguments. Let me post what I think is the right implementation of kvmalloc and you can build on top of that.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>