mmu_context_book3s64.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * MMU context allocation for 64-bit kernels.
  3. *
  4. * Copyright (C) 2004 Anton Blanchard, IBM Corp. <anton@samba.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/mm.h>
  18. #include <linux/pkeys.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/idr.h>
  21. #include <linux/export.h>
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/pgalloc.h>
  26. static DEFINE_IDA(mmu_context_ida);
  27. static int alloc_context_id(int min_id, int max_id)
  28. {
  29. return ida_alloc_range(&mmu_context_ida, min_id, max_id, GFP_KERNEL);
  30. }
  31. void hash__reserve_context_id(int id)
  32. {
  33. int result = ida_alloc_range(&mmu_context_ida, id, id, GFP_KERNEL);
  34. WARN(result != id, "mmu: Failed to reserve context id %d (rc %d)\n", id, result);
  35. }
  36. int hash__alloc_context_id(void)
  37. {
  38. unsigned long max;
  39. if (mmu_has_feature(MMU_FTR_68_BIT_VA))
  40. max = MAX_USER_CONTEXT;
  41. else
  42. max = MAX_USER_CONTEXT_65BIT_VA;
  43. return alloc_context_id(MIN_USER_CONTEXT, max);
  44. }
  45. EXPORT_SYMBOL_GPL(hash__alloc_context_id);
  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. /*
  82. * The old code would re-promote on fork, we don't do that when using
  83. * slices as it could cause problem promoting slices that have been
  84. * forced down to 4K.
  85. *
  86. * For book3s we have MMU_NO_CONTEXT set to be ~0. Hence check
  87. * explicitly against context.id == 0. This ensures that we properly
  88. * initialize context slice details for newly allocated mm's (which will
  89. * have id == 0) and don't alter context slice inherited via fork (which
  90. * will have id != 0).
  91. *
  92. * We should not be calling init_new_context() on init_mm. Hence a
  93. * check against 0 is OK.
  94. */
  95. if (mm->context.id == 0)
  96. slice_init_new_context_exec(mm);
  97. index = realloc_context_ids(&mm->context);
  98. if (index < 0)
  99. return index;
  100. subpage_prot_init_new_context(mm);
  101. pkey_mm_init(mm);
  102. return index;
  103. }
  104. static int radix__init_new_context(struct mm_struct *mm)
  105. {
  106. unsigned long rts_field;
  107. int index, max_id;
  108. max_id = (1 << mmu_pid_bits) - 1;
  109. index = alloc_context_id(mmu_base_pid, max_id);
  110. if (index < 0)
  111. return index;
  112. /*
  113. * set the process table entry,
  114. */
  115. rts_field = radix__get_tree_size();
  116. process_tb[index].prtb0 = cpu_to_be64(rts_field | __pa(mm->pgd) | RADIX_PGD_INDEX_SIZE);
  117. /*
  118. * Order the above store with subsequent update of the PID
  119. * register (at which point HW can start loading/caching
  120. * the entry) and the corresponding load by the MMU from
  121. * the L2 cache.
  122. */
  123. asm volatile("ptesync;isync" : : : "memory");
  124. mm->context.npu_context = NULL;
  125. return index;
  126. }
  127. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  128. {
  129. int index;
  130. if (radix_enabled())
  131. index = radix__init_new_context(mm);
  132. else
  133. index = hash__init_new_context(mm);
  134. if (index < 0)
  135. return index;
  136. mm->context.id = index;
  137. mm->context.pte_frag = NULL;
  138. mm->context.pmd_frag = NULL;
  139. #ifdef CONFIG_SPAPR_TCE_IOMMU
  140. mm_iommu_init(mm);
  141. #endif
  142. atomic_set(&mm->context.active_cpus, 0);
  143. atomic_set(&mm->context.copros, 0);
  144. return 0;
  145. }
  146. void __destroy_context(int context_id)
  147. {
  148. ida_free(&mmu_context_ida, context_id);
  149. }
  150. EXPORT_SYMBOL_GPL(__destroy_context);
  151. static void destroy_contexts(mm_context_t *ctx)
  152. {
  153. int index, context_id;
  154. for (index = 0; index < ARRAY_SIZE(ctx->extended_id); index++) {
  155. context_id = ctx->extended_id[index];
  156. if (context_id)
  157. ida_free(&mmu_context_ida, context_id);
  158. }
  159. }
  160. static void pte_frag_destroy(void *pte_frag)
  161. {
  162. int count;
  163. struct page *page;
  164. page = virt_to_page(pte_frag);
  165. /* drop all the pending references */
  166. count = ((unsigned long)pte_frag & ~PAGE_MASK) >> PTE_FRAG_SIZE_SHIFT;
  167. /* We allow PTE_FRAG_NR fragments from a PTE page */
  168. if (atomic_sub_and_test(PTE_FRAG_NR - count, &page->pt_frag_refcount)) {
  169. pgtable_page_dtor(page);
  170. __free_page(page);
  171. }
  172. }
  173. static void pmd_frag_destroy(void *pmd_frag)
  174. {
  175. int count;
  176. struct page *page;
  177. page = virt_to_page(pmd_frag);
  178. /* drop all the pending references */
  179. count = ((unsigned long)pmd_frag & ~PAGE_MASK) >> PMD_FRAG_SIZE_SHIFT;
  180. /* We allow PTE_FRAG_NR fragments from a PTE page */
  181. if (atomic_sub_and_test(PMD_FRAG_NR - count, &page->pt_frag_refcount)) {
  182. pgtable_pmd_page_dtor(page);
  183. __free_page(page);
  184. }
  185. }
  186. static void destroy_pagetable_cache(struct mm_struct *mm)
  187. {
  188. void *frag;
  189. frag = mm->context.pte_frag;
  190. if (frag)
  191. pte_frag_destroy(frag);
  192. frag = mm->context.pmd_frag;
  193. if (frag)
  194. pmd_frag_destroy(frag);
  195. return;
  196. }
  197. void destroy_context(struct mm_struct *mm)
  198. {
  199. #ifdef CONFIG_SPAPR_TCE_IOMMU
  200. WARN_ON_ONCE(!list_empty(&mm->context.iommu_group_mem_list));
  201. #endif
  202. if (radix_enabled())
  203. WARN_ON(process_tb[mm->context.id].prtb0 != 0);
  204. else
  205. subpage_prot_free(mm);
  206. destroy_contexts(&mm->context);
  207. mm->context.id = MMU_NO_CONTEXT;
  208. }
  209. void arch_exit_mmap(struct mm_struct *mm)
  210. {
  211. destroy_pagetable_cache(mm);
  212. if (radix_enabled()) {
  213. /*
  214. * Radix doesn't have a valid bit in the process table
  215. * entries. However we know that at least P9 implementation
  216. * will avoid caching an entry with an invalid RTS field,
  217. * and 0 is invalid. So this will do.
  218. *
  219. * This runs before the "fullmm" tlb flush in exit_mmap,
  220. * which does a RIC=2 tlbie to clear the process table
  221. * entry. See the "fullmm" comments in tlb-radix.c.
  222. *
  223. * No barrier required here after the store because
  224. * this process will do the invalidate, which starts with
  225. * ptesync.
  226. */
  227. process_tb[mm->context.id].prtb0 = 0;
  228. }
  229. }
  230. #ifdef CONFIG_PPC_RADIX_MMU
  231. void radix__switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
  232. {
  233. mtspr(SPRN_PID, next->context.id);
  234. isync();
  235. }
  236. #endif