aboutsummaryrefslogtreecommitdiff
path: root/mm/interval_tree.c
blob: 7dc565660e562b8e73acdbbdef7df8a43d0b94d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * mm/interval_tree.c - interval tree for mapping->i_mmap
 *
 * Copyright (C) 2012, Michel Lespinasse <walken@google.com>
 *
 * This file is released under the GPL v2.
 */

#include <linux/mm.h>
#include <linux/fs.h>

#define ITSTRUCT   struct vm_area_struct
#define ITRB       shared.linear.rb
#define ITTYPE     unsigned long
#define ITSUBTREE  shared.linear.rb_subtree_last
#define ITSTART(n) ((n)->vm_pgoff)
#define ITLAST(n)  ((n)->vm_pgoff + \
		    (((n)->vm_end - (n)->vm_start) >> PAGE_SHIFT) - 1)
#define ITSTATIC
#define ITPREFIX   vma_interval_tree

#include <linux/interval_tree_tmpl.h>

/* Insert old immediately after vma in the interval tree */
void vma_interval_tree_add(struct vm_area_struct *vma,
			   struct vm_area_struct *old,
			   struct address_space *mapping)
{
	struct rb_node **link;
	struct vm_area_struct *parent;
	unsigned long last;

	if (unlikely(vma->vm_flags & VM_NONLINEAR)) {
		list_add(&vma->shared.nonlinear, &old->shared.nonlinear);
		return;
	}

	last = ITLAST(vma);

	if (!old->shared.linear.rb.rb_right) {
		parent = old;
		link = &old->shared.linear.rb.rb_right;
	} else {
		parent = rb_entry(old->shared.linear.rb.rb_right,
				  struct vm_area_struct, shared.linear.rb);
		if (parent->shared.linear.rb_subtree_last < last)
			parent->shared.linear.rb_subtree_last = last;
		while (parent->shared.linear.rb.rb_left) {
			parent = rb_entry(parent->shared.linear.rb.rb_left,
				struct vm_area_struct, shared.linear.rb);
			if (parent->shared.linear.rb_subtree_last < last)
				parent->shared.linear.rb_subtree_last = last;
		}
		link = &parent->shared.linear.rb.rb_left;
	}

	vma->shared.linear.rb_subtree_last = last;
	rb_link_node(&vma->shared.linear.rb, &parent->shared.linear.rb, link);
	rb_insert_augmented(&vma->shared.linear.rb, &mapping->i_mmap,
			    &vma_interval_tree_augment_callbacks);
}