subpage-prot.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <asm/pgtable.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/tlbflush.h>
  18. /*
  19. * Free all pages allocated for subpage protection maps and pointers.
  20. * Also makes sure that the subpage_prot_table structure is
  21. * reinitialized for the next user.
  22. */
  23. void subpage_prot_free(struct mm_struct *mm)
  24. {
  25. struct subpage_prot_table *spt = &mm->context.spt;
  26. unsigned long i, j, addr;
  27. u32 **p;
  28. for (i = 0; i < 4; ++i) {
  29. if (spt->low_prot[i]) {
  30. free_page((unsigned long)spt->low_prot[i]);
  31. spt->low_prot[i] = NULL;
  32. }
  33. }
  34. addr = 0;
  35. for (i = 0; i < 2; ++i) {
  36. p = spt->protptrs[i];
  37. if (!p)
  38. continue;
  39. spt->protptrs[i] = NULL;
  40. for (j = 0; j < SBP_L2_COUNT && addr < spt->maxaddr;
  41. ++j, addr += PAGE_SIZE)
  42. if (p[j])
  43. free_page((unsigned long)p[j]);
  44. free_page((unsigned long)p);
  45. }
  46. spt->maxaddr = 0;
  47. }
  48. void subpage_prot_init_new_context(struct mm_struct *mm)
  49. {
  50. struct subpage_prot_table *spt = &mm->context.spt;
  51. memset(spt, 0, sizeof(*spt));
  52. }
  53. static void hpte_flush_range(struct mm_struct *mm, unsigned long addr,
  54. int npages)
  55. {
  56. pgd_t *pgd;
  57. pud_t *pud;
  58. pmd_t *pmd;
  59. pte_t *pte;
  60. spinlock_t *ptl;
  61. pgd = pgd_offset(mm, addr);
  62. if (pgd_none(*pgd))
  63. return;
  64. pud = pud_offset(pgd, addr);
  65. if (pud_none(*pud))
  66. return;
  67. pmd = pmd_offset(pud, addr);
  68. if (pmd_none(*pmd))
  69. return;
  70. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  71. arch_enter_lazy_mmu_mode();
  72. for (; npages > 0; --npages) {
  73. pte_update(mm, addr, pte, 0, 0, 0);
  74. addr += PAGE_SIZE;
  75. ++pte;
  76. }
  77. arch_leave_lazy_mmu_mode();
  78. pte_unmap_unlock(pte - 1, ptl);
  79. }
  80. /*
  81. * Clear the subpage protection map for an address range, allowing
  82. * all accesses that are allowed by the pte permissions.
  83. */
  84. static void subpage_prot_clear(unsigned long addr, unsigned long len)
  85. {
  86. struct mm_struct *mm = current->mm;
  87. struct subpage_prot_table *spt = &mm->context.spt;
  88. u32 **spm, *spp;
  89. unsigned long i;
  90. size_t nw;
  91. unsigned long next, limit;
  92. down_write(&mm->mmap_sem);
  93. limit = addr + len;
  94. if (limit > spt->maxaddr)
  95. limit = spt->maxaddr;
  96. for (; addr < limit; addr = next) {
  97. next = pmd_addr_end(addr, limit);
  98. if (addr < 0x100000000UL) {
  99. spm = spt->low_prot;
  100. } else {
  101. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  102. if (!spm)
  103. continue;
  104. }
  105. spp = spm[(addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  106. if (!spp)
  107. continue;
  108. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  109. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  110. nw = PTRS_PER_PTE - i;
  111. if (addr + (nw << PAGE_SHIFT) > next)
  112. nw = (next - addr) >> PAGE_SHIFT;
  113. memset(spp, 0, nw * sizeof(u32));
  114. /* now flush any existing HPTEs for the range */
  115. hpte_flush_range(mm, addr, nw);
  116. }
  117. up_write(&mm->mmap_sem);
  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 void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  128. unsigned long len)
  129. {
  130. struct vm_area_struct *vma;
  131. struct mm_walk subpage_proto_walk = {
  132. .mm = mm,
  133. .pmd_entry = subpage_walk_pmd_entry,
  134. };
  135. /*
  136. * We don't try too hard, we just mark all the vma in that range
  137. * VM_NOHUGEPAGE and split them.
  138. */
  139. vma = find_vma(mm, addr);
  140. /*
  141. * If the range is in unmapped range, just return
  142. */
  143. if (vma && ((addr + len) <= vma->vm_start))
  144. return;
  145. while (vma) {
  146. if (vma->vm_start >= (addr + len))
  147. break;
  148. vma->vm_flags |= VM_NOHUGEPAGE;
  149. walk_page_vma(vma, &subpage_proto_walk);
  150. vma = vma->vm_next;
  151. }
  152. }
  153. #else
  154. static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr,
  155. unsigned long len)
  156. {
  157. return;
  158. }
  159. #endif
  160. /*
  161. * Copy in a subpage protection map for an address range.
  162. * The map has 2 bits per 4k subpage, so 32 bits per 64k page.
  163. * Each 2-bit field is 0 to allow any access, 1 to prevent writes,
  164. * 2 or 3 to prevent all accesses.
  165. * Note that the normal page protections also apply; the subpage
  166. * protection mechanism is an additional constraint, so putting 0
  167. * in a 2-bit field won't allow writes to a page that is otherwise
  168. * write-protected.
  169. */
  170. long sys_subpage_prot(unsigned long addr, unsigned long len, u32 __user *map)
  171. {
  172. struct mm_struct *mm = current->mm;
  173. struct subpage_prot_table *spt = &mm->context.spt;
  174. u32 **spm, *spp;
  175. unsigned long i;
  176. size_t nw;
  177. unsigned long next, limit;
  178. int err;
  179. /* Check parameters */
  180. if ((addr & ~PAGE_MASK) || (len & ~PAGE_MASK) ||
  181. addr >= TASK_SIZE || len >= TASK_SIZE || addr + len > TASK_SIZE)
  182. return -EINVAL;
  183. if (is_hugepage_only_range(mm, addr, len))
  184. return -EINVAL;
  185. if (!map) {
  186. /* Clear out the protection map for the address range */
  187. subpage_prot_clear(addr, len);
  188. return 0;
  189. }
  190. if (!access_ok(VERIFY_READ, map, (len >> PAGE_SHIFT) * sizeof(u32)))
  191. return -EFAULT;
  192. down_write(&mm->mmap_sem);
  193. subpage_mark_vma_nohuge(mm, addr, len);
  194. for (limit = addr + len; addr < limit; addr = next) {
  195. next = pmd_addr_end(addr, limit);
  196. err = -ENOMEM;
  197. if (addr < 0x100000000UL) {
  198. spm = spt->low_prot;
  199. } else {
  200. spm = spt->protptrs[addr >> SBP_L3_SHIFT];
  201. if (!spm) {
  202. spm = (u32 **)get_zeroed_page(GFP_KERNEL);
  203. if (!spm)
  204. goto out;
  205. spt->protptrs[addr >> SBP_L3_SHIFT] = spm;
  206. }
  207. }
  208. spm += (addr >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1);
  209. spp = *spm;
  210. if (!spp) {
  211. spp = (u32 *)get_zeroed_page(GFP_KERNEL);
  212. if (!spp)
  213. goto out;
  214. *spm = spp;
  215. }
  216. spp += (addr >> PAGE_SHIFT) & (SBP_L1_COUNT - 1);
  217. local_irq_disable();
  218. demote_segment_4k(mm, addr);
  219. local_irq_enable();
  220. i = (addr >> PAGE_SHIFT) & (PTRS_PER_PTE - 1);
  221. nw = PTRS_PER_PTE - i;
  222. if (addr + (nw << PAGE_SHIFT) > next)
  223. nw = (next - addr) >> PAGE_SHIFT;
  224. up_write(&mm->mmap_sem);
  225. err = -EFAULT;
  226. if (__copy_from_user(spp, map, nw * sizeof(u32)))
  227. goto out2;
  228. map += nw;
  229. down_write(&mm->mmap_sem);
  230. /* now flush any existing HPTEs for the range */
  231. hpte_flush_range(mm, addr, nw);
  232. }
  233. if (limit > spt->maxaddr)
  234. spt->maxaddr = limit;
  235. err = 0;
  236. out:
  237. up_write(&mm->mmap_sem);
  238. out2:
  239. return err;
  240. }