powerpc/64s/slice: Use addr limit when computing slice mask

Message ID 20171110045507.30147-1-aneesh.kumar@linux.vnet.ibm.com
State New
Headers show
Series
  • powerpc/64s/slice: Use addr limit when computing slice mask
Related show

Commit Message

Aneesh Kumar K.V Nov. 10, 2017, 4:55 a.m.
While computing slice mask for the free area we need make sure we only search
in the addr limit applicable for this mmap. We update the slb_addr_limit
after we request for a mmap above 128TB. But the following mmap request
with hint addr below 128TB should still limit its search to below 128TB. ie.
we should not use slb_addr_limit to compute slice mask in this case. Instead,
we should derive high addr limit based on the mmap hint addr value.

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
---
 arch/powerpc/mm/slice.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

Comments

Michael Ellerman Nov. 10, 2017, 9:44 a.m. | #1
"Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> writes:

> While computing slice mask for the free area we need make sure we only search
> in the addr limit applicable for this mmap. We update the slb_addr_limit
> after we request for a mmap above 128TB. But the following mmap request
> with hint addr below 128TB should still limit its search to below 128TB. ie.
> we should not use slb_addr_limit to compute slice mask in this case. Instead,
> we should derive high addr limit based on the mmap hint addr value.
>
> Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
> ---
>  arch/powerpc/mm/slice.c | 34 ++++++++++++++++++++++------------
>  1 file changed, 22 insertions(+), 12 deletions(-)

How does this relate to the fixes Nick has sent?

cheers

> diff --git a/arch/powerpc/mm/slice.c b/arch/powerpc/mm/slice.c
> index 564fff06f5c1..23ec2c5e3b78 100644
> --- a/arch/powerpc/mm/slice.c
> +++ b/arch/powerpc/mm/slice.c
> @@ -122,7 +122,8 @@ static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
>  	return !slice_area_is_free(mm, start, end - start);
>  }
>  
> -static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret)
> +static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret,
> +				unsigned long high_limit)
>  {
>  	unsigned long i;
>  
> @@ -133,15 +134,16 @@ static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret)
>  		if (!slice_low_has_vma(mm, i))
>  			ret->low_slices |= 1u << i;
>  
> -	if (mm->context.slb_addr_limit <= SLICE_LOW_TOP)
> +	if (high_limit <= SLICE_LOW_TOP)
>  		return;
>  
> -	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++)
> +	for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++)
>  		if (!slice_high_has_vma(mm, i))
>  			__set_bit(i, ret->high_slices);
>  }
>  
> -static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret)
> +static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret,
> +				unsigned long high_limit)
>  {
>  	unsigned char *hpsizes;
>  	int index, mask_index;
> @@ -156,8 +158,11 @@ static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_ma
>  		if (((lpsizes >> (i * 4)) & 0xf) == psize)
>  			ret->low_slices |= 1u << i;
>  
> +	if (high_limit <= SLICE_LOW_TOP)
> +		return;
> +
>  	hpsizes = mm->context.high_slices_psize;
> -	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++) {
> +	for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++) {
>  		mask_index = i & 0x1;
>  		index = i >> 1;
>  		if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
> @@ -169,6 +174,10 @@ static int slice_check_fit(struct mm_struct *mm,
>  			   struct slice_mask mask, struct slice_mask available)
>  {
>  	DECLARE_BITMAP(result, SLICE_NUM_HIGH);
> +	/*
> +	 * Make sure we just do bit compare only to the max
> +	 * addr limit and not the full bit map size.
> +	 */
>  	unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit);
>  
>  	bitmap_and(result, mask.high_slices,
> @@ -472,7 +481,7 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
>  	/* First make up a "good" mask of slices that have the right size
>  	 * already
>  	 */
> -	slice_mask_for_size(mm, psize, &good_mask);
> +	slice_mask_for_size(mm, psize, &good_mask, high_limit);
>  	slice_print_mask(" good_mask", good_mask);
>  
>  	/*
> @@ -497,7 +506,7 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
>  #ifdef CONFIG_PPC_64K_PAGES
>  	/* If we support combo pages, we can allow 64k pages in 4k slices */
>  	if (psize == MMU_PAGE_64K) {
> -		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
> +		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
>  		if (fixed)
>  			slice_or_mask(&good_mask, &compat_mask);
>  	}
> @@ -530,11 +539,11 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
>  			return newaddr;
>  		}
>  	}
> -
> -	/* We don't fit in the good mask, check what other slices are
> +	/*
> +	 * We don't fit in the good mask, check what other slices are
>  	 * empty and thus can be converted
>  	 */
> -	slice_mask_for_free(mm, &potential_mask);
> +	slice_mask_for_free(mm, &potential_mask, high_limit);
>  	slice_or_mask(&potential_mask, &good_mask);
>  	slice_print_mask(" potential", potential_mask);
>  
> @@ -744,17 +753,18 @@ int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
>  {
>  	struct slice_mask mask, available;
>  	unsigned int psize = mm->context.user_psize;
> +	unsigned long high_limit = mm->context.slb_addr_limit;
>  
>  	if (radix_enabled())
>  		return 0;
>  
>  	slice_range_to_mask(addr, len, &mask);
> -	slice_mask_for_size(mm, psize, &available);
> +	slice_mask_for_size(mm, psize, &available, high_limit);
>  #ifdef CONFIG_PPC_64K_PAGES
>  	/* We need to account for 4k slices too */
>  	if (psize == MMU_PAGE_64K) {
>  		struct slice_mask compat_mask;
> -		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
> +		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
>  		slice_or_mask(&available, &compat_mask);
>  	}
>  #endif
> -- 
> 2.13.6
Aneesh Kumar K.V Nov. 10, 2017, 5:29 p.m. | #2
Michael Ellerman <mpe@ellerman.id.au> writes:

> "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> writes:
>
>> While computing slice mask for the free area we need make sure we only search
>> in the addr limit applicable for this mmap. We update the slb_addr_limit
>> after we request for a mmap above 128TB. But the following mmap request
>> with hint addr below 128TB should still limit its search to below 128TB. ie.
>> we should not use slb_addr_limit to compute slice mask in this case. Instead,
>> we should derive high addr limit based on the mmap hint addr value.
>>
>> Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
>> ---
>>  arch/powerpc/mm/slice.c | 34 ++++++++++++++++++++++------------
>>  1 file changed, 22 insertions(+), 12 deletions(-)
>
> How does this relate to the fixes Nick has sent?

This patch is on top of the patch series sent by Nick. Without this
patch we will allocate memory across the 128TB range if hint_addr <
128TB but hint_addr + len is more. Inorder to recreate this issue we
will have to map stack below. Hence one won't hit the error in general
case.



>
> cheers
>
>> diff --git a/arch/powerpc/mm/slice.c b/arch/powerpc/mm/slice.c
>> index 564fff06f5c1..23ec2c5e3b78 100644
>> --- a/arch/powerpc/mm/slice.c
>> +++ b/arch/powerpc/mm/slice.c
>> @@ -122,7 +122,8 @@ static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
>>  	return !slice_area_is_free(mm, start, end - start);
>>  }
>>  
>> -static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret)
>> +static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret,
>> +				unsigned long high_limit)
>>  {
>>  	unsigned long i;
>>  
>> @@ -133,15 +134,16 @@ static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret)
>>  		if (!slice_low_has_vma(mm, i))
>>  			ret->low_slices |= 1u << i;
>>  
>> -	if (mm->context.slb_addr_limit <= SLICE_LOW_TOP)
>> +	if (high_limit <= SLICE_LOW_TOP)
>>  		return;
>>  
>> -	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++)
>> +	for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++)
>>  		if (!slice_high_has_vma(mm, i))
>>  			__set_bit(i, ret->high_slices);
>>  }
>>  
>> -static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret)
>> +static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret,
>> +				unsigned long high_limit)
>>  {
>>  	unsigned char *hpsizes;
>>  	int index, mask_index;
>> @@ -156,8 +158,11 @@ static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_ma
>>  		if (((lpsizes >> (i * 4)) & 0xf) == psize)
>>  			ret->low_slices |= 1u << i;
>>  
>> +	if (high_limit <= SLICE_LOW_TOP)
>> +		return;
>> +
>>  	hpsizes = mm->context.high_slices_psize;
>> -	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++) {
>> +	for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++) {
>>  		mask_index = i & 0x1;
>>  		index = i >> 1;
>>  		if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
>> @@ -169,6 +174,10 @@ static int slice_check_fit(struct mm_struct *mm,
>>  			   struct slice_mask mask, struct slice_mask available)
>>  {
>>  	DECLARE_BITMAP(result, SLICE_NUM_HIGH);
>> +	/*
>> +	 * Make sure we just do bit compare only to the max
>> +	 * addr limit and not the full bit map size.
>> +	 */
>>  	unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit);
>>  
>>  	bitmap_and(result, mask.high_slices,
>> @@ -472,7 +481,7 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
>>  	/* First make up a "good" mask of slices that have the right size
>>  	 * already
>>  	 */
>> -	slice_mask_for_size(mm, psize, &good_mask);
>> +	slice_mask_for_size(mm, psize, &good_mask, high_limit);
>>  	slice_print_mask(" good_mask", good_mask);
>>  
>>  	/*
>> @@ -497,7 +506,7 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
>>  #ifdef CONFIG_PPC_64K_PAGES
>>  	/* If we support combo pages, we can allow 64k pages in 4k slices */
>>  	if (psize == MMU_PAGE_64K) {
>> -		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
>> +		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
>>  		if (fixed)
>>  			slice_or_mask(&good_mask, &compat_mask);
>>  	}
>> @@ -530,11 +539,11 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
>>  			return newaddr;
>>  		}
>>  	}
>> -
>> -	/* We don't fit in the good mask, check what other slices are
>> +	/*
>> +	 * We don't fit in the good mask, check what other slices are
>>  	 * empty and thus can be converted
>>  	 */
>> -	slice_mask_for_free(mm, &potential_mask);
>> +	slice_mask_for_free(mm, &potential_mask, high_limit);
>>  	slice_or_mask(&potential_mask, &good_mask);
>>  	slice_print_mask(" potential", potential_mask);
>>  
>> @@ -744,17 +753,18 @@ int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
>>  {
>>  	struct slice_mask mask, available;
>>  	unsigned int psize = mm->context.user_psize;
>> +	unsigned long high_limit = mm->context.slb_addr_limit;
>>  
>>  	if (radix_enabled())
>>  		return 0;
>>  
>>  	slice_range_to_mask(addr, len, &mask);
>> -	slice_mask_for_size(mm, psize, &available);
>> +	slice_mask_for_size(mm, psize, &available, high_limit);
>>  #ifdef CONFIG_PPC_64K_PAGES
>>  	/* We need to account for 4k slices too */
>>  	if (psize == MMU_PAGE_64K) {
>>  		struct slice_mask compat_mask;
>> -		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
>> +		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
>>  		slice_or_mask(&available, &compat_mask);
>>  	}
>>  #endif
>> -- 
>> 2.13.6
Nicholas Piggin Nov. 11, 2017, 9:09 a.m. | #3
On Fri, 10 Nov 2017 22:59:57 +0530
"Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> wrote:

> Michael Ellerman <mpe@ellerman.id.au> writes:
> 
> > "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> writes:
> >  
> >> While computing slice mask for the free area we need make sure we only search
> >> in the addr limit applicable for this mmap. We update the slb_addr_limit
> >> after we request for a mmap above 128TB. But the following mmap request
> >> with hint addr below 128TB should still limit its search to below 128TB. ie.
> >> we should not use slb_addr_limit to compute slice mask in this case. Instead,
> >> we should derive high addr limit based on the mmap hint addr value.
> >>
> >> Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
> >> ---
> >>  arch/powerpc/mm/slice.c | 34 ++++++++++++++++++++++------------
> >>  1 file changed, 22 insertions(+), 12 deletions(-)  
> >
> > How does this relate to the fixes Nick has sent?  
> 
> This patch is on top of the patch series sent by Nick. Without this
> patch we will allocate memory across the 128TB range if hint_addr <
> 128TB but hint_addr + len is more. Inorder to recreate this issue we
> will have to map stack below. Hence one won't hit the error in general
> case.

I couldn't get it to trigger this case after that series -- hash
get_unmapped_area should be excluding that case up front before
getting into the slice allocator. Do you have an strace to reproduce
it?

Either way I do think it would be good to tighten up all the slice
bitmap limits, including all the other places that hardcodes the
max bitmap size.

Thanks,
Nick
Aneesh Kumar K.V Nov. 11, 2017, 1:46 p.m. | #4
Nicholas Piggin <npiggin@gmail.com> writes:

> On Fri, 10 Nov 2017 22:59:57 +0530
> "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> wrote:
>
>> Michael Ellerman <mpe@ellerman.id.au> writes:
>> 
>> > "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> writes:
>> >  
>> >> While computing slice mask for the free area we need make sure we only search
>> >> in the addr limit applicable for this mmap. We update the slb_addr_limit
>> >> after we request for a mmap above 128TB. But the following mmap request
>> >> with hint addr below 128TB should still limit its search to below 128TB. ie.
>> >> we should not use slb_addr_limit to compute slice mask in this case. Instead,
>> >> we should derive high addr limit based on the mmap hint addr value.
>> >>
>> >> Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
>> >> ---
>> >>  arch/powerpc/mm/slice.c | 34 ++++++++++++++++++++++------------
>> >>  1 file changed, 22 insertions(+), 12 deletions(-)  
>> >
>> > How does this relate to the fixes Nick has sent?  
>> 
>> This patch is on top of the patch series sent by Nick. Without this
>> patch we will allocate memory across the 128TB range if hint_addr <
>> 128TB but hint_addr + len is more. Inorder to recreate this issue we
>> will have to map stack below. Hence one won't hit the error in general
>> case.
>
> I couldn't get it to trigger this case after that series -- hash
> get_unmapped_area should be excluding that case up front before
> getting into the slice allocator. Do you have an strace to reproduce
> it?

That is correct. This change in slice_get_unmapped_area prevents the
issue I mentioned above. I did have that hunk reverted to check the error
mask creation. 

-		if (addr > mm->task_size - len ||
+		if (addr > high_limit - len ||
 		    !slice_area_is_free(mm, addr, len))
 			addr = 0;
 	}


>
> Either way I do think it would be good to tighten up all the slice
> bitmap limits, including all the other places that hardcodes the
> max bitmap size.
>


-aneesh

Patch

diff --git a/arch/powerpc/mm/slice.c b/arch/powerpc/mm/slice.c
index 564fff06f5c1..23ec2c5e3b78 100644
--- a/arch/powerpc/mm/slice.c
+++ b/arch/powerpc/mm/slice.c
@@ -122,7 +122,8 @@  static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
 	return !slice_area_is_free(mm, start, end - start);
 }
 
-static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret)
+static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret,
+				unsigned long high_limit)
 {
 	unsigned long i;
 
@@ -133,15 +134,16 @@  static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret)
 		if (!slice_low_has_vma(mm, i))
 			ret->low_slices |= 1u << i;
 
-	if (mm->context.slb_addr_limit <= SLICE_LOW_TOP)
+	if (high_limit <= SLICE_LOW_TOP)
 		return;
 
-	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++)
+	for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++)
 		if (!slice_high_has_vma(mm, i))
 			__set_bit(i, ret->high_slices);
 }
 
-static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret)
+static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret,
+				unsigned long high_limit)
 {
 	unsigned char *hpsizes;
 	int index, mask_index;
@@ -156,8 +158,11 @@  static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_ma
 		if (((lpsizes >> (i * 4)) & 0xf) == psize)
 			ret->low_slices |= 1u << i;
 
+	if (high_limit <= SLICE_LOW_TOP)
+		return;
+
 	hpsizes = mm->context.high_slices_psize;
-	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++) {
+	for (i = 0; i < GET_HIGH_SLICE_INDEX(high_limit); i++) {
 		mask_index = i & 0x1;
 		index = i >> 1;
 		if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
@@ -169,6 +174,10 @@  static int slice_check_fit(struct mm_struct *mm,
 			   struct slice_mask mask, struct slice_mask available)
 {
 	DECLARE_BITMAP(result, SLICE_NUM_HIGH);
+	/*
+	 * Make sure we just do bit compare only to the max
+	 * addr limit and not the full bit map size.
+	 */
 	unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit);
 
 	bitmap_and(result, mask.high_slices,
@@ -472,7 +481,7 @@  unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 	/* First make up a "good" mask of slices that have the right size
 	 * already
 	 */
-	slice_mask_for_size(mm, psize, &good_mask);
+	slice_mask_for_size(mm, psize, &good_mask, high_limit);
 	slice_print_mask(" good_mask", good_mask);
 
 	/*
@@ -497,7 +506,7 @@  unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 #ifdef CONFIG_PPC_64K_PAGES
 	/* If we support combo pages, we can allow 64k pages in 4k slices */
 	if (psize == MMU_PAGE_64K) {
-		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
+		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
 		if (fixed)
 			slice_or_mask(&good_mask, &compat_mask);
 	}
@@ -530,11 +539,11 @@  unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 			return newaddr;
 		}
 	}
-
-	/* We don't fit in the good mask, check what other slices are
+	/*
+	 * We don't fit in the good mask, check what other slices are
 	 * empty and thus can be converted
 	 */
-	slice_mask_for_free(mm, &potential_mask);
+	slice_mask_for_free(mm, &potential_mask, high_limit);
 	slice_or_mask(&potential_mask, &good_mask);
 	slice_print_mask(" potential", potential_mask);
 
@@ -744,17 +753,18 @@  int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
 {
 	struct slice_mask mask, available;
 	unsigned int psize = mm->context.user_psize;
+	unsigned long high_limit = mm->context.slb_addr_limit;
 
 	if (radix_enabled())
 		return 0;
 
 	slice_range_to_mask(addr, len, &mask);
-	slice_mask_for_size(mm, psize, &available);
+	slice_mask_for_size(mm, psize, &available, high_limit);
 #ifdef CONFIG_PPC_64K_PAGES
 	/* We need to account for 4k slices too */
 	if (psize == MMU_PAGE_64K) {
 		struct slice_mask compat_mask;
-		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
+		slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask, high_limit);
 		slice_or_mask(&available, &compat_mask);
 	}
 #endif