pgtable.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. #include <linux/mm.h>
  2. #include <linux/gfp.h>
  3. #include <linux/hugetlb.h>
  4. #include <asm/pgalloc.h>
  5. #include <asm/pgtable.h>
  6. #include <asm/tlb.h>
  7. #include <asm/fixmap.h>
  8. #include <asm/mtrr.h>
  9. #define PGALLOC_GFP (GFP_KERNEL_ACCOUNT | __GFP_NOTRACK | __GFP_ZERO)
  10. #ifdef CONFIG_HIGHPTE
  11. #define PGALLOC_USER_GFP __GFP_HIGHMEM
  12. #else
  13. #define PGALLOC_USER_GFP 0
  14. #endif
  15. gfp_t __userpte_alloc_gfp = PGALLOC_GFP | PGALLOC_USER_GFP;
  16. pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  17. {
  18. return (pte_t *)__get_free_page(PGALLOC_GFP & ~__GFP_ACCOUNT);
  19. }
  20. pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
  21. {
  22. struct page *pte;
  23. pte = alloc_pages(__userpte_alloc_gfp, 0);
  24. if (!pte)
  25. return NULL;
  26. if (!pgtable_page_ctor(pte)) {
  27. __free_page(pte);
  28. return NULL;
  29. }
  30. return pte;
  31. }
  32. static int __init setup_userpte(char *arg)
  33. {
  34. if (!arg)
  35. return -EINVAL;
  36. /*
  37. * "userpte=nohigh" disables allocation of user pagetables in
  38. * high memory.
  39. */
  40. if (strcmp(arg, "nohigh") == 0)
  41. __userpte_alloc_gfp &= ~__GFP_HIGHMEM;
  42. else
  43. return -EINVAL;
  44. return 0;
  45. }
  46. early_param("userpte", setup_userpte);
  47. void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte)
  48. {
  49. pgtable_page_dtor(pte);
  50. paravirt_release_pte(page_to_pfn(pte));
  51. tlb_remove_page(tlb, pte);
  52. }
  53. #if CONFIG_PGTABLE_LEVELS > 2
  54. void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd)
  55. {
  56. struct page *page = virt_to_page(pmd);
  57. paravirt_release_pmd(__pa(pmd) >> PAGE_SHIFT);
  58. /*
  59. * NOTE! For PAE, any changes to the top page-directory-pointer-table
  60. * entries need a full cr3 reload to flush.
  61. */
  62. #ifdef CONFIG_X86_PAE
  63. tlb->need_flush_all = 1;
  64. #endif
  65. pgtable_pmd_page_dtor(page);
  66. tlb_remove_page(tlb, page);
  67. }
  68. #if CONFIG_PGTABLE_LEVELS > 3
  69. void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud)
  70. {
  71. paravirt_release_pud(__pa(pud) >> PAGE_SHIFT);
  72. tlb_remove_page(tlb, virt_to_page(pud));
  73. }
  74. #endif /* CONFIG_PGTABLE_LEVELS > 3 */
  75. #endif /* CONFIG_PGTABLE_LEVELS > 2 */
  76. static inline void pgd_list_add(pgd_t *pgd)
  77. {
  78. struct page *page = virt_to_page(pgd);
  79. list_add(&page->lru, &pgd_list);
  80. }
  81. static inline void pgd_list_del(pgd_t *pgd)
  82. {
  83. struct page *page = virt_to_page(pgd);
  84. list_del(&page->lru);
  85. }
  86. #define UNSHARED_PTRS_PER_PGD \
  87. (SHARED_KERNEL_PMD ? KERNEL_PGD_BOUNDARY : PTRS_PER_PGD)
  88. static void pgd_set_mm(pgd_t *pgd, struct mm_struct *mm)
  89. {
  90. BUILD_BUG_ON(sizeof(virt_to_page(pgd)->index) < sizeof(mm));
  91. virt_to_page(pgd)->index = (pgoff_t)mm;
  92. }
  93. struct mm_struct *pgd_page_get_mm(struct page *page)
  94. {
  95. return (struct mm_struct *)page->index;
  96. }
  97. static void pgd_ctor(struct mm_struct *mm, pgd_t *pgd)
  98. {
  99. /* If the pgd points to a shared pagetable level (either the
  100. ptes in non-PAE, or shared PMD in PAE), then just copy the
  101. references from swapper_pg_dir. */
  102. if (CONFIG_PGTABLE_LEVELS == 2 ||
  103. (CONFIG_PGTABLE_LEVELS == 3 && SHARED_KERNEL_PMD) ||
  104. CONFIG_PGTABLE_LEVELS == 4) {
  105. clone_pgd_range(pgd + KERNEL_PGD_BOUNDARY,
  106. swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  107. KERNEL_PGD_PTRS);
  108. }
  109. /* list required to sync kernel mapping updates */
  110. if (!SHARED_KERNEL_PMD) {
  111. pgd_set_mm(pgd, mm);
  112. pgd_list_add(pgd);
  113. }
  114. }
  115. static void pgd_dtor(pgd_t *pgd)
  116. {
  117. if (SHARED_KERNEL_PMD)
  118. return;
  119. spin_lock(&pgd_lock);
  120. pgd_list_del(pgd);
  121. spin_unlock(&pgd_lock);
  122. }
  123. /*
  124. * List of all pgd's needed for non-PAE so it can invalidate entries
  125. * in both cached and uncached pgd's; not needed for PAE since the
  126. * kernel pmd is shared. If PAE were not to share the pmd a similar
  127. * tactic would be needed. This is essentially codepath-based locking
  128. * against pageattr.c; it is the unique case in which a valid change
  129. * of kernel pagetables can't be lazily synchronized by vmalloc faults.
  130. * vmalloc faults work because attached pagetables are never freed.
  131. * -- nyc
  132. */
  133. #ifdef CONFIG_X86_PAE
  134. /*
  135. * In PAE mode, we need to do a cr3 reload (=tlb flush) when
  136. * updating the top-level pagetable entries to guarantee the
  137. * processor notices the update. Since this is expensive, and
  138. * all 4 top-level entries are used almost immediately in a
  139. * new process's life, we just pre-populate them here.
  140. *
  141. * Also, if we're in a paravirt environment where the kernel pmd is
  142. * not shared between pagetables (!SHARED_KERNEL_PMDS), we allocate
  143. * and initialize the kernel pmds here.
  144. */
  145. #define PREALLOCATED_PMDS UNSHARED_PTRS_PER_PGD
  146. void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmd)
  147. {
  148. paravirt_alloc_pmd(mm, __pa(pmd) >> PAGE_SHIFT);
  149. /* Note: almost everything apart from _PAGE_PRESENT is
  150. reserved at the pmd (PDPT) level. */
  151. set_pud(pudp, __pud(__pa(pmd) | _PAGE_PRESENT));
  152. /*
  153. * According to Intel App note "TLBs, Paging-Structure Caches,
  154. * and Their Invalidation", April 2007, document 317080-001,
  155. * section 8.1: in PAE mode we explicitly have to flush the
  156. * TLB via cr3 if the top-level pgd is changed...
  157. */
  158. flush_tlb_mm(mm);
  159. }
  160. #else /* !CONFIG_X86_PAE */
  161. /* No need to prepopulate any pagetable entries in non-PAE modes. */
  162. #define PREALLOCATED_PMDS 0
  163. #endif /* CONFIG_X86_PAE */
  164. static void free_pmds(struct mm_struct *mm, pmd_t *pmds[])
  165. {
  166. int i;
  167. for(i = 0; i < PREALLOCATED_PMDS; i++)
  168. if (pmds[i]) {
  169. pgtable_pmd_page_dtor(virt_to_page(pmds[i]));
  170. free_page((unsigned long)pmds[i]);
  171. mm_dec_nr_pmds(mm);
  172. }
  173. }
  174. static int preallocate_pmds(struct mm_struct *mm, pmd_t *pmds[])
  175. {
  176. int i;
  177. bool failed = false;
  178. gfp_t gfp = PGALLOC_GFP;
  179. if (mm == &init_mm)
  180. gfp &= ~__GFP_ACCOUNT;
  181. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  182. pmd_t *pmd = (pmd_t *)__get_free_page(gfp);
  183. if (!pmd)
  184. failed = true;
  185. if (pmd && !pgtable_pmd_page_ctor(virt_to_page(pmd))) {
  186. free_page((unsigned long)pmd);
  187. pmd = NULL;
  188. failed = true;
  189. }
  190. if (pmd)
  191. mm_inc_nr_pmds(mm);
  192. pmds[i] = pmd;
  193. }
  194. if (failed) {
  195. free_pmds(mm, pmds);
  196. return -ENOMEM;
  197. }
  198. return 0;
  199. }
  200. /*
  201. * Mop up any pmd pages which may still be attached to the pgd.
  202. * Normally they will be freed by munmap/exit_mmap, but any pmd we
  203. * preallocate which never got a corresponding vma will need to be
  204. * freed manually.
  205. */
  206. static void pgd_mop_up_pmds(struct mm_struct *mm, pgd_t *pgdp)
  207. {
  208. int i;
  209. for(i = 0; i < PREALLOCATED_PMDS; i++) {
  210. pgd_t pgd = pgdp[i];
  211. if (pgd_val(pgd) != 0) {
  212. pmd_t *pmd = (pmd_t *)pgd_page_vaddr(pgd);
  213. pgdp[i] = native_make_pgd(0);
  214. paravirt_release_pmd(pgd_val(pgd) >> PAGE_SHIFT);
  215. pmd_free(mm, pmd);
  216. mm_dec_nr_pmds(mm);
  217. }
  218. }
  219. }
  220. static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
  221. {
  222. pud_t *pud;
  223. int i;
  224. if (PREALLOCATED_PMDS == 0) /* Work around gcc-3.4.x bug */
  225. return;
  226. pud = pud_offset(pgd, 0);
  227. for (i = 0; i < PREALLOCATED_PMDS; i++, pud++) {
  228. pmd_t *pmd = pmds[i];
  229. if (i >= KERNEL_PGD_BOUNDARY)
  230. memcpy(pmd, (pmd_t *)pgd_page_vaddr(swapper_pg_dir[i]),
  231. sizeof(pmd_t) * PTRS_PER_PMD);
  232. pud_populate(mm, pud, pmd);
  233. }
  234. }
  235. /*
  236. * Xen paravirt assumes pgd table should be in one page. 64 bit kernel also
  237. * assumes that pgd should be in one page.
  238. *
  239. * But kernel with PAE paging that is not running as a Xen domain
  240. * only needs to allocate 32 bytes for pgd instead of one page.
  241. */
  242. #ifdef CONFIG_X86_PAE
  243. #include <linux/slab.h>
  244. #define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
  245. #define PGD_ALIGN 32
  246. static struct kmem_cache *pgd_cache;
  247. static int __init pgd_cache_init(void)
  248. {
  249. /*
  250. * When PAE kernel is running as a Xen domain, it does not use
  251. * shared kernel pmd. And this requires a whole page for pgd.
  252. */
  253. if (!SHARED_KERNEL_PMD)
  254. return 0;
  255. /*
  256. * when PAE kernel is not running as a Xen domain, it uses
  257. * shared kernel pmd. Shared kernel pmd does not require a whole
  258. * page for pgd. We are able to just allocate a 32-byte for pgd.
  259. * During boot time, we create a 32-byte slab for pgd table allocation.
  260. */
  261. pgd_cache = kmem_cache_create("pgd_cache", PGD_SIZE, PGD_ALIGN,
  262. SLAB_PANIC, NULL);
  263. if (!pgd_cache)
  264. return -ENOMEM;
  265. return 0;
  266. }
  267. core_initcall(pgd_cache_init);
  268. static inline pgd_t *_pgd_alloc(void)
  269. {
  270. /*
  271. * If no SHARED_KERNEL_PMD, PAE kernel is running as a Xen domain.
  272. * We allocate one page for pgd.
  273. */
  274. if (!SHARED_KERNEL_PMD)
  275. return (pgd_t *)__get_free_page(PGALLOC_GFP);
  276. /*
  277. * Now PAE kernel is not running as a Xen domain. We can allocate
  278. * a 32-byte slab for pgd to save memory space.
  279. */
  280. return kmem_cache_alloc(pgd_cache, PGALLOC_GFP);
  281. }
  282. static inline void _pgd_free(pgd_t *pgd)
  283. {
  284. if (!SHARED_KERNEL_PMD)
  285. free_page((unsigned long)pgd);
  286. else
  287. kmem_cache_free(pgd_cache, pgd);
  288. }
  289. #else
  290. static inline pgd_t *_pgd_alloc(void)
  291. {
  292. return (pgd_t *)__get_free_pages(PGALLOC_GFP, PGD_ALLOCATION_ORDER);
  293. }
  294. static inline void _pgd_free(pgd_t *pgd)
  295. {
  296. free_pages((unsigned long)pgd, PGD_ALLOCATION_ORDER);
  297. }
  298. #endif /* CONFIG_X86_PAE */
  299. pgd_t *pgd_alloc(struct mm_struct *mm)
  300. {
  301. pgd_t *pgd;
  302. pmd_t *pmds[PREALLOCATED_PMDS];
  303. pgd = _pgd_alloc();
  304. if (pgd == NULL)
  305. goto out;
  306. mm->pgd = pgd;
  307. if (preallocate_pmds(mm, pmds) != 0)
  308. goto out_free_pgd;
  309. if (paravirt_pgd_alloc(mm) != 0)
  310. goto out_free_pmds;
  311. /*
  312. * Make sure that pre-populating the pmds is atomic with
  313. * respect to anything walking the pgd_list, so that they
  314. * never see a partially populated pgd.
  315. */
  316. spin_lock(&pgd_lock);
  317. pgd_ctor(mm, pgd);
  318. pgd_prepopulate_pmd(mm, pgd, pmds);
  319. spin_unlock(&pgd_lock);
  320. return pgd;
  321. out_free_pmds:
  322. free_pmds(mm, pmds);
  323. out_free_pgd:
  324. _pgd_free(pgd);
  325. out:
  326. return NULL;
  327. }
  328. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  329. {
  330. pgd_mop_up_pmds(mm, pgd);
  331. pgd_dtor(pgd);
  332. paravirt_pgd_free(mm, pgd);
  333. _pgd_free(pgd);
  334. }
  335. /*
  336. * Used to set accessed or dirty bits in the page table entries
  337. * on other architectures. On x86, the accessed and dirty bits
  338. * are tracked by hardware. However, do_wp_page calls this function
  339. * to also make the pte writeable at the same time the dirty bit is
  340. * set. In that case we do actually need to write the PTE.
  341. */
  342. int ptep_set_access_flags(struct vm_area_struct *vma,
  343. unsigned long address, pte_t *ptep,
  344. pte_t entry, int dirty)
  345. {
  346. int changed = !pte_same(*ptep, entry);
  347. if (changed && dirty) {
  348. *ptep = entry;
  349. pte_update(vma->vm_mm, address, ptep);
  350. }
  351. return changed;
  352. }
  353. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  354. int pmdp_set_access_flags(struct vm_area_struct *vma,
  355. unsigned long address, pmd_t *pmdp,
  356. pmd_t entry, int dirty)
  357. {
  358. int changed = !pmd_same(*pmdp, entry);
  359. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  360. if (changed && dirty) {
  361. *pmdp = entry;
  362. /*
  363. * We had a write-protection fault here and changed the pmd
  364. * to to more permissive. No need to flush the TLB for that,
  365. * #PF is architecturally guaranteed to do that and in the
  366. * worst-case we'll generate a spurious fault.
  367. */
  368. }
  369. return changed;
  370. }
  371. #endif
  372. int ptep_test_and_clear_young(struct vm_area_struct *vma,
  373. unsigned long addr, pte_t *ptep)
  374. {
  375. int ret = 0;
  376. if (pte_young(*ptep))
  377. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  378. (unsigned long *) &ptep->pte);
  379. if (ret)
  380. pte_update(vma->vm_mm, addr, ptep);
  381. return ret;
  382. }
  383. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  384. int pmdp_test_and_clear_young(struct vm_area_struct *vma,
  385. unsigned long addr, pmd_t *pmdp)
  386. {
  387. int ret = 0;
  388. if (pmd_young(*pmdp))
  389. ret = test_and_clear_bit(_PAGE_BIT_ACCESSED,
  390. (unsigned long *)pmdp);
  391. return ret;
  392. }
  393. #endif
  394. int ptep_clear_flush_young(struct vm_area_struct *vma,
  395. unsigned long address, pte_t *ptep)
  396. {
  397. /*
  398. * On x86 CPUs, clearing the accessed bit without a TLB flush
  399. * doesn't cause data corruption. [ It could cause incorrect
  400. * page aging and the (mistaken) reclaim of hot pages, but the
  401. * chance of that should be relatively low. ]
  402. *
  403. * So as a performance optimization don't flush the TLB when
  404. * clearing the accessed bit, it will eventually be flushed by
  405. * a context switch or a VM operation anyway. [ In the rare
  406. * event of it not getting flushed for a long time the delay
  407. * shouldn't really matter because there's no real memory
  408. * pressure for swapout to react to. ]
  409. */
  410. return ptep_test_and_clear_young(vma, address, ptep);
  411. }
  412. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  413. int pmdp_clear_flush_young(struct vm_area_struct *vma,
  414. unsigned long address, pmd_t *pmdp)
  415. {
  416. int young;
  417. VM_BUG_ON(address & ~HPAGE_PMD_MASK);
  418. young = pmdp_test_and_clear_young(vma, address, pmdp);
  419. if (young)
  420. flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
  421. return young;
  422. }
  423. #endif
  424. /**
  425. * reserve_top_address - reserves a hole in the top of kernel address space
  426. * @reserve - size of hole to reserve
  427. *
  428. * Can be used to relocate the fixmap area and poke a hole in the top
  429. * of kernel address space to make room for a hypervisor.
  430. */
  431. void __init reserve_top_address(unsigned long reserve)
  432. {
  433. #ifdef CONFIG_X86_32
  434. BUG_ON(fixmaps_set > 0);
  435. __FIXADDR_TOP = round_down(-reserve, 1 << PMD_SHIFT) - PAGE_SIZE;
  436. printk(KERN_INFO "Reserving virtual address space above 0x%08lx (rounded to 0x%08lx)\n",
  437. -reserve, __FIXADDR_TOP + PAGE_SIZE);
  438. #endif
  439. }
  440. int fixmaps_set;
  441. void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)
  442. {
  443. unsigned long address = __fix_to_virt(idx);
  444. if (idx >= __end_of_fixed_addresses) {
  445. BUG();
  446. return;
  447. }
  448. set_pte_vaddr(address, pte);
  449. fixmaps_set++;
  450. }
  451. void native_set_fixmap(enum fixed_addresses idx, phys_addr_t phys,
  452. pgprot_t flags)
  453. {
  454. __native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
  455. }
  456. #ifdef CONFIG_HAVE_ARCH_HUGE_VMAP
  457. /**
  458. * pud_set_huge - setup kernel PUD mapping
  459. *
  460. * MTRRs can override PAT memory types with 4KiB granularity. Therefore, this
  461. * function sets up a huge page only if any of the following conditions are met:
  462. *
  463. * - MTRRs are disabled, or
  464. *
  465. * - MTRRs are enabled and the range is completely covered by a single MTRR, or
  466. *
  467. * - MTRRs are enabled and the corresponding MTRR memory type is WB, which
  468. * has no effect on the requested PAT memory type.
  469. *
  470. * Callers should try to decrease page size (1GB -> 2MB -> 4K) if the bigger
  471. * page mapping attempt fails.
  472. *
  473. * Returns 1 on success and 0 on failure.
  474. */
  475. int pud_set_huge(pud_t *pud, phys_addr_t addr, pgprot_t prot)
  476. {
  477. u8 mtrr, uniform;
  478. mtrr = mtrr_type_lookup(addr, addr + PUD_SIZE, &uniform);
  479. if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
  480. (mtrr != MTRR_TYPE_WRBACK))
  481. return 0;
  482. /* Bail out if we are we on a populated non-leaf entry: */
  483. if (pud_present(*pud) && !pud_huge(*pud))
  484. return 0;
  485. prot = pgprot_4k_2_large(prot);
  486. set_pte((pte_t *)pud, pfn_pte(
  487. (u64)addr >> PAGE_SHIFT,
  488. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  489. return 1;
  490. }
  491. /**
  492. * pmd_set_huge - setup kernel PMD mapping
  493. *
  494. * See text over pud_set_huge() above.
  495. *
  496. * Returns 1 on success and 0 on failure.
  497. */
  498. int pmd_set_huge(pmd_t *pmd, phys_addr_t addr, pgprot_t prot)
  499. {
  500. u8 mtrr, uniform;
  501. mtrr = mtrr_type_lookup(addr, addr + PMD_SIZE, &uniform);
  502. if ((mtrr != MTRR_TYPE_INVALID) && (!uniform) &&
  503. (mtrr != MTRR_TYPE_WRBACK)) {
  504. pr_warn_once("%s: Cannot satisfy [mem %#010llx-%#010llx] with a huge-page mapping due to MTRR override.\n",
  505. __func__, addr, addr + PMD_SIZE);
  506. return 0;
  507. }
  508. /* Bail out if we are we on a populated non-leaf entry: */
  509. if (pmd_present(*pmd) && !pmd_huge(*pmd))
  510. return 0;
  511. prot = pgprot_4k_2_large(prot);
  512. set_pte((pte_t *)pmd, pfn_pte(
  513. (u64)addr >> PAGE_SHIFT,
  514. __pgprot(pgprot_val(prot) | _PAGE_PSE)));
  515. return 1;
  516. }
  517. /**
  518. * pud_clear_huge - clear kernel PUD mapping when it is set
  519. *
  520. * Returns 1 on success and 0 on failure (no PUD map is found).
  521. */
  522. int pud_clear_huge(pud_t *pud)
  523. {
  524. if (pud_large(*pud)) {
  525. pud_clear(pud);
  526. return 1;
  527. }
  528. return 0;
  529. }
  530. /**
  531. * pmd_clear_huge - clear kernel PMD mapping when it is set
  532. *
  533. * Returns 1 on success and 0 on failure (no PMD map is found).
  534. */
  535. int pmd_clear_huge(pmd_t *pmd)
  536. {
  537. if (pmd_large(*pmd)) {
  538. pmd_clear(pmd);
  539. return 1;
  540. }
  541. return 0;
  542. }
  543. /**
  544. * pud_free_pmd_page - Clear pud entry and free pmd page.
  545. * @pud: Pointer to a PUD.
  546. *
  547. * Context: The pud range has been unmaped and TLB purged.
  548. * Return: 1 if clearing the entry succeeded. 0 otherwise.
  549. */
  550. int pud_free_pmd_page(pud_t *pud)
  551. {
  552. pmd_t *pmd;
  553. int i;
  554. if (pud_none(*pud))
  555. return 1;
  556. pmd = (pmd_t *)pud_page_vaddr(*pud);
  557. for (i = 0; i < PTRS_PER_PMD; i++)
  558. if (!pmd_free_pte_page(&pmd[i]))
  559. return 0;
  560. pud_clear(pud);
  561. free_page((unsigned long)pmd);
  562. return 1;
  563. }
  564. /**
  565. * pmd_free_pte_page - Clear pmd entry and free pte page.
  566. * @pmd: Pointer to a PMD.
  567. *
  568. * Context: The pmd range has been unmaped and TLB purged.
  569. * Return: 1 if clearing the entry succeeded. 0 otherwise.
  570. */
  571. int pmd_free_pte_page(pmd_t *pmd)
  572. {
  573. pte_t *pte;
  574. if (pmd_none(*pmd))
  575. return 1;
  576. pte = (pte_t *)pmd_page_vaddr(*pmd);
  577. pmd_clear(pmd);
  578. free_page((unsigned long)pte);
  579. return 1;
  580. }
  581. #endif /* CONFIG_HAVE_ARCH_HUGE_VMAP */