Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4449044rwl; Mon, 10 Apr 2023 11:10:14 -0700 (PDT) X-Google-Smtp-Source: AKy350aU1Be4SQJRSQ4Z1LMN4JAbnxqAdw+jx1Lj2PEQHhnJl/x2OtKzS8NKtP3vmZ8cJDgzaGIC X-Received: by 2002:a17:907:9708:b0:92e:3944:716d with SMTP id jg8-20020a170907970800b0092e3944716dmr9291966ejc.3.1681150214469; Mon, 10 Apr 2023 11:10:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681150214; cv=none; d=google.com; s=arc-20160816; b=GvS2+shVlejVTGZqhO7BB6xsGGGQdRsUgucJO97aeGRKh0dnEVD+t8Q9wdN5JsMq+S jVvgbmSKbW9UaEUF91ONyNV/voEOf94Ow2hdNfAInAMOJOYmlVevJKWZwi4y7RdZv8aV VDlC/oxLaaOBLVLZrpvmn+7RE4ENUfDbrFpeaYqCoXJ9iIW+KNsHz8+9EPn1RgOtoV/t vx5Ys4z9xjujzRBePI4hvoieRROP3kcPKXJ/vyZ27mchmeWl68Cm9DlAOfg6/ECTLHas 4URv6wJK7HJ7X1ojU7qRLnYMqK+KZtNJaQFGqx3fhN6NrCzJK4PNZhg/FLIazC/wrqhD vy6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=U23Pxj6RmRXluaupeIIMF2+OgyhNmuLdOBsJ8ac8t5U=; b=UnBo1SMEnUbSeI6Zu3PaCOKGu4g9pHjYX3C9te8Iv7c2Z06Hyqy53sqNXDdnpi/Dn4 3DkXLQg808zqmKjkWnhKLrQMssjcQGYMdGitK8reOPXasEAlwiln+0SeQ/LYCPqb+ytc D4WN6lSE14eCZOzpmBnwdNB6eLrCU3d8vko5hGAvmpbGUlMkkhq7zEAZTPt75GFGEMno G547aZi6qfMfEEyDcp+ERchiY6Yu5FKwTujqvlPROCu60xiWtT0jqBJ9/xlcfTj8e5qr RGoS9IKI0kyp9n5Y5LyTg7i/rgWiRgXNxG+kdmqpG2iIHAW+ysDLwzdQ8gPf6R8WvJSW KjvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=jqTai2Oj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g10-20020a056402114a00b00504805f54adsi61839edw.387.2023.04.10.11.09.49; Mon, 10 Apr 2023 11:10:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=jqTai2Oj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230399AbjDJSFa (ORCPT + 99 others); Mon, 10 Apr 2023 14:05:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230373AbjDJSF0 (ORCPT ); Mon, 10 Apr 2023 14:05:26 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 514801BC1; Mon, 10 Apr 2023 11:05:23 -0700 (PDT) Date: Mon, 10 Apr 2023 18:04:51 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1681149919; x=1681409119; bh=U23Pxj6RmRXluaupeIIMF2+OgyhNmuLdOBsJ8ac8t5U=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=jqTai2OjeGJSWBhhS2vtfoWFT8IrFEzaTAx2Es/P9khzhP5bfX1SZGcqSSDlrgi1P uBgaWpDxOAwW8OMqF7J6n+9PcR5ZOpH7AFCYN1UyF3joWFzYFL7kZB13R0c27WVMtL X8e9g/qczUxDoK7eS/HL7fdDpscy2RQBUHsERv3aXl7a36pHp6L0zWng7x2KdkOs3m qNMlAcI1OkPCGk2aER5BJirri/djpfU3HrZGrrC+1ZHmQPh8C5QUhEqjBjgwFcv7C5 4/18mM8O3mc6P9Sndp1UGvrDNORCQ0t3IizJAx7uJumUFDNbLaXzXUVIHRKuyeSV/h 3Pj41pZx7ookQ== To: Wedson Almeida Filho , rust-for-linux@vger.kernel.org From: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , linux-kernel@vger.kernel.org, Wedson Almeida Filho , Ingo Molnar , Peter Zijlstra Subject: Re: [PATCH v3 10/13] rust: introduce `current` Message-ID: <08df022b-c0e9-bac6-a57f-296217dc81ed@protonmail.com> In-Reply-To: <20230408075340.25237-10-wedsonaf@gmail.com> References: <20230408075340.25237-1-wedsonaf@gmail.com> <20230408075340.25237-10-wedsonaf@gmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08.04.23 09:53, Wedson Almeida Filho wrote: > From: Wedson Almeida Filho > > This allows Rust code to get a reference to the current task without > having to increment the refcount, but still guaranteeing memory safety. > > Cc: Ingo Molnar > Cc: Peter Zijlstra > Signed-off-by: Wedson Almeida Filho > --- > v1 -> v2: Make `current` a macro to prevent it from escaping the caller > v2 -> v3: > - Mention `current` macro in `Task::current` > - Hide implementation of `TaskRef` inside `Task::current` > > rust/helpers.c | 6 +++ > rust/kernel/prelude.rs | 2 + > rust/kernel/task.rs | 88 +++++++++++++++++++++++++++++++++++++++++- > 3 files changed, 95 insertions(+), 1 deletion(-) > > diff --git a/rust/helpers.c b/rust/helpers.c > index 58a194042c86..96441744030e 100644 > --- a/rust/helpers.c > +++ b/rust/helpers.c > @@ -100,6 +100,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r= ) > } > EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); > > +struct task_struct *rust_helper_get_current(void) > +{ > +=09return current; > +} > +EXPORT_SYMBOL_GPL(rust_helper_get_current); > + > void rust_helper_get_task_struct(struct task_struct *t) > { > =09get_task_struct(t); > diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs > index fcdc511d2ce8..c28587d68ebc 100644 > --- a/rust/kernel/prelude.rs > +++ b/rust/kernel/prelude.rs > @@ -36,3 +36,5 @@ pub use super::error::{code::*, Error, Result}; > pub use super::{str::CStr, ThisModule}; > > pub use super::init::{InPlaceInit, Init, PinInit}; > + > +pub use super::current; > diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs > index d70cad131956..5269a562cb1b 100644 > --- a/rust/kernel/task.rs > +++ b/rust/kernel/task.rs > @@ -5,7 +5,17 @@ > //! C header: [`include/linux/sched.h`](../../../../include/linux/sched= .h). > > use crate::{bindings, types::Opaque}; > -use core::ptr; > +use core::{marker::PhantomData, ops::Deref, ptr}; > + > +/// Returns the currently running task. > +#[macro_export] > +macro_rules! current { > + () =3D> { > + // SAFETY: Deref + addr-of below create a temporary `TaskRef` th= at cannot outlive the > + // caller. > + unsafe { &*$crate::task::Task::current() } > + }; > +} > > /// Wraps the kernel's `struct task_struct`. > /// > @@ -15,6 +25,42 @@ use core::ptr; > /// > /// Instances of this type are always ref-counted, that is, a call to `= get_task_struct` ensures > /// that the allocation remains valid at least until the matching call = to `put_task_struct`. > +/// > +/// # Examples > +/// > +/// The following is an example of getting the PID of the current thread= with zero additional cost > +/// when compared to the C version: > +/// > +/// ``` > +/// let pid =3D current!().pid(); > +/// ``` > +/// > +/// Getting the PID of the current process, also zero additional cost: > +/// > +/// ``` > +/// let pid =3D current!().group_leader().pid(); > +/// ``` > +/// > +/// Getting the current task and storing it in some struct. The referenc= e count is automatically > +/// incremented when creating `State` and decremented when it is dropped= : > +/// > +/// ``` > +/// use kernel::{task::Task, types::ARef}; > +/// > +/// struct State { > +/// creator: ARef, > +/// index: u32, > +/// } > +/// > +/// impl State { > +/// fn new() -> Self { > +/// Self { > +/// creator: current!().into(), > +/// index: 0, > +/// } > +/// } > +/// } > +/// ``` > #[repr(transparent)] > pub struct Task(pub(crate) Opaque); > > @@ -27,6 +73,46 @@ unsafe impl Sync for Task {} > type Pid =3D bindings::pid_t; > > impl Task { > + /// Returns a task reference for the currently executing task/thread= . > + /// > + /// The recommended way to get the current task/thread is to use the > + /// [`current`](crate::current) macro because it is safe. > + /// > + /// # Safety > + /// > + /// Callers must ensure that the returned object doesn't outlive the= current task/thread. > + pub unsafe fn current() -> impl Deref { > + pub struct TaskRef<'a> { > + task: &'a Task, > + _not_send: PhantomData<*mut ()>, > + } > + > + impl Deref for TaskRef<'_> { > + type Target =3D Task; > + > + fn deref(&self) -> &Self::Target { > + self.task > + } > + } > + > + impl From> for crate::types::ARef { > + fn from(t: TaskRef<'_>) -> Self { > + t.deref().into() > + } > + } I think we can remove this `From` impl, since the type is never exposed to the outside (there still is the `From<&T> for ARef` impl, so users can still do `current!().into()` to get an `ARef`). -- Cheers, Benno > + > + // SAFETY: Just an FFI call with no additional safety requiremen= ts. > + let ptr =3D unsafe { bindings::get_current() }; > + > + TaskRef { > + // SAFETY: If the current thread is still running, the curre= nt task is valid. Given > + // that `TaskRef` is not `Send`, we know it cannot be transf= erred to another thread > + // (where it could potentially outlive the caller). > + task: unsafe { &*ptr.cast() }, > + _not_send: PhantomData, > + } > + } > + > /// Returns the group leader of the given task. > pub fn group_leader(&self) -> &Task { > // SAFETY: By the type invariant, we know that `self.0` is a va= lid task. Valid tasks always > -- > 2.34.1 >