mmu_context.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MMU context allocation for 64-bit kernels.
  4. *
  5. * Copyright (C) 2004 Anton Blanchard, IBM Corp. <[email protected]>
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/kernel.h>
  9. #include <linux/errno.h>
  10. #include <linux/string.h>
  11. #include <linux/types.h>
  12. #include <linux/mm.h>
  13. #include <linux/pkeys.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/idr.h>
  16. #include <linux/export.h>
  17. #include <linux/gfp.h>
  18. #include <linux/slab.h>
  19. #include <linux/cpu.h>
  20. #include <asm/mmu_context.h>
  21. #include <asm/pgalloc.h>
  22. #include "internal.h"
  23. static DEFINE_IDA(mmu_context_ida);
  24. static int alloc_context_id(int min_id, int max_id)
  25. {
  26. return ida_alloc_range(&mmu_context_ida, min_id, max_id, GFP_KERNEL);
  27. }
  28. #ifdef CONFIG_PPC_64S_HASH_MMU
  29. void __init hash__reserve_context_id(int id)
  30. {
  31. int result = ida_alloc_range(&mmu_context_ida, id, id, GFP_KERNEL);
  32. WARN(result != id, "mmu: Failed to reserve context id %d (rc %d)\n", id, result);
  33. }
  34. int hash__alloc_context_id(void)
  35. {
  36. unsigned long max;
  37. if (mmu_has_feature(MMU_FTR_68_BIT_VA))
  38. max = MAX_USER_CONTEXT;
  39. else
  40. max = MAX_USER_CONTEXT_65BIT_VA;
  41. return alloc_context_id(MIN_USER_CONTEXT, max);
  42. }
  43. EXPORT_SYMBOL_GPL(hash__alloc_context_id);
  44. #endif
  45. #ifdef CONFIG_PPC_64S_HASH_MMU
  46. static int realloc_context_ids(mm_context_t *ctx)
  47. {
  48. int i, id;
  49. /*
  50. * id 0 (aka. ctx->id) is special, we always allocate a new one, even if
  51. * there wasn't one allocated previously (which happens in the exec
  52. * case where ctx is newly allocated).
  53. *
  54. * We have to be a bit careful here. We must keep the existing ids in
  55. * the array, so that we can test if they're non-zero to decide if we
  56. * need to allocate a new one. However in case of error we must free the
  57. * ids we've allocated but *not* any of the existing ones (or risk a
  58. * UAF). That's why we decrement i at the start of the error handling
  59. * loop, to skip the id that we just tested but couldn't reallocate.
  60. */
  61. for (i = 0; i < ARRAY_SIZE(ctx->extended_id); i++) {
  62. if (i == 0 || ctx->extended_id[i]) {
  63. id = hash__alloc_context_id();
  64. if (id < 0)
  65. goto error;
  66. ctx->extended_id[i] = id;
  67. }
  68. }
  69. /* The caller expects us to return id */
  70. return ctx->id;
  71. error:
  72. for (i--; i >= 0; i--) {
  73. if (ctx->extended_id[i])
  74. ida_free(&mmu_context_ida, ctx->extended_id[i]);
  75. }
  76. return id;
  77. }
  78. static int hash__init_new_context(struct mm_struct *mm)
  79. {
  80. int index;
  81. mm->context.hash_context = kmalloc(sizeof(struct hash_mm_context),
  82. GFP_KERNEL);
  83. if (!mm->context.hash_context)
  84. return -ENOMEM;
  85. /*
  86. * The old code would re-promote on fork, we don't do that when using
  87. * slices as it could cause problem promoting slices that have been
  88. * forced down to 4K.
  89. *
  90. * For book3s we have MMU_NO_CONTEXT set to be ~0. Hence check
  91. * explicitly against context.id == 0. This ensures that we properly
  92. * initialize context slice details for newly allocated mm's (which will
  93. * have id == 0) and don't alter context slice inherited via fork (which
  94. * will have id != 0).
  95. *
  96. * We should not be calling init_new_context() on init_mm. Hence a
  97. * check against 0 is OK.
  98. */
  99. if (mm->context.id == 0) {
  100. memset(mm->context.hash_context, 0, sizeof(struct hash_mm_context));
  101. slice_init_new_context_exec(mm);
  102. } else {
  103. /* This is fork. Copy hash_context details from current->mm */
  104. memcpy(mm->context.hash_context, current->mm->context.hash_context, sizeof(struct hash_mm_context));
  105. #ifdef CONFIG_PPC_SUBPAGE_PROT
  106. /* inherit subpage prot details if we have one. */
  107. if (current->mm->context.hash_context->spt) {
  108. mm->context.hash_context->spt = kmalloc(sizeof(struct subpage_prot_table),
  109. GFP_KERNEL);
  110. if (!mm->context.hash_context->spt) {
  111. kfree(mm->context.hash_context);
  112. return -ENOMEM;
  113. }
  114. }
  115. #endif
  116. }
  117. index = realloc_context_ids(&mm->context);
  118. if (index < 0) {
  119. #ifdef CONFIG_PPC_SUBPAGE_PROT
  120. kfree(mm->context.hash_context->spt);
  121. #endif
  122. kfree(mm->context.hash_context);
  123. return index;
  124. }
  125. pkey_mm_init(mm);
  126. return index;
  127. }
  128. void hash__setup_new_exec(void)
  129. {
  130. slice_setup_new_exec();
  131. slb_setup_new_exec();
  132. }
  133. #else
  134. static inline int hash__init_new_context(struct mm_struct *mm)
  135. {
  136. BUILD_BUG();
  137. return 0;
  138. }
  139. #endif
  140. static int radix__init_new_context(struct mm_struct *mm)
  141. {
  142. unsigned long rts_field;
  143. int index, max_id;
  144. max_id = (1 << mmu_pid_bits) - 1;
  145. index = alloc_context_id(mmu_base_pid, max_id);
  146. if (index < 0)
  147. return index;
  148. /*
  149. * set the process table entry,
  150. */
  151. rts_field = radix__get_tree_size();
  152. process_tb[index].prtb0 = cpu_to_be64(rts_field | __pa(mm->pgd) | RADIX_PGD_INDEX_SIZE);
  153. /*
  154. * Order the above store with subsequent update of the PID
  155. * register (at which point HW can start loading/caching
  156. * the entry) and the corresponding load by the MMU from
  157. * the L2 cache.
  158. */
  159. asm volatile("ptesync;isync" : : : "memory");
  160. #ifdef CONFIG_PPC_64S_HASH_MMU
  161. mm->context.hash_context = NULL;
  162. #endif
  163. return index;
  164. }
  165. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  166. {
  167. int index;
  168. if (radix_enabled())
  169. index = radix__init_new_context(mm);
  170. else
  171. index = hash__init_new_context(mm);
  172. if (index < 0)
  173. return index;
  174. mm->context.id = index;
  175. mm->context.pte_frag = NULL;
  176. mm->context.pmd_frag = NULL;
  177. #ifdef CONFIG_SPAPR_TCE_IOMMU
  178. mm_iommu_init(mm);
  179. #endif
  180. atomic_set(&mm->context.active_cpus, 0);
  181. atomic_set(&mm->context.copros, 0);
  182. return 0;
  183. }
  184. void __destroy_context(int context_id)
  185. {
  186. ida_free(&mmu_context_ida, context_id);
  187. }
  188. EXPORT_SYMBOL_GPL(__destroy_context);
  189. static void destroy_contexts(mm_context_t *ctx)
  190. {
  191. if (radix_enabled()) {
  192. ida_free(&mmu_context_ida, ctx->id);
  193. } else {
  194. #ifdef CONFIG_PPC_64S_HASH_MMU
  195. int index, context_id;
  196. for (index = 0; index < ARRAY_SIZE(ctx->extended_id); index++) {
  197. context_id = ctx->extended_id[index];
  198. if (context_id)
  199. ida_free(&mmu_context_ida, context_id);
  200. }
  201. kfree(ctx->hash_context);
  202. #else
  203. BUILD_BUG(); // radix_enabled() should be constant true
  204. #endif
  205. }
  206. }
  207. static void pmd_frag_destroy(void *pmd_frag)
  208. {
  209. int count;
  210. struct page *page;
  211. page = virt_to_page(pmd_frag);
  212. /* drop all the pending references */
  213. count = ((unsigned long)pmd_frag & ~PAGE_MASK) >> PMD_FRAG_SIZE_SHIFT;
  214. /* We allow PTE_FRAG_NR fragments from a PTE page */
  215. if (atomic_sub_and_test(PMD_FRAG_NR - count, &page->pt_frag_refcount)) {
  216. pgtable_pmd_page_dtor(page);
  217. __free_page(page);
  218. }
  219. }
  220. static void destroy_pagetable_cache(struct mm_struct *mm)
  221. {
  222. void *frag;
  223. frag = mm->context.pte_frag;
  224. if (frag)
  225. pte_frag_destroy(frag);
  226. frag = mm->context.pmd_frag;
  227. if (frag)
  228. pmd_frag_destroy(frag);
  229. return;
  230. }
  231. void destroy_context(struct mm_struct *mm)
  232. {
  233. #ifdef CONFIG_SPAPR_TCE_IOMMU
  234. WARN_ON_ONCE(!list_empty(&mm->context.iommu_group_mem_list));
  235. #endif
  236. /*
  237. * For tasks which were successfully initialized we end up calling
  238. * arch_exit_mmap() which clears the process table entry. And
  239. * arch_exit_mmap() is called before the required fullmm TLB flush
  240. * which does a RIC=2 flush. Hence for an initialized task, we do clear
  241. * any cached process table entries.
  242. *
  243. * The condition below handles the error case during task init. We have
  244. * set the process table entry early and if we fail a task
  245. * initialization, we need to ensure the process table entry is zeroed.
  246. * We need not worry about process table entry caches because the task
  247. * never ran with the PID value.
  248. */
  249. if (radix_enabled())
  250. process_tb[mm->context.id].prtb0 = 0;
  251. else
  252. subpage_prot_free(mm);
  253. destroy_contexts(&mm->context);
  254. mm->context.id = MMU_NO_CONTEXT;
  255. }
  256. void arch_exit_mmap(struct mm_struct *mm)
  257. {
  258. destroy_pagetable_cache(mm);
  259. if (radix_enabled()) {
  260. /*
  261. * Radix doesn't have a valid bit in the process table
  262. * entries. However we know that at least P9 implementation
  263. * will avoid caching an entry with an invalid RTS field,
  264. * and 0 is invalid. So this will do.
  265. *
  266. * This runs before the "fullmm" tlb flush in exit_mmap,
  267. * which does a RIC=2 tlbie to clear the process table
  268. * entry. See the "fullmm" comments in tlb-radix.c.
  269. *
  270. * No barrier required here after the store because
  271. * this process will do the invalidate, which starts with
  272. * ptesync.
  273. */
  274. process_tb[mm->context.id].prtb0 = 0;
  275. }
  276. }
  277. #ifdef CONFIG_PPC_RADIX_MMU
  278. void radix__switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
  279. {
  280. mtspr(SPRN_PID, next->context.id);
  281. isync();
  282. }
  283. #endif
  284. /**
  285. * cleanup_cpu_mmu_context - Clean up MMU details for this CPU (newly offlined)
  286. *
  287. * This clears the CPU from mm_cpumask for all processes, and then flushes the
  288. * local TLB to ensure TLB coherency in case the CPU is onlined again.
  289. *
  290. * KVM guest translations are not necessarily flushed here. If KVM started
  291. * using mm_cpumask or the Linux APIs which do, this would have to be resolved.
  292. */
  293. #ifdef CONFIG_HOTPLUG_CPU
  294. void cleanup_cpu_mmu_context(void)
  295. {
  296. int cpu = smp_processor_id();
  297. clear_tasks_mm_cpumask(cpu);
  298. tlbiel_all();
  299. }
  300. #endif