1. 27 Jun, 2023 2 commits
    • Masahiro Yamada's avatar
      kbuild: revive "Entering directory" for Make >= 4.4.1 · 5fc10e76
      Masahiro Yamada authored
      With commit 9da0763b ("kbuild: Use relative path when building in
      a subdir of the source tree"), compiler messages in out-of-tree builds
      include relative paths, which are relative to the build directory, not
      the directory where make was started.
      
      To help IDEs/editors find the source files, Kbuild lets GNU Make print
      "Entering directory ..." when it changes the working directory. It has
      been working fine for a long time, but David reported it is broken with
      the latest GNU Make.
      
      The behavior was changed by GNU Make commit 8f9e7722ff0f ("[SV 63537]
      Fix setting -w in makefiles"). Previously, setting --no-print-directory
      to MAKEFLAGS only affected child makes, but it is now interpreted in
      the current make as soon as it is set.
      
      [test code]
      
        $ cat /tmp/Makefile
        ifneq ($(SUBMAKE),1)
        MAKEFLAGS += --no-print-directory
        all: ; $(MAKE) SUBMAKE=1
        else
        all: ; :
        endif
      
      [before 8f9e7722ff0f]
      
        $ make -C /tmp
        make: Entering directory '/tmp'
        make SUBMAKE=1
        :
        make: Leaving directory '/tmp'
      
      [after 8f9e7722ff0f]
      
        $ make -C /tmp
        make SUBMAKE=1
        :
      
      Previously, the effect of --no-print-directory was delayed until Kbuild
      started the directory descending, but it is no longer true with GNU Make
      4.4.1.
      
      This commit adds one more recursion to cater to GNU Make >= 4.4.1.
      
      When Kbuild needs to change the working directory, __submake will be
      executed twice.
      
        __submake without --no-print-directory  --> show "Entering directory ..."
        __submake with    --no-print-directory  --> parse the rest of Makefile
      
      We end up with one more recursion than needed for GNU Make < 4.4.1, but
      I do not want to complicate the version check.
      Reported-by: default avatarDavid Howells <dhowells@redhat.com>
      Closes: https://lore.kernel.org/all/2427604.1686237298@warthog.procyon.org.uk/Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Tested-by: default avatarNicolas Schier <n.schier@avm.de>
      5fc10e76
    • Masahiro Yamada's avatar
      kbuild: set correct abs_srctree and abs_objtree for package builds · 5fa94ceb
      Masahiro Yamada authored
      When you run 'make rpm-pkg', the rpmbuild tool builds the kernel in
      rpmbuild/BUILD, but $(abs_srctree) and $(abs_objtree) point to the
      directory path where make was started, not the kernel is actually
      being built. The same applies to 'make snap-pkg'. Fix it.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      5fa94ceb
  2. 26 Jun, 2023 3 commits
  3. 25 Jun, 2023 6 commits
  4. 24 Jun, 2023 2 commits
  5. 22 Jun, 2023 9 commits
    • Masahiro Yamada's avatar
      linux/export.h: rename 'sec' argument to 'license' · 8ed7e33a
      Masahiro Yamada authored
      Now, EXPORT_SYMBOL() is populated in two stages. In the first stage,
      all of EXPORT_SYMBOL/EXPORT_SYMBOL_GPL go into the same section,
      '.export_symbol'.
      
      'sec' does not make sense any more. Rename it to 'license'.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      8ed7e33a
    • Masahiro Yamada's avatar
      modpost: show offset from symbol for section mismatch warnings · f2346278
      Masahiro Yamada authored
      Currently, modpost only shows the symbol names and section names, so it
      repeats the same message if there are multiple relocations in the same
      symbol. It is common the relocation spans across multiple instructions.
      
      It is better to show the offset from the symbol.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      f2346278
    • Masahiro Yamada's avatar
      modpost: merge two similar section mismatch warnings · 78dac1a2
      Masahiro Yamada authored
      In case of section mismatch, modpost shows slightly different messages.
      
      For extable section mismatch:
      
       "%s(%s+0x%lx): Section mismatch in reference to the %s:%s\n"
      
      For the other cases:
      
       "%s: section mismatch in reference: %s (section: %s) -> %s (section: %s)\n"
      
      They are similar. Merge them.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      78dac1a2
    • Masahiro Yamada's avatar
      kbuild: implement CONFIG_TRIM_UNUSED_KSYMS without recursion · 5e9e95cc
      Masahiro Yamada authored
      When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses
      the directory tree to determine which EXPORT_SYMBOL to trim. If an
      EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the
      second traverse, where some source files are recompiled with their
      EXPORT_SYMBOL() tuned into a no-op.
      
      Linus stated negative opinions about this slowness in commits:
      
       - 5cf0fd59 ("Kbuild: disable TRIM_UNUSED_KSYMS option")
       - a555bdd0 ("Kbuild: enable TRIM_UNUSED_KSYMS again, with some guarding")
      
      We can do this better now. The final data structures of EXPORT_SYMBOL
      are generated by the modpost stage, so modpost can selectively emit
      KSYMTAB entries that are really used by modules.
      
      Commit f73edc89 ("kbuild: unify two modpost invocations") is another
      ground-work to do this in a one-pass algorithm. With the list of modules,
      modpost sets sym->used if it is used by a module. modpost emits KSYMTAB
      only for symbols with sym->used==true.
      
      BTW, Nicolas explained why the trimming was implemented with recursion:
      
        https://lore.kernel.org/all/2o2rpn97-79nq-p7s2-nq5-8p83391473r@syhkavp.arg/
      
      Actually, we never achieved that level of optimization where the chain
      reaction of trimming comes into play because:
      
       - CONFIG_LTO_CLANG cannot remove any unused symbols
       - CONFIG_LD_DEAD_CODE_DATA_ELIMINATION is enabled only for vmlinux,
         but not modules
      
      If deeper trimming is required, we need to revisit this, but I guess
      that is unlikely to happen.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      5e9e95cc
    • Masahiro Yamada's avatar
      modpost: use null string instead of NULL pointer for default namespace · 700c48b4
      Masahiro Yamada authored
      The default namespace is the null string, "".
      
      When set, the null string "" is converted to NULL:
      
        s->namespace = namespace[0] ? NOFAIL(strdup(namespace)) : NULL;
      
      When printed, the NULL pointer is get back to the null string:
      
        sym->namespace ?: ""
      
      This saves 1 byte memory allocated for "", but loses the readability.
      
      In kernel-space, we strive to save memory, but modpost is a userspace
      tool used to build the kernel. On modern systems, such small piece of
      memory is not a big deal.
      
      Handle the namespace string as is.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      700c48b4
    • Masahiro Yamada's avatar
      modpost: squash sym_update_namespace() into sym_add_exported() · 6e7611c4
      Masahiro Yamada authored
      Pass a set of the name, license, and namespace to sym_add_exported().
      
      sym_update_namespace() is unneeded.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      6e7611c4
    • Masahiro Yamada's avatar
      modpost: check static EXPORT_SYMBOL* by modpost again · 6d62b1c4
      Masahiro Yamada authored
      Commit 31cb50b5 ("kbuild: check static EXPORT_SYMBOL* by script
      instead of modpost") moved the static EXPORT_SYMBOL* check from the
      mostpost to a shell script because I thought it must be checked per
      compilation unit to avoid false negatives.
      
      I came up with an idea to do this in modpost, against combined ELF
      files. The relocation entries in ELF will find the correct exported
      symbol even if there exist symbols with the same name in different
      compilation units.
      
      Again, the same sample code.
      
        Makefile:
      
          obj-y += foo1.o foo2.o
      
        foo1.c:
      
          #include <linux/export.h>
          static void foo(void) {}
          EXPORT_SYMBOL(foo);
      
        foo2.c:
      
          void foo(void) {}
      
      Then, modpost can catch it correctly.
      
          MODPOST Module.symvers
        ERROR: modpost: vmlinux: local symbol 'foo' was exported
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      6d62b1c4
    • Masahiro Yamada's avatar
      ia64,export.h: replace EXPORT_DATA_SYMBOL* with EXPORT_SYMBOL* · 7d59313f
      Masahiro Yamada authored
      With the previous refactoring, you can always use EXPORT_SYMBOL*.
      
      Replace two instances in ia64, then remove EXPORT_DATA_SYMBOL*.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      7d59313f
    • Masahiro Yamada's avatar
      kbuild: generate KSYMTAB entries by modpost · ddb5cdba
      Masahiro Yamada authored
      Commit 7b453719 ("kbuild: link symbol CRCs at final link, removing
      CONFIG_MODULE_REL_CRCS") made modpost output CRCs in the same way
      whether the EXPORT_SYMBOL() is placed in *.c or *.S.
      
      For further cleanups, this commit applies a similar approach to the
      entire data structure of EXPORT_SYMBOL().
      
      The EXPORT_SYMBOL() compilation is split into two stages.
      
      When a source file is compiled, EXPORT_SYMBOL() will be converted into
      a dummy symbol in the .export_symbol section.
      
      For example,
      
          EXPORT_SYMBOL(foo);
          EXPORT_SYMBOL_NS_GPL(bar, BAR_NAMESPACE);
      
      will be encoded into the following assembly code:
      
          .section ".export_symbol","a"
          __export_symbol_foo:
                  .asciz ""                      /* license */
                  .asciz ""                      /* name space */
                  .balign 8
                  .quad foo                      /* symbol reference */
          .previous
      
          .section ".export_symbol","a"
          __export_symbol_bar:
                  .asciz "GPL"                   /* license */
                  .asciz "BAR_NAMESPACE"         /* name space */
                  .balign 8
                  .quad bar                      /* symbol reference */
          .previous
      
      They are mere markers to tell modpost the name, license, and namespace
      of the symbols. They will be dropped from the final vmlinux and modules
      because the *(.export_symbol) will go into /DISCARD/ in the linker script.
      
      Then, modpost extracts all the information about EXPORT_SYMBOL() from the
      .export_symbol section, and generates the final C code:
      
          KSYMTAB_FUNC(foo, "", "");
          KSYMTAB_FUNC(bar, "_gpl", "BAR_NAMESPACE");
      
      KSYMTAB_FUNC() (or KSYMTAB_DATA() if it is data) is expanded to struct
      kernel_symbol that will be linked to the vmlinux or a module.
      
      With this change, EXPORT_SYMBOL() works in the same way for *.c and *.S
      files, providing the following benefits.
      
      [1] Deprecate EXPORT_DATA_SYMBOL()
      
      In the old days, EXPORT_SYMBOL() was only available in C files. To export
      a symbol in *.S, EXPORT_SYMBOL() was placed in a separate *.c file.
      arch/arm/kernel/armksyms.c is one example written in the classic manner.
      
      Commit 22823ab4 ("EXPORT_SYMBOL() for asm") removed this limitation.
      Since then, EXPORT_SYMBOL() can be placed close to the symbol definition
      in *.S files. It was a nice improvement.
      
      However, as that commit mentioned, you need to use EXPORT_DATA_SYMBOL()
      for data objects on some architectures.
      
      In the new approach, modpost checks symbol's type (STT_FUNC or not),
      and outputs KSYMTAB_FUNC() or KSYMTAB_DATA() accordingly.
      
      There are only two users of EXPORT_DATA_SYMBOL:
      
        EXPORT_DATA_SYMBOL_GPL(empty_zero_page)    (arch/ia64/kernel/head.S)
        EXPORT_DATA_SYMBOL(ia64_ivt)               (arch/ia64/kernel/ivt.S)
      
      They are transformed as follows and output into .vmlinux.export.c
      
        KSYMTAB_DATA(empty_zero_page, "_gpl", "");
        KSYMTAB_DATA(ia64_ivt, "", "");
      
      The other EXPORT_SYMBOL users in ia64 assembly are output as
      KSYMTAB_FUNC().
      
      EXPORT_DATA_SYMBOL() is now deprecated.
      
      [2] merge <linux/export.h> and <asm-generic/export.h>
      
      There are two similar header implementations:
      
        include/linux/export.h        for .c files
        include/asm-generic/export.h  for .S files
      
      Ideally, the functionality should be consistent between them, but they
      tend to diverge.
      
      Commit 8651ec01 ("module: add support for symbol namespaces.") did
      not support the namespace for *.S files.
      
      This commit shifts the essential implementation part to C, which supports
      EXPORT_SYMBOL_NS() for *.S files.
      
      <asm/export.h> and <asm-generic/export.h> will remain as a wrapper of
      <linux/export.h> for a while.
      
      They will be removed after #include <asm/export.h> directives are all
      replaced with #include <linux/export.h>.
      
      [3] Implement CONFIG_TRIM_UNUSED_KSYMS in one-pass algorithm (by a later commit)
      
      When CONFIG_TRIM_UNUSED_KSYMS is enabled, Kbuild recursively traverses
      the directory tree to determine which EXPORT_SYMBOL to trim. If an
      EXPORT_SYMBOL turns out to be unused by anyone, Kbuild begins the
      second traverse, where some source files are recompiled with their
      EXPORT_SYMBOL() tuned into a no-op.
      
      We can do this better now; modpost can selectively emit KSYMTAB entries
      that are really used by modules.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      ddb5cdba
  6. 15 Jun, 2023 1 commit
  7. 14 Jun, 2023 4 commits
    • Masahiro Yamada's avatar
      ARC: define ASM_NL and __ALIGN(_STR) outside #ifdef __ASSEMBLY__ guard · 92e2921e
      Masahiro Yamada authored
      ASM_NL is useful not only in *.S files but also in .c files for using
      inline assembler in C code.
      
      On ARC, however, ASM_NL is evaluated inconsistently. It is expanded to
      a backquote (`) in *.S files, but a semicolon (;) in *.c files because
      arch/arc/include/asm/linkage.h defines it inside #ifdef __ASSEMBLY__,
      so the definition for C code falls back to the default value defined in
      include/linux/linkage.h.
      
      If ASM_NL is used in inline assembler in .c files, it will result in
      wrong assembly code because a semicolon is not an instruction separator,
      but the start of a comment for ARC.
      
      Move ASM_NL (also __ALIGN and __ALIGN_STR) out of the #ifdef.
      
      Fixes: 9df62f05 ("arch: use ASM_NL instead of ';' for assembler new line character in the macro")
      Fixes: 8d92e992 ("ARC: define __ALIGN_STR and __ALIGN symbols for ARC")
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      92e2921e
    • Masahiro Yamada's avatar
      scripts/kallsyms: remove KSYM_NAME_LEN_BUFFER · 1c975da5
      Masahiro Yamada authored
      You do not need to decide the buffer size statically.
      
      Use getline() to grow the line buffer as needed.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <n.schier@avm.de>
      1c975da5
    • Masahiro Yamada's avatar
      scripts/kallsyms: constify long_options · 92e74fb6
      Masahiro Yamada authored
      getopt_long() does not modify this.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <n.schier@avm.de>
      92e74fb6
    • Masahiro Yamada's avatar
      Revert "[PATCH] uml: export symbols added by GCC hardened" · 8635e8df
      Masahiro Yamada authored
      This reverts commit cead61a6.
      
      It exported __stack_smash_handler and __guard, while they may not be
      defined by anyone.
      
      The code *declares* __stack_smash_handler and __guard. It does not
      create weak symbols. If no external library is linked, they are left
      undefined, but yet exported.
      
      If a loadable module tries to access non-existing symbols, bad things
      (a page fault, NULL pointer dereference, etc.) will happen. So, the
      current code is wrong and dangerous.
      
      If the code were written as follows, it would *define* them as weak
      symbols so modules would be able to get access to them.
      
        void (*__stack_smash_handler)(void *) __attribute__((weak));
        EXPORT_SYMBOL(__stack_smash_handler);
      
        long __guard __attribute__((weak));
        EXPORT_SYMBOL(__guard);
      
      In fact, modpost forbids exporting undefined symbols. It shows an error
      message if it detects such a mistake.
      
        ERROR: modpost: "..." [...] was exported without definition
      
      Unfortunately, it is checked only when the code is built as modular.
      The problem described above has been unnoticed for a long time because
      arch/um/os-Linux/user_syms.c is always built-in.
      
      With a planned change in Kbuild, exporting undefined symbols will always
      result in a build error instead of a run-time error. It is a good thing,
      but we need to fix the breakage in advance.
      
      One fix is to define weak symbols as shown above. An alternative is to
      export them conditionally as follows:
      
        #ifdef CONFIG_STACKPROTECTOR
        extern void __stack_smash_handler(void *);
        EXPORT_SYMBOL(__stack_smash_handler);
      
        external long __guard;
        EXPORT_SYMBOL(__guard);
        #endif
      
      This is what other architectures do; EXPORT_SYMBOL(__stack_chk_guard)
      is guarded by #ifdef CONFIG_STACKPROTECTOR.
      
      However, adding the #ifdef guard is not sensible because UML cannot
      enable the stack-protector in the first place! (Please note UML does
      not select HAVE_STACKPROTECTOR in Kconfig.)
      
      So, the code is already broken (and unused) in multiple ways.
      
      Just remove.
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      8635e8df
  8. 10 Jun, 2023 2 commits
  9. 08 Jun, 2023 2 commits
  10. 07 Jun, 2023 4 commits
  11. 06 Jun, 2023 1 commit
  12. 05 Jun, 2023 4 commits
    • Masahiro Yamada's avatar
      kbuild: add $(CLANG_FLAGS) to KBUILD_CPPFLAGS · feb843a4
      Masahiro Yamada authored
      When preprocessing arch/*/kernel/vmlinux.lds.S, the target triple is
      not passed to $(CPP) because we add it only to KBUILD_{C,A}FLAGS.
      
      As a result, the linker script is preprocessed with predefined macros
      for the build host instead of the target.
      
      Assuming you use an x86 build machine, compare the following:
      
       $ clang -dM -E -x c /dev/null
       $ clang -dM -E -x c /dev/null -target aarch64-linux-gnu
      
      There is no actual problem presumably because our linker scripts do not
      rely on such predefined macros, but it is better to define correct ones.
      
      Move $(CLANG_FLAGS) to KBUILD_CPPFLAGS, so that all *.c, *.S, *.lds.S
      will be processed with the proper target triple.
      
      [Note]
      After the patch submission, we got an actual problem that needs this
      commit. (CBL issue 1859)
      
      Link: https://github.com/ClangBuiltLinux/linux/issues/1859Reported-by: default avatarTom Rini <trini@konsulko.com>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNathan Chancellor <nathan@kernel.org>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      feb843a4
    • Nathan Chancellor's avatar
      kbuild: Add CLANG_FLAGS to as-instr · cff6e7f5
      Nathan Chancellor authored
      A future change will move CLANG_FLAGS from KBUILD_{A,C}FLAGS to
      KBUILD_CPPFLAGS so that '--target' is available while preprocessing.
      When that occurs, the following errors appear multiple times when
      building ARCH=powerpc powernv_defconfig:
      
        ld.lld: error: vmlinux.a(arch/powerpc/kernel/head_64.o):(.text+0x12d4): relocation R_PPC64_ADDR16_HI out of range: -4611686018409717520 is not in [-2147483648, 2147483647]; references '__start___soft_mask_table'
        ld.lld: error: vmlinux.a(arch/powerpc/kernel/head_64.o):(.text+0x12e8): relocation R_PPC64_ADDR16_HI out of range: -4611686018409717392 is not in [-2147483648, 2147483647]; references '__stop___soft_mask_table'
      
      Diffing the .o.cmd files reveals that -DHAVE_AS_ATHIGH=1 is not present
      anymore, because as-instr only uses KBUILD_AFLAGS, which will no longer
      contain '--target'.
      
      Mirror Kconfig's as-instr and add CLANG_FLAGS explicitly to the
      invocation to ensure the target information is always present.
      Signed-off-by: default avatarNathan Chancellor <nathan@kernel.org>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      cff6e7f5
    • Nathan Chancellor's avatar
      powerpc/vdso: Include CLANG_FLAGS explicitly in ldflags-y · a7e5eb53
      Nathan Chancellor authored
      A future change will move CLANG_FLAGS from KBUILD_{A,C}FLAGS to
      KBUILD_CPPFLAGS so that '--target' is available while preprocessing.
      When that occurs, the following error appears when building the compat
      PowerPC vDSO:
      
        clang: error: unsupported option '-mbig-endian' for target 'x86_64-pc-linux-gnu'
        make[3]: *** [.../arch/powerpc/kernel/vdso/Makefile:76: arch/powerpc/kernel/vdso/vdso32.so.dbg] Error 1
      
      Explicitly add CLANG_FLAGS to ldflags-y, so that '--target' will always
      be present.
      Signed-off-by: default avatarNathan Chancellor <nathan@kernel.org>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      a7e5eb53
    • Nathan Chancellor's avatar
      mips: Include KBUILD_CPPFLAGS in CHECKFLAGS invocation · 08f6554f
      Nathan Chancellor authored
      A future change will move CLANG_FLAGS from KBUILD_{A,C}FLAGS to
      KBUILD_CPPFLAGS so that '--target' is available while preprocessing.
      When that occurs, the following error appears when building ARCH=mips
      with clang (tip of tree error shown):
      
        clang: error: unsupported option '-mabi=' for target 'x86_64-pc-linux-gnu'
      
      Add KBUILD_CPPFLAGS in the CHECKFLAGS invocation to keep everything
      working after the move.
      Signed-off-by: default avatarNathan Chancellor <nathan@kernel.org>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      08f6554f