1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mm/interval_tree.c - interval tree for address_space->i_mmap and 4 * anon_vma->rb_root 5 * 6 * Copyright (C) 2012, Michel Lespinasse <walken@google.com> 7 */ 8 9 #include <linux/mm.h> 10 #include <linux/fs.h> 11 #include <linux/rmap.h> 12 #include <linux/interval_tree_generic.h> 13 14 /* File-backed interval tree (address_space->i_mmap) */ 15 16 INTERVAL_TREE_DEFINE(struct vm_area_struct, shared.rb, 17 pgoff_t, shared.rb_subtree_last, 18 vma_start_pgoff, vma_last_pgoff, static, 19 __mapping_rmap_tree) 20 21 void mapping_rmap_tree_insert(struct vm_area_struct *vma, 22 struct address_space *mapping) 23 { 24 __mapping_rmap_tree_insert(vma, &mapping->i_mmap); 25 } 26 27 /* Insert vma immediately after prev in the interval tree */ 28 void mapping_rmap_tree_insert_after(struct vm_area_struct *vma, 29 struct vm_area_struct *prev, 30 struct address_space *mapping) 31 { 32 struct rb_node **link; 33 struct vm_area_struct *parent; 34 const pgoff_t pgoff_last = vma_last_pgoff(vma); 35 36 VM_WARN_ON_ONCE_VMA(vma_start_pgoff(vma) != vma_start_pgoff(prev), vma); 37 38 if (!prev->shared.rb.rb_right) { 39 parent = prev; 40 link = &prev->shared.rb.rb_right; 41 } else { 42 parent = rb_entry(prev->shared.rb.rb_right, 43 struct vm_area_struct, shared.rb); 44 if (parent->shared.rb_subtree_last < pgoff_last) 45 parent->shared.rb_subtree_last = pgoff_last; 46 while (parent->shared.rb.rb_left) { 47 parent = rb_entry(parent->shared.rb.rb_left, 48 struct vm_area_struct, shared.rb); 49 if (parent->shared.rb_subtree_last < pgoff_last) 50 parent->shared.rb_subtree_last = pgoff_last; 51 } 52 link = &parent->shared.rb.rb_left; 53 } 54 55 vma->shared.rb_subtree_last = pgoff_last; 56 rb_link_node(&vma->shared.rb, &parent->shared.rb, link); 57 rb_insert_augmented(&vma->shared.rb, &mapping->i_mmap.rb_root, 58 &__mapping_rmap_tree_augment); 59 } 60 61 void mapping_rmap_tree_remove(struct vm_area_struct *vma, 62 struct address_space *mapping) 63 { 64 __mapping_rmap_tree_remove(vma, &mapping->i_mmap); 65 } 66 67 static void mapping_rmap_tree_update_inplace(struct vm_area_struct *vma) 68 { 69 /* Propagate all the way up the tree. */ 70 __mapping_rmap_tree_augment.propagate(&vma->shared.rb, NULL); 71 } 72 73 /** 74 * mapping_rmap_tree_pre_update() - Prepare the file rmap tree for a change to 75 * be made to @vma. 76 * @vma: The VMA about to be updated. 77 * @mapping: The file rmap to which @vma belongs. 78 * @pgoff_unchanged: Whether @vma's page offset will remain unchanged. 79 * 80 * The file rmap lock must be held across the entire update. 81 */ 82 void mapping_rmap_tree_pre_update(struct vm_area_struct *vma, 83 struct address_space *mapping, 84 bool pgoff_unchanged) 85 { 86 /* If the pgoff has changed, then remove and reinsert afterwards. */ 87 if (!pgoff_unchanged) 88 mapping_rmap_tree_remove(vma, mapping); 89 } 90 91 /** 92 * mapping_rmap_tree_post_update() - Update the file rmap tree to reflect a 93 * change that has been made to @vma. 94 * @vma: The VMA that has been updated. 95 * @mapping: The file rmap to which @vma belongs. 96 * @pgoff_unchanged: Whether @vma's page offset remained unchanged. 97 * 98 * mapping_rmap_tree_pre_update() must have been called prior to this. 99 * 100 * The file rmap lock must be held across the entire update. 101 */ 102 void mapping_rmap_tree_post_update(struct vm_area_struct *vma, 103 struct address_space *mapping, 104 bool pgoff_unchanged) 105 { 106 if (pgoff_unchanged) 107 mapping_rmap_tree_update_inplace(vma); 108 else 109 mapping_rmap_tree_insert(vma, mapping); 110 } 111 112 struct vm_area_struct * 113 mapping_rmap_tree_iter_first(struct address_space *mapping, 114 pgoff_t pgoff_start, pgoff_t pgoff_last) 115 { 116 return __mapping_rmap_tree_iter_first(&mapping->i_mmap, 117 pgoff_start, pgoff_last); 118 } 119 120 struct vm_area_struct * 121 mapping_rmap_tree_iter_next(struct vm_area_struct *vma, 122 pgoff_t pgoff_start, pgoff_t pgoff_last) 123 { 124 return __mapping_rmap_tree_iter_next(vma, pgoff_start, pgoff_last); 125 } 126 127 /* Anonymous interval tree (anon_vma->rb_root) */ 128 129 static pgoff_t avc_start_pgoff(struct anon_vma_chain *avc) 130 { 131 return vma_start_anon_pgoff(avc->vma); 132 } 133 134 static pgoff_t avc_last_pgoff(struct anon_vma_chain *avc) 135 { 136 return vma_last_anon_pgoff(avc->vma); 137 } 138 139 INTERVAL_TREE_DEFINE(struct anon_vma_chain, rb, pgoff_t, rb_subtree_last, 140 avc_start_pgoff, avc_last_pgoff, 141 static, __anon_rmap_tree) 142 143 void anon_rmap_tree_insert(struct anon_vma_chain *avc, 144 struct anon_vma *anon_vma) 145 { 146 #ifdef CONFIG_DEBUG_VM_RB 147 avc->cached_vma_start = avc_start_pgoff(avc); 148 avc->cached_vma_last = avc_last_pgoff(avc); 149 #endif 150 __anon_rmap_tree_insert(avc, &anon_vma->rb_root); 151 } 152 153 void anon_rmap_tree_remove(struct anon_vma_chain *avc, 154 struct anon_vma *anon_vma) 155 { 156 __anon_rmap_tree_remove(avc, &anon_vma->rb_root); 157 } 158 159 static void anon_rmap_tree_update_inplace(struct anon_vma_chain *avc) 160 { 161 #ifdef CONFIG_DEBUG_VM_RB 162 avc->cached_vma_last = avc_last_pgoff(avc); 163 #endif 164 /* Propagate all the way up the tree. */ 165 __anon_rmap_tree_augment.propagate(&avc->rb, NULL); 166 } 167 168 /** 169 * anon_rmap_tree_pre_update_vma() - Prepare the anon rmap trees for a change 170 * to be made to @vma. 171 * @vma: The VMA about to be updated, which has an anon rmap assigned and is 172 * already inserted on its interval trees. 173 * @anon_pgoff_unchanged: Whether @vma's anonymous page offset will remain 174 * unchanged. 175 * 176 * The anon rmap lock must be held across the entire update. 177 */ 178 void anon_rmap_tree_pre_update_vma(struct vm_area_struct *vma, 179 bool anon_pgoff_unchanged) 180 { 181 struct anon_vma_chain *avc; 182 183 if (anon_pgoff_unchanged) 184 return; 185 186 /* If the pgoff has changed, then remove and reinsert afterwards. */ 187 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) 188 anon_rmap_tree_remove(avc, avc->anon_vma); 189 } 190 191 /** 192 * anon_rmap_tree_post_update_vma() - Update the anon rmap trees to reflect a 193 * change that has been made to @vma. 194 * @vma: The VMA that has been updated. 195 * @anon_pgoff_unchanged: Whether @vma's anonymous page offset remained 196 * unchanged. 197 * 198 * anon_rmap_tree_pre_update_vma() must have been called prior to this. 199 * 200 * The anon rmap lock must be held across the entire update. 201 */ 202 void anon_rmap_tree_post_update_vma(struct vm_area_struct *vma, 203 bool anon_pgoff_unchanged) 204 { 205 struct anon_vma_chain *avc; 206 207 list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) { 208 if (anon_pgoff_unchanged) 209 anon_rmap_tree_update_inplace(avc); 210 else 211 anon_rmap_tree_insert(avc, avc->anon_vma); 212 } 213 } 214 215 struct anon_vma_chain * 216 anon_rmap_tree_iter_first(struct anon_vma *anon_vma, 217 pgoff_t pgoff_start, pgoff_t pgoff_last) 218 { 219 return __anon_rmap_tree_iter_first(&anon_vma->rb_root, 220 pgoff_start, pgoff_last); 221 } 222 223 struct anon_vma_chain * 224 anon_rmap_tree_iter_next(struct anon_vma_chain *avc, 225 pgoff_t pgoff_start, pgoff_t pgoff_last) 226 { 227 return __anon_rmap_tree_iter_next(avc, pgoff_start, pgoff_last); 228 } 229 230 #ifdef CONFIG_DEBUG_VM_RB 231 void anon_rmap_tree_verify(struct anon_vma_chain *avc) 232 { 233 WARN_ON_ONCE(avc->cached_vma_start != avc_start_pgoff(avc)); 234 WARN_ON_ONCE(avc->cached_vma_last != avc_last_pgoff(avc)); 235 } 236 #endif 237