shared.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include "shared.h"
  3. bool fail_prealloc;
  4. unsigned long mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
  5. unsigned long dac_mmap_min_addr = CONFIG_DEFAULT_MMAP_MIN_ADDR;
  6. unsigned long stack_guard_gap = 256UL<<PAGE_SHIFT;
  7. const struct vm_operations_struct vma_dummy_vm_ops;
  8. struct anon_vma dummy_anon_vma;
  9. struct task_struct __current;
  10. struct vm_area_struct *alloc_vma(struct mm_struct *mm,
  11. unsigned long start, unsigned long end,
  12. pgoff_t pgoff, vm_flags_t vm_flags)
  13. {
  14. struct vm_area_struct *vma = vm_area_alloc(mm);
  15. if (vma == NULL)
  16. return NULL;
  17. vma->vm_start = start;
  18. vma->vm_end = end;
  19. vma->vm_pgoff = pgoff;
  20. vm_flags_reset(vma, vm_flags);
  21. vma_assert_detached(vma);
  22. return vma;
  23. }
  24. void detach_free_vma(struct vm_area_struct *vma)
  25. {
  26. vma_mark_detached(vma);
  27. vm_area_free(vma);
  28. }
  29. struct vm_area_struct *alloc_and_link_vma(struct mm_struct *mm,
  30. unsigned long start, unsigned long end,
  31. pgoff_t pgoff, vm_flags_t vm_flags)
  32. {
  33. struct vm_area_struct *vma = alloc_vma(mm, start, end, pgoff, vm_flags);
  34. if (vma == NULL)
  35. return NULL;
  36. if (attach_vma(mm, vma)) {
  37. detach_free_vma(vma);
  38. return NULL;
  39. }
  40. /*
  41. * Reset this counter which we use to track whether writes have
  42. * begun. Linking to the tree will have caused this to be incremented,
  43. * which means we will get a false positive otherwise.
  44. */
  45. vma->vm_lock_seq = UINT_MAX;
  46. return vma;
  47. }
  48. void reset_dummy_anon_vma(void)
  49. {
  50. dummy_anon_vma.was_cloned = false;
  51. dummy_anon_vma.was_unlinked = false;
  52. }
  53. int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi)
  54. {
  55. struct vm_area_struct *vma;
  56. int count = 0;
  57. fail_prealloc = false;
  58. reset_dummy_anon_vma();
  59. vma_iter_set(vmi, 0);
  60. for_each_vma(*vmi, vma) {
  61. detach_free_vma(vma);
  62. count++;
  63. }
  64. mtree_destroy(&mm->mm_mt);
  65. mm->map_count = 0;
  66. return count;
  67. }
  68. bool vma_write_started(struct vm_area_struct *vma)
  69. {
  70. int seq = vma->vm_lock_seq;
  71. /* We reset after each check. */
  72. vma->vm_lock_seq = UINT_MAX;
  73. /* The vma_start_write() stub simply increments this value. */
  74. return seq > -1;
  75. }
  76. void __vma_set_dummy_anon_vma(struct vm_area_struct *vma,
  77. struct anon_vma_chain *avc, struct anon_vma *anon_vma)
  78. {
  79. vma->anon_vma = anon_vma;
  80. INIT_LIST_HEAD(&vma->anon_vma_chain);
  81. list_add(&avc->same_vma, &vma->anon_vma_chain);
  82. avc->anon_vma = vma->anon_vma;
  83. }
  84. void vma_set_dummy_anon_vma(struct vm_area_struct *vma,
  85. struct anon_vma_chain *avc)
  86. {
  87. __vma_set_dummy_anon_vma(vma, avc, &dummy_anon_vma);
  88. }
  89. struct task_struct *get_current(void)
  90. {
  91. return &__current;
  92. }
  93. unsigned long rlimit(unsigned int limit)
  94. {
  95. return (unsigned long)-1;
  96. }
  97. void vma_set_range(struct vm_area_struct *vma,
  98. unsigned long start, unsigned long end,
  99. pgoff_t pgoff)
  100. {
  101. vma->vm_start = start;
  102. vma->vm_end = end;
  103. vma->vm_pgoff = pgoff;
  104. }