Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4786502rwd; Sun, 4 Jun 2023 12:54:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6JIiNuwr5Y17iq77UCmEoytj9zetIprdfV0ZPoqhYf5LeM/hfEfABSDGURPbmLiR4BXU3q X-Received: by 2002:a05:6a20:861e:b0:10c:8f0c:f81c with SMTP id l30-20020a056a20861e00b0010c8f0cf81cmr4333426pze.53.1685908470060; Sun, 04 Jun 2023 12:54:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685908470; cv=none; d=google.com; s=arc-20160816; b=s5o8CrQJa2SZITNZg4fC3N6RK6Qtabn8Jj+WHy+7xv+GpEHvdGGN1T3/fjNu7hJtb3 3YI1ZqAtqL7+j9hlRI6sYJpB7E3wPxdKpeE108R7McUdhBMJd/8NTIBQIFarZJOP/Ot6 QhUu5h8xsBwp3WU+r/f4j405uLUzNa97u90SxooPib1XjLRtK/omXGiIPZKiO3vjZLg3 j3mIViaRF2bdAZ1NE+ZOS+zTorEOxvvCAA9bqwCrVYykBCUnnOK9zlrM7yQFlPrS+HaX 13+SL8Y7kHVhZc4DzVcrDPJZgVt1Ep71ftNYSEMJDorRsZDKwSo4Yn+oVXPcSgX0nFof Bmhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=E6NsTRsJcfO4N6biRcgMr2CR887nwXEKSVwy3ZennbM=; b=QoiEAmn1IiDZpNLsxwYXb0vz7M1oP5PO6s96reLq/74aRPzp+u72cEcGXqtIttHmqa k3T6Jv43VbgzTeFmMgWXnY5/j0GlHxrbZveBYczvUVPgm36FhOGayxgxqsPy01W20OeZ b8hzN+NbO8J66wBoDLP8tcCJqh6K8qv9k/8r56dioLR6vaIugQohUWd64EwRU74irnay 0TeXd9SOyYe+lzfcqZMOKJ/i4M4eC+rBpJodQmPTc7hhOu8xfJT1nLNJzzS9qXQUli6E qfGYSzqLt7jkLN9go7JL8nXGzmnZ/v2kHnd2S2jKJkZs4vrBxkpn/jzNsJhUmOiMiuGk +Spg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@t-8ch.de header.s=mail header.b=MHF1QbHZ; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d10-20020a63360a000000b00537c6c285a0si4283504pga.167.2023.06.04.12.54.05; Sun, 04 Jun 2023 12:54:30 -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=@t-8ch.de header.s=mail header.b=MHF1QbHZ; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231308AbjFDTVP (ORCPT + 99 others); Sun, 4 Jun 2023 15:21:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbjFDTVO (ORCPT ); Sun, 4 Jun 2023 15:21:14 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6841A7; Sun, 4 Jun 2023 12:21:11 -0700 (PDT) Date: Sun, 4 Jun 2023 21:21:09 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=t-8ch.de; s=mail; t=1685906469; bh=1UClnzbPKFLgB8+m4Ye4NhmglZ0eNYJbm+haocS29Dk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MHF1QbHZGPslYjUKOjgngaUKFdFNWbjneJMTIO4Dx43CHu6UxHzxKpry/lClXyJ46 B+i7Qp5tyXzU3frQnpC5kEegItHMHDnt+hA4FcENSbs9tIhkbXpWVcTtcL7QbldFEs uo7Im8wBP/JfQph7F8Vf3H024I4lnFrqYV0HHSRA= From: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= To: Willy Tarreau Cc: Zhangjin Wu , arnd@arndb.de, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org Subject: Re: [PATCH 1/4] tools/nolibc: unistd.h: add __syscall() and __syscall_ret() helpers Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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, T_SCC_BODY_TEXT_LINE 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 On 2023-06-04 14:59:13+0200, Willy Tarreau wrote: > Hi Zhangjin, > > On Sun, Jun 04, 2023 at 01:34:29PM +0800, Zhangjin Wu wrote: > > most of the library routines share the same code model, let's add some > > macros to simplify the coding and shrink the code lines too. > > > > One added for syscall return, one added for syscall call, both of them > > can get the typeof 'return value' automatically. > > > > To get the return type of syscalls, __auto_type is better than typeof(), > > but it is not supported by the old compilers (before 2013, see [1]), so, > > use typeof() here. > > > > [1]: https://gcc.gnu.org/legacy-ml/gcc-patches/2013-11/msg01378.html > > > > Signed-off-by: Zhangjin Wu > > --- > > tools/include/nolibc/sys.h | 15 +++++++++++++++ > > 1 file changed, 15 insertions(+) > > > > diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h > > index 1d6f33f58629..937a8578e3d4 100644 > > --- a/tools/include/nolibc/sys.h > > +++ b/tools/include/nolibc/sys.h > > @@ -28,6 +28,21 @@ > > #include "errno.h" > > #include "types.h" > > > > +/* Syscall call and return helpers */ > > +#define __syscall_ret(ret) \ > > +({ \ > > + if (ret < 0) { \ > > + SET_ERRNO(-ret); \ > > + ret = (typeof(ret))-1; \ > > + } \ > > + ret; \ > > +}) > > + > > +#define __syscall(name, ...) \ > > +({ \ > > + typeof(sys_##name(__VA_ARGS__)) ret = sys_##name(__VA_ARGS__); \ > > + __syscall_ret(ret); \ > > +}) > > Well, I personally don't find that it increases legibility, on the > opposite. At first when reading the series, I thought you had dropped > errno setting on return. I think the reason is that when reading that > last macro, it's not at all obvious that __syscall_ret() is actually > modifying this ret value *and* returning it as the macro's result. > > If we'd want to go down that route, I suspect that something like this > would at least hint about what is being returned: > > +#define __syscall(name, ...) \ > +({ \ > + typeof(sys_##name(__VA_ARGS__)) ret = sys_##name(__VA_ARGS__); \ > + ret = __syscall_ret(ret); \ > +}) > > But I'm interested in others' opinion on this, particularly Thomas and > Arnd who review a significant number of patches. For now I prefer not > to take it before we've settled on a choice. While I see the value in factoring out this pattern I'm also not really happy with the implementation. Especially the magic delegation to "sys_##name". What about something like this: static inline long __ret_as_errno(long ret) /* or some other name */ { if (ret < 0) { SET_ERRNO(-ret); ret = -1; } return ret; } This avoids another macro by using a normal function. Syscall return values should always fit into long, at least extrapolating from syscall(2) and the fact that they are returned in registers. It would be a bit more verbose: int chdir(const char *path) { return __ret_as_errno(sys_chdir(path)); } But it's clear what's going on and also just one line. Thomas