pat_rbtree.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #include <linux/seq_file.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/sched.h>
  16. #include <linux/gfp.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/pat.h>
  19. #include "pat_internal.h"
  20. /*
  21. * The memtype tree keeps track of memory type for specific
  22. * physical memory areas. Without proper tracking, conflicting memory
  23. * types in different mappings can cause CPU cache corruption.
  24. *
  25. * The tree is an interval tree (augmented rbtree) with tree ordered
  26. * on starting address. Tree can contain multiple entries for
  27. * different regions which overlap. All the aliases have the same
  28. * cache attributes of course.
  29. *
  30. * memtype_lock protects the rbtree.
  31. */
  32. static void memtype_rb_augment_cb(struct rb_node *node);
  33. static struct rb_root memtype_rbroot = RB_AUGMENT_ROOT(&memtype_rb_augment_cb);
  34. static int is_node_overlap(struct memtype *node, u64 start, u64 end)
  35. {
  36. if (node->start >= end || node->end <= start)
  37. return 0;
  38. return 1;
  39. }
  40. static u64 get_subtree_max_end(struct rb_node *node)
  41. {
  42. u64 ret = 0;
  43. if (node) {
  44. struct memtype *data = container_of(node, struct memtype, rb);
  45. ret = data->subtree_max_end;
  46. }
  47. return ret;
  48. }
  49. /* Update 'subtree_max_end' for a node, based on node and its children */
  50. static void update_node_max_end(struct rb_node *node)
  51. {
  52. struct memtype *data;
  53. u64 max_end, child_max_end;
  54. if (!node)
  55. return;
  56. data = container_of(node, struct memtype, rb);
  57. max_end = data->end;
  58. child_max_end = get_subtree_max_end(node->rb_right);
  59. if (child_max_end > max_end)
  60. max_end = child_max_end;
  61. child_max_end = get_subtree_max_end(node->rb_left);
  62. if (child_max_end > max_end)
  63. max_end = child_max_end;
  64. data->subtree_max_end = max_end;
  65. }
  66. /* Update 'subtree_max_end' for a node and all its ancestors */
  67. static void update_path_max_end(struct rb_node *node)
  68. {
  69. u64 old_max_end, new_max_end;
  70. while (node) {
  71. struct memtype *data = container_of(node, struct memtype, rb);
  72. old_max_end = data->subtree_max_end;
  73. update_node_max_end(node);
  74. new_max_end = data->subtree_max_end;
  75. if (new_max_end == old_max_end)
  76. break;
  77. node = rb_parent(node);
  78. }
  79. }
  80. /* Find the first (lowest start addr) overlapping range from rb tree */
  81. static struct memtype *memtype_rb_lowest_match(struct rb_root *root,
  82. u64 start, u64 end)
  83. {
  84. struct rb_node *node = root->rb_node;
  85. struct memtype *last_lower = NULL;
  86. while (node) {
  87. struct memtype *data = container_of(node, struct memtype, rb);
  88. if (get_subtree_max_end(node->rb_left) > start) {
  89. /* Lowest overlap if any must be on left side */
  90. node = node->rb_left;
  91. } else if (is_node_overlap(data, start, end)) {
  92. last_lower = data;
  93. break;
  94. } else if (start >= data->start) {
  95. /* Lowest overlap if any must be on right side */
  96. node = node->rb_right;
  97. } else {
  98. break;
  99. }
  100. }
  101. return last_lower; /* Returns NULL if there is no overlap */
  102. }
  103. static struct memtype *memtype_rb_exact_match(struct rb_root *root,
  104. u64 start, u64 end)
  105. {
  106. struct memtype *match;
  107. match = memtype_rb_lowest_match(root, start, end);
  108. while (match != NULL && match->start < end) {
  109. struct rb_node *node;
  110. if (match->start == start && match->end == end)
  111. return match;
  112. node = rb_next(&match->rb);
  113. if (node)
  114. match = container_of(node, struct memtype, rb);
  115. else
  116. match = NULL;
  117. }
  118. return NULL; /* Returns NULL if there is no exact match */
  119. }
  120. static int memtype_rb_check_conflict(struct rb_root *root,
  121. u64 start, u64 end,
  122. unsigned long reqtype, unsigned long *newtype)
  123. {
  124. struct rb_node *node;
  125. struct memtype *match;
  126. int found_type = reqtype;
  127. match = memtype_rb_lowest_match(&memtype_rbroot, start, end);
  128. if (match == NULL)
  129. goto success;
  130. if (match->type != found_type && newtype == NULL)
  131. goto failure;
  132. dprintk("Overlap at 0x%Lx-0x%Lx\n", match->start, match->end);
  133. found_type = match->type;
  134. node = rb_next(&match->rb);
  135. while (node) {
  136. match = container_of(node, struct memtype, rb);
  137. if (match->start >= end) /* Checked all possible matches */
  138. goto success;
  139. if (is_node_overlap(match, start, end) &&
  140. match->type != found_type) {
  141. goto failure;
  142. }
  143. node = rb_next(&match->rb);
  144. }
  145. success:
  146. if (newtype)
  147. *newtype = found_type;
  148. return 0;
  149. failure:
  150. printk(KERN_INFO "%s:%d conflicting memory types "
  151. "%Lx-%Lx %s<->%s\n", current->comm, current->pid, start,
  152. end, cattr_name(found_type), cattr_name(match->type));
  153. return -EBUSY;
  154. }
  155. static void memtype_rb_augment_cb(struct rb_node *node)
  156. {
  157. if (node)
  158. update_path_max_end(node);
  159. }
  160. static void memtype_rb_insert(struct rb_root *root, struct memtype *newdata)
  161. {
  162. struct rb_node **node = &(root->rb_node);
  163. struct rb_node *parent = NULL;
  164. while (*node) {
  165. struct memtype *data = container_of(*node, struct memtype, rb);
  166. parent = *node;
  167. if (newdata->start <= data->start)
  168. node = &((*node)->rb_left);
  169. else if (newdata->start > data->start)
  170. node = &((*node)->rb_right);
  171. }
  172. rb_link_node(&newdata->rb, parent, node);
  173. rb_insert_color(&newdata->rb, root);
  174. }
  175. int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)
  176. {
  177. int err = 0;
  178. err = memtype_rb_check_conflict(&memtype_rbroot, new->start, new->end,
  179. new->type, ret_type);
  180. if (!err) {
  181. if (ret_type)
  182. new->type = *ret_type;
  183. memtype_rb_insert(&memtype_rbroot, new);
  184. }
  185. return err;
  186. }
  187. int rbt_memtype_erase(u64 start, u64 end)
  188. {
  189. struct memtype *data;
  190. data = memtype_rb_exact_match(&memtype_rbroot, start, end);
  191. if (!data)
  192. return -EINVAL;
  193. rb_erase(&data->rb, &memtype_rbroot);
  194. return 0;
  195. }
  196. struct memtype *rbt_memtype_lookup(u64 addr)
  197. {
  198. struct memtype *data;
  199. data = memtype_rb_lowest_match(&memtype_rbroot, addr, addr + PAGE_SIZE);
  200. return data;
  201. }
  202. #if defined(CONFIG_DEBUG_FS)
  203. int rbt_memtype_copy_nth_element(struct memtype *out, loff_t pos)
  204. {
  205. struct rb_node *node;
  206. int i = 1;
  207. node = rb_first(&memtype_rbroot);
  208. while (node && pos != i) {
  209. node = rb_next(node);
  210. i++;
  211. }
  212. if (node) { /* pos == i */
  213. struct memtype *this = container_of(node, struct memtype, rb);
  214. *out = *this;
  215. return 0;
  216. } else {
  217. return 1;
  218. }
  219. }
  220. #endif