prio_tree.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * mm/prio_tree.c - priority search tree for mapping->i_mmap
  3. *
  4. * Copyright (C) 2004, Rajesh Venkatasubramanian <vrajesh@umich.edu>
  5. *
  6. * This file is released under the GPL v2.
  7. *
  8. * Based on the radix priority search tree proposed by Edward M. McCreight
  9. * SIAM Journal of Computing, vol. 14, no.2, pages 257-276, May 1985
  10. *
  11. * 02Feb2004 Initial version
  12. */
  13. #include <linux/mm.h>
  14. #include <linux/prio_tree.h>
  15. /*
  16. * See lib/prio_tree.c for details on the general radix priority search tree
  17. * code.
  18. */
  19. /*
  20. * The following #defines are mirrored from lib/prio_tree.c. They're only used
  21. * for debugging, and should be removed (along with the debugging code using
  22. * them) when switching also VMAs to the regular prio_tree code.
  23. */
  24. #define RADIX_INDEX(vma) ((vma)->vm_pgoff)
  25. #define VMA_SIZE(vma) (((vma)->vm_end - (vma)->vm_start) >> PAGE_SHIFT)
  26. /* avoid overflow */
  27. #define HEAP_INDEX(vma) ((vma)->vm_pgoff + (VMA_SIZE(vma) - 1))
  28. /*
  29. * Radix priority search tree for address_space->i_mmap
  30. *
  31. * For each vma that map a unique set of file pages i.e., unique [radix_index,
  32. * heap_index] value, we have a corresponing priority search tree node. If
  33. * multiple vmas have identical [radix_index, heap_index] value, then one of
  34. * them is used as a tree node and others are stored in a vm_set list. The tree
  35. * node points to the first vma (head) of the list using vm_set.head.
  36. *
  37. * prio_tree_root
  38. * |
  39. * A vm_set.head
  40. * / \ /
  41. * L R -> H-I-J-K-M-N-O-P-Q-S
  42. * ^ ^ <-- vm_set.list -->
  43. * tree nodes
  44. *
  45. * We need some way to identify whether a vma is a tree node, head of a vm_set
  46. * list, or just a member of a vm_set list. We cannot use vm_flags to store
  47. * such information. The reason is, in the above figure, it is possible that
  48. * vm_flags' of R and H are covered by the different mmap_sems. When R is
  49. * removed under R->mmap_sem, H replaces R as a tree node. Since we do not hold
  50. * H->mmap_sem, we cannot use H->vm_flags for marking that H is a tree node now.
  51. * That's why some trick involving shared.vm_set.parent is used for identifying
  52. * tree nodes and list head nodes.
  53. *
  54. * vma radix priority search tree node rules:
  55. *
  56. * vma->shared.vm_set.parent != NULL ==> a tree node
  57. * vma->shared.vm_set.head != NULL ==> list of others mapping same range
  58. * vma->shared.vm_set.head == NULL ==> no others map the same range
  59. *
  60. * vma->shared.vm_set.parent == NULL
  61. * vma->shared.vm_set.head != NULL ==> list head of vmas mapping same range
  62. * vma->shared.vm_set.head == NULL ==> a list node
  63. */
  64. /*
  65. * Add a new vma known to map the same set of pages as the old vma:
  66. * useful for fork's dup_mmap as well as vma_prio_tree_insert below.
  67. * Note that it just happens to work correctly on i_mmap_nonlinear too.
  68. */
  69. void vma_prio_tree_add(struct vm_area_struct *vma, struct vm_area_struct *old)
  70. {
  71. /* Leave these BUG_ONs till prio_tree patch stabilizes */
  72. BUG_ON(RADIX_INDEX(vma) != RADIX_INDEX(old));
  73. BUG_ON(HEAP_INDEX(vma) != HEAP_INDEX(old));
  74. vma->shared.vm_set.head = NULL;
  75. vma->shared.vm_set.parent = NULL;
  76. if (!old->shared.vm_set.parent)
  77. list_add(&vma->shared.vm_set.list,
  78. &old->shared.vm_set.list);
  79. else if (old->shared.vm_set.head)
  80. list_add_tail(&vma->shared.vm_set.list,
  81. &old->shared.vm_set.head->shared.vm_set.list);
  82. else {
  83. INIT_LIST_HEAD(&vma->shared.vm_set.list);
  84. vma->shared.vm_set.head = old;
  85. old->shared.vm_set.head = vma;
  86. }
  87. }
  88. void vma_prio_tree_insert(struct vm_area_struct *vma,
  89. struct prio_tree_root *root)
  90. {
  91. struct prio_tree_node *ptr;
  92. struct vm_area_struct *old;
  93. vma->shared.vm_set.head = NULL;
  94. ptr = raw_prio_tree_insert(root, &vma->shared.prio_tree_node);
  95. if (ptr != (struct prio_tree_node *) &vma->shared.prio_tree_node) {
  96. old = prio_tree_entry(ptr, struct vm_area_struct,
  97. shared.prio_tree_node);
  98. vma_prio_tree_add(vma, old);
  99. }
  100. }
  101. void vma_prio_tree_remove(struct vm_area_struct *vma,
  102. struct prio_tree_root *root)
  103. {
  104. struct vm_area_struct *node, *head, *new_head;
  105. if (!vma->shared.vm_set.head) {
  106. if (!vma->shared.vm_set.parent)
  107. list_del_init(&vma->shared.vm_set.list);
  108. else
  109. raw_prio_tree_remove(root, &vma->shared.prio_tree_node);
  110. } else {
  111. /* Leave this BUG_ON till prio_tree patch stabilizes */
  112. BUG_ON(vma->shared.vm_set.head->shared.vm_set.head != vma);
  113. if (vma->shared.vm_set.parent) {
  114. head = vma->shared.vm_set.head;
  115. if (!list_empty(&head->shared.vm_set.list)) {
  116. new_head = list_entry(
  117. head->shared.vm_set.list.next,
  118. struct vm_area_struct,
  119. shared.vm_set.list);
  120. list_del_init(&head->shared.vm_set.list);
  121. } else
  122. new_head = NULL;
  123. raw_prio_tree_replace(root, &vma->shared.prio_tree_node,
  124. &head->shared.prio_tree_node);
  125. head->shared.vm_set.head = new_head;
  126. if (new_head)
  127. new_head->shared.vm_set.head = head;
  128. } else {
  129. node = vma->shared.vm_set.head;
  130. if (!list_empty(&vma->shared.vm_set.list)) {
  131. new_head = list_entry(
  132. vma->shared.vm_set.list.next,
  133. struct vm_area_struct,
  134. shared.vm_set.list);
  135. list_del_init(&vma->shared.vm_set.list);
  136. node->shared.vm_set.head = new_head;
  137. new_head->shared.vm_set.head = node;
  138. } else
  139. node->shared.vm_set.head = NULL;
  140. }
  141. }
  142. }
  143. /*
  144. * Helper function to enumerate vmas that map a given file page or a set of
  145. * contiguous file pages. The function returns vmas that at least map a single
  146. * page in the given range of contiguous file pages.
  147. */
  148. struct vm_area_struct *vma_prio_tree_next(struct vm_area_struct *vma,
  149. struct prio_tree_iter *iter)
  150. {
  151. struct prio_tree_node *ptr;
  152. struct vm_area_struct *next;
  153. if (!vma) {
  154. /*
  155. * First call is with NULL vma
  156. */
  157. ptr = prio_tree_next(iter);
  158. if (ptr) {
  159. next = prio_tree_entry(ptr, struct vm_area_struct,
  160. shared.prio_tree_node);
  161. prefetch(next->shared.vm_set.head);
  162. return next;
  163. } else
  164. return NULL;
  165. }
  166. if (vma->shared.vm_set.parent) {
  167. if (vma->shared.vm_set.head) {
  168. next = vma->shared.vm_set.head;
  169. prefetch(next->shared.vm_set.list.next);
  170. return next;
  171. }
  172. } else {
  173. next = list_entry(vma->shared.vm_set.list.next,
  174. struct vm_area_struct, shared.vm_set.list);
  175. if (!next->shared.vm_set.head) {
  176. prefetch(next->shared.vm_set.list.next);
  177. return next;
  178. }
  179. }
  180. ptr = prio_tree_next(iter);
  181. if (ptr) {
  182. next = prio_tree_entry(ptr, struct vm_area_struct,
  183. shared.prio_tree_node);
  184. prefetch(next->shared.vm_set.head);
  185. return next;
  186. } else
  187. return NULL;
  188. }