1. 31 Aug, 2023 13 commits
    • Nathan Chancellor's avatar
      lib/Kconfig.debug: Restrict DEBUG_INFO_SPLIT for RISC-V · 89775a27
      Nathan Chancellor authored
      When building for ARCH=riscv using LLVM < 14, there is an error with
      CONFIG_DEBUG_INFO_SPLIT=y:
      
        error: A dwo section may not contain relocations
      
      This was worked around in LLVM 15 by disallowing '-gsplit-dwarf' with
      '-mrelax' (the default), so CONFIG_DEBUG_INFO_SPLIT is not selectable
      with newer versions of LLVM:
      
        $ clang --target=riscv64-linux-gnu -gsplit-dwarf -c -o /dev/null -x c /dev/null
        clang: error: -gsplit-dwarf is unsupported with RISC-V linker relaxation (-mrelax)
      
      GCC silently had a similar issue that was resolved with GCC 12.x.
      Restrict CONFIG_DEBUG_INFO_SPLIT for RISC-V when using LLVM or GCC <
      12.x to avoid these known issues.
      
      Link: https://github.com/ClangBuiltLinux/linux/issues/1914
      Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99090Reported-by: default avatarkernel test robot <lkp@intel.com>
      Closes: https://lore.kernel.org/all/202308090204.9yZffBWo-lkp@intel.com/Signed-off-by: default avatarNathan Chancellor <nathan@kernel.org>
      Reviewed-by: default avatarFangrui Song <maskray@google.com>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      Link: https://lore.kernel.org/r/20230816-riscv-debug_info_split-v1-1-d1019d6ccc11@kernel.orgSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      89775a27
    • Palmer Dabbelt's avatar
      Merge patch series "RISC-V: mm: Make SV48 the default address space" · 94f00388
      Palmer Dabbelt authored
      Charlie Jenkins <charlie@rivosinc.com> says:
      
      Make sv48 the default address space for mmap as some applications
      currently depend on this assumption. Users can now select a
      desired address space using a non-zero hint address to mmap. Previously,
      requesting the default address space from mmap by passing zero as the hint
      address would result in using the largest address space possible. Some
      applications depend on empty bits in the virtual address space, like Go and
      Java, so this patch provides more flexibility for application developers.
      
      * b4-shazam-merge:
        RISC-V: mm: Document mmap changes
        RISC-V: mm: Update pgtable comment documentation
        RISC-V: mm: Add tests for RISC-V mm
        RISC-V: mm: Restrict address space for sv39,sv48,sv57
      
      Link: https://lore.kernel.org/r/20230809232218.849726-1-charlie@rivosinc.comSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      94f00388
    • Palmer Dabbelt's avatar
      Merge patch series "riscv: Reduce ARCH_KMALLOC_MINALIGN to 8" · 52b77c28
      Palmer Dabbelt authored
      Jisheng Zhang <jszhang@kernel.org> says:
      
      Currently, riscv defines ARCH_DMA_MINALIGN as L1_CACHE_BYTES, I.E
      64Bytes, if CONFIG_RISCV_DMA_NONCOHERENT=y. To support unified kernel
      Image, usually we have to enable CONFIG_RISCV_DMA_NONCOHERENT, thus
      it brings some bad effects to coherent platforms:
      
      Firstly, it wastes memory, kmalloc-96, kmalloc-32, kmalloc-16 and
      kmalloc-8 slab caches don't exist any more, they are replaced with
      either kmalloc-128 or kmalloc-64.
      
      Secondly, larger than necessary kmalloc aligned allocations results
      in unnecessary cache/TLB pressure.
      
      This issue also exists on arm64 platforms. From last year, Catalin
      tried to solve this issue by decoupling ARCH_KMALLOC_MINALIGN from
      ARCH_DMA_MINALIGN, limiting kmalloc() minimum alignment to
      dma_get_cache_alignment() and replacing ARCH_KMALLOC_MINALIGN usage
      in various drivers with ARCH_DMA_MINALIGN etc.[1]
      
      One fact we can make use of for riscv: if the CPU doesn't support
      ZICBOM or T-HEAD CMO, we know the platform is coherent. Based on
      Catalin's work and above fact, we can easily solve the kmalloc align
      issue for riscv: we can override dma_get_cache_alignment(), then let
      it return ARCH_DMA_MINALIGN at the beginning and return 1 once we know
      the underlying HW neither supports ZICBOM nor supports T-HEAD CMO.
      
      So what about if the CPU supports ZICBOM or T-HEAD CMO, but all the
      devices are dma coherent? Well, we use ARCH_DMA_MINALIGN as the
      kmalloc minimum alignment, nothing changed in this case. This case
      can be improved in the future once we see such platforms in mainline.
      
      After this patch, a simple test of booting to a small buildroot rootfs
      on qemu shows:
      
      kmalloc-96           5041    5041     96  ...
      kmalloc-64           9606    9606     64  ...
      kmalloc-32           5128    5128     32  ...
      kmalloc-16           7682    7682     16  ...
      kmalloc-8           10246   10246      8  ...
      
      So we save about 1268KB memory. The saving will be much larger in normal
      OS env on real HW platforms.
      
      patch1 allows kmalloc() caches aligned to the smallest value.
      patch2 enables DMA_BOUNCE_UNALIGNED_KMALLOC.
      
      After this series:
      
      As for coherent platforms, kmalloc-{8,16,32,96} caches come back on
      coherent both RV32 and RV64 platforms, I.E !ZICBOM and !THEAD_CMO.
      
      As for noncoherent RV32 platforms, nothing changed.
      
      As for noncoherent RV64 platforms, I.E either ZICBOM or THEAD_CMO, the
      above kmalloc caches also come back if > 4GB memory or users pass
      "swiotlb=mmnn,force" to force swiotlb creation if <= 4GB memory. How
      much mmnn should be depends on the specific platform, it needs to be
      tried and tested all possible usage case on the specific hardware. For
      example, I can use the minimal I/O TLB slabs on Sipeed M1S Dock.
      
      * b4-shazam-merge:
        riscv: enable DMA_BOUNCE_UNALIGNED_KMALLOC for !dma_coherent
        riscv: allow kmalloc() caches aligned to the smallest value
      
      Link: https://lore.kernel.org/linux-arm-kernel/20230524171904.3967031-1-catalin.marinas@arm.com/ [1]
      Link: https://lore.kernel.org/r/20230718152214.2907-1-jszhang@kernel.orgSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      52b77c28
    • Jisheng Zhang's avatar
      riscv: support PREEMPT_DYNAMIC with static keys · 4e90d052
      Jisheng Zhang authored
      Currently, each architecture can support PREEMPT_DYNAMIC through
      either static calls or static keys. To support PREEMPT_DYNAMIC on
      riscv, we face three choices:
      
      1. only add static calls support to riscv
      As Mark pointed out in commit 99cf983c ("sched/preempt: Add
      PREEMPT_DYNAMIC using static keys"), static keys "...should have
      slightly lower overhead than non-inline static calls, as this
      effectively inlines each trampoline into the start of its callee. This
      may avoid redundant work, and may integrate better with CFI schemes."
      So even we add static calls(without inline static calls) to riscv,
      static keys is still a better choice.
      
      2. add static calls and inline static calls to riscv
      Per my understanding, inline static calls requires objtool support
      which is not easy.
      
      3. use static keys
      
      While riscv doesn't have static calls support, it supports static keys
      perfectly. So this patch selects HAVE_PREEMPT_DYNAMIC_KEY to enable
      support for PREEMPT_DYNAMIC on riscv, so that the preemption model can
      be chosen at boot time. It also patches asm-generic/preempt.h, mainly
      to add __preempt_schedule() and __preempt_schedule_notrace() macros
      for PREEMPT_DYNAMIC case. Other architectures which use generic
      preempt.h can also benefit from this patch by simply selecting
      HAVE_PREEMPT_DYNAMIC_KEY to enable PREEMPT_DYNAMIC if they supports
      static keys.
      Signed-off-by: default avatarJisheng Zhang <jszhang@kernel.org>
      Reviewed-by: default avatarConor Dooley <conor.dooley@microchip.com>
      Link: https://lore.kernel.org/r/20230716164925.1858-1-jszhang@kernel.orgSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      4e90d052
    • Palmer Dabbelt's avatar
      Merge patch series "riscv: support ELF format binaries in nommu mode" · 150e3c92
      Palmer Dabbelt authored
      Greg Ungerer <gerg@kernel.org> says:
      
      The following changes add the ability to run ELF format binaries when
      running RISC-V in nommu mode. That support is actually part of the
      ELF-FDPIC loader, so these changes are all about making that work on
      RISC-V.
      
      The first issue to deal with is making the ELF-FDPIC loader capable of
      handling 64-bit ELF files. As coded right now it only supports 32-bit
      ELF files.
      
      Secondly some changes are required to enable and compile the ELF-FDPIC
      loader on RISC-V and to pass the ELF-FDPIC mapping addresses through to
      user space when execing the new program.
      
      These changes have not been used to run actual ELF-FDPIC binaries.
      It is used to load and run normal ELF - compiled -pie format. Though the
      underlying changes are expected to work with full ELF-FDPIC binaries if
      or when that is supported on RISC-V in gcc.
      
      To avoid needing changes to the C-library (tested with uClibc-ng
      currently) there is a simple runtime dynamic loader (interpreter)
      available to do the final relocations, https://github.com/gregungerer/uldso.
      The nice thing about doing it this way is that the same program
      binary can also be loaded with the usual ELF loader in MMU linux.
      
      The motivation here is to provide an easy to use alternative to the
      flat format binaries normally used for RISC-V nommu based systems.
      
      * b4-shazam-merge:
        riscv: support the elf-fdpic binfmt loader
        binfmt_elf_fdpic: support 64-bit systems
      
      Link: https://lore.kernel.org/r/20230711130754.481209-1-gerg@kernel.orgSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      150e3c92
    • Palmer Dabbelt's avatar
      Merge patch series "riscv: KCFI support" · 7f7d3ea6
      Palmer Dabbelt authored
      Sami Tolvanen <samitolvanen@google.com> says:
      
      This series adds KCFI support for RISC-V. KCFI is a fine-grained
      forward-edge control-flow integrity scheme supported in Clang >=16,
      which ensures indirect calls in instrumented code can only branch to
      functions whose type matches the function pointer type, thus making
      code reuse attacks more difficult.
      
      Patch 1 implements a pt_regs based syscall wrapper to address
      function pointer type mismatches in syscall handling. Patches 2 and 3
      annotate indirectly called assembly functions with CFI types. Patch 4
      implements error handling for indirect call checks. Patch 5 disables
      CFI for arch/riscv/purgatory. Patch 6 finally allows CONFIG_CFI_CLANG
      to be enabled for RISC-V.
      
      Note that Clang 16 has a generic architecture-agnostic KCFI
      implementation, which does work with the kernel, but doesn't produce
      a stable code sequence for indirect call checks, which means
      potential failures just trap and won't result in informative error
      messages. Clang 17 includes a RISC-V specific back-end implementation
      for KCFI, which emits a predictable code sequence for the checks and a
      .kcfi_traps section with locations of the traps, which patch 5 uses to
      produce more useful errors.
      
      The type mismatch fixes and annotations in the first three patches
      also become necessary in future if the kernel decides to support
      fine-grained CFI implemented using the hardware landing pad
      feature proposed in the in-progress Zicfisslp extension. Once the
      specification is ratified and hardware support emerges, implementing
      runtime patching support that replaces KCFI instrumentation with
      Zicfisslp landing pads might also be feasible (similarly to KCFI to
      FineIBT patching on x86_64), allowing distributions to ship a unified
      kernel binary for all devices.
      
      * b4-shazam-merge:
        riscv: Allow CONFIG_CFI_CLANG to be selected
        riscv/purgatory: Disable CFI
        riscv: Add CFI error handling
        riscv: Add ftrace_stub_graph
        riscv: Add types to indirectly called assembly functions
        riscv: Implement syscall wrappers
      
      Link: https://lore.kernel.org/r/20230710183544.999540-8-samitolvanen@google.comSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      7f7d3ea6
    • Alexandre Ghiti's avatar
      riscv: Move create_tmp_mapping() to init sections · 9bdd9248
      Alexandre Ghiti authored
      This function is only used at boot time so mark it as __init.
      
      Fixes: 96f9d4da ("riscv: Rework kasan population functions")
      Signed-off-by: default avatarAlexandre Ghiti <alexghiti@rivosinc.com>
      Link: https://lore.kernel.org/r/20230704074357.233982-2-alexghiti@rivosinc.com
      Cc: stable@vger.kernel.org
      Signed-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      9bdd9248
    • Alexandre Ghiti's avatar
      riscv: Mark KASAN tmp* page tables variables as static · dd7664d6
      Alexandre Ghiti authored
      tmp_pg_dir, tmp_p4d and tmp_pud are only used in kasan_init.c so they
      should be declared as static.
      Reported-by: default avatarkernel test robot <lkp@intel.com>
      Closes: https://lore.kernel.org/oe-kbuild-all/202306282202.bODptiGE-lkp@intel.com/
      Fixes: 96f9d4da ("riscv: Rework kasan population functions")
      Signed-off-by: default avatarAlexandre Ghiti <alexghiti@rivosinc.com>
      Link: https://lore.kernel.org/r/20230704074357.233982-1-alexghiti@rivosinc.com
      Cc: stable@vger.kernel.org
      Signed-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      dd7664d6
    • Ye Xingchen's avatar
      riscv: mm: use bitmap_zero() API · 665c51f6
      Ye Xingchen authored
      bitmap_zero() is faster than bitmap_clear(), so use bitmap_zero()
      instead of bitmap_clear().
      Signed-off-by: default avatarYe Xingchen <ye.xingchen@zte.com.cn>
      Reviewed-by: default avatarAnup Patel <anup@brainfault.org>
      Link: https://lore.kernel.org/r/202305061711417142802@zte.com.cnSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      665c51f6
    • Palmer Dabbelt's avatar
      Merge patch series "support allocating crashkernel above 4G explicitly on riscv" · 9389e671
      Palmer Dabbelt authored
      Chen Jiahao <chenjiahao16@huawei.com> says:
      
      On riscv, the current crash kernel allocation logic is trying to
      allocate within 32bit addressible memory region by default, if
      failed, try to allocate without 4G restriction.
      
      In need of saving DMA zone memory while allocating a relatively large
      crash kernel region, allocating the reserved memory top down in
      high memory, without overlapping the DMA zone, is a mature solution.
      Hence this patchset introduces the parameter option crashkernel=X,[high,low].
      
      One can reserve the crash kernel from high memory above DMA zone range
      by explicitly passing "crashkernel=X,high"; or reserve a memory range
      below 4G with "crashkernel=X,low". Besides, there are few rules need
      to take notice:
      1. "crashkernel=X,[high,low]" will be ignored if "crashkernel=size"
         is specified.
      2. "crashkernel=X,low" is valid only when "crashkernel=X,high" is passed
         and there is enough memory to be allocated under 4G.
      3. When allocating crashkernel above 4G and no "crashkernel=X,low" is
         specified, a 128M low memory will be allocated automatically for
         swiotlb bounce buffer.
      See Documentation/admin-guide/kernel-parameters.txt for more information.
      
      To verify loading the crashkernel, adapted kexec-tools is attached below:
      https://github.com/chenjh005/kexec-tools/tree/build-test-riscv-v2
      
      Following test cases have been performed as expected:
      1) crashkernel=256M                          //low=256M
      2) crashkernel=1G                            //low=1G
      3) crashkernel=4G                            //high=4G, low=128M(default)
      4) crashkernel=4G crashkernel=256M,high      //high=4G, low=128M(default), high is ignored
      5) crashkernel=4G crashkernel=256M,low       //high=4G, low=128M(default), low is ignored
      6) crashkernel=4G,high                       //high=4G, low=128M(default)
      7) crashkernel=256M,low                      //low=0M, invalid
      8) crashkernel=4G,high crashkernel=256M,low  //high=4G, low=256M
      9) crashkernel=4G,high crashkernel=4G,low    //high=0M, low=0M, invalid
      10) crashkernel=512M@0xd0000000              //low=512M
      11) crashkernel=1G,high crashkernel=0M,low   //high=1G, low=0M
      
      * b4-shazam-merge:
        docs: kdump: Update the crashkernel description for riscv
        riscv: kdump: Implement crashkernel=X,[high,low]
      
      Link: https://lore.kernel.org/r/20230726175000.2536220-1-chenjiahao16@huawei.comSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      9389e671
    • Palmer Dabbelt's avatar
      Merge patch series "riscv: kprobes: simulate some instructions" · 82dfb5fd
      Palmer Dabbelt authored
      Nam Cao <namcaov@gmail.com> says:
      
      Simulate some currently rejected instructions. Still to be simulated are:
          - c.jal
          - c.ebreak
      
      * b4-shazam-merge:
        riscv: kprobes: simulate c.beqz and c.bnez
        riscv: kprobes: simulate c.jr and c.jalr instructions
        riscv: kprobes: simulate c.j instruction
      
      Link: https://lore.kernel.org/r/cover.1690704360.git.namcaov@gmail.comSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      82dfb5fd
    • Jisheng Zhang's avatar
      riscv: enable DEBUG_FORCE_FUNCTION_ALIGN_64B · 3ed8513c
      Jisheng Zhang authored
      Allow to force all function address 64B aligned as it is possible for
      other architectures. This may be useful when verify if performance
      bump is caused by function alignment changes.
      
      Before commit 1bf18da6 ("lib/Kconfig.debug: add ARCH dependency
      for FUNCTION_ALIGN option"), riscv supports enabling the
      DEBUG_FORCE_FUNCTION_ALIGN_64B option, but after that commit, each
      arch needs to claim the support explicitly.
      Signed-off-by: default avatarJisheng Zhang <jszhang@kernel.org>
      Reviewed-by: default avatarConor Dooley <conor.dooley@microchip.com>
      Link: https://lore.kernel.org/r/20230727160356.3874-1-jszhang@kernel.orgSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      3ed8513c
    • Nam Cao's avatar
      riscv: remove redundant mv instructions · 6b289a3f
      Nam Cao authored
      Some mv instructions were useful when first introduced to preserve a0 and
      a1 before function calls. However the code has changed and they are now
      redundant. Remove them.
      Signed-off-by: default avatarNam Cao <namcaov@gmail.com>
      Reviewed-by: default avatarAlexandre Ghiti <alexghiti@rivosinc.com>
      Link: https://lore.kernel.org/r/20230725053835.138910-1-namcaov@gmail.comSigned-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
      6b289a3f
  2. 23 Aug, 2023 16 commits
  3. 16 Aug, 2023 11 commits