subpage_prot.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. arch_enter_lazy_mmu_mode();
  68. for (; npages > 0; --npages) {
  69. pte_update(mm, addr, pte, 0, 0, 0);
  70. addr += PAGE_SIZE;
  71. ++pte;
  72. }
  73. arch_leave_lazy_mmu_mode();
  74. pte_unmap_unlock(pte - 1, ptl);
  75. }
  76. /*
  77. * Clear the subpage protection map for an address range, allowing
  78. * all accesses that are allowed by the pte permissions.
  79. */
  80. static void subpage_prot_clear(unsigned long addr, unsigned long len)
  81. {
  82. struct mm_struct *mm = current->mm;
  83. struct subpage_prot_table *spt;
  84. u32 **spm, *spp;
  85. unsigned long i;
  86. size_t nw;
  87. unsigned long next, limit;
  88. mmap_write_lock(mm);
  89. spt = mm_ctx_subpage_prot(&mm->context);
  90. if (!spt)
  91. goto err_out;
  92. limit = addr + len;
  93. if (limit > spt->maxaddr)
  94. limit = spt->maxaddr;
  95. for (; addr < limit; addr = next) {
  96. next = pmd_addr_end(addr, limit);
  97. if (addr < 0x100000000UL) {
  98. spm = spt->low_prot;
  99. } else {
  100. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  101. if (!spm)
  102. continue;
  103. }
  104. spp = spm[(addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  105. if (!spp)
  106. continue;
  107. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  108. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  109. nw = PTRS_PER_PTE - i;
  110. if (addr + (nw << PAGE_SHIFT) > next)
  111. nw = (next - addr) >> PAGE_SHIFT;
  112. memset(spp, 0, nw * sizeof(u32));
  113. /* now flush any existing HPTEs for the range */
  114. hpte_flush_range(mm, addr, nw);
  115. }
  116. err_out:
  117. mmap_write_unlock(mm);
  118. }
  119. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  120. static int subpage_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
  121. unsigned long end, struct mm_walk *walk)
  122. {
  123. struct vm_area_struct *vma = walk->vma;
  124. split_huge_pmd(vma, pmd, addr);
  125. return 0;
  126. }
  127. static const struct mm_walk_ops subpage_walk_ops = {
  128. .pmd_entry = subpage_walk_pmd_entry,
  129. .walk_lock = PGWALK_WRLOCK_VERIFY,
  130. };
  131. static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  132. unsigned long len)
  133. {
  134. struct vm_area_struct *vma;
  135. VMA_ITERATOR(vmi, mm, addr);
  136. /*
  137. * We don't try too hard, we just mark all the vma in that range
  138. * VM_NOHUGEPAGE and split them.
  139. */
  140. for_each_vma_range(vmi, vma, addr + len) {
  141. vm_flags_set(vma, VM_NOHUGEPAGE);
  142. walk_page_vma(vma, &subpage_walk_ops, NULL);
  143. }
  144. }
  145. #else
  146. static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  147. unsigned long len)
  148. {
  149. return;
  150. }
  151. #endif
  152. /*
  153. * Copy in a subpage protection map for an address range.
  154. * The map has 2 bits per 4k subpage, so 32 bits per 64k page.
  155. * Each 2-bit field is 0 to allow any access, 1 to prevent writes,
  156. * 2 or 3 to prevent all accesses.
  157. * Note that the normal page protections also apply; the subpage
  158. * protection mechanism is an additional constraint, so putting 0
  159. * in a 2-bit field won't allow writes to a page that is otherwise
  160. * write-protected.
  161. */
  162. SYSCALL_DEFINE3(subpage_prot, unsigned long, addr,
  163. unsigned long, len, u32 __user *, map)
  164. {
  165. struct mm_struct *mm = current->mm;
  166. struct subpage_prot_table *spt;
  167. u32 **spm, *spp;
  168. unsigned long i;
  169. size_t nw;
  170. unsigned long next, limit;
  171. int err;
  172. if (radix_enabled())
  173. return -ENOENT;
  174. /* Check parameters */
  175. if ((addr & ~PAGE_MASK) || (len & ~PAGE_MASK) ||
  176. addr >= mm->task_size || len >= mm->task_size ||
  177. addr + len > mm->task_size)
  178. return -EINVAL;
  179. if (is_hugepage_only_range(mm, addr, len))
  180. return -EINVAL;
  181. if (!map) {
  182. /* Clear out the protection map for the address range */
  183. subpage_prot_clear(addr, len);
  184. return 0;
  185. }
  186. if (!access_ok(map, (len >> PAGE_SHIFT) * sizeof(u32)))
  187. return -EFAULT;
  188. mmap_write_lock(mm);
  189. spt = mm_ctx_subpage_prot(&mm->context);
  190. if (!spt) {
  191. /*
  192. * Allocate subpage prot table if not already done.
  193. * Do this with mmap_lock held
  194. */
  195. spt = kzalloc(sizeof(struct subpage_prot_table), GFP_KERNEL);
  196. if (!spt) {
  197. err = -ENOMEM;
  198. goto out;
  199. }
  200. mm->context.hash_context->spt = spt;
  201. }
  202. subpage_mark_vma_nohuge(mm, addr, len);
  203. for (limit = addr + len; addr < limit; addr = next) {
  204. next = pmd_addr_end(addr, limit);
  205. err = -ENOMEM;
  206. if (addr < 0x100000000UL) {
  207. spm = spt->low_prot;
  208. } else {
  209. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  210. if (!spm) {
  211. spm = (u32 **)get_zeroed_page(GFP_KERNEL);
  212. if (!spm)
  213. goto out;
  214. spt->protptrs[addr >> SBP_L3_SHIFT] = spm;
  215. }
  216. }
  217. spm += (addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1);
  218. spp = *spm;
  219. if (!spp) {
  220. spp = (u32 *)get_zeroed_page(GFP_KERNEL);
  221. if (!spp)
  222. goto out;
  223. *spm = spp;
  224. }
  225. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  226. local_irq_disable();
  227. demote_segment_4k(mm, addr);
  228. local_irq_enable();
  229. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  230. nw = PTRS_PER_PTE - i;
  231. if (addr + (nw << PAGE_SHIFT) > next)
  232. nw = (next - addr) >> PAGE_SHIFT;
  233. mmap_write_unlock(mm);
  234. if (__copy_from_user(spp, map, nw * sizeof(u32)))
  235. return -EFAULT;
  236. map += nw;
  237. mmap_write_lock(mm);
  238. /* now flush any existing HPTEs for the range */
  239. hpte_flush_range(mm, addr, nw);
  240. }
  241. if (limit > spt->maxaddr)
  242. spt->maxaddr = limit;
  243. err = 0;
  244. out:
  245. mmap_write_unlock(mm);
  246. return err;
  247. }