From patchwork Mon Jun 19 15:41:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Zhangjin Wu X-Patchwork-Id: 13284703 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EDA03EB64D9 for ; Mon, 19 Jun 2023 15:41:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231295AbjFSPlj (ORCPT ); Mon, 19 Jun 2023 11:41:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231130AbjFSPli (ORCPT ); Mon, 19 Jun 2023 11:41:38 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.154.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BCC8113; Mon, 19 Jun 2023 08:41:36 -0700 (PDT) X-QQ-mid: bizesmtp75t1687189283tf03uy18 Received: from linux-lab-host.localdomain ( [116.30.126.60]) by bizesmtp.qq.com (ESMTP) with id ; Mon, 19 Jun 2023 23:41:21 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: 3M0okmaRx3jbjpkri1hCtt3wapIdtdZ3mKAKL3x4ywEH+9AAkaH2L1w9F6TnS Tx+YKnNgtl1OQjDw+EgPeDa3FaWoF9IQmEUTi2AyVScB6EvSGVTGHXGnIS67XikEy6+naJo fSmO3ZxxH1j3v3ekw8Pf+fz/3Vw5y0iNtBdxY5vVPRSdvVba2yz0+IlpPxN96v8lDQ9vEY2 zEO3bvznX+ArQ0IfNCgqqXbXZ6lLhLV+/FvfQ6IuoCN3z9HLoccn/YpEDOzgZ15ObHr8ldM 6JtYrJ077RhHpSdzMd4doDetReqFvlVxWZ5HY+fXN+9H53o+TN+P/bL9iFJv+xQQb9gYLW6 Nt2EHqyX1g14gbJiAKteWKJ+DTc5ksX0Nx4cmNTluBs3O+E5h5KOoLZ47NeDSnyQVQuzXij X-QQ-GoodBg: 0 X-BIZMAIL-ID: 11055691782021961800 From: Zhangjin Wu To: w@1wt.eu Cc: david.laight@aculab.com, arnd@arndb.de, falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, thomas@t-8ch.de, =?utf-8?q?Thomas_Wei?= =?utf-8?q?=C3=9Fschuh?= Subject: [PATCH v4 01/10] tools/nolibc: sys.h: add a syscall return helper Date: Mon, 19 Jun 2023 23:41:18 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Most of the library routines share the same syscall return logic: In general, a 0 return value indicates success. A -1 return value indicates an error, and an error number is stored in errno. [1] Let's add a __sysret() helper for the above logic to simplify the coding and shrink the code lines too. Thomas suggested to use inline function instead of macro for __sysret(). Willy suggested to make __sysret() be always inline. [1]: https://man7.org/linux/man-pages/man2/syscall.2.html Suggested-by: Willy Tarreau Link: https://lore.kernel.org/linux-riscv/ZH1+hkhiA2+ItSvX@1wt.eu/ Suggested-by: Thomas Weißschuh Link: https://lore.kernel.org/linux-riscv/ea4e7442-7223-4211-ba29-70821e907888@t-8ch.de/ Reviewed-by: Thomas Weißschuh Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 856249a11890..150777207468 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -28,6 +28,16 @@ #include "errno.h" #include "types.h" +/* Syscall return helper, set errno as -ret when ret < 0 */ +static __inline__ __attribute__((unused, always_inline)) +long __sysret(long ret) +{ + if (ret < 0) { + SET_ERRNO(-ret); + ret = -1; + } + return ret; +} /* Functions in this file only describe syscalls. They're declared static so * that the compiler usually decides to inline them while still being allowed