Skip to content

Commit 293d865

Browse files
ardbiesheuvelctmarinas
authored andcommitted
arm64: mm: Make kaslr_requires_kpti() a static inline
In preparation for moving the first assignment of arm64_use_ng_mappings to an earlier stage in the boot, ensure that kaslr_requires_kpti() is accessible without relying on the core kernel's view on whether or not KASLR is enabled. So make it a static inline, and move the kaslr_enabled() check out of it and into the callers, one of which will disappear in a subsequent patch. Once/when support for the obsolete ThunderX 1 platform is dropped, this check reduces to a E0PD feature check on the local CPU. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20240214122845.2033971-61-ardb+git@google.com Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent aa6a52b commit 293d865

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

arch/arm64/include/asm/mmu.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,43 @@ extern void create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
7171
pgprot_t prot, bool page_mappings_only);
7272
extern void *fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot);
7373
extern void mark_linear_text_alias_ro(void);
74-
extern bool kaslr_requires_kpti(void);
74+
75+
/*
76+
* This check is triggered during the early boot before the cpufeature
77+
* is initialised. Checking the status on the local CPU allows the boot
78+
* CPU to detect the need for non-global mappings and thus avoiding a
79+
* pagetable re-write after all the CPUs are booted. This check will be
80+
* anyway run on individual CPUs, allowing us to get the consistent
81+
* state once the SMP CPUs are up and thus make the switch to non-global
82+
* mappings if required.
83+
*/
84+
static inline bool kaslr_requires_kpti(void)
85+
{
86+
/*
87+
* E0PD does a similar job to KPTI so can be used instead
88+
* where available.
89+
*/
90+
if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
91+
u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
92+
if (cpuid_feature_extract_unsigned_field(mmfr2,
93+
ID_AA64MMFR2_EL1_E0PD_SHIFT))
94+
return false;
95+
}
96+
97+
/*
98+
* Systems affected by Cavium erratum 24756 are incompatible
99+
* with KPTI.
100+
*/
101+
if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
102+
extern const struct midr_range cavium_erratum_27456_cpus[];
103+
104+
if (is_midr_in_range_list(read_cpuid_id(),
105+
cavium_erratum_27456_cpus))
106+
return false;
107+
}
108+
109+
return true;
110+
}
75111

76112
#define INIT_MM_CONTEXT(name) \
77113
.pgd = init_pg_dir,

arch/arm64/kernel/cpufeature.c

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,46 +1620,6 @@ has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
16201620
return has_cpuid_feature(entry, scope);
16211621
}
16221622

1623-
/*
1624-
* This check is triggered during the early boot before the cpufeature
1625-
* is initialised. Checking the status on the local CPU allows the boot
1626-
* CPU to detect the need for non-global mappings and thus avoiding a
1627-
* pagetable re-write after all the CPUs are booted. This check will be
1628-
* anyway run on individual CPUs, allowing us to get the consistent
1629-
* state once the SMP CPUs are up and thus make the switch to non-global
1630-
* mappings if required.
1631-
*/
1632-
bool kaslr_requires_kpti(void)
1633-
{
1634-
if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
1635-
return false;
1636-
1637-
/*
1638-
* E0PD does a similar job to KPTI so can be used instead
1639-
* where available.
1640-
*/
1641-
if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
1642-
u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
1643-
if (cpuid_feature_extract_unsigned_field(mmfr2,
1644-
ID_AA64MMFR2_EL1_E0PD_SHIFT))
1645-
return false;
1646-
}
1647-
1648-
/*
1649-
* Systems affected by Cavium erratum 24756 are incompatible
1650-
* with KPTI.
1651-
*/
1652-
if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
1653-
extern const struct midr_range cavium_erratum_27456_cpus[];
1654-
1655-
if (is_midr_in_range_list(read_cpuid_id(),
1656-
cavium_erratum_27456_cpus))
1657-
return false;
1658-
}
1659-
1660-
return kaslr_enabled();
1661-
}
1662-
16631623
static bool __meltdown_safe = true;
16641624
static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
16651625

@@ -1712,7 +1672,7 @@ static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
17121672
}
17131673

17141674
/* Useful for KASLR robustness */
1715-
if (kaslr_requires_kpti()) {
1675+
if (kaslr_enabled() && kaslr_requires_kpti()) {
17161676
if (!__kpti_forced) {
17171677
str = "KASLR";
17181678
__kpti_forced = 1;

arch/arm64/kernel/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
288288
* mappings from the start, avoiding the cost of rewriting
289289
* everything later.
290290
*/
291-
arm64_use_ng_mappings = kaslr_requires_kpti();
291+
arm64_use_ng_mappings = kaslr_enabled() && kaslr_requires_kpti();
292292

293293
early_fixmap_init();
294294
early_ioremap_init();

0 commit comments

Comments
 (0)