linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog
@ 2014-10-21 18:15 Sasha Levin
  2014-10-22  5:42 ` Andrey Ryabinin
  0 siblings, 1 reply; 7+ messages in thread
From: Sasha Levin @ 2014-10-21 18:15 UTC (permalink / raw)
  To: akpm
  Cc: n-horiguchi, aarcange, mgorman, linux-kernel, linux-mm,
	a.ryabinin, Sasha Levin

hstate_sizelog() would shift left an int rather than long, triggering
undefined behaviour and passing an incorrect value when the requested
page size was more than 4GB, thus breaking >4GB pages.

Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
---
 include/linux/hugetlb.h |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 65e12a2..57e0dfd 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -312,7 +312,8 @@ static inline struct hstate *hstate_sizelog(int page_size_log)
 {
 	if (!page_size_log)
 		return &default_hstate;
-	return size_to_hstate(1 << page_size_log);
+
+	return size_to_hstate(1UL << page_size_log);
 }
 
 static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
-- 
1.7.10.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog
  2014-10-21 18:15 [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog Sasha Levin
@ 2014-10-22  5:42 ` Andrey Ryabinin
  2014-10-22 18:44   ` Andrew Morton
  0 siblings, 1 reply; 7+ messages in thread
From: Andrey Ryabinin @ 2014-10-22  5:42 UTC (permalink / raw)
  To: Sasha Levin, akpm; +Cc: n-horiguchi, aarcange, mgorman, linux-kernel, linux-mm

On 10/21/2014 10:15 PM, Sasha Levin wrote:
> hstate_sizelog() would shift left an int rather than long, triggering
> undefined behaviour and passing an incorrect value when the requested
> page size was more than 4GB, thus breaking >4GB pages.

> 
> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
> ---
>  include/linux/hugetlb.h |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
> index 65e12a2..57e0dfd 100644
> --- a/include/linux/hugetlb.h
> +++ b/include/linux/hugetlb.h
> @@ -312,7 +312,8 @@ static inline struct hstate *hstate_sizelog(int page_size_log)
>  {
>  	if (!page_size_log)
>  		return &default_hstate;
> -	return size_to_hstate(1 << page_size_log);
> +
> +	return size_to_hstate(1UL << page_size_log);

That still could be undefined on 32-bits. Either use 1ULL or reduce SHM_HUGE_MASK on 32bits.


>  }
>  
>  static inline struct hstate *hstate_vma(struct vm_area_struct *vma)
> 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog
  2014-10-22  5:42 ` Andrey Ryabinin
@ 2014-10-22 18:44   ` Andrew Morton
  2014-10-22 18:50     ` Sasha Levin
  2014-10-22 19:04     ` Andrey Ryabinin
  0 siblings, 2 replies; 7+ messages in thread
From: Andrew Morton @ 2014-10-22 18:44 UTC (permalink / raw)
  To: Andrey Ryabinin
  Cc: Sasha Levin, n-horiguchi, aarcange, mgorman, linux-kernel, linux-mm

On Wed, 22 Oct 2014 09:42:46 +0400 Andrey Ryabinin <a.ryabinin@samsung.com> wrote:

> On 10/21/2014 10:15 PM, Sasha Levin wrote:
> > hstate_sizelog() would shift left an int rather than long, triggering
> > undefined behaviour and passing an incorrect value when the requested
> > page size was more than 4GB, thus breaking >4GB pages.
> 
> > 
> > Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
> > ---
> >  include/linux/hugetlb.h |    3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
> > index 65e12a2..57e0dfd 100644
> > --- a/include/linux/hugetlb.h
> > +++ b/include/linux/hugetlb.h
> > @@ -312,7 +312,8 @@ static inline struct hstate *hstate_sizelog(int page_size_log)
> >  {
> >  	if (!page_size_log)
> >  		return &default_hstate;
> > -	return size_to_hstate(1 << page_size_log);
> > +
> > +	return size_to_hstate(1UL << page_size_log);
> 
> That still could be undefined on 32-bits. Either use 1ULL or reduce SHM_HUGE_MASK on 32bits.
> 

But

struct hstate *size_to_hstate(unsigned long size)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog
  2014-10-22 18:44   ` Andrew Morton
@ 2014-10-22 18:50     ` Sasha Levin
  2014-10-22 20:13       ` Andrew Morton
  2014-10-22 19:04     ` Andrey Ryabinin
  1 sibling, 1 reply; 7+ messages in thread
From: Sasha Levin @ 2014-10-22 18:50 UTC (permalink / raw)
  To: Andrew Morton, Andrey Ryabinin
  Cc: n-horiguchi, aarcange, mgorman, linux-kernel, linux-mm

On 10/22/2014 02:44 PM, Andrew Morton wrote:
> On Wed, 22 Oct 2014 09:42:46 +0400 Andrey Ryabinin <a.ryabinin@samsung.com> wrote:
> 
>> > On 10/21/2014 10:15 PM, Sasha Levin wrote:
>>> > > hstate_sizelog() would shift left an int rather than long, triggering
>>> > > undefined behaviour and passing an incorrect value when the requested
>>> > > page size was more than 4GB, thus breaking >4GB pages.
>> > 
>>> > > 
>>> > > Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
>>> > > ---
>>> > >  include/linux/hugetlb.h |    3 ++-
>>> > >  1 file changed, 2 insertions(+), 1 deletion(-)
>>> > > 
>>> > > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
>>> > > index 65e12a2..57e0dfd 100644
>>> > > --- a/include/linux/hugetlb.h
>>> > > +++ b/include/linux/hugetlb.h
>>> > > @@ -312,7 +312,8 @@ static inline struct hstate *hstate_sizelog(int page_size_log)
>>> > >  {
>>> > >  	if (!page_size_log)
>>> > >  		return &default_hstate;
>>> > > -	return size_to_hstate(1 << page_size_log);
>>> > > +
>>> > > +	return size_to_hstate(1UL << page_size_log);
>> > 
>> > That still could be undefined on 32-bits. Either use 1ULL or reduce SHM_HUGE_MASK on 32bits.
>> > 
> But
> 
> struct hstate *size_to_hstate(unsigned long size)

True, but "(1 << page_size_log)" produces an integer rather than long because "1"
is an int and not long.

	#include <stdio.h>

	int main(void)
	{
	        unsigned long a, b;

	        a = 1 << 32;
	        b = 1UL << 32;

	        printf("a: %lu b: %lu\n", a, b);
	}


	$ ./a.out
	a: 0 b: 4294967296


With the patch, size_to_hstate() gets the unsigned long it expects.


Thanks,
Sasha

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog
  2014-10-22 18:44   ` Andrew Morton
  2014-10-22 18:50     ` Sasha Levin
@ 2014-10-22 19:04     ` Andrey Ryabinin
  1 sibling, 0 replies; 7+ messages in thread
From: Andrey Ryabinin @ 2014-10-22 19:04 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andrey Ryabinin, Sasha Levin, Naoya Horiguchi, aarcange,
	Mel Gorman, LKML, linux-mm

2014-10-22 22:44 GMT+04:00 Andrew Morton <akpm@linux-foundation.org>:
> On Wed, 22 Oct 2014 09:42:46 +0400 Andrey Ryabinin <a.ryabinin@samsung.com> wrote:
>
>> On 10/21/2014 10:15 PM, Sasha Levin wrote:
>> > hstate_sizelog() would shift left an int rather than long, triggering
>> > undefined behaviour and passing an incorrect value when the requested
>> > page size was more than 4GB, thus breaking >4GB pages.
>>
>> >
>> > Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
>> > ---
>> >  include/linux/hugetlb.h |    3 ++-
>> >  1 file changed, 2 insertions(+), 1 deletion(-)
>> >
>> > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
>> > index 65e12a2..57e0dfd 100644
>> > --- a/include/linux/hugetlb.h
>> > +++ b/include/linux/hugetlb.h
>> > @@ -312,7 +312,8 @@ static inline struct hstate *hstate_sizelog(int page_size_log)
>> >  {
>> >     if (!page_size_log)
>> >             return &default_hstate;
>> > -   return size_to_hstate(1 << page_size_log);
>> > +
>> > +   return size_to_hstate(1UL << page_size_log);
>>
>> That still could be undefined on 32-bits. Either use 1ULL or reduce SHM_HUGE_MASK on 32bits.
>>
>
> But
>
> struct hstate *size_to_hstate(unsigned long size)
>

What's wrong? On 32 bits, if page_size_log >= 32, then  (unsingned
long)(1ULL << page_size_log) will be truncated to 0. I guess it's ok.
size_to_hstate will just return NULL in that case.


-- 
Best regards,
Andrey Ryabinin

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog
  2014-10-22 18:50     ` Sasha Levin
@ 2014-10-22 20:13       ` Andrew Morton
  2014-10-22 20:26         ` Andrey Ryabinin
  0 siblings, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2014-10-22 20:13 UTC (permalink / raw)
  To: Sasha Levin
  Cc: Andrey Ryabinin, n-horiguchi, aarcange, mgorman, linux-kernel, linux-mm

On Wed, 22 Oct 2014 14:50:22 -0400 Sasha Levin <sasha.levin@oracle.com> wrote:

> On 10/22/2014 02:44 PM, Andrew Morton wrote:
> > On Wed, 22 Oct 2014 09:42:46 +0400 Andrey Ryabinin <a.ryabinin@samsung.com> wrote:
> > 
> >> > On 10/21/2014 10:15 PM, Sasha Levin wrote:
> >>> > > hstate_sizelog() would shift left an int rather than long, triggering
> >>> > > undefined behaviour and passing an incorrect value when the requested
> >>> > > page size was more than 4GB, thus breaking >4GB pages.
> >> > 
> >>> > > 
> >>> > > Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
> >>> > > ---
> >>> > >  include/linux/hugetlb.h |    3 ++-
> >>> > >  1 file changed, 2 insertions(+), 1 deletion(-)
> >>> > > 
> >>> > > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
> >>> > > index 65e12a2..57e0dfd 100644
> >>> > > --- a/include/linux/hugetlb.h
> >>> > > +++ b/include/linux/hugetlb.h
> >>> > > @@ -312,7 +312,8 @@ static inline struct hstate *hstate_sizelog(int page_size_log)
> >>> > >  {
> >>> > >  	if (!page_size_log)
> >>> > >  		return &default_hstate;
> >>> > > -	return size_to_hstate(1 << page_size_log);
> >>> > > +
> >>> > > +	return size_to_hstate(1UL << page_size_log);
> >> > 
> >> > That still could be undefined on 32-bits. Either use 1ULL or reduce SHM_HUGE_MASK on 32bits.
> >> > 
> > But
> > 
> > struct hstate *size_to_hstate(unsigned long size)
> 
> True, but "(1 << page_size_log)" produces an integer rather than long because "1"
> is an int and not long.

My point is that there's no point in using 1ULL because
size_to_hstate() will truncate it anyway.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog
  2014-10-22 20:13       ` Andrew Morton
@ 2014-10-22 20:26         ` Andrey Ryabinin
  0 siblings, 0 replies; 7+ messages in thread
From: Andrey Ryabinin @ 2014-10-22 20:26 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Sasha Levin, Andrey Ryabinin, Naoya Horiguchi, aarcange,
	Mel Gorman, LKML, linux-mm

2014-10-23 0:13 GMT+04:00 Andrew Morton <akpm@linux-foundation.org>:
> On Wed, 22 Oct 2014 14:50:22 -0400 Sasha Levin <sasha.levin@oracle.com> wrote:
>
>> On 10/22/2014 02:44 PM, Andrew Morton wrote:
>> > On Wed, 22 Oct 2014 09:42:46 +0400 Andrey Ryabinin <a.ryabinin@samsung.com> wrote:
>> >
>> >> > On 10/21/2014 10:15 PM, Sasha Levin wrote:
>> >>> > > hstate_sizelog() would shift left an int rather than long, triggering
>> >>> > > undefined behaviour and passing an incorrect value when the requested
>> >>> > > page size was more than 4GB, thus breaking >4GB pages.
>> >> >
>> >>> > >
>> >>> > > Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
>> >>> > > ---
>> >>> > >  include/linux/hugetlb.h |    3 ++-
>> >>> > >  1 file changed, 2 insertions(+), 1 deletion(-)
>> >>> > >
>> >>> > > diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
>> >>> > > index 65e12a2..57e0dfd 100644
>> >>> > > --- a/include/linux/hugetlb.h
>> >>> > > +++ b/include/linux/hugetlb.h
>> >>> > > @@ -312,7 +312,8 @@ static inline struct hstate *hstate_sizelog(int page_size_log)
>> >>> > >  {
>> >>> > >       if (!page_size_log)
>> >>> > >               return &default_hstate;
>> >>> > > -     return size_to_hstate(1 << page_size_log);
>> >>> > > +
>> >>> > > +     return size_to_hstate(1UL << page_size_log);
>> >> >
>> >> > That still could be undefined on 32-bits. Either use 1ULL or reduce SHM_HUGE_MASK on 32bits.
>> >> >
>> > But
>> >
>> > struct hstate *size_to_hstate(unsigned long size)
>>
>> True, but "(1 << page_size_log)" produces an integer rather than long because "1"
>> is an int and not long.
>
> My point is that there's no point in using 1ULL because
> size_to_hstate() will truncate it anyway.
>

There is a point to use 1ULL
On 32-bit with size >= 32
(1UL << size) - undefined, so size_to_hstate() will truncate it to
undefined as well. E.g. It definitely won't be zero on x86.
While (1ULL << size) - is defined and size_to_hstate() will truncate it to zero.


-- 
Best regards,
Andrey Ryabinin

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2014-10-22 20:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-21 18:15 [PATCH] mm, hugetlb: correct bit shift in hstate_sizelog Sasha Levin
2014-10-22  5:42 ` Andrey Ryabinin
2014-10-22 18:44   ` Andrew Morton
2014-10-22 18:50     ` Sasha Levin
2014-10-22 20:13       ` Andrew Morton
2014-10-22 20:26         ` Andrey Ryabinin
2014-10-22 19:04     ` Andrey Ryabinin

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