All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] x86, mpx: Fixes for 3.19
@ 2015-01-08 22:30 Dave Hansen
  2015-01-08 22:30 ` [PATCH 1/3] x86, mpx: explicitly disable 32-bit MPX support on 64-bit kernels Dave Hansen
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Dave Hansen @ 2015-01-08 22:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: tglx, x86, luto, Dave Hansen

This is a resend of stuff sent shortly before the holidays,
plus an addition to the prctl() code to make it more strict
about empty arguments.

Thanks for Andi Lutomirski and Michael Kerrisk for noticing
some of this stuff.

 * Explictly disable 32-bit binaries on 64-bit kernels
 * Fix possible performance regression at unmap time
 * Be strict about enforcing empty prctl() arguments

 arch/x86/include/asm/mmu_context.h |   20 +++++++++++++++++++-
 arch/x86/mm/mpx.c                  |    6 ++++++
 kernel/sys.c                       |    4 ++++
 3 files changed, 29 insertions(+), 1 deletion(-)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] x86, mpx: explicitly disable 32-bit MPX support on 64-bit kernels
  2015-01-08 22:30 [PATCH 0/3] x86, mpx: Fixes for 3.19 Dave Hansen
@ 2015-01-08 22:30 ` Dave Hansen
  2015-01-22 20:12   ` [tip:x86/urgent] x86, mpx: Explicitly " tip-bot for Dave Hansen
  2015-01-08 22:30 ` [PATCH 2/3] x86 mpx: fix potential performance issue on unmaps Dave Hansen
  2015-01-08 22:30 ` [PATCH 3/3] x86 mpx: strictly enforce empty prctl() args Dave Hansen
  2 siblings, 1 reply; 7+ messages in thread
From: Dave Hansen @ 2015-01-08 22:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: tglx, x86, luto, Dave Hansen, dave.hansen


From: Dave Hansen <dave.hansen@linux.intel.com>

We had originally planned on submitting MPX support in one patch
set.  We eventually broke it up in to two pieces for easier
review.  One of the features that didn't make the first round
was supporting 32-bit binaries on 64-bit kernels.

Once we split the set up, we never added code to restrict 32-bit
binaries from _using_ MPX on 64-bit kernels.

The 32-bit bounds tables are a different format than the 64-bit
ones.  Without this patch, the kernel will try to read a 32-bit
binary's tables as if they were the 64-bit version.  They will
likely be noticed as being invalid rather quickly and the app
will get killed, but that's kinda mean.

This patch adds an explicit check, and will make a 64-bit kernel
essentially behave as if it has no MPX support when called from
a 32-bit binary.

I intend to remove this check once the mixed binary support is
added.  Those patches are posted if anyone is interested:

	https://lkml.org/lkml/2014/12/12/632

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andy Lutomirski <luto@amacapital.net>
---

 b/arch/x86/mm/mpx.c |    6 ++++++
 1 file changed, 6 insertions(+)

diff -puN arch/x86/mm/mpx.c~x86-mpx-explicitly-disable-32-bit-on-64-bit-for-3_19 arch/x86/mm/mpx.c
--- a/arch/x86/mm/mpx.c~x86-mpx-explicitly-disable-32-bit-on-64-bit-for-3_19	2015-01-08 12:48:09.123864625 -0800
+++ b/arch/x86/mm/mpx.c	2015-01-08 12:48:09.126864760 -0800
@@ -349,6 +349,12 @@ static __user void *task_get_bounds_dir(
 		return MPX_INVALID_BOUNDS_DIR;
 
 	/*
+	 * 32-bit binaries on 64-bit kernels are currently
+	 * unsupported.
+	 */
+	if (IS_ENABLED(CONFIG_X86_64) && test_thread_flag(TIF_IA32))
+		return MPX_INVALID_BOUNDS_DIR;
+	/*
 	 * The bounds directory pointer is stored in a register
 	 * only accessible if we first do an xsave.
 	 */
_

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/3] x86 mpx: fix potential performance issue on unmaps
  2015-01-08 22:30 [PATCH 0/3] x86, mpx: Fixes for 3.19 Dave Hansen
  2015-01-08 22:30 ` [PATCH 1/3] x86, mpx: explicitly disable 32-bit MPX support on 64-bit kernels Dave Hansen
@ 2015-01-08 22:30 ` Dave Hansen
  2015-01-22 20:13   ` [tip:x86/urgent] x86, mpx: Fix " tip-bot for Dave Hansen
  2015-01-08 22:30 ` [PATCH 3/3] x86 mpx: strictly enforce empty prctl() args Dave Hansen
  2 siblings, 1 reply; 7+ messages in thread
From: Dave Hansen @ 2015-01-08 22:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: tglx, x86, luto, Dave Hansen, dave.hansen


From: Dave Hansen <dave.hansen@linux.intel.com>

The 3.19 release window saw some TLB modifications merged which
caused a performance regression.  They were fixed in commit
045bbb9fa.

Once that fix was applied, I also noticed that there was a small
but intermittent regression still present.  It was not present
consistently enough to bisect reliably, but I'm fairly confident
that it came from (my own) MPX patches.  The source was reading
a relatively unused field in the mm_struct via arch_unmap.

I also noted that this code was in the main instruction flow of
do_munmap() and probably had more icache impact than we want.

This patch does two things:
1. Adds a static (via Kconfig) and dynamic (via cpuid) check
   for MPX with cpu_feature_enabled().  This keeps us from
   reading that cacheline in the mm and trades it for a check
   of the global CPUID variables at least on CPUs without MPX.
2. Adds an unlikely() to ensure that the MPX call ends up out
   of the main instruction flow in do_munmap().  I've added
   a detailed comment about why this was done and why we want
   it even on systems where MPX is present.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
---

 b/arch/x86/include/asm/mmu_context.h |   20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff -puN arch/x86/include/asm/mmu_context.h~fix-mpx-regression-on-unmap arch/x86/include/asm/mmu_context.h
--- a/arch/x86/include/asm/mmu_context.h~fix-mpx-regression-on-unmap	2015-01-08 12:48:09.496881449 -0800
+++ b/arch/x86/include/asm/mmu_context.h	2015-01-08 12:48:09.499881584 -0800
@@ -130,7 +130,25 @@ static inline void arch_bprm_mm_init(str
 static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
 			      unsigned long start, unsigned long end)
 {
-	mpx_notify_unmap(mm, vma, start, end);
+	/*
+	 * mpx_notify_unmap() goes and reads a rarely-hot
+	 * cacheline in the mm_struct.  That can be expensive
+	 * enough to be seen in profiles.
+	 *
+	 * The mpx_notify_unmap() call and its contents have been
+	 * observed to affect munmap() performance on hardware
+	 * where MPX is not present.
+	 *
+	 * The unlikely() optimizes for the fast case: no MPX
+	 * in the CPU, or no MPX use in the process.  Even if
+	 * we get this wrong (in the unlikely event that MPX
+	 * is widely enabled on some system) the overhead of
+	 * MPX itself (reading bounds tables) is expected to
+	 * overwhelm the overhead of getting this unlikely()
+	 * consistently wrong.
+	 */
+	if (unlikely(cpu_feature_enabled(X86_FEATURE_MPX)))
+		mpx_notify_unmap(mm, vma, start, end);
 }
 
 #endif /* _ASM_X86_MMU_CONTEXT_H */
_

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/3] x86 mpx: strictly enforce empty prctl() args
  2015-01-08 22:30 [PATCH 0/3] x86, mpx: Fixes for 3.19 Dave Hansen
  2015-01-08 22:30 ` [PATCH 1/3] x86, mpx: explicitly disable 32-bit MPX support on 64-bit kernels Dave Hansen
  2015-01-08 22:30 ` [PATCH 2/3] x86 mpx: fix potential performance issue on unmaps Dave Hansen
@ 2015-01-08 22:30 ` Dave Hansen
  2015-01-22 20:13   ` [tip:x86/urgent] x86, mpx: Strictly " tip-bot for Dave Hansen
  2 siblings, 1 reply; 7+ messages in thread
From: Dave Hansen @ 2015-01-08 22:30 UTC (permalink / raw)
  To: linux-kernel; +Cc: tglx, x86, luto, Dave Hansen, mtk.manpages, dave.hansen


From: Dave Hansen <dave.hansen@linux.intel.com>

Description from Michael Kerrisk.  He suggested an identical patch
to one I had already coded up and tested.

commit fe8c7f5cbf91124987106faa3bdf0c8b955c4cf7 added two new prctl()
operations, PR_MPX_ENABLE_MANAGEMENT and PR_MPX_DISABLE_MANAGEMENT.
However, no checks were included to ensure that unused arguments
are zero, as is done in many existing prctl()s and as should be 
done for all new prctl()s. This patch adds the required checks.

Suggested-by: Andy Lutomirski <luto@amacapital.net>
Suggested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
---

 b/kernel/sys.c |    4 ++++
 1 file changed, 4 insertions(+)

diff -puN kernel/sys.c~x86-mpx-strictly-enforce-empty-prctl-args kernel/sys.c
--- a/kernel/sys.c~x86-mpx-strictly-enforce-empty-prctl-args	2015-01-08 12:48:09.868898228 -0800
+++ b/kernel/sys.c	2015-01-08 12:48:09.872898408 -0800
@@ -2210,9 +2210,13 @@ SYSCALL_DEFINE5(prctl, int, option, unsi
 		up_write(&me->mm->mmap_sem);
 		break;
 	case PR_MPX_ENABLE_MANAGEMENT:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
 		error = MPX_ENABLE_MANAGEMENT(me);
 		break;
 	case PR_MPX_DISABLE_MANAGEMENT:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
 		error = MPX_DISABLE_MANAGEMENT(me);
 		break;
 	default:
_

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [tip:x86/urgent] x86, mpx: Explicitly disable 32-bit MPX support on 64-bit kernels
  2015-01-08 22:30 ` [PATCH 1/3] x86, mpx: explicitly disable 32-bit MPX support on 64-bit kernels Dave Hansen
@ 2015-01-22 20:12   ` tip-bot for Dave Hansen
  0 siblings, 0 replies; 7+ messages in thread
From: tip-bot for Dave Hansen @ 2015-01-22 20:12 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: mingo, dave.hansen, dave, linux-kernel, hpa, tglx, luto

Commit-ID:  814564a0a1d2faee11ff9de43245d78cb79c85ac
Gitweb:     http://git.kernel.org/tip/814564a0a1d2faee11ff9de43245d78cb79c85ac
Author:     Dave Hansen <dave.hansen@linux.intel.com>
AuthorDate: Thu, 8 Jan 2015 14:30:20 -0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Thu, 22 Jan 2015 21:11:06 +0100

x86, mpx: Explicitly disable 32-bit MPX support on 64-bit kernels

We had originally planned on submitting MPX support in one patch
set.  We eventually broke it up in to two pieces for easier
review.  One of the features that didn't make the first round
was supporting 32-bit binaries on 64-bit kernels.

Once we split the set up, we never added code to restrict 32-bit
binaries from _using_ MPX on 64-bit kernels.

The 32-bit bounds tables are a different format than the 64-bit
ones.  Without this patch, the kernel will try to read a 32-bit
binary's tables as if they were the 64-bit version.  They will
likely be noticed as being invalid rather quickly and the app
will get killed, but that's kinda mean.

This patch adds an explicit check, and will make a 64-bit kernel
essentially behave as if it has no MPX support when called from
a 32-bit binary.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Dave Hansen <dave@sr71.net>
Link: http://lkml.kernel.org/r/20150108223020.9E9AA511@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/mm/mpx.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/x86/mm/mpx.c b/arch/x86/mm/mpx.c
index 67ebf57..c439ec4 100644
--- a/arch/x86/mm/mpx.c
+++ b/arch/x86/mm/mpx.c
@@ -349,6 +349,12 @@ static __user void *task_get_bounds_dir(struct task_struct *tsk)
 		return MPX_INVALID_BOUNDS_DIR;
 
 	/*
+	 * 32-bit binaries on 64-bit kernels are currently
+	 * unsupported.
+	 */
+	if (IS_ENABLED(CONFIG_X86_64) && test_thread_flag(TIF_IA32))
+		return MPX_INVALID_BOUNDS_DIR;
+	/*
 	 * The bounds directory pointer is stored in a register
 	 * only accessible if we first do an xsave.
 	 */

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [tip:x86/urgent] x86, mpx: Fix potential performance issue on unmaps
  2015-01-08 22:30 ` [PATCH 2/3] x86 mpx: fix potential performance issue on unmaps Dave Hansen
@ 2015-01-22 20:13   ` tip-bot for Dave Hansen
  0 siblings, 0 replies; 7+ messages in thread
From: tip-bot for Dave Hansen @ 2015-01-22 20:13 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: mingo, linux-kernel, dave.hansen, hpa, dave, tglx

Commit-ID:  c922228efeeefa32e57f875764bfa6ca8053a68a
Gitweb:     http://git.kernel.org/tip/c922228efeeefa32e57f875764bfa6ca8053a68a
Author:     Dave Hansen <dave.hansen@linux.intel.com>
AuthorDate: Thu, 8 Jan 2015 14:30:21 -0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Thu, 22 Jan 2015 21:11:06 +0100

x86, mpx: Fix potential performance issue on unmaps

The 3.19 merge window saw some TLB modifications merged which caused a
performance regression. They were fixed in commit 045bbb9fa.

Once that fix was applied, I also noticed that there was a small
but intermittent regression still present.  It was not present
consistently enough to bisect reliably, but I'm fairly confident
that it came from (my own) MPX patches.  The source was reading
a relatively unused field in the mm_struct via arch_unmap.

I also noted that this code was in the main instruction flow of
do_munmap() and probably had more icache impact than we want.

This patch does two things:
1. Adds a static (via Kconfig) and dynamic (via cpuid) check
   for MPX with cpu_feature_enabled().  This keeps us from
   reading that cacheline in the mm and trades it for a check
   of the global CPUID variables at least on CPUs without MPX.
2. Adds an unlikely() to ensure that the MPX call ends up out
   of the main instruction flow in do_munmap().  I've added
   a detailed comment about why this was done and why we want
   it even on systems where MPX is present.

Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: luto@amacapital.net
Cc: Dave Hansen <dave@sr71.net>
Link: http://lkml.kernel.org/r/20150108223021.AEEAB987@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/include/asm/mmu_context.h | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h
index 40269a2..4b75d59 100644
--- a/arch/x86/include/asm/mmu_context.h
+++ b/arch/x86/include/asm/mmu_context.h
@@ -130,7 +130,25 @@ static inline void arch_bprm_mm_init(struct mm_struct *mm,
 static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
 			      unsigned long start, unsigned long end)
 {
-	mpx_notify_unmap(mm, vma, start, end);
+	/*
+	 * mpx_notify_unmap() goes and reads a rarely-hot
+	 * cacheline in the mm_struct.  That can be expensive
+	 * enough to be seen in profiles.
+	 *
+	 * The mpx_notify_unmap() call and its contents have been
+	 * observed to affect munmap() performance on hardware
+	 * where MPX is not present.
+	 *
+	 * The unlikely() optimizes for the fast case: no MPX
+	 * in the CPU, or no MPX use in the process.  Even if
+	 * we get this wrong (in the unlikely event that MPX
+	 * is widely enabled on some system) the overhead of
+	 * MPX itself (reading bounds tables) is expected to
+	 * overwhelm the overhead of getting this unlikely()
+	 * consistently wrong.
+	 */
+	if (unlikely(cpu_feature_enabled(X86_FEATURE_MPX)))
+		mpx_notify_unmap(mm, vma, start, end);
 }
 
 #endif /* _ASM_X86_MMU_CONTEXT_H */

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [tip:x86/urgent] x86, mpx: Strictly enforce empty prctl() args
  2015-01-08 22:30 ` [PATCH 3/3] x86 mpx: strictly enforce empty prctl() args Dave Hansen
@ 2015-01-22 20:13   ` tip-bot for Dave Hansen
  0 siblings, 0 replies; 7+ messages in thread
From: tip-bot for Dave Hansen @ 2015-01-22 20:13 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: dave, dave.hansen, luto, mingo, mtk.manpages, hpa, linux-kernel, tglx

Commit-ID:  e9d1b4f3c60997fe197bf0243cb4a41a44387a88
Gitweb:     http://git.kernel.org/tip/e9d1b4f3c60997fe197bf0243cb4a41a44387a88
Author:     Dave Hansen <dave.hansen@linux.intel.com>
AuthorDate: Thu, 8 Jan 2015 14:30:22 -0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Thu, 22 Jan 2015 21:11:06 +0100

x86, mpx: Strictly enforce empty prctl() args

Description from Michael Kerrisk.  He suggested an identical patch
to one I had already coded up and tested.

commit fe3d197f8431 "x86, mpx: On-demand kernel allocation of bounds
tables" added two new prctl() operations, PR_MPX_ENABLE_MANAGEMENT and
PR_MPX_DISABLE_MANAGEMENT.  However, no checks were included to ensure
that unused arguments are zero, as is done in many existing prctl()s
and as should be done for all new prctl()s. This patch adds the
required checks.

Suggested-by: Andy Lutomirski <luto@amacapital.net>
Suggested-by: Michael Kerrisk <mtk.manpages@gmail.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Dave Hansen <dave@sr71.net>
Link: http://lkml.kernel.org/r/20150108223022.7F56FD13@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/sys.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/kernel/sys.c b/kernel/sys.c
index a8c9f5a..ea9c881 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2210,9 +2210,13 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		up_write(&me->mm->mmap_sem);
 		break;
 	case PR_MPX_ENABLE_MANAGEMENT:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
 		error = MPX_ENABLE_MANAGEMENT(me);
 		break;
 	case PR_MPX_DISABLE_MANAGEMENT:
+		if (arg2 || arg3 || arg4 || arg5)
+			return -EINVAL;
 		error = MPX_DISABLE_MANAGEMENT(me);
 		break;
 	default:

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-01-22 20:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-08 22:30 [PATCH 0/3] x86, mpx: Fixes for 3.19 Dave Hansen
2015-01-08 22:30 ` [PATCH 1/3] x86, mpx: explicitly disable 32-bit MPX support on 64-bit kernels Dave Hansen
2015-01-22 20:12   ` [tip:x86/urgent] x86, mpx: Explicitly " tip-bot for Dave Hansen
2015-01-08 22:30 ` [PATCH 2/3] x86 mpx: fix potential performance issue on unmaps Dave Hansen
2015-01-22 20:13   ` [tip:x86/urgent] x86, mpx: Fix " tip-bot for Dave Hansen
2015-01-08 22:30 ` [PATCH 3/3] x86 mpx: strictly enforce empty prctl() args Dave Hansen
2015-01-22 20:13   ` [tip:x86/urgent] x86, mpx: Strictly " tip-bot for Dave Hansen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.