Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
  1. Aug 09, 2024
  2. Aug 06, 2024
  3. Jul 10, 2024
    • Miguel Ojeda's avatar
      rust: relax most deny-level lints to warnings · f8f88aa2
      Miguel Ojeda authored
      Since we are starting to support several Rust toolchains, lints (including
      Clippy ones) now may behave differently and lint groups may include
      new lints.
      
      Therefore, to maximize the chances a given version works, relax some
      deny-level lints to warnings. It may also make our lives a bit easier
      while developing new code or refactoring.
      
      To be clear, the requirements for in-tree code are still the same, since
      Rust code still needs to be warning-free (patches should be clean under
      `WERROR=y`) and the set of lints is not changed.
      
      `unsafe_op_in_unsafe_fn` is left unmodified, i.e. as an error, since it is
      becoming the default in the language (warn-by-default in Rust 2024 [1] and
      ideally an error later on) and thus it should also be very well tested. In
      addition, it is simple enough that it should not have false positives
      (unlike e.g. `rust_2018_idioms`'s `explicit_outlives_requirements`).
      
      `non_ascii_idents` is left unmodified as well, i.e. as an error, since
      it is unlikely one gains any productivity during development if it
      were a warning (in fact, it may be worse, since it is likely one made
      a typo). In addition, it should not have false positives.
      
      Finally, put the two `-D` ones at the top and take the chance to do one
      per line.
      
      Link: https://github.com/rust-lang/rust/pull/112038
      
       [1]
      Reviewed-by: default avatarFinn Behrens <me@kloenk.dev>
      Tested-by: default avatarBenno Lossin <benno.lossin@proton.me>
      Tested-by: default avatarAndreas Hindborg <a.hindborg@samsung.com>
      Link: https://lore.kernel.org/r/20240709160615.998336-5-ojeda@kernel.org
      
      
      Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      f8f88aa2
  4. Jul 08, 2024
  5. Jul 03, 2024
  6. May 14, 2024
  7. May 09, 2024
    • Masahiro Yamada's avatar
      kbuild: use $(src) instead of $(srctree)/$(src) for source directory · b1992c37
      Masahiro Yamada authored
      Kbuild conventionally uses $(obj)/ for generated files, and $(src)/ for
      checked-in source files. It is merely a convention without any functional
      difference. In fact, $(obj) and $(src) are exactly the same, as defined
      in scripts/Makefile.build:
      
          src := $(obj)
      
      When the kernel is built in a separate output directory, $(src) does
      not accurately reflect the source directory location. While Kbuild
      resolves this discrepancy by specifying VPATH=$(srctree) to search for
      source files, it does not cover all cases. For example, when adding a
      header search path for local headers, -I$(srctree)/$(src) is typically
      passed to the compiler.
      
      This introduces inconsistency between upstream and downstream Makefiles
      because $(src) is used instead of $(srctree)/$(src) for the latter.
      
      To address this inconsistency, this commit changes the semantics of
      $(src) so that it always points to the directory in the source tree.
      
      Going forward, the variables used ...
      b1992c37
  8. Apr 22, 2024
  9. Apr 16, 2024
  10. Mar 10, 2024
    • Masahiro Yamada's avatar
      kbuild: unexport abs_srctree and abs_objtree · e2bad142
      Masahiro Yamada authored
      Commit 25b146c5 ("kbuild: allow Kbuild to start from any directory")
      exported abs_srctree and abs_objtree to avoid recomputation after the
      sub-make. However, this approach turned out to be fragile.
      
      Commit 5fa94ceb
      
       ("kbuild: set correct abs_srctree and abs_objtree
      for package builds") moved them above "ifneq ($(sub_make_done),1)",
      eliminating the need for exporting them.
      
      These are only needed in the top Makefile. If an absolute path is
      required in sub-directories, you can use $(abspath ) or $(realpath )
      as needed.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarNicolas Schier <nicolas@fjasle.eu>
      e2bad142
  11. Feb 29, 2024
    • Miguel Ojeda's avatar
      kbuild: mark `rustc` (and others) invocations as recursive · ecab4115
      Miguel Ojeda authored
      `rustc` (like Cargo) may take advantage of the jobserver at any time
      (e.g. for backend parallelism, or eventually frontend too). In the kernel,
      we call `rustc` with `-Ccodegen-units=1` (and `-Zthreads` is 1 so far),
      so we do not expect parallelism. However, in the upcoming Rust 1.76.0, a
      warning is emitted by `rustc` [1] when it cannot connect to the jobserver
      it was passed (in many cases, but not all: compiling and `--print sysroot`
      do, but `--version` does not). And given GNU Make always passes
      the jobserver in the environment variable (even when a line is deemed
      non-recursive), `rustc` will end up complaining about it (in particular
      in Make 4.3 where there is only the simple pipe jobserver style).
      
      One solution is to remove the jobserver from `MAKEFLAGS`. However, we
      can mark the lines with calls to `rustc` (and Cargo) as recursive, which
      looks simpler. This is being documented as a recommendation in `rustc`
      [2] and allows us to be ready for the time we may use parallelism inside
      `rustc` (potentially now, if a user passes `-Zthreads`). Thus do so.
      
      Similarly, do the same for `rustdoc` and `cargo` calls.
      
      Finally, there is one case that the solution does not cover, which is the
      `$(shell ...)` call we have. Thus, for that one, set an empty `MAKEFLAGS`
      environment variable.
      
      Link: https://github.com/rust-lang/rust/issues/120515
      
       [1]
      Acked-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Link: https://github.com/rust-lang/rust/pull/121564 [2]
      Link: https://lore.kernel.org/r/20240217002638.57373-1-ojeda@kernel.org
      
      
      [ Reworded to add link to PR documenting the recommendation. ]
      Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      ecab4115
  12. Feb 09, 2024
  13. Dec 21, 2023
  14. Dec 14, 2023
  15. Oct 19, 2023
  16. Oct 15, 2023
  17. Oct 14, 2023
  18. Oct 12, 2023
    • Andrea Righi's avatar
      rust: fix bindgen build error with fstrict-flex-arrays · 344b6c0a
      Andrea Righi authored
      Commit df8fc4e9 ("kbuild: Enable -fstrict-flex-arrays=3") enabled
      '-fstrict-flex-arrays=3' globally, but bindgen does not recognized this
      compiler option, triggering the following build error:
      
       error: unknown argument: '-fstrict-flex-arrays=3', err: true
      
      [ Miguel: Commit df8fc4e9 ("kbuild: Enable -fstrict-flex-arrays=3")
        did it so only conditionally (i.e. only if the C compiler supports
        it). This explains what Andrea was seeing: he was  compiling with a
        modern enough GCC, which enables the option, but with an old enough
        Clang. Andrea confirmed this was the case: he was using Clang 14 with
        GCC 13; and that Clang 15 worked for him.
      
        While it is possible to construct code (see mailing list for an
        example I came up with) where this could break, it is fairly
        contrived, and anyway GCC-built kernels with Rust enabled should
        only be used for experimentation until we get support for
        `rustc_codegen_gcc` and/or GCC Rust. So let's add this for the
        time being in case it helps somebody. ]
      
      Add '-fstrict-flex-arrays' to the list of cflags that should be ignored
      by bindgen.
      
      Fixes: df8fc4e9
      
       ("kbuild: Enable -fstrict-flex-arrays=3")
      Signed-off-by: default avatarAndrea Righi <andrea.righi@canonical.com>
      Tested-by: default avatarGary Guo <gary@garyguo.net>
      Link: https://lore.kernel.org/r/20230815065346.131387-1-andrea.righi@canonical.com
      
      
      Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      344b6c0a
  19. Aug 20, 2023
  20. Aug 14, 2023
    • Aakash Sen Sharma's avatar
      rust: bindgen: upgrade to 0.65.1 · 08ab7865
      Aakash Sen Sharma authored
      In LLVM 16, anonymous items may return names like `(unnamed union at ..)`
      rather than empty names [1], which breaks Rust-enabled builds because
      bindgen assumed an empty name instead of detecting them via
      `clang_Cursor_isAnonymous` [2]:
      
          $ make rustdoc LLVM=1 CLIPPY=1 -j$(nproc)
            RUSTC L rust/core.o
            BINDGEN rust/bindings/bindings_generated.rs
            BINDGEN rust/bindings/bindings_helpers_generated.rs
            BINDGEN rust/uapi/uapi_generated.rs
          thread 'main' panicked at '"ftrace_branch_data_union_(anonymous_at__/_/include/linux/compiler_types_h_146_2)" is not a valid Ident', .../proc-macro2-1.0.24/src/fallback.rs:693:9
          ...
          thread 'main' panicked at '"ftrace_branch_data_union_(anonymous_at__/_/include/linux/compiler_types_h_146_2)" is not a valid Ident', .../proc-macro2-1.0.24/src/fallback.rs:693:9
          ...
      
      This was fixed in bindgen 0.62.0. Therefore, upgrade bindgen to
      a more recent version, 0.65.1, to support LLVM 16.
      
      Since bindgen 0.58.0 changed the `--{white,black}list-*` flags to
      `--{allow,block}list-*` [3], update them on our side too.
      
      In addition, bindgen 0.61.0 moved its CLI utility into a binary crate
      called `bindgen-cli` [4]. Thus update the installation command in the
      Quick Start guide.
      
      Moreover, bindgen 0.61.0 changed the default functionality to bind
      `size_t` to `usize` [5] and added the `--no-size_t-is-usize` flag
      to not bind `size_t` as `usize`. Then bindgen 0.65.0 removed
      the `--size_t-is-usize` flag [6]. Thus stop passing the flag to bindgen.
      
      Finally, bindgen 0.61.0 added support for the `noreturn` attribute (in
      its different forms) [7]. Thus remove the infinite loop in our Rust
      panic handler after calling `BUG()`, since bindgen now correctly
      generates a `BUG()` binding that returns `!` instead of `()`.
      
      Link: https://github.com/llvm/llvm-project/commit/19e984ef8f49bc3ccced15621989fa9703b2cd5b [1]
      Link: https://github.com/rust-lang/rust-bindgen/pull/2319 [2]
      Link: https://github.com/rust-lang/rust-bindgen/pull/1990 [3]
      Link: https://github.com/rust-lang/rust-bindgen/pull/2284 [4]
      Link: https://github.com/rust-lang/rust-bindgen/commit/cc78b6fdb6e829e5fb8fa1639f2182cb49333569 [5]
      Link: https://github.com/rust-lang/rust-bindgen/pull/2408 [6]
      Link: https://github.com/rust-lang/rust-bindgen/issues/2094
      
       [7]
      Signed-off-by: default avatarAakash Sen Sharma <aakashsensharma@gmail.com>
      Closes: https://github.com/Rust-for-Linux/linux/issues/1013
      
      
      Tested-by: default avatarAriel Miculas <amiculas@cisco.com>
      Reviewed-by: default avatarGary Guo <gary@garyguo.net>
      Link: https://lore.kernel.org/r/20230612194311.24826-1-aakashsensharma@gmail.com
      
      
      [ Reworded commit message. Mentioned the `bindgen-cli` binary crate
        change, linked to it and updated the Quick Start guide. Re-added a
        deleted "as" word in a code comment and reflowed comment to respect
        the maximum length. ]
      Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      08ab7865
    • Miguel Ojeda's avatar
      rust: upgrade to Rust 1.71.1 · 89eed1ab
      Miguel Ojeda authored
      This is the second upgrade to the Rust toolchain, from 1.68.2 to 1.71.1
      (i.e. the latest).
      
      See the upgrade policy [1] and the comments on the first upgrade in
      commit 3ed03f4d ("rust: upgrade to Rust 1.68.2").
      
      # Unstable features
      
      No unstable features (that we use) were stabilized.
      
      Therefore, the only unstable feature allowed to be used outside
      the `kernel` crate is still `new_uninit`, though other code to be
      upstreamed may increase the list.
      
      Please see [2] for details.
      
      # Required changes
      
      For the upgrade, this patch requires the following changes:
      
        - Removal of the `__rust_*` allocator functions, together with
          the addition of the `__rust_no_alloc_shim_is_unstable` static.
          See [3] for details.
      
        - Some more compiler builtins added due to `<f{32,64}>::midpoint()`
          that got added in Rust 1.71 [4].
      
      # `alloc` upgrade and reviewing
      
      The vast majority of changes are due to our `alloc` fork being upgraded
      at once.
      
      There are two kinds of changes to be aware of: the ones coming from
      upstream, which we should follow as closely as possible, and the updates
      needed in our added fallible APIs to keep them matching the newer
      infallible APIs coming from upstream.
      
      Instead of taking a look at the diff of this patch, an alternative
      approach is reviewing a diff of the changes between upstream `alloc` and
      the kernel's. This allows to easily inspect the kernel additions only,
      especially to check if the fallible methods we already have still match
      the infallible ones in the new version coming from upstream.
      
      Another approach is reviewing the changes introduced in the additions in
      the kernel fork between the two versions. This is useful to spot
      potentially unintended changes to our additions.
      
      To apply these approaches, one may follow steps similar to the following
      to generate a pair of patches that show the differences between upstream
      Rust and the kernel (for the subset of `alloc` we use) before and after
      applying this patch:
      
          # Get the difference with respect to the old version.
          git -C rust checkout $(linux/scripts/min-tool-version.sh rustc)
          git -C linux ls-tree -r --name-only HEAD -- rust/alloc |
              cut -d/ -f3- |
              grep -Fv README.md |
              xargs -IPATH cp rust/library/alloc/src/PATH linux/rust/alloc/PATH
          git -C linux diff --patch-with-stat --summary -R > old.patch
          git -C linux restore rust/alloc
      
          # Apply this patch.
          git -C linux am rust-upgrade.patch
      
          # Get the difference with respect to the new version.
          git -C rust checkout $(linux/scripts/min-tool-version.sh rustc)
          git -C linux ls-tree -r --name-only HEAD -- rust/alloc |
              cut -d/ -f3- |
              grep -Fv README.md |
              xargs -IPATH cp rust/library/alloc/src/PATH linux/rust/alloc/PATH
          git -C linux diff --patch-with-stat --summary -R > new.patch
          git -C linux restore rust/alloc
      
      Now one may check the `new.patch` to take a look at the additions (first
      approach) or at the difference between those two patches (second
      approach). For the latter, a side-by-side tool is recommended.
      
      Link: https://rust-for-linux.com/rust-version-policy [1]
      Link: https://github.com/Rust-for-Linux/linux/issues/2 [2]
      Link: https://github.com/rust-lang/rust/pull/86844 [3]
      Link: https://github.com/rust-lang/rust/pull/92048 [4]
      Closes: https://github.com/Rust-for-Linux/linux/issues/68
      
      
      Reviewed-by: default avatarMartin Rodriguez Reboredo <yakoyoku@gmail.com>
      Reviewed-by: default avatarTrevor Gross <tmgross@umich.edu>
      Link: https://lore.kernel.org/r/20230729220317.416771-1-ojeda@kernel.org
      
      
      Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      89eed1ab
  21. Aug 07, 2023
  22. Aug 04, 2023
  23. Jul 21, 2023
  24. Jul 19, 2023
    • Miguel Ojeda's avatar
      rust: support running Rust documentation tests as KUnit ones · a66d733d
      Miguel Ojeda authored
      Rust has documentation tests: these are typically examples of
      usage of any item (e.g. function, struct, module...).
      
      They are very convenient because they are just written
      alongside the documentation. For instance:
      
          /// Sums two numbers.
          ///
          /// ```
          /// assert_eq!(mymod::f(10, 20), 30);
          /// ```
          pub fn f(a: i32, b: i32) -> i32 {
              a + b
          }
      
      In userspace, the tests are collected and run via `rustdoc`.
      Using the tool as-is would be useful already, since it allows
      to compile-test most tests (thus enforcing they are kept
      in sync with the code they document) and run those that do not
      depend on in-kernel APIs.
      
      However, by transforming the tests into a KUnit test suite,
      they can also be run inside the kernel. Moreover, the tests
      get to be compiled as other Rust kernel objects instead of
      targeting userspace.
      
      On top of that, the integration with KUnit means the Rust
      support gets to reuse the existing testing facilities. ...
      a66d733d
  25. Apr 21, 2023
  26. Apr 19, 2023
  27. Apr 06, 2023
  28. Jan 22, 2023
    • Masahiro Yamada's avatar
      kbuild: rust: move rust/target.json to scripts/ · c83b16ce
      Masahiro Yamada authored
      
      scripts/ is a better place to generate files used treewide.
      
      With target.json moved to scripts/, you do not need to add target.json
      to no-clean-files or MRPROPER_FILES.
      
      'make clean' does not visit scripts/, but 'make mrproper' does.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      Tested-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      c83b16ce
    • Masahiro Yamada's avatar
      kbuild: remove sed commands after rustc rules · 2185242f
      Masahiro Yamada authored
      
      rustc may put comments in dep-info, so sed is used to drop them before
      passing it to fixdep.
      
      Now that fixdep can remove comments, Makefiles do not need to run sed.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      Tested-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      Reviewed-by: default avatarVincenzo Palazzo <vincenzopalazzodev@gmail.com>
      2185242f
    • Masahiro Yamada's avatar
      kbuild: specify output names separately for each emission type from rustc · 295d8398
      Masahiro Yamada authored
      
      In Kbuild, two different rules must not write to the same file, but
      it happens when compiling rust source files.
      
      For example, set CONFIG_SAMPLE_RUST_MINIMAL=m and run the following:
      
        $ make -j$(nproc) samples/rust/rust_minimal.o samples/rust/rust_minimal.rsi \
                          samples/rust/rust_minimal.s samples/rust/rust_minimal.ll
          [snip]
          RUSTC [M] samples/rust/rust_minimal.o
          RUSTC [M] samples/rust/rust_minimal.rsi
          RUSTC [M] samples/rust/rust_minimal.s
          RUSTC [M] samples/rust/rust_minimal.ll
        mv: cannot stat 'samples/rust/rust_minimal.d': No such file or directory
        make[3]: *** [scripts/Makefile.build:334: samples/rust/rust_minimal.ll] Error 1
        make[3]: *** Waiting for unfinished jobs....
        mv: cannot stat 'samples/rust/rust_minimal.d': No such file or directory
        make[3]: *** [scripts/Makefile.build:309: samples/rust/rust_minimal.o] Error 1
        mv: cannot stat 'samples/rust/rust_minimal.d': No such file or directory
        make[3]: *** [scripts/Makefile.build:326: samples/rust/rust_minimal.s] Error 1
        make[2]: *** [scripts/Makefile.build:504: samples/rust] Error 2
        make[1]: *** [scripts/Makefile.build:504: samples] Error 2
        make: *** [Makefile:2008: .] Error 2
      
      The reason for the error is that 4 threads running in parallel renames
      the same file, samples/rust/rust_minimal.d.
      
      This does not happen when compiling C or assembly files because
      -Wp,-MMD,$(depfile) explicitly specifies the dependency filepath.
      $(depfile) is a unique path for each target.
      
      Currently, rustc is only given --out-dir and --emit=<list-of-types>
      So, all the rust build rules output the dep-info into the default
      <CRATE_NAME>.d, which causes the path conflict.
      
      Fortunately, the --emit option is able to specify the output path
      individually, with the form --emit=<type>=<path>.
      
      Add --emit=dep-info=$(depfile) to the common part. Also, remove the
      redundant --out-dir because the output path is specified for each type.
      
      The code gets much cleaner because we do not need to rename *.d files.
      
      Signed-off-by: default avatarMasahiro Yamada <masahiroy@kernel.org>
      Reviewed-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      Tested-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      Reviewed-by: default avatarVincenzo Palazzo <vincenzopalazzodev@gmail.com>
      295d8398
  29. Jan 16, 2023
  30. Dec 03, 2022
    • Gary Guo's avatar
      rust: add `build_error` crate · ecaa6ddf
      Gary Guo authored
      
      The `build_error` crate provides a function `build_error` which
      will panic at compile-time if executed in const context and,
      by default, will cause a build error if not executed at compile
      time and the optimizer does not optimise away the call.
      
      The `CONFIG_RUST_BUILD_ASSERT_ALLOW` kernel option allows to
      relax the default build failure and convert it to a runtime
      check. If the runtime check fails, `panic!` will be called.
      
      Its functionality will be exposed to users as a couple macros in
      the `kernel` crate in the following patch, thus some documentation
      here refers to them for simplicity.
      
      Signed-off-by: default avatarGary Guo <gary@garyguo.net>
      Reviewed-by: default avatarWei Liu <wei.liu@kernel.org>
      [Reworded, adapted for upstream and applied latest changes]
      Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
      ecaa6ddf
  31. Sep 28, 2022