xref: /linux/mm/interval_tree.c (revision e2683c8868d03382da7e1ce8453b543a043066d1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * mm/interval_tree.c - interval tree for mapping->i_mmap
4  *
5  * Copyright (C) 2012, Michel Lespinasse <walken@google.com>
6  */
7 
8 #include <linux/mm.h>
9 #include <linux/fs.h>
10 #include <linux/rmap.h>
11 #include <linux/interval_tree_generic.h>
12 
13 static inline unsigned long vma_start_pgoff(struct vm_area_struct *v)
14 {
15 	return v->vm_pgoff;
16 }
17 
18 INTERVAL_TREE_DEFINE(struct vm_area_struct, shared.rb,
19 		     unsigned long, shared.rb_subtree_last,
20 		     vma_start_pgoff, vma_last_pgoff, /* empty */, vma_interval_tree)
21 
22 /* Insert node immediately after prev in the interval tree */
23 void vma_interval_tree_insert_after(struct vm_area_struct *node,
24 				    struct vm_area_struct *prev,
25 				    struct rb_root_cached *root)
26 {
27 	struct rb_node **link;
28 	struct vm_area_struct *parent;
29 	unsigned long last = vma_last_pgoff(node);
30 
31 	VM_BUG_ON_VMA(vma_start_pgoff(node) != vma_start_pgoff(prev), node);
32 
33 	if (!prev->shared.rb.rb_right) {
34 		parent = prev;
35 		link = &prev->shared.rb.rb_right;
36 	} else {
37 		parent = rb_entry(prev->shared.rb.rb_right,
38 				  struct vm_area_struct, shared.rb);
39 		if (parent->shared.rb_subtree_last < last)
40 			parent->shared.rb_subtree_last = last;
41 		while (parent->shared.rb.rb_left) {
42 			parent = rb_entry(parent->shared.rb.rb_left,
43 				struct vm_area_struct, shared.rb);
44 			if (parent->shared.rb_subtree_last < last)
45 				parent->shared.rb_subtree_last = last;
46 		}
47 		link = &parent->shared.rb.rb_left;
48 	}
49 
50 	node->shared.rb_subtree_last = last;
51 	rb_link_node(&node->shared.rb, &parent->shared.rb, link);
52 	rb_insert_augmented(&node->shared.rb, &root->rb_root,
53 			    &vma_interval_tree_augment);
54 }
55 
56 static inline unsigned long avc_start_pgoff(struct anon_vma_chain *avc)
57 {
58 	return vma_start_pgoff(avc->vma);
59 }
60 
61 static inline unsigned long avc_last_pgoff(struct anon_vma_chain *avc)
62 {
63 	return vma_last_pgoff(avc->vma);
64 }
65 
66 INTERVAL_TREE_DEFINE(struct anon_vma_chain, rb, unsigned long, rb_subtree_last,
67 		     avc_start_pgoff, avc_last_pgoff,
68 		     static inline, __anon_vma_interval_tree)
69 
70 void anon_vma_interval_tree_insert(struct anon_vma_chain *node,
71 				   struct rb_root_cached *root)
72 {
73 #ifdef CONFIG_DEBUG_VM_RB
74 	node->cached_vma_start = avc_start_pgoff(node);
75 	node->cached_vma_last = avc_last_pgoff(node);
76 #endif
77 	__anon_vma_interval_tree_insert(node, root);
78 }
79 
80 void anon_vma_interval_tree_remove(struct anon_vma_chain *node,
81 				   struct rb_root_cached *root)
82 {
83 	__anon_vma_interval_tree_remove(node, root);
84 }
85 
86 struct anon_vma_chain *
87 anon_vma_interval_tree_iter_first(struct rb_root_cached *root,
88 				  unsigned long first, unsigned long last)
89 {
90 	return __anon_vma_interval_tree_iter_first(root, first, last);
91 }
92 
93 struct anon_vma_chain *
94 anon_vma_interval_tree_iter_next(struct anon_vma_chain *node,
95 				 unsigned long first, unsigned long last)
96 {
97 	return __anon_vma_interval_tree_iter_next(node, first, last);
98 }
99 
100 #ifdef CONFIG_DEBUG_VM_RB
101 void anon_vma_interval_tree_verify(struct anon_vma_chain *node)
102 {
103 	WARN_ON_ONCE(node->cached_vma_start != avc_start_pgoff(node));
104 	WARN_ON_ONCE(node->cached_vma_last != avc_last_pgoff(node));
105 }
106 #endif
107