Subject: [PATCH 2/8] kernel/fork: Duplicate task_struct before stack allocation.

alloc_thread_stack_node() already populates the task_struct::stack
member except on IA64. The stack pointer is saved and populated again
because IA64 needs it and arch_dup_task_struct() overwrites it.

Allocate thread's stack after task_struct has been duplicated as a
preparation.

Signed-off-by: Sebastian Andrzej Siewior <[email protected]>
---
kernel/fork.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index f63c0af6002da..c47dcba5d66d2 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -888,6 +888,10 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
if (!tsk)
return NULL;

+ err = arch_dup_task_struct(tsk, orig);
+ if (err)
+ goto free_tsk;
+
stack = alloc_thread_stack_node(tsk, node);
if (!stack)
goto free_tsk;
@@ -897,8 +901,6 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)

stack_vm_area = task_stack_vm_area(tsk);

- err = arch_dup_task_struct(tsk, orig);
-
/*
* arch_dup_task_struct() clobbers the stack-related fields. Make
* sure they're properly initialized before using any stack-related
@@ -912,9 +914,6 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
refcount_set(&tsk->stack_refcount, 1);
#endif

- if (err)
- goto free_stack;
-
err = scs_prepare(tsk, node);
if (err)
goto free_stack;
--
2.34.1


2022-02-14 10:00:41

by Andy Lutomirski

[permalink] [raw]
Subject: Re: [PATCH 2/8] kernel/fork: Duplicate task_struct before stack allocation.

On 1/25/22 07:26, Sebastian Andrzej Siewior wrote:
> alloc_thread_stack_node() already populates the task_struct::stack
> member except on IA64. The stack pointer is saved and populated again
> because IA64 needs it and arch_dup_task_struct() overwrites it.

I understand the problem, I think.

>
> Allocate thread's stack after task_struct has been duplicated as a
> preparation.
>

But I don't understand this. How does this patch relate to the problem?

Also, you appear to be missing a change to the free_stack and free_tsk
code at the end of dup_task_struct().

Subject: Re: [PATCH 2/8] kernel/fork: Duplicate task_struct before stack allocation.

On 2022-02-11 15:42:38 [-0800], Andy Lutomirski wrote:
> On 1/25/22 07:26, Sebastian Andrzej Siewior wrote:
> > alloc_thread_stack_node() already populates the task_struct::stack
> > member except on IA64. The stack pointer is saved and populated again
> > because IA64 needs it and arch_dup_task_struct() overwrites it.
>
> I understand the problem, I think.
>
> >
> > Allocate thread's stack after task_struct has been duplicated as a
> > preparation.
> >
>
> But I don't understand this. How does this patch relate to the problem?

So I duplicate the task-struct, assign the stack pointer in
alloc_thread_stack_node() with no need to update the stack pointer
later. Otherwise arch_dup_task_struct() would reset the pointer.

> Also, you appear to be missing a change to the free_stack and free_tsk code
> at the end of dup_task_struct().

It looks right. What am I missing?

Sebastian