public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] kernel/fork.c: avoid division by zero
@ 2015-02-16 18:00 Heinrich Schuchardt
  2015-02-16 18:45 ` Heinrich Schuchardt
  2015-02-17  7:56 ` Vladimir Davydov
  0 siblings, 2 replies; 5+ messages in thread
From: Heinrich Schuchardt @ 2015-02-16 18:00 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kirill A. Shutemov, Oleg Nesterov, Peter Zijlstra, Ingo Molnar,
	Vladimir Davydov, Thomas Gleixner, David Rientjes, Kees Cook,
	Aaron Tomlin, linux-kernel, Heinrich Schuchardt

PAGE_SIZE is not guaranteed to be equal to or less than 8 times the
THREAD_SIZE.

E.g. architecture hexagon may have page size 1M and thread size 4096.

This would lead to a division by zero.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 kernel/fork.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/fork.c b/kernel/fork.c
index cf65139..71e2583 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -273,7 +273,8 @@ void __init fork_init(unsigned long mempages)
 	 * value: the thread structures can take up at most half
 	 * of memory.
 	 */
-	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
+	max_threads = (u64) PAGE_SIZE * (u64) mempages
+		    / (u64) THREAD_SIZE / 8UL;
 
 	/*
 	 * we need to allow at least 20 threads to boot a system
-- 
2.1.4


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

* Re: [PATCH 1/1] kernel/fork.c: avoid division by zero
  2015-02-16 18:00 [PATCH 1/1] kernel/fork.c: avoid division by zero Heinrich Schuchardt
@ 2015-02-16 18:45 ` Heinrich Schuchardt
  2015-02-16 18:49   ` Guenter Roeck
  2015-02-17  7:56 ` Vladimir Davydov
  1 sibling, 1 reply; 5+ messages in thread
From: Heinrich Schuchardt @ 2015-02-16 18:45 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Kirill A. Shutemov, Oleg Nesterov, Peter Zijlstra, Ingo Molnar,
	Vladimir Davydov, Thomas Gleixner, David Rientjes, Kees Cook,
	Aaron Tomlin, linux-kernel, Guenter Roeck

I am resending this mail because the linux-kernel list was not CCed.

On 16.02.2015 19:23, Guenter Roeck wrote:
> On Mon, Feb 16, 2015 at 07:00:44PM +0100, Heinrich Schuchardt wrote:
>> PAGE_SIZE is not guaranteed to be equal to or less than 8 times the
>> THREAD_SIZE.
>>
>> E.g. architecture hexagon may have page size 1M and thread size 4096.
>>
>> This would lead to a division by zero.
>>
>> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
>> ---
>>  kernel/fork.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/fork.c b/kernel/fork.c
>> index cf65139..71e2583 100644
>> --- a/kernel/fork.c
>> +++ b/kernel/fork.c
>> @@ -273,7 +273,8 @@ void __init fork_init(unsigned long mempages)
>>  	 * value: the thread structures can take up at most half
>>  	 * of memory.
>>  	 */
>> -	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
>> +	max_threads = (u64) PAGE_SIZE * (u64) mempages
>> +		    / (u64) THREAD_SIZE / 8UL;
>
> How about
> 	max_threads = mempages / (8 * THREAD_SIZE) * PAGE_SIZE;
>
> instead ? That would avoid the u64.

We have to consider the case mempages < 8 * THREAD_SIZE.

Let
  mempages = 1024
  THREAD_SIZE = 4096
  PAGE_SIZE = 1M

Your code gives 0.
Mine gives 8192.

Best regards

Heinrich



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

* Re: [PATCH 1/1] kernel/fork.c: avoid division by zero
  2015-02-16 18:45 ` Heinrich Schuchardt
@ 2015-02-16 18:49   ` Guenter Roeck
  0 siblings, 0 replies; 5+ messages in thread
From: Guenter Roeck @ 2015-02-16 18:49 UTC (permalink / raw)
  To: Heinrich Schuchardt, Andrew Morton
  Cc: Kirill A. Shutemov, Oleg Nesterov, Peter Zijlstra, Ingo Molnar,
	Vladimir Davydov, Thomas Gleixner, David Rientjes, Kees Cook,
	Aaron Tomlin, linux-kernel

On 02/16/2015 10:45 AM, Heinrich Schuchardt wrote:
> I am resending this mail because the linux-kernel list was not CCed.
>
Yes, sorry, that got lost. The "forward" feature on lkml.org doesn't work
as well as it used to, unfortunately, and I keep forgetting.

> On 16.02.2015 19:23, Guenter Roeck wrote:
>> On Mon, Feb 16, 2015 at 07:00:44PM +0100, Heinrich Schuchardt wrote:
>>> PAGE_SIZE is not guaranteed to be equal to or less than 8 times the
>>> THREAD_SIZE.
>>>
>>> E.g. architecture hexagon may have page size 1M and thread size 4096.
>>>
>>> This would lead to a division by zero.
>>>
>>> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
>>> ---
>>>   kernel/fork.c | 3 ++-
>>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/kernel/fork.c b/kernel/fork.c
>>> index cf65139..71e2583 100644
>>> --- a/kernel/fork.c
>>> +++ b/kernel/fork.c
>>> @@ -273,7 +273,8 @@ void __init fork_init(unsigned long mempages)
>>>   	 * value: the thread structures can take up at most half
>>>   	 * of memory.
>>>   	 */
>>> -	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
>>> +	max_threads = (u64) PAGE_SIZE * (u64) mempages
>>> +		    / (u64) THREAD_SIZE / 8UL;
>>
>> How about
>> 	max_threads = mempages / (8 * THREAD_SIZE) * PAGE_SIZE;
>>
>> instead ? That would avoid the u64.
>
> We have to consider the case mempages < 8 * THREAD_SIZE.
>
> Let
>    mempages = 1024
>    THREAD_SIZE = 4096
>    PAGE_SIZE = 1M
>
> Your code gives 0.
> Mine gives 8192.
>
Makes sense.

Thanks,
Guenter



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

* Re: [PATCH 1/1] kernel/fork.c: avoid division by zero
  2015-02-16 18:00 [PATCH 1/1] kernel/fork.c: avoid division by zero Heinrich Schuchardt
  2015-02-16 18:45 ` Heinrich Schuchardt
@ 2015-02-17  7:56 ` Vladimir Davydov
  2015-02-17  9:14   ` Heinrich Schuchardt
  1 sibling, 1 reply; 5+ messages in thread
From: Vladimir Davydov @ 2015-02-17  7:56 UTC (permalink / raw)
  To: Heinrich Schuchardt
  Cc: Andrew Morton, Kirill A. Shutemov, Oleg Nesterov, Peter Zijlstra,
	Ingo Molnar, Thomas Gleixner, David Rientjes, Kees Cook,
	Aaron Tomlin, linux-kernel

On Mon, Feb 16, 2015 at 07:00:44PM +0100, Heinrich Schuchardt wrote:
> diff --git a/kernel/fork.c b/kernel/fork.c
> index cf65139..71e2583 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -273,7 +273,8 @@ void __init fork_init(unsigned long mempages)
>  	 * value: the thread structures can take up at most half
>  	 * of memory.
>  	 */
> -	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
> +	max_threads = (u64) PAGE_SIZE * (u64) mempages
> +		    / (u64) THREAD_SIZE / 8UL;

This won't compile for a 32 bit arch, will it? Should you use div_u64?

Thanks,
Vladimir

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

* Re: [PATCH 1/1] kernel/fork.c: avoid division by zero
  2015-02-17  7:56 ` Vladimir Davydov
@ 2015-02-17  9:14   ` Heinrich Schuchardt
  0 siblings, 0 replies; 5+ messages in thread
From: Heinrich Schuchardt @ 2015-02-17  9:14 UTC (permalink / raw)
  To: Vladimir Davydov
  Cc: Andrew Morton, Kirill A. Shutemov, Oleg Nesterov, Peter Zijlstra,
	Ingo Molnar, Thomas Gleixner, David Rientjes, Kees Cook,
	Aaron Tomlin, linux-kernel

On 17.02.2015 08:56, Vladimir Davydov wrote:
> On Mon, Feb 16, 2015 at 07:00:44PM +0100, Heinrich Schuchardt wrote:
>> diff --git a/kernel/fork.c b/kernel/fork.c
>> index cf65139..71e2583 100644
>> --- a/kernel/fork.c
>> +++ b/kernel/fork.c
>> @@ -273,7 +273,8 @@ void __init fork_init(unsigned long mempages)
>>  	 * value: the thread structures can take up at most half
>>  	 * of memory.
>>  	 */
>> -	max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
>> +	max_threads = (u64) PAGE_SIZE * (u64) mempages
>> +		    / (u64) THREAD_SIZE / 8UL;
> 
> This won't compile for a 32 bit arch, will it? Should you use div_u64?
> 
> Thanks,
> Vladimir
> 
Hello Vladimir,

I compiled the kernel successfully with the patch on a 32bit arm system
and Kernel 3.19.0.

Obviously that was good luck.
http://gcc.gnu.org/onlinedocs/gccint/Integer-library-routines.html
describes that library function __udivti3 might be used on other
platforms. And using library functions is not allowable in the kernel.

Thank you for reviewing. I will update the patch.

Best regards

Heinrich

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

end of thread, other threads:[~2015-02-17  9:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-16 18:00 [PATCH 1/1] kernel/fork.c: avoid division by zero Heinrich Schuchardt
2015-02-16 18:45 ` Heinrich Schuchardt
2015-02-16 18:49   ` Guenter Roeck
2015-02-17  7:56 ` Vladimir Davydov
2015-02-17  9:14   ` Heinrich Schuchardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox