Path: blob/master/arch/powerpc/mm/mmu_context_nohash.c
10817 views
/*1* This file contains the routines for handling the MMU on those2* PowerPC implementations where the MMU is not using the hash3* table, such as 8xx, 4xx, BookE's etc...4*5* Copyright 2008 Ben Herrenschmidt <[email protected]>6* IBM Corp.7*8* Derived from previous arch/powerpc/mm/mmu_context.c9* and arch/powerpc/include/asm/mmu_context.h10*11* This program is free software; you can redistribute it and/or12* modify it under the terms of the GNU General Public License13* as published by the Free Software Foundation; either version14* 2 of the License, or (at your option) any later version.15*16* TODO:17*18* - The global context lock will not scale very well19* - The maps should be dynamically allocated to allow for processors20* that support more PID bits at runtime21* - Implement flush_tlb_mm() by making the context stale and picking22* a new one23* - More aggressively clear stale map bits and maybe find some way to24* also clear mm->cpu_vm_mask bits when processes are migrated25*/2627//#define DEBUG_MAP_CONSISTENCY28//#define DEBUG_CLAMP_LAST_CONTEXT 3129//#define DEBUG_HARDER3031/* We don't use DEBUG because it tends to be compiled in always nowadays32* and this would generate way too much output33*/34#ifdef DEBUG_HARDER35#define pr_hard(args...) printk(KERN_DEBUG args)36#define pr_hardcont(args...) printk(KERN_CONT args)37#else38#define pr_hard(args...) do { } while(0)39#define pr_hardcont(args...) do { } while(0)40#endif4142#include <linux/kernel.h>43#include <linux/mm.h>44#include <linux/init.h>45#include <linux/spinlock.h>46#include <linux/bootmem.h>47#include <linux/notifier.h>48#include <linux/cpu.h>49#include <linux/slab.h>5051#include <asm/mmu_context.h>52#include <asm/tlbflush.h>5354static unsigned int first_context, last_context;55static unsigned int next_context, nr_free_contexts;56static unsigned long *context_map;57static unsigned long *stale_map[NR_CPUS];58static struct mm_struct **context_mm;59static DEFINE_RAW_SPINLOCK(context_lock);6061#define CTX_MAP_SIZE \62(sizeof(unsigned long) * (last_context / BITS_PER_LONG + 1))636465/* Steal a context from a task that has one at the moment.66*67* This is used when we are running out of available PID numbers68* on the processors.69*70* This isn't an LRU system, it just frees up each context in71* turn (sort-of pseudo-random replacement :). This would be the72* place to implement an LRU scheme if anyone was motivated to do it.73* -- paulus74*75* For context stealing, we use a slightly different approach for76* SMP and UP. Basically, the UP one is simpler and doesn't use77* the stale map as we can just flush the local CPU78* -- benh79*/80#ifdef CONFIG_SMP81static unsigned int steal_context_smp(unsigned int id)82{83struct mm_struct *mm;84unsigned int cpu, max, i;8586max = last_context - first_context;8788/* Attempt to free next_context first and then loop until we manage */89while (max--) {90/* Pick up the victim mm */91mm = context_mm[id];9293/* We have a candidate victim, check if it's active, on SMP94* we cannot steal active contexts95*/96if (mm->context.active) {97id++;98if (id > last_context)99id = first_context;100continue;101}102pr_hardcont(" | steal %d from 0x%p", id, mm);103104/* Mark this mm has having no context anymore */105mm->context.id = MMU_NO_CONTEXT;106107/* Mark it stale on all CPUs that used this mm. For threaded108* implementations, we set it on all threads on each core109* represented in the mask. A future implementation will use110* a core map instead but this will do for now.111*/112for_each_cpu(cpu, mm_cpumask(mm)) {113for (i = cpu_first_thread_sibling(cpu);114i <= cpu_last_thread_sibling(cpu); i++)115__set_bit(id, stale_map[i]);116cpu = i - 1;117}118return id;119}120121/* This will happen if you have more CPUs than available contexts,122* all we can do here is wait a bit and try again123*/124raw_spin_unlock(&context_lock);125cpu_relax();126raw_spin_lock(&context_lock);127128/* This will cause the caller to try again */129return MMU_NO_CONTEXT;130}131#endif /* CONFIG_SMP */132133/* Note that this will also be called on SMP if all other CPUs are134* offlined, which means that it may be called for cpu != 0. For135* this to work, we somewhat assume that CPUs that are onlined136* come up with a fully clean TLB (or are cleaned when offlined)137*/138static unsigned int steal_context_up(unsigned int id)139{140struct mm_struct *mm;141int cpu = smp_processor_id();142143/* Pick up the victim mm */144mm = context_mm[id];145146pr_hardcont(" | steal %d from 0x%p", id, mm);147148/* Flush the TLB for that context */149local_flush_tlb_mm(mm);150151/* Mark this mm has having no context anymore */152mm->context.id = MMU_NO_CONTEXT;153154/* XXX This clear should ultimately be part of local_flush_tlb_mm */155__clear_bit(id, stale_map[cpu]);156157return id;158}159160#ifdef DEBUG_MAP_CONSISTENCY161static void context_check_map(void)162{163unsigned int id, nrf, nact;164165nrf = nact = 0;166for (id = first_context; id <= last_context; id++) {167int used = test_bit(id, context_map);168if (!used)169nrf++;170if (used != (context_mm[id] != NULL))171pr_err("MMU: Context %d is %s and MM is %p !\n",172id, used ? "used" : "free", context_mm[id]);173if (context_mm[id] != NULL)174nact += context_mm[id]->context.active;175}176if (nrf != nr_free_contexts) {177pr_err("MMU: Free context count out of sync ! (%d vs %d)\n",178nr_free_contexts, nrf);179nr_free_contexts = nrf;180}181if (nact > num_online_cpus())182pr_err("MMU: More active contexts than CPUs ! (%d vs %d)\n",183nact, num_online_cpus());184if (first_context > 0 && !test_bit(0, context_map))185pr_err("MMU: Context 0 has been freed !!!\n");186}187#else188static void context_check_map(void) { }189#endif190191void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)192{193unsigned int i, id, cpu = smp_processor_id();194unsigned long *map;195196/* No lockless fast path .. yet */197raw_spin_lock(&context_lock);198199pr_hard("[%d] activating context for mm @%p, active=%d, id=%d",200cpu, next, next->context.active, next->context.id);201202#ifdef CONFIG_SMP203/* Mark us active and the previous one not anymore */204next->context.active++;205if (prev) {206pr_hardcont(" (old=0x%p a=%d)", prev, prev->context.active);207WARN_ON(prev->context.active < 1);208prev->context.active--;209}210211again:212#endif /* CONFIG_SMP */213214/* If we already have a valid assigned context, skip all that */215id = next->context.id;216if (likely(id != MMU_NO_CONTEXT)) {217#ifdef DEBUG_MAP_CONSISTENCY218if (context_mm[id] != next)219pr_err("MMU: mm 0x%p has id %d but context_mm[%d] says 0x%p\n",220next, id, id, context_mm[id]);221#endif222goto ctxt_ok;223}224225/* We really don't have a context, let's try to acquire one */226id = next_context;227if (id > last_context)228id = first_context;229map = context_map;230231/* No more free contexts, let's try to steal one */232if (nr_free_contexts == 0) {233#ifdef CONFIG_SMP234if (num_online_cpus() > 1) {235id = steal_context_smp(id);236if (id == MMU_NO_CONTEXT)237goto again;238goto stolen;239}240#endif /* CONFIG_SMP */241id = steal_context_up(id);242goto stolen;243}244nr_free_contexts--;245246/* We know there's at least one free context, try to find it */247while (__test_and_set_bit(id, map)) {248id = find_next_zero_bit(map, last_context+1, id);249if (id > last_context)250id = first_context;251}252stolen:253next_context = id + 1;254context_mm[id] = next;255next->context.id = id;256pr_hardcont(" | new id=%d,nrf=%d", id, nr_free_contexts);257258context_check_map();259ctxt_ok:260261/* If that context got marked stale on this CPU, then flush the262* local TLB for it and unmark it before we use it263*/264if (test_bit(id, stale_map[cpu])) {265pr_hardcont(" | stale flush %d [%d..%d]",266id, cpu_first_thread_sibling(cpu),267cpu_last_thread_sibling(cpu));268269local_flush_tlb_mm(next);270271/* XXX This clear should ultimately be part of local_flush_tlb_mm */272for (i = cpu_first_thread_sibling(cpu);273i <= cpu_last_thread_sibling(cpu); i++) {274__clear_bit(id, stale_map[i]);275}276}277278/* Flick the MMU and release lock */279pr_hardcont(" -> %d\n", id);280set_context(id, next->pgd);281raw_spin_unlock(&context_lock);282}283284/*285* Set up the context for a new address space.286*/287int init_new_context(struct task_struct *t, struct mm_struct *mm)288{289pr_hard("initing context for mm @%p\n", mm);290291mm->context.id = MMU_NO_CONTEXT;292mm->context.active = 0;293294return 0;295}296297/*298* We're finished using the context for an address space.299*/300void destroy_context(struct mm_struct *mm)301{302unsigned long flags;303unsigned int id;304305if (mm->context.id == MMU_NO_CONTEXT)306return;307308WARN_ON(mm->context.active != 0);309310raw_spin_lock_irqsave(&context_lock, flags);311id = mm->context.id;312if (id != MMU_NO_CONTEXT) {313__clear_bit(id, context_map);314mm->context.id = MMU_NO_CONTEXT;315#ifdef DEBUG_MAP_CONSISTENCY316mm->context.active = 0;317#endif318context_mm[id] = NULL;319nr_free_contexts++;320}321raw_spin_unlock_irqrestore(&context_lock, flags);322}323324#ifdef CONFIG_SMP325326static int __cpuinit mmu_context_cpu_notify(struct notifier_block *self,327unsigned long action, void *hcpu)328{329unsigned int cpu = (unsigned int)(long)hcpu;330#ifdef CONFIG_HOTPLUG_CPU331struct task_struct *p;332#endif333/* We don't touch CPU 0 map, it's allocated at aboot and kept334* around forever335*/336if (cpu == boot_cpuid)337return NOTIFY_OK;338339switch (action) {340case CPU_UP_PREPARE:341case CPU_UP_PREPARE_FROZEN:342pr_devel("MMU: Allocating stale context map for CPU %d\n", cpu);343stale_map[cpu] = kzalloc(CTX_MAP_SIZE, GFP_KERNEL);344break;345#ifdef CONFIG_HOTPLUG_CPU346case CPU_UP_CANCELED:347case CPU_UP_CANCELED_FROZEN:348case CPU_DEAD:349case CPU_DEAD_FROZEN:350pr_devel("MMU: Freeing stale context map for CPU %d\n", cpu);351kfree(stale_map[cpu]);352stale_map[cpu] = NULL;353354/* We also clear the cpu_vm_mask bits of CPUs going away */355read_lock(&tasklist_lock);356for_each_process(p) {357if (p->mm)358cpumask_clear_cpu(cpu, mm_cpumask(p->mm));359}360read_unlock(&tasklist_lock);361break;362#endif /* CONFIG_HOTPLUG_CPU */363}364return NOTIFY_OK;365}366367static struct notifier_block __cpuinitdata mmu_context_cpu_nb = {368.notifier_call = mmu_context_cpu_notify,369};370371#endif /* CONFIG_SMP */372373/*374* Initialize the context management stuff.375*/376void __init mmu_context_init(void)377{378/* Mark init_mm as being active on all possible CPUs since379* we'll get called with prev == init_mm the first time380* we schedule on a given CPU381*/382init_mm.context.active = NR_CPUS;383384/*385* The MPC8xx has only 16 contexts. We rotate through them on each386* task switch. A better way would be to keep track of tasks that387* own contexts, and implement an LRU usage. That way very active388* tasks don't always have to pay the TLB reload overhead. The389* kernel pages are mapped shared, so the kernel can run on behalf390* of any task that makes a kernel entry. Shared does not mean they391* are not protected, just that the ASID comparison is not performed.392* -- Dan393*394* The IBM4xx has 256 contexts, so we can just rotate through these395* as a way of "switching" contexts. If the TID of the TLB is zero,396* the PID/TID comparison is disabled, so we can use a TID of zero397* to represent all kernel pages as shared among all contexts.398* -- Dan399*400* The IBM 47x core supports 16-bit PIDs, thus 65535 contexts. We401* should normally never have to steal though the facility is402* present if needed.403* -- BenH404*/405if (mmu_has_feature(MMU_FTR_TYPE_8xx)) {406first_context = 0;407last_context = 15;408} else if (mmu_has_feature(MMU_FTR_TYPE_47x)) {409first_context = 1;410last_context = 65535;411} else412#ifdef CONFIG_PPC_BOOK3E_MMU413if (mmu_has_feature(MMU_FTR_TYPE_3E)) {414u32 mmucfg = mfspr(SPRN_MMUCFG);415u32 pid_bits = (mmucfg & MMUCFG_PIDSIZE_MASK)416>> MMUCFG_PIDSIZE_SHIFT;417first_context = 1;418last_context = (1UL << (pid_bits + 1)) - 1;419} else420#endif421{422first_context = 1;423last_context = 255;424}425426#ifdef DEBUG_CLAMP_LAST_CONTEXT427last_context = DEBUG_CLAMP_LAST_CONTEXT;428#endif429/*430* Allocate the maps used by context management431*/432context_map = alloc_bootmem(CTX_MAP_SIZE);433context_mm = alloc_bootmem(sizeof(void *) * (last_context + 1));434#ifndef CONFIG_SMP435stale_map[0] = alloc_bootmem(CTX_MAP_SIZE);436#else437stale_map[boot_cpuid] = alloc_bootmem(CTX_MAP_SIZE);438439register_cpu_notifier(&mmu_context_cpu_nb);440#endif441442printk(KERN_INFO443"MMU: Allocated %zu bytes of context maps for %d contexts\n",4442 * CTX_MAP_SIZE + (sizeof(void *) * (last_context + 1)),445last_context - first_context + 1);446447/*448* Some processors have too few contexts to reserve one for449* init_mm, and require using context 0 for a normal task.450* Other processors reserve the use of context zero for the kernel.451* This code assumes first_context < 32.452*/453context_map[0] = (1 << first_context) - 1;454next_context = first_context;455nr_free_contexts = last_context - first_context + 1;456}457458459460