Message ID | 20230224-rust-error-v1-2-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 j14csp1205588dyk; Fri, 24 Feb 2023 01:03:25 -0800 (PST) X-Google-Smtp-Source: AK7set9C9Tqck2UGeSn0kiaINAhq8dKHFJp1wfK/oSlAGKZSJdnO9i1QHub1KbAzHbEHSd6b0Op7 X-Received: by 2002:aa7:8430:0:b0:5e1:a3b1:966a with SMTP id q16-20020aa78430000000b005e1a3b1966amr2214279pfn.34.1677229405201; Fri, 24 Feb 2023 01:03:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229405; cv=none; d=google.com; s=arc-20160816; b=N21Xx0IuQhq4Jf35n3dJPWoLJRlMIOdeFqzQy1aF6u/OrAm3M1qIrvKG+SF//6dg0n BBMDbm6y1HIiqIkiRKyI7Zz/dPCewL6ndJlOWnSZgwQgDGU/apmfCTiPvovTojaSRhXD GasKbAm0NDBiyIVgCFm2ZuO66JcLvhm1tITKFUXx92MUjLW29dXVN7PW2PwO0TN0krmd tSLTbqGRy3fOST6XHFoeKD+/mCigBXPZ9TDAWLtgpIYpRstJ5RqtioEUVmmxH4OhfxcL YexKMBe/vHrGrZA+FC3T2j5AKwtX6oOT7m5wm0Yv2ovDiPeTi6PEB1w00Jdbva3/gnlX 4Pug== 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=iIZhgt7uAyDbe5tuV2laxtsnz6jmygEpJRvmdUw/PZE=; b=plrh07L2salCazAF15qSynBPkhBoZcOidjp1rrrbHJ9X6k0vzEeXWlfDGYUg/k5n5c JDary2uoPdvtxb9o+CW2sPAK7i88w5iPpGSj682dUweYRq/MrduIZnNr4bFZuhr5SpCs aNxxULc1Yz8853ylSJlSqvVIb9RoFiGxOHqPRXlGOXyeT9cUFY4Z3kZAhYCOpiRU4rRN xdIpCRx+lVFAJiGwn8CJs/VvONcmBbUUVZ8ewJz2z0/4j/Pa6diqnRvqbBzrzddw1U2D /1s0J0jaDN1WSr6rSm5yrqGcaneD3d+rWrEi9qn2+LUzQpMdAPyJjXZ7GJMGvq6bqVO5 CNpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=sgSwQWt9; 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 o13-20020a62cd0d000000b0057674a6a662si6502468pfg.138.2023.02.24.01.02.39; Fri, 24 Feb 2023 01:03:25 -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=sgSwQWt9; 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 S229854AbjBXIur (ORCPT <rfc822;jeff.pang.chn@gmail.com> + 99 others); Fri, 24 Feb 2023 03:50:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229844AbjBXIun (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 24 Feb 2023 03:50:43 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CF2B61EFE; Fri, 24 Feb 2023 00:50:42 -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 C372241A42; Fri, 24 Feb 2023 08:50:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677228640; bh=N+RXIF/i8BSXwu8BAV5Rq51ZpnCtf/pwSt4pl5MqBLw=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=sgSwQWt9xGTwGFMB2TdWEr9w5/65uiCuNhnF6EePNANn7kTxyc9j1ki7VZHILynIi nzOz0/JtbOP1KoEPaFyAgKJ/Oti/bNVV7yy/3gsSB1Juy22ZiulYaNXAsEs19tH3tE 1rIWsiSaH+gVFW93LS3A8XsKCp8WHR4tcimOujS/dZLB5SexFouEXMSpVPtANwGvtn LwriYEWDJ0Y9IGgHNeefy4M1xfJwxDYE0k1ZBxRz3/bt9/n2cl22vXFZEMa6zsWQlv LExpOrEzvitw/6oOyy3B3yTADIwl6i5gD6jr9Bo3y4yW5ty2MasfneY+vFxkA2oDmB 2CZLOUUe/4BEA== From: Asahi Lina <lina@asahilina.net> Date: Fri, 24 Feb 2023 17:50:20 +0900 Subject: [PATCH 2/5] rust: error: Add Error::from_kernel_errno() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230224-rust-error-v1-2-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=1835; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=x7nP5Dl+jwwx+80LB5PaFXLvxugGvPP/700imGU1Bp0=; b=CdN4zbiFV2Fkiy8QTvCq6VBwUqTn1obt6jCAuKp+JyoSL0YG47zwHJBiiSUrkvLnt3nZjNt+7 Fj9UV4SjXZ6ApdWLpqILoNme+RbeL+trkrVNJpsxdlZma9ofB4O79/+ 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?1758702500843118049?= X-GMAIL-MSGID: =?utf-8?q?1758702500843118049?= |
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
From: Miguel Ojeda <ojeda@kernel.org> Add a function to create `Error` values out of a kernel error return, which safely upholds the invariant that the error code is well-formed (negative and greater than -MAX_ERRNO). If a malformed code is passed in, it will be converted to EINVAL. Lina: Imported from rust-for-linux/rust as authored by Miguel and Fox with refactoring from Wedson. Co-developed-by: Fox Chen <foxhlchen@gmail.com> Signed-off-by: Fox Chen <foxhlchen@gmail.com> Co-developed-by: Wedson Almeida Filho <wedsonaf@gmail.com> Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Signed-off-by: Miguel Ojeda <ojeda@kernel.org> Signed-off-by: Asahi Lina <lina@asahilina.net> --- rust/kernel/error.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
Comments
On Fri, 24 Feb 2023 17:50:20 +0900 Asahi Lina <lina@asahilina.net> wrote: > From: Miguel Ojeda <ojeda@kernel.org> > > Add a function to create `Error` values out of a kernel error return, > which safely upholds the invariant that the error code is well-formed > (negative and greater than -MAX_ERRNO). If a malformed code is passed > in, it will be converted to EINVAL. > > Lina: Imported from rust-for-linux/rust as authored by Miguel and Fox > with refactoring from Wedson. > > Co-developed-by: Fox Chen <foxhlchen@gmail.com> > Signed-off-by: Fox Chen <foxhlchen@gmail.com> > Co-developed-by: Wedson Almeida Filho <wedsonaf@gmail.com> > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > Signed-off-by: Miguel Ojeda <ojeda@kernel.org> > Signed-off-by: Asahi Lina <lina@asahilina.net> > --- > rust/kernel/error.rs | 19 +++++++++++++++++++ > 1 file changed, 19 insertions(+) > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs > index 8611758e27f4..3b439fdb405c 100644 > --- a/rust/kernel/error.rs > +++ b/rust/kernel/error.rs > @@ -72,6 +72,25 @@ pub mod code { > pub struct Error(core::ffi::c_int); > > impl Error { > + /// Creates an [`Error`] from a kernel error code. > + /// > + /// It is a bug to pass an out-of-range `errno`. `EINVAL` would > + /// be returned in such a case. > + pub(crate) fn from_kernel_errno(errno: core::ffi::c_int) -> Error { Maybe just `from_errno`? I don't know why `kernel` would need to be emphasised here. Best, Gary
On Sat, Feb 25, 2023 at 11:19 PM Gary Guo <gary@garyguo.net> wrote: > > Maybe just `from_errno`? I don't know why `kernel` would need to be > emphasised here. Yeah, that sounds good to me. It is clear and we don't use "errno" elsewhere. This identifier came from the original import, so before we started to think about naming policies. Though perhaps we can clean it up in a patch later, since we should change `to_kernel_errno` below too at the same time. Or if you want to send a quick patch for that one, I can put it in first. Cheers, Miguel
Asahi Lina <lina@asahilina.net> writes: > From: Miguel Ojeda <ojeda@kernel.org> > > Add a function to create `Error` values out of a kernel error return, > which safely upholds the invariant that the error code is well-formed > (negative and greater than -MAX_ERRNO). If a malformed code is passed > in, it will be converted to EINVAL. > > Lina: Imported from rust-for-linux/rust as authored by Miguel and Fox > with refactoring from Wedson. > > Co-developed-by: Fox Chen <foxhlchen@gmail.com> > Signed-off-by: Fox Chen <foxhlchen@gmail.com> > Co-developed-by: Wedson Almeida Filho <wedsonaf@gmail.com> > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> > Signed-off-by: Miguel Ojeda <ojeda@kernel.org> > Signed-off-by: Asahi Lina <lina@asahilina.net> > --- Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com> > rust/kernel/error.rs | 19 +++++++++++++++++++ > 1 file changed, 19 insertions(+) > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs > index 8611758e27f4..3b439fdb405c 100644 > --- a/rust/kernel/error.rs > +++ b/rust/kernel/error.rs > @@ -72,6 +72,25 @@ pub mod code { > pub struct Error(core::ffi::c_int); > > impl Error { > + /// Creates an [`Error`] from a kernel error code. > + /// > + /// It is a bug to pass an out-of-range `errno`. `EINVAL` would > + /// be returned in such a case. > + pub(crate) fn from_kernel_errno(errno: core::ffi::c_int) -> Error { > + if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 { > + // TODO: Make it a `WARN_ONCE` once available. > + crate::pr_warn!( > + "attempted to create `Error` with out of range `errno`: {}", > + errno > + ); > + return code::EINVAL; > + } > + > + // INVARIANT: The check above ensures the type invariant > + // will hold. > + Error(errno) > + } > + > /// Returns the kernel error code. > pub fn to_kernel_errno(self) -> core::ffi::c_int { > self.0
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 8611758e27f4..3b439fdb405c 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -72,6 +72,25 @@ pub mod code { pub struct Error(core::ffi::c_int); impl Error { + /// Creates an [`Error`] from a kernel error code. + /// + /// It is a bug to pass an out-of-range `errno`. `EINVAL` would + /// be returned in such a case. + pub(crate) fn from_kernel_errno(errno: core::ffi::c_int) -> Error { + if errno < -(bindings::MAX_ERRNO as i32) || errno >= 0 { + // TODO: Make it a `WARN_ONCE` once available. + crate::pr_warn!( + "attempted to create `Error` with out of range `errno`: {}", + errno + ); + return code::EINVAL; + } + + // INVARIANT: The check above ensures the type invariant + // will hold. + Error(errno) + } + /// Returns the kernel error code. pub fn to_kernel_errno(self) -> core::ffi::c_int { self.0