subpage-prot.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright 2007-2008 Paul Mackerras, IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/gfp.h>
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/hugetlb.h>
  15. #include <linux/syscalls.h>
  16. #include <asm/pgtable.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/tlbflush.h>
  19. /*
  20. * Free all pages allocated for subpage protection maps and pointers.
  21. * Also makes sure that the subpage_prot_table structure is
  22. * reinitialized for the next user.
  23. */
  24. void subpage_prot_free(struct mm_struct *mm)
  25. {
  26. struct subpage_prot_table *spt = &mm->context.spt;
  27. unsigned long i, j, addr;
  28. u32 **p;
  29. for (i = 0; i < 4; ++i) {
  30. if (spt->low_prot[i]) {
  31. free_page((unsigned long)spt->low_prot[i]);
  32. spt->low_prot[i] = NULL;
  33. }
  34. }
  35. addr = 0;
  36. for (i = 0; i < (TASK_SIZE_USER64 >> 43); ++i) {
  37. p = spt->protptrs[i];
  38. if (!p)
  39. continue;
  40. spt->protptrs[i] = NULL;
  41. for (j = 0; j < SBP_L2_COUNT && addr < spt->maxaddr;
  42. ++j, addr += PAGE_SIZE)
  43. if (p[j])
  44. free_page((unsigned long)p[j]);
  45. free_page((unsigned long)p);
  46. }
  47. spt->maxaddr = 0;
  48. }
  49. void subpage_prot_init_new_context(struct mm_struct *mm)
  50. {
  51. struct subpage_prot_table *spt = &mm->context.spt;
  52. memset(spt, 0, sizeof(*spt));
  53. }
  54. static void hpte_flush_range(struct mm_struct *mm, unsigned long addr,
  55. int npages)
  56. {
  57. pgd_t *pgd;
  58. pud_t *pud;
  59. pmd_t *pmd;
  60. pte_t *pte;
  61. spinlock_t *ptl;
  62. pgd = pgd_offset(mm, addr);
  63. if (pgd_none(*pgd))
  64. return;
  65. pud = pud_offset(pgd, addr);
  66. if (pud_none(*pud))
  67. return;
  68. pmd = pmd_offset(pud, addr);
  69. if (pmd_none(*pmd))
  70. return;
  71. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  72. arch_enter_lazy_mmu_mode();
  73. for (; npages > 0; --npages) {
  74. pte_update(mm, addr, pte, 0, 0, 0);
  75. addr += PAGE_SIZE;
  76. ++pte;
  77. }
  78. arch_leave_lazy_mmu_mode();
  79. pte_unmap_unlock(pte - 1, ptl);
  80. }
  81. /*
  82. * Clear the subpage protection map for an address range, allowing
  83. * all accesses that are allowed by the pte permissions.
  84. */
  85. static void subpage_prot_clear(unsigned long addr, unsigned long len)
  86. {
  87. struct mm_struct *mm = current->mm;
  88. struct subpage_prot_table *spt = &mm->context.spt;
  89. u32 **spm, *spp;
  90. unsigned long i;
  91. size_t nw;
  92. unsigned long next, limit;
  93. down_write(&mm->mmap_sem);
  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. up_write(&mm->mmap_sem);
  119. }
  120. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  121. static int subpage_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
  122. unsigned long end, struct mm_walk *walk)
  123. {
  124. struct vm_area_struct *vma = walk->vma;
  125. split_huge_pmd(vma, pmd, addr);
  126. return 0;
  127. }
  128. static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  129. unsigned long len)
  130. {
  131. struct vm_area_struct *vma;
  132. struct mm_walk subpage_proto_walk = {
  133. .mm = mm,
  134. .pmd_entry = subpage_walk_pmd_entry,
  135. };
  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. vma = find_vma(mm, addr);
  141. /*
  142. * If the range is in unmapped range, just return
  143. */
  144. if (vma && ((addr + len) <= vma->vm_start))
  145. return;
  146. while (vma) {
  147. if (vma->vm_start >= (addr + len))
  148. break;
  149. vma->vm_flags |= VM_NOHUGEPAGE;
  150. walk_page_vma(vma, &subpage_proto_walk);
  151. vma = vma->vm_next;
  152. }
  153. }
  154. #else
  155. static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  156. unsigned long len)
  157. {
  158. return;
  159. }
  160. #endif
  161. /*
  162. * Copy in a subpage protection map for an address range.
  163. * The map has 2 bits per 4k subpage, so 32 bits per 64k page.
  164. * Each 2-bit field is 0 to allow any access, 1 to prevent writes,
  165. * 2 or 3 to prevent all accesses.
  166. * Note that the normal page protections also apply; the subpage
  167. * protection mechanism is an additional constraint, so putting 0
  168. * in a 2-bit field won't allow writes to a page that is otherwise
  169. * write-protected.
  170. */
  171. #pragma GCC diagnostic push
  172. #pragma GCC diagnostic ignored "-Wpragmas"
  173. #pragma GCC diagnostic ignored "-Wattribute-alias"
  174. SYSCALL_DEFINE3(subpage_prot, unsigned long, addr,
  175. unsigned long, len, u32 __user *, map)
  176. {
  177. struct mm_struct *mm = current->mm;
  178. struct subpage_prot_table *spt = &mm->context.spt;
  179. u32 **spm, *spp;
  180. unsigned long i;
  181. size_t nw;
  182. unsigned long next, limit;
  183. int err;
  184. if (radix_enabled())
  185. return -ENOENT;
  186. /* Check parameters */
  187. if ((addr & ~PAGE_MASK) || (len & ~PAGE_MASK) ||
  188. addr >= mm->task_size || len >= mm->task_size ||
  189. addr + len > mm->task_size)
  190. return -EINVAL;
  191. if (is_hugepage_only_range(mm, addr, len))
  192. return -EINVAL;
  193. if (!map) {
  194. /* Clear out the protection map for the address range */
  195. subpage_prot_clear(addr, len);
  196. return 0;
  197. }
  198. if (!access_ok(VERIFY_READ, map, (len >> PAGE_SHIFT) * sizeof(u32)))
  199. return -EFAULT;
  200. down_write(&mm->mmap_sem);
  201. subpage_mark_vma_nohuge(mm, addr, len);
  202. for (limit = addr + len; addr < limit; addr = next) {
  203. next = pmd_addr_end(addr, limit);
  204. err = -ENOMEM;
  205. if (addr < 0x100000000UL) {
  206. spm = spt->low_prot;
  207. } else {
  208. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  209. if (!spm) {
  210. spm = (u32 **)get_zeroed_page(GFP_KERNEL);
  211. if (!spm)
  212. goto out;
  213. spt->protptrs[addr >> SBP_L3_SHIFT] = spm;
  214. }
  215. }
  216. spm += (addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1);
  217. spp = *spm;
  218. if (!spp) {
  219. spp = (u32 *)get_zeroed_page(GFP_KERNEL);
  220. if (!spp)
  221. goto out;
  222. *spm = spp;
  223. }
  224. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  225. local_irq_disable();
  226. demote_segment_4k(mm, addr);
  227. local_irq_enable();
  228. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  229. nw = PTRS_PER_PTE - i;
  230. if (addr + (nw << PAGE_SHIFT) > next)
  231. nw = (next - addr) >> PAGE_SHIFT;
  232. up_write(&mm->mmap_sem);
  233. if (__copy_from_user(spp, map, nw * sizeof(u32)))
  234. return -EFAULT;
  235. map += nw;
  236. down_write(&mm->mmap_sem);
  237. /* now flush any existing HPTEs for the range */
  238. hpte_flush_range(mm, addr, nw);
  239. }
  240. if (limit > spt->maxaddr)
  241. spt->maxaddr = limit;
  242. err = 0;
  243. out:
  244. up_write(&mm->mmap_sem);
  245. return err;
  246. }
  247. #pragma GCC diagnostic pop