pat_rbtree.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_augmented.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. static u64 compute_subtree_max_end(struct memtype *data)
  49. {
  50. u64 max_end = data->end, child_max_end;
  51. child_max_end = get_subtree_max_end(data->rb.rb_right);
  52. if (child_max_end > max_end)
  53. max_end = child_max_end;
  54. child_max_end = get_subtree_max_end(data->rb.rb_left);
  55. if (child_max_end > max_end)
  56. max_end = child_max_end;
  57. return max_end;
  58. }
  59. RB_DECLARE_CALLBACKS(static, memtype_rb_augment_cb, struct memtype, rb,
  60. u64, subtree_max_end, compute_subtree_max_end)
  61. /* Find the first (lowest start addr) overlapping range from rb tree */
  62. static struct memtype *memtype_rb_lowest_match(struct rb_root *root,
  63. u64 start, u64 end)
  64. {
  65. struct rb_node *node = root->rb_node;
  66. struct memtype *last_lower = NULL;
  67. while (node) {
  68. struct memtype *data = container_of(node, struct memtype, rb);
  69. if (get_subtree_max_end(node->rb_left) > start) {
  70. /* Lowest overlap if any must be on left side */
  71. node = node->rb_left;
  72. } else if (is_node_overlap(data, start, end)) {
  73. last_lower = data;
  74. break;
  75. } else if (start >= data->start) {
  76. /* Lowest overlap if any must be on right side */
  77. node = node->rb_right;
  78. } else {
  79. break;
  80. }
  81. }
  82. return last_lower; /* Returns NULL if there is no overlap */
  83. }
  84. static struct memtype *memtype_rb_exact_match(struct rb_root *root,
  85. u64 start, u64 end)
  86. {
  87. struct memtype *match;
  88. match = memtype_rb_lowest_match(root, start, end);
  89. while (match != NULL && match->start < end) {
  90. struct rb_node *node;
  91. if (match->start == start && match->end == end)
  92. return match;
  93. node = rb_next(&match->rb);
  94. if (node)
  95. match = container_of(node, struct memtype, rb);
  96. else
  97. match = NULL;
  98. }
  99. return NULL; /* Returns NULL if there is no exact match */
  100. }
  101. static int memtype_rb_check_conflict(struct rb_root *root,
  102. u64 start, u64 end,
  103. enum page_cache_mode reqtype,
  104. enum page_cache_mode *newtype)
  105. {
  106. struct rb_node *node;
  107. struct memtype *match;
  108. enum page_cache_mode found_type = reqtype;
  109. match = memtype_rb_lowest_match(&memtype_rbroot, start, end);
  110. if (match == NULL)
  111. goto success;
  112. if (match->type != found_type && newtype == NULL)
  113. goto failure;
  114. dprintk("Overlap at 0x%Lx-0x%Lx\n", match->start, match->end);
  115. found_type = match->type;
  116. node = rb_next(&match->rb);
  117. while (node) {
  118. match = container_of(node, struct memtype, rb);
  119. if (match->start >= end) /* Checked all possible matches */
  120. goto success;
  121. if (is_node_overlap(match, start, end) &&
  122. match->type != found_type) {
  123. goto failure;
  124. }
  125. node = rb_next(&match->rb);
  126. }
  127. success:
  128. if (newtype)
  129. *newtype = found_type;
  130. return 0;
  131. failure:
  132. pr_info("x86/PAT: %s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
  133. current->comm, current->pid, start, end,
  134. cattr_name(found_type), cattr_name(match->type));
  135. return -EBUSY;
  136. }
  137. static void memtype_rb_insert(struct rb_root *root, struct memtype *newdata)
  138. {
  139. struct rb_node **node = &(root->rb_node);
  140. struct rb_node *parent = NULL;
  141. while (*node) {
  142. struct memtype *data = container_of(*node, struct memtype, rb);
  143. parent = *node;
  144. if (data->subtree_max_end < newdata->end)
  145. data->subtree_max_end = newdata->end;
  146. if (newdata->start <= data->start)
  147. node = &((*node)->rb_left);
  148. else if (newdata->start > data->start)
  149. node = &((*node)->rb_right);
  150. }
  151. newdata->subtree_max_end = newdata->end;
  152. rb_link_node(&newdata->rb, parent, node);
  153. rb_insert_augmented(&newdata->rb, root, &memtype_rb_augment_cb);
  154. }
  155. int rbt_memtype_check_insert(struct memtype *new,
  156. enum page_cache_mode *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 memtype *data;
  172. data = memtype_rb_exact_match(&memtype_rbroot, start, end);
  173. if (!data)
  174. goto out;
  175. rb_erase_augmented(&data->rb, &memtype_rbroot, &memtype_rb_augment_cb);
  176. out:
  177. return data;
  178. }
  179. struct memtype *rbt_memtype_lookup(u64 addr)
  180. {
  181. struct memtype *data;
  182. data = memtype_rb_lowest_match(&memtype_rbroot, addr, addr + PAGE_SIZE);
  183. return data;
  184. }
  185. #if defined(CONFIG_DEBUG_FS)
  186. int rbt_memtype_copy_nth_element(struct memtype *out, loff_t pos)
  187. {
  188. struct rb_node *node;
  189. int i = 1;
  190. node = rb_first(&memtype_rbroot);
  191. while (node && pos != i) {
  192. node = rb_next(node);
  193. i++;
  194. }
  195. if (node) { /* pos == i */
  196. struct memtype *this = container_of(node, struct memtype, rb);
  197. *out = *this;
  198. return 0;
  199. } else {
  200. return 1;
  201. }
  202. }
  203. #endif