PageRenderTime 69ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/powerpc/mm/book3s64/mmu_context.c

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