From patchwork Mon Apr 3 09:48:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78466 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2180285vqo; Mon, 3 Apr 2023 02:55:16 -0700 (PDT) X-Google-Smtp-Source: AKy350aVXqmw/wyGokb9BJZyd4vMA1mxBxkXHNHe2jRHnhqlfyaeLqtMg5Gx3V2u0YDjka6tqCI8 X-Received: by 2002:aa7:c659:0:b0:502:4c82:6cc0 with SMTP id z25-20020aa7c659000000b005024c826cc0mr19383124edr.40.1680515716592; Mon, 03 Apr 2023 02:55:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680515716; cv=none; d=google.com; s=arc-20160816; b=uCl62tCgq+4uJRx73BZN1la8aSM3Yo7EHQjoHVlGb8H8jTk4SbJqpxqvsdSrneYFAo kpQn0v1uSeUMgffJm8S+BIUfQBRMaNGUqEUHZ7W7KKiuX49hwQ5P3QkQZhvC++DsXw3I ixvif1KsAxd0sfyO4eI4k+zzEAjiDdK1tJxBiDyRMt3jt1Fjy3CeViiHBuBtSSFUHmrk QnIWWewzX83r31sI3UdQLd4CB7w7s+fevinXLRYhzdth4rj86hTQ4obfgHg+WEqldgKD xEShkLQxBIXMY1J+sq27jlJMf1GyLJwke5KZm6oM6/w2+RlKaAXkzJr5g4edBlr8uDmG ykHA== 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=TiOJeTV/0Pm412P8KO/uDS0YqLhdwxZCyjRZIAmHMNk=; b=TbFS36ndak1+xQ2mnnPKo0BB8GLHViJS+cFF0ZbscLN8ChQjotac3Q93m7nMZGGVrx TT7eWDVNsIEbuiMUxZkuZRJyBmhjY8E7COLmYFS8h+LCw/cwWwB7P8kTkSFI/9TdEQ0G qPFVrvuvhecH08Q5/9p3Uaml60d9bKRqhEMtIskFzscHCbIXnrRvUtuAbFib1wYYySCp 85YeutJQtBcmrViUaCZTH1MkGDaYYD42I3Mt/GAKhWGmmcZO9FBIy+X8ZKz3uk3LSJMC 1CmqjRvOQO/WTFkuxebQdNq86o13j5wddO9sR3XrhAu7SFmSs6YJO976XIln7MgwWYbw 8q4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=P4AuYska; 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 oz19-20020a1709077d9300b00948c320fbd9si554725ejc.1020.2023.04.03.02.54.52; Mon, 03 Apr 2023 02:55:16 -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=@asahilina.net header.s=default header.b=P4AuYska; 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 S232340AbjDCJtU (ORCPT + 99 others); Mon, 3 Apr 2023 05:49:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232203AbjDCJs4 (ORCPT ); Mon, 3 Apr 2023 05:48:56 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3028E4C35; Mon, 3 Apr 2023 02:48:25 -0700 (PDT) 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 3F5D64212E; Mon, 3 Apr 2023 09:48:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515303; bh=rLpv9N/MuJdWjBDskv5c1QhUT6HdCop84/bSBVygqeY=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=P4AuYska6w6taph8lKaPQjWDuQWv0/U++vpi25iK/tGHl8+k0ScMPJqDtHS5w6OQk Y2xSwFeE/ppJ2dUMq76+75ztihktpRfMi3K237uiyULMRjw8ExgQcJS0zufJOJhc1N 2cfoKzUAHdaYYIdx9toL8ZCHGBRIepXZOLBqNQx0lvWVShgnbxuo7g38eLCrsLX/Qd V+vCxcq/cYAcI3uHvIn5bWoonkMEPLaLwf8v1W+8zp9z5cQMFzvOLHIHyS80xGG8ji Tgt0QDpqEghlsRV8qCI4xraips5EJFpKKFxzKG8SB4+5Ndg0RTXwZROWQmZkBQJhhD oF3vJZNDPOOng== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:10 +0900 Subject: [PATCH v3 1/6] rust: error: Rename to_kernel_errno() -> to_errno() MIME-Version: 1.0 Message-Id: <20230224-rust-error-v3-1-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , Martin Rodriguez Reboredo , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680515295; l=1299; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=rLpv9N/MuJdWjBDskv5c1QhUT6HdCop84/bSBVygqeY=; b=9nrnwa52xgS4clQ3rmGPD/khl8o3VTE2q12KEAtx4dfi0zZ9q7qGssXBpVKJN5iqp/2XvrYZ9 b0oMHm+OeghAiE5VWRiit7j0+M5na4OVUQKEkLB5eUGHUddgfgIFxJd X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762148447735002167?= X-GMAIL-MSGID: =?utf-8?q?1762148447735002167?= This is kernel code, so specifying "kernel" is redundant. Let's simplify things and just call it to_errno(). Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 2 +- rust/macros/module.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 5b9751d7ff1d..35894fa35efe 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -73,7 +73,7 @@ pub struct Error(core::ffi::c_int); impl Error { /// Returns the kernel error code. - pub fn to_kernel_errno(self) -> core::ffi::c_int { + pub fn to_errno(self) -> core::ffi::c_int { self.0 } } diff --git a/rust/macros/module.rs b/rust/macros/module.rs index a7e363c2b044..143336543866 100644 --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -258,7 +258,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream { return 0; }} Err(e) => {{ - return e.to_kernel_errno(); + return e.to_errno(); }} }} }} From patchwork Mon Apr 3 09:48:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2189398vqo; Mon, 3 Apr 2023 03:12:50 -0700 (PDT) X-Google-Smtp-Source: AKy350Y5KNxDtG+Z4ULo2MtDVOEKf3OxgcjAy8BJP5WaQrXOiS8k45ucl25X+PVVi+C/BWG2AWrH X-Received: by 2002:a05:6402:148f:b0:4fb:4fc2:e600 with SMTP id e15-20020a056402148f00b004fb4fc2e600mr32007235edv.42.1680516770563; Mon, 03 Apr 2023 03:12:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680516770; cv=none; d=google.com; s=arc-20160816; b=x8g9a8gtXZvfuQBf+ce/QejT3baDV0ONGJsZDhIKoow5MppmlWbDPSXaVnRXK1vbkz lGx8xOcbad+ATVmdjNYOLm24KIox9Mrh8cVptyIY5untw6K2TBLhUdPzA5j4J4TNPiOI vbITLwEOEfC9xEHoj7Dexrc5lztM//gPQgoVikVtISWvEZ3UAMJsfdT9mwmjTsbKRYmF Y+y6jDgPMldlC/3aCBhpWUpA3LQSEpQAWD5fEaSsdFazvn92wtdccxEr3LELIlVAyjWs u1bhhd9oB9MBIL/KDXw7L7y/9v7o6PS4DXd5gf32oRxbEv5Wk1JT5oovgnofonzYpOTP w2Nw== 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=95qC3qbiqKZ0/ZS2LxfvEtrci4IfHMLWCH7eoPfo5iQ=; b=RtQb2iCFnxaMgrvBhk50oNdHTaUFYdql47Wn/F2W2psiIv0h2lG1XWK3XxChM8bhRF wf5ytvubDTDoG9rJn9TLo3TmM8ArFzRUQ8F6MMw8ZZt4FN5BTMOk31KSI4ZAOzDpXrHr e7PeioTFV+eMATQ7HB827I7uUPGhYYHRX/MDjTF6ctKj5CDRA0TY6sQWJ0oTClxJRP2r zVK/VuPWF+a/bP34oyyVASBO2+2MzNEKU/MtH4TCUpyh7iap4/7N8dZ9UoBz196AtwVs zKZIEAO7HvMOpA0xrmxowqDJkYZzOJQhpNJlGknoiVOhMcmQHyTTUFA+Efk1vow94fvV ALIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b="KQG/jE6F"; 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 l18-20020aa7c3d2000000b004acb2c92ba6si3085688edr.79.2023.04.03.03.12.26; Mon, 03 Apr 2023 03:12:50 -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=@asahilina.net header.s=default header.b="KQG/jE6F"; 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 S232392AbjDCJta (ORCPT + 99 others); Mon, 3 Apr 2023 05:49:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232251AbjDCJs6 (ORCPT ); Mon, 3 Apr 2023 05:48:58 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CE0B4C31; Mon, 3 Apr 2023 02:48:29 -0700 (PDT) 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 365F3423BA; Mon, 3 Apr 2023 09:48:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515307; bh=RPuHJ6R232r0hFYOL7uwv1XyiKxx1ILr03e3JPbKDFw=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KQG/jE6FxwK7RjGbmyVlAV5GM0iFmOq0z0+oWzGPINY5h9xVL1AlD3exE8L32Zagv fkl60XlcK1WQcWom42C/V/xcjxye5weRSFGDCeA9ewuIVwKTt2w9oIOdSMN2n0Y7SM JT8fDP0Jtt3/4a/mKQCz/bdRTVPHEygr8ecDXKJEKC1s+c/ltUrD9vgbDJzBrTZeNr aVzTjz0z0sV4PeE2BFCv6XCKioxvQsMqHsxMu2IF5f4o1t3tkEyfyIUkxeCArtRNDw kTMohxBuHSJDr6Dr1eaG4U5x0QNXG+YS65LXrTWs5i/k8bk6334hlWTdNUmV6rt5xZ mSbSLr3Rz335w== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:11 +0900 Subject: [PATCH v3 2/6] rust: error: Add Error::to_ptr() MIME-Version: 1.0 Message-Id: <20230224-rust-error-v3-2-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , Martin Rodriguez Reboredo , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680515295; l=1683; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=RPuHJ6R232r0hFYOL7uwv1XyiKxx1ILr03e3JPbKDFw=; b=hvY3vvMJfh3KKdhSs9ptvvbGhmtEqmj3Guf8UT/y6cT43cn8ztSn1XJcxaGoNdJpUghwjyGUB EKJxa2nN8G3BNausNRjwPXuzOebAa0Gvuqe+0ePdMhXkiN5ZSGOGfrn X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762149552995190829?= X-GMAIL-MSGID: =?utf-8?q?1762149552995190829?= 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. Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina Reviewed-by: Gary Guo --- 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 #include +#include #include __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 35894fa35efe..154d0ca6e2dc 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -76,6 +76,13 @@ impl Error { pub fn to_errno(self) -> core::ffi::c_int { self.0 } + + /// Returns the error encoded as a pointer. + #[allow(dead_code)] + pub(crate) fn to_ptr(self) -> *mut T { + // SAFETY: self.0 is a valid error due to its invariant. + unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ } + } } impl From for Error { From patchwork Mon Apr 3 09:48:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78460 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2179458vqo; Mon, 3 Apr 2023 02:53:08 -0700 (PDT) X-Google-Smtp-Source: AKy350YwZcnlAdXOzO5BhP9Vx7tG3Bq8pI8oZo9vrzuLjAP3Jo2vmaiof27CuYIZ5tAhbCYnDHNs X-Received: by 2002:a17:90b:3850:b0:234:4187:1acc with SMTP id nl16-20020a17090b385000b0023441871accmr18571222pjb.19.1680515588188; Mon, 03 Apr 2023 02:53:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680515588; cv=none; d=google.com; s=arc-20160816; b=JWaHEcVXuKtHwLBCq0RdHiCBdlzMywIFDcg3NJpVIhxQYEmHSNkbm9tIIsaHDoLLsp VyreJBYm0OtkjZi9JVnd9oU8EUAXv12PCVvAZ2JHnR7ywQP7Olr2G5cGoJwvIrjiOwZy X/ZxXcjGVJHMtJnZE97H6tnsIawUNdB+KW6VxPi9UU519LaT3x+b075WR053KFHFq2aN pAfNH6K+UW4/8jNjdJfJrqtruVj+TqQgU159J1l5eaREcFMUptI/fHMSGVIp1vLZl9iR +Ym1XQEyAympZlHITdm/CUR9wE/FxWSP/k4CDKasSihrZwcrRNjYBlhWHXKntLGNvTiJ XFHQ== 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=y1PA1NxPzqu/JI3DAbEvYP4tuHV3J0U++kXIqIbfeoU=; b=ew4VqbslrXBAhpYclgO9kaBJuOn/9c+kDOeBtC3ecQE7IN/VszT5H7cdiVRtBqw47L 7Fyox+xcufZUPHUvjoSjR9htd6ecF6akZmwtWfNk3iM4RmrQo0KIYFh0Za8EPqPuohe2 Fmj4FJGk1qat7AEoLSoY4flwCzbSoOlFCD2aSJVRsUoKsusO+0a1NJJHr1wjhZkGIvzw 7gRQ29dM3ldTukdSDZooG+sp934DFCx1VetyKsE9M620WCjVbCG678OUr5Oyax+pDzA7 ybUGhrEluuaYSua2YNrgCjmcTUXmHYScCzCOrWVQ0udNy9xQGdlshnqWvN/4WJmEEZ8S fypQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=G9veylE4; 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 q15-20020a170902dacf00b001a20c983d1csi8448553plx.573.2023.04.03.02.52.56; Mon, 03 Apr 2023 02:53:08 -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=@asahilina.net header.s=default header.b=G9veylE4; 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 S232349AbjDCJtj (ORCPT + 99 others); Mon, 3 Apr 2023 05:49:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232369AbjDCJtD (ORCPT ); Mon, 3 Apr 2023 05:49:03 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6280DF76E; Mon, 3 Apr 2023 02:48:33 -0700 (PDT) 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 2D02C42450; Mon, 3 Apr 2023 09:48:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515311; bh=pvm7WbUlb+NyTlE+c65R2hxzbHfwH1tHLDpoJLpargk=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=G9veylE4GPhDrjRmkuHDPCfGZcFuNYsx42GtF8DDtfOK/LyjsgcnhvpZVbh9acbMa 2Xo4eOT7s+Vs0vBogkN00D+2lSNjEMFF1anQoXlOvvoyR5zIH5EYWxA+Rp4qngZXpX x4jJ9G20IJJnvcAgP/TxVcRCvdetDBiOZf/9UP/RX/yC2fvASojGGc+FjTTghvEFH0 Pu0BcoP+53hBF8jdroViphZdnvpTsS1VVilT3WwzBLiYL21V7VOmPi1Tf9ta4MaeNz Xz2jbunqBGPsZJmFJNV5SoDRPFz0GkVNRs9U4/oFyAdi09kf1nh5YNLd3hPfYCzucK EqiA5p8JkYNCg== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:12 +0900 Subject: [PATCH v3 3/6] rust: error: Add Error::from_errno{_unchecked}() MIME-Version: 1.0 Message-Id: <20230224-rust-error-v3-3-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , Martin Rodriguez Reboredo , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680515295; l=2409; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=NIH7G76x2xHvQYd680nRP0CtYqhzkgYdRdFs2Fnvuo0=; b=UZdEnqHEaSx8QsOcokSBiiKDI0ySOxyOpVj6+4YBFVzVl99Ef+T6kFqCtm2jWoeF8bCtxAHsh tu0CFblE0yQBEt0Y7SD/XJvsEMiPPAGm1dQIfH+Osbo6M9LeKZ1CeCU X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762148313792044598?= X-GMAIL-MSGID: =?utf-8?q?1762148313792044598?= From: Miguel Ojeda 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, renamed from_kernel_errno() to from_errno(). Co-developed-by: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Miguel Ojeda Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 154d0ca6e2dc..1af0d75d3a73 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -72,6 +72,36 @@ 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_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) + } + + /// Creates an [`Error`] from a kernel error code. + /// + /// # Safety + /// + /// `errno` must be within error code range (i.e. `>= -MAX_ERRNO && < 0`). + unsafe fn from_errno_unchecked(errno: core::ffi::c_int) -> Error { + // INVARIANT: The contract ensures the type invariant + // will hold. + Error(errno) + } + /// Returns the kernel error code. pub fn to_errno(self) -> core::ffi::c_int { self.0 From patchwork Mon Apr 3 09:48:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78467 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2182503vqo; Mon, 3 Apr 2023 03:00:52 -0700 (PDT) X-Google-Smtp-Source: AKy350bpw/xYdW56UC1y27sjWOsFklELDQePgubeVv1MfKZSVJRdL2CWgCYxxhvV9lc5JojnYFEh X-Received: by 2002:a17:906:d8d0:b0:8bf:e95c:467b with SMTP id re16-20020a170906d8d000b008bfe95c467bmr34459277ejb.63.1680516052721; Mon, 03 Apr 2023 03:00:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680516052; cv=none; d=google.com; s=arc-20160816; b=xI5LLVanJJm3iY0PQACo0FxhObdWLHi+zm8Dbcz+X1YufAvea8CncdmRZNiVvraXJd ia5JOruicyqoDXftuZicXxJlUNvvjAJaTAzg/I+nngsr4rYdXdy6EPrgLT5Q15LcSuxp kN7GwIloK1gYec1e3RCxaM8EEahEzuGEuZkeD2W2pB6MKHKsLGVKdd10JlrzUJXKSuxZ 7oVfFc990CZaf7i7WOL3JFW/itkTDD3kQ0Jngb6ht6KLaNMYTQe4zYNDE39U2lqY/AW1 +1UCof86PXrfcNd2xSlGgOQRDkzT5pYVZpLYYGkpTz4Eth0nMP0slv0dZ+dxh5MPhOcC DIbg== 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=UHKg/POMl58ii88IGA/UyOIdNPvEWgXZLHU7hxKg2to=; b=RtXdlltyCqfY0jiNYgapilX9F3ovDdLAbxXNAUe7u7zyzxgtSrTnAYu8f54/eSCs/z BjSpkW72Nl6cxGoLr/xThGmgVkbrR2Iu01MB2hHCkjmjZ1PWAHgKwHyTdrpYPn9ZT/bT m/Xzo2j5MrWCqq7LAVElHL7W3KjLjKJ+KqO+RoQRT4TiZp9UBlGlCUwDPRRJnu70OGc7 ICUEqvXKJ88GuZC7/IadqVfCcwp57CyH20qFnZDYNoIfvtNHxsbgE8iIhE+Ac+tRxB5e bpMsq7Cx3ayBUFZEdZsFsKMz3oymwRbad4tKZuLjwAl44L8vEGnlo72J/Wz5CaqR0HQP bweA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=yDLljO8E; 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 z10-20020a1709067e4a00b0093d8207777esi3782704ejr.858.2023.04.03.03.00.28; Mon, 03 Apr 2023 03:00:52 -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=@asahilina.net header.s=default header.b=yDLljO8E; 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 S231744AbjDCJtr (ORCPT + 99 others); Mon, 3 Apr 2023 05:49:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232366AbjDCJtH (ORCPT ); Mon, 3 Apr 2023 05:49:07 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B0FC12BE3; Mon, 3 Apr 2023 02:48:37 -0700 (PDT) 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 24C1E42463; Mon, 3 Apr 2023 09:48:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515315; bh=1P5QGZkC91BVciAq6IwmhV1rY1KwoTA3fWIHUuZy+8g=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=yDLljO8EQYJSU7dJ+J6J9ferhSr78RHQpptYiIXjCN4njyWl+ikQicZrU+qxXvDEf vk5FQKn2DIO/Fw3UKfz/lVKKDX5arP2NJtjDUYjygOZoN/VQ2z3+0nCsCkZhQ8mKWw hhBWhrrXhKA//jjKt+OrZHhIEuVnGebVioKkHOOXASQKrIZGZxUyFJn+gFQd+NhMBP AYXtiwwAmWQ6eMu6L4HNmwbhpGMTJyK3Xg+OuIslqkXoPVBhaH0F2Zvxgri24KKx4j jOo6q75oulz4XEPDquYa74A37ibPMg95pnse8PosvEK8Beul9P+7zUCsUNcuyltsWw gKAQO2ba1cbZA== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:13 +0900 Subject: [PATCH v3 4/6] rust: error: Add to_result() helper MIME-Version: 1.0 Message-Id: <20230224-rust-error-v3-4-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , Martin Rodriguez Reboredo , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680515295; l=1349; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=1wdN2vnmW6zZsxONU/uE2/YF42hR2YW74gn3/MQ5V4U=; b=EE3/RV3dvHT1L2FgxdAj0DSteyWP1D6o3slOa15LxiMp77atWOjnl7bza4WyQYjEugRjcHi0/ ZLnnlVRInMeBqD0zq36OfZoU1YH/4CWbRTujeVXXJeLoVlw2YekLDOH X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762148800275526877?= X-GMAIL-MSGID: =?utf-8?q?1762148800275526877?= From: Wedson Almeida Filho Add a to_result() helper to convert kernel C return values to a Rust Result, mapping >=0 values to Ok(()) and negative values to Err(...), with Error::from_errno() ensuring that the errno is within range. Lina: Imported from rust-for-linux/rust, originally developed by Wedson as part of the AMBA device driver support. Signed-off-by: Wedson Almeida Filho Reviewed-by: Andreas Hindborg Reviewed-by: Gary Guo Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 1af0d75d3a73..e8697ad6b62e 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -178,3 +178,13 @@ impl From for Error { /// it should still be modeled as returning a `Result` rather than /// just an [`Error`]. pub type Result = core::result::Result; + +/// Converts an integer as returned by a C kernel function to an error if it's negative, and +/// `Ok(())` otherwise. +pub fn to_result(err: core::ffi::c_int) -> Result { + if err < 0 { + Err(Error::from_errno(err)) + } else { + Ok(()) + } +} From patchwork Mon Apr 3 09:48:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78462 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2179974vqo; Mon, 3 Apr 2023 02:54:29 -0700 (PDT) X-Google-Smtp-Source: AKy350bFiuaK4xQFzLoPn15KaRObEPgpDZcNs5KWzwamjQo1t9VaJDLybZBC8PHF3agcUlBANqFB X-Received: by 2002:a17:907:1c01:b0:947:ec6e:fe06 with SMTP id nc1-20020a1709071c0100b00947ec6efe06mr7968854ejc.60.1680515669606; Mon, 03 Apr 2023 02:54:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680515669; cv=none; d=google.com; s=arc-20160816; b=AgvxwSuRzFP7tpozVRQdpGy2k/FTqsGL3T6xCSDcj0wf+w/N6ZLZYDcaAbjYFllzFB 5Wg18hbP47soGeK2affAmThQqqe8YEGfSA0cHPJ3BEVqvsLo1xTIp4L3I5bZ14hEderI CJrVQCaHVe0adp7qd35CEe5W1StKu94/MBSU5RXK+MO4XsdAyWTVGC2E/+uOCbZCiOKu C7VfnzLXGANUBCc9SP2KOg7HLPjitraYBw+dkzssT5L/X9/EN7jm4/GleaoexdeRLcVf /jFyEL58RW86I3hieYrIDkl9d79/O9WCej7exJUy0rwroQJ4D94hcTnVQmvlR7whvFhi gf9A== 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=xXUoLBAADaIJQI609c9hDE7IT1zbjMIeqkvhdJ2IPiY=; b=ucr+kcOwck5j3u+okhpSP3u8OzleXnAFTWJCDBmQKsDEcVFtlzemAgMe8pqzm8fhP8 6xlU+iONIikhnGwSzN7WcK9MUBljtmghtclG95JfByK1ycoY0oHUgoM7Ms9Bg8Brw+Ak W9plkSGArcjJtHMzy3SwKyf0M37Y/eAlMv1WvoxD8+ZIVyJf/3WZesiTk0n/gkkjlPrI BaeubaMD23AyBkCxzUeTLOYZpeeoFNjFXMvGH7IQ68QJdn6QoLHqNSmIUirs/EtNs/3M UD7ltdeYnOtxFaY3q8TkocZffMX4oUDwwKV9Hd1bEyMnxzNgh3Gm60dfbIdx8IBi+Acs 7QiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=W5xf2sVK; 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 j10-20020a1709062a0a00b009333cd8a15dsi4209441eje.883.2023.04.03.02.54.05; Mon, 03 Apr 2023 02:54:29 -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=@asahilina.net header.s=default header.b=W5xf2sVK; 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 S231843AbjDCJuC (ORCPT + 99 others); Mon, 3 Apr 2023 05:50:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232391AbjDCJta (ORCPT ); Mon, 3 Apr 2023 05:49:30 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A13012BFF; Mon, 3 Apr 2023 02:48:44 -0700 (PDT) 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 1D0B941E64; Mon, 3 Apr 2023 09:48:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515319; bh=FFczrsn4BHG2jGRMZ9qqibn2umHy3oP8Nzgbsa+sfGQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=W5xf2sVK+gyRZ9offnkTD/0SpWZxA5EunI4QAXKhn1dXM/Q7aRhUxCFzR2EaJCqxG IVGOR7TxkLrmH1QxsVJzZgGlTAdkL3wIaXF6P0eFjkyMMYKcu6NO3EYoBMlqnSBPvF 23N3SQkWxWLeh9S4DF3jiZmgPToZke2PTd8ImbVisGEYCuVHbkjVRueDW8wwcijZ41 vrCz3+HLwjyS1TQuu1l87I2y0YBsZeJeH4Dr9EqRyopO5XoBPoDe0HFBgDAOIMazO3 vh2YfqtyH0+G6hJBDKpxAaLIgFtMNmDmRezcDpOUb47v2XE+RtcE7/BjBa6szHNMQw zOWXkcjq/UjIA== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:14 +0900 Subject: [PATCH v3 5/6] rust: error: Add a helper to convert a C ERR_PTR to a `Result` MIME-Version: 1.0 Message-Id: <20230224-rust-error-v3-5-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , Martin Rodriguez Reboredo , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680515295; l=4192; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=ISUvnAT4TyiogMaTmuiS16KnnBXAUwEeRJutcySkWLo=; b=IELaoMg8SSQorXRnN2gvxejtyyJxGzy0urJANcFWVMLquvf5coqJVqSHlHd8rPd+7VCCKIaGZ K8jx70ApDMRBPAvtOeaih1oh6vfnPfEOrxIIt9D2UQErxcmbWNPEUSu X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762148398944321177?= X-GMAIL-MSGID: =?utf-8?q?1762148398944321177?= From: Sven Van Asbroeck Some kernel C API functions return a pointer which embeds an optional `errno`. Callers are supposed to check the returned pointer with `IS_ERR()` and if this returns `true`, retrieve the `errno` using `PTR_ERR()`. Create a Rust helper function to implement the Rust equivalent: transform a `*mut T` to `Result<*mut T>`. Lina: Imported from rust-for-linux/linux, with subsequent refactoring and contributions squashed in and attributed below. Renamed the function to from_err_ptr(). Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Co-developed-by: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Gary Guo Signed-off-by: Gary Guo Signed-off-by: Sven Van Asbroeck Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/helpers.c | 12 ++++++++++++ rust/kernel/error.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/rust/helpers.c b/rust/helpers.c index 89f4cd1e0df3..04b9be46e887 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -53,6 +53,18 @@ __force void *rust_helper_ERR_PTR(long err) } EXPORT_SYMBOL_GPL(rust_helper_ERR_PTR); +bool rust_helper_IS_ERR(__force const void *ptr) +{ + return IS_ERR(ptr); +} +EXPORT_SYMBOL_GPL(rust_helper_IS_ERR); + +long rust_helper_PTR_ERR(__force const void *ptr) +{ + return PTR_ERR(ptr); +} +EXPORT_SYMBOL_GPL(rust_helper_PTR_ERR); + /* * 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 e8697ad6b62e..67637b874267 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -188,3 +188,52 @@ pub fn to_result(err: core::ffi::c_int) -> Result { Ok(()) } } + +/// Transform a kernel "error pointer" to a normal pointer. +/// +/// Some kernel C API functions return an "error pointer" which optionally +/// embeds an `errno`. Callers are supposed to check the returned pointer +/// for errors. This function performs the check and converts the "error pointer" +/// to a normal pointer in an idiomatic fashion. +/// +/// # Examples +/// +/// ```ignore +/// # use kernel::from_err_ptr; +/// # use kernel::bindings; +/// fn devm_platform_ioremap_resource( +/// pdev: &mut PlatformDevice, +/// index: u32, +/// ) -> Result<*mut core::ffi::c_void> { +/// // SAFETY: FFI call. +/// unsafe { +/// from_err_ptr(bindings::devm_platform_ioremap_resource( +/// pdev.to_ptr(), +/// index, +/// )) +/// } +/// } +/// ``` +// TODO: Remove `dead_code` marker once an in-kernel client is available. +#[allow(dead_code)] +pub(crate) fn from_err_ptr(ptr: *mut T) -> Result<*mut T> { + // CAST: Casting a pointer to `*const core::ffi::c_void` is always valid. + let const_ptr: *const core::ffi::c_void = ptr.cast(); + // SAFETY: The FFI function does not deref the pointer. + if unsafe { bindings::IS_ERR(const_ptr) } { + // SAFETY: The FFI function does not deref the pointer. + let err = unsafe { bindings::PTR_ERR(const_ptr) }; + // CAST: If `IS_ERR()` returns `true`, + // then `PTR_ERR()` is guaranteed to return a + // negative value greater-or-equal to `-bindings::MAX_ERRNO`, + // which always fits in an `i16`, as per the invariant above. + // And an `i16` always fits in an `i32`. So casting `err` to + // an `i32` can never overflow, and is always valid. + // + // SAFETY: `IS_ERR()` ensures `err` is a + // negative value greater-or-equal to `-bindings::MAX_ERRNO`. + #[allow(clippy::unnecessary_cast)] + return Err(unsafe { Error::from_errno_unchecked(err as core::ffi::c_int) }); + } + Ok(ptr) +} From patchwork Mon Apr 3 09:48:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 78468 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2182518vqo; Mon, 3 Apr 2023 03:00:54 -0700 (PDT) X-Google-Smtp-Source: AKy350ahLda/HHNzPyS7Co5beD2XwYG2IuX7/le5Dia3tSqpUIFa9Ee0XCM5oOpPYQ3ZCnu1Yx9F X-Received: by 2002:a17:906:bcd1:b0:921:5cce:6599 with SMTP id lw17-20020a170906bcd100b009215cce6599mr37862840ejb.41.1680516054520; Mon, 03 Apr 2023 03:00:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680516054; cv=none; d=google.com; s=arc-20160816; b=a7xrK1ImnhAm/IP7XI/fQH5B3gqhPjzqnPAPJ6TyJYEcKlFHpjnhN11r17bhX62Sh6 Kft6+7JXsLXWd6nPEPnw2VOEE+THksEF5BMSYL/4uObZdf38iS1Y1oTdlHQF8sqhU/Cg 9iYjmoX6vLpXR77ZJGn2exNF7ASnlesN0tZEEQ1pgqRK+rE0ZBtHzWuowm8joNgY40X6 P3OsUjvkyGNSYI8ec2nWazyv/LwRcxrgdEpNQFqMK7OZ2pIXQUGka7fclcAPELhz8k6j RMcpCK+lUBDUKmzfNJz7dZFwFbRna1dsk9OShtzsSMz2l7SoXWaCGZ1BHSdF3kM1fBWv yeQQ== 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=TzsLtopAKrwOFbzlWuK0lTUJsy1XMT6DOYUnAqR3QWA=; b=o2getVWp09fCwGX5iCB9n5DXM8J+Ig7TfpQNzF8PS0M9SYxu87GFdeaDkKSsTxmNFW nYXukpNOJSbdVvdUqMyZsfsovz3l9vky+LCYAZ2Cj6HC+10kxs+cq1kt/j6uxZfnqKb5 v5oDeQm8v4+5nFk4HA/+jh0/NaufF07w54T8zlLN+w5yZIS0WwpIPRNhk2gpBcJB0GW9 XfCwqXFo+IbOAl8cLGQSqywkIxvFncOG+Rt+wLQdDwaO4mWR+fEBdkYLr8HduzXC/R5X GWdz7MhRKqPEZ2kaTSgxL3V7jv6HoESeQk9P5Oqgaa0LE3k9r8L/xhvbwgI3PVov7I3+ 9YOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=sMKCFGFx; 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 v25-20020a056402185900b004fa04899181si7158052edy.148.2023.04.03.03.00.30; Mon, 03 Apr 2023 03:00:54 -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=@asahilina.net header.s=default header.b=sMKCFGFx; 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 S232025AbjDCJuF (ORCPT + 99 others); Mon, 3 Apr 2023 05:50:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231928AbjDCJti (ORCPT ); Mon, 3 Apr 2023 05:49:38 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A25341880B; Mon, 3 Apr 2023 02:48:45 -0700 (PDT) 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 147614212E; Mon, 3 Apr 2023 09:48:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680515323; bh=0XYYkBWr58zEeLbGvBN8vR1PLhMpCbVIUQ1o32lb38s=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=sMKCFGFxxwmp1IBV+QGIUiiZtR8eBU/H2Zb0MaeI81HRDxM60TEflmViWiZGtb/kp yXXOvxo1s72f3j3ppSMqWAF7xcSZdle6Ju7VPe1LSxK6LseybuTTnMGc8zsDVl65IB o3iumoiBBihcMRKoWNeFOfQDGSCSgzLk0Tn3Q8hF9i+wHzjKuilo/bdzg+STr7vzJw HL/RWUHhseEQ1ySx3y1EsIon7ytjmjdb6wAS5E6ZgdjJbxDddyoUQG9AOwRCahyvTV PwTktu6vhHGRQ5npCOMRC4RWHUmW26baQsA0m5w4U37Uso3WQi8tmyq8Pj9/qfYr51 UbrllLTNIpKqw== From: Asahi Lina Date: Mon, 03 Apr 2023 18:48:15 +0900 Subject: [PATCH v3 6/6] rust: error: Add from_result() helper MIME-Version: 1.0 Message-Id: <20230224-rust-error-v3-6-03779bddc02b@asahilina.net> References: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> In-Reply-To: <20230224-rust-error-v3-0-03779bddc02b@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , Martin Rodriguez Reboredo , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina X-Mailer: b4 0.12.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1680515295; l=2506; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=OO5WuZKnDBBhoWLs7QqycJet5HzYwjS+slg6t+nBjnQ=; b=KZpqgzMr3JkNXDpCxb17Mi7L4pw3c69TRnawuee09u5W1l95mTzU1HBFSSoFHNZzzoi37FUN4 yhLDK+Gk42GAXCVsT52I9IOiugVtKqfzPiAVAF2x4iKHcR86gUTY0yJ X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762148802225448498?= X-GMAIL-MSGID: =?utf-8?q?1762148802225448498?= From: Wedson Almeida Filho Add a helper function to easily return C result codes from a Rust function that calls functions which return a Result. Lina: Imported from rust-for-linux/rust, originally developed by Wedson as part of file_operations.rs. Added the allow() flags since there is no user in the kernel crate yet and fixed a typo in a comment. Replaced the macro with a function taking a closure, per discussion on the ML. Co-developed-by: Fox Chen Signed-off-by: Fox Chen Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Signed-off-by: Wedson Almeida Filho Reviewed-by: Martin Rodriguez Reboredo Signed-off-by: Asahi Lina --- rust/kernel/error.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 67637b874267..5f4114b30b94 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -237,3 +237,42 @@ pub(crate) fn from_err_ptr(ptr: *mut T) -> Result<*mut T> { } Ok(ptr) } + +/// Calls a closure returning a [`crate::error::Result`] and converts the result to +/// a C integer result. +/// +/// This is useful when calling Rust functions that return [`crate::error::Result`] +/// from inside `extern "C"` functions that need to return an integer error result. +/// +/// `T` should be convertible from an `i16` via `From`. +/// +/// # Examples +/// +/// ```ignore +/// # use kernel::from_result; +/// # use kernel::bindings; +/// unsafe extern "C" fn probe_callback( +/// pdev: *mut bindings::platform_device, +/// ) -> core::ffi::c_int { +/// from_result(|| { +/// let ptr = devm_alloc(pdev)?; +/// bindings::platform_set_drvdata(pdev, ptr); +/// Ok(0) +/// }) +/// } +/// ``` +// TODO: Remove `dead_code` marker once an in-kernel client is available. +#[allow(dead_code)] +pub(crate) fn from_result(f: F) -> T +where + T: From, + F: FnOnce() -> Result, +{ + match f() { + Ok(v) => v, + // NO-OVERFLOW: negative `errno`s are no smaller than `-bindings::MAX_ERRNO`, + // `-bindings::MAX_ERRNO` fits in an `i16` as per invariant above, + // therefore a negative `errno` always fits in an `i16` and will not overflow. + Err(e) => T::from(e.to_errno() as i16), + } +}