2013-07-18 16:56:50

by Jerry

[permalink] [raw]
Subject: [PATCH] mm: negative left shift count when PAGE_SHIFT > 20

When PAGE_SHIFT > 20, the result of "20 - PAGE_SHIFT" is negative. The
calculating here will generate an unexpected result. In addition, if
PAGE_SHIFT > 20, The memory size represented by numentries was already
integral multiple of 1MB.

Signed-off-by: Jerry <[email protected]>
---
mm/page_alloc.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index b100255..cd41797 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -5745,9 +5745,11 @@ void *__init alloc_large_system_hash(const char *tablename,
if (!numentries) {
/* round applicable memory size up to nearest megabyte */
numentries = nr_kernel_pages;
- numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
- numentries >>= 20 - PAGE_SHIFT;
- numentries <<= 20 - PAGE_SHIFT;
+ if (20 > PAGE_SHIFT) {
+ numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
+ numentries >>= 20 - PAGE_SHIFT;
+ numentries <<= 20 - PAGE_SHIFT;
+ }

/* limit to 1 bucket per 2^scale bytes of low memory */
if (scale > PAGE_SHIFT)
--
1.8.1.5


2013-07-18 17:21:15

by Jerry

[permalink] [raw]
Subject: Re: [PATCH] mm: negative left shift count when PAGE_SHIFT > 20

> Jerry> When PAGE_SHIFT > 20, the result of "20 - PAGE_SHIFT" is negative. The
> Jerry> calculating here will generate an unexpected result. In addition, if
> Jerry> PAGE_SHIFT > 20, The memory size represented by numentries was already
> Jerry> integral multiple of 1MB.
>
> Why this magic number of 20? Please explain it better and replace it
> was a #define that means something here.

Because 2^20 = 1MB.

The intention of previous code is "/* round applicable memory size up
to nearest megabyte */".

>
> Jerry> Signed-off-by: Jerry <[email protected]>
> Jerry> ---
> Jerry> mm/page_alloc.c | 8 +++++---
> Jerry> 1 file changed, 5 insertions(+), 3 deletions(-)
>
> Jerry> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> Jerry> index b100255..cd41797 100644
> Jerry> --- a/mm/page_alloc.c
> Jerry> +++ b/mm/page_alloc.c
> Jerry> @@ -5745,9 +5745,11 @@ void *__init alloc_large_system_hash(const char *tablename,
> Jerry> if (!numentries) {
> Jerry> /* round applicable memory size up to nearest megabyte */
> Jerry> numentries = nr_kernel_pages;
> Jerry> - numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
> Jerry> - numentries >>= 20 - PAGE_SHIFT;
> Jerry> - numentries <<= 20 - PAGE_SHIFT;
> Jerry> + if (20 > PAGE_SHIFT) {
> Jerry> + numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
> Jerry> + numentries >>= 20 - PAGE_SHIFT;
> Jerry> + numentries <<= 20 - PAGE_SHIFT;
> Jerry> + }
>
> Jerry> /* limit to 1 bucket per 2^scale bytes of low memory */
> Jerry> if (scale > PAGE_SHIFT)
> Jerry> --
> Jerry> 1.8.1.5
>
> Jerry> --
> Jerry> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> Jerry> the body of a message to [email protected]
> Jerry> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Jerry> Please read the FAQ at http://www.tux.org/lkml/



--
I love linux!!!

2013-07-18 17:26:04

by John Stoffel

[permalink] [raw]
Subject: Re: [PATCH] mm: negative left shift count when PAGE_SHIFT > 20


Jerry> When PAGE_SHIFT > 20, the result of "20 - PAGE_SHIFT" is negative. The
Jerry> calculating here will generate an unexpected result. In addition, if
Jerry> PAGE_SHIFT > 20, The memory size represented by numentries was already
Jerry> integral multiple of 1MB.

Why this magic number of 20? Please explain it better and replace it
was a #define that means something here.


Jerry> Signed-off-by: Jerry <[email protected]>
Jerry> ---
Jerry> mm/page_alloc.c | 8 +++++---
Jerry> 1 file changed, 5 insertions(+), 3 deletions(-)

Jerry> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
Jerry> index b100255..cd41797 100644
Jerry> --- a/mm/page_alloc.c
Jerry> +++ b/mm/page_alloc.c
Jerry> @@ -5745,9 +5745,11 @@ void *__init alloc_large_system_hash(const char *tablename,
Jerry> if (!numentries) {
Jerry> /* round applicable memory size up to nearest megabyte */
Jerry> numentries = nr_kernel_pages;
Jerry> - numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
Jerry> - numentries >>= 20 - PAGE_SHIFT;
Jerry> - numentries <<= 20 - PAGE_SHIFT;
Jerry> + if (20 > PAGE_SHIFT) {
Jerry> + numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
Jerry> + numentries >>= 20 - PAGE_SHIFT;
Jerry> + numentries <<= 20 - PAGE_SHIFT;
Jerry> + }

Jerry> /* limit to 1 bucket per 2^scale bytes of low memory */
Jerry> if (scale > PAGE_SHIFT)
Jerry> --
Jerry> 1.8.1.5

Jerry> --
Jerry> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
Jerry> the body of a message to [email protected]
Jerry> More majordomo info at http://vger.kernel.org/majordomo-info.html
Jerry> Please read the FAQ at http://www.tux.org/lkml/

2013-07-18 21:39:31

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] mm: negative left shift count when PAGE_SHIFT > 20

On Fri, 19 Jul 2013 00:56:12 +0800 Jerry <[email protected]> wrote:

> When PAGE_SHIFT > 20, the result of "20 - PAGE_SHIFT" is negative. The
> calculating here will generate an unexpected result. In addition, if
> PAGE_SHIFT > 20, The memory size represented by numentries was already
> integral multiple of 1MB.
>

If you tell me that you have a machine which has PAGE_SIZE=2MB and this
was the only problem which prevented Linux from running on that machine
then I'll apply the patch ;)

2013-07-18 23:47:05

by Jerry

[permalink] [raw]
Subject: Re: [PATCH] mm: negative left shift count when PAGE_SHIFT > 20

2013/7/19 Andrew Morton <[email protected]>:
> On Fri, 19 Jul 2013 00:56:12 +0800 Jerry <[email protected]> wrote:
>
>> When PAGE_SHIFT > 20, the result of "20 - PAGE_SHIFT" is negative. The
>> calculating here will generate an unexpected result. In addition, if
>> PAGE_SHIFT > 20, The memory size represented by numentries was already
>> integral multiple of 1MB.
>>
>
> If you tell me that you have a machine which has PAGE_SIZE=2MB and this
> was the only problem which prevented Linux from running on that machine
> then I'll apply the patch ;)
>

Hi Morton:
I just "grep -rn "#define\s\+PAGE_SHIFT" arch/", and find the
PAGE_SHIFT in some architecture is very big.
such as the following in "arch/hexagon/include/asm/page.h"
....
#ifdef CONFIG_PAGE_SIZE_256KB
#define PAGE_SHIFT 18
#define HEXAGON_L1_PTE_SIZE __HVM_PDE_S_256KB
#endif

#ifdef CONFIG_PAGE_SIZE_1MB
#define PAGE_SHIFT 20
#define HEXAGON_L1_PTE_SIZE __HVM_PDE_S_1MB
#endif
.....

Maybe the day of "A 2MB page" is not far. :-) I know it is just a
latent issue. Even if it won't generate a error when PAGE_SIZE == 20,
the calculating here is not necessary. In my mind, compiler would
optimize the calculating at that situation. But it is a little tricky.

In my patch, I think compiler would optimize "if (20 > PAGE_SIZE)", it
won't generate any machine instruction. Just a guarantee.

--
I love linux!!!

2013-07-19 21:57:32

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] mm: negative left shift count when PAGE_SHIFT > 20

On Fri, 19 Jul 2013 07:47:02 +0800 Jerry <[email protected]> wrote:

> 2013/7/19 Andrew Morton <[email protected]>:
> > On Fri, 19 Jul 2013 00:56:12 +0800 Jerry <[email protected]> wrote:
> >
> >> When PAGE_SHIFT > 20, the result of "20 - PAGE_SHIFT" is negative. The
> >> calculating here will generate an unexpected result. In addition, if
> >> PAGE_SHIFT > 20, The memory size represented by numentries was already
> >> integral multiple of 1MB.
> >>
> >
> > If you tell me that you have a machine which has PAGE_SIZE=2MB and this
> > was the only problem which prevented Linux from running on that machine
> > then I'll apply the patch ;)
> >
>
> Hi Morton:
> I just "grep -rn "#define\s\+PAGE_SHIFT" arch/", and find the
> PAGE_SHIFT in some architecture is very big.
> such as the following in "arch/hexagon/include/asm/page.h"
> ....
> #ifdef CONFIG_PAGE_SIZE_256KB
> #define PAGE_SHIFT 18
> #define HEXAGON_L1_PTE_SIZE __HVM_PDE_S_256KB
> #endif
>
> #ifdef CONFIG_PAGE_SIZE_1MB
> #define PAGE_SHIFT 20
> #define HEXAGON_L1_PTE_SIZE __HVM_PDE_S_1MB
> #endif
> .....

Good heavens.

> In my patch, I think compiler would optimize "if (20 > PAGE_SIZE)", it
> won't generate any machine instruction. Just a guarantee.

Well the existing code is a bit silly looking. Why can't we just do

/* round applicable memory size up to nearest megabyte */
if (PAGE_SHIFT < 20)
numentries = round_up(nr_kernel_pages, (1 << 20)/PAGE_SIZE);

or similar?

2013-07-20 17:17:01

by Jerry

[permalink] [raw]
Subject: Re: [PATCH] mm: negative left shift count when PAGE_SHIFT > 20

2013/7/20 Andrew Morton <[email protected]>:
> On Fri, 19 Jul 2013 07:47:02 +0800 Jerry <[email protected]> wrote:
>
>> 2013/7/19 Andrew Morton <[email protected]>:
>> > On Fri, 19 Jul 2013 00:56:12 +0800 Jerry <[email protected]> wrote:
>> >
>> >> When PAGE_SHIFT > 20, the result of "20 - PAGE_SHIFT" is negative. The
>> >> calculating here will generate an unexpected result. In addition, if
>> >> PAGE_SHIFT > 20, The memory size represented by numentries was already
>> >> integral multiple of 1MB.
>> >>
>> >
>> > If you tell me that you have a machine which has PAGE_SIZE=2MB and this
>> > was the only problem which prevented Linux from running on that machine
>> > then I'll apply the patch ;)
>> >
>>
>> Hi Morton:
>> I just "grep -rn "#define\s\+PAGE_SHIFT" arch/", and find the
>> PAGE_SHIFT in some architecture is very big.
>> such as the following in "arch/hexagon/include/asm/page.h"
>> ....
>> #ifdef CONFIG_PAGE_SIZE_256KB
>> #define PAGE_SHIFT 18
>> #define HEXAGON_L1_PTE_SIZE __HVM_PDE_S_256KB
>> #endif
>>
>> #ifdef CONFIG_PAGE_SIZE_1MB
>> #define PAGE_SHIFT 20
>> #define HEXAGON_L1_PTE_SIZE __HVM_PDE_S_1MB
>> #endif
>> .....
>
> Good heavens.
>
>> In my patch, I think compiler would optimize "if (20 > PAGE_SIZE)", it
>> won't generate any machine instruction. Just a guarantee.
>
> Well the existing code is a bit silly looking. Why can't we just do
>
> /* round applicable memory size up to nearest megabyte */
> if (PAGE_SHIFT < 20)
> numentries = round_up(nr_kernel_pages, (1 << 20)/PAGE_SIZE);
>
> or similar?

Great. I have adjusted these code lines, and sent the latest one.

--
I love linux!!!