2 * Handle caching attributes in page tables (PAT)
4 * Authors: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Suresh B Siddha <suresh.b.siddha@intel.com>
7 * Interval tree (augmented rbtree) used to store the PAT memory type
11 #include <linux/seq_file.h>
12 #include <linux/debugfs.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/rbtree_augmented.h>
16 #include <linux/sched.h>
17 #include <linux/gfp.h>
19 #include <asm/pgtable.h>
22 #include "pat_internal.h"
25 * The memtype tree keeps track of memory type for specific
26 * physical memory areas. Without proper tracking, conflicting memory
27 * types in different mappings can cause CPU cache corruption.
29 * The tree is an interval tree (augmented rbtree) with tree ordered
30 * on starting address. Tree can contain multiple entries for
31 * different regions which overlap. All the aliases have the same
32 * cache attributes of course.
34 * memtype_lock protects the rbtree.
37 static struct rb_root memtype_rbroot
= RB_ROOT
;
39 static int is_node_overlap(struct memtype
*node
, u64 start
, u64 end
)
41 if (node
->start
>= end
|| node
->end
<= start
)
47 static u64
get_subtree_max_end(struct rb_node
*node
)
51 struct memtype
*data
= container_of(node
, struct memtype
, rb
);
52 ret
= data
->subtree_max_end
;
57 static u64
compute_subtree_max_end(struct memtype
*data
)
59 u64 max_end
= data
->end
, child_max_end
;
61 child_max_end
= get_subtree_max_end(data
->rb
.rb_right
);
62 if (child_max_end
> max_end
)
63 max_end
= child_max_end
;
65 child_max_end
= get_subtree_max_end(data
->rb
.rb_left
);
66 if (child_max_end
> max_end
)
67 max_end
= child_max_end
;
72 RB_DECLARE_CALLBACKS(static, memtype_rb_augment_cb
, struct memtype
, rb
,
73 u64
, subtree_max_end
, compute_subtree_max_end
)
75 /* Find the first (lowest start addr) overlapping range from rb tree */
76 static struct memtype
*memtype_rb_lowest_match(struct rb_root
*root
,
79 struct rb_node
*node
= root
->rb_node
;
80 struct memtype
*last_lower
= NULL
;
83 struct memtype
*data
= container_of(node
, struct memtype
, rb
);
85 if (get_subtree_max_end(node
->rb_left
) > start
) {
86 /* Lowest overlap if any must be on left side */
88 } else if (is_node_overlap(data
, start
, end
)) {
91 } else if (start
>= data
->start
) {
92 /* Lowest overlap if any must be on right side */
93 node
= node
->rb_right
;
98 return last_lower
; /* Returns NULL if there is no overlap */
101 static struct memtype
*memtype_rb_exact_match(struct rb_root
*root
,
104 struct memtype
*match
;
106 match
= memtype_rb_lowest_match(root
, start
, end
);
107 while (match
!= NULL
&& match
->start
< end
) {
108 struct rb_node
*node
;
110 if (match
->start
== start
&& match
->end
== end
)
113 node
= rb_next(&match
->rb
);
115 match
= container_of(node
, struct memtype
, rb
);
120 return NULL
; /* Returns NULL if there is no exact match */
123 static int memtype_rb_check_conflict(struct rb_root
*root
,
125 unsigned long reqtype
, unsigned long *newtype
)
127 struct rb_node
*node
;
128 struct memtype
*match
;
129 int found_type
= reqtype
;
131 match
= memtype_rb_lowest_match(&memtype_rbroot
, start
, end
);
135 if (match
->type
!= found_type
&& newtype
== NULL
)
138 dprintk("Overlap at 0x%Lx-0x%Lx\n", match
->start
, match
->end
);
139 found_type
= match
->type
;
141 node
= rb_next(&match
->rb
);
143 match
= container_of(node
, struct memtype
, rb
);
145 if (match
->start
>= end
) /* Checked all possible matches */
148 if (is_node_overlap(match
, start
, end
) &&
149 match
->type
!= found_type
) {
153 node
= rb_next(&match
->rb
);
157 *newtype
= found_type
;
162 printk(KERN_INFO
"%s:%d conflicting memory types "
163 "%Lx-%Lx %s<->%s\n", current
->comm
, current
->pid
, start
,
164 end
, cattr_name(found_type
), cattr_name(match
->type
));
168 static void memtype_rb_insert(struct rb_root
*root
, struct memtype
*newdata
)
170 struct rb_node
**node
= &(root
->rb_node
);
171 struct rb_node
*parent
= NULL
;
174 struct memtype
*data
= container_of(*node
, struct memtype
, rb
);
177 if (data
->subtree_max_end
< newdata
->end
)
178 data
->subtree_max_end
= newdata
->end
;
179 if (newdata
->start
<= data
->start
)
180 node
= &((*node
)->rb_left
);
181 else if (newdata
->start
> data
->start
)
182 node
= &((*node
)->rb_right
);
185 newdata
->subtree_max_end
= newdata
->end
;
186 rb_link_node(&newdata
->rb
, parent
, node
);
187 rb_insert_augmented(&newdata
->rb
, root
, &memtype_rb_augment_cb
);
190 int rbt_memtype_check_insert(struct memtype
*new, unsigned long *ret_type
)
194 err
= memtype_rb_check_conflict(&memtype_rbroot
, new->start
, new->end
,
195 new->type
, ret_type
);
199 new->type
= *ret_type
;
201 new->subtree_max_end
= new->end
;
202 memtype_rb_insert(&memtype_rbroot
, new);
207 struct memtype
*rbt_memtype_erase(u64 start
, u64 end
)
209 struct memtype
*data
;
211 data
= memtype_rb_exact_match(&memtype_rbroot
, start
, end
);
215 rb_erase_augmented(&data
->rb
, &memtype_rbroot
, &memtype_rb_augment_cb
);
220 struct memtype
*rbt_memtype_lookup(u64 addr
)
222 struct memtype
*data
;
223 data
= memtype_rb_lowest_match(&memtype_rbroot
, addr
, addr
+ PAGE_SIZE
);
227 #if defined(CONFIG_DEBUG_FS)
228 int rbt_memtype_copy_nth_element(struct memtype
*out
, loff_t pos
)
230 struct rb_node
*node
;
233 node
= rb_first(&memtype_rbroot
);
234 while (node
&& pos
!= i
) {
235 node
= rb_next(node
);
239 if (node
) { /* pos == i */
240 struct memtype
*this = container_of(node
, struct memtype
, rb
);