vma_init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Functions for initializing, allocating, freeing and duplicating VMAs. Shared
  4. * between CONFIG_MMU and non-CONFIG_MMU kernel configurations.
  5. */
  6. #include "vma_internal.h"
  7. #include "vma.h"
  8. /* SLAB cache for vm_area_struct structures */
  9. static struct kmem_cache *vm_area_cachep;
  10. void __init vma_state_init(void)
  11. {
  12. struct kmem_cache_args args = {
  13. .use_freeptr_offset = true,
  14. .freeptr_offset = offsetof(struct vm_area_struct, vm_freeptr),
  15. .sheaf_capacity = 32,
  16. };
  17. vm_area_cachep = kmem_cache_create("vm_area_struct",
  18. sizeof(struct vm_area_struct), &args,
  19. SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU|
  20. SLAB_ACCOUNT);
  21. }
  22. struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)
  23. {
  24. struct vm_area_struct *vma;
  25. vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  26. if (!vma)
  27. return NULL;
  28. vma_init(vma, mm);
  29. return vma;
  30. }
  31. static void vm_area_init_from(const struct vm_area_struct *src,
  32. struct vm_area_struct *dest)
  33. {
  34. dest->vm_mm = src->vm_mm;
  35. dest->vm_ops = src->vm_ops;
  36. dest->vm_start = src->vm_start;
  37. dest->vm_end = src->vm_end;
  38. dest->anon_vma = src->anon_vma;
  39. dest->vm_pgoff = src->vm_pgoff;
  40. dest->vm_file = src->vm_file;
  41. dest->vm_private_data = src->vm_private_data;
  42. vm_flags_init(dest, src->vm_flags);
  43. memcpy(&dest->vm_page_prot, &src->vm_page_prot,
  44. sizeof(dest->vm_page_prot));
  45. /*
  46. * src->shared.rb may be modified concurrently when called from
  47. * dup_mmap(), but the clone will reinitialize it.
  48. */
  49. data_race(memcpy(&dest->shared, &src->shared, sizeof(dest->shared)));
  50. memcpy(&dest->vm_userfaultfd_ctx, &src->vm_userfaultfd_ctx,
  51. sizeof(dest->vm_userfaultfd_ctx));
  52. #ifdef CONFIG_ANON_VMA_NAME
  53. dest->anon_name = src->anon_name;
  54. #endif
  55. #ifdef CONFIG_SWAP
  56. memcpy(&dest->swap_readahead_info, &src->swap_readahead_info,
  57. sizeof(dest->swap_readahead_info));
  58. #endif
  59. #ifndef CONFIG_MMU
  60. dest->vm_region = src->vm_region;
  61. #endif
  62. #ifdef CONFIG_NUMA
  63. dest->vm_policy = src->vm_policy;
  64. #endif
  65. #ifdef __HAVE_PFNMAP_TRACKING
  66. dest->pfnmap_track_ctx = NULL;
  67. #endif
  68. }
  69. #ifdef __HAVE_PFNMAP_TRACKING
  70. static inline int vma_pfnmap_track_ctx_dup(struct vm_area_struct *orig,
  71. struct vm_area_struct *new)
  72. {
  73. struct pfnmap_track_ctx *ctx = orig->pfnmap_track_ctx;
  74. if (likely(!ctx))
  75. return 0;
  76. /*
  77. * We don't expect to ever hit this. If ever required, we would have
  78. * to duplicate the tracking.
  79. */
  80. if (unlikely(kref_read(&ctx->kref) >= REFCOUNT_MAX))
  81. return -ENOMEM;
  82. kref_get(&ctx->kref);
  83. new->pfnmap_track_ctx = ctx;
  84. return 0;
  85. }
  86. static inline void vma_pfnmap_track_ctx_release(struct vm_area_struct *vma)
  87. {
  88. struct pfnmap_track_ctx *ctx = vma->pfnmap_track_ctx;
  89. if (likely(!ctx))
  90. return;
  91. kref_put(&ctx->kref, pfnmap_track_ctx_release);
  92. vma->pfnmap_track_ctx = NULL;
  93. }
  94. #else
  95. static inline int vma_pfnmap_track_ctx_dup(struct vm_area_struct *orig,
  96. struct vm_area_struct *new)
  97. {
  98. return 0;
  99. }
  100. static inline void vma_pfnmap_track_ctx_release(struct vm_area_struct *vma)
  101. {
  102. }
  103. #endif
  104. struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig)
  105. {
  106. struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
  107. if (!new)
  108. return NULL;
  109. ASSERT_EXCLUSIVE_WRITER(orig->vm_flags);
  110. ASSERT_EXCLUSIVE_WRITER(orig->vm_file);
  111. vm_area_init_from(orig, new);
  112. if (vma_pfnmap_track_ctx_dup(orig, new)) {
  113. kmem_cache_free(vm_area_cachep, new);
  114. return NULL;
  115. }
  116. vma_lock_init(new, true);
  117. INIT_LIST_HEAD(&new->anon_vma_chain);
  118. vma_numab_state_init(new);
  119. dup_anon_vma_name(orig, new);
  120. return new;
  121. }
  122. void vm_area_free(struct vm_area_struct *vma)
  123. {
  124. /* The vma should be detached while being destroyed. */
  125. vma_assert_detached(vma);
  126. vma_numab_state_free(vma);
  127. free_anon_vma_name(vma);
  128. vma_pfnmap_track_ctx_release(vma);
  129. kmem_cache_free(vm_area_cachep, vma);
  130. }