pat_rbtree.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Handle caching attributes in page tables (PAT)
  4. *
  5. * Authors: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  6. * Suresh B Siddha <suresh.b.siddha@intel.com>
  7. *
  8. * Interval tree (augmented rbtree) used to store the PAT memory type
  9. * reservations.
  10. */
  11. #include <linux/seq_file.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/kernel.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 = rb_entry(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 = rb_entry(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. enum {
  85. MEMTYPE_EXACT_MATCH = 0,
  86. MEMTYPE_END_MATCH = 1
  87. };
  88. static struct memtype *memtype_rb_match(struct rb_root *root,
  89. u64 start, u64 end, int match_type)
  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_type == MEMTYPE_EXACT_MATCH) &&
  96. (match->start == start) && (match->end == end))
  97. return match;
  98. if ((match_type == MEMTYPE_END_MATCH) &&
  99. (match->start < start) && (match->end == end))
  100. return match;
  101. node = rb_next(&match->rb);
  102. if (node)
  103. match = rb_entry(node, struct memtype, rb);
  104. else
  105. match = NULL;
  106. }
  107. return NULL; /* Returns NULL if there is no match */
  108. }
  109. static int memtype_rb_check_conflict(struct rb_root *root,
  110. u64 start, u64 end,
  111. enum page_cache_mode reqtype,
  112. enum page_cache_mode *newtype)
  113. {
  114. struct rb_node *node;
  115. struct memtype *match;
  116. enum page_cache_mode found_type = reqtype;
  117. match = memtype_rb_lowest_match(&memtype_rbroot, start, end);
  118. if (match == NULL)
  119. goto success;
  120. if (match->type != found_type && newtype == NULL)
  121. goto failure;
  122. dprintk("Overlap at 0x%Lx-0x%Lx\n", match->start, match->end);
  123. found_type = match->type;
  124. node = rb_next(&match->rb);
  125. while (node) {
  126. match = rb_entry(node, struct memtype, rb);
  127. if (match->start >= end) /* Checked all possible matches */
  128. goto success;
  129. if (is_node_overlap(match, start, end) &&
  130. match->type != found_type) {
  131. goto failure;
  132. }
  133. node = rb_next(&match->rb);
  134. }
  135. success:
  136. if (newtype)
  137. *newtype = found_type;
  138. return 0;
  139. failure:
  140. pr_info("x86/PAT: %s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
  141. current->comm, current->pid, start, end,
  142. cattr_name(found_type), cattr_name(match->type));
  143. return -EBUSY;
  144. }
  145. static void memtype_rb_insert(struct rb_root *root, struct memtype *newdata)
  146. {
  147. struct rb_node **node = &(root->rb_node);
  148. struct rb_node *parent = NULL;
  149. while (*node) {
  150. struct memtype *data = rb_entry(*node, struct memtype, rb);
  151. parent = *node;
  152. if (data->subtree_max_end < newdata->end)
  153. data->subtree_max_end = newdata->end;
  154. if (newdata->start <= data->start)
  155. node = &((*node)->rb_left);
  156. else if (newdata->start > data->start)
  157. node = &((*node)->rb_right);
  158. }
  159. newdata->subtree_max_end = newdata->end;
  160. rb_link_node(&newdata->rb, parent, node);
  161. rb_insert_augmented(&newdata->rb, root, &memtype_rb_augment_cb);
  162. }
  163. int rbt_memtype_check_insert(struct memtype *new,
  164. enum page_cache_mode *ret_type)
  165. {
  166. int err = 0;
  167. err = memtype_rb_check_conflict(&memtype_rbroot, new->start, new->end,
  168. new->type, ret_type);
  169. if (!err) {
  170. if (ret_type)
  171. new->type = *ret_type;
  172. new->subtree_max_end = new->end;
  173. memtype_rb_insert(&memtype_rbroot, new);
  174. }
  175. return err;
  176. }
  177. struct memtype *rbt_memtype_erase(u64 start, u64 end)
  178. {
  179. struct memtype *data;
  180. /*
  181. * Since the memtype_rbroot tree allows overlapping ranges,
  182. * rbt_memtype_erase() checks with EXACT_MATCH first, i.e. free
  183. * a whole node for the munmap case. If no such entry is found,
  184. * it then checks with END_MATCH, i.e. shrink the size of a node
  185. * from the end for the mremap case.
  186. */
  187. data = memtype_rb_match(&memtype_rbroot, start, end,
  188. MEMTYPE_EXACT_MATCH);
  189. if (!data) {
  190. data = memtype_rb_match(&memtype_rbroot, start, end,
  191. MEMTYPE_END_MATCH);
  192. if (!data)
  193. return ERR_PTR(-EINVAL);
  194. }
  195. if (data->start == start) {
  196. /* munmap: erase this node */
  197. rb_erase_augmented(&data->rb, &memtype_rbroot,
  198. &memtype_rb_augment_cb);
  199. } else {
  200. /* mremap: update the end value of this node */
  201. rb_erase_augmented(&data->rb, &memtype_rbroot,
  202. &memtype_rb_augment_cb);
  203. data->end = start;
  204. data->subtree_max_end = data->end;
  205. memtype_rb_insert(&memtype_rbroot, data);
  206. return NULL;
  207. }
  208. return data;
  209. }
  210. struct memtype *rbt_memtype_lookup(u64 addr)
  211. {
  212. return memtype_rb_lowest_match(&memtype_rbroot, addr, addr + PAGE_SIZE);
  213. }
  214. #if defined(CONFIG_DEBUG_FS)
  215. int rbt_memtype_copy_nth_element(struct memtype *out, loff_t pos)
  216. {
  217. struct rb_node *node;
  218. int i = 1;
  219. node = rb_first(&memtype_rbroot);
  220. while (node && pos != i) {
  221. node = rb_next(node);
  222. i++;
  223. }
  224. if (node) { /* pos == i */
  225. struct memtype *this = rb_entry(node, struct memtype, rb);
  226. *out = *this;
  227. return 0;
  228. } else {
  229. return 1;
  230. }
  231. }
  232. #endif