pgtable.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include <linux/mm.h>
  2. #include <linux/gfp.h>
  3. #include <asm/pgalloc.h>
  4. #include <asm/pgtable.h>
  5. #include <asm/tlb.h>
  6. #include <asm/fixmap.h>
  7. #define PGALLOC_GFP GFP_KERNEL | __GFP_NOTRACK | __GFP_REPEAT | __GFP_ZERO
  8. #ifdef CONFIG_HIGHPTE
  9. #define PGALLOC_USER_GFP __GFP_HIGHMEM
  10. #else
  11. #define PGALLOC_USER_GFP 0
  12. #endif
  13. gfp_t __userpte_alloc_gfp = PGALLOC_GFP | PGALLOC_USER_GFP;
  14. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  15. {
  16. return (pte_t *)__get_free_page(PGALLOC_GFP);
  17. }
  18. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  19. {
  20. struct page *pte;
  21. pte = alloc_pages(__userpte_alloc_gfp, 0);
  22. if (!pte)
  23. return NULL;
  24. if (!pgtable_page_ctor(pte)) {
  25. __free_page(pte);
  26. return NULL;
  27. }
  28. return pte;
  29. }
  30. static int __init setup_userpte(char *arg)
  31. {
  32. if (!arg)
  33. return -EINVAL;
  34. /*
  35. * "userpte=nohigh" disables allocation of user pagetables in
  36. * high memory.
  37. */
  38. if (strcmp(arg, "nohigh") == 0)
  39. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  40. else
  41. return -EINVAL;
  42. return 0;
  43. }
  44. early_param("userpte", setup_userpte);
  45. void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
  46. {
  47. pgtable_page_dtor(pte);
  48. paravirt_release_pte(page_to_pfn(pte));
  49. tlb_remove_page(tlb, pte);
  50. }
  51. #if PAGETABLE_LEVELS > 2
  52. void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
  53. {
  54. paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT);
  55. /*
  56. * NOTE! For PAE, any changes to the top page-directory-pointer-table
  57. * entries need a full cr3 reload to flush.
  58. */
  59. #ifdef CONFIG_X86_PAE
  60. tlb->need_flush_all = 1;
  61. #endif
  62. tlb_remove_page(tlb, virt_to_page(pmd));
  63. }
  64. #if PAGETABLE_LEVELS > 3
  65. void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
  66. {
  67. paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
  68. tlb_remove_page(tlb, virt_to_page(pud));
  69. }
  70. #endif /* PAGETABLE_LEVELS > 3 */
  71. #endif /* PAGETABLE_LEVELS > 2 */
  72. static inline void pgd_list_add(pgd_t *pgd)
  73. {
  74. struct page *page = virt_to_page(pgd);
  75. list_add(&page->lru, &pgd_list);
  76. }
  77. static inline void pgd_list_del(pgd_t *pgd)
  78. {
  79. struct page *page = virt_to_page(pgd);
  80. list_del(&page->lru);
  81. }
  82. #define UNSHARED_PTRS_PER_PGD \
  83. (SHARED_KERNEL_PMD ? KERNEL_PGD_BOUNDARY : PTRS_PER_PGD)
  84. static void pgd_set_mm(pgd_t *pgd, struct mm_struct *mm)
  85. {
  86. BUILD_BUG_ON(sizeof(virt_to_page(pgd)->index) < sizeof(mm));
  87. virt_to_page(pgd)->index = (pgoff_t)mm;
  88. }
  89. struct mm_struct *pgd_page_get_mm(struct page *page)
  90. {
  91. return (struct mm_struct *)page->index;
  92. }
  93. static void pgd_ctor(struct mm_struct *mm, pgd_t *pgd)
  94. {
  95. /* If the pgd points to a shared pagetable level (either the
  96. ptes in non-PAE, or shared PMD in PAE), then just copy the
  97. references from swapper_pg_dir. */
  98. if (PAGETABLE_LEVELS == 2 ||
  99. (PAGETABLE_LEVELS == 3 && SHARED_KERNEL_PMD) ||
  100. PAGETABLE_LEVELS == 4) {
  101. clone_pgd_range(pgd + KERNEL_PGD_BOUNDARY,
  102. swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  103. KERNEL_PGD_PTRS);
  104. }
  105. /* list required to sync kernel mapping updates */
  106. if (!SHARED_KERNEL_PMD) {
  107. pgd_set_mm(pgd, mm);
  108. pgd_list_add(pgd);
  109. }
  110. }
  111. static void pgd_dtor(pgd_t *pgd)
  112. {
  113. if (SHARED_KERNEL_PMD)
  114. return;
  115. spin_lock(&pgd_lock);
  116. pgd_list_del(pgd);
  117. spin_unlock(&pgd_lock);
  118. }
  119. /*
  120. * List of all pgd's needed for non-PAE so it can invalidate entries
  121. * in both cached and uncached pgd's; not needed for PAE since the
  122. * kernel pmd is shared. If PAE were not to share the pmd a similar
  123. * tactic would be needed. This is essentially codepath-based locking
  124. * against pageattr.c; it is the unique case in which a valid change
  125. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  126. * vmalloc faults work because attached pagetables are never freed.
  127. * -- nyc
  128. */
  129. #ifdef CONFIG_X86_PAE
  130. /*
  131. * In PAE mode, we need to do a cr3 reload (=tlb flush) when
  132. * updating the top-level pagetable entries to guarantee the
  133. * processor notices the update. Since this is expensive, and
  134. * all 4 top-level entries are used almost immediately in a
  135. * new process's life, we just pre-populate them here.
  136. *
  137. * Also, if we're in a paravirt environment where the kernel pmd is
  138. * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
  139. * and initialize the kernel pmds here.
  140. */
  141. #define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD
  142. void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
  143. {
  144. paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
  145. /* Note: almost everything apart from _PAGE_PRESENT is
  146. reserved at the pmd (PDPT) level. */
  147. set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
  148. /*
  149. * According to Intel App note "TLBs, Paging-Structure Caches,
  150. * and Their Invalidation", April 2007, document 317080-001,
  151. * section 8.1: in PAE mode we explicitly have to flush the
  152. * TLB via cr3 if the top-level pgd is changed...
  153. */
  154. flush_tlb_mm(mm);
  155. }
  156. #else /* !CONFIG_X86_PAE */
  157. /* No need to prepopulate any pagetable entries in non-PAE modes. */
  158. #define PREALLOCATED_PMDS 0
  159. #endif /* CONFIG_X86_PAE */
  160. static void free_pmds(pmd_t *pmds[])
  161. {
  162. int i;
  163. for(i = 0; i < PREALLOCATED_PMDS; i++)
  164. if (pmds[i]) {
  165. pgtable_pmd_page_dtor(virt_to_page(pmds[i]));
  166. free_page((unsigned long)pmds[i]);
  167. }
  168. }
  169. static int preallocate_pmds(pmd_t *pmds[])
  170. {
  171. int i;
  172. bool failed = false;
  173. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  174. pmd_t *pmd = (pmd_t *)__get_free_page(PGALLOC_GFP);
  175. if (!pmd)
  176. failed = true;
  177. if (pmd && !pgtable_pmd_page_ctor(virt_to_page(pmd))) {
  178. free_page((unsigned long)pmds[i]);
  179. pmd = NULL;
  180. failed = true;
  181. }
  182. pmds[i] = pmd;
  183. }
  184. if (failed) {
  185. free_pmds(pmds);
  186. return -ENOMEM;
  187. }
  188. return 0;
  189. }
  190. /*
  191. * Mop up any pmd pages which may still be attached to the pgd.
  192. * Normally they will be freed by munmap/exit_mmap, but any pmd we
  193. * preallocate which never got a corresponding vma will need to be
  194. * freed manually.
  195. */
  196. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  197. {
  198. int i;
  199. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  200. pgd_t pgd = pgdp[i];
  201. if (pgd_val(pgd) != 0) {
  202. pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
  203. pgdp[i] = native_make_pgd(0);
  204. paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
  205. pmd_free(mm, pmd);
  206. }
  207. }
  208. }
  209. static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
  210. {
  211. pud_t *pud;
  212. int i;
  213. if (PREALLOCATED_PMDS == 0) /* Work around gcc-3.4.x bug */
  214. return;
  215. pud = pud_offset(pgd, 0);
  216. for (i = 0; i < PREALLOCATED_PMDS; i++, pud++) {
  217. pmd_t *pmd = pmds[i];
  218. if (i >= KERNEL_PGD_BOUNDARY)
  219. memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
  220. sizeof(pmd_t) * PTRS_PER_PMD);
  221. pud_populate(mm, pud, pmd);
  222. }
  223. }
  224. pgd_t *pgd_alloc(struct mm_struct *mm)
  225. {
  226. pgd_t *pgd;
  227. pmd_t *pmds[PREALLOCATED_PMDS];
  228. pgd = (pgd_t *)__get_free_page(PGALLOC_GFP);
  229. if (pgd == NULL)
  230. goto out;
  231. mm->pgd = pgd;
  232. if (preallocate_pmds(pmds) != 0)
  233. goto out_free_pgd;
  234. if (paravirt_pgd_alloc(mm) != 0)
  235. goto out_free_pmds;
  236. /*
  237. * Make sure that pre-populating the pmds is atomic with
  238. * respect to anything walking the pgd_list, so that they
  239. * never see a partially populated pgd.
  240. */
  241. spin_lock(&pgd_lock);
  242. pgd_ctor(mm, pgd);
  243. pgd_prepopulate_pmd(mm, pgd, pmds);
  244. spin_unlock(&pgd_lock);
  245. return pgd;
  246. out_free_pmds:
  247. free_pmds(pmds);
  248. out_free_pgd:
  249. free_page((unsigned long)pgd);
  250. out:
  251. return NULL;
  252. }
  253. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  254. {
  255. pgd_mop_up_pmds(mm, pgd);
  256. pgd_dtor(pgd);
  257. paravirt_pgd_free(mm, pgd);
  258. free_page((unsigned long)pgd);
  259. }
  260. /*
  261. * Used to set accessed or dirty bits in the page table entries
  262. * on other architectures. On x86, the accessed and dirty bits
  263. * are tracked by hardware. However, do_wp_page calls this function
  264. * to also make the pte writeable at the same time the dirty bit is
  265. * set. In that case we do actually need to write the PTE.
  266. */
  267. int ptep_set_access_flags(struct vm_area_struct *vma,
  268. unsigned long address, pte_t *ptep,
  269. pte_t entry, int dirty)
  270. {
  271. int changed = !pte_same(*ptep, entry);
  272. if (changed && dirty) {
  273. *ptep = entry;
  274. pte_update_defer(vma->vm_mm, address, ptep);
  275. }
  276. return changed;
  277. }
  278. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  279. int pmdp_set_access_flags(struct vm_area_struct *vma,
  280. unsigned long address, pmd_t *pmdp,
  281. pmd_t entry, int dirty)
  282. {
  283. int changed = !pmd_same(*pmdp, entry);
  284. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  285. if (changed && dirty) {
  286. *pmdp = entry;
  287. pmd_update_defer(vma->vm_mm, address, pmdp);
  288. /*
  289. * We had a write-protection fault here and changed the pmd
  290. * to to more permissive. No need to flush the TLB for that,
  291. * #PF is architecturally guaranteed to do that and in the
  292. * worst-case we'll generate a spurious fault.
  293. */
  294. }
  295. return changed;
  296. }
  297. #endif
  298. int ptep_test_and_clear_young(struct vm_area_struct *vma,
  299. unsigned long addr, pte_t *ptep)
  300. {
  301. int ret = 0;
  302. if (pte_young(*ptep))
  303. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  304. (unsigned long *) &ptep->pte);
  305. if (ret)
  306. pte_update(vma->vm_mm, addr, ptep);
  307. return ret;
  308. }
  309. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  310. int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  311. unsigned long addr, pmd_t *pmdp)
  312. {
  313. int ret = 0;
  314. if (pmd_young(*pmdp))
  315. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  316. (unsigned long *)pmdp);
  317. if (ret)
  318. pmd_update(vma->vm_mm, addr, pmdp);
  319. return ret;
  320. }
  321. #endif
  322. int ptep_clear_flush_young(struct vm_area_struct *vma,
  323. unsigned long address, pte_t *ptep)
  324. {
  325. int young;
  326. young = ptep_test_and_clear_young(vma, address, ptep);
  327. if (young)
  328. flush_tlb_page(vma, address);
  329. return young;
  330. }
  331. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  332. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  333. unsigned long address, pmd_t *pmdp)
  334. {
  335. int young;
  336. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  337. young = pmdp_test_and_clear_young(vma, address, pmdp);
  338. if (young)
  339. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  340. return young;
  341. }
  342. void pmdp_splitting_flush(struct vm_area_struct *vma,
  343. unsigned long address, pmd_t *pmdp)
  344. {
  345. int set;
  346. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  347. set = !test_and_set_bit(_PAGE_BIT_SPLITTING,
  348. (unsigned long *)pmdp);
  349. if (set) {
  350. pmd_update(vma->vm_mm, address, pmdp);
  351. /* need tlb flush only to serialize against gup-fast */
  352. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  353. }
  354. }
  355. #endif
  356. /**
  357. * reserve_top_address - reserves a hole in the top of kernel address space
  358. * @reserve - size of hole to reserve
  359. *
  360. * Can be used to relocate the fixmap area and poke a hole in the top
  361. * of kernel address space to make room for a hypervisor.
  362. */
  363. void __init reserve_top_address(unsigned long reserve)
  364. {
  365. #ifdef CONFIG_X86_32
  366. BUG_ON(fixmaps_set > 0);
  367. printk(KERN_INFO "Reserving virtual address space above 0x%08x\n",
  368. (int)-reserve);
  369. __FIXADDR_TOP = -reserve - PAGE_SIZE;
  370. #endif
  371. }
  372. int fixmaps_set;
  373. void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)
  374. {
  375. unsigned long address = __fix_to_virt(idx);
  376. if (idx >= __end_of_fixed_addresses) {
  377. BUG();
  378. return;
  379. }
  380. set_pte_vaddr(address, pte);
  381. fixmaps_set++;
  382. }
  383. void native_set_fixmap(enum fixed_addresses idx, phys_addr_t phys,
  384. pgprot_t flags)
  385. {
  386. __native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
  387. }