Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -Zfunction-return={keep,thunk-extern} option #116892

Merged
merged 2 commits into from Dec 1, 2023
Merged

Conversation

ojeda
Copy link
Contributor

@ojeda ojeda commented Oct 18, 2023

This is intended to be used for Linux kernel RETHUNK builds.

With this commit (optionally backported to Rust 1.73.0), plus a patched Linux kernel to pass the flag, I get a RETHUNK build with Rust enabled that is objtool-warning-free and is able to boot in QEMU and load a sample Rust kernel module.

Issue: #116853.

@rustbot
Copy link
Collaborator

rustbot commented Oct 18, 2023

r? @wesleywiser

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2023
@rust-log-analyzer

This comment has been minimized.

@ojeda
Copy link
Contributor Author

ojeda commented Oct 18, 2023

Note that LLVM ignores the attribute in non-x86, but I have not replicated Clang's check that this only works in x86 for the CLI flag for the moment (should it be part of the target spec?).

Also, I linked the feature request issue as the tracking one in the docs -- we can reuse it changing the OP message, I assume, but please let me know if it is best to create a new one.

@ojeda ojeda force-pushed the rethunk branch 2 times, most recently from 8df5004 to 8b6016a Compare October 19, 2023 22:25
Comment on lines +1710 to +1695
// FIXME: In principle, the inherited base LLVM target code model could be large,
// but this only checks whether we were passed one explicitly (like Clang does).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is a way to get the effective code model, it would be nice to use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That covers even less, no? i.e. code_model() covers codegen and target options.

@rust-log-analyzer

This comment has been minimized.

@ojeda
Copy link
Contributor Author

ojeda commented Oct 19, 2023

I added a folder but that still counts against the tidy limit in tests/ui. Should I move the folder to e.g. tests/ui/invalid-compile-flags or increase the limit by 1?

@ojeda
Copy link
Contributor Author

ojeda commented Oct 20, 2023

One more note: GCC and Clang give an error if one tries to use function-return at all, i.e. even with keep, on non-x86. However, in this PR keep is allowed for all architectures.

I did it like this because it seemed simpler (no Option) and more flexible, especially since (in the case of this flag) other architectures could support particular modes, e.g. s390 expolines (https://github.com/llvm/llvm-project/blob/49af6502c6dcb4a7f7520178bd14df396f78240c/clang/lib/Frontend/CompilerInvocation.cpp#L1942).

But perhaps consistency with GCC/Clang is better?

@ojeda
Copy link
Contributor Author

ojeda commented Oct 20, 2023

I added a folder but that still counts against the tidy limit in tests/ui. Should I move the folder to e.g. tests/ui/invalid-compile-flags or increase the limit by 1?

I moved them inside the other folder, and noticed one in invalid/ should be in that invalid-compile-flags/ folder: #116975.

ojeda added a commit to ojeda/linux that referenced this pull request Oct 23, 2023
The Rust compiler does not support the equivalent of
`-mfunction-return=thunk-extern` yet [1]. Thus, currently, `objtool`
warns about it, e.g.:

    samples/rust/rust_print.o: warning: objtool: _R...init+0xa5c:
    'naked' return found in RETHUNK build

The support in `rustc` for `-Zfunction-return` has been submitted and
is being reviewed [2]. It adds the needed LLVM function attributes and,
with it, I got a RETHUNK kernel build with Rust enabled that does not
print the `objtool` related warnings, boots in QEMU and can load a kernel
loadable module.

In any case, until proper/complete support is added to `rustc`, make it
a hard restriction until the mitigation is in place.

This may have an impact for developers that may not need/care about the
mitigation in the Rust side (e.g. Ubuntu offers Rust as a "technology
preview" [3]), but given we are getting closer to having the first actual
in-tree Rust kernel users, it seems like the right time to disallow
it. This should also avoid confusion [4].

Link: rust-lang/rust#116853 [1]
Link: rust-lang/rust#116892 [2]
Link: https://lore.kernel.org/rust-for-linux/ZSQXqX2%2Flhf5ICZP@gpd/ [3]
Link: https://lore.kernel.org/rust-for-linux/CANiq72n6DMeXQrgOzS_+3VdgNYAmpcnneAHJnZERUQhMExg+0A@mail.gmail.com/ [4]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
ojeda added a commit to ojeda/linux that referenced this pull request Oct 23, 2023
When support for `-Zfunction-return` lands in Rust [1], this patch may
be used to enable RETHUNK support on top of the previous patch.

Link: rust-lang/rust#116892 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Oct 23, 2023
The Rust compiler does not support the equivalent of
`-mfunction-return=thunk-extern` yet [1]. Thus, currently, `objtool`
warns about it, e.g.:

    samples/rust/rust_print.o: warning: objtool: _R...init+0xa5c:
    'naked' return found in RETHUNK build

The support in `rustc` for `-Zfunction-return` has been submitted and
is being reviewed [2]. It adds the needed LLVM function attributes and,
with it, I got a RETHUNK kernel build with Rust enabled that does not
print the `objtool` related warnings, boots in QEMU and can load a kernel
loadable module.

In any case, until proper/complete support is added to `rustc`, make it
a hard restriction until the mitigation is in place.

This may have an impact for developers that may not need/care about the
mitigation in the Rust side (e.g. Ubuntu offers Rust as a "technology
preview" [3]), but given we are getting closer to having the first actual
in-tree Rust kernel users, it seems like the right time to disallow
it. This should also avoid confusion [4].

Link: rust-lang/rust#116853 [1]
Link: rust-lang/rust#116892 [2]
Link: https://lore.kernel.org/rust-for-linux/ZSQXqX2%2Flhf5ICZP@gpd/ [3]
Link: https://lore.kernel.org/rust-for-linux/CANiq72n6DMeXQrgOzS_+3VdgNYAmpcnneAHJnZERUQhMExg+0A@mail.gmail.com/ [4]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Oct 23, 2023
When support for `-Zfunction-return` lands in Rust [1], this patch may
be used to enable RETHUNK support on top of the previous patch.

Link: rust-lang/rust#116892 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
@bors
Copy link
Contributor

bors commented Oct 30, 2023

☔ The latest upstream changes (presumably #117405) made this pull request unmergeable. Please resolve the merge conflicts.

@ojeda
Copy link
Contributor Author

ojeda commented Oct 31, 2023

Resolved trivial formatting conflict.

@wesleywiser
Copy link
Member

Thanks for already getting the tracking issue set up @ojeda!

@bors r+

@ojeda
Copy link
Contributor Author

ojeda commented Nov 30, 2023

Rebased, added // ignore-dawrin and // ignore-sgx.

For Darwin, if there is a "standard" way to check for the extra underscore, please let me know (relaxing the regex felt wrong, but perhaps duplicating the cases is OK for this sort of test). Otherwise, LLVM's own test use linux-gnu for these tests, so I guess it is OK.

@rust-log-analyzer

This comment has been minimized.

@ojeda
Copy link
Contributor Author

ojeda commented Nov 30, 2023

Re-blessed tests (previous error -> 1 previous error).

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

This is intended to be used for Linux kernel RETHUNK builds.

With this commit (optionally backported to Rust 1.73.0), plus a
patched Linux kernel to pass the flag, I get a RETHUNK build with
Rust enabled that is `objtool`-warning-free and is able to boot in
QEMU and load a sample Rust kernel module.

Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
@ojeda
Copy link
Contributor Author

ojeda commented Nov 30, 2023

A bunch of trivial fixes above.

@wesleywiser @Dylan-DPC This is ready again -- let's see if other targets fail in the full CI.

@wesleywiser
Copy link
Member

Thanks for the rebase @ojeda!

@bors r+ rollup=iffy

@bors
Copy link
Contributor

bors commented Nov 30, 2023

📌 Commit 2d47622 has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 30, 2023
@bors
Copy link
Contributor

bors commented Nov 30, 2023

⌛ Testing commit 2d47622 with merge f45631b...

@bors
Copy link
Contributor

bors commented Dec 1, 2023

☀️ Test successful - checks-actions
Approved by: wesleywiser
Pushing f45631b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 1, 2023
@bors bors merged commit f45631b into rust-lang:master Dec 1, 2023
12 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Dec 1, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f45631b): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.8% [-1.8%, -1.8%] 1
Improvements ✅
(secondary)
-1.3% [-1.3%, -1.3%] 1
All ❌✅ (primary) -1.8% [-1.8%, -1.8%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-14.8% [-21.4%, -9.2%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -14.8% [-21.4%, -9.2%] 7

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 674.44s -> 674.191s (-0.04%)
Artifact size: 313.39 MiB -> 313.41 MiB (0.01%)

@ojeda ojeda deleted the rethunk branch December 1, 2023 11:59
@ojeda
Copy link
Contributor Author

ojeda commented Dec 1, 2023

@wesleywiser Thanks for the very quick review and have a nice holiday (I saw the other PR by chance when looking for this one :)

@wesleywiser
Copy link
Member

Thanks! ☺️

Fabo pushed a commit to Fabo/linux that referenced this pull request Dec 6, 2023
The Rust compiler does not support the equivalent of
`-mfunction-return=thunk-extern` yet [1]. Thus, currently, `objtool`
warns about it, e.g.:

    samples/rust/rust_print.o: warning: objtool: _R...init+0xa5c:
    'naked' return found in RETHUNK build

The support in `rustc` for `-Zfunction-return` has been submitted and
is being reviewed [2]. It adds the needed LLVM function attributes and,
with it, I got a RETHUNK kernel build with Rust enabled that does not
print the `objtool` related warnings, boots in QEMU and can load a kernel
loadable module.

In any case, until proper/complete support is added to `rustc`, make it
a hard restriction until the mitigation is in place.

This may have an impact for developers that may not need/care about the
mitigation in the Rust side (e.g. Ubuntu offers Rust as a "technology
preview" [3]), but given we are getting closer to having the first actual
in-tree Rust kernel users, it seems like the right time to disallow
it. This should also avoid confusion [4].

Link: rust-lang/rust#116853 [1]
Link: rust-lang/rust#116892 [2]
Link: https://lore.kernel.org/rust-for-linux/ZSQXqX2%2Flhf5ICZP@gpd/ [3]
Link: https://lore.kernel.org/rust-for-linux/CANiq72n6DMeXQrgOzS_+3VdgNYAmpcnneAHJnZERUQhMExg+0A@mail.gmail.com/ [4]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Acked-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Fabo pushed a commit to Fabo/linux that referenced this pull request Dec 6, 2023
When support for `-Zfunction-return` lands in Rust [1], this patch may
be used to enable RETHUNK support on top of the previous patch.

Link: rust-lang/rust#116892 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Fabo pushed a commit to Fabo/linux that referenced this pull request Dec 16, 2023
The Rust compiler does not support the equivalent of
`-mfunction-return=thunk-extern` yet [1]. Thus, currently, `objtool`
warns about it, e.g.:

    samples/rust/rust_print.o: warning: objtool: _R...init+0xa5c:
    'naked' return found in RETHUNK build

The support in `rustc` for `-Zfunction-return` has been submitted and
is being reviewed [2]. It adds the needed LLVM function attributes and,
with it, I got a RETHUNK kernel build with Rust enabled that does not
print the `objtool` related warnings, boots in QEMU and can load a kernel
loadable module.

In any case, until proper/complete support is added to `rustc`, make it
a hard restriction until the mitigation is in place.

This may have an impact for developers that may not need/care about the
mitigation in the Rust side (e.g. Ubuntu offers Rust as a "technology
preview" [3]), but given we are getting closer to having the first actual
in-tree Rust kernel users, it seems like the right time to disallow
it. This should also avoid confusion [4].

Link: rust-lang/rust#116853 [1]
Link: rust-lang/rust#116892 [2]
Link: https://lore.kernel.org/rust-for-linux/ZSQXqX2%2Flhf5ICZP@gpd/ [3]
Link: https://lore.kernel.org/rust-for-linux/CANiq72n6DMeXQrgOzS_+3VdgNYAmpcnneAHJnZERUQhMExg+0A@mail.gmail.com/ [4]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Acked-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Fabo pushed a commit to Fabo/linux that referenced this pull request Dec 16, 2023
When support for `-Zfunction-return` lands in Rust [1], this patch may
be used to enable RETHUNK support on top of the previous patch.

Link: rust-lang/rust#116892 [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet