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.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 void memtype_rb_augment_cb(struct rb_node *node);
38 static struct rb_root memtype_rbroot = RB_AUGMENT_ROOT(&memtype_rb_augment_cb);
40 static int is_node_overlap(struct memtype *node, u64 start, u64 end)
42 if (node->start >= end || node->end <= start)
48 static u64 get_subtree_max_end(struct rb_node *node)
52 struct memtype *data = container_of(node, struct memtype, rb);
53 ret = data->subtree_max_end;
58 /* Update 'subtree_max_end' for a node, based on node and its children */
59 static void update_node_max_end(struct rb_node *node)
62 u64 max_end, child_max_end;
67 data = container_of(node, struct memtype, rb);
70 child_max_end = get_subtree_max_end(node->rb_right);
71 if (child_max_end > max_end)
72 max_end = child_max_end;
74 child_max_end = get_subtree_max_end(node->rb_left);
75 if (child_max_end > max_end)
76 max_end = child_max_end;
78 data->subtree_max_end = max_end;
81 /* Update 'subtree_max_end' for a node and all its ancestors */
82 static void update_path_max_end(struct rb_node *node)
84 u64 old_max_end, new_max_end;
87 struct memtype *data = container_of(node, struct memtype, rb);
89 old_max_end = data->subtree_max_end;
90 update_node_max_end(node);
91 new_max_end = data->subtree_max_end;
93 if (new_max_end == old_max_end)
96 node = rb_parent(node);
100 /* Find the first (lowest start addr) overlapping range from rb tree */
101 static struct memtype *memtype_rb_lowest_match(struct rb_root *root,
104 struct rb_node *node = root->rb_node;
105 struct memtype *last_lower = NULL;
108 struct memtype *data = container_of(node, struct memtype, rb);
110 if (get_subtree_max_end(node->rb_left) > start) {
111 /* Lowest overlap if any must be on left side */
112 node = node->rb_left;
113 } else if (is_node_overlap(data, start, end)) {
116 } else if (start >= data->start) {
117 /* Lowest overlap if any must be on right side */
118 node = node->rb_right;
123 return last_lower; /* Returns NULL if there is no overlap */
126 static struct memtype *memtype_rb_exact_match(struct rb_root *root,
129 struct memtype *match;
131 match = memtype_rb_lowest_match(root, start, end);
132 while (match != NULL && match->start < end) {
133 struct rb_node *node;
135 if (match->start == start && match->end == end)
138 node = rb_next(&match->rb);
140 match = container_of(node, struct memtype, rb);
145 return NULL; /* Returns NULL if there is no exact match */
148 static int memtype_rb_check_conflict(struct rb_root *root,
150 unsigned long reqtype, unsigned long *newtype)
152 struct rb_node *node;
153 struct memtype *match;
154 int found_type = reqtype;
156 match = memtype_rb_lowest_match(&memtype_rbroot, start, end);
160 if (match->type != found_type && newtype == NULL)
163 dprintk("Overlap at 0x%Lx-0x%Lx\n", match->start, match->end);
164 found_type = match->type;
166 node = rb_next(&match->rb);
168 match = container_of(node, struct memtype, rb);
170 if (match->start >= end) /* Checked all possible matches */
173 if (is_node_overlap(match, start, end) &&
174 match->type != found_type) {
178 node = rb_next(&match->rb);
182 *newtype = found_type;
187 printk(KERN_INFO "%s:%d conflicting memory types "
188 "%Lx-%Lx %s<->%s\n", current->comm, current->pid, start,
189 end, cattr_name(found_type), cattr_name(match->type));
193 static void memtype_rb_augment_cb(struct rb_node *node)
196 update_path_max_end(node);
199 static void memtype_rb_insert(struct rb_root *root, struct memtype *newdata)
201 struct rb_node **node = &(root->rb_node);
202 struct rb_node *parent = NULL;
205 struct memtype *data = container_of(*node, struct memtype, rb);
208 if (newdata->start <= data->start)
209 node = &((*node)->rb_left);
210 else if (newdata->start > data->start)
211 node = &((*node)->rb_right);
214 rb_link_node(&newdata->rb, parent, node);
215 rb_insert_color(&newdata->rb, root);
218 int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)
222 err = memtype_rb_check_conflict(&memtype_rbroot, new->start, new->end,
223 new->type, ret_type);
227 new->type = *ret_type;
229 memtype_rb_insert(&memtype_rbroot, new);
234 int rbt_memtype_erase(u64 start, u64 end)
236 struct memtype *data;
238 data = memtype_rb_exact_match(&memtype_rbroot, start, end);
242 rb_erase(&data->rb, &memtype_rbroot);
246 struct memtype *rbt_memtype_lookup(u64 addr)
248 struct memtype *data;
249 data = memtype_rb_lowest_match(&memtype_rbroot, addr, addr + PAGE_SIZE);
253 #if defined(CONFIG_DEBUG_FS)
254 int rbt_memtype_copy_nth_element(struct memtype *out, loff_t pos)
256 struct rb_node *node;
259 node = rb_first(&memtype_rbroot);
260 while (node && pos != i) {
261 node = rb_next(node);
265 if (node) { /* pos == i */
266 struct memtype *this = container_of(node, struct memtype, rb);