Skip to content
Snippets Groups Projects
  1. Oct 14, 2022
    • Nick Desaulniers's avatar
      kbuild: add -fno-discard-value-names to cmd_cc_ll_c · c67a85be
      Nick Desaulniers authored
      When debugging LLVM IR, it can be handy for clang to not discard value
      names used for local variables and parameters. Compare the generated IR.
      
      -fdiscard-value-names:
        define i32 @core_sys_select(i32 %0, ptr %1, ptr %2, ptr %3, ptr %4) {
          %6 = alloca i64
          %7 = alloca %struct.poll_wqueues
          %8 = alloca [64 x i32]
      
      -fno-discard-value-names:
        define i32 @core_sys_select(i32 %n, ptr %inp, ptr %outp, ptr %exp,
                                    ptr %end_time) {
          %expire.i = alloca i64
          %table.i = alloca %struct.poll_wqueues
          %stack_fds = alloca [64 x i32]
      
      The rule for generating human readable LLVM IR (.ll) is only useful as a
      debugging feature:
      
      $ make LLVM=1 fs/select.ll
      
      As Fangrui notes:
        A LLVM_ENABLE_ASSERTIONS=off build of Clang defaults to
        -fdiscard-value-names.
      
        A LLVM_ENABLE_ASSERTIONS=on build of Clang defaults to
        -fno-discard-value-names.
      
      Explicitly enable -fno-discard-value-names so that the IR always contains
      value names regardless of whether assertions were enabled or not.
      Assertions generally are not enabled in releases of clang packaged by
      distributions.
      
      Link: https://github.com/ClangBuiltLinux/linux/issues/1467
      
      
      Reviewed-by: default avatarNathan Chancellor <nathan@kernel.org>
      Reviewed-by: default avatarFangrui Song <maskray@google.com>
      Signed-off-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      c67a85be
  2. Sep 28, 2022
  3. Jul 27, 2022
  4. Jun 07, 2022
    • Kevin Locke's avatar
      kbuild: avoid regex RS for POSIX awk · 7bf179de
      Kevin Locke authored
      In 22f26f21 awk was added to deduplicate *.mod files.  The awk
      invocation passes -v RS='( |\n)' to match a space or newline character
      as the record separator.  Unfortunately, POSIX states[1]
      
      > If RS contains more than one character, the results are unspecified.
      
      Some implementations (such as the One True Awk[2] used by the BSDs) do
      not treat RS as a regular expression.  When awk does not support regex
      RS, build failures such as the following are produced (first error using
      allmodconfig):
      
            CC [M]  arch/x86/events/intel/uncore.o
            CC [M]  arch/x86/events/intel/uncore_nhmex.o
            CC [M]  arch/x86/events/intel/uncore_snb.o
            CC [M]  arch/x86/events/intel/uncore_snbep.o
            CC [M]  arch/x86/events/intel/uncore_discovery.o
            LD [M]  arch/x86/events/intel/intel-uncore.o
          ld: cannot find uncore_nhmex.o: No such file or directory
          ld: cannot find uncore_snb.o: No such file or directory
          ld: cannot find uncore_snbep.o: No such file or directory
          ld: cannot find uncore_discovery.o: No such file or directory
          make[3]: *** [scripts/Makefile.build:422: arch/x86/events/intel/intel-uncore.o] Error 1
          make[2]: *** [scripts/Makefile.build:487: arch/x86/events/intel] Error 2
          make[1]: *** [scripts/Makefile.build:487: arch/x86/events] Error 2
          make: *** [Makefile:1839: arch/x86] Error 2
      
      To avoid this, use printf(1) to produce a newline between each object
      path, instead of the space produced by echo(1), so that the default RS
      can be used by awk.
      
      [1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html
      [2]: https://github.com/onetrueawk/awk
      
      
      
      Fixes: 22f26f21 ("kbuild: get rid of duplication in *.mod files")
      Signed-off-by: Kevin Locke's avatarKevin Locke <kevin@kevinlocke.name>
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      7bf179de
  5. Jun 04, 2022
    • Masahiro Yamada's avatar
      kbuild: factor out the common objtool arguments · b42d2306
      Masahiro Yamada authored
      
      scripts/Makefile.build and scripts/link-vmlinux.sh have similar setups
      for the objtool arguments.
      
      It was difficult to factor out them because all the vmlinux build rules
      were written in a shell script. It is somewhat tedious to touch the two
      files every time a new objtool option is supported.
      
      To reduce the code duplication, move the objtool for vmlinux.o into
      scripts/Makefile.vmlinux_o. Then, move the common macros to Makefile.lib
      so they are shared between Makefile.build and Makefile.vmlinux_o.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
      b42d2306
  6. Jun 01, 2022
    • Masahiro Yamada's avatar
      kbuild: rebuild multi-object modules when objtool is updated · f6b66ca4
      Masahiro Yamada authored
      
      When CONFIG_LTO_CLANG or CONFIG_X86_KERNEL_IBT is enabled, objtool for
      multi-object modules is postponed until the objects are linked together.
      
      Make sure to re-run objtool and re-link multi-object modules when
      objtool is updated.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarKees Cook <keescook@chromium.org>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      Reviewed-by: default avatarNicolas Schier <n.schier@avm.de>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
      f6b66ca4
    • Masahiro Yamada's avatar
      kbuild: make *.mod rule robust against too long argument error · c6031b1d
      Masahiro Yamada authored
      Like built-in.a, the command length of the *.mod rule scales with
      the depth of the directory times the number of objects in the Makefile.
      
      Add $(obj)/ by the shell command (awk) instead of by Make's builtin
      function.
      
      In-tree modules still have some room to the limit (ARG_MAX=2097152),
      but this is more future-proof for big modules in a deep directory.
      
      For example, you can build i915 as a module (CONFIG_DRM_I915=m) and
      compare drivers/gpu/drm/i915/.i915.mod.cmd with/without this commit.
      
      The issue is more critical for external modules because the M= path
      can be very long as Jeff Johnson reported before [1].
      
      [1] https://lore.kernel.org/linux-kbuild/4c02050c4e95e4cb8cc04282695f8404@codeaurora.org/
      
      
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
      c6031b1d
    • Masahiro Yamada's avatar
      kbuild: make built-in.a rule robust against too long argument error · cd968b97
      Masahiro Yamada authored
      
      Kbuild runs at the top of objtree instead of changing the working
      directory to subdirectories. I think this design is nice overall but
      some commands have a scalability issue.
      
      The build command of built-in.a is one of them whose length scales with:
      
          O(D * N)
      
      Here, D is the length of the directory path (i.e. $(obj)/ prefix),
      N is the number of objects in the Makefile, O() is the big O notation.
      
      The deeper directory the Makefile directory is located, the more easily
      it will hit the too long argument error.
      
      We can make it better. Trim the $(obj)/ by Make's builtin function, and
      restore it by a shell command (sed).
      
      With this, the command length scales with:
      
          O(D + N)
      
      In-tree modules still have some room to the limit (ARG_MAX=2097152),
      but this is more future-proof for big modules in a deep directory.
      
      For example, you can build i915 as builtin (CONFIG_DRM_I915=y) and
      compare drivers/gpu/drm/i915/.built-in.a.cmd with/without this commit.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
      cd968b97
    • Masahiro Yamada's avatar
      kbuild: check static EXPORT_SYMBOL* by script instead of modpost · 31cb50b5
      Masahiro Yamada authored
      
      The 'static' specifier and EXPORT_SYMBOL() are an odd combination.
      
      Commit 15bfc234 ("modpost: check for static EXPORT_SYMBOL*
      functions") tried to detect it, but this check has false negatives.
      
      Here is the 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) {}
      
      foo1.c exports the static symbol 'foo', but modpost cannot catch it
      because it is fooled by foo2.c, which has a global symbol with the
      same name.
      
      s->is_static is cleared if a global symbol with the same name is found
      somewhere, but EXPORT_SYMBOL() and the global symbol do not necessarily
      belong to the same compilation unit.
      
      This check should be done per compilation unit, but I do not know how
      to do it in modpost. modpost runs against vmlinux.o or modules, which
      merges multiple objects, then forgets their origin.
      
      modpost cannot parse individual objects because they may not be ELF but
      LLVM IR when CONFIG_LTO_CLANG=y.
      
      Add a simple bash script to parse the output from ${NM}. This works for
      CONFIG_LTO_CLANG=y because llvm-nm can dump symbols of LLVM IR files.
      
      Revert 15bfc234.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
      31cb50b5
  7. May 29, 2022
    • Masahiro Yamada's avatar
      kbuild: do not create *.prelink.o for Clang LTO or IBT · c25e1c55
      Masahiro Yamada authored
      
      When CONFIG_LTO_CLANG=y, additional intermediate *.prelink.o is created
      for each module. Also, objtool is postponed until LLVM IR is converted
      to ELF.
      
      CONFIG_X86_KERNEL_IBT works in a similar way to postpone objtool until
      objects are merged together.
      
      This commit stops generating *.prelink.o, so the build flow will look
      similar with/without LTO.
      
      The following figures show how the LTO build currently works, and
      how this commit is changing it.
      
      Current build flow
      ==================
      
       [1] single-object module
      
                                            $(LD)
                 $(CC)                     +objtool              $(LD)
          foo.c --------------------> foo.o -----> foo.prelink.o -----> foo.ko
                                    (LLVM IR)          (ELF)       |    (ELF)
                                                                   |
                                                       foo.mod.o --/
                                                       (LLVM IR)
      
       [2] multi-object module
                                            $(LD)
                 $(CC)         $(AR)       +objtool               $(LD)
          foo1.c -----> foo1.o -----> foo.o -----> foo.prelink.o -----> foo.ko
                                 |  (archive)          (ELF)       |    (ELF)
          foo2.c -----> foo2.o --/                                 |
                       (LLVM IR)                       foo.mod.o --/
                                                       (LLVM IR)
      
        One confusion is that foo.o in multi-object module is an archive
        despite of its suffix.
      
      New build flow
      ==============
      
       [1] single-object module
      
        Since there is only one object, there is no need to keep the LLVM IR.
        Use $(CC)+$(LD) to generate an ELF object in one build rule. When LTO
        is disabled, $(LD) is unneeded because $(CC) produces an ELF object.
      
                     $(CC)+$(LD)+objtool              $(LD)
          foo.c ----------------------------> foo.o ---------> foo.ko
                                              (ELF)     |      (ELF)
                                                        |
                                            foo.mod.o --/
                                            (LLVM IR)
      
       [2] multi-object module
      
        Previously, $(AR) was used to combine LLVM IR files into an archive,
        but there was no technical reason to do so. Use $(LD) to merge them
        into a single ELF object.
      
                                     $(LD)
                   $(CC)            +objtool          $(LD)
          foo1.c ---------> foo1.o ---------> foo.o ---------> foo.ko
                                       |      (ELF)     |      (ELF)
          foo2.c ---------> foo2.o ----/                |
                           (LLVM IR)        foo.mod.o --/
                                            (LLVM IR)
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      Reviewed-by: default avatarSami Tolvanen <samitolvanen@google.com>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14 (x86-64)
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@kernel.org>
      c25e1c55
    • Masahiro Yamada's avatar
      kbuild: replace $(linked-object) with CONFIG options · 0cfd9006
      Masahiro Yamada authored
      
      *.prelink.o is created when CONFIG_LTO_CLANG or CONFIG_X86_KERNEL_IBT
      is enabled.
      
      Replace $(linked-object) with $(CONFIG_LTO_CLANG)$(CONFIG_X86_KERNEL_IBT)
      so you will get a quick idea of when the --link option is passed.
      
      No functional change is intended.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Acked-by: default avatarJosh Poimboeuf <jpoimboe@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM-14
      0cfd9006
  8. May 27, 2022
  9. May 24, 2022
  10. May 11, 2022
    • Masahiro Yamada's avatar
      kbuild: record symbol versions in *.cmd files · 78e9e56a
      Masahiro Yamada authored
      
      When CONFIG_MODVERSIONS=y, the output from genksyms is saved in
      separate *.symversions files, and will be used much later when
      CONFIG_LTO_CLANG=y because it is impossible to update LLVM bit code
      here.
      
      This approach is not robust because:
      
       - *.symversions may or may not exist. If *.symversions does not
         exist, we never know if it is missing for legitimate reason
         (i.e. no EXPORT_SYMBOL) or something bad has happened (for
         example, the user accidentally deleted it). Once it occurs,
         it is not self-healing because *.symversions is generated
         as a side effect.
      
       - stale (i.e. invalid) *.symversions might be picked up if an
         object is generated in a non-ordinary way, and corresponding
         *.symversions (, which was generated by old builds) just happen
         to exist.
      
      A more robust approach is to save symbol versions in *.cmd files
      because:
      
       - *.cmd always exists (if the object is generated by if_changed
         rule or friends). Even if the user accidentally deletes it,
         it will be regenerated in the next build.
      
       - *.cmd is always re-generated when the object is updated. This
         avoid stale version information being picked up.
      
      I will remove *.symversions later.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Tested-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      Tested-by: default avatarNathan Chancellor <nathan@kernel.org>
      78e9e56a
  11. May 07, 2022
    • Masahiro Yamada's avatar
      kbuild: read *.mod to get objects passed to $(LD) or $(AR) · feb7d79f
      Masahiro Yamada authored
      
      ld and ar support @file, which command-line options are read from.
      
      Now that *.mod lists the member objects in the correct order, without
      duplication, it is ready to be passed to ld and ar.
      
      By using the @file syntax, people will not be worried about the pitfall
      described in the NOTE.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      feb7d79f
    • Masahiro Yamada's avatar
      kbuild: make *.mod not depend on *.o · fc93a4cd
      Masahiro Yamada authored
      
      The dependency
      
          $(obj)/%.mod: $(obj)/%$(mod-prelink-ext).o
      
      ... exists because *.mod files previously contained undefined symbols,
      which are computed from *.o files when CONFIG_TRIM_UNUSED_KSYMS=y.
      
      Now that the undefined symbols are put into separate *.usyms files,
      there is no reason to make *.mod depend on *.o files.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      fc93a4cd
    • Masahiro Yamada's avatar
      kbuild: get rid of duplication in *.mod files · 22f26f21
      Masahiro Yamada authored
      
      It is allowed to add the same objects multiple times to obj-y / obj-m:
      
        obj-y += foo.o foo.o foo.o
        obj-m += bar.o bar.o bar.o
      
      It is also allowed to add the same objects multiple times to a composite
      module:
      
        obj-m += foo.o
        foo-y := foo1.o foo2.o foo2.o foo1.o
      
      This flexibility is useful because the same object might be selected by
      different CONFIG options, like this:
      
        obj-m               += foo.o
        foo-y               := foo1.o
        foo-$(CONFIG_FOO_X) += foo2.o
        foo-$(CONFIG_FOO_Y) += foo2.o
      
      The duplicated objects are omitted at link time. It works naturally in
      Makefiles because GNU Make removes duplication in $^ without changing
      the order.
      
      It is working well, almost...
      
      A small flaw I notice is, *.mod contains duplication in such a case.
      
      This is probably not a big deal. As far as I know, the only small
      problem is scripts/mod/sumversion.c parses the same file multiple
      times.
      
      I am fixing this because I plan to reuse *.mod for other purposes,
      where the duplication can be problematic.
      
      The code change is quite simple. We already use awk to drop duplicated
      lines in modules.order (see cmd_modules_order in the same file).
      I copied the code, but changed RS to use spaces as record separators.
      
      I also changed the file format to list one object per line.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      22f26f21
    • Masahiro Yamada's avatar
      kbuild: split the second line of *.mod into *.usyms · 9413e764
      Masahiro Yamada authored
      
      The *.mod files have two lines; the first line lists the member objects
      of the module, and the second line, if CONFIG_TRIM_UNUSED_KSYMS=y, lists
      the undefined symbols.
      
      Currently, we generate *.mod after constructing composite modules,
      otherwise, we cannot compute the second line. No prerequisite is
      required to print the first line.
      
      They are orthogonal. Splitting them into separate commands will ease
      further cleanups.
      
      This commit splits the list of undefined symbols out to *.usyms files.
      
      Previously, the list of undefined symbols ended up with a very long
      line, but now it has one symbol per line.
      
      Use sed like we did before commit 7d32358b ("kbuild: avoid split
      lines in .mod files").
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      9413e764
    • Masahiro Yamada's avatar
      kbuild: reuse real-search to simplify cmd_mod · b3591e06
      Masahiro Yamada authored
      
      The first command in cmd_mod is similar to the real-search macro.
      Reuse it.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      b3591e06
    • Masahiro Yamada's avatar
      kbuild: refactor cmd_modversions_S · 7cfa2fcb
      Masahiro Yamada authored
      
      Split the code into two macros, cmd_gen_symversions_S for running
      genksyms, and cmd_modversions for running $(LD) to update the object
      with CRCs.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      7cfa2fcb
    • Masahiro Yamada's avatar
      kbuild: refactor cmd_modversions_c · 8017ce50
      Masahiro Yamada authored
      
      cmd_modversions_c implements two parts; run genksyms to calculate CRCs
      of exported symbols, run $(LD) to update the object with the CRCs. The
      latter is not executed for CONFIG_LTO_CLANG=y since the object is not
      ELF but LLVM bit code at this point.
      
      The first part can be unified because we can always use $(NM) instead
      of "$(OBJDUMP) -h" to dump the symbols.
      
      Split the code into the two macros, cmd_gen_symversions_c and
      cmd_modversions.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
      8017ce50
  12. Apr 22, 2022
  13. Apr 19, 2022
  14. Apr 06, 2022
  15. Apr 04, 2022
  16. Mar 22, 2022
  17. Mar 15, 2022
Loading