From patchwork Fri Feb 24 08:50:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asahi Lina X-Patchwork-Id: 61193 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp1207776dyk; Fri, 24 Feb 2023 01:07:15 -0800 (PST) X-Google-Smtp-Source: AK7set93r+IBZlM0aauYw1XEiBHw/74Tnz5Og8Nga9o1ecI+YmGdVzi1dn6EeaOTd0z9OsJ+ipSE X-Received: by 2002:a17:90a:19ce:b0:233:d10f:5236 with SMTP id 14-20020a17090a19ce00b00233d10f5236mr16095209pjj.28.1677229635090; Fri, 24 Feb 2023 01:07:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677229635; cv=none; d=google.com; s=arc-20160816; b=Vo90qrsi08vDNhO7r46EDXd2g6ReHnEyIPF1Ah5MFtzEL+Wdb4O4AzIC8z7Lj42nvr yUCWrqjdqhNU08LzHpA2ESZMAg8GxJFEkMuqaY/nWWPjbJcq5NnSlQY4TGUvIz5R78Pm f1mYHgO+Ii9LChAd4UPRfRkxYqU1+HAeD5eD6LCdVbG+VDzYFAC4pPOEYNDcpPlhP6Q9 FCFd/2pPeyf5N9xHX+AWNghDHYTlh0aL6XZ1hPthxyTQcTn6Vy5JRQrLtTb48swKG/CN Wl6O//xxW0A1AkFaEWUg6joyeM88rhrtwZcl0LAJk/hHDm2CncU+1d2hxcZLoEFdnyp1 HG5A== 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=NMAx0eZxXEqjXN0fbc16Xx+sF28xGCYfDI68wr+ADgQ=; b=vumKGOnrjf+YD73FtIrXsV3do8xwA7OONiNxZ62u8SwpU7ulCgy/y7X1aohPMjAVsa 1+2jqn2aIiEicGmNYRSB5V8bE+m+nX1dCja8YQl1VyiYqx86pPz98ydzRPCADLUSKQW/ 1CwLofxNhih1I9nUjXJJOWZYtnalLksKVBnrr8UMM2KnRFxxu78qb9i5XvHUBDFdN+C+ RKhbwicNTa5CO8xaFuGQg0OO6dbjJvx8vtufSRPFvbwibOMZalA/2Khr4zFFWYoLIomP g0QqzGzCIqshFqH4N0QlU7FcvNDRIf8H4P/ZeNcbeiShOwnzVJVCjm/pKu7kDIWBUk4s ggBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=jluUxTT4; 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 lg15-20020a170902fb8f00b0019ca3e987dbsi7646121plb.631.2023.02.24.01.06.55; Fri, 24 Feb 2023 01:07:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=jluUxTT4; 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 S229893AbjBXIvZ (ORCPT + 99 others); Fri, 24 Feb 2023 03:51:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229892AbjBXIvI (ORCPT ); Fri, 24 Feb 2023 03:51:08 -0500 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D30B63DC2; Fri, 24 Feb 2023 00:50:53 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: linasend@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 9133941A42; Fri, 24 Feb 2023 08:50:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1677228651; bh=xB/uTkNdcRyAJ9MSmz0Zm2ytGsD1PaLAfUTJ4lXE38o=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=jluUxTT4MsfWow1fquKnY6BSANJ/SOd4Jesz7Uh8zivRONgIVXOwXArRMjNQS/0D5 45iD20o5RBLpaAQRA+5duNKq/skh3y8obwKVEE7ktZXZBSn0ZsGY/XJDI9uXMiQGN7 D3SCiWUJ+1JaXLBWBJDkwb13SIU3jBpAlu20+E2p/fbmdd6B/t7gKj9NYD9sBUa7x5 uCrakUy9Kj/cdP2CiOm7iTR9MJLGYnyIaX5T87NMrrBiDHUyr6ZL6nXlQpo2YPNnEE slCjNYRl9kyhKBiIhqlsNLhkhGpT7ztUT/2gucAYgFJxtJx+whXqpK2BpsKpMKS4tE bQpHo8xTgTtXg== From: Asahi Lina Date: Fri, 24 Feb 2023 17:50:23 +0900 Subject: [PATCH 5/5] rust: error: Add from_kernel_result!() macro MIME-Version: 1.0 Message-Id: <20230224-rust-error-v1-5-f8f9a9a87303@asahilina.net> References: <20230224-rust-error-v1-0-f8f9a9a87303@asahilina.net> In-Reply-To: <20230224-rust-error-v1-0-f8f9a9a87303@asahilina.net> To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Sven Van Asbroeck Cc: Fox Chen , 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=1677228630; l=2780; i=lina@asahilina.net; s=20230221; h=from:subject:message-id; bh=vBDhcwnH5ynZ3xRDLShDFkYEv5ArQWgEwhEf43533EA=; b=boG/WaS3XMs/3FWEPp+1XQvxkXa8H/c6k5q+2b+v2KaY055zgjtJnRQ53iv5jS+h2FpjzuPR1 fI81HlcBJISB+Tr+N/6qsC2xNEW3hQyZxQhKw+ky6RopnnFk1ZYcITp X-Developer-Key: i=lina@asahilina.net; a=ed25519; pk=Qn8jZuOtR1m5GaiDfTrAoQ4NE1XoYVZ/wmt5YtXWFC4= X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758702741636805486?= X-GMAIL-MSGID: =?utf-8?q?1758702741636805486?= From: Wedson Almeida Filho Add a helper macro 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. 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 --- rust/kernel/error.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index cf3d089477d2..8a9222595cd1 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -226,3 +226,55 @@ pub(crate) fn from_kernel_err_ptr(ptr: *mut T) -> Result<*mut T> { } Ok(ptr) } + +// TODO: Remove `dead_code` marker once an in-kernel client is available. +#[allow(dead_code)] +pub(crate) fn from_kernel_result_helper(r: Result) -> T +where + T: From, +{ + match r { + 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_kernel_errno() as i16), + } +} + +/// Transforms a [`crate::error::Result`] to a kernel 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_kernel_result; +/// # use kernel::bindings; +/// unsafe extern "C" fn probe_callback( +/// pdev: *mut bindings::platform_device, +/// ) -> core::ffi::c_int { +/// from_kernel_result! { +/// let ptr = devm_alloc(pdev)?; +/// bindings::platform_set_drvdata(pdev, ptr); +/// Ok(0) +/// } +/// } +/// ``` +// TODO: Remove `unused_macros` marker once an in-kernel client is available. +#[allow(unused_macros)] +macro_rules! from_kernel_result { + ($($tt:tt)*) => {{ + $crate::error::from_kernel_result_helper((|| { + $($tt)* + })()) + }}; +} + +// TODO: Remove `unused_imports` marker once an in-kernel client is available. +#[allow(unused_imports)] +pub(crate) use from_kernel_result;