From: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
To: Greg KH <greg@kroah.com>
Cc: Alice Ryhl <aliceryhl@google.com>,
Thomas Bertschinger <tahbertschinger@gmail.com>,
ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com,
rust-for-linux@vger.kernel.org,
Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Subject: Re: [PATCH v2] rust: place generated init_module() function in .init.text
Date: Tue, 6 Feb 2024 12:18:44 +0100 [thread overview]
Message-ID: <CANiq72kOdNWvr2MMGXOnB5GGPOGaFotV0sB+1T8aze1WSfCTaw@mail.gmail.com> (raw)
In-Reply-To: <2024020613-illusion-quirk-14d4@gregkh>
On Tue, Feb 6, 2024 at 11:50 AM Greg KH <greg@kroah.com> wrote:
>
> Cool, but has anyone tested it?
Yeah, in C, I see some warnings from modpost (section mismatch, plus
the explicitly exported symbol), but it does not seem to catch all
cases under `defconfig` at least.
In Rust, similarly, I also get the warning in some cases, but not all;
e.g. I had to force the function to be non-inline so that modpost can
notice.
Anyway, the "unsafe" approach (or hiding the functions completely
inside a `const` if possible, but IIRC from a previous discussion that
required rearranging things a bit due to the global asm block --
ideally Rust would provide anonymous modules) would be even better.
> It's not "unsafe" at all! It's totally normal, and "safe" and should be
> used for many types of modules. If you get it "wrong" the linker will
> catch the issue, so what is unsafe?
In Rust's terminology, "unsafe function" does not mean "wrong" or
"broken" or "always triggers UB", it just means there are safety
preconditions, i.e. "it is UB if you break my preconditions;
otherwise, there isn't UB".
In other words, it means that callers need to be careful (and cannot
call the function without using unsafe Rust) and they also are
required (via a comment) why their call is correct.
So if we do not want any Rust caller to call this function, we could
say "Rust callers must not call this function". Even if the function
could be obviously called in other cases.
Put another way, as a function writer, you can hide "use cases" if you
do not want to support them. Even if they happen to work today. You
can think of it as opaque/private members in structs, but for
functions, i.e. contracts.
So if it is an unintended use case, it is better to catch it as early
as possible, even assuming there is another safety net somewhere else
that catches that too.
Cheers,
Miguel
next prev parent reply other threads:[~2024-02-06 11:18 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-06 1:25 [PATCH v2] rust: place generated init_module() function in .init.text Thomas Bertschinger
2024-02-06 1:39 ` Martin Rodriguez Reboredo
2024-02-06 2:13 ` Thomas Bertschinger
2024-02-06 3:10 ` Trevor Gross
2024-02-06 3:17 ` Trevor Gross
2024-02-06 10:01 ` Greg KH
2024-02-06 10:51 ` Miguel Ojeda
2024-02-06 10:58 ` Greg KH
2024-02-06 11:31 ` Miguel Ojeda
2024-02-06 14:49 ` Thomas Bertschinger
2024-02-06 16:07 ` Martin Rodriguez Reboredo
2024-02-06 17:19 ` Miguel Ojeda
2024-02-06 21:28 ` Martin Rodriguez Reboredo
2024-02-06 17:19 ` Miguel Ojeda
2024-02-06 10:01 ` Greg KH
2024-02-06 10:29 ` Alice Ryhl
2024-02-06 10:50 ` Greg KH
2024-02-06 11:18 ` Miguel Ojeda [this message]
2024-02-06 10:57 ` Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CANiq72kOdNWvr2MMGXOnB5GGPOGaFotV0sB+1T8aze1WSfCTaw@mail.gmail.com \
--to=miguel.ojeda.sandonis@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=greg@kroah.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tahbertschinger@gmail.com \
--cc=wedsonaf@gmail.com \
--cc=yakoyoku@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).