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 used to store the PAT memory type reservations.
9 */
10
11 #include <linux/seq_file.h>
12 #include <linux/debugfs.h>
13 #include <linux/kernel.h>
14 #include <linux/interval_tree_generic.h>
15 #include <linux/sched.h>
16 #include <linux/gfp.h>
17 #include <linux/pgtable.h>
18
19 #include <asm/memtype.h>
20
21 #include "memtype.h"
22
23 /*
24 * The memtype tree keeps track of memory type for specific
25 * physical memory areas. Without proper tracking, conflicting memory
26 * types in different mappings can cause CPU cache corruption.
27 *
28 * The tree is an interval tree (augmented rbtree) which tree is ordered
29 * by the starting address. The tree can contain multiple entries for
30 * different regions which overlap. All the aliases have the same
31 * cache attributes of course, as enforced by the PAT logic.
32 *
33 * memtype_lock protects the rbtree.
34 */
35
interval_start(struct memtype * entry)36 static inline u64 interval_start(struct memtype *entry)
37 {
38 return entry->start;
39 }
40
interval_end(struct memtype * entry)41 static inline u64 interval_end(struct memtype *entry)
42 {
43 return entry->end - 1;
44 }
45
46 INTERVAL_TREE_DEFINE(struct memtype, rb, u64, subtree_max_end,
47 interval_start, interval_end,
48 static, interval)
49
50 static struct rb_root_cached memtype_rbroot = RB_ROOT_CACHED;
51
memtype_check_conflict(u64 start,u64 end,enum page_cache_mode reqtype,enum page_cache_mode * newtype)52 static int memtype_check_conflict(u64 start, u64 end,
53 enum page_cache_mode reqtype,
54 enum page_cache_mode *newtype)
55 {
56 struct memtype *entry_match;
57 enum page_cache_mode found_type = reqtype;
58
59 entry_match = interval_iter_first(&memtype_rbroot, start, end-1);
60 if (entry_match == NULL)
61 goto success;
62
63 if (entry_match->type != found_type && newtype == NULL)
64 goto failure;
65
66 dprintk("Overlap at 0x%Lx-0x%Lx\n", entry_match->start, entry_match->end);
67 found_type = entry_match->type;
68
69 entry_match = interval_iter_next(entry_match, start, end-1);
70 while (entry_match) {
71 if (entry_match->type != found_type)
72 goto failure;
73
74 entry_match = interval_iter_next(entry_match, start, end-1);
75 }
76 success:
77 if (newtype)
78 *newtype = found_type;
79
80 return 0;
81
82 failure:
83 pr_info("x86/PAT: %s:%d conflicting memory types %Lx-%Lx %s<->%s\n",
84 current->comm, current->pid, start, end,
85 cattr_name(found_type), cattr_name(entry_match->type));
86
87 return -EBUSY;
88 }
89
memtype_check_insert(struct memtype * entry_new,enum page_cache_mode * ret_type)90 int memtype_check_insert(struct memtype *entry_new, enum page_cache_mode *ret_type)
91 {
92 int err = 0;
93
94 err = memtype_check_conflict(entry_new->start, entry_new->end, entry_new->type, ret_type);
95 if (err)
96 return err;
97
98 if (ret_type)
99 entry_new->type = *ret_type;
100
101 interval_insert(entry_new, &memtype_rbroot);
102 return 0;
103 }
104
memtype_erase(u64 start,u64 end)105 struct memtype *memtype_erase(u64 start, u64 end)
106 {
107 struct memtype *entry = interval_iter_first(&memtype_rbroot, start, end - 1);
108
109 while (entry && entry->start < end) {
110 if (entry->start == start && entry->end == end) {
111 interval_remove(entry, &memtype_rbroot);
112 return entry;
113 }
114 entry = interval_iter_next(entry, start, end - 1);
115 }
116 return ERR_PTR(-EINVAL);
117 }
118
memtype_lookup(u64 addr)119 struct memtype *memtype_lookup(u64 addr)
120 {
121 return interval_iter_first(&memtype_rbroot, addr, addr + PAGE_SIZE-1);
122 }
123
124 /*
125 * Debugging helper, copy the Nth entry of the tree into a
126 * a copy for printout. This allows us to print out the tree
127 * via debugfs, without holding the memtype_lock too long:
128 */
129 #ifdef CONFIG_DEBUG_FS
memtype_copy_nth_element(struct memtype * entry_out,loff_t pos)130 int memtype_copy_nth_element(struct memtype *entry_out, loff_t pos)
131 {
132 struct memtype *entry_match;
133 int i = 1;
134
135 entry_match = interval_iter_first(&memtype_rbroot, 0, ULONG_MAX);
136
137 while (entry_match && pos != i) {
138 entry_match = interval_iter_next(entry_match, 0, ULONG_MAX);
139 i++;
140 }
141
142 if (entry_match) { /* pos == i */
143 *entry_out = *entry_match;
144 return 0;
145 } else {
146 return 1;
147 }
148 }
149 #endif
150