Skip to contentSkip to conversation

fork,memcg: alloc_thread_stack_node needs to set tsk->stack

PATCH In review Andrea Arcangeli 4 participants 4 messages Jun 19, 2019

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

PATCH 1/1 fork,memcg: alloc_thread_stack_node needs to set tsk->stack

Commit 5eed6f1dff87bfb5e545935def3843edf42800f2 corrected two instances, but there was a third instance of this bug.

Without setting tsk->stack, if memcg_charge_kernel_stack fails, it'll execute free_thread_stack() on a dangling pointer.

Enterprise kernels are compiled with VMAP_STACK=y so this isn't critical, but custom VMAP_STACK=n builds should have some performance advantage, with the drawback of risking to fail fork because compaction didn't succeed. So as long as VMAP_STACK=n is a supported option it's worth fixing it upstream.

Fixes 9b6f7e163cd0 ("mm: rework memcg kernel stack accounting")
---
 kernel/fork.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/fork.c +5 -1
index d6c324b1b29e..9ee28dfe7c21 100644
@@ -248,7 +248,11 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
 	struct page *page = alloc_pages_node(node, THREADINFO_GFP,
 					     THREAD_SIZE_ORDER);
 
-	return page ? page_address(page) : NULL;
+	if (likely(page)) {
+		tsk->stack = page_address(page);
+		return tsk->stack;
+	}
+	return NULL;
 #endif
 }
 
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Andrea Arcangeli

Commit 5eed6f1dff87bfb5e545935def3843edf42800f2 corrected two instances, but there was a third instance of this bug.

Without setting tsk->stack, if memcg_charge_kernel_stack fails, it'll execute free_thread_stack() on a dangling pointer.

Quoted 8 lines

Enterprise kernels are compiled with VMAP_STACK=y so this isn't critical, but custom VMAP_STACK=n builds should have some performance advantage, with the drawback of risking to fail fork because compaction didn't succeed. So as long as VMAP_STACK=n is a supported option it's worth fixing it upstream.

Fixes 9b6f7e163cd0 ("mm: rework memcg kernel stack accounting")
Signature · 1 line
All Rights Reversed.
Acked-by on v1 1/1
#2 lore raw
In reply to Andrea Arcangeli

Thank you, Andrea!

Quoted 38 lines · Andrea Arcangeli

Commit 5eed6f1dff87bfb5e545935def3843edf42800f2 corrected two instances, but there was a third instance of this bug.

Without setting tsk->stack, if memcg_charge_kernel_stack fails, it'll execute free_thread_stack() on a dangling pointer.

Enterprise kernels are compiled with VMAP_STACK=y so this isn't critical, but custom VMAP_STACK=n builds should have some performance advantage, with the drawback of risking to fail fork because compaction didn't succeed. So as long as VMAP_STACK=n is a supported option it's worth fixing it upstream.

Fixes 9b6f7e163cd0 ("mm: rework memcg kernel stack accounting")

---
kernel/fork.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
kernel/fork.c +5 -1
index d6c324b1b29e..9ee28dfe7c21 100644
@@ -248,7 +248,11 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
   struct page *page = alloc_pages_node(node, THREADINFO_GFP,
                        THREAD_SIZE_ORDER);
 
-    return page ? page_address(page) : NULL;
+    if (likely(page)) {
+        tsk->stack = page_address(page);
+        return tsk->stack;
+    }
+    return NULL;
 #endif
 }
 
Acked-by on v1 1/1
#3 lore raw
In reply to Andrea Arcangeli
Quoted 2 lines · Andrea Arcangeli

Commit 5eed6f1dff87bfb5e545935def3843edf42800f2 corrected two instances, but there was a third instance of this bug.

Sigh. I should have noticed when reviewing the above. My bad.

Quoted 11 lines

Without setting tsk->stack, if memcg_charge_kernel_stack fails, it'll execute free_thread_stack() on a dangling pointer.

Enterprise kernels are compiled with VMAP_STACK=y so this isn't critical, but custom VMAP_STACK=n builds should have some performance advantage, with the drawback of risking to fail fork because compaction didn't succeed. So as long as VMAP_STACK=n is a supported option it's worth fixing it upstream.

Fixes 9b6f7e163cd0 ("mm: rework memcg kernel stack accounting")

I have double checked now and it seems we have covered all the cases finally.

Thanks!

Quoted 21 lines
---
 kernel/fork.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/fork.c +5 -1
index d6c324b1b29e..9ee28dfe7c21 100644
@@ -248,7 +248,11 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
 	struct page *page = alloc_pages_node(node, THREADINFO_GFP,
 					     THREAD_SIZE_ORDER);
 
-	return page ? page_address(page) : NULL;
+	if (likely(page)) {
+		tsk->stack = page_address(page);
+		return tsk->stack;
+	}
+	return NULL;
 #endif
 }
 
Signature · 2 lines
Michal Hocko
SUSE Labs