mmu_context_nohash.c 13 KB

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