mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Yury Norov" <yury.norov@gmail.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH V3 3/3] rust: cpu: Add CpuId::current() to retrieve current CPU ID
Date: Tue, 10 Jun 2025 14:23:13 -0700	[thread overview]
Message-ID: <aEiiQSTe-U1q0fOL@tardis.local> (raw)
In-Reply-To: <a2e12436661fca452de5c417242328ed6f413511.1749554685.git.viresh.kumar@linaro.org>

On Tue, Jun 10, 2025 at 06:51:58PM +0530, Viresh Kumar wrote:
> Introduce `CpuId::current()`, a constructor that wraps the C function
> `raw_smp_processor_id()` to retrieve the current CPU identifier without
> guaranteeing stability.
> 
> This function should be used only when the caller can ensure that
> the CPU ID won't change unexpectedly due to preemption or migration.
> 
> Suggested-by: Boqun Feng <boqun.feng@gmail.com>
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  MAINTAINERS            |  1 +
>  rust/helpers/cpu.c     |  8 ++++++++
>  rust/helpers/helpers.c |  1 +
>  rust/kernel/cpu.rs     | 10 ++++++++++
>  4 files changed, 20 insertions(+)
>  create mode 100644 rust/helpers/cpu.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index a92290fffa16..4255186784c4 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -6254,6 +6254,7 @@ F:	include/linux/cpuhotplug.h
>  F:	include/linux/smpboot.h
>  F:	kernel/cpu.c
>  F:	kernel/smpboot.*
> +F:	rust/helper/cpu.c
>  F:	rust/kernel/cpu.rs
>  
>  CPU IDLE TIME MANAGEMENT FRAMEWORK
> diff --git a/rust/helpers/cpu.c b/rust/helpers/cpu.c
> new file mode 100644
> index 000000000000..824e0adb19d4
> --- /dev/null
> +++ b/rust/helpers/cpu.c
> @@ -0,0 +1,8 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/smp.h>
> +
> +unsigned int rust_helper_raw_smp_processor_id(void)
> +{
> +	return raw_smp_processor_id();
> +}
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 0f1b5d115985..16fa9bca5949 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -13,6 +13,7 @@
>  #include "build_assert.c"
>  #include "build_bug.c"
>  #include "clk.c"
> +#include "cpu.c"
>  #include "cpufreq.c"
>  #include "cpumask.c"
>  #include "cred.c"
> diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs
> index 7549594fad7f..a946c8a9d1a2 100644
> --- a/rust/kernel/cpu.rs
> +++ b/rust/kernel/cpu.rs
> @@ -102,6 +102,16 @@ pub fn from_u32(id: u32) -> Option<Self> {
>      pub fn as_u32(&self) -> u32 {
>          self.0
>      }
> +
> +    /// Returns the ID of the CPU the code is currently running on.
> +    ///
> +    /// The returned value is considered unstable because it may change
> +    /// unexpectedly due to preemption or CPU migration. It should only be
> +    /// used when the context ensures that the task remains on the same CPU.

I would also add "or the users could use a stale (yet valid) CPU ID",
because there are such usages in kernel arleady, e.g.
kvm_pmu_probe_armpmu() in arch/arm64/kvm/pmu-emul.c.

With that, feel free to add:

Reviewed-by: Boqun Feng <boqun.feng@gmail.com>

Regards,
Boqun

> +    pub fn current() -> Self {
> +        // SAFETY: raw_smp_processor_id() always returns a valid CPU ID.
> +        unsafe { Self::from_u32_unchecked(bindings::raw_smp_processor_id()) }
> +    }
>  }
>  
>  impl From<CpuId> for u32 {
> -- 
> 2.31.1.272.g89b43f80a514
> 

  reply	other threads:[~2025-06-10 21:23 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-10 13:21 [PATCH V3 0/3] rust: Introduce CpuId and fix cpumask doctest Viresh Kumar
2025-06-10 13:21 ` [PATCH V3 1/3] rust: cpu: Introduce CpuId abstraction Viresh Kumar
2025-06-11 16:07   ` Boqun Feng
2025-06-10 13:21 ` [PATCH V3 2/3] rust: Use CpuId in place of raw CPU numbers Viresh Kumar
2025-06-11 16:12   ` Boqun Feng
2025-06-12  5:01     ` Viresh Kumar
2025-06-13 15:40       ` Boqun Feng
2025-06-10 13:21 ` [PATCH V3 3/3] rust: cpu: Add CpuId::current() to retrieve current CPU ID Viresh Kumar
2025-06-10 21:23   ` Boqun Feng [this message]
2025-06-11  2:28     ` Viresh Kumar
2025-06-10 17:10 ` [PATCH V3 0/3] rust: Introduce CpuId and fix cpumask doctest Miguel Ojeda
2025-06-11  2:18   ` Viresh Kumar

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=aEiiQSTe-U1q0fOL@tardis.local \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tmgross@umich.edu \
    --cc=vincent.guittot@linaro.org \
    --cc=viresh.kumar@linaro.org \
    --cc=yury.norov@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

all inboxes | Powered by JetHome®