Skip to contentSkip to conversation

x86/mtrr: Correct the returned MTRR type of mtrr_type_lookup.

PATCH Applied Ying-Tsun Huang 2 participants 2 messages Dec 15, 2020 – Jan 6, 2021 x86

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/mtrr: Correct the returned MTRR type of mtrr_type_lookup.

In mtrr_type_lookup, if the input memory address region is not in the MTRR, over 4GB, and not over the top of memory, write-back attribute is returned. These condition checks are for ensuring the input memory address region is mapped to the physical memory actually.

However, if the end address is just aligned with the top of memory, the condition check treats the address is over the top of memory, and write-back attribute is not returned.

There is a real case of NVDIMM. The nd_pmem module tries to map NVDIMMs as cacheable memories when NVDIMMs are connected. If a NVDIMM is the last of the DIMMs, the performance of this NVDIMM becomes very low since it aligned with the top of memory and its memory type is uncached-minus.

The input end address should be changed to inclusive to be checked for the top of memory.

Fixes 0cc705f56e40 ("x86/mm/mtrr: Clean up mtrr_type_lookup()")
---
 arch/x86/kernel/cpu/mtrr/generic.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/mtrr/generic.c +3 -3
index 23ad8e953dfb..a29997e6cf9e 100644
@@ -167,9 +167,6 @@ static u8 mtrr_type_lookup_variable(u64 start, u64 end, u64 *partial_end,
 	*repeat = 0;
 	*uniform = 1;
 
-	/* Make end inclusive instead of exclusive */
-	end--;
-
 	prev_match = MTRR_TYPE_INVALID;
 	for (i = 0; i < num_var_ranges; ++i) {
 		unsigned short start_state, end_state, inclusive;
@@ -261,6 +258,9 @@ u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform)
 	int repeat;
 	u64 partial_end;
 
+	/* Make end inclusive instead of exclusive */
+	end--;
+
 	if (!mtrr_state_set)
 		return MTRR_TYPE_INVALID;
 
Signature · 1 line
2.25.1
#1 lore raw

[tip: x86/urgent] x86/mtrr: Correct the range check before performing MTRR type lookups

The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     cb7f4a8b1fb426a175d1708f05581939c61329d4
Gitweb:        https://git.kernel.org/tip/cb7f4a8b1fb426a175d1708f05581939c61329d4
Author:        Ying-Tsun Huang <[email protected]>
AuthorDate:    Tue, 15 Dec 2020 15:07:20 +08:00
Committer:     Borislav Petkov <[email protected]>
CommitterDate: Wed, 06 Jan 2021 13:01:13 +01:00

x86/mtrr: Correct the range check before performing MTRR type lookups

In mtrr_type_lookup(), if the input memory address region is not in the MTRR, over 4GB, and not over the top of memory, a write-back attribute is returned. These condition checks are for ensuring the input memory address region is actually mapped to the physical memory.

However, if the end address is just aligned with the top of memory, the condition check treats the address is over the top of memory, and write-back attribute is not returned.

And this hits in a real use case with NVDIMM: the nd_pmem module tries to map NVDIMMs as cacheable memories when NVDIMMs are connected. If a NVDIMM is the last of the DIMMs, the performance of this NVDIMM becomes very low since it is aligned with the top of memory and its memory type is uncached-minus.

Move the input end address change to inclusive up into mtrr_type_lookup(), before checking for the top of memory in either mtrr_type_lookup_{variable,fixed}() helpers.

[ bp: Massage commit message. ]

Fixes 0cc705f56e40 ("x86/mm/mtrr: Clean up mtrr_type_lookup()")
---
 arch/x86/kernel/cpu/mtrr/generic.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/mtrr/generic.c +3 -3
index 23ad8e9..a29997e 100644
@@ -167,9 +167,6 @@ static u8 mtrr_type_lookup_variable(u64 start, u64 end, u64 *partial_end,
 	*repeat = 0;
 	*uniform = 1;
 
-	/* Make end inclusive instead of exclusive */
-	end--;
-
 	prev_match = MTRR_TYPE_INVALID;
 	for (i = 0; i < num_var_ranges; ++i) {
 		unsigned short start_state, end_state, inclusive;
@@ -261,6 +258,9 @@ u8 mtrr_type_lookup(u64 start, u64 end, u8 *uniform)
 	int repeat;
 	u64 partial_end;
 
+	/* Make end inclusive instead of exclusive */
+	end--;
+
 	if (!mtrr_state_set)
 		return MTRR_TYPE_INVALID;