Skip to contentSkip to conversation

(memory hotplug) fix null pointer access of kmem_cache_node after memory hotplug

RFC/Patch Yasunori Goto 2 participants 5 messages Sep 18, 2007 – Sep 20, 2007

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

Hi Cristoph-san.

I found panic occuring after memory hot-add on 2.6.23-rc6-mm1 yet.

Its cause was null pointer access to kmem_cache_node of SLUB at discard_slab().
In my understanding, it should be created for all slubs after memory-less-node(or new node) gets new memory. But, current -mm doen't it. This patch fix for it.

In this patch, it is created after that new_slab is allocated from new onlined memory.
If kmem_cache_node is created at online_pages() of memory hot-add, it should be done before build_zonelist to avoid race condition. But, it means kmem_cache_node must be allocated on other old nodes due not to complete initialization.
I think this "delay creation" fix is better way than it.

I know that failure case of kmem_cache_alloc_node() must be written and the prototype of init_kmem_cache_node() here is not good. Just I would like to confirm that I don't overlook something about SLUB.

Bye.

---
 mm/slub.c |   15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
mm/slub.c +14 -1
Index: current/mm/slub.c
===================================================================
@@ -1081,6 +1081,7 @@ static void setup_object(struct kmem_cac
 		s->ctor(s, object);
 }
 
+static void init_kmem_cache_node(struct kmem_cache_node *n);
 static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
 {
 	struct page *page;
@@ -1089,6 +1090,7 @@ static struct page *new_slab(struct kmem
 	void *end;
 	void *last;
 	void *p;
+	int page_nid;
 
 	BUG_ON(flags & GFP_SLAB_BUG_MASK);
 
@@ -1097,9 +1099,20 @@ static struct page *new_slab(struct kmem
 	if (!page)
 		goto out;
 
-	n = get_node(s, page_to_nid(page));
+	page_nid = page_to_nid(page);
+	n = get_node(s, page_nid);
 	if (n)
 		atomic_long_inc(&n->nr_slabs);
+	else if (node_state(page_nid, N_HIGH_MEMORY) && s != kmalloc_caches) {
+		/*
+		 * If new memory is onlined on new(or memory less) node,
+		 * this will happen. (Second comparison is to avoid eternal
+		 * recursion.)
+		 */
+		n = kmem_cache_alloc_node(kmalloc_caches, GFP_KERNEL, page_nid);
+		init_kmem_cache_node(n);
+		s->node[page_nid] = n;
+	}
 	page->slab = s;
 	page->flags |= 1 << PG_slab;
 	if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
Signature · 8 lines
Yasunori Goto 


--
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 5 lines · Yasunori Goto

Its cause was null pointer access to kmem_cache_node of SLUB at discard_slab().
In my understanding, it should be created for all slubs after memory-less-node(or new node) gets new memory. But, current -mm doen't it. This patch fix for it.

Right. Isnt there a notifier chain that can be used to create the missing node structure?

Quoted 4 lines

If kmem_cache_node is created at online_pages() of memory hot-add, it should be done before build_zonelist to avoid race condition. But, it means kmem_cache_node must be allocated on other old nodes due not to complete initialization.

Why before build_zonelist? The regular slab bootstrap occurs after zonelist creation.

Quoted 1 line

I think this "delay creation" fix is better way than it.

Looks like this is a way to on demand node structure creation?

Quoted 3 lines

I know that failure case of kmem_cache_alloc_node() must be written and the prototype of init_kmem_cache_node() here is not good. Just I would like to confirm that I don't overlook something about SLUB.

Could be okay. I would feel better if we always had a per node structure for each available node on the node that it covers.

mm/slub.c static struct page *new_slab(struct kmem · line 1111

Quoted 6 lines
mm/slub.c +6 -0
+	else if (node_state(page_nid, N_HIGH_MEMORY) && s != kmalloc_caches) {
+		/*
+		 * If new memory is onlined on new(or memory less) node,
+		 * this will happen. (Second comparison is to avoid eternal
+		 * recursion.)
+		 */

For memoryless nodes this function will return NULL which will cause fallback. It looks like we are not going into this branch because in that case N_HIGH_MEMORY will not be set for the node.

--
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
Quoted 10 lines · Yasunori Goto
Quoted 6 lines

Its cause was null pointer access to kmem_cache_node of SLUB at discard_slab().
In my understanding, it should be created for all slubs after memory-less-node(or new node) gets new memory. But, current -mm doen't it. This patch fix for it.

Right. Isnt there a notifier chain that can be used to create the missing node structure?

Yes, there is. Though nothing uses it so far....

Quoted 7 lines
Quoted 4 lines

If kmem_cache_node is created at online_pages() of memory hot-add, it should be done before build_zonelist to avoid race condition. But, it means kmem_cache_node must be allocated on other old nodes due not to complete initialization.

Why before build_zonelist? The regular slab bootstrap occurs after zonelist creation.

build_zonelist() is called very early stage of bootstrap, But it is called final stage of hot-add.
When build_zonelist() is called at hot-add, all kernel module can use new memory of the node. So, I'm afraid like following worst case.

   build_zonelist()              
        :                     new_nodes_page = new_slab();
        :                         :
        :                         :
        :                     discard_slab(new_nodes_page)
        :                         (access kmem_cache_node)
        :
   kmem_cache_node setting,
Quoted 3 lines
Quoted 1 line

I think this "delay creation" fix is better way than it.

Looks like this is a way to on demand node structure creation?

Yes.

Quoted 7 lines
Quoted 3 lines

I know that failure case of kmem_cache_alloc_node() must be written and the prototype of init_kmem_cache_node() here is not good. Just I would like to confirm that I don't overlook something about SLUB.

Could be okay. I would feel better if we always had a per node structure for each available node on the node that it covers.

Quoted 10 lines

mm/slub.c static struct page *new_slab(struct kmem · line 1111

Quoted 6 lines
mm/slub.c +6 -0
+	else if (node_state(page_nid, N_HIGH_MEMORY) && s != kmalloc_caches) {
+		/*
+		 * If new memory is onlined on new(or memory less) node,
+		 * this will happen. (Second comparison is to avoid eternal
+		 * recursion.)
+		 */

For memoryless nodes this function will return NULL which will cause fallback. It looks like we are not going into this branch because in that case N_HIGH_MEMORY will not be set for the node.

Probably, the comment was wrong.
When a memory less node gets new memory by hot-add, N_HIGH_MEMORY is set at online_pages(). (It is included in 2.6.23-rc6-mm1). The first comparison is to find it.

Thanks.

Signature · 8 lines
Yasunori Goto 


--
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
Quoted 5 lines · Yasunori Goto

build_zonelist() is called very early stage of bootstrap, But it is called final stage of hot-add.
When build_zonelist() is called at hot-add, all kernel module can use new memory of the node. So, I'm afraid like following worst case.

Quoted 8 lines
   build_zonelist()              
        :                     new_nodes_page = new_slab();
        :                         :
        :                         :
        :                     discard_slab(new_nodes_page)
        :                         (access kmem_cache_node)
        :
   kmem_cache_node setting,

So we cannot do this without holding off other kernel accesses since it is not serialized like bootstrap. Sigh.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I think this "delay creation" fix is better way than it.

Looks like this is a way to on demand node structure creation?

Yes.

Could be useful in general if you can make that work reliably. We can just start out with a single per node structure for the boot node and then add others on demand?

--
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
Quoted 21 lines · Yasunori Goto
Quoted 6 lines

build_zonelist() is called very early stage of bootstrap, But it is called final stage of hot-add.
When build_zonelist() is called at hot-add, all kernel module can use new memory of the node. So, I'm afraid like following worst case.

Quoted 8 lines
   build_zonelist()              
        :                     new_nodes_page = new_slab();
        :                         :
        :                         :
        :                     discard_slab(new_nodes_page)
        :                         (access kmem_cache_node)
        :
   kmem_cache_node setting,

So we cannot do this without holding off other kernel accesses since it is not serialized like bootstrap. Sigh.

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

I think this "delay creation" fix is better way than it.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Looks like this is a way to on demand node structure creation?

Yes.

Could be useful in general if you can make that work reliably. We can just start out with a single per node structure for the boot node and then add others on demand?

Hmmmmm. I don't think demand node creation can be generic. Just I would like to fix the panic.
Ok, I'll make a patch which sets kmem_cache_node before build_zonelist() to fix panic for the present. And I reconsider about allocation place issue later.

Thanks for your comment.

Signature · 8 lines
Yasunori Goto 


--
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>