Skip to contentSkip to conversation

VA allocator fixes

PATCH Superseded Nicholas Piggin 4 participants 22 messages Nov 6, 2017 – Nov 7, 2017 PowerPC

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nicholas Piggin

author reviewer
#0 lore raw

cover VA allocator fixes

Florian found a nasty corner case with the VA allocation logic for crossing from 128TB to 512TB limit on hash, and made a really superb report of the problem -- traces, reproducer recipes, analysis, etc. which already mostly solved it.

The first patch in the series should solve Florian's particular case, the next 3 are other issues with addr_limit. The last patch is technically a cleanup but I think it's fairly important in terms of understanding the code and also enabling some BUG checks (when addr_limit == 0).

I have not tested these exactly on Florian's test case, but some tests of my own behave better afterwards. Hopefully he has time to re-test. Some careful review would be welcome too.

Thanks,
Nick

Nicholas Piggin (5):
powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case
allocation
powerpc/64s/hash: Allow MAP_FIXED allocations to cross 128TB boundary powerpc/64s/hash: Fix fork() with 512TB process address space powerpc/64s/radix: Fix 128TB-512TB virtual address boundary case
allocation
powerpc/64s: mm_context.addr_limit is only used on hash

 arch/powerpc/include/asm/book3s/64/mmu-hash.h |  2 +-
 arch/powerpc/include/asm/book3s/64/mmu.h      |  2 +-
 arch/powerpc/include/asm/paca.h               |  2 +-
 arch/powerpc/kernel/asm-offsets.c             |  2 +-
 arch/powerpc/kernel/paca.c                    |  4 ++--
 arch/powerpc/kernel/setup-common.c            |  3 ++-
 arch/powerpc/mm/hugetlbpage-radix.c           | 14 +++++-------
 arch/powerpc/mm/mmap.c                        | 31 ++++++++++----------------
 arch/powerpc/mm/mmu_context_book3s64.c        |  8 +++----
 arch/powerpc/mm/slb_low.S                     |  2 +-
 arch/powerpc/mm/slice.c                       | 32 ++++++++++++++-------------
 11 files changed, 48 insertions(+), 54 deletions(-)
Signature · 1 line
2.15.0
author reviewer
#1 lore raw

PATCH 1/5 powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case allocation

When allocating VA space with a hint that crosses 128TB, the SLB addr_limit variable is not expanded if addr is not > 128TB, but the slice allocation looks at task_size, which is 512TB. This results in slice_check_fit() incorrectly succeeding because the slice_count truncates off bit 128 of the requested mask, so the comparison to the available mask succeeds.

Fix this by using mm->context.addr_limit instead of mm->task_size for testing allocation limits. This causes such allocations to fail.

Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/slice.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/powerpc/mm/slice.c +6 -5
index 45f6740dd407..567db541c0a1 100644
@@ -96,7 +96,7 @@ static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
 {
 	struct vm_area_struct *vma;
 
-	if ((mm->task_size - len) < addr)
+	if ((mm->context.addr_limit - len) < addr)
 		return 0;
 	vma = find_vma(mm, addr);
 	return (!vma || (addr + len) <= vm_start_gap(vma));
@@ -133,7 +133,7 @@ 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->task_size <= SLICE_LOW_TOP)
+	if (mm->context.addr_limit <= SLICE_LOW_TOP)
 		return;
 
 	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++)
@@ -446,19 +446,20 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 
 	/* Sanity checks */
 	BUG_ON(mm->task_size == 0);
+	BUG_ON(mm->context.addr_limit == 0);
 	VM_BUG_ON(radix_enabled());
 
 	slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
 	slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
 		  addr, len, flags, topdown);
 
-	if (len > mm->task_size)
+	if (len > mm->context.addr_limit)
 		return -ENOMEM;
 	if (len & ((1ul << pshift) - 1))
 		return -EINVAL;
 	if (fixed && (addr & ((1ul << pshift) - 1)))
 		return -EINVAL;
-	if (fixed && addr > (mm->task_size - len))
+	if (fixed && addr > (mm->context.addr_limit - len))
 		return -ENOMEM;
 
 	/* If hint, make sure it matches our alignment restrictions */
@@ -466,7 +467,7 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 		addr = _ALIGN_UP(addr, 1ul << pshift);
 		slice_dbg(" aligned addr=%lx\n", addr);
 		/* Ignore hint if it's too large or overlaps a VMA */
-		if (addr > mm->task_size - len ||
+		if (addr > mm->context.addr_limit - len ||
 		    !slice_area_is_free(mm, addr, len))
 			addr = 0;
 	}
Signature · 1 line
2.15.0
#2 lore raw

Re: [PATCH 1/5] powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case allocation

Quoted 5 lines · Nicholas Piggin

When allocating VA space with a hint that crosses 128TB, the SLB addr_limit variable is not expanded if addr is not > 128TB, but the slice allocation looks at task_size, which is 512TB. This results in slice_check_fit() incorrectly succeeding because the slice_count truncates off bit 128 of the requested mask, so the comparison to the available mask succeeds.

But then the mask passed to slice_check_fit() is generated using context.addr_limit as max value. So how did that return succcess? ie, we get the request mask via

slice_range_to_mask(addr, len, &mask);

And the potential/possible mask using

slice_mask_for_size(mm, psize, &good_mask);

So how did slice_check_fit() return sucess with

slice_check_fit(mm, mask, good_mask);

Quoted 17 lines

Fix this by using mm->context.addr_limit instead of mm->task_size for testing allocation limits. This causes such allocations to fail.

Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/slice.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/powerpc/mm/slice.c +0 -0
index 45f6740dd407..567db541c0a1 100644

arch/powerpc/mm/slice.c static int slice_area_is_free(struct mm_struct *mm, unsigned long addr, · line 99

Quoted 5 lines
arch/powerpc/mm/slice.c +1 -1
@@ -96,7 +96,7 @@ static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
 {
 	struct vm_area_struct *vma;
 
-	if ((mm->task_size - len) < addr)
+	if ((mm->context.addr_limit - len) < addr)

I was looking at these as generic boundary check against task size and for specific range check we should have created mask always using context.addr_limit. That should keep the boundary condition check same across radix/hash.

Quoted 46 lines
arch/powerpc/mm/slice.c +5 -4
 		return 0;
 	vma = find_vma(mm, addr);
 	return (!vma || (addr + len) <= vm_start_gap(vma));
@@ -133,7 +133,7 @@ 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->task_size <= SLICE_LOW_TOP)
+	if (mm->context.addr_limit <= SLICE_LOW_TOP)
 		return;
 
 	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++)
@@ -446,19 +446,20 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 
 	/* Sanity checks */
 	BUG_ON(mm->task_size == 0);
+	BUG_ON(mm->context.addr_limit == 0);
 	VM_BUG_ON(radix_enabled());
 
 	slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
 	slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
 		  addr, len, flags, topdown);
 
-	if (len > mm->task_size)
+	if (len > mm->context.addr_limit)
 		return -ENOMEM;
 	if (len & ((1ul << pshift) - 1))
 		return -EINVAL;
 	if (fixed && (addr & ((1ul << pshift) - 1)))
 		return -EINVAL;
-	if (fixed && addr > (mm->task_size - len))
+	if (fixed && addr > (mm->context.addr_limit - len))
 		return -ENOMEM;
 
 	/* If hint, make sure it matches our alignment restrictions */
@@ -466,7 +467,7 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 		addr = _ALIGN_UP(addr, 1ul << pshift);
 		slice_dbg(" aligned addr=%lx\n", addr);
 		/* Ignore hint if it's too large or overlaps a VMA */
-		if (addr > mm->task_size - len ||
+		if (addr > mm->context.addr_limit - len ||
 		    !slice_area_is_free(mm, addr, len))
 			addr = 0;
 	}
Signature · 1 line
2.15.0
author reviewer
#3 lore raw

Re: [PATCH 1/5] powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case allocation

Quoted 15 lines · On Mon, 06 Nov 2017 16:08:06 +0530 "Aneesh Kumar K.V"…
Quoted 5 lines · Nicholas Piggin

When allocating VA space with a hint that crosses 128TB, the SLB addr_limit variable is not expanded if addr is not > 128TB, but the slice allocation looks at task_size, which is 512TB. This results in slice_check_fit() incorrectly succeeding because the slice_count truncates off bit 128 of the requested mask, so the comparison to the available mask succeeds.

But then the mask passed to slice_check_fit() is generated using context.addr_limit as max value. So how did that return succcess? ie, we get the request mask via

slice_range_to_mask(addr, len, &mask);

Quoted 7 lines

And the potential/possible mask using

slice_mask_for_size(mm, psize, &good_mask);

So how did slice_check_fit() return sucess with

slice_check_fit(mm, mask, good_mask);

Because the addr_limit check is used to *limit* the comparison.

The available mask had bit up to 127 set, and the mask had 127 and 128 set. However the 128T addr_limit causes only bits 0-127 to be compared.

Quoted 19 lines
Quoted 19 lines

Fix this by using mm->context.addr_limit instead of mm->task_size for testing allocation limits. This causes such allocations to fail.

Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/slice.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/powerpc/mm/slice.c +0 -0
index 45f6740dd407..567db541c0a1 100644
@@ -96,7 +96,7 @@ static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
 {
 	struct vm_area_struct *vma;
 
Quoted 7 lines

arch/powerpc/mm/slice.c static int slice_area_is_free(struct mm_struct *mm, unsigned long addr, · line 99

Quoted 2 lines
arch/powerpc/mm/slice.c +1 -1
-	if ((mm->task_size - len) < addr)
+	if ((mm->context.addr_limit - len) < addr)  

I was looking at these as generic boundary check against task size and for specific range check we should have created mask always using context.addr_limit. That should keep the boundary condition check same across radix/hash.

We need to actually fix the radix case too for other-but-similar reasons, so fixing it like this does end up with the same tests for both. See the later radix patch.

Thanks,
Nick

#4 lore raw

Re: [PATCH 1/5] powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case allocation

Quoted 24 lines · Nicholas Piggin
Quoted 21 lines · On Mon, 06 Nov 2017 16:08:06 +0530 "Aneesh Kumar K.V"…
Quoted 5 lines · Nicholas Piggin

When allocating VA space with a hint that crosses 128TB, the SLB addr_limit variable is not expanded if addr is not > 128TB, but the slice allocation looks at task_size, which is 512TB. This results in slice_check_fit() incorrectly succeeding because the slice_count truncates off bit 128 of the requested mask, so the comparison to the available mask succeeds.

But then the mask passed to slice_check_fit() is generated using context.addr_limit as max value. So how did that return succcess? ie, we get the request mask via

slice_range_to_mask(addr, len, &mask);

And the potential/possible mask using

slice_mask_for_size(mm, psize, &good_mask);

So how did slice_check_fit() return sucess with

Quoted 8 lines
Quoted 1 line

slice_check_fit(mm, mask, good_mask);

Because the addr_limit check is used to *limit* the comparison.

The available mask had bit up to 127 set, and the mask had 127 and 128 set. However the 128T addr_limit causes only bits 0-127 to be compared.

Should we fix it then via ? I haven't tested this yet. Also this result in us comparing more bits?

modified arch/powerpc/mm/slice.c
@@ -169,13 +169,12 @@ static int slice_check_fit(struct mm_struct *mm,
struct slice_mask mask, struct slice_mask available)
{
DECLARE_BITMAP(result, SLICE_NUM_HIGH);
- unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.addr_limit);

bitmap_and(result, mask.high_slices,
- available.high_slices, slice_count);
+ available.high_slices, SLICE_NUM_HIGH);

return (mask.low_slices & available.low_slices) == mask.low_slices &&
- bitmap_equal(result, mask.high_slices, slice_count);
+ bitmap_equal(result, mask.high_slices, SLICE_NUM_HIGH)

-aneesh

author reviewer
#5 lore raw

Re: [PATCH 1/5] powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case allocation

Quoted 29 lines · On Mon, 6 Nov 2017 16:35:43 +0530 "Aneesh Kumar K.V"…
Quoted 28 lines · Nicholas Piggin
Quoted 15 lines · On Mon, 06 Nov 2017 16:08:06 +0530 "Aneesh Kumar K.V"…
Quoted 5 lines · Nicholas Piggin

When allocating VA space with a hint that crosses 128TB, the SLB addr_limit variable is not expanded if addr is not > 128TB, but the slice allocation looks at task_size, which is 512TB. This results in slice_check_fit() incorrectly succeeding because the slice_count truncates off bit 128 of the requested mask, so the comparison to the available mask succeeds.

But then the mask passed to slice_check_fit() is generated using context.addr_limit as max value. So how did that return succcess? ie, we get the request mask via

slice_range_to_mask(addr, len, &mask);

Quoted 7 lines

And the potential/possible mask using

slice_mask_for_size(mm, psize, &good_mask);

So how did slice_check_fit() return sucess with

slice_check_fit(mm, mask, good_mask);

Because the addr_limit check is used to *limit* the comparison.

Quoted 7 lines
Quoted 4 lines

The available mask had bit up to 127 set, and the mask had 127 and 128 set. However the 128T addr_limit causes only bits 0-127 to be compared.

Should we fix it then via ? I haven't tested this yet. Also this result in us comparing more bits?

I prefer not to rely on that as the fix because we should not be calling into the slice code with an address beyond addr_limit IMO. There's quite a few other places that use addr_limit. So I prefer my patch.

You could add this as an extra check, but yes it does result in more bitmap to test. So if anything I would prefer to go the other way and actually reduce the scope of *other* bitmap operations that are now using SLICE_NUM_HIGH by similarly using addr_limit (if there are other performance critical ones).

We could add some VM_BUG_ON checks to ensure tail bits are zero if that's a concern.

Quoted 19 lines

modified arch/powerpc/mm/slice.c

(quoted diff) +2 -4
@@ -169,13 +169,12 @@ static int slice_check_fit(struct mm_struct *mm,
  			   struct slice_mask mask, struct slice_mask available)
  {
  	DECLARE_BITMAP(result, SLICE_NUM_HIGH);
-	unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.addr_limit);
 
  	bitmap_and(result, mask.high_slices,
-		   available.high_slices, slice_count);
+		   available.high_slices, SLICE_NUM_HIGH);
 
  	return (mask.low_slices & available.low_slices) == mask.low_slices &&
-		bitmap_equal(result, mask.high_slices, slice_count);
+		bitmap_equal(result, mask.high_slices, SLICE_NUM_HIGH)
 
 
-aneesh
#6 lore raw

Re: [PATCH 1/5] powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case allocation

In reply to Aneesh Kumar K.V
Quoted 45 lines · Aneesh Kumar K.V
Quoted 24 lines · Nicholas Piggin
Quoted 21 lines · On Mon, 06 Nov 2017 16:08:06 +0530 "Aneesh Kumar K.V"…
Quoted 5 lines · Nicholas Piggin

When allocating VA space with a hint that crosses 128TB, the SLB addr_limit variable is not expanded if addr is not > 128TB, but the slice allocation looks at task_size, which is 512TB. This results in slice_check_fit() incorrectly succeeding because the slice_count truncates off bit 128 of the requested mask, so the comparison to the available mask succeeds.

But then the mask passed to slice_check_fit() is generated using context.addr_limit as max value. So how did that return succcess? ie, we get the request mask via

slice_range_to_mask(addr, len, &mask);

And the potential/possible mask using

slice_mask_for_size(mm, psize, &good_mask);

So how did slice_check_fit() return sucess with

Quoted 8 lines
Quoted 1 line

slice_check_fit(mm, mask, good_mask);

Because the addr_limit check is used to *limit* the comparison.

The available mask had bit up to 127 set, and the mask had 127 and 128 set. However the 128T addr_limit causes only bits 0-127 to be compared.

Should we fix it then via ? I haven't tested this yet. Also this result in us comparing more bits?

modified   arch/powerpc/mm/slice.c

(quoted diff) +0 -1
@@ -169,13 +169,12 @@ static int slice_check_fit(struct mm_struct *mm,
                 struct slice_mask mask, struct slice_mask available)
  {
      DECLARE_BITMAP(result, SLICE_NUM_HIGH);
-    unsigned long slice_count = 
 GET_HIGH_SLICE_INDEX(mm->context.addr_limit);

Review static int slice_check_fit(struct mm_struct *mm, · line 180

Quoted 10 lines
(quoted diff) +2 -2
 
      bitmap_and(result, mask.high_slices,
-           available.high_slices, slice_count);
+           available.high_slices, SLICE_NUM_HIGH);
 
      return (mask.low_slices & available.low_slices) == mask.low_slices &&
-        bitmap_equal(result, mask.high_slices, slice_count);
+        bitmap_equal(result, mask.high_slices, SLICE_NUM_HIGH)
 
 

Florian, will you be able to test this patch ? We may not really want to push this. But it will confirm that we end up getting >128TB address because of this.

-aneesh

author reviewer
#7 lore raw

Re: [PATCH 1/5] powerpc/64s/hash: Fix 128TB-512TB virtual address boundary case allocation

Quoted 55 lines · On Tue, 7 Nov 2017 07:30:51 +0530 "Aneesh Kumar K.V"…
Quoted 54 lines · Aneesh Kumar K.V
Quoted 27 lines · Nicholas Piggin
Quoted 24 lines · On Mon, 06 Nov 2017 16:08:06 +0530 "Aneesh Kumar K.V"…
Quoted 8 lines · Nicholas Piggin

When allocating VA space with a hint that crosses 128TB, the SLB addr_limit
variable is not expanded if addr is not > 128TB, but the slice allocation
looks at task_size, which is 512TB. This results in slice_check_fit() incorrectly succeeding because the slice_count truncates off bit 128 of the
requested mask, so the comparison to the available mask succeeds.

But then the mask passed to slice_check_fit() is generated using context.addr_limit as max value. So how did that return succcess? ie, we get the request mask via

slice_range_to_mask(addr, len, &mask);

And the potential/possible mask using

slice_mask_for_size(mm, psize, &good_mask);

So how did slice_check_fit() return sucess with

Quoted 8 lines
Quoted 1 line

slice_check_fit(mm, mask, good_mask);

Because the addr_limit check is used to *limit* the comparison.

The available mask had bit up to 127 set, and the mask had 127 and 128 set. However the 128T addr_limit causes only bits 0-127 to be compared.

Should we fix it then via ? I haven't tested this yet. Also this result in us comparing more bits?

modified   arch/powerpc/mm/slice.c

(quoted diff) +1 -2
@@ -169,13 +169,12 @@ static int slice_check_fit(struct mm_struct *mm,
                 struct slice_mask mask, struct slice_mask available)
  {
      DECLARE_BITMAP(result, SLICE_NUM_HIGH);
-    unsigned long slice_count = 
 GET_HIGH_SLICE_INDEX(mm->context.addr_limit);
 
      bitmap_and(result, mask.high_slices,
-           available.high_slices, slice_count);
+           available.high_slices, SLICE_NUM_HIGH);
 
Quoted 9 lines

Review static int slice_check_fit(struct mm_struct *mm, · line 180

Quoted 5 lines
(quoted diff) +1 -1
      return (mask.low_slices & available.low_slices) == mask.low_slices &&
-        bitmap_equal(result, mask.high_slices, slice_count);
+        bitmap_equal(result, mask.high_slices, SLICE_NUM_HIGH)
 
 

Florian, will you be able to test this patch ? We may not really want to push this. But it will confirm that we end up getting >128TB address because of this.

Oh we are, I went through and traced it, and this is the reason hash's get_unmapped_area gives out > 128TB addresses if addr + len crosses 128TB.

Thanks,
Nick

author reviewer
#8 lore raw

PATCH 2/5 powerpc/64s/hash: Allow MAP_FIXED allocations to cross 128TB boundary

In reply to Nicholas Piggin

While mapping hints with a length that cross 128TB are disallowed, MAP_FIXED allocations that cross 128TB are allowed. These are failing on hash (on radix they succeed). Add an additional case for fixed mappings to expand the addr_limit when crossing 128TB.

Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/slice.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/mm/slice.c +2 -1
index 567db541c0a1..f980397b449d 100644
@@ -419,7 +419,8 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 	/*
 	 * Check if we need to expland slice area.
 	 */
-	if (unlikely(addr > mm->context.addr_limit &&
+	if (unlikely(((addr > mm->context.addr_limit) ||
+			(fixed && addr + len > mm->context.addr_limit)) &&
 		     mm->context.addr_limit != TASK_SIZE)) {
 		mm->context.addr_limit = TASK_SIZE;
 		on_each_cpu(slice_flush_segments, mm, 1);
Signature · 1 line
2.15.0
#9 lore raw

Re: [PATCH 2/5] powerpc/64s/hash: Allow MAP_FIXED allocations to cross 128TB boundary

Quoted 4 lines · Nicholas Piggin

While mapping hints with a length that cross 128TB are disallowed, MAP_FIXED allocations that cross 128TB are allowed. These are failing on hash (on radix they succeed). Add an additional case for fixed mappings to expand the addr_limit when crossing 128TB.

Shouldn't that be fixed in radix. But i see x86 also doing this?

	if (flags & MAP_FIXED)
		return addr;

Kiril,

Is that expected?

Quoted 24 lines
Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/slice.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/powerpc/mm/slice.c +2 -1
index 567db541c0a1..f980397b449d 100644
@@ -419,7 +419,8 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 	/*
 	 * Check if we need to expland slice area.
 	 */
-	if (unlikely(addr > mm->context.addr_limit &&
+	if (unlikely(((addr > mm->context.addr_limit) ||
+			(fixed && addr + len > mm->context.addr_limit)) &&
 		     mm->context.addr_limit != TASK_SIZE)) {
 		mm->context.addr_limit = TASK_SIZE;
 		on_each_cpu(slice_flush_segments, mm, 1);
Signature · 1 line
2.15.0

-aneesh

author reviewer
#10 lore raw

Re: [PATCH 2/5] powerpc/64s/hash: Allow MAP_FIXED allocations to cross 128TB boundary

Quoted 10 lines · On Mon, 06 Nov 2017 16:14:10 +0530 "Aneesh Kumar K.V"…
Quoted 4 lines · Nicholas Piggin

While mapping hints with a length that cross 128TB are disallowed, MAP_FIXED allocations that cross 128TB are allowed. These are failing on hash (on radix they succeed). Add an additional case for fixed mappings to expand the addr_limit when crossing 128TB.

Shouldn't that be fixed in radix. But i see x86 also doing this?

Quoted 6 lines
	if (flags & MAP_FIXED)
		return addr;

Kiril,

Is that expected?

I should actually reply to this one because the other did not have Kirill on cc.

Generic mapping code appears it will always succeed when given an explicit hint request, even if the address is below the boundary and address + length is above it. Even when !MAP_FIXED. This is the sane behaviour AFAIKS. So we should switch powerpc to match, shouldn't we?

Thanks,
Nick

#11 lore raw

Re: [PATCH 2/5] powerpc/64s/hash: Allow MAP_FIXED allocations to cross 128TB boundary

Quoted 20 lines · Nicholas Piggin
Quoted 10 lines · On Mon, 06 Nov 2017 16:14:10 +0530 "Aneesh Kumar K.V"…
Quoted 4 lines · Nicholas Piggin

While mapping hints with a length that cross 128TB are disallowed, MAP_FIXED allocations that cross 128TB are allowed. These are failing on hash (on radix they succeed). Add an additional case for fixed mappings to expand the addr_limit when crossing 128TB.

Shouldn't that be fixed in radix. But i see x86 also doing this?

Quoted 6 lines
	if (flags & MAP_FIXED)
		return addr;

Kiril,

Is that expected?

Quoted 7 lines

I should actually reply to this one because the other did not have Kirill on cc.

Generic mapping code appears it will always succeed when given an explicit hint request, even if the address is below the boundary and address + length is above it. Even when !MAP_FIXED. This is the sane behaviour AFAIKS.

It's "sane" if you want the 128T boundary to be invisible.

But the whole goddamn point was that we wanted apps to have to opt-in to using > 128T, and having brk accidentally go over the 128T boundary does not count as opting in.

So it's a mess as usual.

Quoted 1 line

So we should switch powerpc to match, shouldn't we?

Yes we should do whatever the other arches do.

Actually we should do what the man page describes .. except it doesn't.

cheers

author reviewer
#12 lore raw

Re: [PATCH 2/5] powerpc/64s/hash: Allow MAP_FIXED allocations to cross 128TB boundary

Quoted 25 lines · On Tue, 07 Nov 2017 13:28:08 +1100 Michael Ellerman <[email protected]> wrote:
Quoted 23 lines · Nicholas Piggin
Quoted 10 lines · On Mon, 06 Nov 2017 16:14:10 +0530 "Aneesh Kumar K.V"…
Quoted 4 lines · Nicholas Piggin

While mapping hints with a length that cross 128TB are disallowed, MAP_FIXED allocations that cross 128TB are allowed. These are failing on hash (on radix they succeed). Add an additional case for fixed mappings to expand the addr_limit when crossing 128TB.

Shouldn't that be fixed in radix. But i see x86 also doing this?

Quoted 6 lines
	if (flags & MAP_FIXED)
		return addr;

Kiril,

Is that expected?

I should actually reply to this one because the other did not have Kirill on cc.

Quoted 6 lines
Quoted 4 lines

Generic mapping code appears it will always succeed when given an explicit hint request, even if the address is below the boundary and address + length is above it. Even when !MAP_FIXED. This is the sane behaviour AFAIKS.

It's "sane" if you want the 128T boundary to be invisible.

Not invisible: mmap(NULL, length, ...) + length < 128TB

Quoted 3 lines

But the whole goddamn point was that we wanted apps to have to opt-in to using > 128T, and having brk accidentally go over the 128T boundary does not count as opting in.

If brk() is given an explicit address > 128TB, then that's opting in, right?

If malloc is doing some allocation scheme which "opts in", such as using explicit brk or mmap, then there is no way for the kernel to solve that. Making increasingly convoluted policy to try to allow the good and reject the bad is not right IMO.

Quoted 1 line

So it's a mess as usual.

If we wanted to make this ABI-identical with existing systems, then it can't work without out-of-band opt-in. Why wasn't a personality
+ sysctl used for this, as we apparently have for a previous address space layout change?

Quoted 6 lines
Quoted 2 lines

So we should switch powerpc to match, shouldn't we?

Yes we should do whatever the other arches do.

Actually we should do what the man page describes .. except it doesn't.

author reviewer
#13 lore raw

PATCH 3/5 powerpc/64s/hash: Fix fork() with 512TB process address space

In reply to Nicholas Piggin

Hash unconditionally resets the addr_limit to default (128TB) when the mm context is initialised. If a process has > 128TB mappings when it forks, the child will not get the 512TB addr_limit, so accesses to valid > 128TB mappings will fail in the child.

Fix this by only resetting the addr_limit to default if it was 0. Non zero indicates it was duplicated from the parent (0 means exec()).

Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/mmu_context_book3s64.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/mm/mmu_context_book3s64.c +4 -4
index 05e15386d4cb..b94fb62e60fd 100644
@@ -93,11 +93,11 @@ static int hash__init_new_context(struct mm_struct *mm)
 		return index;
 
 	/*
-	 * We do switch_slb() early in fork, even before we setup the
-	 * mm->context.addr_limit. Default to max task size so that we copy the
-	 * default values to paca which will help us to handle slb miss early.
+	 * In the case of exec, use the default limit,
+	 * otherwise inherit it from the mm we are duplicating.
 	 */
-	mm->context.addr_limit = DEFAULT_MAP_WINDOW_USER64;
+	if (!mm->context.addr_limit)
+		mm->context.addr_limit = DEFAULT_MAP_WINDOW_USER64;
 
 	/*
 	 * The old code would re-promote on fork, we don't do that when using
Signature · 1 line
2.15.0
Reviewed-by on v1 3/5
#14 lore raw

Re: [PATCH 3/5] powerpc/64s/hash: Fix fork() with 512TB process address space

Quoted 8 lines · Nicholas Piggin

Hash unconditionally resets the addr_limit to default (128TB) when the mm context is initialised. If a process has > 128TB mappings when it forks, the child will not get the 512TB addr_limit, so accesses to valid > 128TB mappings will fail in the child.

Fix this by only resetting the addr_limit to default if it was 0. Non zero indicates it was duplicated from the parent (0 means exec()).

Quoted 29 lines
Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/mmu_context_book3s64.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/powerpc/mm/mmu_context_book3s64.c +4 -4
index 05e15386d4cb..b94fb62e60fd 100644
@@ -93,11 +93,11 @@ static int hash__init_new_context(struct mm_struct *mm)
 		return index;
 
 	/*
-	 * We do switch_slb() early in fork, even before we setup the
-	 * mm->context.addr_limit. Default to max task size so that we copy the
-	 * default values to paca which will help us to handle slb miss early.
+	 * In the case of exec, use the default limit,
+	 * otherwise inherit it from the mm we are duplicating.
 	 */
-	mm->context.addr_limit = DEFAULT_MAP_WINDOW_USER64;
+	if (!mm->context.addr_limit)
+		mm->context.addr_limit = DEFAULT_MAP_WINDOW_USER64;
 
 	/*
 	 * The old code would re-promote on fork, we don't do that when using
Signature · 1 line
2.15.0
author reviewer
#15 lore raw

PATCH 4/5 powerpc/64s/radix: Fix 128TB-512TB virtual address boundary case allocation

In reply to Nicholas Piggin

Radix VA space allocations test addresses against mm->task_size which is 512TB, even in cases where the intention is to limit allocation to below 128TB.

This results in mmap with a hint address below 128TB but address + length above 128TB succeeding when it should fail (as hash does after the previous patch).

Set the high address limit to be considered up front, and base subsequent allocation checks on that consistently.

Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/hugetlbpage-radix.c | 13 +++++++------
 arch/powerpc/mm/mmap.c              | 27 ++++++++++++++-------------
 2 files changed, 21 insertions(+), 19 deletions(-)
arch/powerpc/mm/hugetlbpage-radix.c +7 -6
index a12e86395025..9c6a411e9c85 100644
@@ -48,14 +48,18 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	struct hstate *h = hstate_file(file);
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit && addr < TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
 	if (len & ~huge_page_mask(h))
 		return -EINVAL;
-	if (len > mm->task_size)
+	if (len > high_limit)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED) {
@@ -67,7 +71,7 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	if (addr) {
 		addr = ALIGN(addr, huge_page_size(h));
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr &&
+		if (high_limit - len >= addr &&
 		    (!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -78,12 +82,9 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
 	info.length = len;
 	info.low_limit = PAGE_SIZE;
-	info.high_limit = current->mm->mmap_base;
+	info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
 	info.align_mask = PAGE_MASK & ~huge_page_mask(h);
 	info.align_offset = 0;
 
-	if (addr > DEFAULT_MAP_WINDOW)
-		info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
-
 	return vm_unmapped_area(&info);
 }
arch/powerpc/mm/mmap.c +14 -13
index 5d78b193fec4..e6cb3b3f7e93 100644
@@ -106,13 +106,17 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit &&
 		     mm->context.addr_limit != TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
-	if (len > mm->task_size - mmap_min_addr)
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
+	if (len > high_limit - mmap_min_addr)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED)
@@ -121,7 +125,7 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	if (addr) {
 		addr = PAGE_ALIGN(addr);
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
+		if (high_limit - len >= addr && addr >= mmap_min_addr &&
 		    (!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -129,13 +133,9 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	info.flags = 0;
 	info.length = len;
 	info.low_limit = mm->mmap_base;
+	info.high_limit = high_limit;
 	info.align_mask = 0;
 
-	if (unlikely(addr > DEFAULT_MAP_WINDOW))
-		info.high_limit = mm->context.addr_limit;
-	else
-		info.high_limit = DEFAULT_MAP_WINDOW;
-
 	return vm_unmapped_area(&info);
 }
 
@@ -149,14 +149,18 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	struct vm_area_struct *vma;
 	struct mm_struct *mm = current->mm;
 	unsigned long addr = addr0;
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit &&
 		     mm->context.addr_limit != TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
 	/* requested length too big for entire address space */
-	if (len > mm->task_size - mmap_min_addr)
+	if (len > high_limit - mmap_min_addr)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED)
@@ -166,7 +170,7 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	if (addr) {
 		addr = PAGE_ALIGN(addr);
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
+		if (high_limit - len >= addr && addr >= mmap_min_addr &&
 				(!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -174,12 +178,9 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
 	info.length = len;
 	info.low_limit = max(PAGE_SIZE, mmap_min_addr);
-	info.high_limit = mm->mmap_base;
+	info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
 	info.align_mask = 0;
 
-	if (addr > DEFAULT_MAP_WINDOW)
-		info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
-
 	addr = vm_unmapped_area(&info);
 	if (!(addr & ~PAGE_MASK))
 		return addr;
Signature · 1 line
2.15.0
#16 lore raw

Re: [PATCH 4/5] powerpc/64s/radix: Fix 128TB-512TB virtual address boundary case allocation

Quoted 10 lines · Nicholas Piggin

Radix VA space allocations test addresses against mm->task_size which is 512TB, even in cases where the intention is to limit allocation to below 128TB.

This results in mmap with a hint address below 128TB but address + length above 128TB succeeding when it should fail (as hash does after the previous patch).

Set the high address limit to be considered up front, and base subsequent allocation checks on that consistently.

Doesn't setting info.high_limit take care of that ? I would expect vm_unmapped_area to fail based on info.high_limit. Is this with MAP_FIXED?

Quoted 148 lines
Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/hugetlbpage-radix.c | 13 +++++++------
 arch/powerpc/mm/mmap.c              | 27 ++++++++++++++-------------
 2 files changed, 21 insertions(+), 19 deletions(-)
arch/powerpc/mm/hugetlbpage-radix.c +7 -6
index a12e86395025..9c6a411e9c85 100644
@@ -48,14 +48,18 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	struct hstate *h = hstate_file(file);
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit && addr < TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
 	if (len & ~huge_page_mask(h))
 		return -EINVAL;
-	if (len > mm->task_size)
+	if (len > high_limit)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED) {
@@ -67,7 +71,7 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	if (addr) {
 		addr = ALIGN(addr, huge_page_size(h));
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr &&
+		if (high_limit - len >= addr &&
 		    (!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -78,12 +82,9 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
 	info.length = len;
 	info.low_limit = PAGE_SIZE;
-	info.high_limit = current->mm->mmap_base;
+	info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
 	info.align_mask = PAGE_MASK & ~huge_page_mask(h);
 	info.align_offset = 0;
 
-	if (addr > DEFAULT_MAP_WINDOW)
-		info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
-
 	return vm_unmapped_area(&info);
 }
arch/powerpc/mm/mmap.c +14 -13
index 5d78b193fec4..e6cb3b3f7e93 100644
@@ -106,13 +106,17 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit &&
 		     mm->context.addr_limit != TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
-	if (len > mm->task_size - mmap_min_addr)
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
+	if (len > high_limit - mmap_min_addr)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED)
@@ -121,7 +125,7 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	if (addr) {
 		addr = PAGE_ALIGN(addr);
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
+		if (high_limit - len >= addr && addr >= mmap_min_addr &&
 		    (!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -129,13 +133,9 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	info.flags = 0;
 	info.length = len;
 	info.low_limit = mm->mmap_base;
+	info.high_limit = high_limit;
 	info.align_mask = 0;
 
-	if (unlikely(addr > DEFAULT_MAP_WINDOW))
-		info.high_limit = mm->context.addr_limit;
-	else
-		info.high_limit = DEFAULT_MAP_WINDOW;
-
 	return vm_unmapped_area(&info);
 }
 
@@ -149,14 +149,18 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	struct vm_area_struct *vma;
 	struct mm_struct *mm = current->mm;
 	unsigned long addr = addr0;
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit &&
 		     mm->context.addr_limit != TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
 	/* requested length too big for entire address space */
-	if (len > mm->task_size - mmap_min_addr)
+	if (len > high_limit - mmap_min_addr)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED)
@@ -166,7 +170,7 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	if (addr) {
 		addr = PAGE_ALIGN(addr);
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
+		if (high_limit - len >= addr && addr >= mmap_min_addr &&
 				(!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -174,12 +178,9 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
 	info.length = len;
 	info.low_limit = max(PAGE_SIZE, mmap_min_addr);
-	info.high_limit = mm->mmap_base;
+	info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
 	info.align_mask = 0;
 
-	if (addr > DEFAULT_MAP_WINDOW)
-		info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
-
 	addr = vm_unmapped_area(&info);
 	if (!(addr & ~PAGE_MASK))
 		return addr;
Signature · 1 line
2.15.0
author reviewer
#17 lore raw

Re: [PATCH 4/5] powerpc/64s/radix: Fix 128TB-512TB virtual address boundary case allocation

Quoted 10 lines · On Mon, 06 Nov 2017 16:44:48 +0530 "Aneesh Kumar K.V"…
Quoted 8 lines · Nicholas Piggin

Radix VA space allocations test addresses against mm->task_size which is 512TB, even in cases where the intention is to limit allocation to below 128TB.

This results in mmap with a hint address below 128TB but address + length above 128TB succeeding when it should fail (as hash does after the previous patch).

Quoted 5 lines
Quoted 2 lines

Set the high address limit to be considered up front, and base subsequent allocation checks on that consistently.

Doesn't setting info.high_limit take care of that ? I would expect vm_unmapped_area to fail based on info.high_limit.

No, it is the hint address case. info.high_limit only gets involved if the hint area was unavailable.

I prefer the behaviour without this fix because I disagree that the explicit address request should fail, but this is what you asked for.

Actually now I come to look again, it seems that generic code does *not* fail in this case either! Any explicit hint will succeed if it partially or completely crosses 128TB. This is much better behaviour, so I think powerpc has it wrong.

Quoted 1 line

Is this with MAP_FIXED?

With MAP_FIXED, it remains as succeeding as expected (like generic code and hash). I did not change that case.

Quoted 151 lines
Quoted 150 lines
Cc "Aneesh Kumar K.V" <[email protected]>
Fixes f4ea6dcb08 ("powerpc/mm: Enable mappings above 128TB")
---
 arch/powerpc/mm/hugetlbpage-radix.c | 13 +++++++------
 arch/powerpc/mm/mmap.c              | 27 ++++++++++++++-------------
 2 files changed, 21 insertions(+), 19 deletions(-)
arch/powerpc/mm/hugetlbpage-radix.c +7 -6
index a12e86395025..9c6a411e9c85 100644
@@ -48,14 +48,18 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	struct hstate *h = hstate_file(file);
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit && addr < TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
 	if (len & ~huge_page_mask(h))
 		return -EINVAL;
-	if (len > mm->task_size)
+	if (len > high_limit)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED) {
@@ -67,7 +71,7 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	if (addr) {
 		addr = ALIGN(addr, huge_page_size(h));
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr &&
+		if (high_limit - len >= addr &&
 		    (!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -78,12 +82,9 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
 	info.length = len;
 	info.low_limit = PAGE_SIZE;
-	info.high_limit = current->mm->mmap_base;
+	info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
 	info.align_mask = PAGE_MASK & ~huge_page_mask(h);
 	info.align_offset = 0;
 
-	if (addr > DEFAULT_MAP_WINDOW)
-		info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
-
 	return vm_unmapped_area(&info);
 }
arch/powerpc/mm/mmap.c +14 -13
index 5d78b193fec4..e6cb3b3f7e93 100644
@@ -106,13 +106,17 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 {
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit &&
 		     mm->context.addr_limit != TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
-	if (len > mm->task_size - mmap_min_addr)
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
+	if (len > high_limit - mmap_min_addr)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED)
@@ -121,7 +125,7 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	if (addr) {
 		addr = PAGE_ALIGN(addr);
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
+		if (high_limit - len >= addr && addr >= mmap_min_addr &&
 		    (!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -129,13 +133,9 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	info.flags = 0;
 	info.length = len;
 	info.low_limit = mm->mmap_base;
+	info.high_limit = high_limit;
 	info.align_mask = 0;
 
-	if (unlikely(addr > DEFAULT_MAP_WINDOW))
-		info.high_limit = mm->context.addr_limit;
-	else
-		info.high_limit = DEFAULT_MAP_WINDOW;
-
 	return vm_unmapped_area(&info);
 }
 
@@ -149,14 +149,18 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	struct vm_area_struct *vma;
 	struct mm_struct *mm = current->mm;
 	unsigned long addr = addr0;
+	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
 	if (unlikely(addr > mm->context.addr_limit &&
 		     mm->context.addr_limit != TASK_SIZE))
 		mm->context.addr_limit = TASK_SIZE;
 
+	if (addr > high_limit)
+		high_limit = TASK_SIZE;
+
 	/* requested length too big for entire address space */
-	if (len > mm->task_size - mmap_min_addr)
+	if (len > high_limit - mmap_min_addr)
 		return -ENOMEM;
 
 	if (flags & MAP_FIXED)
@@ -166,7 +170,7 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	if (addr) {
 		addr = PAGE_ALIGN(addr);
 		vma = find_vma(mm, addr);
-		if (mm->task_size - len >= addr && addr >= mmap_min_addr &&
+		if (high_limit - len >= addr && addr >= mmap_min_addr &&
 				(!vma || addr + len <= vm_start_gap(vma)))
 			return addr;
 	}
@@ -174,12 +178,9 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
 	info.length = len;
 	info.low_limit = max(PAGE_SIZE, mmap_min_addr);
-	info.high_limit = mm->mmap_base;
+	info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
 	info.align_mask = 0;
 
-	if (addr > DEFAULT_MAP_WINDOW)
-		info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
-
 	addr = vm_unmapped_area(&info);
 	if (!(addr & ~PAGE_MASK))
 		return addr;
Signature · 1 line
2.15.0  
author reviewer
#18 lore raw

PATCH 5/5 powerpc/64s: mm_context.addr_limit is only used on hash

In reply to Nicholas Piggin

Radix keeps no meaningful state in addr_limit, so remove it from radix code and rename to slb_addr_limit to make it clear it applies to hash only.

---
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |  2 +-
 arch/powerpc/include/asm/book3s/64/mmu.h      |  2 +-
 arch/powerpc/include/asm/paca.h               |  2 +-
 arch/powerpc/kernel/asm-offsets.c             |  2 +-
 arch/powerpc/kernel/paca.c                    |  4 ++--
 arch/powerpc/kernel/setup-common.c            |  3 ++-
 arch/powerpc/mm/hugetlbpage-radix.c           |  3 ---
 arch/powerpc/mm/mmap.c                        |  8 -------
 arch/powerpc/mm/mmu_context_book3s64.c        |  4 ++--
 arch/powerpc/mm/slb_low.S                     |  2 +-
 arch/powerpc/mm/slice.c                       | 34 +++++++++++++--------------
 11 files changed, 28 insertions(+), 38 deletions(-)
arch/powerpc/include/asm/book3s/64/mmu-hash.h +1 -1
index 508275bb05d5..e91e115a816f 100644
@@ -606,7 +606,7 @@ extern void slb_set_size(u16 size);
 
 /* 4 bits per slice and we have one slice per 1TB */
 #define SLICE_ARRAY_SIZE	(H_PGTABLE_RANGE >> 41)
-#define TASK_SLICE_ARRAY_SZ(x)	((x)->context.addr_limit >> 41)
+#define TASK_SLICE_ARRAY_SZ(x)	((x)->context.slb_addr_limit >> 41)
 
 #ifndef __ASSEMBLY__
 
arch/powerpc/include/asm/book3s/64/mmu.h +1 -1
index c3b00e8ff791..49a07c5d9e50 100644
@@ -92,7 +92,7 @@ typedef struct {
 #ifdef CONFIG_PPC_MM_SLICES
 	u64 low_slices_psize;	/* SLB page size encodings */
 	unsigned char high_slices_psize[SLICE_ARRAY_SIZE];
-	unsigned long addr_limit;
+	unsigned long slb_addr_limit;
 #else
 	u16 sllp;		/* SLB page size encoding */
 #endif
arch/powerpc/include/asm/paca.h +1 -1
index 7125efa6a6ae..2ef0c0da4bb7 100644
@@ -143,7 +143,7 @@ struct paca_struct {
 #ifdef CONFIG_PPC_MM_SLICES
 	u64 mm_ctx_low_slices_psize;
 	unsigned char mm_ctx_high_slices_psize[SLICE_ARRAY_SIZE];
-	unsigned long addr_limit;
+	unsigned long mm_ctx_slb_addr_limit;
 #else
 	u16 mm_ctx_user_psize;
 	u16 mm_ctx_sllp;
arch/powerpc/kernel/asm-offsets.c +1 -1
index 96c52235ecdc..912880873dfc 100644
@@ -185,7 +185,7 @@ int main(void)
 #ifdef CONFIG_PPC_MM_SLICES
 	OFFSET(PACALOWSLICESPSIZE, paca_struct, mm_ctx_low_slices_psize);
 	OFFSET(PACAHIGHSLICEPSIZE, paca_struct, mm_ctx_high_slices_psize);
-	DEFINE(PACA_ADDR_LIMIT, offsetof(struct paca_struct, addr_limit));
+	OFFSET(PACA_SLB_ADDR_LIMIT, paca_struct, mm_ctx_slb_addr_limit);
 	DEFINE(MMUPSIZEDEFSIZE, sizeof(struct mmu_psize_def));
 #endif /* CONFIG_PPC_MM_SLICES */
 #endif
arch/powerpc/kernel/paca.c +2 -2
index 2ff2b8a19f71..4c69d335863c 100644
@@ -262,8 +262,8 @@ void copy_mm_to_paca(struct mm_struct *mm)
 
 	get_paca()->mm_ctx_id = context->id;
 #ifdef CONFIG_PPC_MM_SLICES
-	VM_BUG_ON(!mm->context.addr_limit);
-	get_paca()->addr_limit = mm->context.addr_limit;
+	VM_BUG_ON(!mm->context.slb_addr_limit);
+	get_paca()->mm_ctx_slb_addr_limit = mm->context.slb_addr_limit;
 	get_paca()->mm_ctx_low_slices_psize = context->low_slices_psize;
 	memcpy(&get_paca()->mm_ctx_high_slices_psize,
 	       &context->high_slices_psize, TASK_SLICE_ARRAY_SZ(mm));
arch/powerpc/kernel/setup-common.c +2 -1
index 2e3bc16d02b2..8c4fa6086b39 100644
@@ -898,7 +898,8 @@ void __init setup_arch(char **cmdline_p)
 
 #ifdef CONFIG_PPC_MM_SLICES
 #ifdef CONFIG_PPC64
-	init_mm.context.addr_limit = DEFAULT_MAP_WINDOW_USER64;
+	if (!radix_enabled())
+		init_mm.context.slb_addr_limit = DEFAULT_MAP_WINDOW_USER64;
 #else
 #error	"context.addr_limit not initialized."
 #endif
arch/powerpc/mm/hugetlbpage-radix.c +0 -3
index 9c6a411e9c85..0f69bdf33367 100644
@@ -51,9 +51,6 @@ radix__hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
-	if (unlikely(addr > mm->context.addr_limit && addr < TASK_SIZE))
-		mm->context.addr_limit = TASK_SIZE;
-
 	if (addr > high_limit)
 		high_limit = TASK_SIZE;
 
arch/powerpc/mm/mmap.c +0 -8
index e6cb3b3f7e93..62c1191670b0 100644
@@ -109,10 +109,6 @@ radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
-	if (unlikely(addr > mm->context.addr_limit &&
-		     mm->context.addr_limit != TASK_SIZE))
-		mm->context.addr_limit = TASK_SIZE;
-
 	if (addr > high_limit)
 		high_limit = TASK_SIZE;
 
@@ -152,10 +148,6 @@ radix__arch_get_unmapped_area_topdown(struct file *filp,
 	unsigned long high_limit = DEFAULT_MAP_WINDOW;
 	struct vm_unmapped_area_info info;
 
-	if (unlikely(addr > mm->context.addr_limit &&
-		     mm->context.addr_limit != TASK_SIZE))
-		mm->context.addr_limit = TASK_SIZE;
-
 	if (addr > high_limit)
 		high_limit = TASK_SIZE;
 
arch/powerpc/mm/mmu_context_book3s64.c +2 -2
index b94fb62e60fd..995c98f3f392 100644
@@ -96,8 +96,8 @@ static int hash__init_new_context(struct mm_struct *mm)
 	 * In the case of exec, use the default limit,
 	 * otherwise inherit it from the mm we are duplicating.
 	 */
-	if (!mm->context.addr_limit)
-		mm->context.addr_limit = DEFAULT_MAP_WINDOW_USER64;
+	if (!mm->context.slb_addr_limit)
+		mm->context.slb_addr_limit = DEFAULT_MAP_WINDOW_USER64;
 
 	/*
 	 * The old code would re-promote on fork, we don't do that when using
arch/powerpc/mm/slb_low.S +1 -1
index 906a86fe457b..7046bb389704 100644
@@ -167,7 +167,7 @@ END_MMU_FTR_SECTION_IFCLR(MMU_FTR_1T_SEGMENT)
         /*
          * user space make sure we are within the allowed limit
 	 */
-	ld	r11,PACA_ADDR_LIMIT(r13)
+	ld	r11,PACA_SLB_ADDR_LIMIT(r13)
 	cmpld	r3,r11
 	bge-	8f
 
arch/powerpc/mm/slice.c +17 -17
index f980397b449d..4933d5c71d57 100644
@@ -96,7 +96,7 @@ static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
 {
 	struct vm_area_struct *vma;
 
-	if ((mm->context.addr_limit - len) < addr)
+	if ((mm->context.slb_addr_limit - len) < addr)
 		return 0;
 	vma = find_vma(mm, addr);
 	return (!vma || (addr + len) <= vm_start_gap(vma));
@@ -133,10 +133,10 @@ 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.addr_limit <= SLICE_LOW_TOP)
+	if (mm->context.slb_addr_limit <= SLICE_LOW_TOP)
 		return;
 
-	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++)
+	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++)
 		if (!slice_high_has_vma(mm, i))
 			__set_bit(i, ret->high_slices);
 }
@@ -157,7 +157,7 @@ static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_ma
 			ret->low_slices |= 1u << i;
 
 	hpsizes = mm->context.high_slices_psize;
-	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++) {
+	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++) {
 		mask_index = i & 0x1;
 		index = i >> 1;
 		if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
@@ -169,7 +169,7 @@ static int slice_check_fit(struct mm_struct *mm,
 			   struct slice_mask mask, struct slice_mask available)
 {
 	DECLARE_BITMAP(result, SLICE_NUM_HIGH);
-	unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.addr_limit);
+	unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit);
 
 	bitmap_and(result, mask.high_slices,
 		   available.high_slices, slice_count);
@@ -219,7 +219,7 @@ static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psiz
 	mm->context.low_slices_psize = lpsizes;
 
 	hpsizes = mm->context.high_slices_psize;
-	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++) {
+	for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.slb_addr_limit); i++) {
 		mask_index = i & 0x1;
 		index = i >> 1;
 		if (test_bit(i, mask.high_slices))
@@ -329,8 +329,8 @@ static unsigned long slice_find_area_topdown(struct mm_struct *mm,
 	 * Only for that request for which high_limit is above
 	 * DEFAULT_MAP_WINDOW we should apply this.
 	 */
-	if (high_limit  > DEFAULT_MAP_WINDOW)
-		addr += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
+	if (high_limit > DEFAULT_MAP_WINDOW)
+		addr += mm->context.slb_addr_limit - DEFAULT_MAP_WINDOW;
 
 	while (addr > PAGE_SIZE) {
 		info.high_limit = addr;
@@ -419,17 +419,17 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 	/*
 	 * Check if we need to expland slice area.
 	 */
-	if (unlikely(((addr > mm->context.addr_limit) ||
-			(fixed && addr + len > mm->context.addr_limit)) &&
-		     mm->context.addr_limit != TASK_SIZE)) {
-		mm->context.addr_limit = TASK_SIZE;
+	if (unlikely(((addr > mm->context.slb_addr_limit) ||
+			(fixed && addr + len > mm->context.slb_addr_limit)) &&
+		     mm->context.slb_addr_limit != TASK_SIZE)) {
+		mm->context.slb_addr_limit = TASK_SIZE;
 		on_each_cpu(slice_flush_segments, mm, 1);
 	}
 	/*
 	 * This mmap request can allocate upt to 512TB
 	 */
 	if (addr > DEFAULT_MAP_WINDOW)
-		high_limit = mm->context.addr_limit;
+		high_limit = mm->context.slb_addr_limit;
 	else
 		high_limit = DEFAULT_MAP_WINDOW;
 	/*
@@ -447,20 +447,20 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 
 	/* Sanity checks */
 	BUG_ON(mm->task_size == 0);
-	BUG_ON(mm->context.addr_limit == 0);
+	BUG_ON(mm->context.slb_addr_limit == 0);
 	VM_BUG_ON(radix_enabled());
 
 	slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
 	slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
 		  addr, len, flags, topdown);
 
-	if (len > mm->context.addr_limit)
+	if (len > mm->context.slb_addr_limit)
 		return -ENOMEM;
 	if (len & ((1ul << pshift) - 1))
 		return -EINVAL;
 	if (fixed && (addr & ((1ul << pshift) - 1)))
 		return -EINVAL;
-	if (fixed && addr > (mm->context.addr_limit - len))
+	if (fixed && addr > (mm->context.slb_addr_limit - len))
 		return -ENOMEM;
 
 	/* If hint, make sure it matches our alignment restrictions */
@@ -468,7 +468,7 @@ unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
 		addr = _ALIGN_UP(addr, 1ul << pshift);
 		slice_dbg(" aligned addr=%lx\n", addr);
 		/* Ignore hint if it's too large or overlaps a VMA */
-		if (addr > mm->context.addr_limit - len ||
+		if (addr > mm->context.slb_addr_limit - len ||
 		    !slice_area_is_free(mm, addr, len))
 			addr = 0;
 	}
Signature · 1 line
2.15.0
#19 lore raw
In reply to Nicholas Piggin
Quoted 11 lines · Nicholas Piggin

Florian found a nasty corner case with the VA allocation logic for crossing from 128TB to 512TB limit on hash, and made a really superb report of the problem -- traces, reproducer recipes, analysis, etc. which already mostly solved it.

The first patch in the series should solve Florian's particular case, the next 3 are other issues with addr_limit. The last patch is technically a cleanup but I think it's fairly important in terms of understanding the code and also enabling some BUG checks (when addr_limit == 0).

Quoted 3 lines

I have not tested these exactly on Florian's test case, but some tests of my own behave better afterwards. Hopefully he has time to re-test. Some careful review would be welcome too.

I think I have applied the five patches you posted, but I still get a brk value above 128 TiB:

# /lib64/ld64.so.1 ./a.out
initial brk value: 0x7fffde960000
probing at 0x80000001fffc

I assumed you wanted to reject those?

In either case, I recommend to tweak the VM layout, so that ld.so does not land closely to to the 128 TiB limit, so that the brk failure or returning of 48-bit addresses is avoided.

Thanks,
Florian

author reviewer
#20 lore raw
Quoted 16 lines · On Mon, 6 Nov 2017 16:16:07 +0100 Florian Weimer <[email protected]> wrote:
Quoted 11 lines · Nicholas Piggin

Florian found a nasty corner case with the VA allocation logic for crossing from 128TB to 512TB limit on hash, and made a really superb report of the problem -- traces, reproducer recipes, analysis, etc. which already mostly solved it.

The first patch in the series should solve Florian's particular case, the next 3 are other issues with addr_limit. The last patch is technically a cleanup but I think it's fairly important in terms of understanding the code and also enabling some BUG checks (when addr_limit == 0).

Quoted 3 lines

I have not tested these exactly on Florian's test case, but some tests of my own behave better afterwards. Hopefully he has time to re-test. Some careful review would be welcome too.

Quoted 8 lines

I think I have applied the five patches you posted, but I still get a brk value above 128 TiB:

# /lib64/ld64.so.1 ./a.out
initial brk value: 0x7fffde960000
probing at 0x80000001fffc

I assumed you wanted to reject those?

It was difficult to understand what the intended semantics are, but I think brk should succeed (it is implemented with MAP_FIXED). Of course it should not succeed then segfault when you try to access it.

Quoted 4 lines

In either case, I recommend to tweak the VM layout, so that ld.so does not land closely to to the 128 TiB limit, so that the brk failure or returning of 48-bit addresses is avoided.

Yeah well that's yet another issue. I was not really involved with the address space extension work. Anees, Kirill, was the intention for the 128T->512T extension logic to be a no-op for all address space allocaiton except those with explicit addresses?

Thanks,
Nick

#21 lore raw
Quoted 33 lines · Nicholas Piggin
Quoted 16 lines · On Mon, 6 Nov 2017 16:16:07 +0100 Florian Weimer <[email protected]> wrote:
Quoted 11 lines · Nicholas Piggin

Florian found a nasty corner case with the VA allocation logic for crossing from 128TB to 512TB limit on hash, and made a really superb report of the problem -- traces, reproducer recipes, analysis, etc. which already mostly solved it.

The first patch in the series should solve Florian's particular case, the next 3 are other issues with addr_limit. The last patch is technically a cleanup but I think it's fairly important in terms of understanding the code and also enabling some BUG checks (when addr_limit == 0).

Quoted 3 lines

I have not tested these exactly on Florian's test case, but some tests of my own behave better afterwards. Hopefully he has time to re-test. Some careful review would be welcome too.

Quoted 8 lines

I think I have applied the five patches you posted, but I still get a brk value above 128 TiB:

# /lib64/ld64.so.1 ./a.out
initial brk value: 0x7fffde960000
probing at 0x80000001fffc

I assumed you wanted to reject those?

It was difficult to understand what the intended semantics are, but I think brk should succeed (it is implemented with MAP_FIXED). Of course it should not succeed then segfault when you try to access it.

Quoted 1 line
Quoted 9 lines
Quoted 3 lines

In either case, I recommend to tweak the VM layout, so that ld.so does not land closely to to the 128 TiB limit, so that the brk failure or returning of 48-bit addresses is avoided.

Yeah well that's yet another issue. I was not really involved with the address space extension work. Anees, Kirill, was the intention for the 128T->512T extension logic to be a no-op for all address space allocaiton except those with explicit addresses?

yes.

-aneesh