qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
To: qeemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, "Stefan Hajnoczi" <stefanha@redhat.com>,
	"Mads Ynddal" <mads@ynddal.dk>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Alex Benné e" <alex.bennee@linaro.org>,
	"Daniel P. Berrangé " <berrange@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Philippe Mathieu-Daudé " <philmd@linaro.org>,
	"Zhao Liu" <zhao1.liu@intel.com>,
	"Gustavo Romero" <gustavo.romero@linaro.org>,
	"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
	rowan.hart@intel.com,
	"Richard Henderson" <richard.henderson@linaro.org>
Subject: Re: [RFC PATCH v5 0/8] Add Rust support, implement ARM PL011
Date: Thu, 25 Jul 2024 08:47:40 +0300	[thread overview]
Message-ID: <h61ku.ipxyjqsxu75@linaro.org> (raw)
In-Reply-To: <CABgObfZOqBogWQtzfghjKMsW-J_sp-iL5dt7mmYnvE5eQb9G5w@mail.gmail.com>

On Wed, 24 Jul 2024 13:34, Paolo Bonzini <pbonzini@redhat.com> wrote:
>On Wed, Jul 24, 2024 at 11:58 AM Manos Pitsidianakis
><manos.pitsidianakis@linaro.org> wrote:
>> >In my opinion we should start with cargo workspaces as the
>> >known-imperfect (but good enough) solution, so that it could be evolved
>> >later.  It is important that any change that deviates from common Rust
>> >conventions is documented, and v4 provided a nice basis to build upon,
>> >with documentation coming as things settle.  This is why I explicitly
>> >didn't include Kconfig in the TODO list in my review of v4.
>>
>> After working with the latest meson releases, it seems we will soon have
>> a good enough way of handling all this with meson. It makes me sceptical
>> of adding cargo wrappers and using a build system out of meson when we
>> might be able to drop all that soonish. We might as well bite the bullet
>> now to avoid working on something we know we will remove.
>
>Ehh, as you say below it's complicated. Sometimes worse is better.
>Personally I wouldn't have minded keeping the v4 approach as a "known
>evil"; but if you can make the Cargo subprojects work, that would be
>fine for me. What I don't like is the vendoring and handwritten (I
>think?) meson.build, I think that's worse than the v4.
>
>> >Also, of the code changes (as opposed to the build system changes) that
>> >I had asked for in the review of v4, almost none of them have been
>> >applied.  I'm copying them here for future reference:
>>
>> Thanks, this helps a lot.
>>
>> >❌ TODO comments when the code is doing potential undefined behavior
>>
>> Do you mean like Rust's safety comments?
>
>No I meant comments where we have known instances of undefined
>behavior. The two I had in my emails are
>
>(in pl011_init):
>// TODO: this assumes that "all zeroes" is a valid state for all fields of
>// PL011State. This is not necessarily true of any #[repr(Rust)] structs,
>// including bilge-generated types. It should instead use MaybeUninit.
>
>(before the call to qemu_chr_fe_accept_input):
>// TODO: this causes a callback that creates another "&mut self".
>// This is forbidden by Rust aliasing rules and has to be fixed
>// using interior mutability.

You mean that we can have a call stack that looks something like...

<qemu code>
|_ pl011_read
  |_ PL011State::read()
     |_ qemu_chr_fe_accept_input()
      |_ pl011_read
        |_ PL011State::read()

If I understand correctly, this does not create another "&mut self", 
because a mutable reference of self is passed to 
qemu_chr_fe_accept_input(), and only exists until it returns.

In any case, I agree that these subtleties must be examined thoroughly 
in general. In this case though PL011State has only Copy fields and no 
side effects when dropped. This means that adding interior mutability 
e.g. with Cell would have exactly the same behavior.

But your bringing it up makes me wonder whether we can detect any bad 
behaviors with miri... It does not play well with FFI boundaries but 
it's possible to mock them in some cases. I will look into the two TODOs 
you mention and also if it's possible to verify the correct behavior 
whenever possible!


>
>> https://std-dev-guide.rust-lang.org/policy/safety-comments.html
>>
>> These can be required by lints which is really helpful. At this point
>> the UART library has safety comments (which needs to be reviewed for
>> validity). I plan on adding some at the macros in qemu-api as well.
>>
>> >
>> >❌ a trait to store the CStr corresponding to the structs
>>
>> I don't know yet if that is helpful in our usecase, because the strings
>> must be visible from C, thus be (rust, not c) statics, unmangled and
>> marked as #[used] for the linker. It makes sense from the Rust POV but
>> must also be FFI-accessible.
>
>Why do they have to be #[used]? You have
>
>+                #[used]
>+                static TYPE_NAME: &::core::ffi::CStr = $tname;
>+                $tname.as_ptr()
>
>but since the cstr crate (and c"" literal) promise to return a
>&'static CStr, I thought it could be just
>
>    $tname.as_ptr()
>
>About traits, I meant something like
>
>pub unsafe trait ObjectType: Sized {
>     const TYPE_NAME: &'static CStr;
>}
>
>So that you can put the trait declaration in the pl011 crate and the
>type_info! macro can do
>
><$t as ObjectType>::TYPE_NAME.as_ptr()
>
>(also for the parent).

That was my approach at the beginning but I was having issues with the 
linker stripping the <some const value>.as_ptr() and it would point to 
invalid memory; I checked it again and I think using #[used] just for 
the TypeInfo struct declaration might be enough so these can be removed.

>
>> >❌ a trait to generate all-zero structs without having to type "unsafe {
>> >MaybeUninit::zeroed().assume_init() }"
>>
>> Traits cannot have const fns at the moment (traits cannot have
>> type-level effects like const or async but it's WIP to get them into
>> rustc), so this cannot be used for statics and consts.
>
>Ah, I see. Anyhow, I've been looking at the Linux kernel's pinned-init
>crate (https://rust-for-linux.com/pinned-init) and it provides a
>Zeroable macro including #[derive] support. So that has gone lower in
>my priority.
>
>> >❌ I'd like to use ctor instead of non-portable linker magic,
>>
>> The linker sections are pretty much standard and in fact ctor uses the
>> same linker attributes. Would writing our own constructor macro be a
>> solution for you? My reasoning is that 1) we support our own specific
>> platforms and it's better for portability to reflect that in our source
>> tree and 2) it avoids the external dependency, linker sections do not
>> change so any ctor update would be in the API or adding more platform
>> support,  not fixes in what we target.
>
>I'd still like to give someone else the burden. :) Writing our own
>constructor macro would be more work for little gain.

Well, it's just that I personally don't view adding __attribute__ 
manually in only two places is a burden but I've no strong preference 
over it.

I'm looking at the ctor dependencies and they are a few:
https://github.com/mmastrac/rust-ctor/blob/cc3ab9160ed9dc3bdf20d735352b519abc2913e9/Cargo.lock

Do you perhaps agree with adding a FIXME comment to replace the linker 
attributes with ctor when we get the cargo dependency story in meson 
sorted out?

Manos


  reply	other threads:[~2024-07-25  6:20 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-22 11:43 [RFC PATCH v5 0/8] Add Rust support, implement ARM PL011 Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 1/8] build-sys: Add rust feature option Manos Pitsidianakis
2024-07-23  6:37   ` Zhao Liu
2024-07-23 10:13     ` Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 2/8] build deps: update lcitool to include rust bits Manos Pitsidianakis
2024-07-23  8:31   ` Richard Henderson
2024-07-23 10:11     ` Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 3/8] CI: Add build-system-rust-debian job Manos Pitsidianakis
2024-07-23  8:32   ` Richard Henderson
2024-07-23  8:39   ` Daniel P. Berrangé
2024-07-23 10:06     ` Manos Pitsidianakis
2024-07-23 10:11       ` Daniel P. Berrangé
2024-07-23 10:24         ` Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 4/8] rust: add bindgen step as a meson dependency Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 5/8] .gitattributes: add Rust diff and merge attributes Manos Pitsidianakis
2024-07-23  8:38   ` Zhao Liu
2024-07-22 11:43 ` [RFC PATCH v5 6/8] rust: add crate to expose bindings and interfaces Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 7/8] rust: add PL011 device model Manos Pitsidianakis
2024-07-22 11:43 ` [RFC PATCH v5 8/8] rust/pl011: vendor dependencies Manos Pitsidianakis
2024-07-23  8:37   ` Zhao Liu
2024-07-23 10:19     ` Manos Pitsidianakis
2024-07-23 15:07 ` [RFC PATCH v5 0/8] Add Rust support, implement ARM PL011 Paolo Bonzini
2024-07-24  9:14   ` Manos Pitsidianakis
2024-07-24 10:34     ` Paolo Bonzini
2024-07-25  5:47       ` Manos Pitsidianakis [this message]
2024-07-25  9:50         ` Paolo Bonzini
2024-07-25 10:02           ` Manos Pitsidianakis
2024-07-25 11:19             ` Paolo Bonzini
2024-07-25 14:48               ` Manos Pitsidianakis
2024-07-25 15:15                 ` Paolo Bonzini
2024-07-26  7:12                   ` Manos Pitsidianakis
2024-07-26  8:19                     ` Paolo Bonzini
2024-07-26  9:26                       ` Manos Pitsidianakis
2024-07-31  9:41                         ` Manos Pitsidianakis
2024-07-31 10:35                           ` Paolo Bonzini

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=h61ku.ipxyjqsxu75@linaro.org \
    --to=manos.pitsidianakis@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=gustavo.romero@linaro.org \
    --cc=mads@ynddal.dk \
    --cc=marcandre.lureau@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=pierrick.bouvier@linaro.org \
    --cc=qeemu-devel@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=rowan.hart@intel.com \
    --cc=stefanha@redhat.com \
    --cc=thuth@redhat.com \
    --cc=zhao1.liu@intel.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).