Merge tag 'riscv-for-linus-5.14-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux

Pull RISC-V fixes from Palmer Dabbelt:

- properly set the memory size, which fixes 32-bit systems

- allow initrd to load anywhere in memory, rather that restricting it
to the first 256MiB

- fix the 'mem=' parameter on 64-bit systems to properly account for
the maximum supported memory now that the kernel is outside the
linear map

- avoid installing mappings into the last 4KiB of memory, which
conflicts with error values

- avoid the stack from being freed while it is being walked

- a handful of fixes to the new copy to/from user routines

* tag 'riscv-for-linus-5.14-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux:
riscv: __asm_copy_to-from_user: Fix: Typos in comments
riscv: __asm_copy_to-from_user: Remove unnecessary size check
riscv: __asm_copy_to-from_user: Fix: fail on RV32
riscv: __asm_copy_to-from_user: Fix: overrun copy
riscv: stacktrace: pin the task's stack in get_wchan
riscv: Make sure the kernel mapping does not overlap with IS_ERR_VALUE
riscv: Make sure the linear mapping does not use the kernel mapping
riscv: Fix memory_limit for 64-bit kernel
RISC-V: load initrd wherever it fits into memory
riscv: Fix 32-bit RISC-V boot failure

+48 -21
+2 -2
arch/riscv/include/asm/efi.h
··· 27 28 #define ARCH_EFI_IRQ_FLAGS_MASK (SR_IE | SR_SPIE) 29 30 - /* Load initrd at enough distance from DRAM start */ 31 static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr) 32 { 33 - return image_addr + SZ_256M; 34 } 35 36 #define alloc_screen_info(x...) (&screen_info)
··· 27 28 #define ARCH_EFI_IRQ_FLAGS_MASK (SR_IE | SR_SPIE) 29 30 + /* Load initrd anywhere in system RAM */ 31 static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr) 32 { 33 + return ULONG_MAX; 34 } 35 36 #define alloc_screen_info(x...) (&screen_info)
+5 -1
arch/riscv/kernel/stacktrace.c
··· 132 { 133 unsigned long pc = 0; 134 135 - if (likely(task && task != current && !task_is_running(task))) 136 walk_stackframe(task, NULL, save_wchan, &pc); 137 return pc; 138 } 139
··· 132 { 133 unsigned long pc = 0; 134 135 + if (likely(task && task != current && !task_is_running(task))) { 136 + if (!try_get_task_stack(task)) 137 + return 0; 138 walk_stackframe(task, NULL, save_wchan, &pc); 139 + put_task_stack(task); 140 + } 141 return pc; 142 } 143
+13 -14
arch/riscv/lib/uaccess.S
··· 30 * t0 - end of uncopied dst 31 */ 32 add t0, a0, a2 33 - bgtu a0, t0, 5f 34 35 /* 36 * Use byte copy only if too small. 37 */ 38 - li a3, 8*SZREG /* size must be larger than size in word_copy */ 39 bltu a2, a3, .Lbyte_copy_tail 40 41 /* 42 - * Copy first bytes until dst is align to word boundary. 43 * a0 - start of dst 44 * t1 - start of aligned dst 45 */ 46 addi t1, a0, SZREG-1 47 andi t1, t1, ~(SZREG-1) 48 /* dst is already aligned, skip */ 49 - beq a0, t1, .Lskip_first_bytes 50 1: 51 /* a5 - one byte for copying data */ 52 fixup lb a5, 0(a1), 10f ··· 55 addi a0, a0, 1 /* dst */ 56 bltu a0, t1, 1b /* t1 - start of aligned dst */ 57 58 - .Lskip_first_bytes: 59 /* 60 * Now dst is aligned. 61 * Use shift-copy if src is misaligned. ··· 72 * 73 * a0 - start of aligned dst 74 * a1 - start of aligned src 75 - * a3 - a1 & mask:(SZREG-1) 76 * t0 - end of aligned dst 77 */ 78 - addi t0, t0, -(8*SZREG-1) /* not to over run */ 79 2: 80 fixup REG_L a4, 0(a1), 10f 81 fixup REG_L a5, SZREG(a1), 10f ··· 96 addi a1, a1, 8*SZREG 97 bltu a0, t0, 2b 98 99 - addi t0, t0, 8*SZREG-1 /* revert to original value */ 100 j .Lbyte_copy_tail 101 102 .Lshift_copy: ··· 106 * For misaligned copy we still perform aligned word copy, but 107 * we need to use the value fetched from the previous iteration and 108 * do some shifts. 109 - * This is safe because reading less than a word size. 110 * 111 * a0 - start of aligned dst 112 * a1 - start of src ··· 116 */ 117 /* calculating aligned word boundary for dst */ 118 andi t1, t0, ~(SZREG-1) 119 - /* Converting unaligned src to aligned arc */ 120 andi a1, a1, ~(SZREG-1) 121 122 /* ··· 124 * t3 - prev shift 125 * t4 - current shift 126 */ 127 - slli t3, a3, LGREG 128 li a5, SZREG*8 129 sub t4, a5, t3 130 131 - /* Load the first word to combine with seceond word */ 132 fixup REG_L a5, 0(a1), 10f 133 134 3: ··· 160 * a1 - start of remaining src 161 * t0 - end of remaining dst 162 */ 163 - bgeu a0, t0, 5f 164 4: 165 fixup lb a5, 0(a1), 10f 166 addi a1, a1, 1 /* src */ ··· 168 addi a0, a0, 1 /* dst */ 169 bltu a0, t0, 4b /* t0 - end of dst */ 170 171 - 5: 172 /* Disable access to user memory */ 173 csrc CSR_STATUS, t6 174 li a0, 0
··· 30 * t0 - end of uncopied dst 31 */ 32 add t0, a0, a2 33 34 /* 35 * Use byte copy only if too small. 36 + * SZREG holds 4 for RV32 and 8 for RV64 37 */ 38 + li a3, 9*SZREG /* size must be larger than size in word_copy */ 39 bltu a2, a3, .Lbyte_copy_tail 40 41 /* 42 + * Copy first bytes until dst is aligned to word boundary. 43 * a0 - start of dst 44 * t1 - start of aligned dst 45 */ 46 addi t1, a0, SZREG-1 47 andi t1, t1, ~(SZREG-1) 48 /* dst is already aligned, skip */ 49 + beq a0, t1, .Lskip_align_dst 50 1: 51 /* a5 - one byte for copying data */ 52 fixup lb a5, 0(a1), 10f ··· 55 addi a0, a0, 1 /* dst */ 56 bltu a0, t1, 1b /* t1 - start of aligned dst */ 57 58 + .Lskip_align_dst: 59 /* 60 * Now dst is aligned. 61 * Use shift-copy if src is misaligned. ··· 72 * 73 * a0 - start of aligned dst 74 * a1 - start of aligned src 75 * t0 - end of aligned dst 76 */ 77 + addi t0, t0, -(8*SZREG) /* not to over run */ 78 2: 79 fixup REG_L a4, 0(a1), 10f 80 fixup REG_L a5, SZREG(a1), 10f ··· 97 addi a1, a1, 8*SZREG 98 bltu a0, t0, 2b 99 100 + addi t0, t0, 8*SZREG /* revert to original value */ 101 j .Lbyte_copy_tail 102 103 .Lshift_copy: ··· 107 * For misaligned copy we still perform aligned word copy, but 108 * we need to use the value fetched from the previous iteration and 109 * do some shifts. 110 + * This is safe because reading is less than a word size. 111 * 112 * a0 - start of aligned dst 113 * a1 - start of src ··· 117 */ 118 /* calculating aligned word boundary for dst */ 119 andi t1, t0, ~(SZREG-1) 120 + /* Converting unaligned src to aligned src */ 121 andi a1, a1, ~(SZREG-1) 122 123 /* ··· 125 * t3 - prev shift 126 * t4 - current shift 127 */ 128 + slli t3, a3, 3 /* converting bytes in a3 to bits */ 129 li a5, SZREG*8 130 sub t4, a5, t3 131 132 + /* Load the first word to combine with second word */ 133 fixup REG_L a5, 0(a1), 10f 134 135 3: ··· 161 * a1 - start of remaining src 162 * t0 - end of remaining dst 163 */ 164 + bgeu a0, t0, .Lout_copy_user /* check if end of copy */ 165 4: 166 fixup lb a5, 0(a1), 10f 167 addi a1, a1, 1 /* src */ ··· 169 addi a0, a0, 1 /* dst */ 170 bltu a0, t0, 4b /* t0 - end of dst */ 171 172 + .Lout_copy_user: 173 /* Disable access to user memory */ 174 csrc CSR_STATUS, t6 175 li a0, 0
+28 -4
arch/riscv/mm/init.c
··· 127 } 128 129 /* 130 - * The default maximal physical memory size is -PAGE_OFFSET, 131 - * limit the memory size via mem. 132 */ 133 static phys_addr_t memory_limit = -PAGE_OFFSET; 134 135 static int __init early_mem(char *p) 136 { ··· 159 { 160 phys_addr_t vmlinux_end = __pa_symbol(&_end); 161 phys_addr_t vmlinux_start = __pa_symbol(&_start); 162 - phys_addr_t max_mapped_addr = __pa(~(ulong)0); 163 phys_addr_t dram_end; 164 165 #ifdef CONFIG_XIP_KERNEL ··· 182 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start); 183 184 dram_end = memblock_end_of_DRAM(); 185 /* 186 * memblock allocator is not aware of the fact that last 4K bytes of 187 * the addressable memory can not be mapped because of IS_ERR_VALUE 188 * macro. Make sure that last 4k bytes are not usable by memblock 189 - * if end of dram is equal to maximum addressable memory. 190 */ 191 if (max_mapped_addr == (dram_end - 1)) 192 memblock_set_current_limit(max_mapped_addr - 4096); 193 194 min_low_pfn = PFN_UP(memblock_start_of_DRAM()); 195 max_low_pfn = max_pfn = PFN_DOWN(dram_end); ··· 584 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); 585 BUG_ON((kernel_map.phys_addr % map_size) != 0); 586 587 pt_ops.alloc_pte = alloc_pte_early; 588 pt_ops.get_pte_virt = get_pte_virt_early; 589 #ifndef __PAGETABLE_PMD_FOLDED ··· 731 if (start <= __pa(PAGE_OFFSET) && 732 __pa(PAGE_OFFSET) < end) 733 start = __pa(PAGE_OFFSET); 734 735 map_size = best_map_size(start, end - start); 736 for (pa = start; pa < end; pa += map_size) {
··· 127 } 128 129 /* 130 + * The default maximal physical memory size is -PAGE_OFFSET for 32-bit kernel, 131 + * whereas for 64-bit kernel, the end of the virtual address space is occupied 132 + * by the modules/BPF/kernel mappings which reduces the available size of the 133 + * linear mapping. 134 + * Limit the memory size via mem. 135 */ 136 + #ifdef CONFIG_64BIT 137 + static phys_addr_t memory_limit = -PAGE_OFFSET - SZ_4G; 138 + #else 139 static phys_addr_t memory_limit = -PAGE_OFFSET; 140 + #endif 141 142 static int __init early_mem(char *p) 143 { ··· 152 { 153 phys_addr_t vmlinux_end = __pa_symbol(&_end); 154 phys_addr_t vmlinux_start = __pa_symbol(&_start); 155 + phys_addr_t __maybe_unused max_mapped_addr; 156 phys_addr_t dram_end; 157 158 #ifdef CONFIG_XIP_KERNEL ··· 175 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start); 176 177 dram_end = memblock_end_of_DRAM(); 178 + 179 + #ifndef CONFIG_64BIT 180 /* 181 * memblock allocator is not aware of the fact that last 4K bytes of 182 * the addressable memory can not be mapped because of IS_ERR_VALUE 183 * macro. Make sure that last 4k bytes are not usable by memblock 184 + * if end of dram is equal to maximum addressable memory. For 64-bit 185 + * kernel, this problem can't happen here as the end of the virtual 186 + * address space is occupied by the kernel mapping then this check must 187 + * be done in create_kernel_page_table. 188 */ 189 + max_mapped_addr = __pa(~(ulong)0); 190 if (max_mapped_addr == (dram_end - 1)) 191 memblock_set_current_limit(max_mapped_addr - 4096); 192 + #endif 193 194 min_low_pfn = PFN_UP(memblock_start_of_DRAM()); 195 max_low_pfn = max_pfn = PFN_DOWN(dram_end); ··· 570 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); 571 BUG_ON((kernel_map.phys_addr % map_size) != 0); 572 573 + #ifdef CONFIG_64BIT 574 + /* 575 + * The last 4K bytes of the addressable memory can not be mapped because 576 + * of IS_ERR_VALUE macro. 577 + */ 578 + BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K); 579 + #endif 580 + 581 pt_ops.alloc_pte = alloc_pte_early; 582 pt_ops.get_pte_virt = get_pte_virt_early; 583 #ifndef __PAGETABLE_PMD_FOLDED ··· 709 if (start <= __pa(PAGE_OFFSET) && 710 __pa(PAGE_OFFSET) < end) 711 start = __pa(PAGE_OFFSET); 712 + if (end >= __pa(PAGE_OFFSET) + memory_limit) 713 + end = __pa(PAGE_OFFSET) + memory_limit; 714 715 map_size = best_map_size(start, end - start); 716 for (pa = start; pa < end; pa += map_size) {