pgtable.c 12 KB

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