Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Handle caching attributes in page tables (PAT) |
| 3 | * |
| 4 | * Authors: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> |
| 5 | * Suresh B Siddha <suresh.b.siddha@intel.com> |
| 6 | * |
| 7 | * Interval tree (augmented rbtree) used to store the PAT memory type |
| 8 | * reservations. |
| 9 | */ |
| 10 | |
| 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> |
| 18 | |
| 19 | #include <asm/pgtable.h> |
| 20 | #include <asm/pat.h> |
| 21 | |
| 22 | #include "pat_internal.h" |
| 23 | |
| 24 | /* |
| 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. |
| 28 | * |
| 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. |
| 33 | * |
| 34 | * memtype_lock protects the rbtree. |
| 35 | */ |
| 36 | |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 37 | static struct rb_root memtype_rbroot = RB_ROOT; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 38 | |
| 39 | static int is_node_overlap(struct memtype *node, u64 start, u64 end) |
| 40 | { |
| 41 | if (node->start >= end || node->end <= start) |
| 42 | return 0; |
| 43 | |
| 44 | return 1; |
| 45 | } |
| 46 | |
| 47 | static u64 get_subtree_max_end(struct rb_node *node) |
| 48 | { |
| 49 | u64 ret = 0; |
| 50 | if (node) { |
| 51 | struct memtype *data = container_of(node, struct memtype, rb); |
| 52 | ret = data->subtree_max_end; |
| 53 | } |
| 54 | return ret; |
| 55 | } |
| 56 | |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 57 | static u64 compute_subtree_max_end(struct memtype *data) |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 58 | { |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 59 | u64 max_end = data->end, child_max_end; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 60 | |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 61 | child_max_end = get_subtree_max_end(data->rb.rb_right); |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 62 | if (child_max_end > max_end) |
| 63 | max_end = child_max_end; |
| 64 | |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 65 | child_max_end = get_subtree_max_end(data->rb.rb_left); |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 66 | if (child_max_end > max_end) |
| 67 | max_end = child_max_end; |
| 68 | |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 69 | return max_end; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 72 | /* Update 'subtree_max_end' for node and its parents */ |
| 73 | static void memtype_rb_propagate_cb(struct rb_node *node, struct rb_node *stop) |
| 74 | { |
| 75 | while (node != stop) { |
| 76 | struct memtype *data = container_of(node, struct memtype, rb); |
| 77 | u64 subtree_max_end = compute_subtree_max_end(data); |
| 78 | if (data->subtree_max_end == subtree_max_end) |
| 79 | break; |
| 80 | data->subtree_max_end = subtree_max_end; |
| 81 | node = rb_parent(&data->rb); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static void memtype_rb_copy_cb(struct rb_node *old, struct rb_node *new) |
| 86 | { |
| 87 | struct memtype *old_data = container_of(old, struct memtype, rb); |
| 88 | struct memtype *new_data = container_of(new, struct memtype, rb); |
| 89 | |
| 90 | new_data->subtree_max_end = old_data->subtree_max_end; |
| 91 | } |
| 92 | |
| 93 | /* Update 'subtree_max_end' after tree rotation. old and new are the |
| 94 | * former and current subtree roots */ |
| 95 | static void memtype_rb_rotate_cb(struct rb_node *old, struct rb_node *new) |
| 96 | { |
| 97 | struct memtype *old_data = container_of(old, struct memtype, rb); |
| 98 | struct memtype *new_data = container_of(new, struct memtype, rb); |
| 99 | |
| 100 | new_data->subtree_max_end = old_data->subtree_max_end; |
| 101 | old_data->subtree_max_end = compute_subtree_max_end(old_data); |
| 102 | } |
| 103 | |
| 104 | static const struct rb_augment_callbacks memtype_rb_augment_cb = { |
| 105 | memtype_rb_propagate_cb, memtype_rb_copy_cb, memtype_rb_rotate_cb |
| 106 | }; |
| 107 | |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 108 | /* Find the first (lowest start addr) overlapping range from rb tree */ |
| 109 | static struct memtype *memtype_rb_lowest_match(struct rb_root *root, |
| 110 | u64 start, u64 end) |
| 111 | { |
| 112 | struct rb_node *node = root->rb_node; |
| 113 | struct memtype *last_lower = NULL; |
| 114 | |
| 115 | while (node) { |
| 116 | struct memtype *data = container_of(node, struct memtype, rb); |
| 117 | |
| 118 | if (get_subtree_max_end(node->rb_left) > start) { |
| 119 | /* Lowest overlap if any must be on left side */ |
| 120 | node = node->rb_left; |
| 121 | } else if (is_node_overlap(data, start, end)) { |
| 122 | last_lower = data; |
| 123 | break; |
| 124 | } else if (start >= data->start) { |
| 125 | /* Lowest overlap if any must be on right side */ |
| 126 | node = node->rb_right; |
| 127 | } else { |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | return last_lower; /* Returns NULL if there is no overlap */ |
| 132 | } |
| 133 | |
| 134 | static struct memtype *memtype_rb_exact_match(struct rb_root *root, |
| 135 | u64 start, u64 end) |
| 136 | { |
| 137 | struct memtype *match; |
| 138 | |
| 139 | match = memtype_rb_lowest_match(root, start, end); |
| 140 | while (match != NULL && match->start < end) { |
| 141 | struct rb_node *node; |
| 142 | |
| 143 | if (match->start == start && match->end == end) |
| 144 | return match; |
| 145 | |
| 146 | node = rb_next(&match->rb); |
| 147 | if (node) |
| 148 | match = container_of(node, struct memtype, rb); |
| 149 | else |
| 150 | match = NULL; |
| 151 | } |
| 152 | |
| 153 | return NULL; /* Returns NULL if there is no exact match */ |
| 154 | } |
| 155 | |
| 156 | static int memtype_rb_check_conflict(struct rb_root *root, |
| 157 | u64 start, u64 end, |
| 158 | unsigned long reqtype, unsigned long *newtype) |
| 159 | { |
| 160 | struct rb_node *node; |
| 161 | struct memtype *match; |
| 162 | int found_type = reqtype; |
| 163 | |
| 164 | match = memtype_rb_lowest_match(&memtype_rbroot, start, end); |
| 165 | if (match == NULL) |
| 166 | goto success; |
| 167 | |
| 168 | if (match->type != found_type && newtype == NULL) |
| 169 | goto failure; |
| 170 | |
| 171 | dprintk("Overlap at 0x%Lx-0x%Lx\n", match->start, match->end); |
| 172 | found_type = match->type; |
| 173 | |
| 174 | node = rb_next(&match->rb); |
| 175 | while (node) { |
| 176 | match = container_of(node, struct memtype, rb); |
| 177 | |
| 178 | if (match->start >= end) /* Checked all possible matches */ |
| 179 | goto success; |
| 180 | |
| 181 | if (is_node_overlap(match, start, end) && |
| 182 | match->type != found_type) { |
| 183 | goto failure; |
| 184 | } |
| 185 | |
| 186 | node = rb_next(&match->rb); |
| 187 | } |
| 188 | success: |
| 189 | if (newtype) |
| 190 | *newtype = found_type; |
| 191 | |
| 192 | return 0; |
| 193 | |
| 194 | failure: |
| 195 | printk(KERN_INFO "%s:%d conflicting memory types " |
| 196 | "%Lx-%Lx %s<->%s\n", current->comm, current->pid, start, |
| 197 | end, cattr_name(found_type), cattr_name(match->type)); |
| 198 | return -EBUSY; |
| 199 | } |
| 200 | |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 201 | static void memtype_rb_insert(struct rb_root *root, struct memtype *newdata) |
| 202 | { |
| 203 | struct rb_node **node = &(root->rb_node); |
| 204 | struct rb_node *parent = NULL; |
| 205 | |
| 206 | while (*node) { |
| 207 | struct memtype *data = container_of(*node, struct memtype, rb); |
| 208 | |
| 209 | parent = *node; |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 210 | if (data->subtree_max_end < newdata->end) |
| 211 | data->subtree_max_end = newdata->end; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 212 | if (newdata->start <= data->start) |
| 213 | node = &((*node)->rb_left); |
| 214 | else if (newdata->start > data->start) |
| 215 | node = &((*node)->rb_right); |
| 216 | } |
| 217 | |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 218 | newdata->subtree_max_end = newdata->end; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 219 | rb_link_node(&newdata->rb, parent, node); |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 220 | rb_insert_augmented(&newdata->rb, root, &memtype_rb_augment_cb); |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type) |
| 224 | { |
| 225 | int err = 0; |
| 226 | |
| 227 | err = memtype_rb_check_conflict(&memtype_rbroot, new->start, new->end, |
| 228 | new->type, ret_type); |
| 229 | |
| 230 | if (!err) { |
Pallipadi, Venkatesh | 4daa2a8 | 2010-02-24 13:43:55 -0800 | [diff] [blame] | 231 | if (ret_type) |
| 232 | new->type = *ret_type; |
| 233 | |
Venkatesh Pallipadi | 6a4f3b5 | 2010-06-10 17:45:01 -0700 | [diff] [blame] | 234 | new->subtree_max_end = new->end; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 235 | memtype_rb_insert(&memtype_rbroot, new); |
| 236 | } |
| 237 | return err; |
| 238 | } |
| 239 | |
Xiaotian Feng | 20413f2 | 2010-05-26 09:51:10 +0800 | [diff] [blame] | 240 | struct memtype *rbt_memtype_erase(u64 start, u64 end) |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 241 | { |
| 242 | struct memtype *data; |
| 243 | |
| 244 | data = memtype_rb_exact_match(&memtype_rbroot, start, end); |
| 245 | if (!data) |
Xiaotian Feng | 20413f2 | 2010-05-26 09:51:10 +0800 | [diff] [blame] | 246 | goto out; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 247 | |
Michel Lespinasse | 9d9e6f9 | 2012-10-08 16:31:20 -0700 | [diff] [blame^] | 248 | rb_erase_augmented(&data->rb, &memtype_rbroot, &memtype_rb_augment_cb); |
Xiaotian Feng | 20413f2 | 2010-05-26 09:51:10 +0800 | [diff] [blame] | 249 | out: |
| 250 | return data; |
Pallipadi, Venkatesh | 9e41a49 | 2010-02-10 15:26:07 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | struct memtype *rbt_memtype_lookup(u64 addr) |
| 254 | { |
| 255 | struct memtype *data; |
| 256 | data = memtype_rb_lowest_match(&memtype_rbroot, addr, addr + PAGE_SIZE); |
| 257 | return data; |
| 258 | } |
| 259 | |
| 260 | #if defined(CONFIG_DEBUG_FS) |
| 261 | int rbt_memtype_copy_nth_element(struct memtype *out, loff_t pos) |
| 262 | { |
| 263 | struct rb_node *node; |
| 264 | int i = 1; |
| 265 | |
| 266 | node = rb_first(&memtype_rbroot); |
| 267 | while (node && pos != i) { |
| 268 | node = rb_next(node); |
| 269 | i++; |
| 270 | } |
| 271 | |
| 272 | if (node) { /* pos == i */ |
| 273 | struct memtype *this = container_of(node, struct memtype, rb); |
| 274 | *out = *this; |
| 275 | return 0; |
| 276 | } else { |
| 277 | return 1; |
| 278 | } |
| 279 | } |
| 280 | #endif |