Message ID | 20230224-rust-error-v1-1-f8f9a9a87303@asahilina.net |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp1205291dyk; Fri, 24 Feb 2023 01:02:57 -0800 (PST) X-Google-Smtp-Source: AK7set9AsmquNPpObmkaa4KYULcIgo7Lmt9QM0CvH8e+2YvJ881ea0L8Mm3D3pAuph9mCpOnW3Zn X-Received: by 2002:a05:6a00:1da3:b0:5a8:16b7:ce6e with SMTP id z35-20020a056a001da300b005a816b7ce6emr13114969pfw.10.1677229236036; Fri, 24 Feb 2023 01:00:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229236; cv=none; d=google.com; s=arc-20160816; b=eSPrz/JNLXQOojXAbets3dZsKS5NsuXq1RjrkZiBd/yKOd3SfIJcVFAAIvtOxvc2iI +TPb9eA8jxQBGyj8HBg/Z+dyGmFMduTOwCDX4ES8s4Viy40wcIwZZky5A+YpdCh3qDsQ YKY03P9zvChBqDoUpEt1HGWiKWP9vzJexluU1bpSN/9vK7xwY2AAoTz/pYqet7tYZfEe 4yldKVhGjNx8mKscVMPD/YbLgXHfttwggdsdVhlpXjbxkk+JIQa9lqM6eIrLS5msxcel WNFgn82oBB6uJ1Js7Pp6owvhk6i81Se1qZINCt7zV4pbe0+9MD0TIjrcRIDt4ECxsYsG vI7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=zSV4g6O++1o+wKdxtpYa3fo7cK2PVaTSiWDCmeKR+Tg=; b=t5OmkxJiFEBGHAK56mwwA2LEXtD5DTqSm8T7rt8Vhr5D06jbpgKLiyV5affWa/ejLt lx9RG6DYGP0oy+2HVfHYiIPGJI2oME9Zc/C4VRBi1gGq1XICENcJAJNfkJEguOny6PW9 QjSmWAbqow7Lbc4Qff1tsgRShHWnoxnSclC5zc0OIa0JCXMIgtYE77ZDWmpPgABg0lBZ 8Og7uArlYYaq4hqX4TkoaOA7Gif0M3XFGW/dBPrwJjKt9KKpUQbXh4XYuqKGO02XJbSx hRoMPyOwmNNcL/ZdQ190/QNxp//nqMbSRpiinU2Dz4OZqtgEsJIkZDDt1xkrt7IcArqg 5eJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=rxZyMibj; 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=asahilina.net Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x196-20020a6286cd000000b005d6a249a5cfsi6828380pfd.0.2023.02.24.01.00.22; Fri, 24 Feb 2023 01:00:36 -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=@asahilina.net header.s=default header.b=rxZyMibj; 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=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229861AbjBXIun (ORCPT <rfc822;jeff.pang.chn@gmail.com> + 99 others); Fri, 24 Feb 2023 03:50:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229758AbjBXIuj (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 24 Feb 2023 03:50:39 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE38526AA; Fri, 24 Feb 2023 00:50:38 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 31A103FB17; Fri, 24 Feb 2023 08:50:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677228637; bh=M10zbadndwKZhD79Kr8HJcwiw4rH1LF/7cTiGWMgTk8=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=rxZyMibjaZ6LZEVtlYDJAKnfIA8G8nCJ6WBhvtUFKt+oa44YUYkNytz/nRl7PIjK3 KsovDYXQ7NxK4j0FoX30fybbD/sTebMJBDIoqR9KSzLGyc8Lp+IO16y+OFsXKltDzy BobDpVsTTYVYOONz883rwyRgu1dv5Xlv+EEDpbCbfJtiTGRoTrf4l7KtudA93X4BTk Uf6hMIV46bR6OCV8WYI7OtjxwKTdWlAUUdfdYBzvRUfpssj719iFfVcqpQcdCasLRe Uq7EB3xIgb9z2DGg/xJgsHcQFIt/bd7Vkm/5htImkA6ybhaCY1hxgEvrC086rTLWo2 Q6uig72WcEk6g== From: Asahi Lina <lina@asahilina.net> Date: Fri, 24 Feb 2023 17:50:19 +0900 Subject: [PATCH 1/5] rust: error: Add Error::to_ptr() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230224-rust-error-v1-1-f8f9a9a87303@asahilina.net> References: <20230224-rust-error-v1-0-f8f9a9a87303@asahilina.net> In-Reply-To: <20230224-rust-error-v1-0-f8f9a9a87303@asahilina.net> To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Wedson Almeida Filho <wedsonaf@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Sven Van Asbroeck <thesven73@gmail.com> Cc: Fox Chen <foxhlchen@gmail.com>, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina <lina@asahilina.net> X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1677228630; l=1624; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=M10zbadndwKZhD79Kr8HJcwiw4rH1LF/7cTiGWMgTk8=; b=z9aZrN4fCmRuP/ostz6C6/F/Fyl5yn8Ylw5KNL2xDbGwWpW4TGwxSiMXo66tBPTFXeAM/J8XP hFMbaVVmizHA2/pLsbqC/BVXiYzZ79/viIlk3yIwIoQl2id9f+fFNdr X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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?1758702323203022475?= X-GMAIL-MSGID: =?utf-8?q?1758702323203022475?= |
Series |
rust: error: Add missing wrappers to convert to/from kernel error codes
|
|
Commit Message
Asahi Lina
Feb. 24, 2023, 8:50 a.m. UTC
This is the Rust equivalent to ERR_PTR(), for use in C callbacks.
Marked as #[allow(dead_code)] for now, since it does not have any
consumers yet.
Signed-off-by: Asahi Lina <lina@asahilina.net>
---
rust/helpers.c | 7 +++++++
rust/kernel/error.rs | 7 +++++++
2 files changed, 14 insertions(+)
Comments
On Fri, 24 Feb 2023 17:50:19 +0900 Asahi Lina <lina@asahilina.net> wrote: > This is the Rust equivalent to ERR_PTR(), for use in C callbacks. > Marked as #[allow(dead_code)] for now, since it does not have any > consumers yet. > > Signed-off-by: Asahi Lina <lina@asahilina.net> > --- > rust/helpers.c | 7 +++++++ > rust/kernel/error.rs | 7 +++++++ > 2 files changed, 14 insertions(+) > > diff --git a/rust/helpers.c b/rust/helpers.c > index 09a4d93f9d62..89f4cd1e0df3 100644 > --- a/rust/helpers.c > +++ b/rust/helpers.c > @@ -20,6 +20,7 @@ > > #include <linux/bug.h> > #include <linux/build_bug.h> > +#include <linux/err.h> > #include <linux/refcount.h> > > __noreturn void rust_helper_BUG(void) > @@ -46,6 +47,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) > } > EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); > > +__force void *rust_helper_ERR_PTR(long err) > +{ > + return ERR_PTR(err); > +} > +EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR); > + I know that we already have `IS_ERR` in helpers.c, but having to go through FFI and helper functions for something as simple as a cast feels awkward to me. Given that `ERR_PTR`'s C definition is very unlike to change, would it be problematic if we just reimplement it in Rust as ```rust fn ERR_PTR(error: core::ffi::c_long) -> *mut core::ffi::c_void { error as _ // Or `core::ptr::invalid(error as _)` with strict provenance } ``` ? I personally think it should be fine, but I'll leave the decision to Miguel. Best, Gary
On Sat, Feb 25, 2023 at 11:14 PM Gary Guo <gary@garyguo.net> wrote: > > I know that we already have `IS_ERR` in helpers.c, but having to go > through FFI and helper functions for something as simple as a cast > feels awkward to me. > > Given that `ERR_PTR`'s C definition is very unlike to change, would it > be problematic if we just reimplement it in Rust as > > ```rust > fn ERR_PTR(error: core::ffi::c_long) -> *mut core::ffi::c_void { > error as _ > // Or `core::ptr::invalid(error as _)` with strict provenance > } > ``` > ? > > I personally think it should be fine, but I'll leave the decision to > Miguel. On one hand, we have tried to minimize duplication (and, in general, any changes to the C side) so far where possible, especially pre-merge, doing it only when needed, e.g. for `const` purposes. On the other hand, being in the kernel opens up a few possibilities to consider, and it is true it feels like some of these could get reimplemented, even if not strictly needed. If we can show a performance/text size difference on e.g. a non-trivial subsystem or module, I think we should do it. If we do it, then I think we should add a note on the C side so that it is clear there is a duplicated implementation elsewhere, avoiding future problems. In fact, it would be ideal to do it consistently, e.g. also for the ioctl ones. Something like: /* Rust: reimplemented as `kernel::error::ERR_PTR`. */ static inline void * __must_check ERR_PTR(long error) { return (void *) error; } Or perhaps something even smaller. But I don't want to block the rest of the work on this, which may need some extra/parallel discussion, so let's keep the helper for the time being. That way we can also do that change independently and justify the change showing the difference in performance/text, if any, in the commit message. Cheers, Miguel
On Sun, Feb 26, 2023 at 3:26 PM Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote: > > But I don't want to block the rest of the work on this, which may need > some extra/parallel discussion, so let's keep the helper for the time > being. That way we can also do that change independently and justify > the change showing the difference in performance/text, if any, in the > commit message. Opened https://github.com/Rust-for-Linux/linux/issues/984 to help to remember it. Cheers, Miguel
diff --git a/rust/helpers.c b/rust/helpers.c index 09a4d93f9d62..89f4cd1e0df3 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -20,6 +20,7 @@ #include <linux/bug.h> #include <linux/build_bug.h> +#include <linux/err.h> #include <linux/refcount.h> __noreturn void rust_helper_BUG(void) @@ -46,6 +47,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) } EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); +__force void *rust_helper_ERR_PTR(long err) +{ + return ERR_PTR(err); +} +EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR); + /* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 5b9751d7ff1d..8611758e27f4 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -76,6 +76,13 @@ impl Error { pub fn to_kernel_errno(self) -> core::ffi::c_int { self.0 } + + /// Returns the error encoded as a pointer. + #[allow(dead_code)] + pub(crate) fn to_ptr<T>(self) -> *mut T { + // SAFETY: Valid as long as self.0 is a valid error + unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ } + } } impl From<AllocError> for Error {