powerpc/mm: Fix some SMP issues with MMU context handling
[deliverable/linux.git] / arch / powerpc / mm / mmu_context_nohash.c
CommitLineData
5e696617
BH
1/*
2 * This file contains the routines for handling the MMU on those
3 * PowerPC implementations where the MMU is not using the hash
4 * table, such as 8xx, 4xx, BookE's etc...
5 *
6 * Copyright 2008 Ben Herrenschmidt <benh@kernel.crashing.org>
7 * IBM Corp.
8 *
9 * Derived from previous arch/powerpc/mm/mmu_context.c
10 * and arch/powerpc/include/asm/mmu_context.h
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
2ca8cf73
BH
17 * TODO:
18 *
19 * - The global context lock will not scale very well
20 * - The maps should be dynamically allocated to allow for processors
21 * that support more PID bits at runtime
22 * - Implement flush_tlb_mm() by making the context stale and picking
23 * a new one
24 * - More aggressively clear stale map bits and maybe find some way to
25 * also clear mm->cpu_vm_mask bits when processes are migrated
5e696617
BH
26 */
27
2ca8cf73
BH
28#undef DEBUG
29#define DEBUG_STEAL_ONLY
30#undef DEBUG_MAP_CONSISTENCY
77520351 31/*#define DEBUG_CLAMP_LAST_CONTEXT 15 */
2ca8cf73
BH
32
33#include <linux/kernel.h>
5e696617
BH
34#include <linux/mm.h>
35#include <linux/init.h>
77520351
BH
36#include <linux/spinlock.h>
37#include <linux/bootmem.h>
38#include <linux/notifier.h>
39#include <linux/cpu.h>
5e696617
BH
40
41#include <asm/mmu_context.h>
42#include <asm/tlbflush.h>
5e696617 43
77520351 44static unsigned int first_context, last_context;
2ca8cf73 45static unsigned int next_context, nr_free_contexts;
77520351
BH
46static unsigned long *context_map;
47static unsigned long *stale_map[NR_CPUS];
48static struct mm_struct **context_mm;
2ca8cf73 49static spinlock_t context_lock = SPIN_LOCK_UNLOCKED;
5e696617 50
77520351
BH
51#define CTX_MAP_SIZE \
52 (sizeof(unsigned long) * (last_context / BITS_PER_LONG + 1))
53
54
5e696617 55/* Steal a context from a task that has one at the moment.
2ca8cf73
BH
56 *
57 * This is used when we are running out of available PID numbers
58 * on the processors.
59 *
5e696617
BH
60 * This isn't an LRU system, it just frees up each context in
61 * turn (sort-of pseudo-random replacement :). This would be the
62 * place to implement an LRU scheme if anyone was motivated to do it.
63 * -- paulus
2ca8cf73
BH
64 *
65 * For context stealing, we use a slightly different approach for
66 * SMP and UP. Basically, the UP one is simpler and doesn't use
67 * the stale map as we can just flush the local CPU
68 * -- benh
5e696617 69 */
2ca8cf73
BH
70#ifdef CONFIG_SMP
71static unsigned int steal_context_smp(unsigned int id)
5e696617
BH
72{
73 struct mm_struct *mm;
2ca8cf73 74 unsigned int cpu, max;
5e696617 75
77520351 76 max = last_context - first_context;
5e696617 77
2ca8cf73
BH
78 /* Attempt to free next_context first and then loop until we manage */
79 while (max--) {
80 /* Pick up the victim mm */
81 mm = context_mm[id];
5e696617 82
2ca8cf73
BH
83 /* We have a candidate victim, check if it's active, on SMP
84 * we cannot steal active contexts
85 */
86 if (mm->context.active) {
87 id++;
77520351
BH
88 if (id > last_context)
89 id = first_context;
2ca8cf73
BH
90 continue;
91 }
92 pr_debug("[%d] steal context %d from mm @%p\n",
93 smp_processor_id(), id, mm);
94
95 /* Mark this mm has having no context anymore */
96 mm->context.id = MMU_NO_CONTEXT;
97
98 /* Mark it stale on all CPUs that used this mm */
56aa4129 99 for_each_cpu(cpu, mm_cpumask(mm))
2ca8cf73
BH
100 __set_bit(id, stale_map[cpu]);
101 return id;
102 }
103
104 /* This will happen if you have more CPUs than available contexts,
105 * all we can do here is wait a bit and try again
106 */
107 spin_unlock(&context_lock);
108 cpu_relax();
109 spin_lock(&context_lock);
3035c863
BH
110
111 /* This will cause the caller to try again */
112 return MMU_NO_CONTEXT;
2ca8cf73
BH
113}
114#endif /* CONFIG_SMP */
115
116/* Note that this will also be called on SMP if all other CPUs are
117 * offlined, which means that it may be called for cpu != 0. For
118 * this to work, we somewhat assume that CPUs that are onlined
119 * come up with a fully clean TLB (or are cleaned when offlined)
5e696617 120 */
2ca8cf73 121static unsigned int steal_context_up(unsigned int id)
5e696617 122{
2ca8cf73
BH
123 struct mm_struct *mm;
124 int cpu = smp_processor_id();
5e696617 125
2ca8cf73
BH
126 /* Pick up the victim mm */
127 mm = context_mm[id];
128
129 pr_debug("[%d] steal context %d from mm @%p\n", cpu, id, mm);
5e696617 130
2ca8cf73
BH
131 /* Flush the TLB for that context */
132 local_flush_tlb_mm(mm);
133
8e35961b
HS
134 /* Mark this mm has having no context anymore */
135 mm->context.id = MMU_NO_CONTEXT;
136
2ca8cf73
BH
137 /* XXX This clear should ultimately be part of local_flush_tlb_mm */
138 __clear_bit(id, stale_map[cpu]);
139
140 return id;
141}
142
143#ifdef DEBUG_MAP_CONSISTENCY
144static void context_check_map(void)
145{
146 unsigned int id, nrf, nact;
147
148 nrf = nact = 0;
77520351 149 for (id = first_context; id <= last_context; id++) {
2ca8cf73
BH
150 int used = test_bit(id, context_map);
151 if (!used)
152 nrf++;
153 if (used != (context_mm[id] != NULL))
154 pr_err("MMU: Context %d is %s and MM is %p !\n",
155 id, used ? "used" : "free", context_mm[id]);
156 if (context_mm[id] != NULL)
157 nact += context_mm[id]->context.active;
5e696617 158 }
2ca8cf73
BH
159 if (nrf != nr_free_contexts) {
160 pr_err("MMU: Free context count out of sync ! (%d vs %d)\n",
161 nr_free_contexts, nrf);
162 nr_free_contexts = nrf;
163 }
164 if (nact > num_online_cpus())
165 pr_err("MMU: More active contexts than CPUs ! (%d vs %d)\n",
166 nact, num_online_cpus());
77520351
BH
167 if (first_context > 0 && !test_bit(0, context_map))
168 pr_err("MMU: Context 0 has been freed !!!\n");
5e696617 169}
2ca8cf73
BH
170#else
171static void context_check_map(void) { }
172#endif
5e696617
BH
173
174void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
175{
2ca8cf73
BH
176 unsigned int id, cpu = smp_processor_id();
177 unsigned long *map;
5e696617 178
2ca8cf73
BH
179 /* No lockless fast path .. yet */
180 spin_lock(&context_lock);
181
182#ifndef DEBUG_STEAL_ONLY
183 pr_debug("[%d] activating context for mm @%p, active=%d, id=%d\n",
184 cpu, next, next->context.active, next->context.id);
185#endif
186
187#ifdef CONFIG_SMP
188 /* Mark us active and the previous one not anymore */
189 next->context.active++;
190 if (prev) {
77520351
BH
191#ifndef DEBUG_STEAL_ONLY
192 pr_debug(" old context %p active was: %d\n",
193 prev, prev->context.active);
194#endif
2ca8cf73
BH
195 WARN_ON(prev->context.active < 1);
196 prev->context.active--;
197 }
3035c863
BH
198
199 again:
2ca8cf73
BH
200#endif /* CONFIG_SMP */
201
202 /* If we already have a valid assigned context, skip all that */
203 id = next->context.id;
204 if (likely(id != MMU_NO_CONTEXT))
205 goto ctxt_ok;
206
207 /* We really don't have a context, let's try to acquire one */
208 id = next_context;
77520351
BH
209 if (id > last_context)
210 id = first_context;
2ca8cf73
BH
211 map = context_map;
212
213 /* No more free contexts, let's try to steal one */
214 if (nr_free_contexts == 0) {
215#ifdef CONFIG_SMP
216 if (num_online_cpus() > 1) {
217 id = steal_context_smp(id);
3035c863
BH
218 if (id == MMU_NO_CONTEXT)
219 goto again;
2ca8cf73
BH
220 }
221#endif /* CONFIG_SMP */
222 id = steal_context_up(id);
223 goto stolen;
224 }
225 nr_free_contexts--;
226
227 /* We know there's at least one free context, try to find it */
228 while (__test_and_set_bit(id, map)) {
77520351
BH
229 id = find_next_zero_bit(map, last_context+1, id);
230 if (id > last_context)
231 id = first_context;
2ca8cf73
BH
232 }
233 stolen:
234 next_context = id + 1;
235 context_mm[id] = next;
236 next->context.id = id;
237
238#ifndef DEBUG_STEAL_ONLY
239 pr_debug("[%d] picked up new id %d, nrf is now %d\n",
240 cpu, id, nr_free_contexts);
241#endif
242
243 context_check_map();
244 ctxt_ok:
245
246 /* If that context got marked stale on this CPU, then flush the
247 * local TLB for it and unmark it before we use it
248 */
249 if (test_bit(id, stale_map[cpu])) {
250 pr_debug("[%d] flushing stale context %d for mm @%p !\n",
251 cpu, id, next);
252 local_flush_tlb_mm(next);
253
254 /* XXX This clear should ultimately be part of local_flush_tlb_mm */
255 __clear_bit(id, stale_map[cpu]);
256 }
257
258 /* Flick the MMU and release lock */
259 set_context(id, next->pgd);
260 spin_unlock(&context_lock);
5e696617
BH
261}
262
263/*
264 * Set up the context for a new address space.
265 */
266int init_new_context(struct task_struct *t, struct mm_struct *mm)
267{
2ca8cf73
BH
268 mm->context.id = MMU_NO_CONTEXT;
269 mm->context.active = 0;
270
5e696617
BH
271 return 0;
272}
273
274/*
275 * We're finished using the context for an address space.
276 */
277void destroy_context(struct mm_struct *mm)
278{
2ca8cf73
BH
279 unsigned int id;
280
281 if (mm->context.id == MMU_NO_CONTEXT)
282 return;
283
284 WARN_ON(mm->context.active != 0);
285
286 spin_lock(&context_lock);
287 id = mm->context.id;
288 if (id != MMU_NO_CONTEXT) {
289 __clear_bit(id, context_map);
290 mm->context.id = MMU_NO_CONTEXT;
291#ifdef DEBUG_MAP_CONSISTENCY
292 mm->context.active = 0;
2ca8cf73 293#endif
3035c863 294 context_mm[id] = NULL;
2ca8cf73 295 nr_free_contexts++;
5e696617 296 }
2ca8cf73 297 spin_unlock(&context_lock);
5e696617
BH
298}
299
77520351
BH
300#ifdef CONFIG_SMP
301
302static int __cpuinit mmu_context_cpu_notify(struct notifier_block *self,
303 unsigned long action, void *hcpu)
304{
305 unsigned int cpu = (unsigned int)(long)hcpu;
306
307 /* We don't touch CPU 0 map, it's allocated at aboot and kept
308 * around forever
309 */
310 if (cpu == 0)
311 return NOTIFY_OK;
312
313 switch (action) {
314 case CPU_ONLINE:
315 case CPU_ONLINE_FROZEN:
316 pr_debug("MMU: Allocating stale context map for CPU %d\n", cpu);
317 stale_map[cpu] = kzalloc(CTX_MAP_SIZE, GFP_KERNEL);
318 break;
319#ifdef CONFIG_HOTPLUG_CPU
320 case CPU_DEAD:
321 case CPU_DEAD_FROZEN:
322 pr_debug("MMU: Freeing stale context map for CPU %d\n", cpu);
323 kfree(stale_map[cpu]);
324 stale_map[cpu] = NULL;
325 break;
326#endif
327 }
328 return NOTIFY_OK;
329}
330
331static struct notifier_block __cpuinitdata mmu_context_cpu_nb = {
332 .notifier_call = mmu_context_cpu_notify,
333};
334
335#endif /* CONFIG_SMP */
5e696617
BH
336
337/*
338 * Initialize the context management stuff.
339 */
340void __init mmu_context_init(void)
341{
2ca8cf73
BH
342 /* Mark init_mm as being active on all possible CPUs since
343 * we'll get called with prev == init_mm the first time
344 * we schedule on a given CPU
345 */
346 init_mm.context.active = NR_CPUS;
347
77520351
BH
348 /*
349 * The MPC8xx has only 16 contexts. We rotate through them on each
350 * task switch. A better way would be to keep track of tasks that
351 * own contexts, and implement an LRU usage. That way very active
352 * tasks don't always have to pay the TLB reload overhead. The
353 * kernel pages are mapped shared, so the kernel can run on behalf
354 * of any task that makes a kernel entry. Shared does not mean they
355 * are not protected, just that the ASID comparison is not performed.
356 * -- Dan
357 *
358 * The IBM4xx has 256 contexts, so we can just rotate through these
359 * as a way of "switching" contexts. If the TID of the TLB is zero,
360 * the PID/TID comparison is disabled, so we can use a TID of zero
361 * to represent all kernel pages as shared among all contexts.
362 * -- Dan
363 */
364 if (mmu_has_feature(MMU_FTR_TYPE_8xx)) {
365 first_context = 0;
366 last_context = 15;
367 } else {
368 first_context = 1;
369 last_context = 255;
370 }
371
372#ifdef DEBUG_CLAMP_LAST_CONTEXT
373 last_context = DEBUG_CLAMP_LAST_CONTEXT;
374#endif
375 /*
376 * Allocate the maps used by context management
377 */
378 context_map = alloc_bootmem(CTX_MAP_SIZE);
379 context_mm = alloc_bootmem(sizeof(void *) * (last_context + 1));
380 stale_map[0] = alloc_bootmem(CTX_MAP_SIZE);
381
382#ifdef CONFIG_SMP
383 register_cpu_notifier(&mmu_context_cpu_nb);
384#endif
385
386 printk(KERN_INFO
ff7c6600 387 "MMU: Allocated %zu bytes of context maps for %d contexts\n",
77520351
BH
388 2 * CTX_MAP_SIZE + (sizeof(void *) * (last_context + 1)),
389 last_context - first_context + 1);
390
5e696617
BH
391 /*
392 * Some processors have too few contexts to reserve one for
393 * init_mm, and require using context 0 for a normal task.
394 * Other processors reserve the use of context zero for the kernel.
77520351 395 * This code assumes first_context < 32.
5e696617 396 */
77520351
BH
397 context_map[0] = (1 << first_context) - 1;
398 next_context = first_context;
399 nr_free_contexts = last_context - first_context + 1;
5e696617
BH
400}
401
This page took 0.072569 seconds and 5 git commands to generate.