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/rbtree_augmented.h>
15 #include <linux/sched.h>
16 #include <linux/gfp.h>
18 #include <asm/pgtable.h>
21 #include "pat_internal.h"
24 * The memtype tree keeps track of memory type for specific
25 * physical memory areas. Without proper tracking, conflicting memory
26 * types in different mappings can cause CPU cache corruption.
28 * The tree is an interval tree (augmented rbtree) with tree ordered
29 * on starting address. Tree can contain multiple entries for
30 * different regions which overlap. All the aliases have the same
31 * cache attributes of course.
33 * memtype_lock protects the rbtree.
36 static struct rb_root memtype_rbroot
= RB_ROOT
;
38 static int is_node_overlap(struct memtype
*node
, u64 start
, u64 end
)
40 if (node
->start
>= end
|| node
->end
<= start
)
46 static u64
get_subtree_max_end(struct rb_node
*node
)
50 struct memtype
*data
= rb_entry(node
, struct memtype
, rb
);
51 ret
= data
->subtree_max_end
;
56 static u64
compute_subtree_max_end(struct memtype
*data
)
58 u64 max_end
= data
->end
, child_max_end
;
60 child_max_end
= get_subtree_max_end(data
->rb
.rb_right
);
61 if (child_max_end
> max_end
)
62 max_end
= child_max_end
;
64 child_max_end
= get_subtree_max_end(data
->rb
.rb_left
);
65 if (child_max_end
> max_end
)
66 max_end
= child_max_end
;
71 RB_DECLARE_CALLBACKS(static, memtype_rb_augment_cb
, struct memtype
, rb
,
72 u64
, subtree_max_end
, compute_subtree_max_end
)
74 /* Find the first (lowest start addr) overlapping range from rb tree */
75 static struct memtype
*memtype_rb_lowest_match(struct rb_root
*root
,
78 struct rb_node
*node
= root
->rb_node
;
79 struct memtype
*last_lower
= NULL
;
82 struct memtype
*data
= rb_entry(node
, struct memtype
, rb
);
84 if (get_subtree_max_end(node
->rb_left
) > start
) {
85 /* Lowest overlap if any must be on left side */
87 } else if (is_node_overlap(data
, start
, end
)) {
90 } else if (start
>= data
->start
) {
91 /* Lowest overlap if any must be on right side */
92 node
= node
->rb_right
;
97 return last_lower
; /* Returns NULL if there is no overlap */
101 MEMTYPE_EXACT_MATCH
= 0,
102 MEMTYPE_END_MATCH
= 1
105 static struct memtype
*memtype_rb_match(struct rb_root
*root
,
106 u64 start
, u64 end
, int match_type
)
108 struct memtype
*match
;
110 match
= memtype_rb_lowest_match(root
, start
, end
);
111 while (match
!= NULL
&& match
->start
< end
) {
112 struct rb_node
*node
;
114 if ((match_type
== MEMTYPE_EXACT_MATCH
) &&
115 (match
->start
== start
) && (match
->end
== end
))
118 if ((match_type
== MEMTYPE_END_MATCH
) &&
119 (match
->start
< start
) && (match
->end
== end
))
122 node
= rb_next(&match
->rb
);
124 match
= rb_entry(node
, struct memtype
, rb
);
129 return NULL
; /* Returns NULL if there is no match */
132 static int memtype_rb_check_conflict(struct rb_root
*root
,
134 enum page_cache_mode reqtype
,
135 enum page_cache_mode
*newtype
)
137 struct rb_node
*node
;
138 struct memtype
*match
;
139 enum page_cache_mode found_type
= reqtype
;
141 match
= memtype_rb_lowest_match(&memtype_rbroot
, start
, end
);
145 if (match
->type
!= found_type
&& newtype
== NULL
)
148 dprintk("Overlap at 0x%Lx-0x%Lx\n", match
->start
, match
->end
);
149 found_type
= match
->type
;
151 node
= rb_next(&match
->rb
);
153 match
= rb_entry(node
, struct memtype
, rb
);
155 if (match
->start
>= end
) /* Checked all possible matches */
158 if (is_node_overlap(match
, start
, end
) &&
159 match
->type
!= found_type
) {
163 node
= rb_next(&match
->rb
);
167 *newtype
= found_type
;
172 pr_info("x86/PAT: %s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
173 current
->comm
, current
->pid
, start
, end
,
174 cattr_name(found_type
), cattr_name(match
->type
));
178 static void memtype_rb_insert(struct rb_root
*root
, struct memtype
*newdata
)
180 struct rb_node
**node
= &(root
->rb_node
);
181 struct rb_node
*parent
= NULL
;
184 struct memtype
*data
= rb_entry(*node
, struct memtype
, rb
);
187 if (data
->subtree_max_end
< newdata
->end
)
188 data
->subtree_max_end
= newdata
->end
;
189 if (newdata
->start
<= data
->start
)
190 node
= &((*node
)->rb_left
);
191 else if (newdata
->start
> data
->start
)
192 node
= &((*node
)->rb_right
);
195 newdata
->subtree_max_end
= newdata
->end
;
196 rb_link_node(&newdata
->rb
, parent
, node
);
197 rb_insert_augmented(&newdata
->rb
, root
, &memtype_rb_augment_cb
);
200 int rbt_memtype_check_insert(struct memtype
*new,
201 enum page_cache_mode
*ret_type
)
205 err
= memtype_rb_check_conflict(&memtype_rbroot
, new->start
, new->end
,
206 new->type
, ret_type
);
210 new->type
= *ret_type
;
212 new->subtree_max_end
= new->end
;
213 memtype_rb_insert(&memtype_rbroot
, new);
218 struct memtype
*rbt_memtype_erase(u64 start
, u64 end
)
220 struct memtype
*data
;
223 * Since the memtype_rbroot tree allows overlapping ranges,
224 * rbt_memtype_erase() checks with EXACT_MATCH first, i.e. free
225 * a whole node for the munmap case. If no such entry is found,
226 * it then checks with END_MATCH, i.e. shrink the size of a node
227 * from the end for the mremap case.
229 data
= memtype_rb_match(&memtype_rbroot
, start
, end
,
230 MEMTYPE_EXACT_MATCH
);
232 data
= memtype_rb_match(&memtype_rbroot
, start
, end
,
235 return ERR_PTR(-EINVAL
);
238 if (data
->start
== start
) {
239 /* munmap: erase this node */
240 rb_erase_augmented(&data
->rb
, &memtype_rbroot
,
241 &memtype_rb_augment_cb
);
243 /* mremap: update the end value of this node */
244 rb_erase_augmented(&data
->rb
, &memtype_rbroot
,
245 &memtype_rb_augment_cb
);
247 data
->subtree_max_end
= data
->end
;
248 memtype_rb_insert(&memtype_rbroot
, data
);
255 struct memtype
*rbt_memtype_lookup(u64 addr
)
257 return memtype_rb_lowest_match(&memtype_rbroot
, addr
, addr
+ PAGE_SIZE
);
260 #if defined(CONFIG_DEBUG_FS)
261 int rbt_memtype_copy_nth_element(struct memtype
*out
, loff_t pos
)
263 struct rb_node
*node
;
266 node
= rb_first(&memtype_rbroot
);
267 while (node
&& pos
!= i
) {
268 node
= rb_next(node
);
272 if (node
) { /* pos == i */
273 struct memtype
*this = rb_entry(node
, struct memtype
, rb
);