aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/mm/pat_rbtree.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/mm/pat_rbtree.c')
-rw-r--r--arch/x86/mm/pat_rbtree.c65
1 files changed, 46 insertions, 19 deletions
diff --git a/arch/x86/mm/pat_rbtree.c b/arch/x86/mm/pat_rbtree.c
index 8acaddd0fb2..7e1515bd477 100644
--- a/arch/x86/mm/pat_rbtree.c
+++ b/arch/x86/mm/pat_rbtree.c
@@ -54,29 +54,57 @@ static u64 get_subtree_max_end(struct rb_node *node)
54 return ret; 54 return ret;
55} 55}
56 56
57/* Update 'subtree_max_end' for a node, based on node and its children */ 57static u64 compute_subtree_max_end(struct memtype *data)
58static void memtype_rb_augment_cb(struct rb_node *node, void *__unused)
59{ 58{
60 struct memtype *data; 59 u64 max_end = data->end, child_max_end;
61 u64 max_end, child_max_end;
62
63 if (!node)
64 return;
65
66 data = container_of(node, struct memtype, rb);
67 max_end = data->end;
68 60
69 child_max_end = get_subtree_max_end(node->rb_right); 61 child_max_end = get_subtree_max_end(data->rb.rb_right);
70 if (child_max_end > max_end) 62 if (child_max_end > max_end)
71 max_end = child_max_end; 63 max_end = child_max_end;
72 64
73 child_max_end = get_subtree_max_end(node->rb_left); 65 child_max_end = get_subtree_max_end(data->rb.rb_left);
74 if (child_max_end > max_end) 66 if (child_max_end > max_end)
75 max_end = child_max_end; 67 max_end = child_max_end;
76 68
77 data->subtree_max_end = max_end; 69 return max_end;
70}
71
72/* Update 'subtree_max_end' for node and its parents */
73static void memtype_rb_propagate_cb(struct rb_node *node, struct rb_node *stop)
74{
75 while (node != stop) {
76 struct memtype *data = container_of(node, struct memtype, rb);
77 u64 subtree_max_end = compute_subtree_max_end(data);
78 if (data->subtree_max_end == subtree_max_end)
79 break;
80 data->subtree_max_end = subtree_max_end;
81 node = rb_parent(&data->rb);
82 }
83}
84
85static void memtype_rb_copy_cb(struct rb_node *old, struct rb_node *new)
86{
87 struct memtype *old_data = container_of(old, struct memtype, rb);
88 struct memtype *new_data = container_of(new, struct memtype, rb);
89
90 new_data->subtree_max_end = old_data->subtree_max_end;
78} 91}
79 92
93/* Update 'subtree_max_end' after tree rotation. old and new are the
94 * former and current subtree roots */
95static void memtype_rb_rotate_cb(struct rb_node *old, struct rb_node *new)
96{
97 struct memtype *old_data = container_of(old, struct memtype, rb);
98 struct memtype *new_data = container_of(new, struct memtype, rb);
99
100 new_data->subtree_max_end = old_data->subtree_max_end;
101 old_data->subtree_max_end = compute_subtree_max_end(old_data);
102}
103
104static const struct rb_augment_callbacks memtype_rb_augment_cb = {
105 memtype_rb_propagate_cb, memtype_rb_copy_cb, memtype_rb_rotate_cb
106};
107
80/* Find the first (lowest start addr) overlapping range from rb tree */ 108/* Find the first (lowest start addr) overlapping range from rb tree */
81static struct memtype *memtype_rb_lowest_match(struct rb_root *root, 109static struct memtype *memtype_rb_lowest_match(struct rb_root *root,
82 u64 start, u64 end) 110 u64 start, u64 end)
@@ -179,15 +207,17 @@ static void memtype_rb_insert(struct rb_root *root, struct memtype *newdata)
179 struct memtype *data = container_of(*node, struct memtype, rb); 207 struct memtype *data = container_of(*node, struct memtype, rb);
180 208
181 parent = *node; 209 parent = *node;
210 if (data->subtree_max_end < newdata->end)
211 data->subtree_max_end = newdata->end;
182 if (newdata->start <= data->start) 212 if (newdata->start <= data->start)
183 node = &((*node)->rb_left); 213 node = &((*node)->rb_left);
184 else if (newdata->start > data->start) 214 else if (newdata->start > data->start)
185 node = &((*node)->rb_right); 215 node = &((*node)->rb_right);
186 } 216 }
187 217
218 newdata->subtree_max_end = newdata->end;
188 rb_link_node(&newdata->rb, parent, node); 219 rb_link_node(&newdata->rb, parent, node);
189 rb_insert_color(&newdata->rb, root); 220 rb_insert_augmented(&newdata->rb, root, &memtype_rb_augment_cb);
190 rb_augment_insert(&newdata->rb, memtype_rb_augment_cb, NULL);
191} 221}
192 222
193int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type) 223int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)
@@ -209,16 +239,13 @@ int rbt_memtype_check_insert(struct memtype *new, unsigned long *ret_type)
209 239
210struct memtype *rbt_memtype_erase(u64 start, u64 end) 240struct memtype *rbt_memtype_erase(u64 start, u64 end)
211{ 241{
212 struct rb_node *deepest;
213 struct memtype *data; 242 struct memtype *data;
214 243
215 data = memtype_rb_exact_match(&memtype_rbroot, start, end); 244 data = memtype_rb_exact_match(&memtype_rbroot, start, end);
216 if (!data) 245 if (!data)
217 goto out; 246 goto out;
218 247
219 deepest = rb_augment_erase_begin(&data->rb); 248 rb_erase_augmented(&data->rb, &memtype_rbroot, &memtype_rb_augment_cb);
220 rb_erase(&data->rb, &memtype_rbroot);
221 rb_augment_erase_end(deepest, memtype_rb_augment_cb, NULL);
222out: 249out:
223 return data; 250 return data;
224} 251}