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