Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/mtrr: Don't let mtrr_type_lookup() return MTRR_TYPE_INVALID

mtrr_type_lookup() should always return a valid memory type. In case
there is no information available, it should return the default UC.

This will remove the last case where mtrr_type_lookup() can return
MTRR_TYPE_INVALID, so adjust the comment in include/uapi/asm/mtrr.h.

Note that removing the MTRR_TYPE_INVALID #define from that header
could break user code, so it has to stay.

At the same time the mtrr_type_lookup() stub for the !CONFIG_MTRR
case should set uniform to 1, as if the memory range would be
covered by no MTRR at all.

Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Juergen Gross <jgross@suse.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Tested-by: Michael Kelley <mikelley@microsoft.com>
Link: https://lore.kernel.org/r/20230502120931.20719-15-jgross@suse.com
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>

authored by

Juergen Gross and committed by
Borislav Petkov (AMD)
973df194 8227f40a

+10 -7
+5 -2
arch/x86/include/asm/mtrr.h
··· 86 86 static inline u8 mtrr_type_lookup(u64 addr, u64 end, u8 *uniform) 87 87 { 88 88 /* 89 - * Return no-MTRRs: 89 + * Return the default MTRR type, without any known other types in 90 + * that range. 90 91 */ 91 - return MTRR_TYPE_INVALID; 92 + *uniform = 1; 93 + 94 + return MTRR_TYPE_UNCACHABLE; 92 95 } 93 96 #define mtrr_save_fixed_ranges(arg) do {} while (0) 94 97 #define mtrr_save_state() do {} while (0)
+3 -3
arch/x86/include/uapi/asm/mtrr.h
··· 107 107 #define MTRR_NUM_TYPES 7 108 108 109 109 /* 110 - * Invalid MTRR memory type. mtrr_type_lookup() returns this value when 111 - * MTRRs are disabled. Note, this value is allocated from the reserved 112 - * values (0x7-0xff) of the MTRR memory types. 110 + * Invalid MTRR memory type. No longer used outside of MTRR code. 111 + * Note, this value is allocated from the reserved values (0x7-0xff) of 112 + * the MTRR memory types. 113 113 */ 114 114 #define MTRR_TYPE_INVALID 0xff 115 115
+2 -2
arch/x86/kernel/cpu/mtrr/generic.c
··· 508 508 if (!mtrr_state_set) { 509 509 /* Uniformity is unknown. */ 510 510 *uniform = 0; 511 - return MTRR_TYPE_INVALID; 511 + return MTRR_TYPE_UNCACHABLE; 512 512 } 513 513 514 514 *uniform = 1; 515 515 516 516 if (!(mtrr_state.enabled & MTRR_STATE_MTRR_ENABLED)) 517 - return MTRR_TYPE_INVALID; 517 + return MTRR_TYPE_UNCACHABLE; 518 518 519 519 for (i = 0; i < cache_map_n && start < end; i++) { 520 520 /* Region after current map entry? -> continue with next one. */