pat_rbtree.c 6.8 KB

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