kbuild: rust: avoid creating temporary files

Message ID 20230718055235.1050223-1-ojeda@kernel.org
State New
Headers
Series kbuild: rust: avoid creating temporary files |

Commit Message

Miguel Ojeda July 18, 2023, 5:52 a.m. UTC
  `rustc` outputs by default the temporary files (i.e. the ones saved
by `-Csave-temps`, such as `*.rcgu*` files) in the current working
directory when `-o` and `--out-dir` are not given (even if
`--emit=x=path` is given, i.e. it does not use those for temporaries).

Since out-of-tree modules are compiled from the `linux` tree,
`rustc` then tries to create them there, which may not be accessible.

Thus pass `--out-dir` explicitly, even if it is just for the temporary
files.

Reported-by: Raphael Nestler <raphael.nestler@gmail.com>
Closes: https://github.com/Rust-for-Linux/linux/issues/1015
Reported-by: Andrea Righi <andrea.righi@canonical.com>
Tested-by: Raphael Nestler <raphael.nestler@gmail.com>
Tested-by: Andrea Righi <andrea.righi@canonical.com>
Cc: stable@vger.kernel.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 scripts/Makefile.build | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)


base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
  

Comments

Nathan Chancellor July 18, 2023, 4:43 p.m. UTC | #1
On Tue, Jul 18, 2023 at 07:52:35AM +0200, Miguel Ojeda wrote:
> `rustc` outputs by default the temporary files (i.e. the ones saved
> by `-Csave-temps`, such as `*.rcgu*` files) in the current working
> directory when `-o` and `--out-dir` are not given (even if
> `--emit=x=path` is given, i.e. it does not use those for temporaries).
> 
> Since out-of-tree modules are compiled from the `linux` tree,
> `rustc` then tries to create them there, which may not be accessible.
> 
> Thus pass `--out-dir` explicitly, even if it is just for the temporary
> files.
> 
> Reported-by: Raphael Nestler <raphael.nestler@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1015
> Reported-by: Andrea Righi <andrea.righi@canonical.com>
> Tested-by: Raphael Nestler <raphael.nestler@gmail.com>
> Tested-by: Andrea Righi <andrea.righi@canonical.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Seems reasonable to me.

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

> ---
>  scripts/Makefile.build | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 6413342a03f4..82e3fb19fdaf 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -264,6 +264,9 @@ $(obj)/%.lst: $(src)/%.c FORCE
>  
>  rust_allowed_features := new_uninit
>  
> +# `--out-dir` is required to avoid temporaries being created by `rustc` in the
> +# current working directory, which may be not accessible in the out-of-tree
> +# modules case.
>  rust_common_cmd = \
>  	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
>  	-Zallow-features=$(rust_allowed_features) \
> @@ -272,7 +275,7 @@ rust_common_cmd = \
>  	--extern alloc --extern kernel \
>  	--crate-type rlib -L $(objtree)/rust/ \
>  	--crate-name $(basename $(notdir $@)) \
> -	--emit=dep-info=$(depfile)
> +	--out-dir $(dir $@) --emit=dep-info=$(depfile)
>  
>  # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
>  # will be used. We explicitly request `-Ccodegen-units=1` in any case, and
> 
> base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
> -- 
> 2.41.0
>
  
Martin Rodriguez Reboredo July 19, 2023, 5:44 p.m. UTC | #2
On 7/18/23 02:52, Miguel Ojeda wrote:
> `rustc` outputs by default the temporary files (i.e. the ones saved
> by `-Csave-temps`, such as `*.rcgu*` files) in the current working
> directory when `-o` and `--out-dir` are not given (even if
> `--emit=x=path` is given, i.e. it does not use those for temporaries).
> 
> Since out-of-tree modules are compiled from the `linux` tree,
> `rustc` then tries to create them there, which may not be accessible.
> 
> Thus pass `--out-dir` explicitly, even if it is just for the temporary
> files.
> 
> Reported-by: Raphael Nestler <raphael.nestler@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1015
> Reported-by: Andrea Righi <andrea.righi@canonical.com>
> Tested-by: Raphael Nestler <raphael.nestler@gmail.com>
> Tested-by: Andrea Righi <andrea.righi@canonical.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
> [...]
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
  
Masahiro Yamada July 20, 2023, 5:17 p.m. UTC | #3
On Tue, Jul 18, 2023 at 2:52 PM Miguel Ojeda <ojeda@kernel.org> wrote:
>
> `rustc` outputs by default the temporary files (i.e. the ones saved
> by `-Csave-temps`, such as `*.rcgu*` files) in the current working
> directory when `-o` and `--out-dir` are not given (even if
> `--emit=x=path` is given, i.e. it does not use those for temporaries).
>
> Since out-of-tree modules are compiled from the `linux` tree,
> `rustc` then tries to create them there, which may not be accessible.
>
> Thus pass `--out-dir` explicitly, even if it is just for the temporary
> files.
>
> Reported-by: Raphael Nestler <raphael.nestler@gmail.com>
> Closes: https://github.com/Rust-for-Linux/linux/issues/1015
> Reported-by: Andrea Righi <andrea.righi@canonical.com>
> Tested-by: Raphael Nestler <raphael.nestler@gmail.com>
> Tested-by: Andrea Righi <andrea.righi@canonical.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---



Applied to linux-kbuild/fixes. Thanks.

I believe this is a fix, so I will include it
in my next pull request.





>  scripts/Makefile.build | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 6413342a03f4..82e3fb19fdaf 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -264,6 +264,9 @@ $(obj)/%.lst: $(src)/%.c FORCE
>
>  rust_allowed_features := new_uninit
>
> +# `--out-dir` is required to avoid temporaries being created by `rustc` in the
> +# current working directory, which may be not accessible in the out-of-tree
> +# modules case.
>  rust_common_cmd = \
>         RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
>         -Zallow-features=$(rust_allowed_features) \
> @@ -272,7 +275,7 @@ rust_common_cmd = \
>         --extern alloc --extern kernel \
>         --crate-type rlib -L $(objtree)/rust/ \
>         --crate-name $(basename $(notdir $@)) \
> -       --emit=dep-info=$(depfile)
> +       --out-dir $(dir $@) --emit=dep-info=$(depfile)
>
>  # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
>  # will be used. We explicitly request `-Ccodegen-units=1` in any case, and
>
> base-commit: 06c2afb862f9da8dc5efa4b6076a0e48c3fbaaa5
> --
> 2.41.0
>
  
Miguel Ojeda July 20, 2023, 5:29 p.m. UTC | #4
On Thu, Jul 20, 2023 at 7:18 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> Applied to linux-kbuild/fixes. Thanks.
>
> I believe this is a fix, so I will include it
> in my next pull request.

Thanks Masahiro! Yeah, it can be considered a fix. By the way, in case you want:

    Fixes: 2f7ab1267dc9 ("Kbuild: add Rust support")

I guess it can be also considered a feature (e.g. "supporting more
setups"), but having the temporaries created where they were was
unintentional.

Cheers,
Miguel
  
Masahiro Yamada July 20, 2023, 6:34 p.m. UTC | #5
On Fri, Jul 21, 2023 at 2:30 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Thu, Jul 20, 2023 at 7:18 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
> >
> > Applied to linux-kbuild/fixes. Thanks.
> >
> > I believe this is a fix, so I will include it
> > in my next pull request.
>
> Thanks Masahiro! Yeah, it can be considered a fix. By the way, in case you want:
>
>     Fixes: 2f7ab1267dc9 ("Kbuild: add Rust support")


Maybe, the following was a breakage.


commit 295d8398c67e314d99bb070f38883f83fe94a97a
Author: Masahiro Yamada <masahiroy@kernel.org>
Date:   Sat Jan 7 18:18:15 2023 +0900

    kbuild: specify output names separately for each emission type from rustc




Before that, rust_common_cmd had --out-dir.


BTW, do we also need to fix scripts/Makefile.host
in case the external module Makefile creates host programs?







> I guess it can be also considered a feature (e.g. "supporting more
> setups"), but having the temporaries created where they were was
> unintentional.
  
Miguel Ojeda July 22, 2023, 3:51 p.m. UTC | #6
On Thu, Jul 20, 2023 at 8:35 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> Maybe, the following was a breakage.
>
> commit 295d8398c67e314d99bb070f38883f83fe94a97a
> Author: Masahiro Yamada <masahiroy@kernel.org>
> Date:   Sat Jan 7 18:18:15 2023 +0900
>
>     kbuild: specify output names separately for each emission type from rustc
>
> Before that, rust_common_cmd had --out-dir.

That's right, good catch!

> BTW, do we also need to fix scripts/Makefile.host
> in case the external module Makefile creates host programs?

Indeed, we need it too. [1] would fix it (tested it with a trivial
out-of-tree Rust hostprog).

Do you want me to send it separately? i.e. to avoid losing the
`Tested-by`s etc. that we already got for this one, which is the
important one since that actually has users.

The hostprogs fix is not really critical for stable, since it is
unlikely there are users at all (we just got the first in-tree real
user in the Rust+KUnit integration coming into 6.6), but I guess it
does not hurt for the same reason.

Thanks!

Cheers,
Miguel

[1]

diff --git a/scripts/Makefile.host b/scripts/Makefile.host
index 7aea9005e497..54adf2e2ec51 100644
--- a/scripts/Makefile.host
+++ b/scripts/Makefile.host
@@ -86,7 +86,7 @@ hostc_flags    = -Wp,-MMD,$(depfile) \
 hostcxx_flags  = -Wp,-MMD,$(depfile) \
                  $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
                  $(HOSTCXXFLAGS_$(target-stem).o)
-hostrust_flags = --emit=dep-info=$(depfile) \
+hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
                  $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
                  $(HOSTRUSTFLAGS_$(target-stem))
  
Masahiro Yamada July 23, 2023, 5:05 a.m. UTC | #7
On Sun, Jul 23, 2023 at 12:52 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Thu, Jul 20, 2023 at 8:35 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
> >
> > Maybe, the following was a breakage.
> >
> > commit 295d8398c67e314d99bb070f38883f83fe94a97a
> > Author: Masahiro Yamada <masahiroy@kernel.org>
> > Date:   Sat Jan 7 18:18:15 2023 +0900
> >
> >     kbuild: specify output names separately for each emission type from rustc
> >
> > Before that, rust_common_cmd had --out-dir.
>
> That's right, good catch!
>
> > BTW, do we also need to fix scripts/Makefile.host
> > in case the external module Makefile creates host programs?
>
> Indeed, we need it too. [1] would fix it (tested it with a trivial
> out-of-tree Rust hostprog).
>
> Do you want me to send it separately? i.e. to avoid losing the
> `Tested-by`s etc. that we already got for this one, which is the
> important one since that actually has users.
>
> The hostprogs fix is not really critical for stable, since it is
> unlikely there are users at all (we just got the first in-tree real
> user in the Rust+KUnit integration coming into 6.6), but I guess it
> does not hurt for the same reason.


Can you send v2 with the following squashed?


I think it makes sense to fix both if we add
Fixes: 295d8398c67e ("kbuild: specify output names separately for each
emission type from rustc")





> Thanks!
>
> Cheers,
> Miguel
>
> [1]
>
> diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> index 7aea9005e497..54adf2e2ec51 100644
> --- a/scripts/Makefile.host
> +++ b/scripts/Makefile.host
> @@ -86,7 +86,7 @@ hostc_flags    = -Wp,-MMD,$(depfile) \
>  hostcxx_flags  = -Wp,-MMD,$(depfile) \
>                   $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
>                   $(HOSTCXXFLAGS_$(target-stem).o)
> -hostrust_flags = --emit=dep-info=$(depfile) \
> +hostrust_flags = --out-dir $(dir $@) --emit=dep-info=$(depfile) \
>                   $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
>                   $(HOSTRUSTFLAGS_$(target-stem))
  
Masahiro Yamada July 23, 2023, 1:35 p.m. UTC | #8
On Sun, Jul 23, 2023 at 2:05 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> On Sun, Jul 23, 2023 at 12:52 AM Miguel Ojeda
> <miguel.ojeda.sandonis@gmail.com> wrote:
> >
> > On Thu, Jul 20, 2023 at 8:35 PM Masahiro Yamada <masahiroy@kernel.org> wrote:
> > >
> > > Maybe, the following was a breakage.
> > >
> > > commit 295d8398c67e314d99bb070f38883f83fe94a97a
> > > Author: Masahiro Yamada <masahiroy@kernel.org>
> > > Date:   Sat Jan 7 18:18:15 2023 +0900
> > >
> > >     kbuild: specify output names separately for each emission type from rustc
> > >
> > > Before that, rust_common_cmd had --out-dir.
> >
> > That's right, good catch!
> >
> > > BTW, do we also need to fix scripts/Makefile.host
> > > in case the external module Makefile creates host programs?
> >
> > Indeed, we need it too. [1] would fix it (tested it with a trivial
> > out-of-tree Rust hostprog).
> >
> > Do you want me to send it separately? i.e. to avoid losing the
> > `Tested-by`s etc. that we already got for this one, which is the
> > important one since that actually has users.
> >
> > The hostprogs fix is not really critical for stable, since it is
> > unlikely there are users at all (we just got the first in-tree real
> > user in the Rust+KUnit integration coming into 6.6), but I guess it
> > does not hurt for the same reason.
>
>
> Can you send v2 with the following squashed?
>
>
> I think it makes sense to fix both if we add
> Fixes: 295d8398c67e ("kbuild: specify output names separately for each
> emission type from rustc")


I dropped v1 for now,
expecting to get v2 soon.
  
Miguel Ojeda July 23, 2023, 2:27 p.m. UTC | #9
On Sun, Jul 23, 2023 at 7:06 AM Masahiro Yamada <masahiroy@kernel.org> wrote:
>
> Can you send v2 with the following squashed?
>
> I think it makes sense to fix both if we add
> Fixes: 295d8398c67e ("kbuild: specify output names separately for each
> emission type from rustc")

Both done [1] -- I marked the `Tested-by`s with "# non-hostprogs" to avoid
losing the tags.

Thanks!

[1] https://lore.kernel.org/rust-for-linux/20230723142128.194339-1-ojeda@kernel.org/

Cheers,
Miguel
  

Patch

diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 6413342a03f4..82e3fb19fdaf 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -264,6 +264,9 @@  $(obj)/%.lst: $(src)/%.c FORCE
 
 rust_allowed_features := new_uninit
 
+# `--out-dir` is required to avoid temporaries being created by `rustc` in the
+# current working directory, which may be not accessible in the out-of-tree
+# modules case.
 rust_common_cmd = \
 	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
 	-Zallow-features=$(rust_allowed_features) \
@@ -272,7 +275,7 @@  rust_common_cmd = \
 	--extern alloc --extern kernel \
 	--crate-type rlib -L $(objtree)/rust/ \
 	--crate-name $(basename $(notdir $@)) \
-	--emit=dep-info=$(depfile)
+	--out-dir $(dir $@) --emit=dep-info=$(depfile)
 
 # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
 # will be used. We explicitly request `-Ccodegen-units=1` in any case, and