pgtable.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/mm.h>
  3. #include <linux/slab.h>
  4. #define PGALLOC_GFP GFP_KERNEL | __GFP_ZERO
  5. static struct kmem_cache *pgd_cachep;
  6. #if PAGETABLE_LEVELS > 2
  7. static struct kmem_cache *pmd_cachep;
  8. #endif
  9. void pgd_ctor(void *x)
  10. {
  11. pgd_t *pgd = x;
  12. memcpy(pgd + USER_PTRS_PER_PGD,
  13. swapper_pg_dir + USER_PTRS_PER_PGD,
  14. (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
  15. }
  16. void pgtable_cache_init(void)
  17. {
  18. pgd_cachep = kmem_cache_create("pgd_cache",
  19. PTRS_PER_PGD * (1<<PTE_MAGNITUDE),
  20. PAGE_SIZE, SLAB_PANIC, pgd_ctor);
  21. #if PAGETABLE_LEVELS > 2
  22. pmd_cachep = kmem_cache_create("pmd_cache",
  23. PTRS_PER_PMD * (1<<PTE_MAGNITUDE),
  24. PAGE_SIZE, SLAB_PANIC, NULL);
  25. #endif
  26. }
  27. pgd_t *pgd_alloc(struct mm_struct *mm)
  28. {
  29. return kmem_cache_alloc(pgd_cachep, PGALLOC_GFP);
  30. }
  31. void pgd_free(struct mm_struct *mm, pgd_t *pgd)
  32. {
  33. kmem_cache_free(pgd_cachep, pgd);
  34. }
  35. #if PAGETABLE_LEVELS > 2
  36. void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
  37. {
  38. set_pud(pud, __pud((unsigned long)pmd));
  39. }
  40. pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
  41. {
  42. return kmem_cache_alloc(pmd_cachep, PGALLOC_GFP);
  43. }
  44. void pmd_free(struct mm_struct *mm, pmd_t *pmd)
  45. {
  46. kmem_cache_free(pmd_cachep, pmd);
  47. }
  48. #endif /* PAGETABLE_LEVELS > 2 */