mmu_context_nohash.c 13 KB

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