All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fork: fix oops after fork failure
@ 2012-08-23 13:08 Glauber Costa
  2012-08-23 14:33 ` Michal Hocko
  0 siblings, 1 reply; 6+ messages in thread
From: Glauber Costa @ 2012-08-23 13:08 UTC (permalink / raw)
  To: linux-kernel; +Cc: Andrew Morton, torvalds, Peter Zijlstra, Glauber Costa

When we want to duplicate a new process, dup_task_struct() will undergo
a series of allocations. If alloc_thread_info_node() fails, we call
free_task_struct() and return.

This seems right, but it is not. free_task_struct() will not only free
the task struct from the kmem_cache, but will also call
arch_release_task_struct(). The problem is that this function is
supposed to undo whatever arch-specific work done by
arch_dup_task_struct(), that is not yet called at this point.  The
particular problem I ran accross was that in x86, we will arrive at
fpu_free() without having ever allocated it.

This code is very ancient, and according to git, it is there since the
pre-git era. But forks don't fail that often, so that made it well
hidden.

Signed-off-by: Glauber Costa <glommer@parallels.com>
Reported-by: Frederic Weisbecker <fweisbec@redhat.com>
---
 kernel/fork.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index 152d023..b397435 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -299,7 +299,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
 
 	ti = alloc_thread_info_node(tsk, node);
 	if (!ti) {
-		free_task_struct(tsk);
+		kmem_cache_free(task_struct_cachep, tsk);
 		return NULL;
 	}
 
-- 
1.7.11.4


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] fork: fix oops after fork failure
  2012-08-23 13:08 [PATCH] fork: fix oops after fork failure Glauber Costa
@ 2012-08-23 14:33 ` Michal Hocko
  2012-08-23 14:38   ` Glauber Costa
  2012-08-23 14:38   ` Michal Hocko
  0 siblings, 2 replies; 6+ messages in thread
From: Michal Hocko @ 2012-08-23 14:33 UTC (permalink / raw)
  To: Glauber Costa; +Cc: linux-kernel, Andrew Morton, torvalds, Peter Zijlstra

On Thu 23-08-12 17:08:46, Glauber Costa wrote:
> When we want to duplicate a new process, dup_task_struct() will undergo
> a series of allocations. If alloc_thread_info_node() fails, we call
> free_task_struct() and return.
> 
> This seems right, but it is not. free_task_struct() will not only free
> the task struct from the kmem_cache, but will also call
> arch_release_task_struct(). The problem is that this function is
> supposed to undo whatever arch-specific work done by
> arch_dup_task_struct(), that is not yet called at this point.  The
> particular problem I ran accross was that in x86, we will arrive at
> fpu_free() without having ever allocated it.
> 
> This code is very ancient, and according to git, it is there since the
> pre-git era. But forks don't fail that often, so that made it well
> hidden.
> 
> Signed-off-by: Glauber Costa <glommer@parallels.com>
> Reported-by: Frederic Weisbecker <fweisbec@redhat.com>
> ---
>  kernel/fork.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/kernel/fork.c b/kernel/fork.c
> index 152d023..b397435 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -299,7 +299,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
>  
>  	ti = alloc_thread_info_node(tsk, node);
>  	if (!ti) {
> -		free_task_struct(tsk);
> +		kmem_cache_free(task_struct_cachep, tsk);

What about ia64 (or !CONFIG_ARCH_THREAD_INFO_ALLOCATOR in general) which
doesn't allocate thread_info at all?

>  		return NULL;
>  	}
>  
> -- 
> 1.7.11.4
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fork: fix oops after fork failure
  2012-08-23 14:33 ` Michal Hocko
@ 2012-08-23 14:38   ` Glauber Costa
  2012-08-23 14:38   ` Michal Hocko
  1 sibling, 0 replies; 6+ messages in thread
From: Glauber Costa @ 2012-08-23 14:38 UTC (permalink / raw)
  To: Michal Hocko; +Cc: linux-kernel, Andrew Morton, torvalds, Peter Zijlstra

On 08/23/2012 06:33 PM, Michal Hocko wrote:
> On Thu 23-08-12 17:08:46, Glauber Costa wrote:
>> When we want to duplicate a new process, dup_task_struct() will undergo
>> a series of allocations. If alloc_thread_info_node() fails, we call
>> free_task_struct() and return.
>>
>> This seems right, but it is not. free_task_struct() will not only free
>> the task struct from the kmem_cache, but will also call
>> arch_release_task_struct(). The problem is that this function is
>> supposed to undo whatever arch-specific work done by
>> arch_dup_task_struct(), that is not yet called at this point.  The
>> particular problem I ran accross was that in x86, we will arrive at
>> fpu_free() without having ever allocated it.
>>
>> This code is very ancient, and according to git, it is there since the
>> pre-git era. But forks don't fail that often, so that made it well
>> hidden.
>>
>> Signed-off-by: Glauber Costa <glommer@parallels.com>
>> Reported-by: Frederic Weisbecker <fweisbec@redhat.com>
>> ---
>>  kernel/fork.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/kernel/fork.c b/kernel/fork.c
>> index 152d023..b397435 100644
>> --- a/kernel/fork.c
>> +++ b/kernel/fork.c
>> @@ -299,7 +299,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
>>  
>>  	ti = alloc_thread_info_node(tsk, node);
>>  	if (!ti) {
>> -		free_task_struct(tsk);
>> +		kmem_cache_free(task_struct_cachep, tsk);
> 
> What about ia64 (or !CONFIG_ARCH_THREAD_INFO_ALLOCATOR in general) which
> doesn't allocate thread_info at all?

They would return something meaningful here anyway, otherwise this would
already error out and exit.

But you actually have a point. Not all architectures (all but ia64) will
allocathe the task struct from the slab... sigh...

Sorry, I will come up with something for this.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fork: fix oops after fork failure
  2012-08-23 14:33 ` Michal Hocko
  2012-08-23 14:38   ` Glauber Costa
@ 2012-08-23 14:38   ` Michal Hocko
  2012-08-23 14:45     ` Michal Hocko
  1 sibling, 1 reply; 6+ messages in thread
From: Michal Hocko @ 2012-08-23 14:38 UTC (permalink / raw)
  To: Glauber Costa; +Cc: linux-kernel, Andrew Morton, torvalds, Peter Zijlstra

On Thu 23-08-12 16:33:12, Michal Hocko wrote:
> On Thu 23-08-12 17:08:46, Glauber Costa wrote:
> > When we want to duplicate a new process, dup_task_struct() will undergo
> > a series of allocations. If alloc_thread_info_node() fails, we call
> > free_task_struct() and return.
> > 
> > This seems right, but it is not. free_task_struct() will not only free
> > the task struct from the kmem_cache, but will also call
> > arch_release_task_struct(). The problem is that this function is
> > supposed to undo whatever arch-specific work done by
> > arch_dup_task_struct(), that is not yet called at this point.  The
> > particular problem I ran accross was that in x86, we will arrive at
> > fpu_free() without having ever allocated it.
> > 
> > This code is very ancient, and according to git, it is there since the
> > pre-git era. But forks don't fail that often, so that made it well
> > hidden.
> > 
> > Signed-off-by: Glauber Costa <glommer@parallels.com>
> > Reported-by: Frederic Weisbecker <fweisbec@redhat.com>
> > ---
> >  kernel/fork.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/kernel/fork.c b/kernel/fork.c
> > index 152d023..b397435 100644
> > --- a/kernel/fork.c
> > +++ b/kernel/fork.c
> > @@ -299,7 +299,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
> >  
> >  	ti = alloc_thread_info_node(tsk, node);
> >  	if (!ti) {
> > -		free_task_struct(tsk);
> > +		kmem_cache_free(task_struct_cachep, tsk);
> 
> What about ia64 (or !CONFIG_ARCH_THREAD_INFO_ALLOCATOR in general) which
> doesn't allocate thread_info at all?

Hit send button too fast. Should read (or CONFIG_ARCH_THREAD_INFO_ALLOCATOR)
ia64 will not fail obviously and there is no other arch which would
define own thread infor allocators but there might be some in future.
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fork: fix oops after fork failure
  2012-08-23 14:45     ` Michal Hocko
@ 2012-08-23 14:43       ` Glauber Costa
  0 siblings, 0 replies; 6+ messages in thread
From: Glauber Costa @ 2012-08-23 14:43 UTC (permalink / raw)
  To: Michal Hocko; +Cc: linux-kernel, Andrew Morton, torvalds, Peter Zijlstra

On 08/23/2012 06:45 PM, Michal Hocko wrote:
> On Thu 23-08-12 16:38:50, Michal Hocko wrote:
>> On Thu 23-08-12 16:33:12, Michal Hocko wrote:
>>> On Thu 23-08-12 17:08:46, Glauber Costa wrote:
>>>> When we want to duplicate a new process, dup_task_struct() will undergo
>>>> a series of allocations. If alloc_thread_info_node() fails, we call
>>>> free_task_struct() and return.
>>>>
>>>> This seems right, but it is not. free_task_struct() will not only free
>>>> the task struct from the kmem_cache, but will also call
>>>> arch_release_task_struct(). The problem is that this function is
>>>> supposed to undo whatever arch-specific work done by
>>>> arch_dup_task_struct(), that is not yet called at this point.  The
>>>> particular problem I ran accross was that in x86, we will arrive at
>>>> fpu_free() without having ever allocated it.
>>>>
>>>> This code is very ancient, and according to git, it is there since the
>>>> pre-git era. But forks don't fail that often, so that made it well
>>>> hidden.
>>>>
>>>> Signed-off-by: Glauber Costa <glommer@parallels.com>
>>>> Reported-by: Frederic Weisbecker <fweisbec@redhat.com>
>>>> ---
>>>>  kernel/fork.c | 2 +-
>>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/kernel/fork.c b/kernel/fork.c
>>>> index 152d023..b397435 100644
>>>> --- a/kernel/fork.c
>>>> +++ b/kernel/fork.c
>>>> @@ -299,7 +299,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
>>>>  
>>>>  	ti = alloc_thread_info_node(tsk, node);
>>>>  	if (!ti) {
>>>> -		free_task_struct(tsk);
>>>> +		kmem_cache_free(task_struct_cachep, tsk);
>>>
>>> What about ia64 (or !CONFIG_ARCH_THREAD_INFO_ALLOCATOR in general) which
>>> doesn't allocate thread_info at all?
>>
>> Hit send button too fast. Should read (or CONFIG_ARCH_THREAD_INFO_ALLOCATOR)
>> ia64 will not fail obviously and there is no other arch which would
>> define own thread infor allocators but there might be some in future.
> 
> Bahh, and I should have been looking at CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
> instead. Anyway ia64 uses page allocator directly so kmem_cache_free is
> not appropriate.
> 
Yes, you are right. Thanks for spotting this

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fork: fix oops after fork failure
  2012-08-23 14:38   ` Michal Hocko
@ 2012-08-23 14:45     ` Michal Hocko
  2012-08-23 14:43       ` Glauber Costa
  0 siblings, 1 reply; 6+ messages in thread
From: Michal Hocko @ 2012-08-23 14:45 UTC (permalink / raw)
  To: Glauber Costa; +Cc: linux-kernel, Andrew Morton, torvalds, Peter Zijlstra

On Thu 23-08-12 16:38:50, Michal Hocko wrote:
> On Thu 23-08-12 16:33:12, Michal Hocko wrote:
> > On Thu 23-08-12 17:08:46, Glauber Costa wrote:
> > > When we want to duplicate a new process, dup_task_struct() will undergo
> > > a series of allocations. If alloc_thread_info_node() fails, we call
> > > free_task_struct() and return.
> > > 
> > > This seems right, but it is not. free_task_struct() will not only free
> > > the task struct from the kmem_cache, but will also call
> > > arch_release_task_struct(). The problem is that this function is
> > > supposed to undo whatever arch-specific work done by
> > > arch_dup_task_struct(), that is not yet called at this point.  The
> > > particular problem I ran accross was that in x86, we will arrive at
> > > fpu_free() without having ever allocated it.
> > > 
> > > This code is very ancient, and according to git, it is there since the
> > > pre-git era. But forks don't fail that often, so that made it well
> > > hidden.
> > > 
> > > Signed-off-by: Glauber Costa <glommer@parallels.com>
> > > Reported-by: Frederic Weisbecker <fweisbec@redhat.com>
> > > ---
> > >  kernel/fork.c | 2 +-
> > >  1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/kernel/fork.c b/kernel/fork.c
> > > index 152d023..b397435 100644
> > > --- a/kernel/fork.c
> > > +++ b/kernel/fork.c
> > > @@ -299,7 +299,7 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
> > >  
> > >  	ti = alloc_thread_info_node(tsk, node);
> > >  	if (!ti) {
> > > -		free_task_struct(tsk);
> > > +		kmem_cache_free(task_struct_cachep, tsk);
> > 
> > What about ia64 (or !CONFIG_ARCH_THREAD_INFO_ALLOCATOR in general) which
> > doesn't allocate thread_info at all?
> 
> Hit send button too fast. Should read (or CONFIG_ARCH_THREAD_INFO_ALLOCATOR)
> ia64 will not fail obviously and there is no other arch which would
> define own thread infor allocators but there might be some in future.

Bahh, and I should have been looking at CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
instead. Anyway ia64 uses page allocator directly so kmem_cache_free is
not appropriate.
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-08-23 14:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-23 13:08 [PATCH] fork: fix oops after fork failure Glauber Costa
2012-08-23 14:33 ` Michal Hocko
2012-08-23 14:38   ` Glauber Costa
2012-08-23 14:38   ` Michal Hocko
2012-08-23 14:45     ` Michal Hocko
2012-08-23 14:43       ` Glauber Costa

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.