vma.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * vma.h
  4. *
  5. * Core VMA manipulation API implemented in vma.c.
  6. */
  7. #ifndef __MM_VMA_H
  8. #define __MM_VMA_H
  9. /*
  10. * VMA lock generalization
  11. */
  12. struct vma_prepare {
  13. struct vm_area_struct *vma;
  14. struct vm_area_struct *adj_next;
  15. struct file *file;
  16. struct address_space *mapping;
  17. struct anon_vma *anon_vma;
  18. struct vm_area_struct *insert;
  19. struct vm_area_struct *remove;
  20. struct vm_area_struct *remove2;
  21. bool skip_vma_uprobe :1;
  22. };
  23. struct unlink_vma_file_batch {
  24. int count;
  25. struct vm_area_struct *vmas[8];
  26. };
  27. /*
  28. * vma munmap operation
  29. */
  30. struct vma_munmap_struct {
  31. struct vma_iterator *vmi;
  32. struct vm_area_struct *vma; /* The first vma to munmap */
  33. struct vm_area_struct *prev; /* vma before the munmap area */
  34. struct vm_area_struct *next; /* vma after the munmap area */
  35. struct list_head *uf; /* Userfaultfd list_head */
  36. unsigned long start; /* Aligned start addr (inclusive) */
  37. unsigned long end; /* Aligned end addr (exclusive) */
  38. unsigned long unmap_start; /* Unmap PTE start */
  39. unsigned long unmap_end; /* Unmap PTE end */
  40. int vma_count; /* Number of vmas that will be removed */
  41. bool unlock; /* Unlock after the munmap */
  42. bool clear_ptes; /* If there are outstanding PTE to be cleared */
  43. /* 2 byte hole */
  44. unsigned long nr_pages; /* Number of pages being removed */
  45. unsigned long locked_vm; /* Number of locked pages */
  46. unsigned long nr_accounted; /* Number of VM_ACCOUNT pages */
  47. unsigned long exec_vm;
  48. unsigned long stack_vm;
  49. unsigned long data_vm;
  50. };
  51. enum vma_merge_state {
  52. VMA_MERGE_START,
  53. VMA_MERGE_ERROR_NOMEM,
  54. VMA_MERGE_NOMERGE,
  55. VMA_MERGE_SUCCESS,
  56. };
  57. /*
  58. * Describes a VMA merge operation and is threaded throughout it.
  59. *
  60. * Any of the fields may be mutated by the merge operation, so no guarantees are
  61. * made to the contents of this structure after a merge operation has completed.
  62. */
  63. struct vma_merge_struct {
  64. struct mm_struct *mm;
  65. struct vma_iterator *vmi;
  66. /*
  67. * Adjacent VMAs, any of which may be NULL if not present:
  68. *
  69. * |------|--------|------|
  70. * | prev | middle | next |
  71. * |------|--------|------|
  72. *
  73. * middle may not yet exist in the case of a proposed new VMA being
  74. * merged, or it may be an existing VMA.
  75. *
  76. * next may be assigned by the caller.
  77. */
  78. struct vm_area_struct *prev;
  79. struct vm_area_struct *middle;
  80. struct vm_area_struct *next;
  81. /* This is the VMA we ultimately target to become the merged VMA. */
  82. struct vm_area_struct *target;
  83. /*
  84. * Initially, the start, end, pgoff fields are provided by the caller
  85. * and describe the proposed new VMA range, whether modifying an
  86. * existing VMA (which will be 'middle'), or adding a new one.
  87. *
  88. * During the merge process these fields are updated to describe the new
  89. * range _including those VMAs which will be merged_.
  90. */
  91. unsigned long start;
  92. unsigned long end;
  93. pgoff_t pgoff;
  94. vm_flags_t vm_flags;
  95. struct file *file;
  96. struct anon_vma *anon_vma;
  97. struct mempolicy *policy;
  98. struct vm_userfaultfd_ctx uffd_ctx;
  99. struct anon_vma_name *anon_name;
  100. enum vma_merge_state state;
  101. /* If copied from (i.e. mremap()'d) the VMA from which we are copying. */
  102. struct vm_area_struct *copied_from;
  103. /* Flags which callers can use to modify merge behaviour: */
  104. /*
  105. * If we can expand, simply do so. We know there is nothing to merge to
  106. * the right. Does not reset state upon failure to merge. The VMA
  107. * iterator is assumed to be positioned at the previous VMA, rather than
  108. * at the gap.
  109. */
  110. bool just_expand :1;
  111. /*
  112. * If a merge is possible, but an OOM error occurs, give up and don't
  113. * execute the merge, returning NULL.
  114. */
  115. bool give_up_on_oom :1;
  116. /*
  117. * If set, skip uprobe_mmap upon merged vma.
  118. */
  119. bool skip_vma_uprobe :1;
  120. /* Internal flags set during merge process: */
  121. /*
  122. * Internal flag indicating the merge increases vmg->middle->vm_start
  123. * (and thereby, vmg->prev->vm_end).
  124. */
  125. bool __adjust_middle_start :1;
  126. /*
  127. * Internal flag indicating the merge decreases vmg->next->vm_start
  128. * (and thereby, vmg->middle->vm_end).
  129. */
  130. bool __adjust_next_start :1;
  131. /*
  132. * Internal flag used during the merge operation to indicate we will
  133. * remove vmg->middle.
  134. */
  135. bool __remove_middle :1;
  136. /*
  137. * Internal flag used during the merge operation to indicate we will
  138. * remove vmg->next.
  139. */
  140. bool __remove_next :1;
  141. };
  142. struct unmap_desc {
  143. struct ma_state *mas; /* the maple state point to the first vma */
  144. struct vm_area_struct *first; /* The first vma */
  145. unsigned long pg_start; /* The first pagetable address to free (floor) */
  146. unsigned long pg_end; /* The last pagetable address to free (ceiling) */
  147. unsigned long vma_start; /* The min vma address */
  148. unsigned long vma_end; /* The max vma address */
  149. unsigned long tree_end; /* Maximum for the vma tree search */
  150. unsigned long tree_reset; /* Where to reset the vma tree walk */
  151. bool mm_wr_locked; /* If the mmap write lock is held */
  152. };
  153. /*
  154. * unmap_all_init() - Initialize unmap_desc to remove all vmas, point the
  155. * pg_start and pg_end to a safe location.
  156. */
  157. static inline void unmap_all_init(struct unmap_desc *unmap,
  158. struct vma_iterator *vmi, struct vm_area_struct *vma)
  159. {
  160. unmap->mas = &vmi->mas;
  161. unmap->first = vma;
  162. unmap->pg_start = FIRST_USER_ADDRESS;
  163. unmap->pg_end = USER_PGTABLES_CEILING;
  164. unmap->vma_start = 0;
  165. unmap->vma_end = ULONG_MAX;
  166. unmap->tree_end = ULONG_MAX;
  167. unmap->tree_reset = vma->vm_end;
  168. unmap->mm_wr_locked = false;
  169. }
  170. /*
  171. * unmap_pgtable_init() - Initialize unmap_desc to remove all page tables within
  172. * the user range.
  173. *
  174. * ARM can have mappings outside of vmas.
  175. * See: e2cdef8c847b4 ("[PATCH] freepgt: free_pgtables from FIRST_USER_ADDRESS")
  176. *
  177. * ARM LPAE uses page table mappings beyond the USER_PGTABLES_CEILING
  178. * See: CONFIG_ARM_LPAE in arch/arm/include/asm/pgtable.h
  179. */
  180. static inline void unmap_pgtable_init(struct unmap_desc *unmap,
  181. struct vma_iterator *vmi)
  182. {
  183. vma_iter_set(vmi, unmap->tree_reset);
  184. unmap->vma_start = FIRST_USER_ADDRESS;
  185. unmap->vma_end = USER_PGTABLES_CEILING;
  186. unmap->tree_end = USER_PGTABLES_CEILING;
  187. }
  188. #define UNMAP_STATE(name, _vmi, _vma, _vma_start, _vma_end, _prev, _next) \
  189. struct unmap_desc name = { \
  190. .mas = &(_vmi)->mas, \
  191. .first = _vma, \
  192. .pg_start = _prev ? ((struct vm_area_struct *)_prev)->vm_end : \
  193. FIRST_USER_ADDRESS, \
  194. .pg_end = _next ? ((struct vm_area_struct *)_next)->vm_start : \
  195. USER_PGTABLES_CEILING, \
  196. .vma_start = _vma_start, \
  197. .vma_end = _vma_end, \
  198. .tree_end = _next ? \
  199. ((struct vm_area_struct *)_next)->vm_start : \
  200. USER_PGTABLES_CEILING, \
  201. .tree_reset = _vma->vm_end, \
  202. .mm_wr_locked = true, \
  203. }
  204. static inline bool vmg_nomem(struct vma_merge_struct *vmg)
  205. {
  206. return vmg->state == VMA_MERGE_ERROR_NOMEM;
  207. }
  208. /* Assumes addr >= vma->vm_start. */
  209. static inline pgoff_t vma_pgoff_offset(struct vm_area_struct *vma,
  210. unsigned long addr)
  211. {
  212. return vma->vm_pgoff + PHYS_PFN(addr - vma->vm_start);
  213. }
  214. #define VMG_STATE(name, mm_, vmi_, start_, end_, vm_flags_, pgoff_) \
  215. struct vma_merge_struct name = { \
  216. .mm = mm_, \
  217. .vmi = vmi_, \
  218. .start = start_, \
  219. .end = end_, \
  220. .vm_flags = vm_flags_, \
  221. .pgoff = pgoff_, \
  222. .state = VMA_MERGE_START, \
  223. }
  224. #define VMG_VMA_STATE(name, vmi_, prev_, vma_, start_, end_) \
  225. struct vma_merge_struct name = { \
  226. .mm = vma_->vm_mm, \
  227. .vmi = vmi_, \
  228. .prev = prev_, \
  229. .middle = vma_, \
  230. .next = NULL, \
  231. .start = start_, \
  232. .end = end_, \
  233. .vm_flags = vma_->vm_flags, \
  234. .pgoff = vma_pgoff_offset(vma_, start_), \
  235. .file = vma_->vm_file, \
  236. .anon_vma = vma_->anon_vma, \
  237. .policy = vma_policy(vma_), \
  238. .uffd_ctx = vma_->vm_userfaultfd_ctx, \
  239. .anon_name = anon_vma_name(vma_), \
  240. .state = VMA_MERGE_START, \
  241. }
  242. #ifdef CONFIG_DEBUG_VM_MAPLE_TREE
  243. void validate_mm(struct mm_struct *mm);
  244. #else
  245. #define validate_mm(mm) do { } while (0)
  246. #endif
  247. __must_check int vma_expand(struct vma_merge_struct *vmg);
  248. __must_check int vma_shrink(struct vma_iterator *vmi,
  249. struct vm_area_struct *vma,
  250. unsigned long start, unsigned long end, pgoff_t pgoff);
  251. static inline int vma_iter_store_gfp(struct vma_iterator *vmi,
  252. struct vm_area_struct *vma, gfp_t gfp)
  253. {
  254. if (vmi->mas.status != ma_start &&
  255. ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start)))
  256. vma_iter_invalidate(vmi);
  257. __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1);
  258. mas_store_gfp(&vmi->mas, vma, gfp);
  259. if (unlikely(mas_is_err(&vmi->mas)))
  260. return -ENOMEM;
  261. vma_mark_attached(vma);
  262. return 0;
  263. }
  264. /*
  265. * Temporary helper function for stacked mmap handlers which specify
  266. * f_op->mmap() but which might have an underlying file system which implements
  267. * f_op->mmap_prepare().
  268. */
  269. static inline void set_vma_from_desc(struct vm_area_struct *vma,
  270. struct vm_area_desc *desc)
  271. {
  272. /*
  273. * Since we're invoking .mmap_prepare() despite having a partially
  274. * established VMA, we must take care to handle setting fields
  275. * correctly.
  276. */
  277. /* Mutable fields. Populated with initial state. */
  278. vma->vm_pgoff = desc->pgoff;
  279. if (desc->vm_file != vma->vm_file)
  280. vma_set_file(vma, desc->vm_file);
  281. vma->flags = desc->vma_flags;
  282. vma->vm_page_prot = desc->page_prot;
  283. /* User-defined fields. */
  284. vma->vm_ops = desc->vm_ops;
  285. vma->vm_private_data = desc->private_data;
  286. }
  287. int
  288. do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma,
  289. struct mm_struct *mm, unsigned long start,
  290. unsigned long end, struct list_head *uf, bool unlock);
  291. int do_vmi_munmap(struct vma_iterator *vmi, struct mm_struct *mm,
  292. unsigned long start, size_t len, struct list_head *uf,
  293. bool unlock);
  294. void remove_vma(struct vm_area_struct *vma);
  295. void unmap_region(struct unmap_desc *unmap);
  296. /**
  297. * vma_modify_flags() - Perform any necessary split/merge in preparation for
  298. * setting VMA flags to *@vm_flags in the range @start to @end contained within
  299. * @vma.
  300. * @vmi: Valid VMA iterator positioned at @vma.
  301. * @prev: The VMA immediately prior to @vma or NULL if @vma is the first.
  302. * @vma: The VMA containing the range @start to @end to be updated.
  303. * @start: The start of the range to update. May be offset within @vma.
  304. * @end: The exclusive end of the range to update, may be offset within @vma.
  305. * @vm_flags_ptr: A pointer to the VMA flags that the @start to @end range is
  306. * about to be set to. On merge, this will be updated to include sticky flags.
  307. *
  308. * IMPORTANT: The actual modification being requested here is NOT applied,
  309. * rather the VMA is perhaps split, perhaps merged to accommodate the change,
  310. * and the caller is expected to perform the actual modification.
  311. *
  312. * In order to account for sticky VMA flags, the @vm_flags_ptr parameter points
  313. * to the requested flags which are then updated so the caller, should they
  314. * overwrite any existing flags, correctly retains these.
  315. *
  316. * Returns: A VMA which contains the range @start to @end ready to have its
  317. * flags altered to *@vm_flags.
  318. */
  319. __must_check struct vm_area_struct *vma_modify_flags(struct vma_iterator *vmi,
  320. struct vm_area_struct *prev, struct vm_area_struct *vma,
  321. unsigned long start, unsigned long end,
  322. vm_flags_t *vm_flags_ptr);
  323. /**
  324. * vma_modify_name() - Perform any necessary split/merge in preparation for
  325. * setting anonymous VMA name to @new_name in the range @start to @end contained
  326. * within @vma.
  327. * @vmi: Valid VMA iterator positioned at @vma.
  328. * @prev: The VMA immediately prior to @vma or NULL if @vma is the first.
  329. * @vma: The VMA containing the range @start to @end to be updated.
  330. * @start: The start of the range to update. May be offset within @vma.
  331. * @end: The exclusive end of the range to update, may be offset within @vma.
  332. * @new_name: The anonymous VMA name that the @start to @end range is about to
  333. * be set to.
  334. *
  335. * IMPORTANT: The actual modification being requested here is NOT applied,
  336. * rather the VMA is perhaps split, perhaps merged to accommodate the change,
  337. * and the caller is expected to perform the actual modification.
  338. *
  339. * Returns: A VMA which contains the range @start to @end ready to have its
  340. * anonymous VMA name changed to @new_name.
  341. */
  342. __must_check struct vm_area_struct *vma_modify_name(struct vma_iterator *vmi,
  343. struct vm_area_struct *prev, struct vm_area_struct *vma,
  344. unsigned long start, unsigned long end,
  345. struct anon_vma_name *new_name);
  346. /**
  347. * vma_modify_policy() - Perform any necessary split/merge in preparation for
  348. * setting NUMA policy to @new_pol in the range @start to @end contained
  349. * within @vma.
  350. * @vmi: Valid VMA iterator positioned at @vma.
  351. * @prev: The VMA immediately prior to @vma or NULL if @vma is the first.
  352. * @vma: The VMA containing the range @start to @end to be updated.
  353. * @start: The start of the range to update. May be offset within @vma.
  354. * @end: The exclusive end of the range to update, may be offset within @vma.
  355. * @new_pol: The NUMA policy that the @start to @end range is about to be set
  356. * to.
  357. *
  358. * IMPORTANT: The actual modification being requested here is NOT applied,
  359. * rather the VMA is perhaps split, perhaps merged to accommodate the change,
  360. * and the caller is expected to perform the actual modification.
  361. *
  362. * Returns: A VMA which contains the range @start to @end ready to have its
  363. * NUMA policy changed to @new_pol.
  364. */
  365. __must_check struct vm_area_struct *vma_modify_policy(struct vma_iterator *vmi,
  366. struct vm_area_struct *prev, struct vm_area_struct *vma,
  367. unsigned long start, unsigned long end,
  368. struct mempolicy *new_pol);
  369. /**
  370. * vma_modify_flags_uffd() - Perform any necessary split/merge in preparation for
  371. * setting VMA flags to @vm_flags and UFFD context to @new_ctx in the range
  372. * @start to @end contained within @vma.
  373. * @vmi: Valid VMA iterator positioned at @vma.
  374. * @prev: The VMA immediately prior to @vma or NULL if @vma is the first.
  375. * @vma: The VMA containing the range @start to @end to be updated.
  376. * @start: The start of the range to update. May be offset within @vma.
  377. * @end: The exclusive end of the range to update, may be offset within @vma.
  378. * @vm_flags: The VMA flags that the @start to @end range is about to be set to.
  379. * @new_ctx: The userfaultfd context that the @start to @end range is about to
  380. * be set to.
  381. * @give_up_on_oom: If an out of memory condition occurs on merge, simply give
  382. * up on it and treat the merge as best-effort.
  383. *
  384. * IMPORTANT: The actual modification being requested here is NOT applied,
  385. * rather the VMA is perhaps split, perhaps merged to accommodate the change,
  386. * and the caller is expected to perform the actual modification.
  387. *
  388. * Returns: A VMA which contains the range @start to @end ready to have its VMA
  389. * flags changed to @vm_flags and its userfaultfd context changed to @new_ctx.
  390. */
  391. __must_check struct vm_area_struct *vma_modify_flags_uffd(struct vma_iterator *vmi,
  392. struct vm_area_struct *prev, struct vm_area_struct *vma,
  393. unsigned long start, unsigned long end, vm_flags_t vm_flags,
  394. struct vm_userfaultfd_ctx new_ctx, bool give_up_on_oom);
  395. __must_check struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg);
  396. __must_check struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
  397. struct vm_area_struct *vma, unsigned long delta);
  398. void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb);
  399. void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb);
  400. void unlink_file_vma_batch_add(struct unlink_vma_file_batch *vb,
  401. struct vm_area_struct *vma);
  402. struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
  403. unsigned long addr, unsigned long len, pgoff_t pgoff,
  404. bool *need_rmap_locks);
  405. struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma);
  406. bool vma_needs_dirty_tracking(struct vm_area_struct *vma);
  407. bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot);
  408. int mm_take_all_locks(struct mm_struct *mm);
  409. void mm_drop_all_locks(struct mm_struct *mm);
  410. unsigned long mmap_region(struct file *file, unsigned long addr,
  411. unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
  412. struct list_head *uf);
  413. int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma,
  414. unsigned long addr, unsigned long request, unsigned long flags);
  415. unsigned long unmapped_area(struct vm_unmapped_area_info *info);
  416. unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info);
  417. static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma)
  418. {
  419. /*
  420. * We want to check manually if we can change individual PTEs writable
  421. * if we can't do that automatically for all PTEs in a mapping. For
  422. * private mappings, that's always the case when we have write
  423. * permissions as we properly have to handle COW.
  424. */
  425. if (vma->vm_flags & VM_SHARED)
  426. return vma_wants_writenotify(vma, vma->vm_page_prot);
  427. return !!(vma->vm_flags & VM_WRITE);
  428. }
  429. #ifdef CONFIG_MMU
  430. static inline pgprot_t vm_pgprot_modify(pgprot_t oldprot, vm_flags_t vm_flags)
  431. {
  432. return pgprot_modify(oldprot, vm_get_page_prot(vm_flags));
  433. }
  434. #endif
  435. static inline struct vm_area_struct *vma_prev_limit(struct vma_iterator *vmi,
  436. unsigned long min)
  437. {
  438. return mas_prev(&vmi->mas, min);
  439. }
  440. /*
  441. * These three helpers classifies VMAs for virtual memory accounting.
  442. */
  443. /*
  444. * Executable code area - executable, not writable, not stack
  445. */
  446. static inline bool is_exec_mapping(vm_flags_t flags)
  447. {
  448. return (flags & (VM_EXEC | VM_WRITE | VM_STACK)) == VM_EXEC;
  449. }
  450. /*
  451. * Stack area (including shadow stacks)
  452. *
  453. * VM_GROWSUP / VM_GROWSDOWN VMAs are always private anonymous:
  454. * do_mmap() forbids all other combinations.
  455. */
  456. static inline bool is_stack_mapping(vm_flags_t flags)
  457. {
  458. return ((flags & VM_STACK) == VM_STACK) || (flags & VM_SHADOW_STACK);
  459. }
  460. /*
  461. * Data area - private, writable, not stack
  462. */
  463. static inline bool is_data_mapping(vm_flags_t flags)
  464. {
  465. return (flags & (VM_WRITE | VM_SHARED | VM_STACK)) == VM_WRITE;
  466. }
  467. static inline void vma_iter_config(struct vma_iterator *vmi,
  468. unsigned long index, unsigned long last)
  469. {
  470. __mas_set_range(&vmi->mas, index, last - 1);
  471. }
  472. static inline void vma_iter_reset(struct vma_iterator *vmi)
  473. {
  474. mas_reset(&vmi->mas);
  475. }
  476. static inline
  477. struct vm_area_struct *vma_iter_prev_range_limit(struct vma_iterator *vmi, unsigned long min)
  478. {
  479. return mas_prev_range(&vmi->mas, min);
  480. }
  481. static inline
  482. struct vm_area_struct *vma_iter_next_range_limit(struct vma_iterator *vmi, unsigned long max)
  483. {
  484. return mas_next_range(&vmi->mas, max);
  485. }
  486. static inline int vma_iter_area_lowest(struct vma_iterator *vmi, unsigned long min,
  487. unsigned long max, unsigned long size)
  488. {
  489. return mas_empty_area(&vmi->mas, min, max - 1, size);
  490. }
  491. static inline int vma_iter_area_highest(struct vma_iterator *vmi, unsigned long min,
  492. unsigned long max, unsigned long size)
  493. {
  494. return mas_empty_area_rev(&vmi->mas, min, max - 1, size);
  495. }
  496. /*
  497. * VMA Iterator functions shared between nommu and mmap
  498. */
  499. static inline int vma_iter_prealloc(struct vma_iterator *vmi,
  500. struct vm_area_struct *vma)
  501. {
  502. return mas_preallocate(&vmi->mas, vma, GFP_KERNEL);
  503. }
  504. static inline void vma_iter_clear(struct vma_iterator *vmi)
  505. {
  506. mas_store_prealloc(&vmi->mas, NULL);
  507. }
  508. static inline struct vm_area_struct *vma_iter_load(struct vma_iterator *vmi)
  509. {
  510. return mas_walk(&vmi->mas);
  511. }
  512. /* Store a VMA with preallocated memory */
  513. static inline void vma_iter_store_overwrite(struct vma_iterator *vmi,
  514. struct vm_area_struct *vma)
  515. {
  516. vma_assert_attached(vma);
  517. #if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
  518. if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&
  519. vmi->mas.index > vma->vm_start)) {
  520. pr_warn("%lx > %lx\n store vma %lx-%lx\n into slot %lx-%lx\n",
  521. vmi->mas.index, vma->vm_start, vma->vm_start,
  522. vma->vm_end, vmi->mas.index, vmi->mas.last);
  523. }
  524. if (MAS_WARN_ON(&vmi->mas, vmi->mas.status != ma_start &&
  525. vmi->mas.last < vma->vm_start)) {
  526. pr_warn("%lx < %lx\nstore vma %lx-%lx\ninto slot %lx-%lx\n",
  527. vmi->mas.last, vma->vm_start, vma->vm_start, vma->vm_end,
  528. vmi->mas.index, vmi->mas.last);
  529. }
  530. #endif
  531. if (vmi->mas.status != ma_start &&
  532. ((vmi->mas.index > vma->vm_start) || (vmi->mas.last < vma->vm_start)))
  533. vma_iter_invalidate(vmi);
  534. __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1);
  535. mas_store_prealloc(&vmi->mas, vma);
  536. }
  537. static inline void vma_iter_store_new(struct vma_iterator *vmi,
  538. struct vm_area_struct *vma)
  539. {
  540. vma_mark_attached(vma);
  541. vma_iter_store_overwrite(vmi, vma);
  542. }
  543. static inline unsigned long vma_iter_addr(struct vma_iterator *vmi)
  544. {
  545. return vmi->mas.index;
  546. }
  547. static inline unsigned long vma_iter_end(struct vma_iterator *vmi)
  548. {
  549. return vmi->mas.last + 1;
  550. }
  551. static inline
  552. struct vm_area_struct *vma_iter_prev_range(struct vma_iterator *vmi)
  553. {
  554. return mas_prev_range(&vmi->mas, 0);
  555. }
  556. /*
  557. * Retrieve the next VMA and rewind the iterator to end of the previous VMA, or
  558. * if no previous VMA, to index 0.
  559. */
  560. static inline
  561. struct vm_area_struct *vma_iter_next_rewind(struct vma_iterator *vmi,
  562. struct vm_area_struct **pprev)
  563. {
  564. struct vm_area_struct *next = vma_next(vmi);
  565. struct vm_area_struct *prev = vma_prev(vmi);
  566. /*
  567. * Consider the case where no previous VMA exists. We advance to the
  568. * next VMA, skipping any gap, then rewind to the start of the range.
  569. *
  570. * If we were to unconditionally advance to the next range we'd wind up
  571. * at the next VMA again, so we check to ensure there is a previous VMA
  572. * to skip over.
  573. */
  574. if (prev)
  575. vma_iter_next_range(vmi);
  576. if (pprev)
  577. *pprev = prev;
  578. return next;
  579. }
  580. #ifdef CONFIG_64BIT
  581. static inline bool vma_is_sealed(struct vm_area_struct *vma)
  582. {
  583. return (vma->vm_flags & VM_SEALED);
  584. }
  585. #else
  586. static inline bool vma_is_sealed(struct vm_area_struct *vma)
  587. {
  588. return false;
  589. }
  590. #endif
  591. #if defined(CONFIG_STACK_GROWSUP)
  592. int expand_upwards(struct vm_area_struct *vma, unsigned long address);
  593. #endif
  594. int expand_downwards(struct vm_area_struct *vma, unsigned long address);
  595. int __vm_munmap(unsigned long start, size_t len, bool unlock);
  596. int insert_vm_struct(struct mm_struct *mm, struct vm_area_struct *vma);
  597. /* vma_init.h, shared between CONFIG_MMU and nommu. */
  598. void __init vma_state_init(void);
  599. struct vm_area_struct *vm_area_alloc(struct mm_struct *mm);
  600. struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig);
  601. void vm_area_free(struct vm_area_struct *vma);
  602. /* vma_exec.c */
  603. #ifdef CONFIG_MMU
  604. int create_init_stack_vma(struct mm_struct *mm, struct vm_area_struct **vmap,
  605. unsigned long *top_mem_p);
  606. int relocate_vma_down(struct vm_area_struct *vma, unsigned long shift);
  607. #endif
  608. #endif /* __MM_VMA_H */