Message ID | 20230119174036.64046-4-wedsonaf@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp461129wrn; Thu, 19 Jan 2023 09:47:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXtn5Al5KebqioVPoOCqwuVhSxZuBGhKs7hhBfc/2AMl2ulQOPyPV7n2jCZNjsMaQzNt4xRG X-Received: by 2002:aa7:84c1:0:b0:58d:9b18:a36e with SMTP id x1-20020aa784c1000000b0058d9b18a36emr11923193pfn.31.1674150473730; Thu, 19 Jan 2023 09:47:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674150473; cv=none; d=google.com; s=arc-20160816; b=ulEDWuFy3827BiJewJfZZzzi2X8AY5CzMs3cGik8Hlzl3VBk6LON3TF9qf6s1Tscx9 +fJrBkAUKVrD/ndwqIgVGOCoT4ARLjb24OZiLWszbno9Smay9UFZ4zWGvEMkWPanmC3g 05AhORNefMrcyPTiFENwlzmw+E36PgbpwRk+4Dopwcf8CLAtLWb1ceuFfo3+0vMvsLMP kcC9bo4eSZphWbyKrPip6daum6bh1NzR1bGUWzPxUVixUILNph1c49KwhZsWU3SyLMr1 MuGQO7zKL3+l4xCJFdoz70uvgQpniErwuQBk7oeENbfSk9+Q/HLtv9sTffYOCCMd77td PRkQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NO/tLxVBmWB1dJAexFMTC87LMSP1V8gc/xUgEhy6fvI=; b=Nv7K6gYhun/KL08u7nIGAB10q+xxUnFzgcE4hgmN2d6eg12r+TjTUdozhkXnE1LvFP +g/uRrz3szG6fjha0pO99V5N5ofuUL27itdclQarM8LfuPSkXD0pVAV60YHNmbDTOSSW GZqfyM8dVN4lt6OuGF80obXEosTTaZh7G7VAD7jhxVXFWKQQK4KTg8iPm/h1QYcwRyoD EWIRg/7pG8bBouUt6Mtz0RqnT4qYF5P8e9oHdXESRN4AvuQrefpGnrqJM7ecAqgLODiW NMdtG02481FoaPct6Uem+bAv2s7acZiJMh6W6mbNhPqJKlH2dur3LRL54vUPD393ItL3 HzkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="YBF/bv8H"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y71-20020a62644a000000b0058a71b56a06si27772240pfb.130.2023.01.19.09.47.41; Thu, 19 Jan 2023 09:47:53 -0800 (PST) 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=@gmail.com header.s=20210112 header.b="YBF/bv8H"; 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=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230089AbjASRld (ORCPT <rfc822;pfffrao@gmail.com> + 99 others); Thu, 19 Jan 2023 12:41:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229609AbjASRlW (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 19 Jan 2023 12:41:22 -0500 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0AE2798F9; Thu, 19 Jan 2023 09:41:08 -0800 (PST) Received: by mail-oi1-x22c.google.com with SMTP id v17so2297841oie.5; Thu, 19 Jan 2023 09:41:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NO/tLxVBmWB1dJAexFMTC87LMSP1V8gc/xUgEhy6fvI=; b=YBF/bv8HlX6jUtB0z4bb1xtYgg4EdDkd80BPaxQb8LIoAa6fmzgul3M6qSS5yRI8Cy fGIybIuQB5cT1uuCDHK3kr9/mIHcetjAftYjO88JzxaBX8e5VWpkkxPeu/gwtlmfwySs VUy4NLBcyyxcgsWBDrf78+meibyXGyuM4zzRsXhSpU5LrIWbeXMSjwbopFHonDvUeJag pYYIYKAD2jAqT2JzLQLfu5E1Iqk6teivVTyqrFiOhABiX1/yEc8lpFrYnFfj3TUGHiVK m88jKaFFsb/qKwQ0K9mlhfcJlgzHvGqzU1zR+vl0tVUIbsbu5f7FqlHMeSgZQqyTghny OCJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NO/tLxVBmWB1dJAexFMTC87LMSP1V8gc/xUgEhy6fvI=; b=ZZ0MyAIDcewrtMHq9udoGHkTzPVtU9QbsqSRRaKXKC+ExaalGiOo6EH1H1CFcowBcz 0x0kf/268TS7b+w/DY6P3Q+7so51m4lH4FdbYlPOLAd88ZNMtZdxKvAlPm+YMJE/hnT+ F4XSFWJPskaLw7tqCPvvP+TZ9OdMVW2B92BQwIz+YBYfDlTdTIp7EmSRWEAzz2Dc4ncO tFfh4/CgrRWWUBYHjAxXwt+vR8WKnURE5cZ/ElAXwPGXX/Md9hIyhngeJ2tW6NgE9g3x ose5x9e000yQ2e9G8iMqTcM504nkGEYEsbsfy1BoGJlnGyjmSAtgpAUsTEf4xnR+s3qx Cgzg== X-Gm-Message-State: AFqh2kq0s8BI8EsZ/DrzAH/0hC2vM+uQUUs1jKmEsYArmjiaxGx9rts5 FmBLlbdoPeOEyZyyAvtTlyVCXar7Lh729VYj X-Received: by 2002:aca:1710:0:b0:36e:b552:1ec3 with SMTP id j16-20020aca1710000000b0036eb5521ec3mr576626oii.38.1674150068191; Thu, 19 Jan 2023 09:41:08 -0800 (PST) Received: from wedsonaf-dev.home.lan ([189.124.190.154]) by smtp.googlemail.com with ESMTPSA id i7-20020a056808054700b003631fe1810dsm10226906oig.47.2023.01.19.09.41.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 09:41:08 -0800 (PST) From: Wedson Almeida Filho <wedsonaf@gmail.com> To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?B?= =?utf-8?q?j=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, linux-kernel@vger.kernel.org, Wedson Almeida Filho <wedsonaf@gmail.com> Subject: [PATCH 4/5] rust: types: implement `ForeignOwnable` for the unit type Date: Thu, 19 Jan 2023 14:40:35 -0300 Message-Id: <20230119174036.64046-4-wedsonaf@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230119174036.64046-1-wedsonaf@gmail.com> References: <20230119174036.64046-1-wedsonaf@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755474006769420102?= X-GMAIL-MSGID: =?utf-8?q?1755474006769420102?= |
Series |
[1/5] rust: types: introduce `ScopeGuard`
|
|
Commit Message
Wedson Almeida Filho
Jan. 19, 2023, 5:40 p.m. UTC
This allows us to use the unit type `()` when we have no object whose
ownership must be managed but one implementing the `ForeignOwnable`
trait is needed.
Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
---
rust/kernel/types.rs | 13 +++++++++++++
1 file changed, 13 insertions(+)
Comments
On Thu, 19 Jan 2023 14:40:35 -0300 Wedson Almeida Filho <wedsonaf@gmail.com> wrote: > This allows us to use the unit type `()` when we have no object whose > ownership must be managed but one implementing the `ForeignOwnable` > trait is needed. > > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > --- > rust/kernel/types.rs | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index e037c262f23e..8f80cffbff59 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -85,6 +85,19 @@ impl<T: 'static> ForeignOwnable for Box<T> { > } > } > > +impl ForeignOwnable for () { > + type Borrowed<'a> = (); > + > + fn into_foreign(self) -> *const core::ffi::c_void { > + // We use 1 to be different from a null pointer. > + 1usize as _ this should really be `core::ptr::invalid(1)`. That's currently unstable, but can be equivalently written as `NonNull::<()>::dangling().as_ptr()`. This has a different semantic meaning from `as` since it explicitly suggests an invalid provenance and thus will not alias with other pointers. (Although I don't think compiler currently can take advantage of this fact yet) > + } > + > + unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {} > + > + unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} > +} > + > /// Runs a cleanup function/closure when dropped. > /// > /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
On Fri, Jan 27, 2023 at 3:03 PM Gary Guo <gary@garyguo.net> wrote: > > this should really be `core::ptr::invalid(1)`. That's currently > unstable, but can be equivalently written as > `NonNull::<()>::dangling().as_ptr()`. > > This has a different semantic meaning from `as` since it explicitly > suggests an invalid provenance and thus will not alias with other > pointers. (Although I don't think compiler currently can take advantage > of this fact yet) We talked about starting to use `strict_provenance` when it came out -- what is the latest status? i.e. do you know if it is expected that it will pass FCP etc.? (my understanding originally was that it was an experiment). If it is likely to become stable, then I agree it could be nice to start using it before a lot of kernel code gets written without it. Thanks Gary! Cheers, Miguel
On Fri Jan 27, 2023 at 3:11 PM CET, Miguel Ojeda wrote: > On Fri, Jan 27, 2023 at 3:03 PM Gary Guo <gary@garyguo.net> wrote: > > > > this should really be `core::ptr::invalid(1)`. That's currently > > unstable, but can be equivalently written as > > `NonNull::<()>::dangling().as_ptr()`. > > > > This has a different semantic meaning from `as` since it explicitly > > suggests an invalid provenance and thus will not alias with other > > pointers. (Although I don't think compiler currently can take advantage > > of this fact yet) > > We talked about starting to use `strict_provenance` when it came out > -- what is the latest status? i.e. do you know if it is expected that > it will pass FCP etc.? (my understanding originally was that it was an > experiment). From what I remember the feeling was positing into hace `strict_provenance` Here is the last meeting that was back in August https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Stabilizing.20strict.20provenance.20APIs.3F I guess, we could just put a fix me around the actual code, I feel that the Gary change deserve a own patch with the own description. Cheers! Vincent.
On Thu Jan 19, 2023 at 6:40 PM CET, Wedson Almeida Filho wrote: > This allows us to use the unit type `()` when we have no object whose > ownership must be managed but one implementing the `ForeignOwnable` > trait is needed. > > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com> > --- > rust/kernel/types.rs | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index e037c262f23e..8f80cffbff59 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -85,6 +85,19 @@ impl<T: 'static> ForeignOwnable for Box<T> { > } > } > > +impl ForeignOwnable for () { > + type Borrowed<'a> = (); > + > + fn into_foreign(self) -> *const core::ffi::c_void { > + // We use 1 to be different from a null pointer. > + 1usize as _ > + } > + > + unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {} > + > + unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} > +} > + > /// Runs a cleanup function/closure when dropped. > /// > /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running. > -- > 2.34.1
On Fri, 27 Jan 2023 at 11:03, Gary Guo <gary@garyguo.net> wrote: > > On Thu, 19 Jan 2023 14:40:35 -0300 > Wedson Almeida Filho <wedsonaf@gmail.com> wrote: > > > This allows us to use the unit type `()` when we have no object whose > > ownership must be managed but one implementing the `ForeignOwnable` > > trait is needed. > > > > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > > --- > > rust/kernel/types.rs | 13 +++++++++++++ > > 1 file changed, 13 insertions(+) > > > > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > > index e037c262f23e..8f80cffbff59 100644 > > --- a/rust/kernel/types.rs > > +++ b/rust/kernel/types.rs > > @@ -85,6 +85,19 @@ impl<T: 'static> ForeignOwnable for Box<T> { > > } > > } > > > > +impl ForeignOwnable for () { > > + type Borrowed<'a> = (); > > + > > + fn into_foreign(self) -> *const core::ffi::c_void { > > + // We use 1 to be different from a null pointer. > > + 1usize as _ > > this should really be `core::ptr::invalid(1)`. That's currently > unstable, but can be equivalently written as > `NonNull::<()>::dangling().as_ptr()`. This has the wrong type, but I agree with the spirit of the suggestion. I'll add it to V2. > This has a different semantic meaning from `as` since it explicitly > suggests an invalid provenance and thus will not alias with other > pointers. (Although I don't think compiler currently can take advantage > of this fact yet) > > > + } > > + > > + unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {} > > + > > + unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} > > +} > > + > > /// Runs a cleanup function/closure when dropped. > > /// > > /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running. >
On Sat, Jan 28, 2023 at 12:13 PM Vincenzo Palazzo <vincenzopalazzodev@gmail.com> wrote: > > From what I remember the feeling was positing into hace `strict_provenance` > > Here is the last meeting that was back in August > https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Stabilizing.20strict.20provenance.20APIs.3F Thanks Vincent! Yeah, as far as I understand Gary was in that meeting (in October). If there has not been anything after that, then I guess it is still the case that provenance will probably happen (for better or worse, depending on the perspective :) and that a subset of the APIs may get stabilized first. Cheers, Miguel
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index e037c262f23e..8f80cffbff59 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -85,6 +85,19 @@ impl<T: 'static> ForeignOwnable for Box<T> { } } +impl ForeignOwnable for () { + type Borrowed<'a> = (); + + fn into_foreign(self) -> *const core::ffi::c_void { + // We use 1 to be different from a null pointer. + 1usize as _ + } + + unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {} + + unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} +} + /// Runs a cleanup function/closure when dropped. /// /// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.