Skip to content

Commit 7f0431e

Browse files
Toshi KaniIngo Molnar
authored andcommitted
x86/mm/mtrr: Fix MTRR lookup to handle an inclusive entry
When an MTRR entry is inclusive to a requested range, i.e. the start and end of the request are not within the MTRR entry range but the range contains the MTRR entry entirely: range_start ... [mtrr_start ... mtrr_end] ... range_end __mtrr_type_lookup() ignores such a case because both start_state and end_state are set to zero. This bug can cause the following issues: 1) reserve_memtype() tracks an effective memory type in case a request type is WB (ex. /dev/mem blindly uses WB). Missing to track with its effective type causes a subsequent request to map the same range with the effective type to fail. 2) pud_set_huge() and pmd_set_huge() check if a requested range has any overlap with MTRRs. Missing to detect an overlap may cause a performance penalty or undefined behavior. This patch fixes the bug by adding a new flag, 'inclusive', to detect the inclusive case. This case is then handled in the same way as end_state:1 since the first region is the same. With this fix, __mtrr_type_lookup() handles the inclusive case properly. Signed-off-by: Toshi Kani <toshi.kani@hp.com> Signed-off-by: Borislav Petkov <bp@suse.de> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: Elliott@hp.com Cc: H. Peter Anvin <hpa@zytor.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Luis R. Rodriguez <mcgrof@suse.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: dave.hansen@intel.com Cc: linux-mm <linux-mm@kvack.org> Cc: pebolle@tiscali.nl Link: http://lkml.kernel.org/r/1431714237-880-3-git-send-email-toshi.kani@hp.com Link: http://lkml.kernel.org/r/1432628901-18044-3-git-send-email-bp@alien8.de Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 10455f6 commit 7f0431e

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

arch/x86/kernel/cpu/mtrr/generic.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static u8 __mtrr_type_lookup(u64 start, u64 end, u64 *partial_end, int *repeat)
154154

155155
prev_match = 0xFF;
156156
for (i = 0; i < num_var_ranges; ++i) {
157-
unsigned short start_state, end_state;
157+
unsigned short start_state, end_state, inclusive;
158158

159159
if (!(mtrr_state.var_ranges[i].mask_lo & (1 << 11)))
160160
continue;
@@ -166,19 +166,27 @@ static u8 __mtrr_type_lookup(u64 start, u64 end, u64 *partial_end, int *repeat)
166166

167167
start_state = ((start & mask) == (base & mask));
168168
end_state = ((end & mask) == (base & mask));
169+
inclusive = ((start < base) && (end > base));
169170

170-
if (start_state != end_state) {
171+
if ((start_state != end_state) || inclusive) {
171172
/*
172173
* We have start:end spanning across an MTRR.
173-
* We split the region into
174-
* either
175-
* (start:mtrr_end) (mtrr_end:end)
176-
* or
177-
* (start:mtrr_start) (mtrr_start:end)
174+
* We split the region into either
175+
*
176+
* - start_state:1
177+
* (start:mtrr_end)(mtrr_end:end)
178+
* - end_state:1
179+
* (start:mtrr_start)(mtrr_start:end)
180+
* - inclusive:1
181+
* (start:mtrr_start)(mtrr_start:mtrr_end)(mtrr_end:end)
182+
*
178183
* depending on kind of overlap.
179-
* Return the type for first region and a pointer to
180-
* the start of second region so that caller will
181-
* lookup again on the second region.
184+
*
185+
* Return the type of the first region and a pointer
186+
* to the start of next region so that caller will be
187+
* advised to lookup again after having adjusted start
188+
* and end.
189+
*
182190
* Note: This way we handle multiple overlaps as well.
183191
*/
184192
if (start_state)

0 commit comments

Comments
 (0)