rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
To: FUJITA Tomonori <tomo@exabit.dev>
Cc: rust-for-linux@vger.kernel.org, aliceryhl@google.com,
	andrew@lunn.ch, FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: Re: [PATCH v2 1/5] rust: core abstractions for network device drivers
Date: Sat, 10 Jun 2023 21:49:50 +0200	[thread overview]
Message-ID: <CANiq72ki5CEY4QpOW63YTAU8_2OB8rS0FEuY-qXyncahkNUMFg@mail.gmail.com> (raw)
In-Reply-To: <01010188a42d5319-9cd7b5ec-5b06-4d08-88cb-c2a82a8e3a0d-000000@us-west-2.amazonses.com>

On Sat, Jun 10, 2023 at 9:35 AM FUJITA Tomonori <tomo@exabit.dev> wrote:
>
> +/// Corresponds to the kernel's `struct net_device`.
> +///
> +/// # Safety
> +///
> +/// The kernel uses a `net_device` object with various functions like `struct net_device_ops`.
> +/// This object is passed to Rust callbacks while these functions are running.
> +/// The C API guarantees that `net_device` isn't released while this function is running.
> +pub struct Device(*mut bindings::net_device);

This is not a function :) So "while this function is running" does not
make too much sense here.

Also, this is a `struct`, so we do not use `# Safety` sections.
Instead, you may want to give this struct a  `# Invariants` section,
and explain what is guaranteed by this type.

Similarly for `SkBuff` below.

> +    fn priv_data_ptr(netdev: *mut bindings::net_device) -> *const c_void {
> +        // SAFETY: The safety requirement ensures that the pointer is valid.
> +        unsafe { core::ptr::read(bindings::netdev_priv(&mut *netdev) as *const *const c_void) }
> +    }

Like in the other patch, there is no safety requirement in this
function, so you can't use that to justify it.

Even if this is a private function, it would be best if this is
`unsafe`, then you can require callers to pass you a valid pointer --
you already have a `SAFETY` comment in the callers anyway, and those
are the ones that can actually claim that the pointer is valid.

> +// SAFETY: `Device` exposes the state, `D::Data` object across threads
> +// but that type is required to be Send and Sync.
> +unsafe impl Send for Device {}
> +unsafe impl Sync for Device {}

Please provide a `SAFETY` comment for each. See e.g. the ones we have
for `ARef`.

In addition, what is `D::Data` here? I guess you are referring to
`DriverData::Data` somehow, but it is unclear what is `D` here.

Also, please use Markdown in comments, not just documentation, to be
consistent, e.g.

    ... to be `Send` and `Sync`.

> +/// # Safety
> +///
> +/// The pointer to the `net_device` object is guaranteed to be valid until
> +/// the registration object is dropped.
> +pub struct Registration<T: DeviceOperations<D>, D: DriverData> {
> +    dev: Device,
> +    is_registered: bool,
> +    _p: PhantomData<(D, T)>,
> +}

Ditto about this being a `struct` and `# Safety`.

Also, if it is valid until dropped, then it is "always" valid (as far
as someone having such an object is concerned), so there is no need to
say so.

> +    fn drop(&mut self) {
> +        // SAFETY: `dev` was allocated during initialization and guaranteed to be valid.

Do you mean `dev.0`?

> +        // SAFETY: the kernel allocated the space for a pointer.

Please capitalize to be consistent.

> +        unsafe {
> +            let priv_ptr = bindings::netdev_priv(ptr) as *mut *const c_void;
> +            core::ptr::write(priv_ptr, data.into_foreign());
> +        }

> +        ..unsafe { core::mem::MaybeUninit::<bindings::net_device_ops>::zeroed().assume_init() }

A comment on this would be nice. Also, missing `SAFETY` comment, even
if it is a `const`.

Thanks!

Cheers,
Miguel

  parent reply	other threads:[~2023-06-10 19:50 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20230610071848.3722492-1-tomo@exabit.dev>
2023-06-10  7:20 ` [PATCH v2 1/5] rust: core abstractions for network device drivers FUJITA Tomonori
2023-06-10 14:11   ` Andrew Lunn
2023-06-11  8:03     ` Alice Ryhl
2023-06-11 15:30       ` Andrew Lunn
2023-06-11 17:48         ` Miguel Ojeda
2023-06-12  6:47     ` FUJITA Tomonori
2023-06-12 12:46       ` Andrew Lunn
2023-06-10 19:49   ` Miguel Ojeda [this message]
2023-06-12  5:04     ` FUJITA Tomonori
2023-06-12 13:26       ` Miguel Ojeda
2023-06-10  7:20 ` [PATCH v2 5/5] samples: rust: add dummy network driver FUJITA Tomonori
2023-06-10 16:59   ` Andrew Lunn
2023-06-12  7:02     ` FUJITA Tomonori
2023-06-10 19:14   ` Miguel Ojeda
2023-06-10  7:20 ` [PATCH v2 2/5] rust: add support for ethernet operations FUJITA Tomonori
2023-06-10 16:48   ` Andrew Lunn
2023-06-12  6:51     ` FUJITA Tomonori
2023-06-10 19:14   ` Miguel Ojeda
2023-06-12  8:51     ` FUJITA Tomonori
2023-06-12 13:35       ` Miguel Ojeda
2023-06-10  7:20 ` [PATCH v2 3/5] rust: add support for get_stats64 in struct net_device_ops FUJITA Tomonori
2023-06-10  7:20 ` [PATCH v2 4/5] rust: add methods for configure net_device FUJITA Tomonori
2023-07-10  7:36 [PATCH v2 0/5] Rust abstractions for network device drivers FUJITA Tomonori
2023-07-10  7:36 ` [PATCH v2 1/5] rust: core " FUJITA Tomonori
2023-07-14 18:59   ` Benno Lossin

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=CANiq72ki5CEY4QpOW63YTAU8_2OB8rS0FEuY-qXyncahkNUMFg@mail.gmail.com \
    --to=miguel.ojeda.sandonis@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=andrew@lunn.ch \
    --cc=fujita.tomonori@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tomo@exabit.dev \
    /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).