diff mbox series

[v3,7/7] mm/sparse.c: Use __get_free_pages() instead in populate_section_memmap()

Message ID 20200307084229.28251-8-bhe@redhat.com (mailing list archive)
State New, archived
Headers show
Series mm/hotplug: Only use subsection map for VMEMMAP | expand

Commit Message

Baoquan He March 7, 2020, 8:42 a.m. UTC
This removes the unnecessary goto, and simplify codes.

Signed-off-by: Baoquan He <bhe@redhat.com>
Reviewed-by: Wei Yang <richardw.yang@linux.intel.com>
---
 mm/sparse.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

Comments

Michal Hocko March 10, 2020, 2:56 p.m. UTC | #1
On Sat 07-03-20 16:42:29, Baoquan He wrote:
> This removes the unnecessary goto, and simplify codes.
> 
> Signed-off-by: Baoquan He <bhe@redhat.com>
> Reviewed-by: Wei Yang <richardw.yang@linux.intel.com>
> ---
>  mm/sparse.c | 16 ++++++----------
>  1 file changed, 6 insertions(+), 10 deletions(-)
> 
> diff --git a/mm/sparse.c b/mm/sparse.c
> index fde651ab8741..266f7f5040fb 100644
> --- a/mm/sparse.c
> +++ b/mm/sparse.c
> @@ -735,23 +735,19 @@ static void free_map_bootmem(struct page *memmap)
>  struct page * __meminit populate_section_memmap(unsigned long pfn,
>  		unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
>  {
> -	struct page *page, *ret;
> +	struct page *ret;
>  	unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
>  
> -	page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
> -	if (page)
> -		goto got_map_page;
> +	ret = (void*)__get_free_pages(GFP_KERNEL|__GFP_NOWARN,
> +				get_order(memmap_size));
> +	if (ret)
> +		return ret;
>  
>  	ret = vmalloc(memmap_size);
>  	if (ret)
> -		goto got_map_ptr;
> +		return ret;
>  
>  	return NULL;
> -got_map_page:
> -	ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
> -got_map_ptr:
> -
> -	return ret;
>  }

Boy this code is ugly. Is there any reason we cannot simply use
kvmalloc_array(PAGES_PER_SECTION, sizeof(struct page), GFP_KERNEL | __GFP_NOWARN)

And if we care about locality then go even one step further
kvmalloc_node(PAGES_PER_SECTION * sizeof(struct page), GFP_KERNEL | __GFP_NOWARN, nid)
David Hildenbrand March 10, 2020, 2:59 p.m. UTC | #2
On 10.03.20 15:56, Michal Hocko wrote:
> On Sat 07-03-20 16:42:29, Baoquan He wrote:
>> This removes the unnecessary goto, and simplify codes.
>>
>> Signed-off-by: Baoquan He <bhe@redhat.com>
>> Reviewed-by: Wei Yang <richardw.yang@linux.intel.com>
>> ---
>>  mm/sparse.c | 16 ++++++----------
>>  1 file changed, 6 insertions(+), 10 deletions(-)
>>
>> diff --git a/mm/sparse.c b/mm/sparse.c
>> index fde651ab8741..266f7f5040fb 100644
>> --- a/mm/sparse.c
>> +++ b/mm/sparse.c
>> @@ -735,23 +735,19 @@ static void free_map_bootmem(struct page *memmap)
>>  struct page * __meminit populate_section_memmap(unsigned long pfn,
>>  		unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
>>  {
>> -	struct page *page, *ret;
>> +	struct page *ret;
>>  	unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
>>  
>> -	page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
>> -	if (page)
>> -		goto got_map_page;
>> +	ret = (void*)__get_free_pages(GFP_KERNEL|__GFP_NOWARN,
>> +				get_order(memmap_size));
>> +	if (ret)
>> +		return ret;
>>  
>>  	ret = vmalloc(memmap_size);
>>  	if (ret)
>> -		goto got_map_ptr;
>> +		return ret;
>>  
>>  	return NULL;
>> -got_map_page:
>> -	ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
>> -got_map_ptr:
>> -
>> -	return ret;
>>  }
> 
> Boy this code is ugly. Is there any reason we cannot simply use
> kvmalloc_array(PAGES_PER_SECTION, sizeof(struct page), GFP_KERNEL | __GFP_NOWARN)
> 
> And if we care about locality then go even one step further
> kvmalloc_node(PAGES_PER_SECTION * sizeof(struct page), GFP_KERNEL | __GFP_NOWARN, nid)
> 

Makes perfect sense to me.
Baoquan He March 11, 2020, 9:31 a.m. UTC | #3
On 03/10/20 at 03:56pm, Michal Hocko wrote:
> On Sat 07-03-20 16:42:29, Baoquan He wrote:
> > This removes the unnecessary goto, and simplify codes.
> > 
> > Signed-off-by: Baoquan He <bhe@redhat.com>
> > Reviewed-by: Wei Yang <richardw.yang@linux.intel.com>
> > ---
> >  mm/sparse.c | 16 ++++++----------
> >  1 file changed, 6 insertions(+), 10 deletions(-)
> > 
> > diff --git a/mm/sparse.c b/mm/sparse.c
> > index fde651ab8741..266f7f5040fb 100644
> > --- a/mm/sparse.c
> > +++ b/mm/sparse.c
> > @@ -735,23 +735,19 @@ static void free_map_bootmem(struct page *memmap)
> >  struct page * __meminit populate_section_memmap(unsigned long pfn,
> >  		unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
> >  {
> > -	struct page *page, *ret;
> > +	struct page *ret;
> >  	unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
> >  
> > -	page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
> > -	if (page)
> > -		goto got_map_page;
> > +	ret = (void*)__get_free_pages(GFP_KERNEL|__GFP_NOWARN,
> > +				get_order(memmap_size));
> > +	if (ret)
> > +		return ret;
> >  
> >  	ret = vmalloc(memmap_size);
> >  	if (ret)
> > -		goto got_map_ptr;
> > +		return ret;
> >  
> >  	return NULL;
> > -got_map_page:
> > -	ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
> > -got_map_ptr:
> > -
> > -	return ret;
> >  }
> 
> Boy this code is ugly. Is there any reason we cannot simply use
> kvmalloc_array(PAGES_PER_SECTION, sizeof(struct page), GFP_KERNEL | __GFP_NOWARN)
> 
> And if we care about locality then go even one step further
> kvmalloc_node(PAGES_PER_SECTION * sizeof(struct page), GFP_KERNEL | __GFP_NOWARN, nid)

Yes, this looks better. I will use this to make a new version. Thanks.
diff mbox series

Patch

diff --git a/mm/sparse.c b/mm/sparse.c
index fde651ab8741..266f7f5040fb 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -735,23 +735,19 @@  static void free_map_bootmem(struct page *memmap)
 struct page * __meminit populate_section_memmap(unsigned long pfn,
 		unsigned long nr_pages, int nid, struct vmem_altmap *altmap)
 {
-	struct page *page, *ret;
+	struct page *ret;
 	unsigned long memmap_size = sizeof(struct page) * PAGES_PER_SECTION;
 
-	page = alloc_pages(GFP_KERNEL|__GFP_NOWARN, get_order(memmap_size));
-	if (page)
-		goto got_map_page;
+	ret = (void*)__get_free_pages(GFP_KERNEL|__GFP_NOWARN,
+				get_order(memmap_size));
+	if (ret)
+		return ret;
 
 	ret = vmalloc(memmap_size);
 	if (ret)
-		goto got_map_ptr;
+		return ret;
 
 	return NULL;
-got_map_page:
-	ret = (struct page *)pfn_to_kaddr(page_to_pfn(page));
-got_map_ptr:
-
-	return ret;
 }
 
 static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,