pat_rbtree.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 struct rb_root memtype_rbroot = RB_ROOT;
  33. static int is_node_overlap(struct memtype *node, u64 start, u64 end)
  34. {
  35. if (node->start >= end || node->end <= start)
  36. return 0;
  37. return 1;
  38. }
  39. static u64 get_subtree_max_end(struct rb_node *node)
  40. {
  41. u64 ret = 0;
  42. if (node) {
  43. struct memtype *data = container_of(node, struct memtype, rb);
  44. ret = data->subtree_max_end;
  45. }
  46. return ret;
  47. }
  48. /* Update 'subtree_max_end' for a node, based on node and its children */
  49. static void memtype_rb_augment_cb(struct rb_node *node, void *__unused)
  50. {
  51. struct memtype *data;
  52. u64 max_end, child_max_end;
  53. if (!node)
  54. return;
  55. data = container_of(node, struct memtype, rb);
  56. max_end = data->end;
  57. child_max_end = get_subtree_max_end(node->rb_right);
  58. if (child_max_end > max_end)
  59. max_end = child_max_end;
  60. child_max_end = get_subtree_max_end(node->rb_left);
  61. if (child_max_end > max_end)
  62. max_end = child_max_end;
  63. data->subtree_max_end = max_end;
  64. }
  65. /* Find the first (lowest start addr) overlapping range from rb tree */
  66. static struct memtype *memtype_rb_lowest_match(struct rb_root *root,
  67. u64 start, u64 end)
  68. {
  69. struct rb_node *node = root->rb_node;
  70. struct memtype *last_lower = NULL;
  71. while (node) {
  72. struct memtype *data = container_of(node, struct memtype, rb);
  73. if (get_subtree_max_end(node->rb_left) > start) {
  74. /* Lowest overlap if any must be on left side */
  75. node = node->rb_left;
  76. } else if (is_node_overlap(data, start, end)) {
  77. last_lower = data;
  78. break;
  79. } else if (start >= data->start) {
  80. /* Lowest overlap if any must be on right side */
  81. node = node->rb_right;
  82. } else {
  83. break;
  84. }
  85. }
  86. return last_lower; /* Returns NULL if there is no overlap */
  87. }
  88. static struct memtype *memtype_rb_exact_match(struct rb_root *root,
  89. u64 start, u64 end)
  90. {
  91. struct memtype *match;
  92. match = memtype_rb_lowest_match(root, start, end);
  93. while (match != NULL && match->start < end) {
  94. struct rb_node *node;
  95. if (match->start == start && match->end == end)
  96. return match;
  97. node = rb_next(&match->rb);
  98. if (node)
  99. match = container_of(node, struct memtype, rb);
  100. else
  101. match = NULL;
  102. }
  103. return NULL; /* Returns NULL if there is no exact match */
  104. }
  105. static int memtype_rb_check_conflict(struct rb_root *root,
  106. u64 start, u64 end,
  107. unsigned long reqtype, unsigned long *newtype)
  108. {
  109. struct rb_node *node;
  110. struct memtype *match;
  111. int found_type = reqtype;
  112. match = memtype_rb_lowest_match(&memtype_rbroot, start, end);
  113. if (match == NULL)
  114. goto success;
  115. if (match->type != found_type && newtype == NULL)
  116. goto failure;
  117. dprintk("Overlap at 0x%Lx-0x%Lx\n", match->start, match->end);
  118. found_type = match->type;
  119. node = rb_next(&match->rb);
  120. while (node) {
  121. match = container_of(node, struct memtype, rb);
  122. if (match->start >= end) /* Checked all possible matches */
  123. goto success;
  124. if (is_node_overlap(match, start, end) &&
  125. match->type != found_type) {
  126. goto failure;
  127. }
  128. node = rb_next(&match->rb);
  129. }
  130. success:
  131. if (newtype)
  132. *newtype = found_type;
  133. return 0;
  134. failure:
  135. printk(KERN_INFO "%s:%d conflicting memory types "
  136. "%Lx-%Lx %s<->%s\n", current->comm, current->pid, start,
  137. end, cattr_name(found_type), cattr_name(match->type));
  138. return -EBUSY;
  139. }
  140. static void memtype_rb_insert(struct rb_root *root, struct memtype *newdata)
  141. {
  142. struct rb_node **node = &(root->rb_node);
  143. struct rb_node *parent = NULL;
  144. while (*node) {
  145. struct memtype *data = container_of(*node, struct memtype, rb);
  146. parent = *node;
  147. if (newdata->start <= data->start)
  148. node = &((*node)->rb_left);
  149. else if (newdata->start > data->start)
  150. node = &((*node)->rb_right);
  151. }
  152. rb_link_node(&newdata->rb, parent, node);
  153. rb_insert_color(&newdata->rb, root);
  154. rb_augment_insert(&newdata->rb, memtype_rb_augment_cb, NULL);
  155. }
  156. int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)
  157. {
  158. int err = 0;
  159. err = memtype_rb_check_conflict(&memtype_rbroot, new->start, new->end,
  160. new->type, ret_type);
  161. if (!err) {
  162. if (ret_type)
  163. new->type = *ret_type;
  164. new->subtree_max_end = new->end;
  165. memtype_rb_insert(&memtype_rbroot, new);
  166. }
  167. return err;
  168. }
  169. struct memtype *rbt_memtype_erase(u64 start, u64 end)
  170. {
  171. struct rb_node *deepest;
  172. struct memtype *data;
  173. data = memtype_rb_exact_match(&memtype_rbroot, start, end);
  174. if (!data)
  175. goto out;
  176. deepest = rb_augment_erase_begin(&data->rb);
  177. rb_erase(&data->rb, &memtype_rbroot);
  178. rb_augment_erase_end(deepest, memtype_rb_augment_cb, NULL);
  179. out:
  180. return data;
  181. }
  182. struct memtype *rbt_memtype_lookup(u64 addr)
  183. {
  184. struct memtype *data;
  185. data = memtype_rb_lowest_match(&memtype_rbroot, addr, addr + PAGE_SIZE);
  186. return data;
  187. }
  188. #if defined(CONFIG_DEBUG_FS)
  189. int rbt_memtype_copy_nth_element(struct memtype *out, loff_t pos)
  190. {
  191. struct rb_node *node;
  192. int i = 1;
  193. node = rb_first(&memtype_rbroot);
  194. while (node && pos != i) {
  195. node = rb_next(node);
  196. i++;
  197. }
  198. if (node) { /* pos == i */
  199. struct memtype *this = container_of(node, struct memtype, rb);
  200. *out = *this;
  201. return 0;
  202. } else {
  203. return 1;
  204. }
  205. }
  206. #endif