pgtable.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mm.h>
  3. #include <linux/gfp.h>
  4. #include <linux/hugetlb.h>
  5. #include <asm/pgalloc.h>
  6. #include <asm/tlb.h>
  7. #include <asm/fixmap.h>
  8. #include <asm/mtrr.h>
  9. #ifdef CONFIG_DYNAMIC_PHYSICAL_MASK
  10. phys_addr_t physical_mask __ro_after_init = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;
  11. EXPORT_SYMBOL(physical_mask);
  12. #endif
  13. #ifdef CONFIG_HIGHPTE
  14. #define PGTABLE_HIGHMEM __GFP_HIGHMEM
  15. #else
  16. #define PGTABLE_HIGHMEM 0
  17. #endif
  18. #ifndef CONFIG_PARAVIRT
  19. static inline
  20. void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table)
  21. {
  22. tlb_remove_page(tlb, table);
  23. }
  24. #endif
  25. gfp_t __userpte_alloc_gfp = GFP_PGTABLE_USER | PGTABLE_HIGHMEM;
  26. pgtable_t pte_alloc_one(struct mm_struct *mm)
  27. {
  28. return __pte_alloc_one(mm, __userpte_alloc_gfp);
  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_pte_page_dtor(pte);
  48. paravirt_release_pte(page_to_pfn(pte));
  49. paravirt_tlb_remove_table(tlb, pte);
  50. }
  51. #if CONFIG_PGTABLE_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. paravirt_tlb_remove_table(tlb, page);
  65. }
  66. #if CONFIG_PGTABLE_LEVELS > 3
  67. void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
  68. {
  69. paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
  70. paravirt_tlb_remove_table(tlb, virt_to_page(pud));
  71. }
  72. #if CONFIG_PGTABLE_LEVELS > 4
  73. void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d)
  74. {
  75. paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT);
  76. paravirt_tlb_remove_table(tlb, virt_to_page(p4d));
  77. }
  78. #endif /* CONFIG_PGTABLE_LEVELS > 4 */
  79. #endif /* CONFIG_PGTABLE_LEVELS > 3 */
  80. #endif /* CONFIG_PGTABLE_LEVELS > 2 */
  81. static inline void pgd_list_add(pgd_t *pgd)
  82. {
  83. struct page *page = virt_to_page(pgd);
  84. list_add(&page->lru, &pgd_list);
  85. }
  86. static inline void pgd_list_del(pgd_t *pgd)
  87. {
  88. struct page *page = virt_to_page(pgd);
  89. list_del(&page->lru);
  90. }
  91. #define UNSHARED_PTRS_PER_PGD \
  92. (SHARED_KERNEL_PMD ? KERNEL_PGD_BOUNDARY : PTRS_PER_PGD)
  93. #define MAX_UNSHARED_PTRS_PER_PGD \
  94. max_t(size_t, KERNEL_PGD_BOUNDARY, PTRS_PER_PGD)
  95. static void pgd_set_mm(pgd_t *pgd, struct mm_struct *mm)
  96. {
  97. virt_to_page(pgd)->pt_mm = mm;
  98. }
  99. struct mm_struct *pgd_page_get_mm(struct page *page)
  100. {
  101. return page->pt_mm;
  102. }
  103. static void pgd_ctor(struct mm_struct *mm, pgd_t *pgd)
  104. {
  105. /* If the pgd points to a shared pagetable level (either the
  106. ptes in non-PAE, or shared PMD in PAE), then just copy the
  107. references from swapper_pg_dir. */
  108. if (CONFIG_PGTABLE_LEVELS == 2 ||
  109. (CONFIG_PGTABLE_LEVELS == 3 && SHARED_KERNEL_PMD) ||
  110. CONFIG_PGTABLE_LEVELS >= 4) {
  111. clone_pgd_range(pgd + KERNEL_PGD_BOUNDARY,
  112. swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  113. KERNEL_PGD_PTRS);
  114. }
  115. /* list required to sync kernel mapping updates */
  116. if (!SHARED_KERNEL_PMD) {
  117. pgd_set_mm(pgd, mm);
  118. pgd_list_add(pgd);
  119. }
  120. }
  121. static void pgd_dtor(pgd_t *pgd)
  122. {
  123. if (SHARED_KERNEL_PMD)
  124. return;
  125. spin_lock(&pgd_lock);
  126. pgd_list_del(pgd);
  127. spin_unlock(&pgd_lock);
  128. }
  129. /*
  130. * List of all pgd's needed for non-PAE so it can invalidate entries
  131. * in both cached and uncached pgd's; not needed for PAE since the
  132. * kernel pmd is shared. If PAE were not to share the pmd a similar
  133. * tactic would be needed. This is essentially codepath-based locking
  134. * against pageattr.c; it is the unique case in which a valid change
  135. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  136. * vmalloc faults work because attached pagetables are never freed.
  137. * -- nyc
  138. */
  139. #ifdef CONFIG_X86_PAE
  140. /*
  141. * In PAE mode, we need to do a cr3 reload (=tlb flush) when
  142. * updating the top-level pagetable entries to guarantee the
  143. * processor notices the update. Since this is expensive, and
  144. * all 4 top-level entries are used almost immediately in a
  145. * new process's life, we just pre-populate them here.
  146. *
  147. * Also, if we're in a paravirt environment where the kernel pmd is
  148. * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
  149. * and initialize the kernel pmds here.
  150. */
  151. #define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD
  152. #define MAX_PREALLOCATED_PMDS MAX_UNSHARED_PTRS_PER_PGD
  153. /*
  154. * We allocate separate PMDs for the kernel part of the user page-table
  155. * when PTI is enabled. We need them to map the per-process LDT into the
  156. * user-space page-table.
  157. */
  158. #define PREALLOCATED_USER_PMDS (boot_cpu_has(X86_FEATURE_PTI) ? \
  159. KERNEL_PGD_PTRS : 0)
  160. #define MAX_PREALLOCATED_USER_PMDS KERNEL_PGD_PTRS
  161. void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
  162. {
  163. paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
  164. /* Note: almost everything apart from _PAGE_PRESENT is
  165. reserved at the pmd (PDPT) level. */
  166. set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
  167. /*
  168. * According to Intel App note "TLBs, Paging-Structure Caches,
  169. * and Their Invalidation", April 2007, document 317080-001,
  170. * section 8.1: in PAE mode we explicitly have to flush the
  171. * TLB via cr3 if the top-level pgd is changed...
  172. */
  173. flush_tlb_mm(mm);
  174. }
  175. #else /* !CONFIG_X86_PAE */
  176. /* No need to prepopulate any pagetable entries in non-PAE modes. */
  177. #define PREALLOCATED_PMDS 0
  178. #define MAX_PREALLOCATED_PMDS 0
  179. #define PREALLOCATED_USER_PMDS 0
  180. #define MAX_PREALLOCATED_USER_PMDS 0
  181. #endif /* CONFIG_X86_PAE */
  182. static void free_pmds(struct mm_struct *mm, pmd_t *pmds[], int count)
  183. {
  184. int i;
  185. for (i = 0; i < count; i++)
  186. if (pmds[i]) {
  187. pgtable_pmd_page_dtor(virt_to_page(pmds[i]));
  188. free_page((unsigned long)pmds[i]);
  189. mm_dec_nr_pmds(mm);
  190. }
  191. }
  192. static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[], int count)
  193. {
  194. int i;
  195. bool failed = false;
  196. gfp_t gfp = GFP_PGTABLE_USER;
  197. if (mm == &init_mm)
  198. gfp &= ~__GFP_ACCOUNT;
  199. for (i = 0; i < count; i++) {
  200. pmd_t *pmd = (pmd_t *)__get_free_page(gfp);
  201. if (!pmd)
  202. failed = true;
  203. if (pmd && !pgtable_pmd_page_ctor(virt_to_page(pmd))) {
  204. free_page((unsigned long)pmd);
  205. pmd = NULL;
  206. failed = true;
  207. }
  208. if (pmd)
  209. mm_inc_nr_pmds(mm);
  210. pmds[i] = pmd;
  211. }
  212. if (failed) {
  213. free_pmds(mm, pmds, count);
  214. return -ENOMEM;
  215. }
  216. return 0;
  217. }
  218. /*
  219. * Mop up any pmd pages which may still be attached to the pgd.
  220. * Normally they will be freed by munmap/exit_mmap, but any pmd we
  221. * preallocate which never got a corresponding vma will need to be
  222. * freed manually.
  223. */
  224. static void mop_up_one_pmd(struct mm_struct *mm, pgd_t *pgdp)
  225. {
  226. pgd_t pgd = *pgdp;
  227. if (pgd_val(pgd) != 0) {
  228. pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
  229. pgd_clear(pgdp);
  230. paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
  231. pmd_free(mm, pmd);
  232. mm_dec_nr_pmds(mm);
  233. }
  234. }
  235. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  236. {
  237. int i;
  238. for (i = 0; i < PREALLOCATED_PMDS; i++)
  239. mop_up_one_pmd(mm, &pgdp[i]);
  240. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  241. if (!boot_cpu_has(X86_FEATURE_PTI))
  242. return;
  243. pgdp = kernel_to_user_pgdp(pgdp);
  244. for (i = 0; i < PREALLOCATED_USER_PMDS; i++)
  245. mop_up_one_pmd(mm, &pgdp[i + KERNEL_PGD_BOUNDARY]);
  246. #endif
  247. }
  248. static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
  249. {
  250. p4d_t *p4d;
  251. pud_t *pud;
  252. int i;
  253. if (PREALLOCATED_PMDS == 0) /* Work around gcc-3.4.x bug */
  254. return;
  255. p4d = p4d_offset(pgd, 0);
  256. pud = pud_offset(p4d, 0);
  257. for (i = 0; i < PREALLOCATED_PMDS; i++, pud++) {
  258. pmd_t *pmd = pmds[i];
  259. if (i >= KERNEL_PGD_BOUNDARY)
  260. memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
  261. sizeof(pmd_t) * PTRS_PER_PMD);
  262. pud_populate(mm, pud, pmd);
  263. }
  264. }
  265. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  266. static void pgd_prepopulate_user_pmd(struct mm_struct *mm,
  267. pgd_t *k_pgd, pmd_t *pmds[])
  268. {
  269. pgd_t *s_pgd = kernel_to_user_pgdp(swapper_pg_dir);
  270. pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd);
  271. p4d_t *u_p4d;
  272. pud_t *u_pud;
  273. int i;
  274. u_p4d = p4d_offset(u_pgd, 0);
  275. u_pud = pud_offset(u_p4d, 0);
  276. s_pgd += KERNEL_PGD_BOUNDARY;
  277. u_pud += KERNEL_PGD_BOUNDARY;
  278. for (i = 0; i < PREALLOCATED_USER_PMDS; i++, u_pud++, s_pgd++) {
  279. pmd_t *pmd = pmds[i];
  280. memcpy(pmd, (pmd_t *)pgd_page_vaddr(*s_pgd),
  281. sizeof(pmd_t) * PTRS_PER_PMD);
  282. pud_populate(mm, u_pud, pmd);
  283. }
  284. }
  285. #else
  286. static void pgd_prepopulate_user_pmd(struct mm_struct *mm,
  287. pgd_t *k_pgd, pmd_t *pmds[])
  288. {
  289. }
  290. #endif
  291. /*
  292. * Xen paravirt assumes pgd table should be in one page. 64 bit kernel also
  293. * assumes that pgd should be in one page.
  294. *
  295. * But kernel with PAE paging that is not running as a Xen domain
  296. * only needs to allocate 32 bytes for pgd instead of one page.
  297. */
  298. #ifdef CONFIG_X86_PAE
  299. #include <linux/slab.h>
  300. #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
  301. #define PGD_ALIGN 32
  302. static struct kmem_cache *pgd_cache;
  303. void __init pgtable_cache_init(void)
  304. {
  305. /*
  306. * When PAE kernel is running as a Xen domain, it does not use
  307. * shared kernel pmd. And this requires a whole page for pgd.
  308. */
  309. if (!SHARED_KERNEL_PMD)
  310. return;
  311. /*
  312. * when PAE kernel is not running as a Xen domain, it uses
  313. * shared kernel pmd. Shared kernel pmd does not require a whole
  314. * page for pgd. We are able to just allocate a 32-byte for pgd.
  315. * During boot time, we create a 32-byte slab for pgd table allocation.
  316. */
  317. pgd_cache = kmem_cache_create("pgd_cache", PGD_SIZE, PGD_ALIGN,
  318. SLAB_PANIC, NULL);
  319. }
  320. static inline pgd_t *_pgd_alloc(void)
  321. {
  322. /*
  323. * If no SHARED_KERNEL_PMD, PAE kernel is running as a Xen domain.
  324. * We allocate one page for pgd.
  325. */
  326. if (!SHARED_KERNEL_PMD)
  327. return (pgd_t *)__get_free_pages(GFP_PGTABLE_USER,
  328. PGD_ALLOCATION_ORDER);
  329. /*
  330. * Now PAE kernel is not running as a Xen domain. We can allocate
  331. * a 32-byte slab for pgd to save memory space.
  332. */
  333. return kmem_cache_alloc(pgd_cache, GFP_PGTABLE_USER);
  334. }
  335. static inline void _pgd_free(pgd_t *pgd)
  336. {
  337. if (!SHARED_KERNEL_PMD)
  338. free_pages((unsigned long)pgd, PGD_ALLOCATION_ORDER);
  339. else
  340. kmem_cache_free(pgd_cache, pgd);
  341. }
  342. #else
  343. static inline pgd_t *_pgd_alloc(void)
  344. {
  345. return (pgd_t *)__get_free_pages(GFP_PGTABLE_USER,
  346. PGD_ALLOCATION_ORDER);
  347. }
  348. static inline void _pgd_free(pgd_t *pgd)
  349. {
  350. free_pages((unsigned long)pgd, PGD_ALLOCATION_ORDER);
  351. }
  352. #endif /* CONFIG_X86_PAE */
  353. pgd_t *pgd_alloc(struct mm_struct *mm)
  354. {
  355. pgd_t *pgd;
  356. pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS];
  357. pmd_t *pmds[MAX_PREALLOCATED_PMDS];
  358. pgd = _pgd_alloc();
  359. if (pgd == NULL)
  360. goto out;
  361. mm->pgd = pgd;
  362. if (preallocate_pmds(mm, pmds, PREALLOCATED_PMDS) != 0)
  363. goto out_free_pgd;
  364. if (preallocate_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS) != 0)
  365. goto out_free_pmds;
  366. if (paravirt_pgd_alloc(mm) != 0)
  367. goto out_free_user_pmds;
  368. /*
  369. * Make sure that pre-populating the pmds is atomic with
  370. * respect to anything walking the pgd_list, so that they
  371. * never see a partially populated pgd.
  372. */
  373. spin_lock(&pgd_lock);
  374. pgd_ctor(mm, pgd);
  375. pgd_prepopulate_pmd(mm, pgd, pmds);
  376. pgd_prepopulate_user_pmd(mm, pgd, u_pmds);
  377. spin_unlock(&pgd_lock);
  378. return pgd;
  379. out_free_user_pmds:
  380. free_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS);
  381. out_free_pmds:
  382. free_pmds(mm, pmds, PREALLOCATED_PMDS);
  383. out_free_pgd:
  384. _pgd_free(pgd);
  385. out:
  386. return NULL;
  387. }
  388. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  389. {
  390. pgd_mop_up_pmds(mm, pgd);
  391. pgd_dtor(pgd);
  392. paravirt_pgd_free(mm, pgd);
  393. _pgd_free(pgd);
  394. }
  395. /*
  396. * Used to set accessed or dirty bits in the page table entries
  397. * on other architectures. On x86, the accessed and dirty bits
  398. * are tracked by hardware. However, do_wp_page calls this function
  399. * to also make the pte writeable at the same time the dirty bit is
  400. * set. In that case we do actually need to write the PTE.
  401. */
  402. int ptep_set_access_flags(struct vm_area_struct *vma,
  403. unsigned long address, pte_t *ptep,
  404. pte_t entry, int dirty)
  405. {
  406. int changed = !pte_same(*ptep, entry);
  407. if (changed && dirty)
  408. set_pte(ptep, entry);
  409. return changed;
  410. }
  411. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  412. int pmdp_set_access_flags(struct vm_area_struct *vma,
  413. unsigned long address, pmd_t *pmdp,
  414. pmd_t entry, int dirty)
  415. {
  416. int changed = !pmd_same(*pmdp, entry);
  417. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  418. if (changed && dirty) {
  419. set_pmd(pmdp, entry);
  420. /*
  421. * We had a write-protection fault here and changed the pmd
  422. * to to more permissive. No need to flush the TLB for that,
  423. * #PF is architecturally guaranteed to do that and in the
  424. * worst-case we'll generate a spurious fault.
  425. */
  426. }
  427. return changed;
  428. }
  429. int pudp_set_access_flags(struct vm_area_struct *vma, unsigned long address,
  430. pud_t *pudp, pud_t entry, int dirty)
  431. {
  432. int changed = !pud_same(*pudp, entry);
  433. VM_BUG_ON(address & ~HPAGE_PUD_MASK);
  434. if (changed && dirty) {
  435. set_pud(pudp, entry);
  436. /*
  437. * We had a write-protection fault here and changed the pud
  438. * to to more permissive. No need to flush the TLB for that,
  439. * #PF is architecturally guaranteed to do that and in the
  440. * worst-case we'll generate a spurious fault.
  441. */
  442. }
  443. return changed;
  444. }
  445. #endif
  446. int ptep_test_and_clear_young(struct vm_area_struct *vma,
  447. unsigned long addr, pte_t *ptep)
  448. {
  449. int ret = 0;
  450. if (pte_young(*ptep))
  451. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  452. (unsigned long *) &ptep->pte);
  453. return ret;
  454. }
  455. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_ARCH_HAS_NONLEAF_PMD_YOUNG)
  456. int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  457. unsigned long addr, pmd_t *pmdp)
  458. {
  459. int ret = 0;
  460. if (pmd_young(*pmdp))
  461. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  462. (unsigned long *)pmdp);
  463. return ret;
  464. }
  465. #endif
  466. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  467. int pudp_test_and_clear_young(struct vm_area_struct *vma,
  468. unsigned long addr, pud_t *pudp)
  469. {
  470. int ret = 0;
  471. if (pud_young(*pudp))
  472. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  473. (unsigned long *)pudp);
  474. return ret;
  475. }
  476. #endif
  477. int ptep_clear_flush_young(struct vm_area_struct *vma,
  478. unsigned long address, pte_t *ptep)
  479. {
  480. /*
  481. * On x86 CPUs, clearing the accessed bit without a TLB flush
  482. * doesn't cause data corruption. [ It could cause incorrect
  483. * page aging and the (mistaken) reclaim of hot pages, but the
  484. * chance of that should be relatively low. ]
  485. *
  486. * So as a performance optimization don't flush the TLB when
  487. * clearing the accessed bit, it will eventually be flushed by
  488. * a context switch or a VM operation anyway. [ In the rare
  489. * event of it not getting flushed for a long time the delay
  490. * shouldn't really matter because there's no real memory
  491. * pressure for swapout to react to. ]
  492. */
  493. return ptep_test_and_clear_young(vma, address, ptep);
  494. }
  495. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  496. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  497. unsigned long address, pmd_t *pmdp)
  498. {
  499. int young;
  500. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  501. young = pmdp_test_and_clear_young(vma, address, pmdp);
  502. if (young)
  503. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  504. return young;
  505. }
  506. pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address,
  507. pmd_t *pmdp)
  508. {
  509. /*
  510. * No flush is necessary. Once an invalid PTE is established, the PTE's
  511. * access and dirty bits cannot be updated.
  512. */
  513. return pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp));
  514. }
  515. #endif
  516. /**
  517. * reserve_top_address - reserves a hole in the top of kernel address space
  518. * @reserve - size of hole to reserve
  519. *
  520. * Can be used to relocate the fixmap area and poke a hole in the top
  521. * of kernel address space to make room for a hypervisor.
  522. */
  523. void __init reserve_top_address(unsigned long reserve)
  524. {
  525. #ifdef CONFIG_X86_32
  526. BUG_ON(fixmaps_set > 0);
  527. __FIXADDR_TOP = round_down(-reserve, 1 << PMD_SHIFT) - PAGE_SIZE;
  528. printk(KERN_INFO "Reserving virtual address space above 0x%08lx (rounded to 0x%08lx)\n",
  529. -reserve, __FIXADDR_TOP + PAGE_SIZE);
  530. #endif
  531. }
  532. int fixmaps_set;
  533. void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)
  534. {
  535. unsigned long address = __fix_to_virt(idx);
  536. #ifdef CONFIG_X86_64
  537. /*
  538. * Ensure that the static initial page tables are covering the
  539. * fixmap completely.
  540. */
  541. BUILD_BUG_ON(__end_of_permanent_fixed_addresses >
  542. (FIXMAP_PMD_NUM * PTRS_PER_PTE));
  543. #endif
  544. if (idx >= __end_of_fixed_addresses) {
  545. BUG();
  546. return;
  547. }
  548. set_pte_vaddr(address, pte);
  549. fixmaps_set++;
  550. }
  551. void native_set_fixmap(unsigned /* enum fixed_addresses */ idx,
  552. phys_addr_t phys, pgprot_t flags)
  553. {
  554. /* Sanitize 'prot' against any unsupported bits: */
  555. pgprot_val(flags) &= __default_kernel_pte_mask;
  556. __native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
  557. }
  558. #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
  559. #ifdef CONFIG_X86_5LEVEL
  560. /**
  561. * p4d_set_huge - setup kernel P4D mapping
  562. *
  563. * No 512GB pages yet -- always return 0
  564. */
  565. int p4d_set_huge(p4d_t *p4d, phys_addr_t addr, pgprot_t prot)
  566. {
  567. return 0;
  568. }
  569. /**
  570. * p4d_clear_huge - clear kernel P4D mapping when it is set
  571. *
  572. * No 512GB pages yet -- always return 0
  573. */
  574. void p4d_clear_huge(p4d_t *p4d)
  575. {
  576. }
  577. #endif
  578. /**
  579. * pud_set_huge - setup kernel PUD mapping
  580. *
  581. * MTRRs can override PAT memory types with 4KiB granularity. Therefore, this
  582. * function sets up a huge page only if any of the following conditions are met:
  583. *
  584. * - MTRRs are disabled, or
  585. *
  586. * - MTRRs are enabled and the range is completely covered by a single MTRR, or
  587. *
  588. * - MTRRs are enabled and the corresponding MTRR memory type is WB, which
  589. * has no effect on the requested PAT memory type.
  590. *
  591. * Callers should try to decrease page size (1GB -> 2MB -> 4K) if the bigger
  592. * page mapping attempt fails.
  593. *
  594. * Returns 1 on success and 0 on failure.
  595. */
  596. int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
  597. {
  598. u8 mtrr, uniform;
  599. mtrr = mtrr_type_lookup(addr, addr + PUD_SIZE, &uniform);
  600. if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
  601. (mtrr != MTRR_TYPE_WRBACK))
  602. return 0;
  603. /* Bail out if we are we on a populated non-leaf entry: */
  604. if (pud_present(*pud) && !pud_huge(*pud))
  605. return 0;
  606. set_pte((pte_t *)pud, pfn_pte(
  607. (u64)addr >> PAGE_SHIFT,
  608. __pgprot(protval_4k_2_large(pgprot_val(prot)) | _PAGE_PSE)));
  609. return 1;
  610. }
  611. /**
  612. * pmd_set_huge - setup kernel PMD mapping
  613. *
  614. * See text over pud_set_huge() above.
  615. *
  616. * Returns 1 on success and 0 on failure.
  617. */
  618. int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
  619. {
  620. u8 mtrr, uniform;
  621. mtrr = mtrr_type_lookup(addr, addr + PMD_SIZE, &uniform);
  622. if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
  623. (mtrr != MTRR_TYPE_WRBACK)) {
  624. pr_warn_once("%s: Cannot satisfy [mem %#010llx-%#010llx] with a huge-page mapping due to MTRR override.\n",
  625. __func__, addr, addr + PMD_SIZE);
  626. return 0;
  627. }
  628. /* Bail out if we are we on a populated non-leaf entry: */
  629. if (pmd_present(*pmd) && !pmd_huge(*pmd))
  630. return 0;
  631. set_pte((pte_t *)pmd, pfn_pte(
  632. (u64)addr >> PAGE_SHIFT,
  633. __pgprot(protval_4k_2_large(pgprot_val(prot)) | _PAGE_PSE)));
  634. return 1;
  635. }
  636. /**
  637. * pud_clear_huge - clear kernel PUD mapping when it is set
  638. *
  639. * Returns 1 on success and 0 on failure (no PUD map is found).
  640. */
  641. int pud_clear_huge(pud_t *pud)
  642. {
  643. if (pud_large(*pud)) {
  644. pud_clear(pud);
  645. return 1;
  646. }
  647. return 0;
  648. }
  649. /**
  650. * pmd_clear_huge - clear kernel PMD mapping when it is set
  651. *
  652. * Returns 1 on success and 0 on failure (no PMD map is found).
  653. */
  654. int pmd_clear_huge(pmd_t *pmd)
  655. {
  656. if (pmd_large(*pmd)) {
  657. pmd_clear(pmd);
  658. return 1;
  659. }
  660. return 0;
  661. }
  662. #ifdef CONFIG_X86_64
  663. /**
  664. * pud_free_pmd_page - Clear pud entry and free pmd page.
  665. * @pud: Pointer to a PUD.
  666. * @addr: Virtual address associated with pud.
  667. *
  668. * Context: The pud range has been unmapped and TLB purged.
  669. * Return: 1 if clearing the entry succeeded. 0 otherwise.
  670. *
  671. * NOTE: Callers must allow a single page allocation.
  672. */
  673. int pud_free_pmd_page(pud_t *pud, unsigned long addr)
  674. {
  675. pmd_t *pmd, *pmd_sv;
  676. pte_t *pte;
  677. int i;
  678. pmd = pud_pgtable(*pud);
  679. pmd_sv = (pmd_t *)__get_free_page(GFP_KERNEL);
  680. if (!pmd_sv)
  681. return 0;
  682. for (i = 0; i < PTRS_PER_PMD; i++) {
  683. pmd_sv[i] = pmd[i];
  684. if (!pmd_none(pmd[i]))
  685. pmd_clear(&pmd[i]);
  686. }
  687. pud_clear(pud);
  688. /* INVLPG to clear all paging-structure caches */
  689. flush_tlb_kernel_range(addr, addr + PAGE_SIZE-1);
  690. for (i = 0; i < PTRS_PER_PMD; i++) {
  691. if (!pmd_none(pmd_sv[i])) {
  692. pte = (pte_t *)pmd_page_vaddr(pmd_sv[i]);
  693. free_page((unsigned long)pte);
  694. }
  695. }
  696. free_page((unsigned long)pmd_sv);
  697. pgtable_pmd_page_dtor(virt_to_page(pmd));
  698. free_page((unsigned long)pmd);
  699. return 1;
  700. }
  701. /**
  702. * pmd_free_pte_page - Clear pmd entry and free pte page.
  703. * @pmd: Pointer to a PMD.
  704. * @addr: Virtual address associated with pmd.
  705. *
  706. * Context: The pmd range has been unmapped and TLB purged.
  707. * Return: 1 if clearing the entry succeeded. 0 otherwise.
  708. */
  709. int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  710. {
  711. pte_t *pte;
  712. pte = (pte_t *)pmd_page_vaddr(*pmd);
  713. pmd_clear(pmd);
  714. /* INVLPG to clear all paging-structure caches */
  715. flush_tlb_kernel_range(addr, addr + PAGE_SIZE-1);
  716. free_page((unsigned long)pte);
  717. return 1;
  718. }
  719. #else /* !CONFIG_X86_64 */
  720. /*
  721. * Disable free page handling on x86-PAE. This assures that ioremap()
  722. * does not update sync'd pmd entries. See vmalloc_sync_one().
  723. */
  724. int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)
  725. {
  726. return pmd_none(*pmd);
  727. }
  728. #endif /* CONFIG_X86_64 */
  729. #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */