Skip to contentSkip to conversation

fixes for data/bss linear alias unmap series

PATCH Superseded Ard Biesheuvel 3 participants 7 messages Jun 3 – Jun 4

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fixes for data/bss linear alias unmap series

From: Ard Biesheuvel <[email protected]>

Fixes for the data/bss linear alias unmap series:

- Fix KASAN related issue reported by Mark, by moving all KASAN page tables out of BSS [on arm64], not just the ones defined under arch/arm64

- Fix two issues spotted by Sashiko

- Fix the CKI reported WARN() splat on BBML2_NOABORT systems that try to split block mappings too early.

Cc Kevin Brodsky <[email protected]>
Cc Mark Brown <[email protected]>

Ard Biesheuvel (4):
arm64: Move generic KASAN page tables out of BSS too
arm64: Avoid double evaluation of __ptep_get()
arm64: mte: Disregard the zero page explicitly for manipulating tags
arm64: mm: Defer remap of linear alias of data/bss

 arch/arm64/include/asm/linkage.h |  2 ++
 arch/arm64/include/asm/mmu.h     |  2 --
 arch/arm64/include/asm/mte.h     |  5 +++++
 arch/arm64/include/asm/pgtable.h |  4 ----
 arch/arm64/kernel/vmlinux.lds.S  |  8 ++++----
 arch/arm64/mm/fixmap.c           |  6 +++---
 arch/arm64/mm/kasan_init.c       |  2 +-
 arch/arm64/mm/mmu.c              | 20 +++++++++++++-------
 include/linux/linkage.h          |  4 ++++
 mm/kasan/init.c                  | 10 +++++-----
 10 files changed, 37 insertions(+), 26 deletions(-)

base-commit: 63e0b6a5b6934d6a919d1c65ea185303200a1874

Signature · 1 line
2.54.0.1013.g208068f2d8-goog
#1 lore raw

PATCH 1/4 arm64: Move generic KASAN page tables out of BSS too

From: Ard Biesheuvel <[email protected]>

Make sure that all KASAN page tables are emitted into the .pgdir section (provided that the arch has one - otherwise, fall back to page aligned BSS)

This is needed because BSS itself is no longer accessible via the linear map.

---
 arch/arm64/include/asm/linkage.h |  2 ++
 arch/arm64/include/asm/mmu.h     |  2 --
 arch/arm64/kernel/vmlinux.lds.S  |  8 ++++----
 arch/arm64/mm/fixmap.c           |  6 +++---
 arch/arm64/mm/kasan_init.c       |  2 +-
 include/linux/linkage.h          |  4 ++++
 mm/kasan/init.c                  | 10 +++++-----
 7 files changed, 19 insertions(+), 15 deletions(-)
arch/arm64/include/asm/linkage.h +2 -0
index 40bd17add539..8637f667667c 100644
@@ -43,4 +43,6 @@
 	SYM_TYPED_START(name, SYM_L_GLOBAL, SYM_A_ALIGN)	\
 	bti c ;
 
+#define __bss_pgtbl __section(".bss..pgtbl") __aligned(PAGE_SIZE)
+
 #endif
arch/arm64/include/asm/mmu.h +0 -2
index fb95754f2876..5e1211c540ab 100644
@@ -13,8 +13,6 @@
 
 #ifndef __ASSEMBLER__
 
-#define __pgtbl_bss __section(".pgdir.bss") __aligned(PAGE_SIZE)
-
 #include <linux/refcount.h>
 #include <asm/cpufeature.h>
 
arch/arm64/kernel/vmlinux.lds.S +4 -4
index 2b0ebfb30c63..d3ed59abab38 100644
@@ -352,11 +352,11 @@ SECTIONS
 	BSS_SECTION(SBSS_ALIGN, 0, PAGE_SIZE)
 	__pi___bss_start = __bss_start;
 
-	/* fixmap BSS starts here - preceding data/BSS is omitted from the linear map */
-	.pgdir.bss (NOLOAD) : ALIGN(PAGE_SIZE) {
-		*(.pgdir.bss)
+	/* page table BSS starts here - preceding data/BSS is omitted from the linear map */
+	.pgtbl : ALIGN(PAGE_SIZE) {
+		*(.bss..pgtbl)
 	}
-	ASSERT(ADDR(.pgdir.bss) == __bss_stop, ".pgdir.bss must follow BSS")
+	ASSERT(ADDR(.pgtbl) == __bss_stop, ".pgtbl must follow BSS")
 
 	. = ALIGN(PAGE_SIZE);
 	__pi_init_pg_dir = .;
arch/arm64/mm/fixmap.c +3 -3
index 1a3bbd67dd76..f66a0016dd02 100644
@@ -31,9 +31,9 @@ static_assert(NR_BM_PMD_TABLES == 1);
 
 #define BM_PTE_TABLE_IDX(addr)	__BM_TABLE_IDX(addr, PMD_SHIFT)
 
-static pte_t bm_pte[NR_BM_PTE_TABLES][PTRS_PER_PTE] __pgtbl_bss;
-static pmd_t bm_pmd[PTRS_PER_PMD] __pgtbl_bss __maybe_unused;
-static pud_t bm_pud[PTRS_PER_PUD] __pgtbl_bss __maybe_unused;
+static pte_t bm_pte[NR_BM_PTE_TABLES][PTRS_PER_PTE] __bss_pgtbl;
+static pmd_t bm_pmd[PTRS_PER_PMD] __bss_pgtbl __maybe_unused;
+static pud_t bm_pud[PTRS_PER_PUD] __bss_pgtbl __maybe_unused;
 
 static inline pte_t *fixmap_pte(unsigned long addr)
 {
arch/arm64/mm/kasan_init.c +1 -1
index dbf22cae82ee..3fcad956fdf7 100644
@@ -214,7 +214,7 @@ asmlinkage void __init kasan_early_init(void)
 		 * shadow pud_t[]/p4d_t[], which could end up getting corrupted
 		 * when the linear region is mapped.
 		 */
-		static pte_t tbl[PTRS_PER_PTE] __pgtbl_bss;
+		static pte_t tbl[PTRS_PER_PTE] __bss_pgtbl;
 		pgd_t *pgdp = pgd_offset_k(KASAN_SHADOW_START);
 
 		set_pgd(pgdp, __pgd(__pa_symbol(tbl) | PGD_TYPE_TABLE));
include/linux/linkage.h +4 -0
index b11660b706c5..53fe1f48fd28 100644
@@ -39,6 +39,10 @@
 #define __page_aligned_data	__section(".data..page_aligned") __aligned(PAGE_SIZE)
 #define __page_aligned_bss	__section(".bss..page_aligned") __aligned(PAGE_SIZE)
 
+#ifndef __bss_pgtbl
+#define __bss_pgtbl		__page_aligned_bss
+#endif
+
 /*
  * For assembly routines.
  *
mm/kasan/init.c +5 -5
index 9c880f607c6a..66a883887987 100644
@@ -26,10 +26,10 @@
  *   - Latter it reused it as zero shadow to cover large ranges of memory
  *     that allowed to access, but not handled by kasan (vmalloc/vmemmap ...).
  */
-unsigned char kasan_early_shadow_page[PAGE_SIZE] __page_aligned_bss;
+unsigned char kasan_early_shadow_page[PAGE_SIZE] __bss_pgtbl;
 
 #if CONFIG_PGTABLE_LEVELS > 4
-p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D] __page_aligned_bss;
+p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D] __bss_pgtbl;
 static inline bool kasan_p4d_table(pgd_t pgd)
 {
 	return pgd_page(pgd) == virt_to_page(lm_alias(kasan_early_shadow_p4d));
@@ -41,7 +41,7 @@ static inline bool kasan_p4d_table(pgd_t pgd)
 }
 #endif
 #if CONFIG_PGTABLE_LEVELS > 3
-pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __page_aligned_bss;
+pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD] __bss_pgtbl;
 static inline bool kasan_pud_table(p4d_t p4d)
 {
 	return p4d_page(p4d) == virt_to_page(lm_alias(kasan_early_shadow_pud));
@@ -53,7 +53,7 @@ static inline bool kasan_pud_table(p4d_t p4d)
 }
 #endif
 #if CONFIG_PGTABLE_LEVELS > 2
-pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __page_aligned_bss;
+pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD] __bss_pgtbl;
 static inline bool kasan_pmd_table(pud_t pud)
 {
 	return pud_page(pud) == virt_to_page(lm_alias(kasan_early_shadow_pmd));
@@ -65,7 +65,7 @@ static inline bool kasan_pmd_table(pud_t pud)
 }
 #endif
 pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]
-	__page_aligned_bss;
+	__bss_pgtbl;
 
 static inline bool kasan_pte_table(pmd_t pmd)
 {
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
#2 lore raw

PATCH 2/4 arm64: Avoid double evaluation of __ptep_get()

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Sashiko warns that the new pte_valid_noncont() macro is used in a manner where the argument (which performs a READ_ONCE() of the descriptor) is evaluated twice.

Drop the macro that we just added, and move the check into the newly added users.

---
 arch/arm64/include/asm/pgtable.h |  4 ----
 arch/arm64/mm/mmu.c              | 14 ++++++++++----
 2 files changed, 10 insertions(+), 8 deletions(-)
arch/arm64/include/asm/pgtable.h +0 -4
index 491ba0a6492d..c9e4e00a9af2 100644
@@ -181,10 +181,6 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys)
  * Returns true if the pte is valid and has the contiguous bit set.
  */
 #define pte_valid_cont(pte)	(pte_valid(pte) && pte_cont(pte))
-/*
- * Returns true if the pte is valid and has the contiguous bit cleared.
- */
-#define pte_valid_noncont(pte)	(pte_valid(pte) && !pte_cont(pte))
 /*
  * Could the pte be present in the TLB? We must check mm_tlb_flush_pending
  * so that we don't erroneously return false for pages that have been
arch/arm64/mm/mmu.c +10 -4
index be51f6cac86f..d68e691c093a 100644
@@ -186,9 +186,12 @@ static void init_pte(pte_t *ptep, unsigned long addr, unsigned long end,
 
 static bool pte_range_has_valid_noncont(pte_t *ptep)
 {
-	for (int i = 0; i < CONT_PTES; i++)
-		if (pte_valid_noncont(__ptep_get(&ptep[i])))
+	for (int i = 0; i < CONT_PTES; i++) {
+		pte_t pte = __ptep_get(&ptep[i]);
+
+		if (pte_valid(pte) && !pte_cont(pte))
 			return true;
+	}
 	return false;
 }
 
@@ -291,9 +294,12 @@ static int init_pmd(pmd_t *pmdp, unsigned long addr, unsigned long end,
 
 static bool pmd_range_has_valid_noncont(pmd_t *pmdp)
 {
-	for (int i = 0; i < CONT_PMDS; i++)
-		if (pte_valid_noncont(pmd_pte(READ_ONCE(pmdp[i]))))
+	for (int i = 0; i < CONT_PMDS; i++) {
+		pte_t pte = pmd_pte(READ_ONCE(pmdp[i]));
+
+		if (pte_valid(pte) && !pte_cont(pte))
 			return true;
+	}
 	return false;
 }
 
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
#3 lore raw

PATCH 3/4 arm64: mte: Disregard the zero page explicitly for manipulating tags

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The zero page is conceptually immutable, and will be moved into .rodata to prevent inadvertent corruption.

Prepare the MTE code for this, by ensuring that the zero page is never taken into account for tag manipulation, given that those actions will no longer be permitted on the read-only alias of .rodata in the linear map.

---
 arch/arm64/include/asm/mte.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/include/asm/mte.h +5 -0
index 7f7b97e09996..093b34944aee 100644
@@ -80,6 +80,11 @@ static inline bool page_mte_tagged(struct page *page)
  */
 static inline bool try_page_mte_tagging(struct page *page)
 {
+	extern struct page *__zero_page;
+
+	if (page == __zero_page)
+		return false;
+
 	VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page)));
 
 	if (!test_and_set_bit(PG_mte_lock, &page->flags.f))
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
#6 lore raw

PATCH 4/4 arm64: mm: Defer remap of linear alias of data/bss

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Marking the linear alias of data/bss invalid involves calling set_memory_valid(), which calls split_kernel_leaf_mapping() under the hood.

On BBML2_NOABORT capable systems, this may result in the need to allocate page tables at a time when the generic memory allocation APIs are not yet available, resulting in a splat like

   WARNING: arch/arm64/mm/mmu.c:821 at split_kernel_leaf_mapping+0x15c/0x170, CPU#0: swapper/0
   Modules linked in:
   CPU: 0 UID: 0 PID: 0 Comm: swapper Not tainted 7.1.0-rc6 #1 PREEMPT(undef)
   pstate: a04000c9 (NzCv daIF +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
   pc : split_kernel_leaf_mapping+0x15c/0x170
   lr : update_range_prot+0x40/0x128
   sp : ffffc99ad3863c80
   ...
   Call trace:
    split_kernel_leaf_mapping+0x15c/0x170 (P)
    update_range_prot+0x40/0x128
    set_memory_valid+0x94/0xe0
    mark_linear_data_alias_valid+0x54/0x68
    map_mem+0x1fc/0x240
    paging_init+0x48/0x210
    setup_arch+0x274/0x338
    start_kernel+0x98/0x538
    __primary_switched+0x88/0x98

as reported by CKI automated testing.

---
 arch/arm64/mm/mmu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/mmu.c +3 -3
index d68e691c093a..3134f1c1097c 100644
@@ -1095,6 +1095,9 @@ void __init mark_linear_text_alias_ro(void)
 			    (unsigned long)__init_begin - (unsigned long)_text,
 			    PAGE_KERNEL_RO);
 
+	/* Map the kernel data/bss as invalid in the linear map */
+	mark_linear_data_alias_valid(false);
+
 	/*
 	 * Register a PM notifier to remap the linear alias of data/bss as
 	 * valid read-only before hibernation. This is needed because the
@@ -1237,9 +1240,6 @@ static void __init map_mem(void)
 		__map_memblock(start, end, pgprot_tagged(PAGE_KERNEL),
 			       flags);
 	}
-
-	/* Map the kernel data/bss as invalid in the linear map */
-	mark_linear_data_alias_valid(false);
 }
 
 void mark_rodata_ro(void)
Signature · 1 line
2.54.0.1013.g208068f2d8-goog
maintainer
#4 lore raw

Re: [PATCH 3/4] arm64: mte: Disregard the zero page explicitly for manipulating tags

In reply to Ard Biesheuvel
Quoted 20 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The zero page is conceptually immutable, and will be moved into .rodata to prevent inadvertent corruption.

Prepare the MTE code for this, by ensuring that the zero page is never taken into account for tag manipulation, given that those actions will no longer be permitted on the read-only alias of .rodata in the linear map.

---
 arch/arm64/include/asm/mte.h | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/include/asm/mte.h +0 -0
index 7f7b97e09996..093b34944aee 100644

arch/arm64/include/asm/mte.h static inline bool page_mte_tagged(struct page *page) · line 86

Quoted 7 lines
arch/arm64/include/asm/mte.h +4 -0
@@ -80,6 +80,11 @@ static inline bool page_mte_tagged(struct page *page)
  */
 static inline bool try_page_mte_tagging(struct page *page)
 {
+	extern struct page *__zero_page;
+
+	if (page == __zero_page)
+		return false;

Better as is_zero_page()

arch/arm64/include/asm/mte.h static inline bool page_mte_tagged(struct page *page) · line 90

Quoted 4 lines
arch/arm64/include/asm/mte.h +1 -0
+
 	VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page)));
 
 	if (!test_and_set_bit(PG_mte_lock, &page->flags.f))

Some form of this fix should have:

Fixes f620d66af316 ("arm64: mte: Do not flag the zero page as PG_mte_tagged")
Cc <[email protected]> # 5.10.x

The current mainline assumption is that mapping the zero page in user space is always mapped with pte_special() and we skip the MTE tag zeroing (and PG flag setting). However, the above commit missed the KVM kvm_s2_fault_map() -> sanitise_mte_tags() path and we don't have a form of pte_special() for stage 2 mappings.

I'm more inclined to go with a specific test in the KVM path. It matches the stage 1 where we skip the actual tagging. We could add a VM_WARN_ONCE in try_page_mte_tagging() to trap future changes.

-------------8<-----------------------

arch/arm64/kvm/mmu.c +5 -0
index d089c107d9b7..445d6cf035c9 100644
@@ -1479,6 +1479,11 @@ static void sanitise_mte_tags(struct kvm *kvm, kvm_pfn_t pfn,
 	if (!kvm_has_mte(kvm))
 		return;
 
+	if (is_zero_pfn(pfn)) {
+		WARN_ON_ONCE(nr_pages != 1);
+		return;
+	}
+
 	if (folio_test_hugetlb(folio)) {
 		/* Hugetlb has MTE flags set on head page only */
 		if (folio_try_hugetlb_mte_tagging(folio)) {
Signature · 1 line
Catalin
#5 lore raw

Re: [PATCH 3/4] arm64: mte: Disregard the zero page explicitly for manipulating tags

Quoted 16 lines · Catalin Marinas
Quoted 15 lines · Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The zero page is conceptually immutable, and will be moved into .rodata to prevent inadvertent corruption.

Prepare the MTE code for this, by ensuring that the zero page is never taken into account for tag manipulation, given that those actions will no longer be permitted on the read-only alias of .rodata in the linear map.

---
 arch/arm64/include/asm/mte.h | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 15 lines

arch/arm64/include/asm/mte.h static inline bool page_mte_tagged(struct page *page) · line 86

Quoted 12 lines
arch/arm64/include/asm/mte.h +4 -0
index 7f7b97e09996..093b34944aee 100644
@@ -80,6 +80,11 @@ static inline bool page_mte_tagged(struct page *page)
  */
 static inline bool try_page_mte_tagging(struct page *page)
 {
+	extern struct page *__zero_page;
+
+	if (page == __zero_page)
+		return false;

Better as is_zero_page()

True, but I was concerned about #inclusion hell.

Quoted 16 lines

arch/arm64/include/asm/mte.h static inline bool page_mte_tagged(struct page *page) · line 90

Quoted 4 lines
arch/arm64/include/asm/mte.h +1 -0
+
 	VM_WARN_ON_ONCE(folio_test_hugetlb(page_folio(page)));
 
 	if (!test_and_set_bit(PG_mte_lock, &page->flags.f))

Some form of this fix should have:

Fixes f620d66af316 ("arm64: mte: Do not flag the zero page as PG_mte_tagged")
Cc <[email protected]> # 5.10.x

The current mainline assumption is that mapping the zero page in user space is always mapped with pte_special() and we skip the MTE tag zeroing (and PG flag setting). However, the above commit missed the KVM kvm_s2_fault_map() -> sanitise_mte_tags() path and we don't have a form of pte_special() for stage 2 mappings.

Quoted 4 lines

I'm more inclined to go with a specific test in the KVM path. It matches the stage 1 where we skip the actual tagging. We could add a VM_WARN_ONCE in try_page_mte_tagging() to trap future changes.

Let's go with that - I'll turn this into a patch for v2

Quoted 21 lines

-------------8<-----------------------

arch/arm64/kvm/mmu.c +5 -0
index d089c107d9b7..445d6cf035c9 100644
@@ -1479,6 +1479,11 @@ static void sanitise_mte_tags(struct kvm *kvm, 
 kvm_pfn_t pfn,
 	if (!kvm_has_mte(kvm))
 		return;
 
+	if (is_zero_pfn(pfn)) {
+		WARN_ON_ONCE(nr_pages != 1);
+		return;
+	}
+
 	if (folio_test_hugetlb(folio)) {
 		/* Hugetlb has MTE flags set on head page only */

if (folio_try_hugetlb_mte_tagging(folio)) {

Signature · 1 line
Catalin