Linux Audio

Check our new training course

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