From patchwork Wed Mar 29 12:04:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 76557 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp372213vqo; Wed, 29 Mar 2023 05:26:23 -0700 (PDT) X-Google-Smtp-Source: AKy350bdRfrkxxac5hTwpqX/iKom0Y0KAWx2EhP6+Xx8f0E+iR2OyWKV/6Zr1qnKWgHPmJIboBcr X-Received: by 2002:a17:90a:4bca:b0:23d:2d91:ab71 with SMTP id u10-20020a17090a4bca00b0023d2d91ab71mr20430706pjl.6.1680092782841; Wed, 29 Mar 2023 05:26:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680092782; cv=none; d=google.com; s=arc-20160816; b=Ts1TFJWfuzSIyjr+pkXiqwEVGcWeA3WtyTpOPxF0R+oa84oAlFpz7CdGuSGwnhh7wK Mr9/K9SoOs0KABuNTzigENqg6qUhyfVLLrjjib89yKtBEdWK0mKn0arj8dzQkSerrWGe 6slFla9roWjunHwDl1jFm42gLAcHQ2L6aW74uyd2mly+LOjJra/HhXoXaubMbVL/wbsZ 6Q3F2jOfxitOxiQD4su6CEqUR1huVei1sh6vzqphXYtR860MW0LYIMuJMXTSDdKpYmcl AOYCMIdCtGy367tk58GX+Tuu68AuQSGTtqQcBuyBv2/cWdrMkXUuVELQRnokAb38Ns6o N9yw== 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=d/ZDZp//qJmdJeOAOMWmN1GFeD5lXGfLDYrTs+QHN0c=; b=JRmCUklNN8O6m/CGF2B+urZWaP0q7bKwDV0OVl+XAJvmy9ciAk3mmSJxUrcxSLS6GZ Fk4nw2kO5x2QknjPxtA2Zx1ixQ/2bKTAMMjqgTGhGP6A5eNihpcnCgHlOQ0rO8Ov67gF f2vVi0rDZTQO9yuBxInlXn27O1nw+963VNcp33Hro+YtmqVf5ijEJQIErYEgPmaVXNn5 8MEsxZKTfXpJCH1XVsE7jVrySmKfpbberQ3woIHtFboDatHaF4PedlLq/c7rqlAB4CNf t59yxrA2XmVgw/AfDoKrB05HGQ8COZiapiCH92ePe1PX+ZZa1QJYmIBjaPA+PeSucNWE MVHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=LkP9Ew+Y; 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 t14-20020a63224e000000b0050be4f9909bsi31371888pgm.695.2023.03.29.05.26.09; Wed, 29 Mar 2023 05:26:22 -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=LkP9Ew+Y; 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 S230012AbjC2MFB (ORCPT + 99 others); Wed, 29 Mar 2023 08:05:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbjC2MEs (ORCPT ); Wed, 29 Mar 2023 08:04:48 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEFC4C0; Wed, 29 Mar 2023 05:04:47 -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 DF14441F98; Wed, 29 Mar 2023 12:04:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091486; bh=7wja8b7e1cR7sIIhKDHqBGrFHH4ZjjMoaG3eC2S/l8g=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=LkP9Ew+YkGZHBzX3b2qYbkE7xE0CHA/iZmwS4N+CxN7LgjWneIu9PHW5BFwVvChfx 004xcRMCJ9EmycJwq1YC7TsehLfdcq5z0BR/EBb6xlotuU6Dwt0cYsjj+zM0vwYCd4 A78F7/TuXmCFRUnvkgv0JTWx+uVVt3dijOdkNp6jva+WSSG/9qlmq9U0n+Ql6E1msi j1md0IBdYgcyQP+ksBGqWNgWgijcaOFAoxdUbyKFzBwWXWLdQNfZKVH8jpJJBJ7V5f bHJtjnH4WBsb9chQsRPvS+4bb4wBvGFK0fH1NtFFZYPMob4Jy9RaaL5b1RzBnHNpMe gAn2Z/VKBNwVQ== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:33 +0900 Subject: [PATCH v2 1/6] rust: error: Rename to_kernel_errno() -> to_errno() MIME-Version: 1.0 Message-Id: <20230224-rust-error-v2-1-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@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=1680091478; l=1196; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=7wja8b7e1cR7sIIhKDHqBGrFHH4ZjjMoaG3eC2S/l8g=; b=kne0zLSHrTRwLBNWWdJZlyhEqojrap6mEiUNwT9Hya5xcb05TI6ZxL69UCWKZQx6xmRUYUVM3 Jzl9RWFoI49D0aF6WsvriRs9AcTLEXYceVVElED7tqThMqYoX15j38o 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?1761704969976212830?= X-GMAIL-MSGID: =?utf-8?q?1761704969976212830?= This is kernel code, so specifying "kernel" is redundant. Let's simplify things and just call it to_errno(). Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Gary Guo --- 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 Wed Mar 29 12:04:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 76556 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp370044vqo; Wed, 29 Mar 2023 05:22:04 -0700 (PDT) X-Google-Smtp-Source: AKy350Y3BWhDtGs02/EE4khDH037Z5TYbrKSnN/ze2YbqTd0vZlv1IXC6rBJCRZcDhseVt8K9y4X X-Received: by 2002:a62:6346:0:b0:626:cc72:51a7 with SMTP id x67-20020a626346000000b00626cc7251a7mr19063076pfb.9.1680092524366; Wed, 29 Mar 2023 05:22:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680092524; cv=none; d=google.com; s=arc-20160816; b=maNtWnC1r0HoIYMmlmw407ZjUXxid9q9SiYec3vw5GVx9HroX4Pbl1fOHbCYRUhwx1 bp5XZr5f39/p/1yMpLt6d4XJ5yQg47ynAAqa2acJWf80VXH55B8q5nrz9rbhn3UG5wGo 4zDg4rI/hdqGPxSk2I3mp2xU/j0O75P9CA97I8OMSJ8qST9wS4JA3mmeyh8JY6F+rDms 3urJN6URW5EPpJo/AJ7j4OnzzDuunQWEVtN04kW7HrdRRpp5XfwuMa77YDjIJMh1mBOT gt20DwJ/Qjk1u31mYIB9ZRCbkc19dD+b0xDcIXhjv0MYjwWywAGPkjva6aDuI0wIrqxN ewgA== 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=tBS7M5UgdNRsIRnUkHEiU5OxRv14+Bua1Wc1S3FISqI=; b=ttOP+rC/bqsXn21/daSuFAJUFNhm/DP4HGnJpMyefPVcBTVx9bvpn6H9ozUiUo1g9a aOFaQuFLvVOukE9PquvfAwwLrOOdQtGnVXxDy/DdhbAmjF6w5tS4NNzDiUSc0Opa8DIJ AB/zy0+/Ly3Twoi7M/wlq9Qvt6Rz8FP7zZRFNugpb9J8/DKDYgPNXkf57UiMsJnPJuBj qc07p4Kv7yunptUPTZUd50TiJc9eLZjMIvo872fktW+pTzux/Z6VuxKyaH+ginUucsz8 DrjQBmBZoZ2oSOhteZpPRFQ8Vu7qMB3yvBKIjZLKHSZkJX0YaQyUSyxHBIcIW7kwrTzB YzPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=PqITEoRL; 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 y10-20020aa78f2a000000b0062d9ba6b58esi3850214pfr.224.2023.03.29.05.21.50; Wed, 29 Mar 2023 05:22:04 -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=PqITEoRL; 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 S229753AbjC2MFF (ORCPT + 99 others); Wed, 29 Mar 2023 08:05:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230019AbjC2MEw (ORCPT ); Wed, 29 Mar 2023 08:04:52 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5FAF30FF; Wed, 29 Mar 2023 05:04:51 -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 E0317424A5; Wed, 29 Mar 2023 12:04:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091490; bh=yizatryz94HI38ZhrhHNZKorJP2yMGzaneMJyUc/yLs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=PqITEoRLJt6wek92hSnvKc+Y1lmrOZsCLkof9zV7pn1ZNqZZWcdU2I15hFi/0iu5E qAl/+y7C+v5f5jtOnENPkB4CvMhF8EgkbglkUtW8YSNF5/1YDH2cZH1qx0FuegkBws kl9MQqd6kE/7jb2NLfNZLxQrkEjByz9eOQp/qNQkZO+getG5pn8J369MZyd853W+uc bVH7NfhyspZ7pd4ArAzCusASEKVEvo8N2LT5iq36AuF2YTdzQjFXscoHiR60iDTs6A LszjKUuvn0pp7JlYzdskZzmV8QbA/TbPaJ4tWxptPoI3qOR3EJoBp4cCZd4P323+ul dVl0caXyxuPLw== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:34 +0900 Subject: [PATCH v2 2/6] rust: error: Add Error::to_ptr() MIME-Version: 1.0 Message-Id: <20230224-rust-error-v2-2-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@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=1680091478; l=1617; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=yizatryz94HI38ZhrhHNZKorJP2yMGzaneMJyUc/yLs=; b=g3wfjFA5bNlmO1XJYJc+pIss8D54z8fnJrLrR9I1KnVL0/HzvcuTRtMeiraME/fJKA07ACpAh Rb62kJ4bwzLDe8glRgolrXsew//PuOyb/iOnkUki7e2ldOY6UP/20pz 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?1761704698720251615?= X-GMAIL-MSGID: =?utf-8?q?1761704698720251615?= 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 Reviewed-by: Martin Rodriguez Reboredo --- 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..e97e652a1aec 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: Valid as long as self.0 is a valid error + unsafe { bindings::ERR_PTR(self.0.into()) as *mut _ } + } } impl From for Error { From patchwork Wed Mar 29 12:04:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 76553 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp362823vqo; Wed, 29 Mar 2023 05:10:37 -0700 (PDT) X-Google-Smtp-Source: AKy350YfW8GJNeZHF/0tXloNrgAf1coL2C6dQBqGDuiVro4gMRGaZkDnLb475CsIc6Aq5CckpgoS X-Received: by 2002:a17:90a:1ce:b0:233:ee50:d26d with SMTP id 14-20020a17090a01ce00b00233ee50d26dmr21765311pjd.4.1680091837308; Wed, 29 Mar 2023 05:10:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680091837; cv=none; d=google.com; s=arc-20160816; b=wfZB8YGxdVZ0l0JaEBHZldwa5UY4DZFS/YUWaKS3ndIqPWZhTMAIepfUgAROSWBASJ pDlf/2WBSrAITpGbb2pfd2SQ3u1vY7vvZ12v/6yD9SYub75cBo6j3GeaAlM1qalYqsvo kE+xQwCDJwK0qLYH94taLG6Y7fx9MaYyEfois6ajRBWrFPvSO8zknKlQ9CUOY+IzzaBP zt1b+j5gYPqu2UsghpTQk82Nj8aAujKESqadb68knig1c/XceGmdnJSowrN/sNyB0OtH ltRNhtIyAtFeZH6UnYyLqwNhDPfMKgDyrGLV4qFVdZvNPNqL7pTWUHiDdHRYbDwBZNaW qo+g== 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=cwY7fAp4y0Yv7kAbzwz9yeZHnZd9I+nOWcpjuT+qRS4=; b=k/p3PW0mg8CqeGqIhSW2yGJ6RP9lUwK3gmVPb8tMDOdQv2Vk92aeiIcxHCDMqMOI9+ QeulOmDc0upPA2orADlo7AeOrJvebbH6wB1gW1gHkhjZmWzzwIV43jxNgx/65Ku/X6ak tiQYGeQE1awyRQrMF81twcS1pn2JvFzkHcRCzdeJSZCyJNLPOuQlWypXEeeKt4H1jV7I oJbN5Pgfx+famkXuWubiyLjwt04Pu/vfqIWAmAjOtHj5ADIesOScNRBaF4/JT+/J3r4o nTc9MB62YZc8CC+Wb3mUusHXrChYVGi2q0YoF5MqnjTohMoeTH/KLvSi4uByshuTceH9 MIcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=LhnYkk72; 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 pf7-20020a17090b1d8700b002406d55224fsi1362356pjb.178.2023.03.29.05.10.22; Wed, 29 Mar 2023 05:10:37 -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=LhnYkk72; 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 S230063AbjC2MFI (ORCPT + 99 others); Wed, 29 Mar 2023 08:05:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230058AbjC2ME5 (ORCPT ); Wed, 29 Mar 2023 08:04:57 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E041830ED; Wed, 29 Mar 2023 05:04:55 -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 19E7E41F46; Wed, 29 Mar 2023 12:04:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091494; bh=i4OiR3LCVFp1o7pcdF1+sBJPH/zxSaRWGdgRE2WJDNU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=LhnYkk72xZfQ9ysMIZ32YxC6aiSSoincImJAGgNxUoaYeiBX78sfY5rq8WLcal/i+ XZggEobuj3q0kLkwBWwiU1VzJcw7M5MQoytDIm3CSC3xz8lHu3fituOehDia+s9xM9 b0LG3l0mae6+Ne93MosdY45QU/Z1omitZFuDQi/weoTWOpycW1SyqdKAAZCuaseFOc 3uiFUPxxlbUY59CE7drn6qZ/fHmvwND3KiLdMKPtvB8Taj8pQ1B78VQlNGSQOzuT4h Qndab1aq5ByHy6j6TqikiV8CgHpncvJjdGzpVDkFJqdv+8XxTvj47rjqsxnBd9Wzby Hh2eeuGFnGuzQ== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:35 +0900 Subject: [PATCH v2 3/6] rust: error: Add Error::from_errno() MIME-Version: 1.0 Message-Id: <20230224-rust-error-v2-3-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@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=1680091478; l=1923; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=VSgp8GJmq0GyyWZ2FrIFWGZbohpKXUspc+RJ4lAro+A=; b=MI7BIrTtZmn6MoA5cs6z3VjONdRNDkoCAfIOu+dk9btyw14filP7UP7mvZNtWRVQSD36I41+j sB23CuOjfluCfsXkohXWQdCKwXyqrrETbQlWVggmrqD6pBoRt9D6iIp 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?1761703978053656775?= X-GMAIL-MSGID: =?utf-8?q?1761703978053656775?= 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 Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Gary Guo --- rust/kernel/error.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index e97e652a1aec..659468bd1735 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_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_errno(self) -> core::ffi::c_int { self.0 From patchwork Wed Mar 29 12:04:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 76551 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp362637vqo; Wed, 29 Mar 2023 05:10:20 -0700 (PDT) X-Google-Smtp-Source: AKy350aHCAOZo3SIqTtA6pRbpbRwjdwBAsnmmXDOk3kmGIxQHw0TAI5MPQ6l2JYP6XUm1LgbFM4K X-Received: by 2002:a17:90b:3ec3:b0:23f:9448:89c2 with SMTP id rm3-20020a17090b3ec300b0023f944889c2mr21360686pjb.7.1680091819956; Wed, 29 Mar 2023 05:10:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680091819; cv=none; d=google.com; s=arc-20160816; b=oS/vetWfBXbB0Lh/9jvKfHFsoFv7Lrba4Gf/IdM7kLU/r82p2iqa+lu92tp+sLyigW HKxX5fUagjSpHcyW65fBUozMyiMAfSp43VDkdJo+8A+9OT49+QocdIwHD0rk2nfrrWAY mN8oc4fIoZluoogLaAIGOv9NlZbPPwBYklgsdd1eXmP9byyqlhXB+CAA5hKiAziKWSRM kpUqgRIpv0ZUGQK3HgJ7gt6Ap+w8XhsmknqksLKGIaHkLPajt/HFQKFZ5jN273WeqDgc SLJWKie2Iw4QOoLFV/c5Z/4phrQGNheLeOwQNBYH4cnP+QCm5SPPS9zynZxmUnf1KgQu 8dJg== 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=f5drIn1BosmycdOXC/Def4OPHkX3J8O0CQK0F9anX2Q=; b=P7bs//vUX0bEJ7jjgxWkLADj7ZghYWtXz3VBH6tznWCMgZEI5CrXOGA4wHl3LExleK 8XQeR7xuHym+SaOyQ4enrcowD/y2Ag441Cca4AWxO8nonP8Y4gBSVb507B8YUrD8v4NY wDprPDEG5jZz5k19PAYZeIHI6l9ajw1fSvWSJz5kIZJ0kpnWaWq7AP/j7NUmzCiPnh9i 2eG65kawXnFvvR4q+4KTxkHiTZEPJDxFSj7+WX9PxSrkG2sKsPLLbvboirgOgaPaKcyA 7mZweDgj9XfrZGEXXf1xnzRWkFoeQukYe9tDBrf/ESJdfUSuFbbS3NZkoZrEKJiGD9CI 5/NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=O9YVbyPo; 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 a21-20020a63e855000000b0050bdca1b6eesi32584860pgk.861.2023.03.29.05.10.06; Wed, 29 Mar 2023 05:10:19 -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=O9YVbyPo; 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 S230079AbjC2MFK (ORCPT + 99 others); Wed, 29 Mar 2023 08:05:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229477AbjC2MFD (ORCPT ); Wed, 29 Mar 2023 08:05:03 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03166468B; Wed, 29 Mar 2023 05:04:59 -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 1B3A4424A5; Wed, 29 Mar 2023 12:04:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091498; bh=D4/twBr+bzIjY4ZGn+sbXBAJvi8KroM1AK8OX54iYyI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=O9YVbyPosEDBz1Y3e376jGTJdPZ56RObMPGEQKs2kbZDSMJLGz0iqM48RP9Bgg4jP 9j92qyyx8XYyVdP9cHvMvyyaXSRb9OtgPm1wKg/eiWnptEAI6XELAMSSY5aNs1Ar58 X9N0AK2DO4ogwJQwYo2KRRQAeLgzB9Wip9ZJ8n6/0XSPCiOka5Xs06nUzf1sWZ5JgZ LkPFlZMXMaX7E8pZ70aWPsUFsyoarZEJnCR6uF1GjvRmWt+nOWCFN/dmINb2qEsQUW svC9yWrzKhMMRJH07fzM0S9pvF5tlqwpfp2DDeaywrYCtFsFwugn3rLElXy4x5NthK px9nunpoJGpJg== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:36 +0900 Subject: [PATCH v2 4/6] rust: error: Add to_result() helper MIME-Version: 1.0 Message-Id: <20230224-rust-error-v2-4-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@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=1680091478; l=1246; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=Pyrepc31fu1j2hIHl+GlAJgfJXkFKrgb4Ue04icQgQk=; b=Qi1Fy6I/4bHQHbB9VuqBIwCJsY+ax2RlS2ro/Kyr4QO8xcgJz+/zdD8IOdnkYaufWCDB//8ot kA5XhtU7hJlADXksO7SehAh9Q98rL6Ptixy/XzzymQEtuAjJBKlp5vR 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?1761703959872583022?= X-GMAIL-MSGID: =?utf-8?q?1761703959872583022?= 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 Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Gary Guo --- rust/kernel/error.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 659468bd1735..4f599c4d1752 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -167,3 +167,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 Wed Mar 29 12:04:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 76552 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp362636vqo; Wed, 29 Mar 2023 05:10:20 -0700 (PDT) X-Google-Smtp-Source: AK7set+mQa0Q8g6aAVWDlbLP6Q8sEZjkfs/oRdlF92L9pb+YkLR/hVX7WplqDbVfzW4IHXba3m1Y X-Received: by 2002:a05:6a20:4a1d:b0:d9:199a:c719 with SMTP id fr29-20020a056a204a1d00b000d9199ac719mr19660008pzb.37.1680091820004; Wed, 29 Mar 2023 05:10:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680091819; cv=none; d=google.com; s=arc-20160816; b=a4iWEeQJj4AQGlfc55aLWQrFRLDjxBgdJcIxJhXGBeftPvAINbp5DcZDgryANCIjKw tZvC5V99ecIcvIvvmzXB2T8PnCuCS5tmHMwOBncr7qYKFGH5n8Lrx1Hl/15VWKLPs3wm 6y9PtFM2tbR8zF/kM327elYdDIJZrb3xeuj4Uj1b/hILSQb3yw0a/drJMEGcbeIBkiWO 7fuldsik35lO4HoUvZ3hMu/QP1dtRbKPFhj9S0GUOLumLf1CZPyLVROmeT7CX6ww/Emk 5rPmXjgn7AiUetioKOMiNtYCtXK0KgBT1sWY9nxif6f9TMQk19qq/oQlzW0uXYA2rAtr ving== 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=Qwnnvea04wNRRIFVJLuVK5QD35VYZcLHs9wHN6ZXuuI=; b=tSEsG8df7Y1WwNPnbACJPkx+6tEdyAIclwAbGjUFS0FbeWXoPzOPaLEPewZesa9ymD /hJiiqg/zR8LcnS7502L86tEjP2uUSKzAPryxmPCXgxXLaKDXSRPJU8Crr7d34FpD4/w IVNI6stMT7uKvA7jLd0TLG5Ojh0qFuXS+RyDh+vdzPjX8au2Pjh3GuuB6VPSonWQMWeB J384GlF0d84hHrgNGu4VWVCDe/eGcVveZiusSJdhTIU09lLgtDneiGLcCJFi05Dkc+zh qY/w7Sa2v/dLevzCcWclyakTi59D7KJgVKxf5cP19IcIq0UtotFHvpj6hQmCoUZECBU/ dszQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=LgcC9Rbc; 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 f13-20020a056a00228d00b00623d0b5b8c4si32697701pfe.115.2023.03.29.05.10.06; Wed, 29 Mar 2023 05:10:19 -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=LgcC9Rbc; 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 S230036AbjC2MFT (ORCPT + 99 others); Wed, 29 Mar 2023 08:05:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229627AbjC2MFH (ORCPT ); Wed, 29 Mar 2023 08:05:07 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1754B4EC1; Wed, 29 Mar 2023 05:05:04 -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 1A545424D0; Wed, 29 Mar 2023 12:04:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091502; bh=Z2Msqmu+TDL/Y0kqemtpmsxm6/7qz/c1AG9xBrS/GVA=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=LgcC9RbcMlP1MIG3M4+1vSvrPC2561Y7xnNXcvGF8kl5gBLmrZuTAFzICs2gk8F9I 1IDOMz1otzPrd6sNLh5zALuVGasmJM5TM7vbbiIKnFQgopxpnep80BxsWR6UHV3W9v AXuHq8/2En4kIX6zRy9PIujREUHDndBeGsrQplvEYv9Tr/nCIejII+Apk8WRTZO2x3 0m6SRNcNNQKYIhvJyzScDbtooJi4tFx04XtZhmeBuChvjYFs7F8Oym/NsQXS4LgeWD Z7ishp6wiYMZqG4xuAN1vFiJ07KGK2Nu+lTPk2zRfKZJ2MPdLZRQluIIvVd7z3rfJR xEESjrku4pW/w== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:37 +0900 Subject: [PATCH v2 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-v2-5-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@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=1680091478; l=4224; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=qOm0Nqiw7jlI1w438+NRLYRRJvpIZVUQLi5w4HxGf24=; b=lVOZaAXcQ20z1kEBxLI7TArlo/VvpaBP4j7TWhGxiiK4DT7IbyYMONGf6nwTeO6cm/ltdQLmJ RMkzsjzmSoyCyPkM/4gpY50WP+qwFE6VRK3bkuDvZxFtMyqf+J7rz4S 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?1761703960159419597?= X-GMAIL-MSGID: =?utf-8?q?1761703960159419597?= 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. Replaced usage of from_kernel_errno_unchecked() with an open-coded constructor, since this is the only user anyway. 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 Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo --- 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 4f599c4d1752..6b10129075a7 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -177,3 +177,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`. + #[cfg_attr(CONFIG_ARM, allow(clippy::unnecessary_cast))] + return Err(Error(err as i32)); + } + Ok(ptr) +} From patchwork Wed Mar 29 12:04:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 76554 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp363184vqo; Wed, 29 Mar 2023 05:11:12 -0700 (PDT) X-Google-Smtp-Source: AK7set+OQ+blqTCk4NW6uBY0AlWVOvt5kBzeiZofp766rJORevvv/OaTLva5n3Yilz4I0UBxiNQP X-Received: by 2002:a05:6a20:3b88:b0:d9:250:665c with SMTP id b8-20020a056a203b8800b000d90250665cmr13800629pzh.15.1680091872034; Wed, 29 Mar 2023 05:11:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680091872; cv=none; d=google.com; s=arc-20160816; b=N9v/Cm5EygxNtkecjf67crNMSdZEPqz8Nfzfn6zpUp5It7xtY7aDVSfLO5IVqmSslP 8KqdmtJp1Y36Koivnkq1op9JUHBXTgjvwgPpURh0J7vlFuNbOjgsRot2Ao8YjrCPZjc+ TWygWPm3lPu00OZrvqT0s9FwXtV0z1wewMJebEcdMWXRmjCDTyRUy3sh2uT0xlK+B+Jo Cycn/IIdcfR3d4//BpjXqmRiTFmbMiydGr1wZEWI3nTEa3RzRBbocssX+AhQ/eIcgjnA YZHr6E3LDcoMTz2asNACssQaiORhsjaRmS32QwHBzvSCwqrBiri+692bu/Z2mwvpUycu G+Hw== 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=IQVRMJz3RH3UjaLvC/1/9ZzmSZPw5PqCrtw87tnSIPQ=; b=cJIk+ZqoomwNmpkAYxgaAnnRCUwt1uG68ykqzblTRaV0nVxdO6xQzy4ws5wxCAROQO muWlr5Yh0WaC6mbda/Mcv2x9LvlaBHehl5YNmrSsTrOILbsoDRo38dXeRhuDCVWucBeN 3BnvzN8AZ0XbOrvhPUQI54ifLIFNc/8O9wXcBeuawZsJxI5WgGka/I3m1yHVFbBwtMVJ yP5Z7OIqrzLWEHq4HbhA3XoPdbW6yxMeuC77D3lrQauSAYrYpve8zAHZtCAuoST0MAyR FpGeqvYN46KCdOON0c+kQoztkbsOZvZBzPJy1VUbDHYkjfQWJXI8BGXipVtlB8L+TD1q fg3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=Dcx7JmfY; 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 18-20020a631552000000b004fbd23110d4si30174915pgv.773.2023.03.29.05.10.59; Wed, 29 Mar 2023 05:11:12 -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=Dcx7JmfY; 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 S229849AbjC2MFc (ORCPT + 99 others); Wed, 29 Mar 2023 08:05:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230071AbjC2MFU (ORCPT ); Wed, 29 Mar 2023 08:05:20 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5037344A8; Wed, 29 Mar 2023 05:05:08 -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 1C55241F46; Wed, 29 Mar 2023 12:05:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1680091506; bh=70azhY624dmqDR1WbAO/UkRG4/uvUwfXnn1O/f8fXaM=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Dcx7JmfYEJOvEoHOUZ0fYa8ZVFzUaYpAK5mmZz51FeSgXeupwRNpt4uhxB3kdh6Kx I3WFNita+VmGIE0BV7TYP1oAJ++hgFK3kCyD/ZIiiVYHgm3eJtTnnGOPvdQNtJvqLx 7jbEz+IqnqU8uwkOYIm/cv0zRKszm60IWa1Moh7TW/BS0WcJZZqmR+/PTx+PVKSyz8 0EtFdvd7DtDatbAhe+vvqqku9Q3zPI3vUybkdJ19cst5iehOGWYWIvoAoreY2v5n+w 0nWp4cKK5IAtscHKfWTEJiuxmVJkmnMBcEWQoeb+43MSQm7f0KdChKYYyD5prnOkp/ K9Zfxsz3jgzfw== From: Asahi Lina Date: Wed, 29 Mar 2023 21:04:38 +0900 Subject: [PATCH v2 6/6] rust: error: Add from_result() helper MIME-Version: 1.0 Message-Id: <20230224-rust-error-v2-6-3900319812da@asahilina.net> References: <20230224-rust-error-v2-0-3900319812da@asahilina.net> In-Reply-To: <20230224-rust-error-v2-0-3900319812da@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=1680091478; l=2445; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=Ai/iEgQ/fBNHagg7igl9ARuv9+jNWr+C78oJzdHwp1Q=; b=FtgUuaSd6mC3+e/DMioQcQS/rPK7XBseDo6+v/MVJi3rTWw7yGSMv3Q7DU/Oyw7ISdeBXG5+d BPUZKi78hqqB9vP31WhnZuhDT44q/p8pQiS+4Z+ejaIpN6TNuA0Z4Fz 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?1761704014613891108?= X-GMAIL-MSGID: =?utf-8?q?1761704014613891108?= 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 Signed-off-by: Asahi Lina Reviewed-by: Martin Rodriguez Reboredo --- rust/kernel/error.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 6b10129075a7..4d8b19d5967d 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -226,3 +226,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), + } +}