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 {