blob: b476643587966102e90813663b2039461d1dc679 [file] [log] [blame]
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07001/*
2 * mm/interval_tree.c - interval tree for mapping->i_mmap
3 *
4 * Copyright (C) 2012, Michel Lespinasse <walken@google.com>
5 *
6 * This file is released under the GPL v2.
7 */
8
9#include <linux/mm.h>
10#include <linux/fs.h>
Michel Lespinassebf181b92012-10-08 16:31:39 -070011#include <linux/rmap.h>
Michel Lespinasse9826a512012-10-08 16:31:35 -070012#include <linux/interval_tree_generic.h>
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070013
Michel Lespinasse9826a512012-10-08 16:31:35 -070014static inline unsigned long vma_start_pgoff(struct vm_area_struct *v)
15{
16 return v->vm_pgoff;
17}
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070018
Michel Lespinasse9826a512012-10-08 16:31:35 -070019static inline unsigned long vma_last_pgoff(struct vm_area_struct *v)
20{
21 return v->vm_pgoff + ((v->vm_end - v->vm_start) >> PAGE_SHIFT) - 1;
22}
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070023
Kirill A. Shutemovac51b932015-02-10 14:10:02 -080024INTERVAL_TREE_DEFINE(struct vm_area_struct, shared.rb,
25 unsigned long, shared.rb_subtree_last,
Michel Lespinasse9826a512012-10-08 16:31:35 -070026 vma_start_pgoff, vma_last_pgoff,, vma_interval_tree)
27
28/* Insert node immediately after prev in the interval tree */
29void vma_interval_tree_insert_after(struct vm_area_struct *node,
30 struct vm_area_struct *prev,
Davidlohr Buesof808c132017-09-08 16:15:08 -070031 struct rb_root_cached *root)
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070032{
33 struct rb_node **link;
34 struct vm_area_struct *parent;
Michel Lespinasse9826a512012-10-08 16:31:35 -070035 unsigned long last = vma_last_pgoff(node);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070036
Sasha Levin81d1b092014-10-09 15:28:10 -070037 VM_BUG_ON_VMA(vma_start_pgoff(node) != vma_start_pgoff(prev), node);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070038
Kirill A. Shutemovac51b932015-02-10 14:10:02 -080039 if (!prev->shared.rb.rb_right) {
Michel Lespinasse9826a512012-10-08 16:31:35 -070040 parent = prev;
Kirill A. Shutemovac51b932015-02-10 14:10:02 -080041 link = &prev->shared.rb.rb_right;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070042 } else {
Kirill A. Shutemovac51b932015-02-10 14:10:02 -080043 parent = rb_entry(prev->shared.rb.rb_right,
44 struct vm_area_struct, shared.rb);
45 if (parent->shared.rb_subtree_last < last)
46 parent->shared.rb_subtree_last = last;
47 while (parent->shared.rb.rb_left) {
48 parent = rb_entry(parent->shared.rb.rb_left,
49 struct vm_area_struct, shared.rb);
50 if (parent->shared.rb_subtree_last < last)
51 parent->shared.rb_subtree_last = last;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070052 }
Kirill A. Shutemovac51b932015-02-10 14:10:02 -080053 link = &parent->shared.rb.rb_left;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070054 }
55
Kirill A. Shutemovac51b932015-02-10 14:10:02 -080056 node->shared.rb_subtree_last = last;
57 rb_link_node(&node->shared.rb, &parent->shared.rb, link);
Davidlohr Buesof808c132017-09-08 16:15:08 -070058 rb_insert_augmented(&node->shared.rb, &root->rb_root,
Michel Lespinasse9826a512012-10-08 16:31:35 -070059 &vma_interval_tree_augment);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070060}
Michel Lespinassebf181b92012-10-08 16:31:39 -070061
62static inline unsigned long avc_start_pgoff(struct anon_vma_chain *avc)
63{
64 return vma_start_pgoff(avc->vma);
65}
66
67static inline unsigned long avc_last_pgoff(struct anon_vma_chain *avc)
68{
69 return vma_last_pgoff(avc->vma);
70}
71
72INTERVAL_TREE_DEFINE(struct anon_vma_chain, rb, unsigned long, rb_subtree_last,
Michel Lespinasseed8ea812012-10-08 16:31:45 -070073 avc_start_pgoff, avc_last_pgoff,
74 static inline, __anon_vma_interval_tree)
75
76void anon_vma_interval_tree_insert(struct anon_vma_chain *node,
Davidlohr Buesof808c132017-09-08 16:15:08 -070077 struct rb_root_cached *root)
Michel Lespinasseed8ea812012-10-08 16:31:45 -070078{
79#ifdef CONFIG_DEBUG_VM_RB
80 node->cached_vma_start = avc_start_pgoff(node);
81 node->cached_vma_last = avc_last_pgoff(node);
82#endif
83 __anon_vma_interval_tree_insert(node, root);
84}
85
86void anon_vma_interval_tree_remove(struct anon_vma_chain *node,
Davidlohr Buesof808c132017-09-08 16:15:08 -070087 struct rb_root_cached *root)
Michel Lespinasseed8ea812012-10-08 16:31:45 -070088{
89 __anon_vma_interval_tree_remove(node, root);
90}
91
92struct anon_vma_chain *
Davidlohr Buesof808c132017-09-08 16:15:08 -070093anon_vma_interval_tree_iter_first(struct rb_root_cached *root,
Michel Lespinasseed8ea812012-10-08 16:31:45 -070094 unsigned long first, unsigned long last)
95{
96 return __anon_vma_interval_tree_iter_first(root, first, last);
97}
98
99struct anon_vma_chain *
100anon_vma_interval_tree_iter_next(struct anon_vma_chain *node,
101 unsigned long first, unsigned long last)
102{
103 return __anon_vma_interval_tree_iter_next(node, first, last);
104}
105
106#ifdef CONFIG_DEBUG_VM_RB
107void anon_vma_interval_tree_verify(struct anon_vma_chain *node)
108{
109 WARN_ON_ONCE(node->cached_vma_start != avc_start_pgoff(node));
110 WARN_ON_ONCE(node->cached_vma_last != avc_last_pgoff(node));
111}
112#endif