mmu_context_nohash.c 11 KB

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