mmu_context_nohash.c 12 KB

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