Skip to contentSkip to conversation

x86/mm: consolidate bad_area handling to the end

PATCH In review Wei Yang 3 participants 5 messages Sep 19, 2019 – Oct 1, 2019

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/2 x86/mm: consolidate bad_area handling to the end

There are totally 7 bad_area[_nosemaphore] error branch in do_user_addr_fault().

Consolidate all these handling to the end to make the code a little neat.

BTW, after doing so, function bad_area is not used any more. Remove it.

---
 arch/x86/mm/fault.c | 44 ++++++++++++++++----------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)
arch/x86/mm/fault.c +16 -28
index 9ceacd1156db..9d18b73b5f77 100644
@@ -933,12 +933,6 @@ __bad_area(struct pt_regs *regs, unsigned long error_code,
 	__bad_area_nosemaphore(regs, error_code, address, pkey, si_code);
 }
 
-static noinline void
-bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
-{
-	__bad_area(regs, error_code, address, 0, SEGV_MAPERR);
-}
-
 static inline bool bad_area_access_from_pkeys(unsigned long error_code,
 		struct vm_area_struct *vma)
 {
@@ -1313,19 +1307,14 @@ void do_user_addr_fault(struct pt_regs *regs,
 	if (unlikely(cpu_feature_enabled(X86_FEATURE_SMAP) &&
 		     !(hw_error_code & X86_PF_USER) &&
 		     !(regs->flags & X86_EFLAGS_AC)))
-	{
-		bad_area_nosemaphore(regs, hw_error_code, address);
-		return;
-	}
+		goto bad_area_nosem;
 
 	/*
 	 * If we're in an interrupt, have no user context or are running
 	 * in a region with pagefaults disabled then we must not take the fault
 	 */
-	if (unlikely(faulthandler_disabled() || !mm)) {
-		bad_area_nosemaphore(regs, hw_error_code, address);
-		return;
-	}
+	if (unlikely(faulthandler_disabled() || !mm))
+		goto bad_area_nosem;
 
 	/*
 	 * It's safe to allow irq's after cr2 has been saved and the
@@ -1385,8 +1374,7 @@ void do_user_addr_fault(struct pt_regs *regs,
 			 * Fault from code in kernel from
 			 * which we do not expect faults.
 			 */
-			bad_area_nosemaphore(regs, hw_error_code, address);
-			return;
+			goto bad_area_nosem;
 		}
 retry:
 		down_read(&mm->mmap_sem);
@@ -1400,20 +1388,14 @@ void do_user_addr_fault(struct pt_regs *regs,
 	}
 
 	vma = find_vma(mm, address);
-	if (unlikely(!vma)) {
-		bad_area(regs, hw_error_code, address);
-		return;
-	}
+	if (unlikely(!vma))
+		goto bad_area;
 	if (likely(vma->vm_start <= address))
 		goto good_area;
-	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
-		bad_area(regs, hw_error_code, address);
-		return;
-	}
-	if (unlikely(expand_stack(vma, address))) {
-		bad_area(regs, hw_error_code, address);
-		return;
-	}
+	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
+		goto bad_area;
+	if (unlikely(expand_stack(vma, address)))
+		goto bad_area;
 
 	/*
 	 * Ok, we have a good vm_area for this memory access, so
@@ -1483,6 +1465,12 @@ void do_user_addr_fault(struct pt_regs *regs,
 	}
 
 	check_v8086_mode(regs, address, tsk);
+	return;
+
+bad_area:
+	up_read(&mm->mmap_sem);
+bad_area_nosem:
+	bad_area_nosemaphore(regs, hw_error_code, address);
 }
 NOKPROBE_SYMBOL(do_user_addr_fault);
 
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 x86/mm: replace a goto by merging two if clause

There is only one place to use good_area jump, which could be reduced by merging the following two if clause.

---
 arch/x86/mm/fault.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/mm/fault.c +5 -6
index 9d18b73b5f77..72ce6c69e195 100644
@@ -1390,18 +1390,17 @@ void do_user_addr_fault(struct pt_regs *regs,
 	vma = find_vma(mm, address);
 	if (unlikely(!vma))
 		goto bad_area;
-	if (likely(vma->vm_start <= address))
-		goto good_area;
-	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
-		goto bad_area;
-	if (unlikely(expand_stack(vma, address)))
+	if (likely(vma->vm_start <= address)) {
+		/* good area, do nothing */
+	} else if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)) ||
+		   unlikely(expand_stack(vma, address))) {
 		goto bad_area;
+	}
 
 	/*
 	 * Ok, we have a good vm_area for this memory access, so
 	 * we can handle it..
 	 */
-good_area:
 	if (unlikely(access_error(hw_error_code, vma))) {
 		bad_area_access_error(regs, hw_error_code, address, vma);
 		return;
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH 2/2] x86/mm: replace a goto by merging two if clause

Quoted 26 lines · Wei Yang

There is only one place to use good_area jump, which could be reduced by merging the following two if clause.

---
 arch/x86/mm/fault.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/mm/fault.c +4 -5
index 9d18b73b5f77..72ce6c69e195 100644
@@ -1390,18 +1390,17 @@ void do_user_addr_fault(struct pt_regs *regs,
 	vma = find_vma(mm, address);
 	if (unlikely(!vma))
 		goto bad_area;
-	if (likely(vma->vm_start <= address))
-		goto good_area;
-	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
-		goto bad_area;
-	if (unlikely(expand_stack(vma, address)))
+	if (likely(vma->vm_start <= address)) {
+		/* good area, do nothing */
+	} else if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)) ||
+		   unlikely(expand_stack(vma, address))) {
 		goto bad_area;

arch/x86/mm/fault.c void do_user_addr_fault(struct pt_regs *regs, · line 1406

Quoted 10 lines
arch/x86/mm/fault.c +1 -1
+	}
 
 	/*
 	 * Ok, we have a good vm_area for this memory access, so
 	 * we can handle it..
 	 */
-good_area:
 	if (unlikely(access_error(hw_error_code, vma))) {
 		bad_area_access_error(regs, hw_error_code, address, vma);
 		return;

I find the old code far easier to read... is there any actual reason to do this?

author
#3 lore raw

Re: [PATCH 2/2] x86/mm: replace a goto by merging two if clause

Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · Wei Yang

There is only one place to use good_area jump, which could be reduced by merging the following two if clause.

---
 arch/x86/mm/fault.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/mm/fault.c +5 -5
index 9d18b73b5f77..72ce6c69e195 100644
@@ -1390,18 +1390,17 @@ void do_user_addr_fault(struct pt_regs *regs,
 	vma = find_vma(mm, address);
 	if (unlikely(!vma))
 		goto bad_area;
-	if (likely(vma->vm_start <= address))
-		goto good_area;
-	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
-		goto bad_area;
-	if (unlikely(expand_stack(vma, address)))
+	if (likely(vma->vm_start <= address)) {
+		/* good area, do nothing */
+	} else if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)) ||
+		   unlikely(expand_stack(vma, address))) {
 		goto bad_area;
+	}
 
Quoted 11 lines

arch/x86/mm/fault.c void do_user_addr_fault(struct pt_regs *regs, · line 1406

Quoted 8 lines
arch/x86/mm/fault.c +0 -1
 	/*
 	 * Ok, we have a good vm_area for this memory access, so
 	 * we can handle it..
 	 */
-good_area:
 	if (unlikely(access_error(hw_error_code, vma))) {
 		bad_area_access_error(regs, hw_error_code, address, vma);
 		return;

I find the old code far easier to read... is there any actual reason to do this?

No, just want to make it easy to read.

Signature · 2 lines
Wei Yang
Help you, Help me
#4 lore raw

Re: [PATCH 2/2] x86/mm: replace a goto by merging two if clause

In reply to Peter Zijlstra
Quoted 29 lines · Peter Zijlstra
Quoted 28 lines · Wei Yang

There is only one place to use good_area jump, which could be reduced by merging the following two if clause.

---
 arch/x86/mm/fault.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
arch/x86/mm/fault.c +5 -5
index 9d18b73b5f77..72ce6c69e195 100644
@@ -1390,18 +1390,17 @@ void do_user_addr_fault(struct pt_regs *regs,
 	vma = find_vma(mm, address);
 	if (unlikely(!vma))
 		goto bad_area;
-	if (likely(vma->vm_start <= address))
-		goto good_area;
-	if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)))
-		goto bad_area;
-	if (unlikely(expand_stack(vma, address)))
+	if (likely(vma->vm_start <= address)) {
+		/* good area, do nothing */
+	} else if (unlikely(!(vma->vm_flags & VM_GROWSDOWN)) ||
+		   unlikely(expand_stack(vma, address))) {
 		goto bad_area;
+	}
 
Quoted 11 lines

arch/x86/mm/fault.c void do_user_addr_fault(struct pt_regs *regs, · line 1406

Quoted 8 lines
arch/x86/mm/fault.c +0 -1
 	/*
 	 * Ok, we have a good vm_area for this memory access, so
 	 * we can handle it..
 	 */
-good_area:
 	if (unlikely(access_error(hw_error_code, vma))) {
 		bad_area_access_error(regs, hw_error_code, address, vma);
 		return;

I find the old code far easier to read... is there any actual reason to do this?

Hi, Peter,

Do you have some comment for the Patch 1?

Signature · 2 lines
Wei Yang
Help you, Help me