Commit b565b032 authored by Catalin Marinas's avatar Catalin Marinas

Global ASID allocation on SMP

The current ASID allocation algorithm doesn't ensure the notification
of the other CPUs when the ASID rolls over. This may lead to two
processes using the same ASID (but different generation) or multiple
threads of the same process using different ASIDs.

This patch adds the broadcasting of the ASID rollover event to the
other CPUs. To avoid a race on multiple CPUs modifying "cpu_last_asid"
during the handling of the broadcast, the ASID numbering now starts at
"smp_processor_id() + 1". At rollover, the cpu_last_asid will be set
to NR_CPUS.
Signed-off-by: default avatarCatalin Marinas <catalin.marinas@arm.com>
parent 96cf3f17
...@@ -10,12 +10,17 @@ ...@@ -10,12 +10,17 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/smp.h>
#include <linux/percpu.h>
#include <asm/mmu_context.h> #include <asm/mmu_context.h>
#include <asm/tlbflush.h> #include <asm/tlbflush.h>
static DEFINE_SPINLOCK(cpu_asid_lock); static DEFINE_SPINLOCK(cpu_asid_lock);
unsigned int cpu_last_asid = ASID_FIRST_VERSION; unsigned int cpu_last_asid = ASID_FIRST_VERSION;
#ifdef CONFIG_SMP
DEFINE_PER_CPU(struct mm_struct *, current_mm);
#endif
/* /*
* We fork()ed a process, and we need a new context for the child * We fork()ed a process, and we need a new context for the child
...@@ -26,13 +31,98 @@ unsigned int cpu_last_asid = ASID_FIRST_VERSION; ...@@ -26,13 +31,98 @@ unsigned int cpu_last_asid = ASID_FIRST_VERSION;
void __init_new_context(struct task_struct *tsk, struct mm_struct *mm) void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
{ {
mm->context.id = 0; mm->context.id = 0;
spin_lock_init(&mm->context.id_lock);
} }
static void flush_context(void)
{
/* set the reserved ASID before flushing the TLB */
asm("mcr p15, 0, %0, c13, c0, 1\n" : : "r" (0));
isb();
local_flush_tlb_all();
if (icache_is_vivt_asid_tagged()) {
__flush_icache_all();
dsb();
}
}
#ifdef CONFIG_SMP
static void set_mm_context(struct mm_struct *mm, unsigned int asid)
{
/*
* Locking needed for multi-threaded applications where the
* same mm->context.id could be set from different CPUs during
* the broadcast.
*/
spin_lock(&mm->context.id_lock);
if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
/*
* Old version of ASID found. Set the new one and
* reset mm->cpu_vm_mask.
*/
mm->context.id = asid;
cpus_clear(mm->cpu_vm_mask);
}
spin_unlock(&mm->context.id_lock);
/*
* Set the cpu_vm_mask bit for the current CPU.
*/
cpu_set(smp_processor_id(), mm->cpu_vm_mask);
}
/*
* Reset the ASID on the current CPU. This function call is broadcast
* from the CPU handling the ASID rollover and holding cpu_asid_lock.
*/
static void reset_context(void *info)
{
unsigned int asid;
unsigned int cpu = smp_processor_id();
struct mm_struct *mm = per_cpu(current_mm, cpu);
smp_rmb();
asid = cpu_last_asid + cpu + 1;
flush_context();
set_mm_context(mm, asid);
/* set the new ASID */
asm("mcr p15, 0, %0, c13, c0, 1\n" : : "r" (mm->context.id));
}
#else
static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
{
mm->context.id = asid;
mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id());
}
#endif
void __new_context(struct mm_struct *mm) void __new_context(struct mm_struct *mm)
{ {
unsigned int asid; unsigned int asid;
spin_lock(&cpu_asid_lock); spin_lock(&cpu_asid_lock);
#ifdef CONFIG_SMP
/*
* Check the ASID again, in case the change was broadcast from
* another CPU before we acquired the lock.
*/
if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
cpu_set(smp_processor_id(), mm->cpu_vm_mask);
spin_unlock(&cpu_asid_lock);
return;
}
#endif
/*
* At this point, it is guaranteed that the current mm (with
* an old ASID) isn't active on any other CPU since the ASIDs
* are changed simultaneously via IPI.
*/
asid = ++cpu_last_asid; asid = ++cpu_last_asid;
if (asid == 0) if (asid == 0)
asid = cpu_last_asid = ASID_FIRST_VERSION; asid = cpu_last_asid = ASID_FIRST_VERSION;
...@@ -42,23 +132,15 @@ void __new_context(struct mm_struct *mm) ...@@ -42,23 +132,15 @@ void __new_context(struct mm_struct *mm)
* to start a new version and flush the TLB. * to start a new version and flush the TLB.
*/ */
if (unlikely((asid & ~ASID_MASK) == 0)) { if (unlikely((asid & ~ASID_MASK) == 0)) {
asid = ++cpu_last_asid; asid = cpu_last_asid + smp_processor_id() + 1;
/* set the reserved ASID before flushing the TLB */ flush_context();
asm("mcr p15, 0, %0, c13, c0, 1 @ set reserved context ID\n" #ifdef CONFIG_SMP
: smp_wmb();
: "r" (0)); smp_call_function(reset_context, NULL, 1, 1);
isb(); #endif
flush_tlb_all(); cpu_last_asid += NR_CPUS;
if (icache_is_vivt_asid_tagged()) {
asm("mcr p15, 0, %0, c7, c5, 0 @ invalidate I-cache\n"
"mcr p15, 0, %0, c7, c5, 6 @ flush BTAC/BTB\n"
:
: "r" (0));
dsb();
}
} }
spin_unlock(&cpu_asid_lock);
mm->cpu_vm_mask = cpumask_of_cpu(smp_processor_id()); set_mm_context(mm, asid);
mm->context.id = asid; spin_unlock(&cpu_asid_lock);
} }
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
typedef struct { typedef struct {
#ifdef CONFIG_CPU_HAS_ASID #ifdef CONFIG_CPU_HAS_ASID
unsigned int id; unsigned int id;
spinlock_t id_lock;
#endif #endif
unsigned int kvm_seq; unsigned int kvm_seq;
} mm_context_t; } mm_context_t;
......
...@@ -41,12 +41,23 @@ void __check_kvm_seq(struct mm_struct *mm); ...@@ -41,12 +41,23 @@ void __check_kvm_seq(struct mm_struct *mm);
#define ASID_FIRST_VERSION (1 << ASID_BITS) #define ASID_FIRST_VERSION (1 << ASID_BITS)
extern unsigned int cpu_last_asid; extern unsigned int cpu_last_asid;
#ifdef CONFIG_SMP
DECLARE_PER_CPU(struct mm_struct *, current_mm);
#endif
void __init_new_context(struct task_struct *tsk, struct mm_struct *mm); void __init_new_context(struct task_struct *tsk, struct mm_struct *mm);
void __new_context(struct mm_struct *mm); void __new_context(struct mm_struct *mm);
static inline void check_context(struct mm_struct *mm) static inline void check_context(struct mm_struct *mm)
{ {
/*
* This code is executed with interrupts enabled. Therefore,
* mm->context.id cannot be updated to the latest ASID version
* on a different CPU (and condition below not triggered)
* without first getting an IPI to reset the context. The
* alternative is to take a read_lock on mm->context.id_lock
* (after changing its type to rwlock_t).
*/
if (unlikely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) if (unlikely((mm->context.id ^ cpu_last_asid) >> ASID_BITS))
__new_context(mm); __new_context(mm);
...@@ -105,6 +116,10 @@ switch_mm(struct mm_struct *prev, struct mm_struct *next, ...@@ -105,6 +116,10 @@ switch_mm(struct mm_struct *prev, struct mm_struct *next,
__flush_icache_all(); __flush_icache_all();
#endif #endif
if (!cpu_test_and_set(cpu, next->cpu_vm_mask) || prev != next) { if (!cpu_test_and_set(cpu, next->cpu_vm_mask) || prev != next) {
#ifdef CONFIG_SMP
struct mm_struct **crt_mm = &per_cpu(current_mm, cpu);
*crt_mm = next;
#endif
check_context(next); check_context(next);
cpu_switch_mm(next->pgd, next); cpu_switch_mm(next->pgd, next);
if (cache_is_vivt()) if (cache_is_vivt())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment