pgtable.c 21 KB

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