diff mbox

[v5,2/5] arm: arm64: page_alloc: reduce unnecessary binary search in memblock_next_valid_pfn()

Message ID 1522636236-12625-3-git-send-email-hejianet@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jia He April 2, 2018, 2:30 a.m. UTC
Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns
where possible") optimized the loop in memmap_init_zone(). But there is
still some room for improvement. E.g. if pfn and pfn+1 are in the same
memblock region, we can simply pfn++ instead of doing the binary search
in memblock_next_valid_pfn.

Signed-off-by: Jia He <jia.he@hxt-semitech.com>
---
 arch/arm/include/asm/page.h   |  1 +
 arch/arm/mm/init.c            | 28 ++++++++++++++++++++++------
 arch/arm64/include/asm/page.h |  1 +
 arch/arm64/mm/init.c          | 28 ++++++++++++++++++++++------
 4 files changed, 46 insertions(+), 12 deletions(-)

Comments

Ard Biesheuvel April 2, 2018, 6:57 a.m. UTC | #1
On 2 April 2018 at 04:30, Jia He <hejianet@gmail.com> wrote:
> Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns
> where possible") optimized the loop in memmap_init_zone(). But there is
> still some room for improvement. E.g. if pfn and pfn+1 are in the same
> memblock region, we can simply pfn++ instead of doing the binary search
> in memblock_next_valid_pfn.
>
> Signed-off-by: Jia He <jia.he@hxt-semitech.com>
> ---
>  arch/arm/include/asm/page.h   |  1 +
>  arch/arm/mm/init.c            | 28 ++++++++++++++++++++++------
>  arch/arm64/include/asm/page.h |  1 +
>  arch/arm64/mm/init.c          | 28 ++++++++++++++++++++++------
>  4 files changed, 46 insertions(+), 12 deletions(-)
>

Could we put this in a shared file somewhere? This is the second patch
where you put make identical changes to ARM and arm64.


> diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h
> index 489875c..f38909c 100644
> --- a/arch/arm/include/asm/page.h
> +++ b/arch/arm/include/asm/page.h
> @@ -157,6 +157,7 @@ extern void copy_page(void *to, const void *from);
>  typedef struct page *pgtable_t;
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +extern int early_region_idx;
>  extern int pfn_valid(unsigned long);
>  extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
>  #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
> diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c
> index 0fb85ca..06ed190 100644
> --- a/arch/arm/mm/init.c
> +++ b/arch/arm/mm/init.c
> @@ -193,6 +193,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
>  }
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +int early_region_idx __meminitdata = -1;
> +
>  int pfn_valid(unsigned long pfn)
>  {
>         return memblock_is_map_memory(__pfn_to_phys(pfn));
> @@ -203,28 +205,42 @@ EXPORT_SYMBOL(pfn_valid);
>  unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
>  {
>         struct memblock_type *type = &memblock.memory;
> +       struct memblock_region *regions = type->regions;
>         unsigned int right = type->cnt;
>         unsigned int mid, left = 0;
> +       unsigned long start_pfn, end_pfn;
>         phys_addr_t addr = PFN_PHYS(++pfn);
>
> +       /* fast path, return pfn+1 if next pfn is in the same region */
> +       if (early_region_idx != -1) {
> +               start_pfn = PFN_DOWN(regions[early_region_idx].base);
> +               end_pfn = PFN_DOWN(regions[early_region_idx].base +
> +                               regions[early_region_idx].size);
> +
> +               if (pfn >= start_pfn && pfn < end_pfn)
> +                       return pfn;
> +       }
> +
> +       /* slow path, do the binary searching */
>         do {
>                 mid = (right + left) / 2;
>
> -               if (addr < type->regions[mid].base)
> +               if (addr < regions[mid].base)
>                         right = mid;
> -               else if (addr >= (type->regions[mid].base +
> -                                 type->regions[mid].size))
> +               else if (addr >= (regions[mid].base + regions[mid].size))
>                         left = mid + 1;
>                 else {
> -                       /* addr is within the region, so pfn is valid */
> +                       early_region_idx = mid;
>                         return pfn;
>                 }
>         } while (left < right);
>
>         if (right == type->cnt)
>                 return -1UL;
> -       else
> -               return PHYS_PFN(type->regions[right].base);
> +
> +       early_region_idx = right;
> +
> +       return PHYS_PFN(regions[early_region_idx].base);
>  }
>  EXPORT_SYMBOL(memblock_next_valid_pfn);
>  #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
> diff --git a/arch/arm64/include/asm/page.h b/arch/arm64/include/asm/page.h
> index e57d3f2..f0d8c8e5 100644
> --- a/arch/arm64/include/asm/page.h
> +++ b/arch/arm64/include/asm/page.h
> @@ -38,6 +38,7 @@ extern void clear_page(void *to);
>  typedef struct page *pgtable_t;
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +extern int early_region_idx;
>  extern int pfn_valid(unsigned long);
>  extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
>  #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
> diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
> index 13e43ff..342e4e2 100644
> --- a/arch/arm64/mm/init.c
> +++ b/arch/arm64/mm/init.c
> @@ -285,6 +285,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
>  #endif /* CONFIG_NUMA */
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +int early_region_idx __meminitdata = -1;
> +
>  int pfn_valid(unsigned long pfn)
>  {
>         return memblock_is_map_memory(pfn << PAGE_SHIFT);
> @@ -295,28 +297,42 @@ EXPORT_SYMBOL(pfn_valid);
>  unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
>  {
>         struct memblock_type *type = &memblock.memory;
> +       struct memblock_region *regions = type->regions;
>         unsigned int right = type->cnt;
>         unsigned int mid, left = 0;
> +       unsigned long start_pfn, end_pfn;
>         phys_addr_t addr = PFN_PHYS(++pfn);
>
> +       /* fast path, return pfn+1 if next pfn is in the same region */
> +       if (early_region_idx != -1) {
> +               start_pfn = PFN_DOWN(regions[early_region_idx].base);
> +               end_pfn = PFN_DOWN(regions[early_region_idx].base +
> +                               regions[early_region_idx].size);
> +
> +               if (pfn >= start_pfn && pfn < end_pfn)
> +                       return pfn;
> +       }
> +
> +       /* slow path, do the binary searching */
>         do {
>                 mid = (right + left) / 2;
>
> -               if (addr < type->regions[mid].base)
> +               if (addr < regions[mid].base)
>                         right = mid;
> -               else if (addr >= (type->regions[mid].base +
> -                                 type->regions[mid].size))
> +               else if (addr >= (regions[mid].base + regions[mid].size))
>                         left = mid + 1;
>                 else {
> -                       /* addr is within the region, so pfn is valid */
> +                       early_region_idx = mid;
>                         return pfn;
>                 }
>         } while (left < right);
>
>         if (right == type->cnt)
>                 return -1UL;
> -       else
> -               return PHYS_PFN(type->regions[right].base);
> +
> +       early_region_idx = right;
> +
> +       return PHYS_PFN(regions[early_region_idx].base);
>  }
>  EXPORT_SYMBOL(memblock_next_valid_pfn);
>  #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
> --
> 2.7.4
>
Daniel Vacek April 2, 2018, 8:01 a.m. UTC | #2
On Mon, Apr 2, 2018 at 4:30 AM, Jia He <hejianet@gmail.com> wrote:
> Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns
> where possible") optimized the loop in memmap_init_zone(). But there is
> still some room for improvement. E.g. if pfn and pfn+1 are in the same
> memblock region, we can simply pfn++ instead of doing the binary search
> in memblock_next_valid_pfn.
>
> Signed-off-by: Jia He <jia.he@hxt-semitech.com>
> ---
>  arch/arm/include/asm/page.h   |  1 +
>  arch/arm/mm/init.c            | 28 ++++++++++++++++++++++------
>  arch/arm64/include/asm/page.h |  1 +
>  arch/arm64/mm/init.c          | 28 ++++++++++++++++++++++------
>  4 files changed, 46 insertions(+), 12 deletions(-)
>
> diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h
> index 489875c..f38909c 100644
> --- a/arch/arm/include/asm/page.h
> +++ b/arch/arm/include/asm/page.h
> @@ -157,6 +157,7 @@ extern void copy_page(void *to, const void *from);
>  typedef struct page *pgtable_t;
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +extern int early_region_idx;

I believe this is not needed anymore.

>  extern int pfn_valid(unsigned long);
>  extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
>  #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
> diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c
> index 0fb85ca..06ed190 100644
> --- a/arch/arm/mm/init.c
> +++ b/arch/arm/mm/init.c
> @@ -193,6 +193,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
>  }
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +int early_region_idx __meminitdata = -1;

static?

> +
>  int pfn_valid(unsigned long pfn)
>  {
>         return memblock_is_map_memory(__pfn_to_phys(pfn));
> @@ -203,28 +205,42 @@ EXPORT_SYMBOL(pfn_valid);
>  unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
>  {
>         struct memblock_type *type = &memblock.memory;
> +       struct memblock_region *regions = type->regions;
>         unsigned int right = type->cnt;
>         unsigned int mid, left = 0;
> +       unsigned long start_pfn, end_pfn;
>         phys_addr_t addr = PFN_PHYS(++pfn);
>
> +       /* fast path, return pfn+1 if next pfn is in the same region */
> +       if (early_region_idx != -1) {
> +               start_pfn = PFN_DOWN(regions[early_region_idx].base);
> +               end_pfn = PFN_DOWN(regions[early_region_idx].base +
> +                               regions[early_region_idx].size);
> +
> +               if (pfn >= start_pfn && pfn < end_pfn)
> +                       return pfn;
> +       }
> +
> +       /* slow path, do the binary searching */
>         do {
>                 mid = (right + left) / 2;
>
> -               if (addr < type->regions[mid].base)
> +               if (addr < regions[mid].base)
>                         right = mid;
> -               else if (addr >= (type->regions[mid].base +
> -                                 type->regions[mid].size))
> +               else if (addr >= (regions[mid].base + regions[mid].size))
>                         left = mid + 1;
>                 else {
> -                       /* addr is within the region, so pfn is valid */
> +                       early_region_idx = mid;
>                         return pfn;
>                 }
>         } while (left < right);
>
>         if (right == type->cnt)
>                 return -1UL;
> -       else
> -               return PHYS_PFN(type->regions[right].base);
> +
> +       early_region_idx = right;
> +
> +       return PHYS_PFN(regions[early_region_idx].base);
>  }
>  EXPORT_SYMBOL(memblock_next_valid_pfn);
>  #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
> diff --git a/arch/arm64/include/asm/page.h b/arch/arm64/include/asm/page.h
> index e57d3f2..f0d8c8e5 100644
> --- a/arch/arm64/include/asm/page.h
> +++ b/arch/arm64/include/asm/page.h
> @@ -38,6 +38,7 @@ extern void clear_page(void *to);
>  typedef struct page *pgtable_t;
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +extern int early_region_idx;

ditto

>  extern int pfn_valid(unsigned long);
>  extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
>  #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
> diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
> index 13e43ff..342e4e2 100644
> --- a/arch/arm64/mm/init.c
> +++ b/arch/arm64/mm/init.c
> @@ -285,6 +285,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
>  #endif /* CONFIG_NUMA */
>
>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
> +int early_region_idx __meminitdata = -1;

ditto

--nX

> +
>  int pfn_valid(unsigned long pfn)
>  {
>         return memblock_is_map_memory(pfn << PAGE_SHIFT);
> @@ -295,28 +297,42 @@ EXPORT_SYMBOL(pfn_valid);
>  unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
>  {
>         struct memblock_type *type = &memblock.memory;
> +       struct memblock_region *regions = type->regions;
>         unsigned int right = type->cnt;
>         unsigned int mid, left = 0;
> +       unsigned long start_pfn, end_pfn;
>         phys_addr_t addr = PFN_PHYS(++pfn);
>
> +       /* fast path, return pfn+1 if next pfn is in the same region */
> +       if (early_region_idx != -1) {
> +               start_pfn = PFN_DOWN(regions[early_region_idx].base);
> +               end_pfn = PFN_DOWN(regions[early_region_idx].base +
> +                               regions[early_region_idx].size);
> +
> +               if (pfn >= start_pfn && pfn < end_pfn)
> +                       return pfn;
> +       }
> +
> +       /* slow path, do the binary searching */
>         do {
>                 mid = (right + left) / 2;
>
> -               if (addr < type->regions[mid].base)
> +               if (addr < regions[mid].base)
>                         right = mid;
> -               else if (addr >= (type->regions[mid].base +
> -                                 type->regions[mid].size))
> +               else if (addr >= (regions[mid].base + regions[mid].size))
>                         left = mid + 1;
>                 else {
> -                       /* addr is within the region, so pfn is valid */
> +                       early_region_idx = mid;
>                         return pfn;
>                 }
>         } while (left < right);
>
>         if (right == type->cnt)
>                 return -1UL;
> -       else
> -               return PHYS_PFN(type->regions[right].base);
> +
> +       early_region_idx = right;
> +
> +       return PHYS_PFN(regions[early_region_idx].base);
>  }
>  EXPORT_SYMBOL(memblock_next_valid_pfn);
>  #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
> --
> 2.7.4
>
Daniel Vacek April 2, 2018, 8:43 a.m. UTC | #3
On Mon, Apr 2, 2018 at 8:57 AM, Ard Biesheuvel
<ard.biesheuvel@linaro.org> wrote:
> On 2 April 2018 at 04:30, Jia He <hejianet@gmail.com> wrote:
>> Commit b92df1de5d28 ("mm: page_alloc: skip over regions of invalid pfns
>> where possible") optimized the loop in memmap_init_zone(). But there is
>> still some room for improvement. E.g. if pfn and pfn+1 are in the same
>> memblock region, we can simply pfn++ instead of doing the binary search
>> in memblock_next_valid_pfn.
>>
>> Signed-off-by: Jia He <jia.he@hxt-semitech.com>
>> ---
>>  arch/arm/include/asm/page.h   |  1 +
>>  arch/arm/mm/init.c            | 28 ++++++++++++++++++++++------
>>  arch/arm64/include/asm/page.h |  1 +
>>  arch/arm64/mm/init.c          | 28 ++++++++++++++++++++++------
>>  4 files changed, 46 insertions(+), 12 deletions(-)
>>
>
> Could we put this in a shared file somewhere? This is the second patch
> where you put make identical changes to ARM and arm64.

Ard, I was wondering if we can actually have this changed to something
like CONFIG_MEMBLOCK_PFN_VALID and shared instead of it being arm
specific? Is there a reason it's only usable for arm? The rest is
dependent on this, hence I suggested to place it close-by. But
generalizing it all would make it a lot cleaner.

arch/arm/mm/init.c:196:
#ifdef CONFIG_HAVE_ARCH_PFN_VALID
int pfn_valid(unsigned long pfn)
{
        return memblock_is_map_memory(__pfn_to_phys(pfn));
}
EXPORT_SYMBOL(pfn_valid);
#endif

arch/arm64/mm/init.c:287:
#ifdef CONFIG_HAVE_ARCH_PFN_VALID
int pfn_valid(unsigned long pfn)
{
        return memblock_is_map_memory(pfn << PAGE_SHIFT);
}
EXPORT_SYMBOL(pfn_valid);
#endif

--nX

>> diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h
>> index 489875c..f38909c 100644
>> --- a/arch/arm/include/asm/page.h
>> +++ b/arch/arm/include/asm/page.h
>> @@ -157,6 +157,7 @@ extern void copy_page(void *to, const void *from);
>>  typedef struct page *pgtable_t;
>>
>>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
>> +extern int early_region_idx;
>>  extern int pfn_valid(unsigned long);
>>  extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
>>  #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
>> diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c
>> index 0fb85ca..06ed190 100644
>> --- a/arch/arm/mm/init.c
>> +++ b/arch/arm/mm/init.c
>> @@ -193,6 +193,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
>>  }
>>
>>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
>> +int early_region_idx __meminitdata = -1;
>> +
>>  int pfn_valid(unsigned long pfn)
>>  {
>>         return memblock_is_map_memory(__pfn_to_phys(pfn));
>> @@ -203,28 +205,42 @@ EXPORT_SYMBOL(pfn_valid);
>>  unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
>>  {
>>         struct memblock_type *type = &memblock.memory;
>> +       struct memblock_region *regions = type->regions;
>>         unsigned int right = type->cnt;
>>         unsigned int mid, left = 0;
>> +       unsigned long start_pfn, end_pfn;
>>         phys_addr_t addr = PFN_PHYS(++pfn);
>>
>> +       /* fast path, return pfn+1 if next pfn is in the same region */
>> +       if (early_region_idx != -1) {
>> +               start_pfn = PFN_DOWN(regions[early_region_idx].base);
>> +               end_pfn = PFN_DOWN(regions[early_region_idx].base +
>> +                               regions[early_region_idx].size);
>> +
>> +               if (pfn >= start_pfn && pfn < end_pfn)
>> +                       return pfn;
>> +       }
>> +
>> +       /* slow path, do the binary searching */
>>         do {
>>                 mid = (right + left) / 2;
>>
>> -               if (addr < type->regions[mid].base)
>> +               if (addr < regions[mid].base)
>>                         right = mid;
>> -               else if (addr >= (type->regions[mid].base +
>> -                                 type->regions[mid].size))
>> +               else if (addr >= (regions[mid].base + regions[mid].size))
>>                         left = mid + 1;
>>                 else {
>> -                       /* addr is within the region, so pfn is valid */
>> +                       early_region_idx = mid;
>>                         return pfn;
>>                 }
>>         } while (left < right);
>>
>>         if (right == type->cnt)
>>                 return -1UL;
>> -       else
>> -               return PHYS_PFN(type->regions[right].base);
>> +
>> +       early_region_idx = right;
>> +
>> +       return PHYS_PFN(regions[early_region_idx].base);
>>  }
>>  EXPORT_SYMBOL(memblock_next_valid_pfn);
>>  #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
>> diff --git a/arch/arm64/include/asm/page.h b/arch/arm64/include/asm/page.h
>> index e57d3f2..f0d8c8e5 100644
>> --- a/arch/arm64/include/asm/page.h
>> +++ b/arch/arm64/include/asm/page.h
>> @@ -38,6 +38,7 @@ extern void clear_page(void *to);
>>  typedef struct page *pgtable_t;
>>
>>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
>> +extern int early_region_idx;
>>  extern int pfn_valid(unsigned long);
>>  extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
>>  #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
>> diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
>> index 13e43ff..342e4e2 100644
>> --- a/arch/arm64/mm/init.c
>> +++ b/arch/arm64/mm/init.c
>> @@ -285,6 +285,8 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
>>  #endif /* CONFIG_NUMA */
>>
>>  #ifdef CONFIG_HAVE_ARCH_PFN_VALID
>> +int early_region_idx __meminitdata = -1;
>> +
>>  int pfn_valid(unsigned long pfn)
>>  {
>>         return memblock_is_map_memory(pfn << PAGE_SHIFT);
>> @@ -295,28 +297,42 @@ EXPORT_SYMBOL(pfn_valid);
>>  unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
>>  {
>>         struct memblock_type *type = &memblock.memory;
>> +       struct memblock_region *regions = type->regions;
>>         unsigned int right = type->cnt;
>>         unsigned int mid, left = 0;
>> +       unsigned long start_pfn, end_pfn;
>>         phys_addr_t addr = PFN_PHYS(++pfn);
>>
>> +       /* fast path, return pfn+1 if next pfn is in the same region */
>> +       if (early_region_idx != -1) {
>> +               start_pfn = PFN_DOWN(regions[early_region_idx].base);
>> +               end_pfn = PFN_DOWN(regions[early_region_idx].base +
>> +                               regions[early_region_idx].size);
>> +
>> +               if (pfn >= start_pfn && pfn < end_pfn)
>> +                       return pfn;
>> +       }
>> +
>> +       /* slow path, do the binary searching */
>>         do {
>>                 mid = (right + left) / 2;
>>
>> -               if (addr < type->regions[mid].base)
>> +               if (addr < regions[mid].base)
>>                         right = mid;
>> -               else if (addr >= (type->regions[mid].base +
>> -                                 type->regions[mid].size))
>> +               else if (addr >= (regions[mid].base + regions[mid].size))
>>                         left = mid + 1;
>>                 else {
>> -                       /* addr is within the region, so pfn is valid */
>> +                       early_region_idx = mid;
>>                         return pfn;
>>                 }
>>         } while (left < right);
>>
>>         if (right == type->cnt)
>>                 return -1UL;
>> -       else
>> -               return PHYS_PFN(type->regions[right].base);
>> +
>> +       early_region_idx = right;
>> +
>> +       return PHYS_PFN(regions[early_region_idx].base);
>>  }
>>  EXPORT_SYMBOL(memblock_next_valid_pfn);
>>  #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
>> --
>> 2.7.4
>>
kernel test robot April 2, 2018, 3:07 p.m. UTC | #4
Hi Jia,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v4.16 next-20180329]
[cannot apply to arm64/for-next/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Jia-He/optimize-memblock_next_valid_pfn-and-early_pfn_valid-on-arm-and-arm64/20180402-131223
config: arm64-allmodconfig (attached as .config)
compiler: aarch64-linux-gnu-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=arm64 

All warnings (new ones prefixed by >>):

>> WARNING: vmlinux.o(.text+0x39a80): Section mismatch in reference from the function memblock_next_valid_pfn() to the variable .meminit.data:$d
   The function memblock_next_valid_pfn() references
   the variable __meminitdata $d.
   This is often because memblock_next_valid_pfn lacks a __meminitdata
   annotation or the annotation of $d is wrong.

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
diff mbox

Patch

diff --git a/arch/arm/include/asm/page.h b/arch/arm/include/asm/page.h
index 489875c..f38909c 100644
--- a/arch/arm/include/asm/page.h
+++ b/arch/arm/include/asm/page.h
@@ -157,6 +157,7 @@  extern void copy_page(void *to, const void *from);
 typedef struct page *pgtable_t;
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
+extern int early_region_idx;
 extern int pfn_valid(unsigned long);
 extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
 #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
diff --git a/arch/arm/mm/init.c b/arch/arm/mm/init.c
index 0fb85ca..06ed190 100644
--- a/arch/arm/mm/init.c
+++ b/arch/arm/mm/init.c
@@ -193,6 +193,8 @@  static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
 }
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
+int early_region_idx __meminitdata = -1;
+
 int pfn_valid(unsigned long pfn)
 {
 	return memblock_is_map_memory(__pfn_to_phys(pfn));
@@ -203,28 +205,42 @@  EXPORT_SYMBOL(pfn_valid);
 unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
 {
 	struct memblock_type *type = &memblock.memory;
+	struct memblock_region *regions = type->regions;
 	unsigned int right = type->cnt;
 	unsigned int mid, left = 0;
+	unsigned long start_pfn, end_pfn;
 	phys_addr_t addr = PFN_PHYS(++pfn);
 
+	/* fast path, return pfn+1 if next pfn is in the same region */
+	if (early_region_idx != -1) {
+		start_pfn = PFN_DOWN(regions[early_region_idx].base);
+		end_pfn = PFN_DOWN(regions[early_region_idx].base +
+				regions[early_region_idx].size);
+
+		if (pfn >= start_pfn && pfn < end_pfn)
+			return pfn;
+	}
+
+	/* slow path, do the binary searching */
 	do {
 		mid = (right + left) / 2;
 
-		if (addr < type->regions[mid].base)
+		if (addr < regions[mid].base)
 			right = mid;
-		else if (addr >= (type->regions[mid].base +
-				  type->regions[mid].size))
+		else if (addr >= (regions[mid].base + regions[mid].size))
 			left = mid + 1;
 		else {
-			/* addr is within the region, so pfn is valid */
+			early_region_idx = mid;
 			return pfn;
 		}
 	} while (left < right);
 
 	if (right == type->cnt)
 		return -1UL;
-	else
-		return PHYS_PFN(type->regions[right].base);
+
+	early_region_idx = right;
+
+	return PHYS_PFN(regions[early_region_idx].base);
 }
 EXPORT_SYMBOL(memblock_next_valid_pfn);
 #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/
diff --git a/arch/arm64/include/asm/page.h b/arch/arm64/include/asm/page.h
index e57d3f2..f0d8c8e5 100644
--- a/arch/arm64/include/asm/page.h
+++ b/arch/arm64/include/asm/page.h
@@ -38,6 +38,7 @@  extern void clear_page(void *to);
 typedef struct page *pgtable_t;
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
+extern int early_region_idx;
 extern int pfn_valid(unsigned long);
 extern unsigned long memblock_next_valid_pfn(unsigned long pfn);
 #define skip_to_last_invalid_pfn(pfn) (memblock_next_valid_pfn(pfn) - 1)
diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c
index 13e43ff..342e4e2 100644
--- a/arch/arm64/mm/init.c
+++ b/arch/arm64/mm/init.c
@@ -285,6 +285,8 @@  static void __init zone_sizes_init(unsigned long min, unsigned long max)
 #endif /* CONFIG_NUMA */
 
 #ifdef CONFIG_HAVE_ARCH_PFN_VALID
+int early_region_idx __meminitdata = -1;
+
 int pfn_valid(unsigned long pfn)
 {
 	return memblock_is_map_memory(pfn << PAGE_SHIFT);
@@ -295,28 +297,42 @@  EXPORT_SYMBOL(pfn_valid);
 unsigned long __init_memblock memblock_next_valid_pfn(unsigned long pfn)
 {
 	struct memblock_type *type = &memblock.memory;
+	struct memblock_region *regions = type->regions;
 	unsigned int right = type->cnt;
 	unsigned int mid, left = 0;
+	unsigned long start_pfn, end_pfn;
 	phys_addr_t addr = PFN_PHYS(++pfn);
 
+	/* fast path, return pfn+1 if next pfn is in the same region */
+	if (early_region_idx != -1) {
+		start_pfn = PFN_DOWN(regions[early_region_idx].base);
+		end_pfn = PFN_DOWN(regions[early_region_idx].base +
+				regions[early_region_idx].size);
+
+		if (pfn >= start_pfn && pfn < end_pfn)
+			return pfn;
+	}
+
+	/* slow path, do the binary searching */
 	do {
 		mid = (right + left) / 2;
 
-		if (addr < type->regions[mid].base)
+		if (addr < regions[mid].base)
 			right = mid;
-		else if (addr >= (type->regions[mid].base +
-				  type->regions[mid].size))
+		else if (addr >= (regions[mid].base + regions[mid].size))
 			left = mid + 1;
 		else {
-			/* addr is within the region, so pfn is valid */
+			early_region_idx = mid;
 			return pfn;
 		}
 	} while (left < right);
 
 	if (right == type->cnt)
 		return -1UL;
-	else
-		return PHYS_PFN(type->regions[right].base);
+
+	early_region_idx = right;
+
+	return PHYS_PFN(regions[early_region_idx].base);
 }
 EXPORT_SYMBOL(memblock_next_valid_pfn);
 #endif /*CONFIG_HAVE_ARCH_PFN_VALID*/