pgtable.c 21 KB

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