Rust-for-linux archive mirror
 help / color / mirror / Atom feed
From: Trevor Gross <tmgross@umich.edu>
To: Thomas Bertschinger <tahbertschinger@gmail.com>
Cc: rust-for-linux@vger.kernel.org, linux-bcachefs@vger.kernel.org,
	 linux-fsdevel@vger.kernel.org, kent.overstreet@linux.dev,
	bfoster@redhat.com,  ojeda@kernel.org, alex.gaynor@gmail.com,
	wedsonaf@gmail.com
Subject: Re: [PATCH RFC 3/3] bcachefs: introduce Rust module implementation
Date: Wed, 7 Feb 2024 01:58:40 -0500	[thread overview]
Message-ID: <CALNs47u9QU2t1ug9_3_-nPzJCOR=uAHpuCT-yVd-MfWFkVVeYA@mail.gmail.com> (raw)
In-Reply-To: <20240207055845.611710-1-tahbertschinger@gmail.com>

On Wed, Feb 7, 2024 at 12:59 AM Thomas Bertschinger
<tahbertschinger@gmail.com> wrote:
>
> This patch uses the bcachefs bindgen framework to introduce a Rust
> implementation of the module entry and exit functions. With this change,
> bcachefs is now a Rust kernel module (that calls C functions to do most
> of its work).
>
> This is only if CONFIG_BCACHEFS_RUST is defined; the C implementation of
> the module init and exit code is left around so that bcachefs remains
> usable in kernels compiled without Rust support.
>
> Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
> ---
>
> [...]
>
> diff --git a/fs/bcachefs/bcachefs_module.rs b/fs/bcachefs/bcachefs_module.rs
> new file mode 100644
> index 000000000000..8db2de8139bc
> --- /dev/null
> +++ b/fs/bcachefs/bcachefs_module.rs
> @@ -0,0 +1,66 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! bcachefs
> +//!
> +//! Rust kernel module for bcachefs.
> +
> +pub mod bindings;
> +
> +use kernel::prelude::*;
> +
> +use crate::bindings::*;

Most in-tree code uses the `bindings::` prefix when referencing C to
make extern calls clear, rather than doing the glob import. I think we
probably want to keep this style.

> +module! {
> +    type: Bcachefs,
> +    name: "bcachefs",
> +    author: "Kent Overstreet <kent.overstreet@gmail.com>",
> +    description: "bcachefs filesystem",
> +    license: "GPL",
> +}
> +
> +struct Bcachefs;
> +
> +impl kernel::Module for Bcachefs {
> +    #[link_section = ".init.text"]

Is the attribute still needed if this lands?
https://lore.kernel.org/rust-for-linux/20240206153806.567055-1-tahbertschinger@gmail.com/T/#u

> +    fn init(_module: &'static ThisModule) -> Result<Self> {
> +        // SAFETY: this block registers the bcachefs services with the kernel. After succesful
> +        // registration, all such services are guaranteed by the kernel to exist as long as the
> +        // driver is loaded. In the event of any failure in the registration, all registered
> +        // services are unregistered.
> +        unsafe {
> +            bch2_bkey_pack_test();
> +
> +            if bch2_kset_init() != 0
> +                || bch2_btree_key_cache_init() != 0
> +                || bch2_chardev_init() != 0
> +                || bch2_vfs_init() != 0
> +                || bch2_debug_init() != 0
> +            {
> +                __drop();
> +                return Err(ENOMEM);

Do these init functions ever return anything more descriptive than
ENOMEM that should be returned instead? Maybe not worth changing if
the next phase will let you `?` the results.

> +            }
> +        }
> +
> +        Ok(Bcachefs)
> +    }
> +}
> +
> +fn __drop() {

Something like `drop_impl` or `unregister` is probably more in line
with naming, dunder is really only used when something
unstable/generated needs to be made public.

> [...]

Cool to see the ball rolling on this :)

- Trevor

      parent reply	other threads:[~2024-02-07  6:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-07  5:58 [PATCH RFC 3/3] bcachefs: introduce Rust module implementation Thomas Bertschinger
2024-02-07  6:07 ` Kent Overstreet
2024-02-07  6:58 ` Trevor Gross [this message]

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='CALNs47u9QU2t1ug9_3_-nPzJCOR=uAHpuCT-yVd-MfWFkVVeYA@mail.gmail.com' \
    --to=tmgross@umich.edu \
    --cc=alex.gaynor@gmail.com \
    --cc=bfoster@redhat.com \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcachefs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tahbertschinger@gmail.com \
    --cc=wedsonaf@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 read-only IMAP folder(s) and NNTP newsgroup(s).