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), + } +}