subpage_prot.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2007-2008 Paul Mackerras, IBM Corp.
  4. */
  5. #include <linux/errno.h>
  6. #include <linux/kernel.h>
  7. #include <linux/gfp.h>
  8. #include <linux/types.h>
  9. #include <linux/pagewalk.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/pgtable.h>
  13. #include <linux/uaccess.h>
  14. /*
  15. * Free all pages allocated for subpage protection maps and pointers.
  16. * Also makes sure that the subpage_prot_table structure is
  17. * reinitialized for the next user.
  18. */
  19. void subpage_prot_free(struct mm_struct *mm)
  20. {
  21. struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context);
  22. unsigned long i, j, addr;
  23. u32 **p;
  24. if (!spt)
  25. return;
  26. for (i = 0; i < 4; ++i) {
  27. if (spt->low_prot[i]) {
  28. free_page((unsigned long)spt->low_prot[i]);
  29. spt->low_prot[i] = NULL;
  30. }
  31. }
  32. addr = 0;
  33. for (i = 0; i < (TASK_SIZE_USER64 >> 43); ++i) {
  34. p = spt->protptrs[i];
  35. if (!p)
  36. continue;
  37. spt->protptrs[i] = NULL;
  38. for (j = 0; j < SBP_L2_COUNT && addr < spt->maxaddr;
  39. ++j, addr += PAGE_SIZE)
  40. if (p[j])
  41. free_page((unsigned long)p[j]);
  42. free_page((unsigned long)p);
  43. }
  44. spt->maxaddr = 0;
  45. kfree(spt);
  46. }
  47. static void hpte_flush_range(struct mm_struct *mm, unsigned long addr,
  48. int npages)
  49. {
  50. pgd_t *pgd;
  51. p4d_t *p4d;
  52. pud_t *pud;
  53. pmd_t *pmd;
  54. pte_t *pte;
  55. spinlock_t *ptl;
  56. pgd = pgd_offset(mm, addr);
  57. p4d = p4d_offset(pgd, addr);
  58. if (p4d_none(*p4d))
  59. return;
  60. pud = pud_offset(p4d, addr);
  61. if (pud_none(*pud))
  62. return;
  63. pmd = pmd_offset(pud, addr);
  64. if (pmd_none(*pmd))
  65. return;
  66. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  67. if (!pte)
  68. return;
  69. arch_enter_lazy_mmu_mode();
  70. for (; npages > 0; --npages) {
  71. pte_update(mm, addr, pte, 0, 0, 0);
  72. addr += PAGE_SIZE;
  73. ++pte;
  74. }
  75. arch_leave_lazy_mmu_mode();
  76. pte_unmap_unlock(pte - 1, ptl);
  77. }
  78. /*
  79. * Clear the subpage protection map for an address range, allowing
  80. * all accesses that are allowed by the pte permissions.
  81. */
  82. static void subpage_prot_clear(unsigned long addr, unsigned long len)
  83. {
  84. struct mm_struct *mm = current->mm;
  85. struct subpage_prot_table *spt;
  86. u32 **spm, *spp;
  87. unsigned long i;
  88. size_t nw;
  89. unsigned long next, limit;
  90. mmap_write_lock(mm);
  91. spt = mm_ctx_subpage_prot(&mm->context);
  92. if (!spt)
  93. goto err_out;
  94. limit = addr + len;
  95. if (limit > spt->maxaddr)
  96. limit = spt->maxaddr;
  97. for (; addr < limit; addr = next) {
  98. next = pmd_addr_end(addr, limit);
  99. if (addr < 0x100000000UL) {
  100. spm = spt->low_prot;
  101. } else {
  102. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  103. if (!spm)
  104. continue;
  105. }
  106. spp = spm[(addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  107. if (!spp)
  108. continue;
  109. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  110. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  111. nw = PTRS_PER_PTE - i;
  112. if (addr + (nw << PAGE_SHIFT) > next)
  113. nw = (next - addr) >> PAGE_SHIFT;
  114. memset(spp, 0, nw * sizeof(u32));
  115. /* now flush any existing HPTEs for the range */
  116. hpte_flush_range(mm, addr, nw);
  117. }
  118. err_out:
  119. mmap_write_unlock(mm);
  120. }
  121. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  122. static int subpage_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
  123. unsigned long end, struct mm_walk *walk)
  124. {
  125. struct vm_area_struct *vma = walk->vma;
  126. split_huge_pmd(vma, pmd, addr);
  127. return 0;
  128. }
  129. static const struct mm_walk_ops subpage_walk_ops = {
  130. .pmd_entry = subpage_walk_pmd_entry,
  131. .walk_lock = PGWALK_WRLOCK_VERIFY,
  132. };
  133. static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  134. unsigned long len)
  135. {
  136. struct vm_area_struct *vma;
  137. VMA_ITERATOR(vmi, mm, addr);
  138. /*
  139. * We don't try too hard, we just mark all the vma in that range
  140. * VM_NOHUGEPAGE and split them.
  141. */
  142. for_each_vma_range(vmi, vma, addr + len) {
  143. vm_flags_set(vma, VM_NOHUGEPAGE);
  144. walk_page_vma(vma, &subpage_walk_ops, NULL);
  145. }
  146. }
  147. #else
  148. static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  149. unsigned long len)
  150. {
  151. return;
  152. }
  153. #endif
  154. /*
  155. * Copy in a subpage protection map for an address range.
  156. * The map has 2 bits per 4k subpage, so 32 bits per 64k page.
  157. * Each 2-bit field is 0 to allow any access, 1 to prevent writes,
  158. * 2 or 3 to prevent all accesses.
  159. * Note that the normal page protections also apply; the subpage
  160. * protection mechanism is an additional constraint, so putting 0
  161. * in a 2-bit field won't allow writes to a page that is otherwise
  162. * write-protected.
  163. */
  164. SYSCALL_DEFINE3(subpage_prot, unsigned long, addr,
  165. unsigned long, len, u32 __user *, map)
  166. {
  167. struct mm_struct *mm = current->mm;
  168. struct subpage_prot_table *spt;
  169. u32 **spm, *spp;
  170. unsigned long i;
  171. size_t nw;
  172. unsigned long next, limit;
  173. int err;
  174. if (radix_enabled())
  175. return -ENOENT;
  176. /* Check parameters */
  177. if ((addr & ~PAGE_MASK) || (len & ~PAGE_MASK) ||
  178. addr >= mm->task_size || len >= mm->task_size ||
  179. addr + len > mm->task_size)
  180. return -EINVAL;
  181. if (is_hugepage_only_range(mm, addr, len))
  182. return -EINVAL;
  183. if (!map) {
  184. /* Clear out the protection map for the address range */
  185. subpage_prot_clear(addr, len);
  186. return 0;
  187. }
  188. if (!access_ok(map, (len >> PAGE_SHIFT) * sizeof(u32)))
  189. return -EFAULT;
  190. mmap_write_lock(mm);
  191. spt = mm_ctx_subpage_prot(&mm->context);
  192. if (!spt) {
  193. /*
  194. * Allocate subpage prot table if not already done.
  195. * Do this with mmap_lock held
  196. */
  197. spt = kzalloc(sizeof(struct subpage_prot_table), GFP_KERNEL);
  198. if (!spt) {
  199. err = -ENOMEM;
  200. goto out;
  201. }
  202. mm->context.hash_context->spt = spt;
  203. }
  204. subpage_mark_vma_nohuge(mm, addr, len);
  205. for (limit = addr + len; addr < limit; addr = next) {
  206. next = pmd_addr_end(addr, limit);
  207. err = -ENOMEM;
  208. if (addr < 0x100000000UL) {
  209. spm = spt->low_prot;
  210. } else {
  211. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  212. if (!spm) {
  213. spm = (u32 **)get_zeroed_page(GFP_KERNEL);
  214. if (!spm)
  215. goto out;
  216. spt->protptrs[addr >> SBP_L3_SHIFT] = spm;
  217. }
  218. }
  219. spm += (addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1);
  220. spp = *spm;
  221. if (!spp) {
  222. spp = (u32 *)get_zeroed_page(GFP_KERNEL);
  223. if (!spp)
  224. goto out;
  225. *spm = spp;
  226. }
  227. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  228. local_irq_disable();
  229. demote_segment_4k(mm, addr);
  230. local_irq_enable();
  231. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  232. nw = PTRS_PER_PTE - i;
  233. if (addr + (nw << PAGE_SHIFT) > next)
  234. nw = (next - addr) >> PAGE_SHIFT;
  235. mmap_write_unlock(mm);
  236. if (__copy_from_user(spp, map, nw * sizeof(u32)))
  237. return -EFAULT;
  238. map += nw;
  239. mmap_write_lock(mm);
  240. /* now flush any existing HPTEs for the range */
  241. hpte_flush_range(mm, addr, nw);
  242. }
  243. if (limit > spt->maxaddr)
  244. spt->maxaddr = limit;
  245. err = 0;
  246. out:
  247. mmap_write_unlock(mm);
  248. return err;
  249. }