vma_exec.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Functions explicitly implemented for exec functionality which however are
  4. * explicitly VMA-only logic.
  5. */
  6. #include "vma_internal.h"
  7. #include "vma.h"
  8. /*
  9. * Relocate a VMA downwards by shift bytes. There cannot be any VMAs between
  10. * this VMA and its relocated range, which will now reside at [vma->vm_start -
  11. * shift, vma->vm_end - shift).
  12. *
  13. * This function is almost certainly NOT what you want for anything other than
  14. * early executable temporary stack relocation.
  15. */
  16. int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift)
  17. {
  18. /*
  19. * The process proceeds as follows:
  20. *
  21. * 1) Use shift to calculate the new vma endpoints.
  22. * 2) Extend vma to cover both the old and new ranges. This ensures the
  23. * arguments passed to subsequent functions are consistent.
  24. * 3) Move vma's page tables to the new range.
  25. * 4) Free up any cleared pgd range.
  26. * 5) Shrink the vma to cover only the new range.
  27. */
  28. struct mm_struct *mm = vma->vm_mm;
  29. unsigned long old_start = vma->vm_start;
  30. unsigned long old_end = vma->vm_end;
  31. unsigned long length = old_end - old_start;
  32. unsigned long new_start = old_start - shift;
  33. unsigned long new_end = old_end - shift;
  34. VMA_ITERATOR(vmi, mm, new_start);
  35. VMG_STATE(vmg, mm, &vmi, new_start, old_end, 0, vma->vm_pgoff);
  36. struct vm_area_struct *next;
  37. struct mmu_gather tlb;
  38. PAGETABLE_MOVE(pmc, vma, vma, old_start, new_start, length);
  39. BUG_ON(new_start > new_end);
  40. /*
  41. * ensure there are no vmas between where we want to go
  42. * and where we are
  43. */
  44. if (vma != vma_next(&vmi))
  45. return -EFAULT;
  46. vma_iter_prev_range(&vmi);
  47. /*
  48. * cover the whole range: [new_start, old_end)
  49. */
  50. vmg.target = vma;
  51. if (vma_expand(&vmg))
  52. return -ENOMEM;
  53. /*
  54. * move the page tables downwards, on failure we rely on
  55. * process cleanup to remove whatever mess we made.
  56. */
  57. pmc.for_stack = true;
  58. if (length != move_page_tables(&pmc))
  59. return -ENOMEM;
  60. tlb_gather_mmu(&tlb, mm);
  61. next = vma_next(&vmi);
  62. if (new_end > old_start) {
  63. /*
  64. * when the old and new regions overlap clear from new_end.
  65. */
  66. free_pgd_range(&tlb, new_end, old_end, new_end,
  67. next ? next->vm_start : USER_PGTABLES_CEILING);
  68. } else {
  69. /*
  70. * otherwise, clean from old_start; this is done to not touch
  71. * the address space in [new_end, old_start) some architectures
  72. * have constraints on va-space that make this illegal (IA64) -
  73. * for the others its just a little faster.
  74. */
  75. free_pgd_range(&tlb, old_start, old_end, new_end,
  76. next ? next->vm_start : USER_PGTABLES_CEILING);
  77. }
  78. tlb_finish_mmu(&tlb);
  79. vma_prev(&vmi);
  80. /* Shrink the vma to just the new range */
  81. return vma_shrink(&vmi, vma, new_start, new_end, vma->vm_pgoff);
  82. }
  83. /*
  84. * Establish the stack VMA in an execve'd process, located temporarily at the
  85. * maximum stack address provided by the architecture.
  86. *
  87. * We later relocate this downwards in relocate_vma_down().
  88. *
  89. * This function is almost certainly NOT what you want for anything other than
  90. * early executable initialisation.
  91. *
  92. * On success, returns 0 and sets *vmap to the stack VMA and *top_mem_p to the
  93. * maximum addressable location in the stack (that is capable of storing a
  94. * system word of data).
  95. */
  96. int create_init_stack_vma(struct mm_struct *mm, struct vm_area_struct **vmap,
  97. unsigned long *top_mem_p)
  98. {
  99. unsigned long flags = VM_STACK_FLAGS | VM_STACK_INCOMPLETE_SETUP;
  100. int err;
  101. struct vm_area_struct *vma = vm_area_alloc(mm);
  102. if (!vma)
  103. return -ENOMEM;
  104. vma_set_anonymous(vma);
  105. if (mmap_write_lock_killable(mm)) {
  106. err = -EINTR;
  107. goto err_free;
  108. }
  109. /*
  110. * Need to be called with mmap write lock
  111. * held, to avoid race with ksmd.
  112. */
  113. err = ksm_execve(mm);
  114. if (err)
  115. goto err_ksm;
  116. /*
  117. * Place the stack at the largest stack address the architecture
  118. * supports. Later, we'll move this to an appropriate place. We don't
  119. * use STACK_TOP because that can depend on attributes which aren't
  120. * configured yet.
  121. */
  122. BUILD_BUG_ON(VM_STACK_FLAGS & VM_STACK_INCOMPLETE_SETUP);
  123. vma->vm_end = STACK_TOP_MAX;
  124. vma->vm_start = vma->vm_end - PAGE_SIZE;
  125. if (pgtable_supports_soft_dirty())
  126. flags |= VM_SOFTDIRTY;
  127. vm_flags_init(vma, flags);
  128. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  129. err = insert_vm_struct(mm, vma);
  130. if (err)
  131. goto err;
  132. mm->stack_vm = mm->total_vm = 1;
  133. mmap_write_unlock(mm);
  134. *vmap = vma;
  135. *top_mem_p = vma->vm_end - sizeof(void *);
  136. return 0;
  137. err:
  138. ksm_exit(mm);
  139. err_ksm:
  140. mmap_write_unlock(mm);
  141. err_free:
  142. *vmap = NULL;
  143. vm_area_free(vma);
  144. return err;
  145. }