Skip to content

Commit 800ed39

Browse files
arndbksacilotto
authored andcommitted
arch: pgtable: define MAX_POSSIBLE_PHYSMEM_BITS where needed
BugLink: https://bugs.launchpad.net/bugs/1908562 [ Upstream commit cef3970 ] Stefan Agner reported a bug when using zsram on 32-bit Arm machines with RAM above the 4GB address boundary: Unable to handle kernel NULL pointer dereference at virtual address 00000000 pgd = a27bd01c [00000000] *pgd=236a0003, *pmd=1ffa64003 Internal error: Oops: 207 [#1] SMP ARM Modules linked in: mdio_bcm_unimac(+) brcmfmac cfg80211 brcmutil raspberrypi_hwmon hci_uart crc32_arm_ce bcm2711_thermal phy_generic genet CPU: 0 PID: 123 Comm: mkfs.ext4 Not tainted 5.9.6 #1 Hardware name: BCM2711 PC is at zs_map_object+0x94/0x338 LR is at zram_bvec_rw.constprop.0+0x330/0xa64 pc : [<c0602b38>] lr : [<c0bda6a0>] psr: 60000013 sp : e376bbe0 ip : 00000000 fp : c1e2921c r10: 00000002 r9 : c1dda730 r8 : 00000000 r7 : e8ff7a00 r6 : 00000000 r5 : 02f9ffa0 r4 : e3710000 r3 : 000fdffe r2 : c1e0ce80 r1 : ebf979a0 r0 : 00000000 Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA ARM Segment user Control: 30c5383d Table: 235c2a80 DAC: fffffffd Process mkfs.ext4 (pid: 123, stack limit = 0x495a22e6) Stack: (0xe376bbe0 to 0xe376c000) As it turns out, zsram needs to know the maximum memory size, which is defined in MAX_PHYSMEM_BITS when CONFIG_SPARSEMEM is set, or in MAX_POSSIBLE_PHYSMEM_BITS on the x86 architecture. The same problem will be hit on all 32-bit architectures that have a physical address space larger than 4GB and happen to not enable sparsemem and include asm/sparsemem.h from asm/pgtable.h. After the initial discussion, I suggested just always defining MAX_POSSIBLE_PHYSMEM_BITS whenever CONFIG_PHYS_ADDR_T_64BIT is set, or provoking a build error otherwise. This addresses all configurations that can currently have this runtime bug, but leaves all other configurations unchanged. I looked up the possible number of bits in source code and datasheets, here is what I found: - on ARC, CONFIG_ARC_HAS_PAE40 controls whether 32 or 40 bits are used - on ARM, CONFIG_LPAE enables 40 bit addressing, without it we never support more than 32 bits, even though supersections in theory allow up to 40 bits as well. - on MIPS, some MIPS32r1 or later chips support 36 bits, and MIPS32r5 XPA supports up to 60 bits in theory, but 40 bits are more than anyone will ever ship - On PowerPC, there are three different implementations of 36 bit addressing, but 32-bit is used without CONFIG_PTE_64BIT - On RISC-V, the normal page table format can support 34 bit addressing. There is no highmem support on RISC-V, so anything above 2GB is unused, but it might be useful to eventually support CONFIG_ZRAM for high pages. Fixes: 61989a8 ("staging: zsmalloc: zsmalloc memory allocation library") Fixes: 02390b8 ("mm/zsmalloc: Prepare to variable MAX_PHYSMEM_BITS") Acked-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de> Reviewed-by: Stefan Agner <stefan@agner.ch> Tested-by: Stefan Agner <stefan@agner.ch> Acked-by: Mike Rapoport <rppt@linux.ibm.com> Link: https://lore.kernel.org/linux-mm/bdfa44bf1c570b05d6c70898e2bbb0acf234ecdf.1604762181.git.stefan@agner.ch/ Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Kamal Mostafa <kamal@canonical.com> Signed-off-by: Ian May <ian.may@canonical.com>
1 parent 92adbc9 commit 800ed39

File tree

8 files changed

+28
-0
lines changed

8 files changed

+28
-0
lines changed

arch/arc/include/asm/pgtable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@
135135

136136
#ifdef CONFIG_ARC_HAS_PAE40
137137
#define PTE_BITS_NON_RWX_IN_PD1 (0xff00000000 | PAGE_MASK | _PAGE_CACHEABLE)
138+
#define MAX_POSSIBLE_PHYSMEM_BITS 40
138139
#else
139140
#define PTE_BITS_NON_RWX_IN_PD1 (PAGE_MASK | _PAGE_CACHEABLE)
141+
#define MAX_POSSIBLE_PHYSMEM_BITS 32
140142
#endif
141143

142144
/**************************************************************************

arch/arm/include/asm/pgtable-2level.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@
7575
#define PTE_HWTABLE_OFF (PTE_HWTABLE_PTRS * sizeof(pte_t))
7676
#define PTE_HWTABLE_SIZE (PTRS_PER_PTE * sizeof(u32))
7777

78+
#define MAX_POSSIBLE_PHYSMEM_BITS 32
79+
7880
/*
7981
* PMD_SHIFT determines the size of the area a second-level page table can map
8082
* PGDIR_SHIFT determines what a third-level page table entry can map

arch/arm/include/asm/pgtable-3level.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#define PTE_HWTABLE_OFF (0)
2626
#define PTE_HWTABLE_SIZE (PTRS_PER_PTE * sizeof(u64))
2727

28+
#define MAX_POSSIBLE_PHYSMEM_BITS 40
29+
2830
/*
2931
* PGDIR_SHIFT determines the size a top-level page table entry can map.
3032
*/

arch/mips/include/asm/pgtable-32.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ static inline void pmd_clear(pmd_t *pmdp)
155155

156156
#if defined(CONFIG_XPA)
157157

158+
#define MAX_POSSIBLE_PHYSMEM_BITS 40
158159
#define pte_pfn(x) (((unsigned long)((x).pte_high >> _PFN_SHIFT)) | (unsigned long)((x).pte_low << _PAGE_PRESENT_SHIFT))
159160
static inline pte_t
160161
pfn_pte(unsigned long pfn, pgprot_t prot)
@@ -170,6 +171,7 @@ pfn_pte(unsigned long pfn, pgprot_t prot)
170171

171172
#elif defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
172173

174+
#define MAX_POSSIBLE_PHYSMEM_BITS 36
173175
#define pte_pfn(x) ((unsigned long)((x).pte_high >> 6))
174176

175177
static inline pte_t pfn_pte(unsigned long pfn, pgprot_t prot)
@@ -184,6 +186,7 @@ static inline pte_t pfn_pte(unsigned long pfn, pgprot_t prot)
184186

185187
#else
186188

189+
#define MAX_POSSIBLE_PHYSMEM_BITS 32
187190
#ifdef CONFIG_CPU_VR41XX
188191
#define pte_pfn(x) ((unsigned long)((x).pte >> (PAGE_SHIFT + 2)))
189192
#define pfn_pte(pfn, prot) __pte(((pfn) << (PAGE_SHIFT + 2)) | pgprot_val(prot))

arch/powerpc/include/asm/book3s/32/pgtable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ static inline bool pte_user(pte_t pte)
3737
*/
3838
#ifdef CONFIG_PTE_64BIT
3939
#define PTE_RPN_MASK (~((1ULL << PTE_RPN_SHIFT) - 1))
40+
#define MAX_POSSIBLE_PHYSMEM_BITS 36
4041
#else
4142
#define PTE_RPN_MASK (~((1UL << PTE_RPN_SHIFT) - 1))
43+
#define MAX_POSSIBLE_PHYSMEM_BITS 32
4244
#endif
4345

4446
/*

arch/powerpc/include/asm/nohash/32/pgtable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ int map_kernel_page(unsigned long va, phys_addr_t pa, pgprot_t prot);
148148
*/
149149
#if defined(CONFIG_PPC32) && defined(CONFIG_PTE_64BIT)
150150
#define PTE_RPN_MASK (~((1ULL << PTE_RPN_SHIFT) - 1))
151+
#define MAX_POSSIBLE_PHYSMEM_BITS 36
151152
#else
152153
#define PTE_RPN_MASK (~((1UL << PTE_RPN_SHIFT) - 1))
154+
#define MAX_POSSIBLE_PHYSMEM_BITS 32
153155
#endif
154156

155157
/*

arch/riscv/include/asm/pgtable-32.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@
1414
#define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT)
1515
#define PGDIR_MASK (~(PGDIR_SIZE - 1))
1616

17+
#define MAX_POSSIBLE_PHYSMEM_BITS 34
18+
1719
#endif /* _ASM_RISCV_PGTABLE_32_H */

include/asm-generic/pgtable.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,19 @@ static inline bool arch_has_pfn_modify_check(void)
11591159

11601160
#endif /* !__ASSEMBLY__ */
11611161

1162+
#if !defined(MAX_POSSIBLE_PHYSMEM_BITS) && !defined(CONFIG_64BIT)
1163+
#ifdef CONFIG_PHYS_ADDR_T_64BIT
1164+
/*
1165+
* ZSMALLOC needs to know the highest PFN on 32-bit architectures
1166+
* with physical address space extension, but falls back to
1167+
* BITS_PER_LONG otherwise.
1168+
*/
1169+
#error Missing MAX_POSSIBLE_PHYSMEM_BITS definition
1170+
#else
1171+
#define MAX_POSSIBLE_PHYSMEM_BITS 32
1172+
#endif
1173+
#endif
1174+
11621175
#ifndef has_transparent_hugepage
11631176
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
11641177
#define has_transparent_hugepage() 1

0 commit comments

Comments
 (0)