2008-10-23 12:26:29 +07:00
|
|
|
#ifndef _ASM_X86_MMU_CONTEXT_H
|
|
|
|
#define _ASM_X86_MMU_CONTEXT_H
|
2008-06-25 11:19:07 +07:00
|
|
|
|
|
|
|
#include <asm/desc.h>
|
2011-07-27 06:09:06 +07:00
|
|
|
#include <linux/atomic.h>
|
2014-07-31 22:40:59 +07:00
|
|
|
#include <linux/mm_types.h>
|
|
|
|
|
|
|
|
#include <trace/events/tlb.h>
|
|
|
|
|
2008-06-25 11:19:07 +07:00
|
|
|
#include <asm/pgalloc.h>
|
|
|
|
#include <asm/tlbflush.h>
|
|
|
|
#include <asm/paravirt.h>
|
x86, mpx: On-demand kernel allocation of bounds tables
This is really the meat of the MPX patch set. If there is one patch to
review in the entire series, this is the one. There is a new ABI here
and this kernel code also interacts with userspace memory in a
relatively unusual manner. (small FAQ below).
Long Description:
This patch adds two prctl() commands to provide enable or disable the
management of bounds tables in kernel, including on-demand kernel
allocation (See the patch "on-demand kernel allocation of bounds tables")
and cleanup (See the patch "cleanup unused bound tables"). Applications
do not strictly need the kernel to manage bounds tables and we expect
some applications to use MPX without taking advantage of this kernel
support. This means the kernel can not simply infer whether an application
needs bounds table management from the MPX registers. The prctl() is an
explicit signal from userspace.
PR_MPX_ENABLE_MANAGEMENT is meant to be a signal from userspace to
require kernel's help in managing bounds tables.
PR_MPX_DISABLE_MANAGEMENT is the opposite, meaning that userspace don't
want kernel's help any more. With PR_MPX_DISABLE_MANAGEMENT, the kernel
won't allocate and free bounds tables even if the CPU supports MPX.
PR_MPX_ENABLE_MANAGEMENT will fetch the base address of the bounds
directory out of a userspace register (bndcfgu) and then cache it into
a new field (->bd_addr) in the 'mm_struct'. PR_MPX_DISABLE_MANAGEMENT
will set "bd_addr" to an invalid address. Using this scheme, we can
use "bd_addr" to determine whether the management of bounds tables in
kernel is enabled.
Also, the only way to access that bndcfgu register is via an xsaves,
which can be expensive. Caching "bd_addr" like this also helps reduce
the cost of those xsaves when doing table cleanup at munmap() time.
Unfortunately, we can not apply this optimization to #BR fault time
because we need an xsave to get the value of BNDSTATUS.
==== Why does the hardware even have these Bounds Tables? ====
MPX only has 4 hardware registers for storing bounds information.
If MPX-enabled code needs more than these 4 registers, it needs to
spill them somewhere. It has two special instructions for this
which allow the bounds to be moved between the bounds registers
and some new "bounds tables".
They are similar conceptually to a page fault and will be raised by
the MPX hardware during both bounds violations or when the tables
are not present. This patch handles those #BR exceptions for
not-present tables by carving the space out of the normal processes
address space (essentially calling the new mmap() interface indroduced
earlier in this patch set.) and then pointing the bounds-directory
over to it.
The tables *need* to be accessed and controlled by userspace because
the instructions for moving bounds in and out of them are extremely
frequent. They potentially happen every time a register pointing to
memory is dereferenced. Any direct kernel involvement (like a syscall)
to access the tables would obviously destroy performance.
==== Why not do this in userspace? ====
This patch is obviously doing this allocation in the kernel.
However, MPX does not strictly *require* anything in the kernel.
It can theoretically be done completely from userspace. Here are
a few ways this *could* be done. I don't think any of them are
practical in the real-world, but here they are.
Q: Can virtual space simply be reserved for the bounds tables so
that we never have to allocate them?
A: As noted earlier, these tables are *HUGE*. An X-GB virtual
area needs 4*X GB of virtual space, plus 2GB for the bounds
directory. If we were to preallocate them for the 128TB of
user virtual address space, we would need to reserve 512TB+2GB,
which is larger than the entire virtual address space today.
This means they can not be reserved ahead of time. Also, a
single process's pre-popualated bounds directory consumes 2GB
of virtual *AND* physical memory. IOW, it's completely
infeasible to prepopulate bounds directories.
Q: Can we preallocate bounds table space at the same time memory
is allocated which might contain pointers that might eventually
need bounds tables?
A: This would work if we could hook the site of each and every
memory allocation syscall. This can be done for small,
constrained applications. But, it isn't practical at a larger
scale since a given app has no way of controlling how all the
parts of the app might allocate memory (think libraries). The
kernel is really the only place to intercept these calls.
Q: Could a bounds fault be handed to userspace and the tables
allocated there in a signal handler instead of in the kernel?
A: (thanks to tglx) mmap() is not on the list of safe async
handler functions and even if mmap() would work it still
requires locking or nasty tricks to keep track of the
allocation state there.
Having ruled out all of the userspace-only approaches for managing
bounds tables that we could think of, we create them on demand in
the kernel.
Based-on-patch-by: Qiaowei Ren <qiaowei.ren@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-mm@kvack.org
Cc: linux-mips@linux-mips.org
Cc: Dave Hansen <dave@sr71.net>
Link: http://lkml.kernel.org/r/20141114151829.AD4310DE@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2014-11-14 22:18:29 +07:00
|
|
|
#include <asm/mpx.h>
|
2008-06-25 11:19:07 +07:00
|
|
|
#ifndef CONFIG_PARAVIRT
|
|
|
|
static inline void paravirt_activate_mm(struct mm_struct *prev,
|
|
|
|
struct mm_struct *next)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_PARAVIRT */
|
|
|
|
|
2014-10-25 05:58:12 +07:00
|
|
|
#ifdef CONFIG_PERF_EVENTS
|
2014-10-25 05:58:13 +07:00
|
|
|
extern struct static_key rdpmc_always_available;
|
|
|
|
|
2014-10-25 05:58:12 +07:00
|
|
|
static inline void load_mm_cr4(struct mm_struct *mm)
|
|
|
|
{
|
2015-07-10 00:23:38 +07:00
|
|
|
if (static_key_false(&rdpmc_always_available) ||
|
2014-10-25 05:58:13 +07:00
|
|
|
atomic_read(&mm->context.perf_rdpmc_allowed))
|
2014-10-25 05:58:12 +07:00
|
|
|
cr4_set_bits(X86_CR4_PCE);
|
|
|
|
else
|
|
|
|
cr4_clear_bits(X86_CR4_PCE);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void load_mm_cr4(struct mm_struct *mm) {}
|
|
|
|
#endif
|
|
|
|
|
2015-07-31 04:31:34 +07:00
|
|
|
#ifdef CONFIG_MODIFY_LDT_SYSCALL
|
2015-07-31 04:31:32 +07:00
|
|
|
/*
|
|
|
|
* ldt_structs can be allocated, used, and freed, but they are never
|
|
|
|
* modified while live.
|
|
|
|
*/
|
|
|
|
struct ldt_struct {
|
|
|
|
/*
|
|
|
|
* Xen requires page-aligned LDTs with special permissions. This is
|
|
|
|
* needed to prevent us from installing evil descriptors such as
|
|
|
|
* call gates. On native, we could merge the ldt_struct and LDT
|
|
|
|
* allocations, but it's not worth trying to optimize.
|
|
|
|
*/
|
|
|
|
struct desc_struct *entries;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
2015-07-31 04:31:34 +07:00
|
|
|
/*
|
|
|
|
* Used for LDT copy/destruction.
|
|
|
|
*/
|
|
|
|
int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
|
|
|
|
void destroy_context(struct mm_struct *mm);
|
|
|
|
#else /* CONFIG_MODIFY_LDT_SYSCALL */
|
|
|
|
static inline int init_new_context(struct task_struct *tsk,
|
|
|
|
struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static inline void destroy_context(struct mm_struct *mm) {}
|
|
|
|
#endif
|
|
|
|
|
2015-07-31 04:31:32 +07:00
|
|
|
static inline void load_mm_ldt(struct mm_struct *mm)
|
|
|
|
{
|
2015-07-31 04:31:34 +07:00
|
|
|
#ifdef CONFIG_MODIFY_LDT_SYSCALL
|
2015-07-31 04:31:32 +07:00
|
|
|
struct ldt_struct *ldt;
|
|
|
|
|
|
|
|
/* lockless_dereference synchronizes with smp_store_release */
|
|
|
|
ldt = lockless_dereference(mm->context.ldt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Any change to mm->context.ldt is followed by an IPI to all
|
|
|
|
* CPUs with the mm active. The LDT will not be freed until
|
|
|
|
* after the IPI is handled by all such CPUs. This means that,
|
|
|
|
* if the ldt_struct changes before we return, the values we see
|
|
|
|
* will be safe, and the new values will be loaded before we run
|
|
|
|
* any user code.
|
|
|
|
*
|
|
|
|
* NB: don't try to convert this to use RCU without extreme care.
|
|
|
|
* We would still need IRQs off, because we don't want to change
|
|
|
|
* the local LDT after an IPI loaded a newer value than the one
|
|
|
|
* that we can see.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (unlikely(ldt))
|
|
|
|
set_ldt(ldt->entries, ldt->size);
|
|
|
|
else
|
|
|
|
clear_LDT();
|
2015-07-31 04:31:34 +07:00
|
|
|
#else
|
|
|
|
clear_LDT();
|
|
|
|
#endif
|
2015-07-31 04:31:32 +07:00
|
|
|
|
|
|
|
DEBUG_LOCKS_WARN_ON(preemptible());
|
|
|
|
}
|
|
|
|
|
2009-01-21 15:26:06 +07:00
|
|
|
static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_SMP
|
2012-05-11 14:35:27 +07:00
|
|
|
if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
|
|
|
|
this_cpu_write(cpu_tlbstate.state, TLBSTATE_LAZY);
|
2009-01-21 15:26:06 +07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
|
|
|
|
struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
unsigned cpu = smp_processor_id();
|
|
|
|
|
|
|
|
if (likely(prev != next)) {
|
|
|
|
#ifdef CONFIG_SMP
|
2012-05-11 14:35:27 +07:00
|
|
|
this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
|
|
|
|
this_cpu_write(cpu_tlbstate.active_mm, next);
|
2007-10-11 16:20:03 +07:00
|
|
|
#endif
|
2009-09-24 22:34:51 +07:00
|
|
|
cpumask_set_cpu(cpu, mm_cpumask(next));
|
2009-01-21 15:26:06 +07:00
|
|
|
|
2016-01-07 03:21:01 +07:00
|
|
|
/*
|
|
|
|
* Re-load page tables.
|
|
|
|
*
|
|
|
|
* This logic has an ordering constraint:
|
|
|
|
*
|
|
|
|
* CPU 0: Write to a PTE for 'next'
|
|
|
|
* CPU 0: load bit 1 in mm_cpumask. if nonzero, send IPI.
|
|
|
|
* CPU 1: set bit 1 in next's mm_cpumask
|
|
|
|
* CPU 1: load from the PTE that CPU 0 writes (implicit)
|
|
|
|
*
|
|
|
|
* We need to prevent an outcome in which CPU 1 observes
|
|
|
|
* the new PTE value and CPU 0 observes bit 1 clear in
|
|
|
|
* mm_cpumask. (If that occurs, then the IPI will never
|
|
|
|
* be sent, and CPU 0's TLB will contain a stale entry.)
|
|
|
|
*
|
|
|
|
* The bad outcome can occur if either CPU's load is
|
2016-01-13 03:47:40 +07:00
|
|
|
* reordered before that CPU's store, so both CPUs must
|
2016-01-07 03:21:01 +07:00
|
|
|
* execute full barriers to prevent this from happening.
|
|
|
|
*
|
|
|
|
* Thus, switch_mm needs a full barrier between the
|
|
|
|
* store to mm_cpumask and any operation that could load
|
2016-01-13 03:47:40 +07:00
|
|
|
* from next->pgd. TLB fills are special and can happen
|
|
|
|
* due to instruction fetches or for no reason at all,
|
|
|
|
* and neither LOCK nor MFENCE orders them.
|
|
|
|
* Fortunately, load_cr3() is serializing and gives the
|
|
|
|
* ordering guarantee we need.
|
2016-01-07 03:21:01 +07:00
|
|
|
*
|
|
|
|
*/
|
2009-01-21 15:26:06 +07:00
|
|
|
load_cr3(next->pgd);
|
2016-01-07 03:21:01 +07:00
|
|
|
|
2014-07-31 22:40:59 +07:00
|
|
|
trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
|
2009-01-21 15:26:06 +07:00
|
|
|
|
2013-08-01 09:14:21 +07:00
|
|
|
/* Stop flush ipis for the previous mm */
|
2011-02-04 03:20:04 +07:00
|
|
|
cpumask_clear_cpu(cpu, mm_cpumask(prev));
|
|
|
|
|
2014-10-25 05:58:12 +07:00
|
|
|
/* Load per-mm CR4 state */
|
|
|
|
load_mm_cr4(next);
|
|
|
|
|
2015-07-31 04:31:34 +07:00
|
|
|
#ifdef CONFIG_MODIFY_LDT_SYSCALL
|
2014-10-07 02:36:47 +07:00
|
|
|
/*
|
|
|
|
* Load the LDT, if the LDT is different.
|
|
|
|
*
|
2014-10-25 05:58:09 +07:00
|
|
|
* It's possible that prev->context.ldt doesn't match
|
|
|
|
* the LDT register. This can happen if leave_mm(prev)
|
|
|
|
* was called and then modify_ldt changed
|
|
|
|
* prev->context.ldt but suppressed an IPI to this CPU.
|
|
|
|
* In this case, prev->context.ldt != NULL, because we
|
2015-07-31 04:31:32 +07:00
|
|
|
* never set context.ldt to NULL while the mm still
|
|
|
|
* exists. That means that next->context.ldt !=
|
|
|
|
* prev->context.ldt, because mms never share an LDT.
|
2014-10-07 02:36:47 +07:00
|
|
|
*/
|
2009-01-21 15:26:06 +07:00
|
|
|
if (unlikely(prev->context.ldt != next->context.ldt))
|
2015-07-31 04:31:32 +07:00
|
|
|
load_mm_ldt(next);
|
2015-07-31 04:31:34 +07:00
|
|
|
#endif
|
2009-01-21 15:26:06 +07:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_SMP
|
2013-08-01 09:14:21 +07:00
|
|
|
else {
|
2012-05-11 14:35:27 +07:00
|
|
|
this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
|
|
|
|
BUG_ON(this_cpu_read(cpu_tlbstate.active_mm) != next);
|
2009-01-21 15:26:06 +07:00
|
|
|
|
2013-08-01 09:14:21 +07:00
|
|
|
if (!cpumask_test_cpu(cpu, mm_cpumask(next))) {
|
|
|
|
/*
|
|
|
|
* On established mms, the mm_cpumask is only changed
|
|
|
|
* from irq context, from ptep_clear_flush() while in
|
|
|
|
* lazy tlb mode, and here. Irqs are blocked during
|
|
|
|
* schedule, protecting us from simultaneous changes.
|
|
|
|
*/
|
|
|
|
cpumask_set_cpu(cpu, mm_cpumask(next));
|
2016-01-07 03:21:01 +07:00
|
|
|
|
2013-08-01 09:14:21 +07:00
|
|
|
/*
|
|
|
|
* We were in lazy tlb mode and leave_mm disabled
|
2009-01-21 15:26:06 +07:00
|
|
|
* tlb flush IPI delivery. We must reload CR3
|
|
|
|
* to make sure to use no freed page tables.
|
2016-01-07 03:21:01 +07:00
|
|
|
*
|
2016-01-13 03:47:40 +07:00
|
|
|
* As above, load_cr3() is serializing and orders TLB
|
|
|
|
* fills with respect to the mm_cpumask write.
|
2009-01-21 15:26:06 +07:00
|
|
|
*/
|
|
|
|
load_cr3(next->pgd);
|
2014-07-31 22:40:59 +07:00
|
|
|
trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
|
2014-10-25 05:58:12 +07:00
|
|
|
load_mm_cr4(next);
|
2015-07-31 04:31:32 +07:00
|
|
|
load_mm_ldt(next);
|
2009-01-21 15:26:06 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2008-06-25 11:19:07 +07:00
|
|
|
|
|
|
|
#define activate_mm(prev, next) \
|
|
|
|
do { \
|
|
|
|
paravirt_activate_mm((prev), (next)); \
|
|
|
|
switch_mm((prev), (next), NULL); \
|
|
|
|
} while (0);
|
|
|
|
|
2009-01-21 15:26:06 +07:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
#define deactivate_mm(tsk, mm) \
|
|
|
|
do { \
|
2009-02-09 20:17:40 +07:00
|
|
|
lazy_load_gs(0); \
|
2009-01-21 15:26:06 +07:00
|
|
|
} while (0)
|
|
|
|
#else
|
|
|
|
#define deactivate_mm(tsk, mm) \
|
|
|
|
do { \
|
|
|
|
load_gs_index(0); \
|
|
|
|
loadsegment(fs, 0); \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
2008-06-25 11:19:07 +07:00
|
|
|
|
2014-11-19 01:23:49 +07:00
|
|
|
static inline void arch_dup_mmap(struct mm_struct *oldmm,
|
|
|
|
struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
paravirt_arch_dup_mmap(oldmm, mm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void arch_exit_mmap(struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
paravirt_arch_exit_mmap(mm);
|
|
|
|
}
|
|
|
|
|
2015-06-08 01:37:04 +07:00
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
static inline bool is_64bit_mm(struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
return !config_enabled(CONFIG_IA32_EMULATION) ||
|
|
|
|
!(mm->context.ia32_compat == TIF_IA32);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline bool is_64bit_mm(struct mm_struct *mm)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
x86, mpx: On-demand kernel allocation of bounds tables
This is really the meat of the MPX patch set. If there is one patch to
review in the entire series, this is the one. There is a new ABI here
and this kernel code also interacts with userspace memory in a
relatively unusual manner. (small FAQ below).
Long Description:
This patch adds two prctl() commands to provide enable or disable the
management of bounds tables in kernel, including on-demand kernel
allocation (See the patch "on-demand kernel allocation of bounds tables")
and cleanup (See the patch "cleanup unused bound tables"). Applications
do not strictly need the kernel to manage bounds tables and we expect
some applications to use MPX without taking advantage of this kernel
support. This means the kernel can not simply infer whether an application
needs bounds table management from the MPX registers. The prctl() is an
explicit signal from userspace.
PR_MPX_ENABLE_MANAGEMENT is meant to be a signal from userspace to
require kernel's help in managing bounds tables.
PR_MPX_DISABLE_MANAGEMENT is the opposite, meaning that userspace don't
want kernel's help any more. With PR_MPX_DISABLE_MANAGEMENT, the kernel
won't allocate and free bounds tables even if the CPU supports MPX.
PR_MPX_ENABLE_MANAGEMENT will fetch the base address of the bounds
directory out of a userspace register (bndcfgu) and then cache it into
a new field (->bd_addr) in the 'mm_struct'. PR_MPX_DISABLE_MANAGEMENT
will set "bd_addr" to an invalid address. Using this scheme, we can
use "bd_addr" to determine whether the management of bounds tables in
kernel is enabled.
Also, the only way to access that bndcfgu register is via an xsaves,
which can be expensive. Caching "bd_addr" like this also helps reduce
the cost of those xsaves when doing table cleanup at munmap() time.
Unfortunately, we can not apply this optimization to #BR fault time
because we need an xsave to get the value of BNDSTATUS.
==== Why does the hardware even have these Bounds Tables? ====
MPX only has 4 hardware registers for storing bounds information.
If MPX-enabled code needs more than these 4 registers, it needs to
spill them somewhere. It has two special instructions for this
which allow the bounds to be moved between the bounds registers
and some new "bounds tables".
They are similar conceptually to a page fault and will be raised by
the MPX hardware during both bounds violations or when the tables
are not present. This patch handles those #BR exceptions for
not-present tables by carving the space out of the normal processes
address space (essentially calling the new mmap() interface indroduced
earlier in this patch set.) and then pointing the bounds-directory
over to it.
The tables *need* to be accessed and controlled by userspace because
the instructions for moving bounds in and out of them are extremely
frequent. They potentially happen every time a register pointing to
memory is dereferenced. Any direct kernel involvement (like a syscall)
to access the tables would obviously destroy performance.
==== Why not do this in userspace? ====
This patch is obviously doing this allocation in the kernel.
However, MPX does not strictly *require* anything in the kernel.
It can theoretically be done completely from userspace. Here are
a few ways this *could* be done. I don't think any of them are
practical in the real-world, but here they are.
Q: Can virtual space simply be reserved for the bounds tables so
that we never have to allocate them?
A: As noted earlier, these tables are *HUGE*. An X-GB virtual
area needs 4*X GB of virtual space, plus 2GB for the bounds
directory. If we were to preallocate them for the 128TB of
user virtual address space, we would need to reserve 512TB+2GB,
which is larger than the entire virtual address space today.
This means they can not be reserved ahead of time. Also, a
single process's pre-popualated bounds directory consumes 2GB
of virtual *AND* physical memory. IOW, it's completely
infeasible to prepopulate bounds directories.
Q: Can we preallocate bounds table space at the same time memory
is allocated which might contain pointers that might eventually
need bounds tables?
A: This would work if we could hook the site of each and every
memory allocation syscall. This can be done for small,
constrained applications. But, it isn't practical at a larger
scale since a given app has no way of controlling how all the
parts of the app might allocate memory (think libraries). The
kernel is really the only place to intercept these calls.
Q: Could a bounds fault be handed to userspace and the tables
allocated there in a signal handler instead of in the kernel?
A: (thanks to tglx) mmap() is not on the list of safe async
handler functions and even if mmap() would work it still
requires locking or nasty tricks to keep track of the
allocation state there.
Having ruled out all of the userspace-only approaches for managing
bounds tables that we could think of, we create them on demand in
the kernel.
Based-on-patch-by: Qiaowei Ren <qiaowei.ren@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-mm@kvack.org
Cc: linux-mips@linux-mips.org
Cc: Dave Hansen <dave@sr71.net>
Link: http://lkml.kernel.org/r/20141114151829.AD4310DE@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2014-11-14 22:18:29 +07:00
|
|
|
static inline void arch_bprm_mm_init(struct mm_struct *mm,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
mpx_mm_init(mm);
|
|
|
|
}
|
|
|
|
|
x86, mpx: Cleanup unused bound tables
The previous patch allocates bounds tables on-demand. As noted in
an earlier description, these can add up to *HUGE* amounts of
memory. This has caused OOMs in practice when running tests.
This patch adds support for freeing bounds tables when they are no
longer in use.
There are two types of mappings in play when unmapping tables:
1. The mapping with the actual data, which userspace is
munmap()ing or brk()ing away, etc...
2. The mapping for the bounds table *backing* the data
(is tagged with VM_MPX, see the patch "add MPX specific
mmap interface").
If userspace use the prctl() indroduced earlier in this patchset
to enable the management of bounds tables in kernel, when it
unmaps the first type of mapping with the actual data, the kernel
needs to free the mapping for the bounds table backing the data.
This patch hooks in at the very end of do_unmap() to do so.
We look at the addresses being unmapped and find the bounds
directory entries and tables which cover those addresses. If
an entire table is unused, we clear associated directory entry
and free the table.
Once we unmap the bounds table, we would have a bounds directory
entry pointing at empty address space. That address space might
now be allocated for some other (random) use, and the MPX
hardware might now try to walk it as if it were a bounds table.
That would be bad. So any unmapping of an enture bounds table
has to be accompanied by a corresponding write to the bounds
directory entry to invalidate it. That write to the bounds
directory can fault, which causes the following problem:
Since we are doing the freeing from munmap() (and other paths
like it), we hold mmap_sem for write. If we fault, the page
fault handler will attempt to acquire mmap_sem for read and
we will deadlock. To avoid the deadlock, we pagefault_disable()
when touching the bounds directory entry and use a
get_user_pages() to resolve the fault.
The unmapping of bounds tables happends under vm_munmap(). We
also (indirectly) call vm_munmap() to _do_ the unmapping of the
bounds tables. We avoid unbounded recursion by disallowing
freeing of bounds tables *for* bounds tables. This would not
occur normally, so should not have any practical impact. Being
strict about it here helps ensure that we do not have an
exploitable stack overflow.
Based-on-patch-by: Qiaowei Ren <qiaowei.ren@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-mm@kvack.org
Cc: linux-mips@linux-mips.org
Cc: Dave Hansen <dave@sr71.net>
Link: http://lkml.kernel.org/r/20141114151831.E4531C4A@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2014-11-14 22:18:31 +07:00
|
|
|
static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
|
|
|
|
unsigned long start, unsigned long end)
|
|
|
|
{
|
2015-01-09 05:30:21 +07:00
|
|
|
/*
|
|
|
|
* 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);
|
x86, mpx: Cleanup unused bound tables
The previous patch allocates bounds tables on-demand. As noted in
an earlier description, these can add up to *HUGE* amounts of
memory. This has caused OOMs in practice when running tests.
This patch adds support for freeing bounds tables when they are no
longer in use.
There are two types of mappings in play when unmapping tables:
1. The mapping with the actual data, which userspace is
munmap()ing or brk()ing away, etc...
2. The mapping for the bounds table *backing* the data
(is tagged with VM_MPX, see the patch "add MPX specific
mmap interface").
If userspace use the prctl() indroduced earlier in this patchset
to enable the management of bounds tables in kernel, when it
unmaps the first type of mapping with the actual data, the kernel
needs to free the mapping for the bounds table backing the data.
This patch hooks in at the very end of do_unmap() to do so.
We look at the addresses being unmapped and find the bounds
directory entries and tables which cover those addresses. If
an entire table is unused, we clear associated directory entry
and free the table.
Once we unmap the bounds table, we would have a bounds directory
entry pointing at empty address space. That address space might
now be allocated for some other (random) use, and the MPX
hardware might now try to walk it as if it were a bounds table.
That would be bad. So any unmapping of an enture bounds table
has to be accompanied by a corresponding write to the bounds
directory entry to invalidate it. That write to the bounds
directory can fault, which causes the following problem:
Since we are doing the freeing from munmap() (and other paths
like it), we hold mmap_sem for write. If we fault, the page
fault handler will attempt to acquire mmap_sem for read and
we will deadlock. To avoid the deadlock, we pagefault_disable()
when touching the bounds directory entry and use a
get_user_pages() to resolve the fault.
The unmapping of bounds tables happends under vm_munmap(). We
also (indirectly) call vm_munmap() to _do_ the unmapping of the
bounds tables. We avoid unbounded recursion by disallowing
freeing of bounds tables *for* bounds tables. This would not
occur normally, so should not have any practical impact. Being
strict about it here helps ensure that we do not have an
exploitable stack overflow.
Based-on-patch-by: Qiaowei Ren <qiaowei.ren@intel.com>
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-mm@kvack.org
Cc: linux-mips@linux-mips.org
Cc: Dave Hansen <dave@sr71.net>
Link: http://lkml.kernel.org/r/20141114151831.E4531C4A@viggo.jf.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2014-11-14 22:18:31 +07:00
|
|
|
}
|
|
|
|
|
2016-02-13 04:02:10 +07:00
|
|
|
static inline int vma_pkey(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
u16 pkey = 0;
|
|
|
|
#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
|
|
|
|
unsigned long vma_pkey_mask = VM_PKEY_BIT0 | VM_PKEY_BIT1 |
|
|
|
|
VM_PKEY_BIT2 | VM_PKEY_BIT3;
|
|
|
|
pkey = (vma->vm_flags & vma_pkey_mask) >> VM_PKEY_SHIFT;
|
|
|
|
#endif
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
mm/gup, x86/mm/pkeys: Check VMAs and PTEs for protection keys
Today, for normal faults and page table walks, we check the VMA
and/or PTE to ensure that it is compatible with the action. For
instance, if we get a write fault on a non-writeable VMA, we
SIGSEGV.
We try to do the same thing for protection keys. Basically, we
try to make sure that if a user does this:
mprotect(ptr, size, PROT_NONE);
*ptr = foo;
they see the same effects with protection keys when they do this:
mprotect(ptr, size, PROT_READ|PROT_WRITE);
set_pkey(ptr, size, 4);
wrpkru(0xffffff3f); // access disable pkey 4
*ptr = foo;
The state to do that checking is in the VMA, but we also
sometimes have to do it on the page tables only, like when doing
a get_user_pages_fast() where we have no VMA.
We add two functions and expose them to generic code:
arch_pte_access_permitted(pte_flags, write)
arch_vma_access_permitted(vma, write)
These are, of course, backed up in x86 arch code with checks
against the PTE or VMA's protection key.
But, there are also cases where we do not want to respect
protection keys. When we ptrace(), for instance, we do not want
to apply the tracer's PKRU permissions to the PTEs from the
process being traced.
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Alexey Kardashevskiy <aik@ozlabs.ru>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Boaz Harrosh <boaz@plexistor.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Dave Hansen <dave@sr71.net>
Cc: David Gibson <david@gibson.dropbear.id.au>
Cc: David Hildenbrand <dahi@linux.vnet.ibm.com>
Cc: David Vrabel <david.vrabel@citrix.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Dominik Dingel <dingel@linux.vnet.ibm.com>
Cc: Dominik Vogt <vogt@linux.vnet.ibm.com>
Cc: Guan Xuetao <gxt@mprc.pku.edu.cn>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jason Low <jason.low2@hp.com>
Cc: Jerome Marchand <jmarchan@redhat.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Cc: Laurent Dufour <ldufour@linux.vnet.ibm.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Matthew Wilcox <willy@linux.intel.com>
Cc: Mel Gorman <mgorman@suse.de>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mikulas Patocka <mpatocka@redhat.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rik van Riel <riel@redhat.com>
Cc: Sasha Levin <sasha.levin@oracle.com>
Cc: Shachar Raindel <raindel@mellanox.com>
Cc: Stephen Smalley <sds@tycho.nsa.gov>
Cc: Toshi Kani <toshi.kani@hpe.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Cc: linux-arch@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-mm@kvack.org
Cc: linux-s390@vger.kernel.org
Cc: linuxppc-dev@lists.ozlabs.org
Link: http://lkml.kernel.org/r/20160212210219.14D5D715@viggo.jf.intel.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2016-02-13 04:02:19 +07:00
|
|
|
static inline bool __pkru_allows_pkey(u16 pkey, bool write)
|
|
|
|
{
|
|
|
|
u32 pkru = read_pkru();
|
|
|
|
|
|
|
|
if (!__pkru_allows_read(pkru, pkey))
|
|
|
|
return false;
|
|
|
|
if (write && !__pkru_allows_write(pkru, pkey))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only want to enforce protection keys on the current process
|
|
|
|
* because we effectively have no access to PKRU for other
|
|
|
|
* processes or any way to tell *which * PKRU in a threaded
|
|
|
|
* process we could use.
|
|
|
|
*
|
|
|
|
* So do not enforce things if the VMA is not from the current
|
|
|
|
* mm, or if we are in a kernel thread.
|
|
|
|
*/
|
|
|
|
static inline bool vma_is_foreign(struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
if (!current->mm)
|
|
|
|
return true;
|
|
|
|
/*
|
|
|
|
* Should PKRU be enforced on the access to this VMA? If
|
|
|
|
* the VMA is from another process, then PKRU has no
|
|
|
|
* relevance and should not be enforced.
|
|
|
|
*/
|
|
|
|
if (current->mm != vma->vm_mm)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write)
|
|
|
|
{
|
|
|
|
/* allow access if the VMA is not one from this process */
|
|
|
|
if (vma_is_foreign(vma))
|
|
|
|
return true;
|
|
|
|
return __pkru_allows_pkey(vma_pkey(vma), write);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool arch_pte_access_permitted(pte_t pte, bool write)
|
|
|
|
{
|
|
|
|
return __pkru_allows_pkey(pte_flags_pkey(pte_flags(pte)), write);
|
|
|
|
}
|
|
|
|
|
2008-10-23 12:26:29 +07:00
|
|
|
#endif /* _ASM_X86_MMU_CONTEXT_H */
|