blob: f7c72cd35e1d82aeb8e907a88a48e5cb0d9f3e9b [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
Michel Lespinasse9826a512012-10-08 16:31:35 -070024INTERVAL_TREE_DEFINE(struct vm_area_struct, shared.linear.rb,
25 unsigned long, shared.linear.rb_subtree_last,
26 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,
31 struct rb_root *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
Michel Lespinasse9826a512012-10-08 16:31:35 -070037 VM_BUG_ON(vma_start_pgoff(node) != vma_start_pgoff(prev));
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070038
Michel Lespinasse9826a512012-10-08 16:31:35 -070039 if (!prev->shared.linear.rb.rb_right) {
40 parent = prev;
41 link = &prev->shared.linear.rb.rb_right;
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070042 } else {
Michel Lespinasse9826a512012-10-08 16:31:35 -070043 parent = rb_entry(prev->shared.linear.rb.rb_right,
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -070044 struct vm_area_struct, shared.linear.rb);
45 if (parent->shared.linear.rb_subtree_last < last)
46 parent->shared.linear.rb_subtree_last = last;
47 while (parent->shared.linear.rb.rb_left) {
48 parent = rb_entry(parent->shared.linear.rb.rb_left,
49 struct vm_area_struct, shared.linear.rb);
50 if (parent->shared.linear.rb_subtree_last < last)
51 parent->shared.linear.rb_subtree_last = last;
52 }
53 link = &parent->shared.linear.rb.rb_left;
54 }
55
Michel Lespinasse9826a512012-10-08 16:31:35 -070056 node->shared.linear.rb_subtree_last = last;
57 rb_link_node(&node->shared.linear.rb, &parent->shared.linear.rb, link);
58 rb_insert_augmented(&node->shared.linear.rb, root,
59 &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,
73 avc_start_pgoff, avc_last_pgoff,, anon_vma_interval_tree)