[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Z_lZWPr1vBCrYbkA@cassiopeiae>
Date: Fri, 11 Apr 2025 20:03:04 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: Yury Norov <yury.norov@...il.com>
Cc: Viresh Kumar <viresh.kumar@...aro.org>,
"Rafael J. Wysocki" <rafael@...nel.org>,
Miguel Ojeda <miguel.ojeda.sandonis@...il.com>,
Danilo Krummrich <dakr@...hat.com>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Thomas Gleixner <tglx@...utronix.de>,
Peter Zijlstra <peterz@...radead.org>, linux-pm@...r.kernel.org,
Vincent Guittot <vincent.guittot@...aro.org>,
Stephen Boyd <sboyd@...nel.org>, Nishanth Menon <nm@...com>,
rust-for-linux@...r.kernel.org,
Manos Pitsidianakis <manos.pitsidianakis@...aro.org>,
Erik Schilling <erik.schilling@...aro.org>,
Alex Bennée <alex.bennee@...aro.org>,
Joakim Bech <joakim.bech@...aro.org>, Rob Herring <robh@...nel.org>,
Burak Emir <bqe@...gle.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Russell King <linux@...linux.org.uk>, linux-clk@...r.kernel.org,
Michael Turquette <mturquette@...libre.com>,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH V9 09/17] rust: cpu: Add from_cpu()
On Fri, Apr 11, 2025 at 12:18:26PM -0400, Yury Norov wrote:
> On Fri, Apr 11, 2025 at 04:25:08PM +0530, Viresh Kumar wrote:
> > +/// Creates a new instance of CPU's device.
> > +///
> > +/// # Safety
> > +///
> > +/// Reference counting is not implemented for the CPU device in the C code. When a CPU is
> > +/// hot-unplugged, the corresponding CPU device is unregistered, but its associated memory
> > +/// is not freed.
> > +///
> > +/// Callers must ensure that the CPU device is not used after it has been unregistered.
> > +/// This can be achieved, for example, by registering a CPU hotplug notifier and removing
> > +/// any references to the CPU device within the notifier's callback.
> > +pub unsafe fn from_cpu(cpu: u32) -> Result<&'static Device> {
> > + // SAFETY: The pointer returned by `get_cpu_device()`, if not `NULL`, is a valid pointer to
> > + // a `struct device` and is never freed by the C code.
> > + let ptr = unsafe { bindings::get_cpu_device(cpu) };
You don't need to justify the return value here, you need to justify why cpu is
a valid value to call get_cpu_device() with.
> > + if ptr.is_null() {
> > + return Err(ENODEV);
> > + }
> > +
> > + // SAFETY: The pointer returned by `get_cpu_device()`, if not `NULL`, is a valid pointer to
> > + // a `struct device` and is never freed by the C code.
Here, the comment is good.
> If you think that writing the same thing twice would help in SAFETY
> department, I'd recommend you to write it for a couple more times.
Fixing the above, should also fix the duplication. :)
> > + Ok(unsafe { Device::as_ref(ptr) })
Powered by blists - more mailing lists