Skip to contentSkip to conversation

memcg: defer page_cgroup initialization

PATCH In review Glauber Costa 3 participants 5 messages Apr 9, 2013

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

PATCH 1/1 memcg: defer page_cgroup initialization

We have now reached the point in which there is no real need to allocate page_cgroup upon system boot. We can defer it to the first memcg initialization, and if it fails, we treat it like any other memcg memory failures (like for instance, if the mem_cgroup structure itself failed). In the future, we may want to defer this to the first non-root cgroup initialization, but we are not there yet. With that, page_cgroup can be more silent in its initialization.

Unfortunately, doing that for flatmem models would lead to significant vmalloc-area waste. Since big-memory 32-bit machines are quite common, this would be reality for most of them. This means that we will leave FLATMEM alone, and fix only the SPARSEMEM case. We modify the message slightly so that in future reports we know precisely if this message is from a flatmem kernel or a older kernel initializing page_cgroup early.

Cc Michal Hocko <[email protected]>
Cc Kamezawa Hiroyuki <[email protected]>
Cc Johannes Weiner <[email protected]>
---
 include/linux/page_cgroup.h | 13 +++++++------
 init/main.c                 |  1 -
 mm/memcontrol.c             |  2 ++
 mm/page_cgroup.c            | 19 ++++++++-----------
 4 files changed, 17 insertions(+), 18 deletions(-)
include/linux/page_cgroup.h +7 -6
index 777a524..bfb43f0 100644
@@ -33,11 +33,16 @@ void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat);
 static inline void __init page_cgroup_init_flatmem(void)
 {
 }
-extern void __init page_cgroup_init(void);
+extern bool page_cgroup_init(void);
 #else
 void __init page_cgroup_init_flatmem(void);
-static inline void __init page_cgroup_init(void)
+/*
+ * If we reach here, we would have already initialized flatmem mappings.
+ * So just always succeed
+ */
+static inline bool page_cgroup_init(void)
 {
+	return 0;
 }
 #endif
 
@@ -94,10 +99,6 @@ static inline struct page_cgroup *lookup_page_cgroup(struct page *page)
 	return NULL;
 }
 
-static inline void page_cgroup_init(void)
-{
-}
-
 static inline void __init page_cgroup_init_flatmem(void)
 {
 }
init/main.c +0 -1
index cee4b5c..49aa019 100644
@@ -592,7 +592,6 @@ asmlinkage void __init start_kernel(void)
 		initrd_start = 0;
 	}
 #endif
-	page_cgroup_init();
 	debug_objects_mem_init();
 	kmemleak_init();
 	setup_per_cpu_pageset();
mm/memcontrol.c +2 -0
index f608546..59a5b1f 100644
@@ -6357,6 +6357,8 @@ mem_cgroup_css_alloc(struct cgroup *cont)
 		res_counter_init(&memcg->res, NULL);
 		res_counter_init(&memcg->memsw, NULL);
 		res_counter_init(&memcg->kmem, NULL);
+		if (page_cgroup_init())
+			goto free_out;
 	}
 
 	memcg->last_scanned_node = MAX_NUMNODES;
mm/page_cgroup.c +8 -11
index 6d757e3..679de38 100644
@@ -78,7 +78,8 @@ void __init page_cgroup_init_flatmem(void)
 	}
 	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
 	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
-	" don't want memory cgroups\n");
+	" don't want memory cgroups. Alternatively, use SPARSEMEM mappings"
+	" to defer initialization until actual use.");
 	return;
 fail:
 	printk(KERN_CRIT "allocation of page_cgroup failed.\n");
@@ -266,13 +267,13 @@ static int __meminit page_cgroup_callback(struct notifier_block *self,
 
 #endif
 
-void __init page_cgroup_init(void)
+bool page_cgroup_init(void)
 {
 	unsigned long pfn;
 	int nid;
 
 	if (mem_cgroup_disabled())
-		return;
+		return 0;
 
 	for_each_node_state(nid, N_MEMORY) {
 		unsigned long start_pfn, end_pfn;
@@ -299,17 +300,13 @@ void __init page_cgroup_init(void)
 			if (pfn_to_nid(pfn) != nid)
 				continue;
 			if (init_section_page_cgroup(pfn, nid))
-				goto oom;
+				return 1;
 		}
 	}
+#ifdef CONFIG_MEMORY_HOTPLUG
 	hotplug_memory_notifier(page_cgroup_callback, 0);
-	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
-	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
-			 "don't want memory cgroups\n");
-	return;
-oom:
-	printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
-	panic("Out of memory");
+#endif
+	return 0;
 }
 
 void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat)
Signature · 1 line
1.8.1.4
maintainer
#1 lore raw
Quoted 36 lines · Glauber Costa

We have now reached the point in which there is no real need to allocate page_cgroup upon system boot. We can defer it to the first memcg initialization, and if it fails, we treat it like any other memcg memory failures (like for instance, if the mem_cgroup structure itself failed). In the future, we may want to defer this to the first non-root cgroup initialization, but we are not there yet. With that, page_cgroup can be more silent in its initialization.

Unfortunately, doing that for flatmem models would lead to significant vmalloc-area waste. Since big-memory 32-bit machines are quite common, this would be reality for most of them. This means that we will leave FLATMEM alone, and fix only the SPARSEMEM case. We modify the message slightly so that in future reports we know precisely if this message is from a flatmem kernel or a older kernel initializing page_cgroup early.

Cc Michal Hocko <[email protected]>
Cc Kamezawa Hiroyuki <[email protected]>
Cc Johannes Weiner <[email protected]>
---
 include/linux/page_cgroup.h | 13 +++++++------
 init/main.c                 |  1 -
 mm/memcontrol.c             |  2 ++
 mm/page_cgroup.c            | 19 ++++++++-----------
 4 files changed, 17 insertions(+), 18 deletions(-)
include/linux/page_cgroup.h +1 -1
index 777a524..bfb43f0 100644
@@ -33,11 +33,16 @@ void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat);
 static inline void __init page_cgroup_init_flatmem(void)
 {
 }
-extern void __init page_cgroup_init(void);
+extern bool page_cgroup_init(void);

include/linux/page_cgroup.h void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat); · line 45

Quoted 10 lines
include/linux/page_cgroup.h +6 -1
 #else
 void __init page_cgroup_init_flatmem(void);
-static inline void __init page_cgroup_init(void)
+/*
+ * If we reach here, we would have already initialized flatmem mappings.
+ * So just always succeed
+ */
+static inline bool page_cgroup_init(void)
 {
+	return 0;

Could you please make it either int (*)(void) OR return true for success? :-)

include/linux/page_cgroup.h void __init page_cgroup_init_flatmem(void) · line 82

Quoted 7 lines
include/linux/page_cgroup.h +2 -1
@@ -78,7 +78,8 @@ void __init page_cgroup_init_flatmem(void)
 	}
 	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
 	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
-	" don't want memory cgroups\n");
+	" don't want memory cgroups. Alternatively, use SPARSEMEM mappings"
+	" to defer initialization until actual use.");

Isn't that promising a bit much as long as "actual use" means "until we create the root_mem_cgroup during boot time"?

Quoted 9 lines
include/linux/page_cgroup.h +2 -1
@@ -299,17 +300,13 @@ void __init page_cgroup_init(void)
 			if (pfn_to_nid(pfn) != nid)
 				continue;
 			if (init_section_page_cgroup(pfn, nid))
-				goto oom;
+				return 1;
 		}
 	}
+#ifdef CONFIG_MEMORY_HOTPLUG

include/linux/page_cgroup.h void __init page_cgroup_init(void) · line 309

Quoted 10 lines
include/linux/page_cgroup.h +2 -7
 	hotplug_memory_notifier(page_cgroup_callback, 0);
-	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
-	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
-			 "don't want memory cgroups\n");
-	return;
-oom:
-	printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
-	panic("Out of memory");
+#endif
+	return 0;

Ok, so this message will be replaced with BUG() in cgroup.c, right?

#2 lore raw
Quoted 42 lines · Johannes Weiner
Quoted 41 lines · Glauber Costa

We have now reached the point in which there is no real need to allocate page_cgroup upon system boot. We can defer it to the first memcg initialization, and if it fails, we treat it like any other memcg memory failures (like for instance, if the mem_cgroup structure itself failed). In the future, we may want to defer this to the first non-root cgroup initialization, but we are not there yet. With that, page_cgroup can be more silent in its initialization.

Unfortunately, doing that for flatmem models would lead to significant vmalloc-area waste. Since big-memory 32-bit machines are quite common, this would be reality for most of them. This means that we will leave FLATMEM alone, and fix only the SPARSEMEM case. We modify the message slightly so that in future reports we know precisely if this message is from a flatmem kernel or a older kernel initializing page_cgroup early.

Cc Michal Hocko <[email protected]>
Cc Kamezawa Hiroyuki <[email protected]>
Cc Johannes Weiner <[email protected]>
---
 include/linux/page_cgroup.h | 13 +++++++------
 init/main.c                 |  1 -
 mm/memcontrol.c             |  2 ++
 mm/page_cgroup.c            | 19 ++++++++-----------
 4 files changed, 17 insertions(+), 18 deletions(-)
include/linux/page_cgroup.h +3 -2
index 777a524..bfb43f0 100644
@@ -33,11 +33,16 @@ void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat);
 static inline void __init page_cgroup_init_flatmem(void)
 {
 }
-extern void __init page_cgroup_init(void);
+extern bool page_cgroup_init(void);
 #else
 void __init page_cgroup_init_flatmem(void);
-static inline void __init page_cgroup_init(void)
+/*
+ * If we reach here, we would have already initialized flatmem mappings.
Quoted 8 lines

include/linux/page_cgroup.h void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat); · line 45

Quoted 5 lines
include/linux/page_cgroup.h +4 -0
+ * So just always succeed
+ */
+static inline bool page_cgroup_init(void)
 {
+	return 0;

Could you please make it either int (*)(void) OR return true for success? :-)

I can return true, of course.

Quoted 12 lines

include/linux/page_cgroup.h void __init page_cgroup_init_flatmem(void) · line 82

Quoted 8 lines
include/linux/page_cgroup.h +2 -1
@@ -78,7 +78,8 @@ void __init page_cgroup_init_flatmem(void)
 	}
 	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
 	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
-	" don't want memory cgroups\n");
+	" don't want memory cgroups. Alternatively, use SPARSEMEM mappings"
+	" to defer initialization until actual use.");

Isn't that promising a bit much as long as "actual use" means "until we create the root_mem_cgroup during boot time"?

If you look at this patch alone, then yes, maybe. (It is still correct, though). Mostly, I wanted the message to change somehow, so if we get reports about it, we can easily differentiate a flatmem scenario from an older kernel, in which both messages are equal. And also inform the user that he could be better of if using SPARSEMEM.

Quoted 13 lines
Quoted 13 lines
include/linux/page_cgroup.h +2 -4
@@ -299,17 +300,13 @@ void __init page_cgroup_init(void)
 			if (pfn_to_nid(pfn) != nid)
 				continue;
 			if (init_section_page_cgroup(pfn, nid))
-				goto oom;
+				return 1;
 		}
 	}
+#ifdef CONFIG_MEMORY_HOTPLUG
 	hotplug_memory_notifier(page_cgroup_callback, 0);
-	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
-	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
-			 "don't want memory cgroups\n");
Quoted 8 lines

include/linux/page_cgroup.h void __init page_cgroup_init(void) · line 309

Quoted 6 lines
include/linux/page_cgroup.h +2 -4
-	return;
-oom:
-	printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
-	panic("Out of memory");
+#endif
+	return 0;

Ok, so this message will be replaced with BUG() in cgroup.c, right?

For the root cgroup, yes. When page_cgroup_init is moved to the first-non-root, then it is just a normal ENOMEM situation. More importantly, because it becomes just memory allocation, and not a special boot-time memory allocation, then it just follow the normal error paths whatever they are.

--
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
In reply to Johannes Weiner
Quoted 2 lines · Johannes Weiner

Could you please make it either int (*)(void) OR return true for success? :-)

I can actually do better: I can return an error code and then do the cleanup on error that I forgot to backport from the bypass patchset...

Thanks for drawing my attention to it.

--
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
In reply to Glauber Costa
Quoted 65 lines · Glauber Costa

We have now reached the point in which there is no real need to allocate page_cgroup upon system boot. We can defer it to the first memcg initialization, and if it fails, we treat it like any other memcg memory failures (like for instance, if the mem_cgroup structure itself failed). In the future, we may want to defer this to the first non-root cgroup initialization, but we are not there yet. With that, page_cgroup can be more silent in its initialization.

Unfortunately, doing that for flatmem models would lead to significant vmalloc-area waste. Since big-memory 32-bit machines are quite common, this would be reality for most of them. This means that we will leave FLATMEM alone, and fix only the SPARSEMEM case. We modify the message slightly so that in future reports we know precisely if this message is from a flatmem kernel or a older kernel initializing page_cgroup early.

Cc Michal Hocko <[email protected]>
Cc Kamezawa Hiroyuki <[email protected]>
Cc Johannes Weiner <[email protected]>
---
  include/linux/page_cgroup.h | 13 +++++++------
  init/main.c                 |  1 -
  mm/memcontrol.c             |  2 ++
  mm/page_cgroup.c            | 19 ++++++++-----------
  4 files changed, 17 insertions(+), 18 deletions(-)
include/linux/page_cgroup.h +7 -6
index 777a524..bfb43f0 100644
@@ -33,11 +33,16 @@ void __meminit pgdat_page_cgroup_init(struct pglist_data *pgdat);
  static inline void __init page_cgroup_init_flatmem(void)
  {
  }
-extern void __init page_cgroup_init(void);
+extern bool page_cgroup_init(void);
  #else
  void __init page_cgroup_init_flatmem(void);
-static inline void __init page_cgroup_init(void)
+/*
+ * If we reach here, we would have already initialized flatmem mappings.
+ * So just always succeed
+ */
+static inline bool page_cgroup_init(void)
  {
+	return 0;
  }
  #endif
 
@@ -94,10 +99,6 @@ static inline struct page_cgroup *lookup_page_cgroup(struct page *page)
  	return NULL;
  }
 
-static inline void page_cgroup_init(void)
-{
-}
-
  static inline void __init page_cgroup_init_flatmem(void)
  {
  }
init/main.c +0 -0
index cee4b5c..49aa019 100644

init/main.c asmlinkage void __init start_kernel(void) · line 597

Quoted 7 lines
init/main.c +0 -1
@@ -592,7 +592,6 @@ asmlinkage void __init start_kernel(void)
  		initrd_start = 0;
  	}
  #endif
-	page_cgroup_init();
  	debug_objects_mem_init();
  	kmemleak_init();
  	setup_per_cpu_pageset();

This is good.

Quoted 18 lines
mm/memcontrol.c +2 -0
index f608546..59a5b1f 100644
@@ -6357,6 +6357,8 @@ mem_cgroup_css_alloc(struct cgroup *cont)
  		res_counter_init(&memcg->res, NULL);
  		res_counter_init(&memcg->memsw, NULL);
  		res_counter_init(&memcg->kmem, NULL);
+		if (page_cgroup_init())
+			goto free_out;
  	}
 
  	memcg->last_scanned_node = MAX_NUMNODES;
mm/page_cgroup.c +0 -0
index 6d757e3..679de38 100644

mm/page_cgroup.c void __init page_cgroup_init_flatmem(void) · line 82

Quoted 6 lines
mm/page_cgroup.c +2 -1
@@ -78,7 +78,8 @@ void __init page_cgroup_init_flatmem(void)
  	}
  	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
  	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you"
-	" don't want memory cgroups\n");
+	" don't want memory cgroups. Alternatively, use SPARSEMEM mappings"
+	" to defer initialization until actual use.");

I'm sorry that I think you don't defer initialization until actual use...yet. page_cgroup() will allocated at initialization even if memory cgroup is not mounted at all. Right ?

Quoted 28 lines
mm/page_cgroup.c +4 -3
  	return;
  fail:
  	printk(KERN_CRIT "allocation of page_cgroup failed.\n");
@@ -266,13 +267,13 @@ static int __meminit page_cgroup_callback(struct notifier_block *self,
 
  #endif
 
-void __init page_cgroup_init(void)
+bool page_cgroup_init(void)
  {
  	unsigned long pfn;
  	int nid;
 
  	if (mem_cgroup_disabled())
-		return;
+		return 0;
 
  	for_each_node_state(nid, N_MEMORY) {
  		unsigned long start_pfn, end_pfn;
@@ -299,17 +300,13 @@ void __init page_cgroup_init(void)
  			if (pfn_to_nid(pfn) != nid)
  				continue;
  			if (init_section_page_cgroup(pfn, nid))
-				goto oom;
+				return 1;
  		}
  	}
+#ifdef CONFIG_MEMORY_HOTPLUG

mm/page_cgroup.c void __init page_cgroup_init(void) · line 309

Quoted 10 lines
mm/page_cgroup.c +2 -7
  	hotplug_memory_notifier(page_cgroup_callback, 0);
-	printk(KERN_INFO "allocated %ld bytes of page_cgroup\n", total_usage);
-	printk(KERN_INFO "please try 'cgroup_disable=memory' option if you "
-			 "don't want memory cgroups\n");
-	return;
-oom:
-	printk(KERN_CRIT "try 'cgroup_disable=memory' boot option\n");
-	panic("Out of memory");
+#endif
+	return 0;

IIUC, hotplug_memory_notifier() is NOP if CONFIG_MEMORY_HOTPLUG is set. So, you don't need to add #ifdef

Thanks,
-Kame